evp_lib.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /*
  2. * Copyright 1995-2026 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, iv, EVP_MAX_IV_LENGTH);
  200. if (i <= 0 || i > EVP_MAX_IV_LENGTH)
  201. return -1;
  202. memcpy(asn1_params->iv, iv, i);
  203. asn1_params->iv_len = i;
  204. return i;
  205. }
  206. int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  207. evp_cipher_aead_asn1_params *asn1_params)
  208. {
  209. if (type == NULL || asn1_params == NULL)
  210. return 0;
  211. return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
  212. asn1_params->iv,
  213. asn1_params->iv_len);
  214. }
  215. #endif /* !defined(FIPS_MODULE) */
  216. /* Convert the various cipher NIDs and dummies to a proper OID NID */
  217. int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
  218. {
  219. int nid;
  220. nid = EVP_CIPHER_get_nid(cipher);
  221. switch (nid) {
  222. case NID_rc2_cbc:
  223. case NID_rc2_64_cbc:
  224. case NID_rc2_40_cbc:
  225. return NID_rc2_cbc;
  226. case NID_rc4:
  227. case NID_rc4_40:
  228. return NID_rc4;
  229. case NID_aes_128_cfb128:
  230. case NID_aes_128_cfb8:
  231. case NID_aes_128_cfb1:
  232. return NID_aes_128_cfb128;
  233. case NID_aes_192_cfb128:
  234. case NID_aes_192_cfb8:
  235. case NID_aes_192_cfb1:
  236. return NID_aes_192_cfb128;
  237. case NID_aes_256_cfb128:
  238. case NID_aes_256_cfb8:
  239. case NID_aes_256_cfb1:
  240. return NID_aes_256_cfb128;
  241. case NID_des_cfb64:
  242. case NID_des_cfb8:
  243. case NID_des_cfb1:
  244. return NID_des_cfb64;
  245. case NID_des_ede3_cfb64:
  246. case NID_des_ede3_cfb8:
  247. case NID_des_ede3_cfb1:
  248. return NID_des_cfb64;
  249. default:
  250. #ifdef FIPS_MODULE
  251. return NID_undef;
  252. #else
  253. {
  254. /* Check it has an OID and it is valid */
  255. ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
  256. if (OBJ_get0_data(otmp) == NULL)
  257. nid = NID_undef;
  258. ASN1_OBJECT_free(otmp);
  259. return nid;
  260. }
  261. #endif
  262. }
  263. }
  264. int evp_cipher_cache_constants(EVP_CIPHER *cipher)
  265. {
  266. int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
  267. size_t ivlen = 0;
  268. size_t blksz = 0;
  269. size_t keylen = 0;
  270. unsigned int mode = 0;
  271. OSSL_PARAM params[10];
  272. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
  273. params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
  274. params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
  275. params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
  276. params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
  277. params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
  278. &custom_iv);
  279. params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
  280. params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
  281. &multiblock);
  282. params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
  283. &randkey);
  284. params[9] = OSSL_PARAM_construct_end();
  285. ok = evp_do_ciph_getparams(cipher, params) > 0;
  286. if (ok) {
  287. cipher->block_size = blksz;
  288. cipher->iv_len = ivlen;
  289. cipher->key_len = keylen;
  290. cipher->flags = mode;
  291. if (aead)
  292. cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
  293. if (custom_iv)
  294. cipher->flags |= EVP_CIPH_CUSTOM_IV;
  295. if (cts)
  296. cipher->flags |= EVP_CIPH_FLAG_CTS;
  297. if (multiblock)
  298. cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
  299. if (cipher->ccipher != NULL)
  300. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
  301. if (randkey)
  302. cipher->flags |= EVP_CIPH_RAND_KEY;
  303. if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
  304. OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
  305. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
  306. }
  307. return ok;
  308. }
  309. int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
  310. {
  311. return (cipher == NULL) ? 0 : cipher->block_size;
  312. }
  313. int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
  314. {
  315. return (ctx == NULL) ? 0 : EVP_CIPHER_get_block_size(ctx->cipher);
  316. }
  317. int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
  318. {
  319. return e->ctx_size;
  320. }
  321. int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  322. const unsigned char *in, unsigned int inl)
  323. {
  324. if (ctx == NULL || ctx->cipher == NULL)
  325. return 0;
  326. if (ctx->cipher->prov != NULL) {
  327. /*
  328. * If the provided implementation has a ccipher function, we use it,
  329. * and translate its return value like this: 0 => -1, 1 => outlen
  330. *
  331. * Otherwise, we call the cupdate function if in != NULL, or cfinal
  332. * if in == NULL. Regardless of which, we return what we got.
  333. */
  334. int ret = -1;
  335. size_t outl = 0;
  336. size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  337. if (blocksize == 0)
  338. return 0;
  339. if (ctx->cipher->ccipher != NULL)
  340. ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
  341. inl + (blocksize == 1 ? 0 : blocksize),
  342. in, (size_t)inl)
  343. ? (int)outl
  344. : -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)
  434. != 0) {
  435. rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
  436. 0, &len);
  437. if (rv <= 0)
  438. return -1;
  439. }
  440. /*-
  441. * Casting away the const is annoying but required here. We need to
  442. * cache the result for performance reasons.
  443. */
  444. ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
  445. }
  446. return ctx->iv_len;
  447. }
  448. int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
  449. {
  450. int ret;
  451. size_t v = 0;
  452. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  453. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
  454. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  455. return ret == 1 ? (int)v : 0;
  456. }
  457. #ifndef OPENSSL_NO_DEPRECATED_3_0
  458. const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
  459. {
  460. int ok;
  461. const unsigned char *v = ctx->oiv;
  462. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  463. params[0] = 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] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  477. (void **)&v, sizeof(ctx->iv));
  478. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  479. return ok != 0 ? v : NULL;
  480. }
  481. unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
  482. {
  483. int ok;
  484. unsigned char *v = ctx->iv;
  485. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  486. params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  487. (void **)&v, sizeof(ctx->iv));
  488. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  489. return ok != 0 ? v : NULL;
  490. }
  491. #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
  492. int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  493. {
  494. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  495. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
  496. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
  497. }
  498. int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  499. {
  500. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  501. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
  502. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
  503. }
  504. unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
  505. {
  506. return ctx->buf;
  507. }
  508. int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
  509. {
  510. int ok;
  511. unsigned int v = (unsigned int)ctx->num;
  512. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  513. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
  514. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  515. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  516. }
  517. int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
  518. {
  519. int ok;
  520. unsigned int n = (unsigned int)num;
  521. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  522. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
  523. ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  524. if (ok != 0)
  525. ctx->num = (int)n;
  526. return ok != 0;
  527. }
  528. int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
  529. {
  530. return cipher->key_len;
  531. }
  532. int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
  533. {
  534. if (ctx->cipher == NULL)
  535. return 0;
  536. if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
  537. int ok;
  538. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  539. size_t len;
  540. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
  541. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  542. if (ok <= 0)
  543. return EVP_CTRL_RET_UNSUPPORTED;
  544. /*-
  545. * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
  546. * less than INT_MAX but best to be safe.
  547. *
  548. * Casting away the const is annoying but required here. We need to
  549. * cache the result for performance reasons.
  550. */
  551. if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
  552. return -1;
  553. ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
  554. }
  555. return ctx->key_len;
  556. }
  557. int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
  558. {
  559. return (cipher == NULL) ? NID_undef : cipher->nid;
  560. }
  561. int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
  562. {
  563. return EVP_CIPHER_get_nid(ctx->cipher);
  564. }
  565. int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
  566. {
  567. if (cipher == NULL)
  568. return 0;
  569. if (cipher->prov != NULL)
  570. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  571. return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
  572. }
  573. int evp_cipher_get_number(const EVP_CIPHER *cipher)
  574. {
  575. return cipher->name_id;
  576. }
  577. const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
  578. {
  579. if (cipher->type_name != NULL)
  580. return cipher->type_name;
  581. #ifndef FIPS_MODULE
  582. return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
  583. #else
  584. return NULL;
  585. #endif
  586. }
  587. const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
  588. {
  589. if (cipher->description != NULL)
  590. return cipher->description;
  591. #ifndef FIPS_MODULE
  592. return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
  593. #else
  594. return NULL;
  595. #endif
  596. }
  597. int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
  598. void (*fn)(const char *name, void *data),
  599. void *data)
  600. {
  601. if (cipher->prov != NULL)
  602. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  603. return 1;
  604. }
  605. const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
  606. {
  607. return cipher->prov;
  608. }
  609. int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
  610. {
  611. return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
  612. }
  613. int EVP_MD_is_a(const EVP_MD *md, const char *name)
  614. {
  615. if (md == NULL)
  616. return 0;
  617. if (md->prov != NULL)
  618. return evp_is_a(md->prov, md->name_id, NULL, name);
  619. return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
  620. }
  621. int evp_md_get_number(const EVP_MD *md)
  622. {
  623. return md->name_id;
  624. }
  625. const char *EVP_MD_get0_description(const EVP_MD *md)
  626. {
  627. if (md->description != NULL)
  628. return md->description;
  629. #ifndef FIPS_MODULE
  630. return OBJ_nid2ln(EVP_MD_nid(md));
  631. #else
  632. return NULL;
  633. #endif
  634. }
  635. const char *EVP_MD_get0_name(const EVP_MD *md)
  636. {
  637. if (md == NULL)
  638. return NULL;
  639. if (md->type_name != NULL)
  640. return md->type_name;
  641. #ifndef FIPS_MODULE
  642. return OBJ_nid2sn(EVP_MD_nid(md));
  643. #else
  644. return NULL;
  645. #endif
  646. }
  647. int EVP_MD_names_do_all(const EVP_MD *md,
  648. void (*fn)(const char *name, void *data),
  649. void *data)
  650. {
  651. if (md->prov != NULL)
  652. return evp_names_do_all(md->prov, md->name_id, fn, data);
  653. return 1;
  654. }
  655. const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
  656. {
  657. return md->prov;
  658. }
  659. int EVP_MD_get_type(const EVP_MD *md)
  660. {
  661. return md->type;
  662. }
  663. int EVP_MD_get_pkey_type(const EVP_MD *md)
  664. {
  665. return md->pkey_type;
  666. }
  667. int EVP_MD_get_block_size(const EVP_MD *md)
  668. {
  669. if (md == NULL) {
  670. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  671. return -1;
  672. }
  673. return md->block_size;
  674. }
  675. int EVP_MD_get_size(const EVP_MD *md)
  676. {
  677. if (md == NULL) {
  678. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  679. return -1;
  680. }
  681. return md->md_size;
  682. }
  683. int EVP_MD_xof(const EVP_MD *md)
  684. {
  685. return md != NULL && ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0);
  686. }
  687. unsigned long EVP_MD_get_flags(const EVP_MD *md)
  688. {
  689. return md->flags;
  690. }
  691. EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
  692. {
  693. EVP_MD *md = evp_md_new();
  694. if (md != NULL) {
  695. md->type = md_type;
  696. md->pkey_type = pkey_type;
  697. md->origin = EVP_ORIG_METH;
  698. }
  699. return md;
  700. }
  701. EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
  702. {
  703. EVP_MD *to = NULL;
  704. /*
  705. * Non-legacy EVP_MDs can't be duplicated like this.
  706. * Use EVP_MD_up_ref() instead.
  707. */
  708. if (md->prov != NULL)
  709. return NULL;
  710. if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
  711. CRYPTO_REF_COUNT refcnt = to->refcnt;
  712. memcpy(to, md, sizeof(*to));
  713. to->refcnt = refcnt;
  714. to->origin = EVP_ORIG_METH;
  715. }
  716. return to;
  717. }
  718. void evp_md_free_int(EVP_MD *md)
  719. {
  720. OPENSSL_free(md->type_name);
  721. ossl_provider_free(md->prov);
  722. CRYPTO_FREE_REF(&md->refcnt);
  723. OPENSSL_free(md);
  724. }
  725. void EVP_MD_meth_free(EVP_MD *md)
  726. {
  727. if (md == NULL || md->origin != EVP_ORIG_METH)
  728. return;
  729. evp_md_free_int(md);
  730. }
  731. int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
  732. {
  733. if (md->block_size != 0)
  734. return 0;
  735. md->block_size = blocksize;
  736. return 1;
  737. }
  738. int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
  739. {
  740. if (md->md_size != 0)
  741. return 0;
  742. md->md_size = resultsize;
  743. return 1;
  744. }
  745. int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
  746. {
  747. if (md->ctx_size != 0)
  748. return 0;
  749. md->ctx_size = datasize;
  750. return 1;
  751. }
  752. int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
  753. {
  754. if (md->flags != 0)
  755. return 0;
  756. md->flags = flags;
  757. return 1;
  758. }
  759. int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
  760. {
  761. if (md->init != NULL)
  762. return 0;
  763. md->init = init;
  764. return 1;
  765. }
  766. int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count))
  767. {
  768. if (md->update != NULL)
  769. return 0;
  770. md->update = update;
  771. return 1;
  772. }
  773. int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, unsigned char *md))
  774. {
  775. if (md->final != NULL)
  776. return 0;
  777. md->final = final;
  778. return 1;
  779. }
  780. int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from))
  781. {
  782. if (md->copy != NULL)
  783. return 0;
  784. md->copy = copy;
  785. return 1;
  786. }
  787. int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
  788. {
  789. if (md->cleanup != NULL)
  790. return 0;
  791. md->cleanup = cleanup;
  792. return 1;
  793. }
  794. int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2))
  795. {
  796. if (md->md_ctrl != NULL)
  797. return 0;
  798. md->md_ctrl = ctrl;
  799. return 1;
  800. }
  801. int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
  802. {
  803. return md->block_size;
  804. }
  805. int EVP_MD_meth_get_result_size(const EVP_MD *md)
  806. {
  807. return md->md_size;
  808. }
  809. int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
  810. {
  811. return md->ctx_size;
  812. }
  813. unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
  814. {
  815. return md->flags;
  816. }
  817. int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
  818. {
  819. return md->init;
  820. }
  821. int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
  822. const void *data,
  823. size_t count)
  824. {
  825. return md->update;
  826. }
  827. int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
  828. unsigned char *md)
  829. {
  830. return md->final;
  831. }
  832. int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
  833. const EVP_MD_CTX *from)
  834. {
  835. return md->copy;
  836. }
  837. int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
  838. {
  839. return md->cleanup;
  840. }
  841. int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
  842. int p1, void *p2)
  843. {
  844. return md->md_ctrl;
  845. }
  846. #ifndef OPENSSL_NO_DEPRECATED_3_0
  847. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
  848. {
  849. if (ctx == NULL)
  850. return NULL;
  851. return ctx->reqdigest;
  852. }
  853. #endif
  854. const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
  855. {
  856. if (ctx == NULL)
  857. return NULL;
  858. return ctx->reqdigest;
  859. }
  860. EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
  861. {
  862. EVP_MD *md;
  863. if (ctx == NULL)
  864. return NULL;
  865. md = (EVP_MD *)ctx->reqdigest;
  866. if (md == NULL || !EVP_MD_up_ref(md))
  867. return NULL;
  868. return md;
  869. }
  870. int EVP_MD_CTX_get_size_ex(const EVP_MD_CTX *ctx)
  871. {
  872. EVP_MD_CTX *c = (EVP_MD_CTX *)ctx;
  873. const OSSL_PARAM *gettables;
  874. gettables = EVP_MD_CTX_gettable_params(c);
  875. if (gettables != NULL
  876. && OSSL_PARAM_locate_const(gettables,
  877. OSSL_DIGEST_PARAM_SIZE)
  878. != NULL) {
  879. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  880. size_t sz = 0;
  881. /*
  882. * For XOF's EVP_MD_get_size() returns 0
  883. * So try to get the xoflen instead. This will return -1 if the
  884. * xof length has not been set.
  885. */
  886. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &sz);
  887. if (EVP_MD_CTX_get_params(c, params) != 1
  888. || sz == SIZE_MAX
  889. || sz == 0)
  890. return -1;
  891. return sz;
  892. }
  893. /* Normal digests have a constant fixed size output */
  894. return EVP_MD_get_size(EVP_MD_CTX_get0_md(ctx));
  895. }
  896. EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
  897. {
  898. return ctx->pctx;
  899. }
  900. #if !defined(FIPS_MODULE)
  901. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  902. {
  903. /*
  904. * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
  905. * we have to deal with the cleanup job here.
  906. */
  907. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  908. EVP_PKEY_CTX_free(ctx->pctx);
  909. ctx->pctx = pctx;
  910. if (pctx != NULL) {
  911. /* make sure pctx is not freed when destroying EVP_MD_CTX */
  912. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  913. } else {
  914. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  915. }
  916. }
  917. #endif /* !defined(FIPS_MODULE) */
  918. void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
  919. {
  920. return ctx->md_data;
  921. }
  922. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  923. const void *data, size_t count)
  924. {
  925. return ctx->update;
  926. }
  927. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  928. int (*update)(EVP_MD_CTX *ctx,
  929. const void *data, size_t count))
  930. {
  931. ctx->update = update;
  932. }
  933. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
  934. {
  935. ctx->flags |= flags;
  936. }
  937. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
  938. {
  939. ctx->flags &= ~flags;
  940. }
  941. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
  942. {
  943. return (ctx->flags & flags);
  944. }
  945. static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
  946. unsigned int enable)
  947. {
  948. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  949. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
  950. return EVP_CIPHER_CTX_set_params(ctx, params);
  951. }
  952. void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
  953. {
  954. int oldflags = ctx->flags;
  955. ctx->flags |= flags;
  956. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  957. evp_cipher_ctx_enable_use_bits(ctx, 1);
  958. }
  959. void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
  960. {
  961. int oldflags = ctx->flags;
  962. ctx->flags &= ~flags;
  963. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  964. evp_cipher_ctx_enable_use_bits(ctx, 0);
  965. }
  966. int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
  967. {
  968. return (ctx->flags & flags);
  969. }
  970. #if !defined(FIPS_MODULE)
  971. int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
  972. {
  973. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  974. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  975. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  976. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  977. return -2;
  978. }
  979. if (name == NULL)
  980. return -1;
  981. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  982. (char *)name, 0);
  983. return EVP_PKEY_CTX_set_params(ctx, params);
  984. }
  985. int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
  986. {
  987. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  988. OSSL_PARAM *p = params;
  989. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  990. /* There is no legacy support for this */
  991. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  992. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  993. return -2;
  994. }
  995. if (name == NULL)
  996. return -1;
  997. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  998. name, namelen);
  999. if (!EVP_PKEY_CTX_get_params(ctx, params))
  1000. return -1;
  1001. return 1;
  1002. }
  1003. #endif /* !FIPS_MODULE */
  1004. /*
  1005. * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
  1006. * while providing a generic way of generating a new asymmetric key pair
  1007. * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
  1008. * The library context I<libctx> and property query I<propq>
  1009. * are used when fetching algorithms from providers.
  1010. * The I<params> specify algorithm-specific parameters
  1011. * such as the RSA modulus size or the name of an EC curve.
  1012. */
  1013. static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
  1014. const char *propq, const OSSL_PARAM *params)
  1015. {
  1016. EVP_PKEY *pkey = NULL;
  1017. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
  1018. if (ctx != NULL
  1019. && EVP_PKEY_keygen_init(ctx) > 0
  1020. && EVP_PKEY_CTX_set_params(ctx, params))
  1021. (void)EVP_PKEY_generate(ctx, &pkey);
  1022. EVP_PKEY_CTX_free(ctx);
  1023. return pkey;
  1024. }
  1025. EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
  1026. const char *type, ...)
  1027. {
  1028. va_list args;
  1029. size_t bits;
  1030. char *name;
  1031. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1032. EVP_PKEY *ret = NULL;
  1033. va_start(args, type);
  1034. if (OPENSSL_strcasecmp(type, "RSA") == 0) {
  1035. bits = va_arg(args, size_t);
  1036. params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
  1037. } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
  1038. name = va_arg(args, char *);
  1039. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1040. name, 0);
  1041. }
  1042. ret = evp_pkey_keygen(libctx, type, propq, params);
  1043. va_end(args);
  1044. return ret;
  1045. }
  1046. #if !defined(FIPS_MODULE)
  1047. int EVP_CIPHER_CTX_set_algor_params(EVP_CIPHER_CTX *ctx, const X509_ALGOR *alg)
  1048. {
  1049. int ret = -1; /* Assume the worst */
  1050. unsigned char *der = NULL;
  1051. int derl = -1;
  1052. if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
  1053. const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
  1054. const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
  1055. OSSL_PARAM params[3];
  1056. /*
  1057. * Passing the same data with both the old (deprecated) and the
  1058. * new AlgID parameters OSSL_PARAM key.
  1059. */
  1060. params[0] = OSSL_PARAM_construct_octet_string(k_old, der, (size_t)derl);
  1061. params[1] = OSSL_PARAM_construct_octet_string(k_new, der, (size_t)derl);
  1062. params[2] = OSSL_PARAM_construct_end();
  1063. ret = EVP_CIPHER_CTX_set_params(ctx, params);
  1064. }
  1065. OPENSSL_free(der);
  1066. return ret;
  1067. }
  1068. int EVP_CIPHER_CTX_get_algor_params(EVP_CIPHER_CTX *ctx, X509_ALGOR *alg)
  1069. {
  1070. int ret = -1; /* Assume the worst */
  1071. unsigned char *der = NULL;
  1072. size_t derl;
  1073. ASN1_TYPE *type = NULL;
  1074. int i = -1;
  1075. const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
  1076. const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
  1077. const char *derk;
  1078. OSSL_PARAM params[3];
  1079. /*
  1080. * We make two passes, the first to get the appropriate buffer size,
  1081. * and the second to get the actual value.
  1082. * Also, using both the old (deprecated) and the new AlgID parameters
  1083. * OSSL_PARAM key, and using whichever the provider responds to.
  1084. * Should the provider respond on both, the new key takes priority.
  1085. */
  1086. params[0] = OSSL_PARAM_construct_octet_string(k_old, NULL, 0);
  1087. params[1] = OSSL_PARAM_construct_octet_string(k_new, NULL, 0);
  1088. params[2] = OSSL_PARAM_construct_end();
  1089. if (!EVP_CIPHER_CTX_get_params(ctx, params))
  1090. goto err;
  1091. /* ... but, we should get a return size too! */
  1092. if (OSSL_PARAM_modified(&params[0]) && params[0].return_size != 0)
  1093. i = 0;
  1094. if (OSSL_PARAM_modified(&params[1]) && params[1].return_size != 0)
  1095. i = 1;
  1096. if (i < 0)
  1097. goto err;
  1098. /*
  1099. * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
  1100. * below. If it is NULL, the d2i_ASN1_TYPE() call will allocate new
  1101. * space for it. Either way, alg->parameter can be safely assigned
  1102. * with type after the d2i_ASN1_TYPE() call, with the safety that it
  1103. * will be ok.
  1104. */
  1105. type = alg->parameter;
  1106. derk = params[i].key;
  1107. derl = params[i].return_size;
  1108. if ((der = OPENSSL_malloc(derl)) != NULL) {
  1109. unsigned char *derp = der;
  1110. params[i] = OSSL_PARAM_construct_octet_string(derk, der, derl);
  1111. if (EVP_CIPHER_CTX_get_params(ctx, params)
  1112. && OSSL_PARAM_modified(&params[i])
  1113. && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
  1114. (int)derl)
  1115. != NULL) {
  1116. /*
  1117. * Don't free alg->parameter, see comment further up.
  1118. * Worst case, alg->parameter gets assigned its own value.
  1119. */
  1120. alg->parameter = type;
  1121. ret = 1;
  1122. }
  1123. }
  1124. err:
  1125. OPENSSL_free(der);
  1126. return ret;
  1127. }
  1128. int EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX *ctx, X509_ALGOR **alg)
  1129. {
  1130. int ret = -1; /* Assume the worst */
  1131. OSSL_PARAM params[2];
  1132. size_t aid_len = 0;
  1133. const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
  1134. params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
  1135. params[1] = OSSL_PARAM_construct_end();
  1136. if (EVP_CIPHER_CTX_get_params(ctx, params) <= 0)
  1137. goto err;
  1138. if (OSSL_PARAM_modified(&params[0]))
  1139. aid_len = params[0].return_size;
  1140. if (aid_len == 0) {
  1141. ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
  1142. ret = -2;
  1143. goto err;
  1144. }
  1145. if (alg != NULL) {
  1146. unsigned char *aid = NULL;
  1147. const unsigned char *pp = NULL;
  1148. if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
  1149. params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
  1150. pp = aid;
  1151. if (EVP_CIPHER_CTX_get_params(ctx, params)
  1152. && OSSL_PARAM_modified(&params[0])
  1153. && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
  1154. ret = 1;
  1155. }
  1156. OPENSSL_free(aid);
  1157. }
  1158. err:
  1159. return ret;
  1160. }
  1161. int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg)
  1162. {
  1163. int ret = -1; /* Assume the worst */
  1164. unsigned char *der = NULL;
  1165. int derl = -1;
  1166. if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
  1167. const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
  1168. OSSL_PARAM params[2];
  1169. /*
  1170. * Passing the same data with both the old (deprecated) and the
  1171. * new AlgID parameters OSSL_PARAM key.
  1172. */
  1173. params[0] = OSSL_PARAM_construct_octet_string(k, der, (size_t)derl);
  1174. params[1] = OSSL_PARAM_construct_end();
  1175. ret = EVP_PKEY_CTX_set_params(ctx, params);
  1176. }
  1177. OPENSSL_free(der);
  1178. return ret;
  1179. }
  1180. int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg)
  1181. {
  1182. int ret = -1; /* Assume the worst */
  1183. OSSL_PARAM params[2];
  1184. unsigned char *der = NULL;
  1185. size_t derl;
  1186. ASN1_TYPE *type = NULL;
  1187. const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
  1188. /*
  1189. * We make two passes, the first to get the appropriate buffer size,
  1190. * and the second to get the actual value.
  1191. * Also, using both the old (deprecated) and the new AlgID parameters
  1192. * OSSL_PARAM key, and using whichever the provider responds to.
  1193. * Should the provider respond on both, the new key takes priority.
  1194. */
  1195. params[0] = OSSL_PARAM_construct_octet_string(k, NULL, 0);
  1196. params[1] = OSSL_PARAM_construct_end();
  1197. if (!EVP_PKEY_CTX_get_params(ctx, params))
  1198. goto err;
  1199. /*
  1200. * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
  1201. * below. If it is NULL, the d2i_ASN1_TYPE() call will allocate new
  1202. * space for it. Either way, alg->parameter can be safely assigned
  1203. * with type after the d2i_ASN1_TYPE() call, with the safety that it
  1204. * will be ok.
  1205. */
  1206. type = alg->parameter;
  1207. derl = params[0].return_size;
  1208. if (OSSL_PARAM_modified(&params[0])
  1209. /* ... but, we should get a return size too! */
  1210. && derl != 0
  1211. && (der = OPENSSL_malloc(derl)) != NULL) {
  1212. unsigned char *derp = der;
  1213. params[0] = OSSL_PARAM_construct_octet_string(k, der, derl);
  1214. if (EVP_PKEY_CTX_get_params(ctx, params)
  1215. && OSSL_PARAM_modified(&params[0])
  1216. && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
  1217. derl)
  1218. != NULL) {
  1219. /*
  1220. * Don't free alg->parameter, see comment further up.
  1221. * Worst case, alg->parameter gets assigned its own value.
  1222. */
  1223. alg->parameter = type;
  1224. ret = 1;
  1225. }
  1226. }
  1227. err:
  1228. OPENSSL_free(der);
  1229. return ret;
  1230. }
  1231. int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg)
  1232. {
  1233. int ret = -1; /* Assume the worst */
  1234. OSSL_PARAM params[2];
  1235. size_t aid_len = 0;
  1236. const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
  1237. params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
  1238. params[1] = OSSL_PARAM_construct_end();
  1239. if (EVP_PKEY_CTX_get_params(ctx, params) <= 0)
  1240. goto err;
  1241. if (OSSL_PARAM_modified(&params[0]))
  1242. aid_len = params[0].return_size;
  1243. if (aid_len == 0) {
  1244. ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
  1245. ret = -2;
  1246. goto err;
  1247. }
  1248. if (alg != NULL) {
  1249. unsigned char *aid = NULL;
  1250. const unsigned char *pp = NULL;
  1251. if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
  1252. params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
  1253. pp = aid;
  1254. if (EVP_PKEY_CTX_get_params(ctx, params)
  1255. && OSSL_PARAM_modified(&params[0])
  1256. && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
  1257. ret = 1;
  1258. }
  1259. OPENSSL_free(aid);
  1260. }
  1261. err:
  1262. return ret;
  1263. }
  1264. #endif /* !defined(FIPS_MODULE) */