evp_lib.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /*
  2. * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * EVP _meth_ APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "internal/cryptlib.h"
  17. #include <openssl/evp.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/objects.h>
  20. #include <openssl/params.h>
  21. #include <openssl/core_names.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/dh.h>
  24. #include <openssl/ec.h>
  25. #include "crypto/evp.h"
  26. #include "crypto/cryptlib.h"
  27. #include "internal/provider.h"
  28. #include "evp_local.h"
  29. #if !defined(FIPS_MODULE)
  30. # include "crypto/asn1.h"
  31. int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  32. {
  33. return evp_cipher_param_to_asn1_ex(c, type, NULL);
  34. }
  35. int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  36. {
  37. return evp_cipher_asn1_to_param_ex(c, type, NULL);
  38. }
  39. int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
  40. {
  41. int i = 0;
  42. unsigned int l;
  43. if (type != NULL) {
  44. unsigned char iv[EVP_MAX_IV_LENGTH];
  45. l = EVP_CIPHER_CTX_get_iv_length(ctx);
  46. if (!ossl_assert(l <= sizeof(iv)))
  47. return -1;
  48. i = ASN1_TYPE_get_octetstring(type, iv, l);
  49. if (i != (int)l)
  50. return -1;
  51. if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
  52. return -1;
  53. }
  54. return i;
  55. }
  56. int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  57. {
  58. int i = 0;
  59. unsigned int j;
  60. unsigned char *oiv = NULL;
  61. if (type != NULL) {
  62. oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
  63. j = EVP_CIPHER_CTX_get_iv_length(c);
  64. OPENSSL_assert(j <= sizeof(c->iv));
  65. i = ASN1_TYPE_set_octetstring(type, oiv, j);
  66. }
  67. return i;
  68. }
  69. int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  70. evp_cipher_aead_asn1_params *asn1_params)
  71. {
  72. int ret = -1; /* Assume the worst */
  73. const EVP_CIPHER *cipher;
  74. if (c == NULL || c->cipher == NULL)
  75. goto err;
  76. cipher = c->cipher;
  77. /*
  78. * For legacy implementations, we detect custom AlgorithmIdentifier
  79. * parameter handling by checking if the function pointer
  80. * cipher->set_asn1_parameters is set. We know that this pointer
  81. * is NULL for provided implementations.
  82. *
  83. * Otherwise, for any implementation, we check the flag
  84. * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
  85. * default AI parameter extraction.
  86. *
  87. * Otherwise, for provided implementations, we convert |type| to
  88. * a DER encoded blob and pass to the implementation in OSSL_PARAM
  89. * form.
  90. *
  91. * If none of the above applies, this operation is unsupported.
  92. */
  93. if (cipher->set_asn1_parameters != NULL) {
  94. ret = cipher->set_asn1_parameters(c, type);
  95. } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
  96. switch (EVP_CIPHER_get_mode(cipher)) {
  97. case EVP_CIPH_WRAP_MODE:
  98. if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
  99. ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
  100. ret = 1;
  101. break;
  102. case EVP_CIPH_GCM_MODE:
  103. ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
  104. break;
  105. case EVP_CIPH_CCM_MODE:
  106. case EVP_CIPH_XTS_MODE:
  107. case EVP_CIPH_OCB_MODE:
  108. ret = -2;
  109. break;
  110. default:
  111. ret = EVP_CIPHER_set_asn1_iv(c, type);
  112. }
  113. } else if (cipher->prov != NULL) {
  114. /* We cheat, there's no need for an object ID for this use */
  115. X509_ALGOR alg;
  116. alg.algorithm = NULL;
  117. alg.parameter = type;
  118. ret = EVP_CIPHER_CTX_get_algor_params(c, &alg);
  119. } else {
  120. ret = -2;
  121. }
  122. err:
  123. if (ret == -2)
  124. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  125. else if (ret <= 0)
  126. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  127. if (ret < -1)
  128. ret = -1;
  129. return ret;
  130. }
  131. int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  132. evp_cipher_aead_asn1_params *asn1_params)
  133. {
  134. int ret = -1; /* Assume the worst */
  135. const EVP_CIPHER *cipher;
  136. if (c == NULL || c->cipher == NULL)
  137. goto err;
  138. cipher = c->cipher;
  139. /*
  140. * For legacy implementations, we detect custom AlgorithmIdentifier
  141. * parameter handling by checking if there the function pointer
  142. * cipher->get_asn1_parameters is set. We know that this pointer
  143. * is NULL for provided implementations.
  144. *
  145. * Otherwise, for any implementation, we check the flag
  146. * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
  147. * default AI parameter creation.
  148. *
  149. * Otherwise, for provided implementations, we get the AI parameter
  150. * in DER encoded form from the implementation by requesting the
  151. * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
  152. *
  153. * If none of the above applies, this operation is unsupported.
  154. */
  155. if (cipher->get_asn1_parameters != NULL) {
  156. ret = cipher->get_asn1_parameters(c, type);
  157. } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
  158. switch (EVP_CIPHER_get_mode(cipher)) {
  159. case EVP_CIPH_WRAP_MODE:
  160. ret = 1;
  161. break;
  162. case EVP_CIPH_GCM_MODE:
  163. ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
  164. break;
  165. case EVP_CIPH_CCM_MODE:
  166. case EVP_CIPH_XTS_MODE:
  167. case EVP_CIPH_OCB_MODE:
  168. ret = -2;
  169. break;
  170. default:
  171. ret = EVP_CIPHER_get_asn1_iv(c, type) >= 0 ? 1 : -1;
  172. }
  173. } else if (cipher->prov != NULL) {
  174. /* We cheat, there's no need for an object ID for this use */
  175. X509_ALGOR alg;
  176. alg.algorithm = NULL;
  177. alg.parameter = type;
  178. ret = EVP_CIPHER_CTX_set_algor_params(c, &alg);
  179. } else {
  180. ret = -2;
  181. }
  182. err:
  183. if (ret == -2)
  184. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  185. else if (ret <= 0)
  186. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  187. if (ret < -1)
  188. ret = -1;
  189. return ret;
  190. }
  191. int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  192. evp_cipher_aead_asn1_params *asn1_params)
  193. {
  194. int i = 0;
  195. long tl;
  196. unsigned char iv[EVP_MAX_IV_LENGTH];
  197. if (type == NULL || asn1_params == NULL)
  198. return 0;
  199. i = ossl_asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
  200. if (i <= 0)
  201. return -1;
  202. ossl_asn1_type_get_octetstring_int(type, &tl, iv, i);
  203. memcpy(asn1_params->iv, iv, i);
  204. asn1_params->iv_len = i;
  205. return i;
  206. }
  207. int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  208. evp_cipher_aead_asn1_params *asn1_params)
  209. {
  210. if (type == NULL || asn1_params == NULL)
  211. return 0;
  212. return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
  213. asn1_params->iv,
  214. asn1_params->iv_len);
  215. }
  216. #endif /* !defined(FIPS_MODULE) */
  217. /* Convert the various cipher NIDs and dummies to a proper OID NID */
  218. int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
  219. {
  220. int nid;
  221. nid = EVP_CIPHER_get_nid(cipher);
  222. switch (nid) {
  223. case NID_rc2_cbc:
  224. case NID_rc2_64_cbc:
  225. case NID_rc2_40_cbc:
  226. return NID_rc2_cbc;
  227. case NID_rc4:
  228. case NID_rc4_40:
  229. return NID_rc4;
  230. case NID_aes_128_cfb128:
  231. case NID_aes_128_cfb8:
  232. case NID_aes_128_cfb1:
  233. return NID_aes_128_cfb128;
  234. case NID_aes_192_cfb128:
  235. case NID_aes_192_cfb8:
  236. case NID_aes_192_cfb1:
  237. return NID_aes_192_cfb128;
  238. case NID_aes_256_cfb128:
  239. case NID_aes_256_cfb8:
  240. case NID_aes_256_cfb1:
  241. return NID_aes_256_cfb128;
  242. case NID_des_cfb64:
  243. case NID_des_cfb8:
  244. case NID_des_cfb1:
  245. return NID_des_cfb64;
  246. case NID_des_ede3_cfb64:
  247. case NID_des_ede3_cfb8:
  248. case NID_des_ede3_cfb1:
  249. return NID_des_cfb64;
  250. default:
  251. #ifdef FIPS_MODULE
  252. return NID_undef;
  253. #else
  254. {
  255. /* Check it has an OID and it is valid */
  256. ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
  257. if (OBJ_get0_data(otmp) == NULL)
  258. nid = NID_undef;
  259. ASN1_OBJECT_free(otmp);
  260. return nid;
  261. }
  262. #endif
  263. }
  264. }
  265. int evp_cipher_cache_constants(EVP_CIPHER *cipher)
  266. {
  267. int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
  268. size_t ivlen = 0;
  269. size_t blksz = 0;
  270. size_t keylen = 0;
  271. unsigned int mode = 0;
  272. OSSL_PARAM params[10];
  273. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
  274. params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
  275. params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
  276. params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
  277. params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
  278. params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
  279. &custom_iv);
  280. params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
  281. params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
  282. &multiblock);
  283. params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
  284. &randkey);
  285. params[9] = OSSL_PARAM_construct_end();
  286. ok = evp_do_ciph_getparams(cipher, params) > 0;
  287. if (ok) {
  288. cipher->block_size = blksz;
  289. cipher->iv_len = ivlen;
  290. cipher->key_len = keylen;
  291. cipher->flags = mode;
  292. if (aead)
  293. cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
  294. if (custom_iv)
  295. cipher->flags |= EVP_CIPH_CUSTOM_IV;
  296. if (cts)
  297. cipher->flags |= EVP_CIPH_FLAG_CTS;
  298. if (multiblock)
  299. cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
  300. if (cipher->ccipher != NULL)
  301. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
  302. if (randkey)
  303. cipher->flags |= EVP_CIPH_RAND_KEY;
  304. if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
  305. OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
  306. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
  307. }
  308. return ok;
  309. }
  310. int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
  311. {
  312. return (cipher == NULL) ? 0 : cipher->block_size;
  313. }
  314. int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
  315. {
  316. return (ctx == NULL) ? 0 : EVP_CIPHER_get_block_size(ctx->cipher);
  317. }
  318. int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
  319. {
  320. return e->ctx_size;
  321. }
  322. int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  323. const unsigned char *in, unsigned int inl)
  324. {
  325. if (ctx == NULL || ctx->cipher == NULL)
  326. return 0;
  327. if (ctx->cipher->prov != NULL) {
  328. /*
  329. * If the provided implementation has a ccipher function, we use it,
  330. * and translate its return value like this: 0 => -1, 1 => outlen
  331. *
  332. * Otherwise, we call the cupdate function if in != NULL, or cfinal
  333. * if in == NULL. Regardless of which, we return what we got.
  334. */
  335. int ret = -1;
  336. size_t outl = 0;
  337. size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  338. if (blocksize == 0)
  339. return 0;
  340. if (ctx->cipher->ccipher != NULL)
  341. ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
  342. inl + (blocksize == 1 ? 0 : blocksize),
  343. in, (size_t)inl)
  344. ? (int)outl : -1;
  345. else if (in != NULL)
  346. ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
  347. inl + (blocksize == 1 ? 0 : blocksize),
  348. in, (size_t)inl);
  349. else
  350. ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
  351. blocksize == 1 ? 0 : blocksize);
  352. return ret;
  353. }
  354. return ctx->cipher->do_cipher(ctx, out, in, inl);
  355. }
  356. #ifndef OPENSSL_NO_DEPRECATED_3_0
  357. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
  358. {
  359. if (ctx == NULL)
  360. return NULL;
  361. return ctx->cipher;
  362. }
  363. #endif
  364. const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
  365. {
  366. if (ctx == NULL)
  367. return NULL;
  368. return ctx->cipher;
  369. }
  370. EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
  371. {
  372. EVP_CIPHER *cipher;
  373. if (ctx == NULL || ctx->cipher == NULL)
  374. return NULL;
  375. cipher = (EVP_CIPHER *)ctx->cipher;
  376. if (!EVP_CIPHER_up_ref(cipher))
  377. return NULL;
  378. return cipher;
  379. }
  380. int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
  381. {
  382. return ctx->encrypt;
  383. }
  384. unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
  385. {
  386. return cipher == NULL ? 0 : cipher->flags;
  387. }
  388. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
  389. {
  390. return ctx->app_data;
  391. }
  392. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
  393. {
  394. ctx->app_data = data;
  395. }
  396. void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
  397. {
  398. return ctx->cipher_data;
  399. }
  400. void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
  401. {
  402. void *old_cipher_data;
  403. old_cipher_data = ctx->cipher_data;
  404. ctx->cipher_data = cipher_data;
  405. return old_cipher_data;
  406. }
  407. int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
  408. {
  409. return (cipher == NULL) ? 0 : cipher->iv_len;
  410. }
  411. int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
  412. {
  413. if (ctx->cipher == NULL)
  414. return 0;
  415. if (ctx->iv_len < 0) {
  416. int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
  417. size_t v = len;
  418. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  419. if (ctx->cipher->get_ctx_params != NULL) {
  420. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
  421. &v);
  422. rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  423. if (rv > 0) {
  424. if (OSSL_PARAM_modified(params)
  425. && !OSSL_PARAM_get_int(params, &len))
  426. return -1;
  427. } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
  428. return -1;
  429. }
  430. }
  431. /* Code below to be removed when legacy support is dropped. */
  432. else if ((EVP_CIPHER_get_flags(ctx->cipher)
  433. & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
  434. rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
  435. 0, &len);
  436. if (rv <= 0)
  437. return -1;
  438. }
  439. /*-
  440. * Casting away the const is annoying but required here. We need to
  441. * cache the result for performance reasons.
  442. */
  443. ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
  444. }
  445. return ctx->iv_len;
  446. }
  447. int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
  448. {
  449. int ret;
  450. size_t v = 0;
  451. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  452. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
  453. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  454. return ret == 1 ? (int)v : 0;
  455. }
  456. #ifndef OPENSSL_NO_DEPRECATED_3_0
  457. const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
  458. {
  459. int ok;
  460. const unsigned char *v = ctx->oiv;
  461. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  462. params[0] =
  463. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
  464. (void **)&v, sizeof(ctx->oiv));
  465. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  466. return ok != 0 ? v : NULL;
  467. }
  468. /*
  469. * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
  470. */
  471. const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
  472. {
  473. int ok;
  474. const unsigned char *v = ctx->iv;
  475. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  476. params[0] =
  477. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  478. (void **)&v, sizeof(ctx->iv));
  479. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  480. return ok != 0 ? v : NULL;
  481. }
  482. unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
  483. {
  484. int ok;
  485. unsigned char *v = ctx->iv;
  486. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  487. params[0] =
  488. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  489. (void **)&v, sizeof(ctx->iv));
  490. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  491. return ok != 0 ? v : NULL;
  492. }
  493. #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
  494. int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  495. {
  496. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  497. params[0] =
  498. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
  499. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
  500. }
  501. int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  502. {
  503. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  504. params[0] =
  505. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
  506. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
  507. }
  508. unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
  509. {
  510. return ctx->buf;
  511. }
  512. int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
  513. {
  514. int ok;
  515. unsigned int v = (unsigned int)ctx->num;
  516. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  517. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
  518. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  519. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  520. }
  521. int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
  522. {
  523. int ok;
  524. unsigned int n = (unsigned int)num;
  525. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  526. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
  527. ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  528. if (ok != 0)
  529. ctx->num = (int)n;
  530. return ok != 0;
  531. }
  532. int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
  533. {
  534. return cipher->key_len;
  535. }
  536. int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
  537. {
  538. if (ctx->cipher == NULL)
  539. return 0;
  540. if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
  541. int ok;
  542. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  543. size_t len;
  544. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
  545. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  546. if (ok <= 0)
  547. return EVP_CTRL_RET_UNSUPPORTED;
  548. /*-
  549. * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
  550. * less than INT_MAX but best to be safe.
  551. *
  552. * Casting away the const is annoying but required here. We need to
  553. * cache the result for performance reasons.
  554. */
  555. if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
  556. return -1;
  557. ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
  558. }
  559. return ctx->key_len;
  560. }
  561. int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
  562. {
  563. return (cipher == NULL) ? NID_undef : cipher->nid;
  564. }
  565. int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
  566. {
  567. return EVP_CIPHER_get_nid(ctx->cipher);
  568. }
  569. int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
  570. {
  571. if (cipher == NULL)
  572. return 0;
  573. if (cipher->prov != NULL)
  574. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  575. return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
  576. }
  577. int evp_cipher_get_number(const EVP_CIPHER *cipher)
  578. {
  579. return cipher->name_id;
  580. }
  581. const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
  582. {
  583. if (cipher->type_name != NULL)
  584. return cipher->type_name;
  585. #ifndef FIPS_MODULE
  586. return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
  587. #else
  588. return NULL;
  589. #endif
  590. }
  591. const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
  592. {
  593. if (cipher->description != NULL)
  594. return cipher->description;
  595. #ifndef FIPS_MODULE
  596. return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
  597. #else
  598. return NULL;
  599. #endif
  600. }
  601. int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
  602. void (*fn)(const char *name, void *data),
  603. void *data)
  604. {
  605. if (cipher->prov != NULL)
  606. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  607. return 1;
  608. }
  609. const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
  610. {
  611. return cipher->prov;
  612. }
  613. int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
  614. {
  615. return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
  616. }
  617. int EVP_MD_is_a(const EVP_MD *md, const char *name)
  618. {
  619. if (md == NULL)
  620. return 0;
  621. if (md->prov != NULL)
  622. return evp_is_a(md->prov, md->name_id, NULL, name);
  623. return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
  624. }
  625. int evp_md_get_number(const EVP_MD *md)
  626. {
  627. return md->name_id;
  628. }
  629. const char *EVP_MD_get0_description(const EVP_MD *md)
  630. {
  631. if (md->description != NULL)
  632. return md->description;
  633. #ifndef FIPS_MODULE
  634. return OBJ_nid2ln(EVP_MD_nid(md));
  635. #else
  636. return NULL;
  637. #endif
  638. }
  639. const char *EVP_MD_get0_name(const EVP_MD *md)
  640. {
  641. if (md == NULL)
  642. return NULL;
  643. if (md->type_name != NULL)
  644. return md->type_name;
  645. #ifndef FIPS_MODULE
  646. return OBJ_nid2sn(EVP_MD_nid(md));
  647. #else
  648. return NULL;
  649. #endif
  650. }
  651. int EVP_MD_names_do_all(const EVP_MD *md,
  652. void (*fn)(const char *name, void *data),
  653. void *data)
  654. {
  655. if (md->prov != NULL)
  656. return evp_names_do_all(md->prov, md->name_id, fn, data);
  657. return 1;
  658. }
  659. const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
  660. {
  661. return md->prov;
  662. }
  663. int EVP_MD_get_type(const EVP_MD *md)
  664. {
  665. return md->type;
  666. }
  667. int EVP_MD_get_pkey_type(const EVP_MD *md)
  668. {
  669. return md->pkey_type;
  670. }
  671. int EVP_MD_get_block_size(const EVP_MD *md)
  672. {
  673. if (md == NULL) {
  674. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  675. return -1;
  676. }
  677. return md->block_size;
  678. }
  679. int EVP_MD_get_size(const EVP_MD *md)
  680. {
  681. if (md == NULL) {
  682. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  683. return -1;
  684. }
  685. return md->md_size;
  686. }
  687. int EVP_MD_xof(const EVP_MD *md)
  688. {
  689. return md != NULL && ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0);
  690. }
  691. unsigned long EVP_MD_get_flags(const EVP_MD *md)
  692. {
  693. return md->flags;
  694. }
  695. EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
  696. {
  697. EVP_MD *md = evp_md_new();
  698. if (md != NULL) {
  699. md->type = md_type;
  700. md->pkey_type = pkey_type;
  701. md->origin = EVP_ORIG_METH;
  702. }
  703. return md;
  704. }
  705. EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
  706. {
  707. EVP_MD *to = NULL;
  708. /*
  709. * Non-legacy EVP_MDs can't be duplicated like this.
  710. * Use EVP_MD_up_ref() instead.
  711. */
  712. if (md->prov != NULL)
  713. return NULL;
  714. if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
  715. CRYPTO_REF_COUNT refcnt = to->refcnt;
  716. memcpy(to, md, sizeof(*to));
  717. to->refcnt = refcnt;
  718. to->origin = EVP_ORIG_METH;
  719. }
  720. return to;
  721. }
  722. void evp_md_free_int(EVP_MD *md)
  723. {
  724. OPENSSL_free(md->type_name);
  725. ossl_provider_free(md->prov);
  726. CRYPTO_FREE_REF(&md->refcnt);
  727. OPENSSL_free(md);
  728. }
  729. void EVP_MD_meth_free(EVP_MD *md)
  730. {
  731. if (md == NULL || md->origin != EVP_ORIG_METH)
  732. return;
  733. evp_md_free_int(md);
  734. }
  735. int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
  736. {
  737. if (md->block_size != 0)
  738. return 0;
  739. md->block_size = blocksize;
  740. return 1;
  741. }
  742. int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
  743. {
  744. if (md->md_size != 0)
  745. return 0;
  746. md->md_size = resultsize;
  747. return 1;
  748. }
  749. int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
  750. {
  751. if (md->ctx_size != 0)
  752. return 0;
  753. md->ctx_size = datasize;
  754. return 1;
  755. }
  756. int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
  757. {
  758. if (md->flags != 0)
  759. return 0;
  760. md->flags = flags;
  761. return 1;
  762. }
  763. int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
  764. {
  765. if (md->init != NULL)
  766. return 0;
  767. md->init = init;
  768. return 1;
  769. }
  770. int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
  771. const void *data,
  772. size_t count))
  773. {
  774. if (md->update != NULL)
  775. return 0;
  776. md->update = update;
  777. return 1;
  778. }
  779. int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
  780. unsigned char *md))
  781. {
  782. if (md->final != NULL)
  783. return 0;
  784. md->final = final;
  785. return 1;
  786. }
  787. int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
  788. const EVP_MD_CTX *from))
  789. {
  790. if (md->copy != NULL)
  791. return 0;
  792. md->copy = copy;
  793. return 1;
  794. }
  795. int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
  796. {
  797. if (md->cleanup != NULL)
  798. return 0;
  799. md->cleanup = cleanup;
  800. return 1;
  801. }
  802. int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
  803. int p1, void *p2))
  804. {
  805. if (md->md_ctrl != NULL)
  806. return 0;
  807. md->md_ctrl = ctrl;
  808. return 1;
  809. }
  810. int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
  811. {
  812. return md->block_size;
  813. }
  814. int EVP_MD_meth_get_result_size(const EVP_MD *md)
  815. {
  816. return md->md_size;
  817. }
  818. int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
  819. {
  820. return md->ctx_size;
  821. }
  822. unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
  823. {
  824. return md->flags;
  825. }
  826. int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
  827. {
  828. return md->init;
  829. }
  830. int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
  831. const void *data,
  832. size_t count)
  833. {
  834. return md->update;
  835. }
  836. int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
  837. unsigned char *md)
  838. {
  839. return md->final;
  840. }
  841. int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
  842. const EVP_MD_CTX *from)
  843. {
  844. return md->copy;
  845. }
  846. int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
  847. {
  848. return md->cleanup;
  849. }
  850. int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
  851. int p1, void *p2)
  852. {
  853. return md->md_ctrl;
  854. }
  855. #ifndef OPENSSL_NO_DEPRECATED_3_0
  856. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
  857. {
  858. if (ctx == NULL)
  859. return NULL;
  860. return ctx->reqdigest;
  861. }
  862. #endif
  863. const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
  864. {
  865. if (ctx == NULL)
  866. return NULL;
  867. return ctx->reqdigest;
  868. }
  869. EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
  870. {
  871. EVP_MD *md;
  872. if (ctx == NULL)
  873. return NULL;
  874. md = (EVP_MD *)ctx->reqdigest;
  875. if (md == NULL || !EVP_MD_up_ref(md))
  876. return NULL;
  877. return md;
  878. }
  879. int EVP_MD_CTX_get_size_ex(const EVP_MD_CTX *ctx)
  880. {
  881. EVP_MD_CTX *c = (EVP_MD_CTX *)ctx;
  882. const OSSL_PARAM *gettables;
  883. gettables = EVP_MD_CTX_gettable_params(c);
  884. if (gettables != NULL
  885. && OSSL_PARAM_locate_const(gettables,
  886. OSSL_DIGEST_PARAM_SIZE) != NULL) {
  887. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  888. size_t sz = 0;
  889. /*
  890. * For XOF's EVP_MD_get_size() returns 0
  891. * So try to get the xoflen instead. This will return -1 if the
  892. * xof length has not been set.
  893. */
  894. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &sz);
  895. if (EVP_MD_CTX_get_params(c, params) != 1
  896. || sz == SIZE_MAX
  897. || sz == 0)
  898. return -1;
  899. return sz;
  900. }
  901. /* Normal digests have a constant fixed size output */
  902. return EVP_MD_get_size(EVP_MD_CTX_get0_md(ctx));
  903. }
  904. EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
  905. {
  906. return ctx->pctx;
  907. }
  908. #if !defined(FIPS_MODULE)
  909. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  910. {
  911. /*
  912. * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
  913. * we have to deal with the cleanup job here.
  914. */
  915. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  916. EVP_PKEY_CTX_free(ctx->pctx);
  917. ctx->pctx = pctx;
  918. if (pctx != NULL) {
  919. /* make sure pctx is not freed when destroying EVP_MD_CTX */
  920. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  921. } else {
  922. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  923. }
  924. }
  925. #endif /* !defined(FIPS_MODULE) */
  926. void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
  927. {
  928. return ctx->md_data;
  929. }
  930. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  931. const void *data, size_t count)
  932. {
  933. return ctx->update;
  934. }
  935. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  936. int (*update) (EVP_MD_CTX *ctx,
  937. const void *data, size_t count))
  938. {
  939. ctx->update = update;
  940. }
  941. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
  942. {
  943. ctx->flags |= flags;
  944. }
  945. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
  946. {
  947. ctx->flags &= ~flags;
  948. }
  949. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
  950. {
  951. return (ctx->flags & flags);
  952. }
  953. static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
  954. unsigned int enable)
  955. {
  956. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  957. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
  958. return EVP_CIPHER_CTX_set_params(ctx, params);
  959. }
  960. void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
  961. {
  962. int oldflags = ctx->flags;
  963. ctx->flags |= flags;
  964. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  965. evp_cipher_ctx_enable_use_bits(ctx, 1);
  966. }
  967. void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
  968. {
  969. int oldflags = ctx->flags;
  970. ctx->flags &= ~flags;
  971. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  972. evp_cipher_ctx_enable_use_bits(ctx, 0);
  973. }
  974. int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
  975. {
  976. return (ctx->flags & flags);
  977. }
  978. #if !defined(FIPS_MODULE)
  979. int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
  980. {
  981. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  982. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  983. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  984. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  985. return -2;
  986. }
  987. if (name == NULL)
  988. return -1;
  989. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  990. (char *)name, 0);
  991. return EVP_PKEY_CTX_set_params(ctx, params);
  992. }
  993. int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
  994. {
  995. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  996. OSSL_PARAM *p = params;
  997. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  998. /* There is no legacy support for this */
  999. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1000. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1001. return -2;
  1002. }
  1003. if (name == NULL)
  1004. return -1;
  1005. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1006. name, namelen);
  1007. if (!EVP_PKEY_CTX_get_params(ctx, params))
  1008. return -1;
  1009. return 1;
  1010. }
  1011. #endif /* !FIPS_MODULE */
  1012. /*
  1013. * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
  1014. * while providing a generic way of generating a new asymmetric key pair
  1015. * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
  1016. * The library context I<libctx> and property query I<propq>
  1017. * are used when fetching algorithms from providers.
  1018. * The I<params> specify algorithm-specific parameters
  1019. * such as the RSA modulus size or the name of an EC curve.
  1020. */
  1021. static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
  1022. const char *propq, const OSSL_PARAM *params)
  1023. {
  1024. EVP_PKEY *pkey = NULL;
  1025. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
  1026. if (ctx != NULL
  1027. && EVP_PKEY_keygen_init(ctx) > 0
  1028. && EVP_PKEY_CTX_set_params(ctx, params))
  1029. (void)EVP_PKEY_generate(ctx, &pkey);
  1030. EVP_PKEY_CTX_free(ctx);
  1031. return pkey;
  1032. }
  1033. EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
  1034. const char *type, ...)
  1035. {
  1036. va_list args;
  1037. size_t bits;
  1038. char *name;
  1039. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1040. EVP_PKEY *ret = NULL;
  1041. va_start(args, type);
  1042. if (OPENSSL_strcasecmp(type, "RSA") == 0) {
  1043. bits = va_arg(args, size_t);
  1044. params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
  1045. } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
  1046. name = va_arg(args, char *);
  1047. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1048. name, 0);
  1049. }
  1050. ret = evp_pkey_keygen(libctx, type, propq, params);
  1051. va_end(args);
  1052. return ret;
  1053. }
  1054. #if !defined(FIPS_MODULE)
  1055. int EVP_CIPHER_CTX_set_algor_params(EVP_CIPHER_CTX *ctx, const X509_ALGOR *alg)
  1056. {
  1057. int ret = -1; /* Assume the worst */
  1058. unsigned char *der = NULL;
  1059. int derl = -1;
  1060. if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
  1061. const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
  1062. const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
  1063. OSSL_PARAM params[3];
  1064. /*
  1065. * Passing the same data with both the old (deprecated) and the
  1066. * new AlgID parameters OSSL_PARAM key.
  1067. */
  1068. params[0] = OSSL_PARAM_construct_octet_string(k_old, der, (size_t)derl);
  1069. params[1] = OSSL_PARAM_construct_octet_string(k_new, der, (size_t)derl);
  1070. params[2] = OSSL_PARAM_construct_end();
  1071. ret = EVP_CIPHER_CTX_set_params(ctx, params);
  1072. }
  1073. OPENSSL_free(der);
  1074. return ret;
  1075. }
  1076. int EVP_CIPHER_CTX_get_algor_params(EVP_CIPHER_CTX *ctx, X509_ALGOR *alg)
  1077. {
  1078. int ret = -1; /* Assume the worst */
  1079. unsigned char *der = NULL;
  1080. size_t derl;
  1081. ASN1_TYPE *type = NULL;
  1082. int i = -1;
  1083. const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
  1084. const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
  1085. const char *derk;
  1086. OSSL_PARAM params[3];
  1087. /*
  1088. * We make two passes, the first to get the appropriate buffer size,
  1089. * and the second to get the actual value.
  1090. * Also, using both the old (deprecated) and the new AlgID parameters
  1091. * OSSL_PARAM key, and using whichever the provider responds to.
  1092. * Should the provider respond on both, the new key takes priority.
  1093. */
  1094. params[0] = OSSL_PARAM_construct_octet_string(k_old, NULL, 0);
  1095. params[1] = OSSL_PARAM_construct_octet_string(k_new, NULL, 0);
  1096. params[2] = OSSL_PARAM_construct_end();
  1097. if (!EVP_CIPHER_CTX_get_params(ctx, params))
  1098. goto err;
  1099. /* ... but, we should get a return size too! */
  1100. if (OSSL_PARAM_modified(&params[0]) && params[0].return_size != 0)
  1101. i = 0;
  1102. if (OSSL_PARAM_modified(&params[1]) && params[1].return_size != 0)
  1103. i = 1;
  1104. if (i < 0)
  1105. goto err;
  1106. /*
  1107. * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
  1108. * below. If it is NULL, the d2i_ASN1_TYPE() call will allocate new
  1109. * space for it. Either way, alg->parameter can be safely assigned
  1110. * with type after the d2i_ASN1_TYPE() call, with the safety that it
  1111. * will be ok.
  1112. */
  1113. type = alg->parameter;
  1114. derk = params[i].key;
  1115. derl = params[i].return_size;
  1116. if ((der = OPENSSL_malloc(derl)) != NULL) {
  1117. unsigned char *derp = der;
  1118. params[i] = OSSL_PARAM_construct_octet_string(derk, der, derl);
  1119. if (EVP_CIPHER_CTX_get_params(ctx, params)
  1120. && OSSL_PARAM_modified(&params[i])
  1121. && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
  1122. (int)derl) != NULL) {
  1123. /*
  1124. * Don't free alg->parameter, see comment further up.
  1125. * Worst case, alg->parameter gets assigned its own value.
  1126. */
  1127. alg->parameter = type;
  1128. ret = 1;
  1129. }
  1130. }
  1131. err:
  1132. OPENSSL_free(der);
  1133. return ret;
  1134. }
  1135. int EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX *ctx, X509_ALGOR **alg)
  1136. {
  1137. int ret = -1; /* Assume the worst */
  1138. OSSL_PARAM params[2];
  1139. size_t aid_len = 0;
  1140. const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
  1141. params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
  1142. params[1] = OSSL_PARAM_construct_end();
  1143. if (EVP_CIPHER_CTX_get_params(ctx, params) <= 0)
  1144. goto err;
  1145. if (OSSL_PARAM_modified(&params[0]))
  1146. aid_len = params[0].return_size;
  1147. if (aid_len == 0) {
  1148. ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
  1149. ret = -2;
  1150. goto err;
  1151. }
  1152. if (alg != NULL) {
  1153. unsigned char *aid = NULL;
  1154. const unsigned char *pp = NULL;
  1155. if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
  1156. params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
  1157. pp = aid;
  1158. if (EVP_CIPHER_CTX_get_params(ctx, params)
  1159. && OSSL_PARAM_modified(&params[0])
  1160. && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
  1161. ret = 1;
  1162. }
  1163. OPENSSL_free(aid);
  1164. }
  1165. err:
  1166. return ret;
  1167. }
  1168. int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg)
  1169. {
  1170. int ret = -1; /* Assume the worst */
  1171. unsigned char *der = NULL;
  1172. int derl = -1;
  1173. if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
  1174. const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
  1175. OSSL_PARAM params[2];
  1176. /*
  1177. * Passing the same data with both the old (deprecated) and the
  1178. * new AlgID parameters OSSL_PARAM key.
  1179. */
  1180. params[0] = OSSL_PARAM_construct_octet_string(k, der, (size_t)derl);
  1181. params[1] = OSSL_PARAM_construct_end();
  1182. ret = EVP_PKEY_CTX_set_params(ctx, params);
  1183. }
  1184. OPENSSL_free(der);
  1185. return ret;
  1186. }
  1187. int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg)
  1188. {
  1189. int ret = -1; /* Assume the worst */
  1190. OSSL_PARAM params[2];
  1191. unsigned char *der = NULL;
  1192. size_t derl;
  1193. ASN1_TYPE *type = NULL;
  1194. const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
  1195. /*
  1196. * We make two passes, the first to get the appropriate buffer size,
  1197. * and the second to get the actual value.
  1198. * Also, using both the old (deprecated) and the new AlgID parameters
  1199. * OSSL_PARAM key, and using whichever the provider responds to.
  1200. * Should the provider respond on both, the new key takes priority.
  1201. */
  1202. params[0] = OSSL_PARAM_construct_octet_string(k, NULL, 0);
  1203. params[1] = OSSL_PARAM_construct_end();
  1204. if (!EVP_PKEY_CTX_get_params(ctx, params))
  1205. goto err;
  1206. /*
  1207. * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
  1208. * below. If it is NULL, the d2i_ASN1_TYPE() call will allocate new
  1209. * space for it. Either way, alg->parameter can be safely assigned
  1210. * with type after the d2i_ASN1_TYPE() call, with the safety that it
  1211. * will be ok.
  1212. */
  1213. type = alg->parameter;
  1214. derl = params[0].return_size;
  1215. if (OSSL_PARAM_modified(&params[0])
  1216. /* ... but, we should get a return size too! */
  1217. && derl != 0
  1218. && (der = OPENSSL_malloc(derl)) != NULL) {
  1219. unsigned char *derp = der;
  1220. params[0] = OSSL_PARAM_construct_octet_string(k, der, derl);
  1221. if (EVP_PKEY_CTX_get_params(ctx, params)
  1222. && OSSL_PARAM_modified(&params[0])
  1223. && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
  1224. derl) != NULL) {
  1225. /*
  1226. * Don't free alg->parameter, see comment further up.
  1227. * Worst case, alg->parameter gets assigned its own value.
  1228. */
  1229. alg->parameter = type;
  1230. ret = 1;
  1231. }
  1232. }
  1233. err:
  1234. OPENSSL_free(der);
  1235. return ret;
  1236. }
  1237. int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg)
  1238. {
  1239. int ret = -1; /* Assume the worst */
  1240. OSSL_PARAM params[2];
  1241. size_t aid_len = 0;
  1242. const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
  1243. params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
  1244. params[1] = OSSL_PARAM_construct_end();
  1245. if (EVP_PKEY_CTX_get_params(ctx, params) <= 0)
  1246. goto err;
  1247. if (OSSL_PARAM_modified(&params[0]))
  1248. aid_len = params[0].return_size;
  1249. if (aid_len == 0) {
  1250. ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
  1251. ret = -2;
  1252. goto err;
  1253. }
  1254. if (alg != NULL) {
  1255. unsigned char *aid = NULL;
  1256. const unsigned char *pp = NULL;
  1257. if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
  1258. params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
  1259. pp = aid;
  1260. if (EVP_PKEY_CTX_get_params(ctx, params)
  1261. && OSSL_PARAM_modified(&params[0])
  1262. && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
  1263. ret = 1;
  1264. }
  1265. OPENSSL_free(aid);
  1266. }
  1267. err:
  1268. return ret;
  1269. }
  1270. #endif /* !defined(FIPS_MODULE) */