pmeth_lib.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  1. /*
  2. * Copyright 2006-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. * Low level key APIs (DH etc) are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #ifndef FIPS_MODULE
  17. #include <openssl/engine.h>
  18. #endif
  19. #include <openssl/evp.h>
  20. #include <openssl/core_names.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/kdf.h>
  24. #include "internal/cryptlib.h"
  25. #ifndef FIPS_MODULE
  26. #include "crypto/asn1.h"
  27. #endif
  28. #include "crypto/evp.h"
  29. #include "crypto/dh.h"
  30. #include "crypto/ec.h"
  31. #include "internal/ffc.h"
  32. #include "internal/numbers.h"
  33. #include "internal/provider.h"
  34. #include "evp_local.h"
  35. #ifndef FIPS_MODULE
  36. static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
  37. int keytype, int optype,
  38. int cmd, const char *name,
  39. const void *data, size_t data_len);
  40. static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
  41. int cmd, const char *name);
  42. static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
  43. typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
  44. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  45. static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
  46. /* This array needs to be in order of NIDs */
  47. static pmeth_fn standard_methods[] = {
  48. ossl_rsa_pkey_method,
  49. #ifndef OPENSSL_NO_DH
  50. ossl_dh_pkey_method,
  51. #endif
  52. #ifndef OPENSSL_NO_DSA
  53. ossl_dsa_pkey_method,
  54. #endif
  55. #ifndef OPENSSL_NO_EC
  56. ossl_ec_pkey_method,
  57. #endif
  58. ossl_rsa_pss_pkey_method,
  59. #ifndef OPENSSL_NO_DH
  60. ossl_dhx_pkey_method,
  61. #endif
  62. #ifndef OPENSSL_NO_ECX
  63. ossl_ecx25519_pkey_method,
  64. ossl_ecx448_pkey_method,
  65. ossl_ed25519_pkey_method,
  66. ossl_ed448_pkey_method,
  67. #endif
  68. };
  69. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
  70. static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
  71. {
  72. return ((*a)->pkey_id - ((**b)())->pkey_id);
  73. }
  74. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
  75. static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
  76. const EVP_PKEY_METHOD *const *b)
  77. {
  78. return ((*a)->pkey_id - (*b)->pkey_id);
  79. }
  80. static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
  81. {
  82. if (app_pkey_methods != NULL) {
  83. int idx;
  84. EVP_PKEY_METHOD tmp;
  85. tmp.pkey_id = type;
  86. idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
  87. if (idx >= 0)
  88. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  89. }
  90. return NULL;
  91. }
  92. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
  93. {
  94. pmeth_fn *ret;
  95. EVP_PKEY_METHOD tmp;
  96. const EVP_PKEY_METHOD *t;
  97. if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
  98. return t;
  99. tmp.pkey_id = type;
  100. t = &tmp;
  101. ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
  102. OSSL_NELEM(standard_methods));
  103. if (ret == NULL || *ret == NULL)
  104. return NULL;
  105. return (**ret)();
  106. }
  107. EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
  108. {
  109. EVP_PKEY_METHOD *pmeth;
  110. pmeth = OPENSSL_zalloc(sizeof(*pmeth));
  111. if (pmeth == NULL)
  112. return NULL;
  113. pmeth->pkey_id = id;
  114. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  115. return pmeth;
  116. }
  117. #endif /* FIPS_MODULE */
  118. int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
  119. {
  120. if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
  121. return EVP_PKEY_STATE_UNKNOWN;
  122. if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  123. && ctx->op.kex.algctx != NULL)
  124. || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  125. && ctx->op.sig.algctx != NULL)
  126. || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  127. && ctx->op.ciph.algctx != NULL)
  128. || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  129. && ctx->op.keymgmt.genctx != NULL)
  130. || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  131. && ctx->op.encap.algctx != NULL))
  132. return EVP_PKEY_STATE_PROVIDER;
  133. return EVP_PKEY_STATE_LEGACY;
  134. }
  135. static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
  136. EVP_PKEY *pkey, ENGINE *e,
  137. const char *keytype, const char *propquery,
  138. int id)
  139. {
  140. EVP_PKEY_CTX *ret = NULL;
  141. const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
  142. EVP_KEYMGMT *keymgmt = NULL;
  143. /* Code below to be removed when legacy support is dropped. */
  144. /* BEGIN legacy */
  145. if (id == -1) {
  146. if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
  147. id = pkey->type;
  148. } else {
  149. if (pkey != NULL) {
  150. /* Must be provided if we get here */
  151. keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
  152. }
  153. #ifndef FIPS_MODULE
  154. if (keytype != NULL) {
  155. id = evp_pkey_name2type(keytype);
  156. if (id == NID_undef)
  157. id = -1;
  158. }
  159. #endif
  160. }
  161. }
  162. /* If no ID was found here, we can only resort to find a keymgmt */
  163. if (id == -1) {
  164. #ifndef FIPS_MODULE
  165. /* Using engine with a key without id will not work */
  166. if (e != NULL) {
  167. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  168. return NULL;
  169. }
  170. #endif
  171. goto common;
  172. }
  173. #ifndef FIPS_MODULE
  174. /*
  175. * Here, we extract what information we can for the purpose of
  176. * supporting usage with implementations from providers, to make
  177. * for a smooth transition from legacy stuff to provider based stuff.
  178. *
  179. * If an engine is given, this is entirely legacy, and we should not
  180. * pretend anything else, so we clear the name.
  181. */
  182. if (e != NULL)
  183. keytype = NULL;
  184. if (e == NULL && (pkey == NULL || pkey->foreign == 0))
  185. keytype = OBJ_nid2sn(id);
  186. #ifndef OPENSSL_NO_ENGINE
  187. if (e == NULL && pkey != NULL)
  188. e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
  189. /* Try to find an ENGINE which implements this method */
  190. if (e != NULL) {
  191. if (!ENGINE_init(e)) {
  192. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  193. return NULL;
  194. }
  195. } else {
  196. e = ENGINE_get_pkey_meth_engine(id);
  197. }
  198. /*
  199. * If an ENGINE handled this method look it up. Otherwise use internal
  200. * tables.
  201. */
  202. if (e != NULL)
  203. pmeth = ENGINE_get_pkey_meth(e, id);
  204. else
  205. #endif /* OPENSSL_NO_ENGINE */
  206. if (pkey != NULL && pkey->foreign)
  207. pmeth = EVP_PKEY_meth_find(id);
  208. else
  209. app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
  210. /* END legacy */
  211. #endif /* FIPS_MODULE */
  212. common:
  213. /*
  214. * If there's no engine and no app supplied pmeth and there's a name, we try
  215. * fetching a provider implementation.
  216. */
  217. if (e == NULL && app_pmeth == NULL && keytype != NULL) {
  218. /*
  219. * If |pkey| is given and is provided, we take a reference to its
  220. * keymgmt. Otherwise, we fetch one for the keytype we got. This
  221. * is to ensure that operation init functions can access what they
  222. * need through this single pointer.
  223. */
  224. if (pkey != NULL && pkey->keymgmt != NULL) {
  225. if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
  226. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  227. else
  228. keymgmt = pkey->keymgmt;
  229. } else {
  230. keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
  231. }
  232. if (keymgmt == NULL)
  233. return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
  234. #ifndef FIPS_MODULE
  235. /*
  236. * Chase down the legacy NID, as that might be needed for diverse
  237. * purposes, such as ensure that EVP_PKEY_type() can return sensible
  238. * values. We go through all keymgmt names, because the keytype
  239. * that's passed to this function doesn't necessarily translate
  240. * directly.
  241. */
  242. if (keymgmt != NULL) {
  243. int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt);
  244. if (tmp_id != NID_undef) {
  245. if (id == -1) {
  246. id = tmp_id;
  247. } else {
  248. /*
  249. * It really really shouldn't differ. If it still does,
  250. * something is very wrong.
  251. */
  252. if (!ossl_assert(id == tmp_id)) {
  253. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  254. EVP_KEYMGMT_free(keymgmt);
  255. return NULL;
  256. }
  257. }
  258. }
  259. }
  260. #endif
  261. }
  262. if (pmeth == NULL && keymgmt == NULL) {
  263. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  264. } else {
  265. ret = OPENSSL_zalloc(sizeof(*ret));
  266. }
  267. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  268. if ((ret == NULL || pmeth == NULL) && e != NULL)
  269. ENGINE_finish(e);
  270. #endif
  271. if (ret == NULL) {
  272. EVP_KEYMGMT_free(keymgmt);
  273. return NULL;
  274. }
  275. if (propquery != NULL) {
  276. ret->propquery = OPENSSL_strdup(propquery);
  277. if (ret->propquery == NULL) {
  278. OPENSSL_free(ret);
  279. EVP_KEYMGMT_free(keymgmt);
  280. return NULL;
  281. }
  282. }
  283. ret->libctx = libctx;
  284. ret->keytype = keytype;
  285. ret->keymgmt = keymgmt;
  286. ret->legacy_keytype = id;
  287. ret->engine = e;
  288. ret->pmeth = pmeth;
  289. ret->operation = EVP_PKEY_OP_UNDEFINED;
  290. if (pkey != NULL && !EVP_PKEY_up_ref(pkey)) {
  291. EVP_PKEY_CTX_free(ret);
  292. return NULL;
  293. }
  294. ret->pkey = pkey;
  295. if (pmeth != NULL && pmeth->init != NULL) {
  296. if (pmeth->init(ret) <= 0) {
  297. ret->pmeth = NULL;
  298. EVP_PKEY_CTX_free(ret);
  299. return NULL;
  300. }
  301. }
  302. return ret;
  303. }
  304. /*- All methods below can also be used in FIPS_MODULE */
  305. EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
  306. const char *name,
  307. const char *propquery)
  308. {
  309. return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
  310. }
  311. EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
  312. const char *propquery)
  313. {
  314. return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
  315. }
  316. void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
  317. {
  318. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  319. if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
  320. ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
  321. EVP_SIGNATURE_free(ctx->op.sig.signature);
  322. ctx->op.sig.algctx = NULL;
  323. ctx->op.sig.signature = NULL;
  324. } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  325. if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
  326. ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
  327. EVP_KEYEXCH_free(ctx->op.kex.exchange);
  328. ctx->op.kex.algctx = NULL;
  329. ctx->op.kex.exchange = NULL;
  330. } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  331. if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
  332. ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
  333. EVP_KEM_free(ctx->op.encap.kem);
  334. ctx->op.encap.algctx = NULL;
  335. ctx->op.encap.kem = NULL;
  336. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  337. if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
  338. ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
  339. EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
  340. ctx->op.ciph.algctx = NULL;
  341. ctx->op.ciph.cipher = NULL;
  342. } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  343. if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
  344. evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
  345. }
  346. }
  347. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  348. {
  349. if (ctx == NULL)
  350. return;
  351. if (ctx->pmeth && ctx->pmeth->cleanup)
  352. ctx->pmeth->cleanup(ctx);
  353. evp_pkey_ctx_free_old_ops(ctx);
  354. #ifndef FIPS_MODULE
  355. evp_pkey_ctx_free_all_cached_data(ctx);
  356. #endif
  357. EVP_KEYMGMT_free(ctx->keymgmt);
  358. OPENSSL_free(ctx->propquery);
  359. EVP_PKEY_free(ctx->pkey);
  360. EVP_PKEY_free(ctx->peerkey);
  361. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  362. ENGINE_finish(ctx->engine);
  363. #endif
  364. BN_free(ctx->rsa_pubexp);
  365. OPENSSL_free(ctx);
  366. }
  367. #ifndef FIPS_MODULE
  368. void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
  369. const EVP_PKEY_METHOD *meth)
  370. {
  371. if (ppkey_id)
  372. *ppkey_id = meth->pkey_id;
  373. if (pflags)
  374. *pflags = meth->flags;
  375. }
  376. void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
  377. {
  378. int pkey_id = dst->pkey_id;
  379. int flags = dst->flags;
  380. *dst = *src;
  381. /* We only copy the function pointers so restore the other values */
  382. dst->pkey_id = pkey_id;
  383. dst->flags = flags;
  384. }
  385. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  386. {
  387. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  388. OPENSSL_free(pmeth);
  389. }
  390. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  391. {
  392. return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
  393. }
  394. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  395. {
  396. return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
  397. }
  398. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
  399. {
  400. EVP_PKEY_CTX *rctx;
  401. #ifndef OPENSSL_NO_ENGINE
  402. /* Make sure it's safe to copy a pkey context using an ENGINE */
  403. if (pctx->engine && !ENGINE_init(pctx->engine)) {
  404. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  405. return 0;
  406. }
  407. #endif
  408. rctx = OPENSSL_zalloc(sizeof(*rctx));
  409. if (rctx == NULL)
  410. return NULL;
  411. if (pctx->pkey != NULL && !EVP_PKEY_up_ref(pctx->pkey))
  412. goto err;
  413. rctx->pkey = pctx->pkey;
  414. rctx->operation = pctx->operation;
  415. rctx->libctx = pctx->libctx;
  416. rctx->keytype = pctx->keytype;
  417. rctx->propquery = NULL;
  418. if (pctx->propquery != NULL) {
  419. rctx->propquery = OPENSSL_strdup(pctx->propquery);
  420. if (rctx->propquery == NULL)
  421. goto err;
  422. }
  423. rctx->legacy_keytype = pctx->legacy_keytype;
  424. if (pctx->keymgmt != NULL) {
  425. if (!EVP_KEYMGMT_up_ref(pctx->keymgmt))
  426. goto err;
  427. rctx->keymgmt = pctx->keymgmt;
  428. }
  429. if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
  430. if (pctx->op.kex.exchange != NULL) {
  431. rctx->op.kex.exchange = pctx->op.kex.exchange;
  432. if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
  433. goto err;
  434. }
  435. if (pctx->op.kex.algctx != NULL) {
  436. if (!ossl_assert(pctx->op.kex.exchange != NULL))
  437. goto err;
  438. if (pctx->op.kex.exchange->dupctx != NULL)
  439. rctx->op.kex.algctx
  440. = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
  441. if (rctx->op.kex.algctx == NULL) {
  442. EVP_KEYEXCH_free(rctx->op.kex.exchange);
  443. rctx->op.kex.exchange = NULL;
  444. goto err;
  445. }
  446. return rctx;
  447. }
  448. } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
  449. if (pctx->op.sig.signature != NULL) {
  450. rctx->op.sig.signature = pctx->op.sig.signature;
  451. if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
  452. goto err;
  453. }
  454. if (pctx->op.sig.algctx != NULL) {
  455. if (!ossl_assert(pctx->op.sig.signature != NULL))
  456. goto err;
  457. if (pctx->op.sig.signature->dupctx != NULL)
  458. rctx->op.sig.algctx
  459. = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
  460. if (rctx->op.sig.algctx == NULL) {
  461. EVP_SIGNATURE_free(rctx->op.sig.signature);
  462. rctx->op.sig.signature = NULL;
  463. goto err;
  464. }
  465. return rctx;
  466. }
  467. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
  468. if (pctx->op.ciph.cipher != NULL) {
  469. rctx->op.ciph.cipher = pctx->op.ciph.cipher;
  470. if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
  471. goto err;
  472. }
  473. if (pctx->op.ciph.algctx != NULL) {
  474. if (!ossl_assert(pctx->op.ciph.cipher != NULL))
  475. goto err;
  476. if (pctx->op.ciph.cipher->dupctx != NULL)
  477. rctx->op.ciph.algctx
  478. = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
  479. if (rctx->op.ciph.algctx == NULL) {
  480. EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
  481. rctx->op.ciph.cipher = NULL;
  482. goto err;
  483. }
  484. return rctx;
  485. }
  486. } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
  487. if (pctx->op.encap.kem != NULL) {
  488. rctx->op.encap.kem = pctx->op.encap.kem;
  489. if (!EVP_KEM_up_ref(rctx->op.encap.kem))
  490. goto err;
  491. }
  492. if (pctx->op.encap.algctx != NULL) {
  493. if (!ossl_assert(pctx->op.encap.kem != NULL))
  494. goto err;
  495. if (pctx->op.encap.kem->dupctx != NULL)
  496. rctx->op.encap.algctx
  497. = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
  498. if (rctx->op.encap.algctx == NULL) {
  499. EVP_KEM_free(rctx->op.encap.kem);
  500. rctx->op.encap.kem = NULL;
  501. goto err;
  502. }
  503. return rctx;
  504. }
  505. } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
  506. /* Not supported - This would need a gen_dupctx() to work */
  507. goto err;
  508. }
  509. rctx->pmeth = pctx->pmeth;
  510. #ifndef OPENSSL_NO_ENGINE
  511. rctx->engine = pctx->engine;
  512. #endif
  513. if (pctx->peerkey != NULL && !EVP_PKEY_up_ref(pctx->peerkey))
  514. goto err;
  515. rctx->peerkey = pctx->peerkey;
  516. if (pctx->pmeth == NULL) {
  517. if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
  518. EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
  519. void *provkey;
  520. if (pctx->pkey == NULL)
  521. return rctx;
  522. provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
  523. &tmp_keymgmt, pctx->propquery);
  524. if (provkey == NULL)
  525. goto err;
  526. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
  527. goto err;
  528. EVP_KEYMGMT_free(rctx->keymgmt);
  529. rctx->keymgmt = tmp_keymgmt;
  530. return rctx;
  531. }
  532. } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
  533. return rctx;
  534. }
  535. err:
  536. rctx->pmeth = NULL;
  537. EVP_PKEY_CTX_free(rctx);
  538. return NULL;
  539. }
  540. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  541. {
  542. if (app_pkey_methods == NULL) {
  543. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  544. if (app_pkey_methods == NULL) {
  545. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  546. return 0;
  547. }
  548. }
  549. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
  550. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  551. return 0;
  552. }
  553. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  554. return 1;
  555. }
  556. void evp_app_cleanup_int(void)
  557. {
  558. if (app_pkey_methods != NULL)
  559. sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
  560. }
  561. int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
  562. {
  563. const EVP_PKEY_METHOD *ret;
  564. ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
  565. return ret == NULL ? 0 : 1;
  566. }
  567. size_t EVP_PKEY_meth_get_count(void)
  568. {
  569. size_t rv = OSSL_NELEM(standard_methods);
  570. if (app_pkey_methods)
  571. rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
  572. return rv;
  573. }
  574. const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
  575. {
  576. if (idx < OSSL_NELEM(standard_methods))
  577. return (standard_methods[idx])();
  578. if (app_pkey_methods == NULL)
  579. return NULL;
  580. idx -= OSSL_NELEM(standard_methods);
  581. if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
  582. return NULL;
  583. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  584. }
  585. #endif
  586. int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
  587. {
  588. #ifndef FIPS_MODULE
  589. if (evp_pkey_ctx_is_legacy(ctx))
  590. return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
  591. #endif
  592. return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
  593. }
  594. int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
  595. {
  596. switch (evp_pkey_ctx_state(ctx)) {
  597. case EVP_PKEY_STATE_PROVIDER:
  598. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  599. && ctx->op.kex.exchange != NULL
  600. && ctx->op.kex.exchange->set_ctx_params != NULL)
  601. return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
  602. params);
  603. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  604. && ctx->op.sig.signature != NULL
  605. && ctx->op.sig.signature->set_ctx_params != NULL)
  606. return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
  607. params);
  608. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  609. && ctx->op.ciph.cipher != NULL
  610. && ctx->op.ciph.cipher->set_ctx_params != NULL)
  611. return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
  612. params);
  613. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  614. && ctx->keymgmt != NULL
  615. && ctx->keymgmt->gen_set_params != NULL)
  616. return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
  617. params);
  618. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  619. && ctx->op.encap.kem != NULL
  620. && ctx->op.encap.kem->set_ctx_params != NULL)
  621. return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
  622. params);
  623. break;
  624. case EVP_PKEY_STATE_UNKNOWN:
  625. break;
  626. #ifndef FIPS_MODULE
  627. case EVP_PKEY_STATE_LEGACY:
  628. return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
  629. #endif
  630. }
  631. return 0;
  632. }
  633. int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  634. {
  635. switch (evp_pkey_ctx_state(ctx)) {
  636. case EVP_PKEY_STATE_PROVIDER:
  637. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  638. && ctx->op.kex.exchange != NULL
  639. && ctx->op.kex.exchange->get_ctx_params != NULL)
  640. return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
  641. params);
  642. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  643. && ctx->op.sig.signature != NULL
  644. && ctx->op.sig.signature->get_ctx_params != NULL)
  645. return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
  646. params);
  647. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  648. && ctx->op.ciph.cipher != NULL
  649. && ctx->op.ciph.cipher->get_ctx_params != NULL)
  650. return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
  651. params);
  652. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  653. && ctx->op.encap.kem != NULL
  654. && ctx->op.encap.kem->get_ctx_params != NULL)
  655. return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
  656. params);
  657. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  658. && ctx->keymgmt != NULL
  659. && ctx->keymgmt->gen_get_params != NULL)
  660. return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
  661. params);
  662. break;
  663. case EVP_PKEY_STATE_UNKNOWN:
  664. break;
  665. #ifndef FIPS_MODULE
  666. case EVP_PKEY_STATE_LEGACY:
  667. return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
  668. #endif
  669. }
  670. return 0;
  671. }
  672. #ifndef FIPS_MODULE
  673. const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
  674. {
  675. void *provctx;
  676. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  677. && ctx->op.kex.exchange != NULL
  678. && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
  679. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
  680. return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
  681. provctx);
  682. }
  683. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  684. && ctx->op.sig.signature != NULL
  685. && ctx->op.sig.signature->gettable_ctx_params != NULL) {
  686. provctx = ossl_provider_ctx(
  687. EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
  688. return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
  689. provctx);
  690. }
  691. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  692. && ctx->op.ciph.cipher != NULL
  693. && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
  694. provctx = ossl_provider_ctx(
  695. EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
  696. return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
  697. provctx);
  698. }
  699. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  700. && ctx->op.encap.kem != NULL
  701. && ctx->op.encap.kem->gettable_ctx_params != NULL) {
  702. provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
  703. return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
  704. provctx);
  705. }
  706. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  707. && ctx->keymgmt != NULL
  708. && ctx->keymgmt->gen_gettable_params != NULL) {
  709. provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
  710. return ctx->keymgmt->gen_gettable_params(ctx->op.keymgmt.genctx,
  711. provctx);
  712. }
  713. return NULL;
  714. }
  715. const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
  716. {
  717. void *provctx;
  718. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  719. && ctx->op.kex.exchange != NULL
  720. && ctx->op.kex.exchange->settable_ctx_params != NULL) {
  721. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
  722. return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
  723. provctx);
  724. }
  725. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  726. && ctx->op.sig.signature != NULL
  727. && ctx->op.sig.signature->settable_ctx_params != NULL) {
  728. provctx = ossl_provider_ctx(
  729. EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
  730. return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
  731. provctx);
  732. }
  733. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  734. && ctx->op.ciph.cipher != NULL
  735. && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
  736. provctx = ossl_provider_ctx(
  737. EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
  738. return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
  739. provctx);
  740. }
  741. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  742. && ctx->keymgmt != NULL
  743. && ctx->keymgmt->gen_settable_params != NULL) {
  744. provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
  745. return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
  746. provctx);
  747. }
  748. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  749. && ctx->op.encap.kem != NULL
  750. && ctx->op.encap.kem->settable_ctx_params != NULL) {
  751. provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
  752. return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
  753. provctx);
  754. }
  755. return NULL;
  756. }
  757. /*
  758. * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
  759. *
  760. * Return 1 on success, 0 or negative for errors.
  761. *
  762. * In particular they return -2 if any of the params is not supported.
  763. *
  764. * They are not available in FIPS_MODULE as they depend on
  765. * - EVP_PKEY_CTX_{get,set}_params()
  766. * - EVP_PKEY_CTX_{gettable,settable}_params()
  767. *
  768. */
  769. int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  770. {
  771. if (ctx == NULL || params == NULL)
  772. return 0;
  773. /*
  774. * We only check for provider side EVP_PKEY_CTX. For #legacy, we
  775. * depend on the translation that happens in EVP_PKEY_CTX_set_params()
  776. * call, and that the resulting ctrl call will return -2 if it doesn't
  777. * known the ctrl command number.
  778. */
  779. if (evp_pkey_ctx_is_provided(ctx)) {
  780. const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
  781. const OSSL_PARAM *p;
  782. for (p = params; p->key != NULL; p++) {
  783. /* Check the ctx actually understands this parameter */
  784. if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
  785. return -2;
  786. }
  787. }
  788. return EVP_PKEY_CTX_set_params(ctx, params);
  789. }
  790. int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  791. {
  792. if (ctx == NULL || params == NULL)
  793. return 0;
  794. /*
  795. * We only check for provider side EVP_PKEY_CTX. For #legacy, we
  796. * depend on the translation that happens in EVP_PKEY_CTX_get_params()
  797. * call, and that the resulting ctrl call will return -2 if it doesn't
  798. * known the ctrl command number.
  799. */
  800. if (evp_pkey_ctx_is_provided(ctx)) {
  801. const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
  802. const OSSL_PARAM *p;
  803. for (p = params; p->key != NULL; p++) {
  804. /* Check the ctx actually understands this parameter */
  805. if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
  806. return -2;
  807. }
  808. }
  809. return EVP_PKEY_CTX_get_params(ctx, params);
  810. }
  811. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  812. {
  813. OSSL_PARAM sig_md_params[2], *p = sig_md_params;
  814. /* 80 should be big enough */
  815. char name[80] = "";
  816. const EVP_MD *tmp;
  817. if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  818. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  819. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  820. return -2;
  821. }
  822. if (ctx->op.sig.algctx == NULL)
  823. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
  824. EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
  825. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  826. name,
  827. sizeof(name));
  828. *p = OSSL_PARAM_construct_end();
  829. if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
  830. return 0;
  831. tmp = evp_get_digestbyname_ex(ctx->libctx, name);
  832. if (tmp == NULL)
  833. return 0;
  834. *md = tmp;
  835. return 1;
  836. }
  837. static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
  838. int fallback, const char *param, int op,
  839. int ctrl)
  840. {
  841. OSSL_PARAM md_params[2], *p = md_params;
  842. const char *name;
  843. if (ctx == NULL || (ctx->operation & op) == 0) {
  844. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  845. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  846. return -2;
  847. }
  848. if (fallback)
  849. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
  850. if (md == NULL) {
  851. name = "";
  852. } else {
  853. name = EVP_MD_get0_name(md);
  854. }
  855. *p++ = OSSL_PARAM_construct_utf8_string(param,
  856. /*
  857. * Cast away the const. This is read
  858. * only so should be safe
  859. */
  860. (char *)name, 0);
  861. *p = OSSL_PARAM_construct_end();
  862. return EVP_PKEY_CTX_set_params(ctx, md_params);
  863. }
  864. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  865. {
  866. return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
  867. OSSL_SIGNATURE_PARAM_DIGEST,
  868. EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
  869. }
  870. int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  871. {
  872. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  873. OSSL_KDF_PARAM_DIGEST,
  874. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
  875. }
  876. static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
  877. const char *param, int op, int ctrl,
  878. const unsigned char *data,
  879. int datalen)
  880. {
  881. OSSL_PARAM octet_string_params[2], *p = octet_string_params;
  882. if (ctx == NULL || (ctx->operation & op) == 0) {
  883. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  884. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  885. return -2;
  886. }
  887. /* Code below to be removed when legacy support is dropped. */
  888. if (fallback)
  889. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
  890. /* end of legacy support */
  891. if (datalen < 0) {
  892. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
  893. return 0;
  894. }
  895. *p++ = OSSL_PARAM_construct_octet_string(param,
  896. /*
  897. * Cast away the const. This is read
  898. * only so should be safe
  899. */
  900. (unsigned char *)data,
  901. (size_t)datalen);
  902. *p = OSSL_PARAM_construct_end();
  903. return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
  904. }
  905. static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
  906. const char *param, int op, int ctrl,
  907. const unsigned char *data,
  908. int datalen)
  909. {
  910. OSSL_PARAM os_params[2];
  911. const OSSL_PARAM *gettables;
  912. unsigned char *info = NULL;
  913. size_t info_len = 0;
  914. size_t info_alloc = 0;
  915. int ret = 0;
  916. if (ctx == NULL || (ctx->operation & op) == 0) {
  917. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  918. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  919. return -2;
  920. }
  921. /* Code below to be removed when legacy support is dropped. */
  922. if (fallback)
  923. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
  924. /* end of legacy support */
  925. if (datalen < 0) {
  926. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
  927. return 0;
  928. } else if (datalen == 0) {
  929. return 1;
  930. }
  931. /* Check for older provider that doesn't support getting this parameter */
  932. gettables = EVP_PKEY_CTX_gettable_params(ctx);
  933. if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
  934. return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
  935. data, datalen);
  936. /* Get the original value length */
  937. os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
  938. os_params[1] = OSSL_PARAM_construct_end();
  939. if (!EVP_PKEY_CTX_get_params(ctx, os_params))
  940. return 0;
  941. /* This should not happen but check to be sure. */
  942. if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
  943. return 0;
  944. info_alloc = os_params[0].return_size + datalen;
  945. if (info_alloc == 0)
  946. return 0;
  947. info = OPENSSL_zalloc(info_alloc);
  948. if (info == NULL)
  949. return 0;
  950. info_len = os_params[0].return_size;
  951. os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
  952. /* if we have data, then go get it */
  953. if (info_len > 0) {
  954. if (!EVP_PKEY_CTX_get_params(ctx, os_params))
  955. goto error;
  956. }
  957. /* Copy the input data */
  958. memcpy(&info[info_len], data, datalen);
  959. ret = EVP_PKEY_CTX_set_params(ctx, os_params);
  960. error:
  961. OPENSSL_clear_free(info, info_alloc);
  962. return ret;
  963. }
  964. int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
  965. const unsigned char *sec, int seclen)
  966. {
  967. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  968. OSSL_KDF_PARAM_SECRET,
  969. EVP_PKEY_OP_DERIVE,
  970. EVP_PKEY_CTRL_TLS_SECRET,
  971. sec, seclen);
  972. }
  973. int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
  974. const unsigned char *seed, int seedlen)
  975. {
  976. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  977. OSSL_KDF_PARAM_SEED,
  978. EVP_PKEY_OP_DERIVE,
  979. EVP_PKEY_CTRL_TLS_SEED,
  980. seed, seedlen);
  981. }
  982. int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  983. {
  984. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  985. OSSL_KDF_PARAM_DIGEST,
  986. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
  987. }
  988. int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
  989. const unsigned char *salt, int saltlen)
  990. {
  991. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  992. OSSL_KDF_PARAM_SALT,
  993. EVP_PKEY_OP_DERIVE,
  994. EVP_PKEY_CTRL_HKDF_SALT,
  995. salt, saltlen);
  996. }
  997. int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
  998. const unsigned char *key, int keylen)
  999. {
  1000. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1001. OSSL_KDF_PARAM_KEY,
  1002. EVP_PKEY_OP_DERIVE,
  1003. EVP_PKEY_CTRL_HKDF_KEY,
  1004. key, keylen);
  1005. }
  1006. int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
  1007. const unsigned char *info, int infolen)
  1008. {
  1009. return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1010. OSSL_KDF_PARAM_INFO,
  1011. EVP_PKEY_OP_DERIVE,
  1012. EVP_PKEY_CTRL_HKDF_INFO,
  1013. info, infolen);
  1014. }
  1015. int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
  1016. {
  1017. OSSL_PARAM int_params[2], *p = int_params;
  1018. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1019. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1020. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1021. return -2;
  1022. }
  1023. /* Code below to be removed when legacy support is dropped. */
  1024. if (ctx->op.kex.algctx == NULL)
  1025. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
  1026. EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
  1027. /* end of legacy support */
  1028. if (mode < 0) {
  1029. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  1030. return 0;
  1031. }
  1032. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
  1033. *p = OSSL_PARAM_construct_end();
  1034. return EVP_PKEY_CTX_set_params(ctx, int_params);
  1035. }
  1036. int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
  1037. int passlen)
  1038. {
  1039. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1040. OSSL_KDF_PARAM_PASSWORD,
  1041. EVP_PKEY_OP_DERIVE,
  1042. EVP_PKEY_CTRL_PASS,
  1043. (const unsigned char *)pass, passlen);
  1044. }
  1045. int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
  1046. const unsigned char *salt, int saltlen)
  1047. {
  1048. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1049. OSSL_KDF_PARAM_SALT,
  1050. EVP_PKEY_OP_DERIVE,
  1051. EVP_PKEY_CTRL_SCRYPT_SALT,
  1052. salt, saltlen);
  1053. }
  1054. static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
  1055. int op, int ctrl, uint64_t val)
  1056. {
  1057. OSSL_PARAM uint64_params[2], *p = uint64_params;
  1058. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1059. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1060. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1061. return -2;
  1062. }
  1063. /* Code below to be removed when legacy support is dropped. */
  1064. if (ctx->op.kex.algctx == NULL)
  1065. return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
  1066. /* end of legacy support */
  1067. *p++ = OSSL_PARAM_construct_uint64(param, &val);
  1068. *p = OSSL_PARAM_construct_end();
  1069. return EVP_PKEY_CTX_set_params(ctx, uint64_params);
  1070. }
  1071. int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
  1072. {
  1073. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
  1074. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
  1075. n);
  1076. }
  1077. int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
  1078. {
  1079. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
  1080. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
  1081. r);
  1082. }
  1083. int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
  1084. {
  1085. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
  1086. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
  1087. p);
  1088. }
  1089. int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
  1090. uint64_t maxmem_bytes)
  1091. {
  1092. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
  1093. EVP_PKEY_OP_DERIVE,
  1094. EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
  1095. maxmem_bytes);
  1096. }
  1097. int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
  1098. int keylen)
  1099. {
  1100. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
  1101. OSSL_PKEY_PARAM_PRIV_KEY,
  1102. EVP_PKEY_OP_KEYGEN,
  1103. EVP_PKEY_CTRL_SET_MAC_KEY,
  1104. key, keylen);
  1105. }
  1106. int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
  1107. {
  1108. OSSL_PARAM params[2], *p = params;
  1109. if (ctx == NULL || op == NULL) {
  1110. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  1111. return 0;
  1112. }
  1113. if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1114. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1115. return -2;
  1116. }
  1117. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
  1118. (char *)op, 0);
  1119. *p = OSSL_PARAM_construct_end();
  1120. return EVP_PKEY_CTX_set_params(ctx, params);
  1121. }
  1122. int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
  1123. {
  1124. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1125. EVP_PKEY_CTRL_SET1_ID, (int)len, (void *)(id));
  1126. }
  1127. int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
  1128. {
  1129. return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void *)id);
  1130. }
  1131. int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
  1132. {
  1133. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1134. EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void *)id_len);
  1135. }
  1136. static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1137. int cmd, int p1, void *p2)
  1138. {
  1139. int ret = 0;
  1140. /*
  1141. * If the method has a |digest_custom| function, we can relax the
  1142. * operation type check, since this can be called before the operation
  1143. * is initialized.
  1144. */
  1145. if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
  1146. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  1147. ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
  1148. return -1;
  1149. }
  1150. if ((optype != -1) && !(ctx->operation & optype)) {
  1151. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1152. return -1;
  1153. }
  1154. }
  1155. switch (evp_pkey_ctx_state(ctx)) {
  1156. case EVP_PKEY_STATE_PROVIDER:
  1157. return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
  1158. case EVP_PKEY_STATE_UNKNOWN:
  1159. case EVP_PKEY_STATE_LEGACY:
  1160. if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
  1161. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1162. return -2;
  1163. }
  1164. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  1165. return -1;
  1166. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  1167. if (ret == -2)
  1168. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1169. break;
  1170. }
  1171. return ret;
  1172. }
  1173. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1174. int cmd, int p1, void *p2)
  1175. {
  1176. int ret = 0;
  1177. if (ctx == NULL) {
  1178. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1179. return -2;
  1180. }
  1181. /* If unsupported, we don't want that reported here */
  1182. ERR_set_mark();
  1183. ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
  1184. cmd, NULL, p2, p1);
  1185. if (ret == -2) {
  1186. ERR_pop_to_mark();
  1187. } else {
  1188. ERR_clear_last_mark();
  1189. /*
  1190. * If there was an error, there was an error.
  1191. * If the operation isn't initialized yet, we also return, as
  1192. * the saved values will be used then anyway.
  1193. */
  1194. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1195. return ret;
  1196. }
  1197. return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
  1198. }
  1199. int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1200. int cmd, uint64_t value)
  1201. {
  1202. return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
  1203. }
  1204. static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
  1205. const char *name, const char *value)
  1206. {
  1207. int ret = 0;
  1208. if (ctx == NULL) {
  1209. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1210. return -2;
  1211. }
  1212. switch (evp_pkey_ctx_state(ctx)) {
  1213. case EVP_PKEY_STATE_PROVIDER:
  1214. return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
  1215. case EVP_PKEY_STATE_UNKNOWN:
  1216. case EVP_PKEY_STATE_LEGACY:
  1217. if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
  1218. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1219. return -2;
  1220. }
  1221. if (strcmp(name, "digest") == 0)
  1222. ret = EVP_PKEY_CTX_md(ctx,
  1223. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  1224. EVP_PKEY_CTRL_MD, value);
  1225. else
  1226. ret = ctx->pmeth->ctrl_str(ctx, name, value);
  1227. break;
  1228. }
  1229. return ret;
  1230. }
  1231. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  1232. const char *name, const char *value)
  1233. {
  1234. int ret = 0;
  1235. /* If unsupported, we don't want that reported here */
  1236. ERR_set_mark();
  1237. ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
  1238. name, value, strlen(value) + 1);
  1239. if (ret == -2) {
  1240. ERR_pop_to_mark();
  1241. } else {
  1242. ERR_clear_last_mark();
  1243. /*
  1244. * If there was an error, there was an error.
  1245. * If the operation isn't initialized yet, we also return, as
  1246. * the saved values will be used then anyway.
  1247. */
  1248. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1249. return ret;
  1250. }
  1251. return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
  1252. }
  1253. static int decode_cmd(int cmd, const char *name)
  1254. {
  1255. if (cmd == -1) {
  1256. /*
  1257. * The consequence of the assertion not being true is that this
  1258. * function will return -1, which will cause the calling functions
  1259. * to signal that the command is unsupported... in non-debug mode.
  1260. */
  1261. if (ossl_assert(name != NULL))
  1262. if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
  1263. cmd = EVP_PKEY_CTRL_SET1_ID;
  1264. }
  1265. return cmd;
  1266. }
  1267. static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
  1268. int keytype, int optype,
  1269. int cmd, const char *name,
  1270. const void *data, size_t data_len)
  1271. {
  1272. /*
  1273. * Check that it's one of the supported commands. The ctrl commands
  1274. * number cases here must correspond to the cases in the bottom switch
  1275. * in this function.
  1276. */
  1277. switch (cmd = decode_cmd(cmd, name)) {
  1278. case EVP_PKEY_CTRL_SET1_ID:
  1279. break;
  1280. default:
  1281. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1282. return -2;
  1283. }
  1284. if (keytype != -1) {
  1285. switch (evp_pkey_ctx_state(ctx)) {
  1286. case EVP_PKEY_STATE_PROVIDER:
  1287. if (ctx->keymgmt == NULL) {
  1288. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1289. return -2;
  1290. }
  1291. if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
  1292. evp_pkey_type2name(keytype))) {
  1293. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1294. return -1;
  1295. }
  1296. break;
  1297. case EVP_PKEY_STATE_UNKNOWN:
  1298. case EVP_PKEY_STATE_LEGACY:
  1299. if (ctx->pmeth == NULL) {
  1300. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1301. return -2;
  1302. }
  1303. if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
  1304. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1305. return -1;
  1306. }
  1307. break;
  1308. }
  1309. }
  1310. if (optype != -1 && (ctx->operation & optype) == 0) {
  1311. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1312. return -1;
  1313. }
  1314. switch (cmd) {
  1315. case EVP_PKEY_CTRL_SET1_ID:
  1316. evp_pkey_ctx_free_cached_data(ctx, cmd, name);
  1317. if (name != NULL) {
  1318. ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
  1319. if (ctx->cached_parameters.dist_id_name == NULL)
  1320. return 0;
  1321. }
  1322. if (data_len > 0) {
  1323. ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
  1324. if (ctx->cached_parameters.dist_id == NULL)
  1325. return 0;
  1326. }
  1327. ctx->cached_parameters.dist_id_set = 1;
  1328. ctx->cached_parameters.dist_id_len = data_len;
  1329. break;
  1330. }
  1331. return 1;
  1332. }
  1333. static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
  1334. int cmd, const char *name)
  1335. {
  1336. cmd = decode_cmd(cmd, name);
  1337. switch (cmd) {
  1338. case EVP_PKEY_CTRL_SET1_ID:
  1339. OPENSSL_free(ctx->cached_parameters.dist_id);
  1340. OPENSSL_free(ctx->cached_parameters.dist_id_name);
  1341. ctx->cached_parameters.dist_id = NULL;
  1342. ctx->cached_parameters.dist_id_name = NULL;
  1343. break;
  1344. }
  1345. }
  1346. static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
  1347. {
  1348. evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
  1349. }
  1350. int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
  1351. {
  1352. int ret = 1;
  1353. if (ret && ctx->cached_parameters.dist_id_set) {
  1354. const char *name = ctx->cached_parameters.dist_id_name;
  1355. const void *val = ctx->cached_parameters.dist_id;
  1356. size_t len = ctx->cached_parameters.dist_id_len;
  1357. if (name != NULL)
  1358. ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
  1359. else
  1360. ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
  1361. EVP_PKEY_CTRL_SET1_ID,
  1362. (int)len, (void *)val);
  1363. }
  1364. return ret;
  1365. }
  1366. OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
  1367. {
  1368. return ctx->libctx;
  1369. }
  1370. const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
  1371. {
  1372. return ctx->propquery;
  1373. }
  1374. const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
  1375. {
  1376. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1377. if (ctx->op.sig.signature != NULL)
  1378. return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
  1379. } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1380. if (ctx->op.kex.exchange != NULL)
  1381. return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
  1382. } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1383. if (ctx->op.encap.kem != NULL)
  1384. return EVP_KEM_get0_provider(ctx->op.encap.kem);
  1385. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  1386. if (ctx->op.ciph.cipher != NULL)
  1387. return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
  1388. } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1389. if (ctx->keymgmt != NULL)
  1390. return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
  1391. }
  1392. return NULL;
  1393. }
  1394. /* Utility functions to send a string of hex string to a ctrl */
  1395. int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
  1396. {
  1397. size_t len;
  1398. len = strlen(str);
  1399. if (len > INT_MAX)
  1400. return -1;
  1401. return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
  1402. }
  1403. int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
  1404. {
  1405. unsigned char *bin;
  1406. long binlen;
  1407. int rv = -1;
  1408. bin = OPENSSL_hexstr2buf(hex, &binlen);
  1409. if (bin == NULL)
  1410. return 0;
  1411. if (binlen <= INT_MAX)
  1412. rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
  1413. OPENSSL_free(bin);
  1414. return rv;
  1415. }
  1416. /* Pass a message digest to a ctrl */
  1417. int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
  1418. {
  1419. const EVP_MD *m;
  1420. if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
  1421. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
  1422. return 0;
  1423. }
  1424. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
  1425. }
  1426. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  1427. {
  1428. return ctx->operation;
  1429. }
  1430. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  1431. {
  1432. ctx->keygen_info = dat;
  1433. ctx->keygen_info_count = datlen;
  1434. }
  1435. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  1436. {
  1437. ctx->data = data;
  1438. }
  1439. void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
  1440. {
  1441. return ctx->data;
  1442. }
  1443. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  1444. {
  1445. return ctx->pkey;
  1446. }
  1447. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  1448. {
  1449. return ctx->peerkey;
  1450. }
  1451. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  1452. {
  1453. ctx->app_data = data;
  1454. }
  1455. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  1456. {
  1457. return ctx->app_data;
  1458. }
  1459. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  1460. int (*init)(EVP_PKEY_CTX *ctx))
  1461. {
  1462. pmeth->init = init;
  1463. }
  1464. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  1465. int (*copy)(EVP_PKEY_CTX *dst,
  1466. const EVP_PKEY_CTX *src))
  1467. {
  1468. pmeth->copy = copy;
  1469. }
  1470. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  1471. void (*cleanup)(EVP_PKEY_CTX *ctx))
  1472. {
  1473. pmeth->cleanup = cleanup;
  1474. }
  1475. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  1476. int (*paramgen_init)(EVP_PKEY_CTX *ctx),
  1477. int (*paramgen)(EVP_PKEY_CTX *ctx,
  1478. EVP_PKEY *pkey))
  1479. {
  1480. pmeth->paramgen_init = paramgen_init;
  1481. pmeth->paramgen = paramgen;
  1482. }
  1483. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  1484. int (*keygen_init)(EVP_PKEY_CTX *ctx),
  1485. int (*keygen)(EVP_PKEY_CTX *ctx,
  1486. EVP_PKEY *pkey))
  1487. {
  1488. pmeth->keygen_init = keygen_init;
  1489. pmeth->keygen = keygen;
  1490. }
  1491. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  1492. int (*sign_init)(EVP_PKEY_CTX *ctx),
  1493. int (*sign)(EVP_PKEY_CTX *ctx,
  1494. unsigned char *sig, size_t *siglen,
  1495. const unsigned char *tbs,
  1496. size_t tbslen))
  1497. {
  1498. pmeth->sign_init = sign_init;
  1499. pmeth->sign = sign;
  1500. }
  1501. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  1502. int (*verify_init)(EVP_PKEY_CTX *ctx),
  1503. int (*verify)(EVP_PKEY_CTX *ctx,
  1504. const unsigned char *sig,
  1505. size_t siglen,
  1506. const unsigned char *tbs,
  1507. size_t tbslen))
  1508. {
  1509. pmeth->verify_init = verify_init;
  1510. pmeth->verify = verify;
  1511. }
  1512. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  1513. int (*verify_recover_init)(EVP_PKEY_CTX
  1514. *ctx),
  1515. int (*verify_recover)(EVP_PKEY_CTX
  1516. *ctx,
  1517. unsigned char
  1518. *sig,
  1519. size_t *siglen,
  1520. const unsigned char *tbs,
  1521. size_t tbslen))
  1522. {
  1523. pmeth->verify_recover_init = verify_recover_init;
  1524. pmeth->verify_recover = verify_recover;
  1525. }
  1526. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  1527. int (*signctx_init)(EVP_PKEY_CTX *ctx,
  1528. EVP_MD_CTX *mctx),
  1529. int (*signctx)(EVP_PKEY_CTX *ctx,
  1530. unsigned char *sig,
  1531. size_t *siglen,
  1532. EVP_MD_CTX *mctx))
  1533. {
  1534. pmeth->signctx_init = signctx_init;
  1535. pmeth->signctx = signctx;
  1536. }
  1537. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  1538. int (*verifyctx_init)(EVP_PKEY_CTX *ctx,
  1539. EVP_MD_CTX *mctx),
  1540. int (*verifyctx)(EVP_PKEY_CTX *ctx,
  1541. const unsigned char *sig,
  1542. int siglen,
  1543. EVP_MD_CTX *mctx))
  1544. {
  1545. pmeth->verifyctx_init = verifyctx_init;
  1546. pmeth->verifyctx = verifyctx;
  1547. }
  1548. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  1549. int (*encrypt_init)(EVP_PKEY_CTX *ctx),
  1550. int (*encryptfn)(EVP_PKEY_CTX *ctx,
  1551. unsigned char *out,
  1552. size_t *outlen,
  1553. const unsigned char *in,
  1554. size_t inlen))
  1555. {
  1556. pmeth->encrypt_init = encrypt_init;
  1557. pmeth->encrypt = encryptfn;
  1558. }
  1559. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  1560. int (*decrypt_init)(EVP_PKEY_CTX *ctx),
  1561. int (*decrypt)(EVP_PKEY_CTX *ctx,
  1562. unsigned char *out,
  1563. size_t *outlen,
  1564. const unsigned char *in,
  1565. size_t inlen))
  1566. {
  1567. pmeth->decrypt_init = decrypt_init;
  1568. pmeth->decrypt = decrypt;
  1569. }
  1570. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  1571. int (*derive_init)(EVP_PKEY_CTX *ctx),
  1572. int (*derive)(EVP_PKEY_CTX *ctx,
  1573. unsigned char *key,
  1574. size_t *keylen))
  1575. {
  1576. pmeth->derive_init = derive_init;
  1577. pmeth->derive = derive;
  1578. }
  1579. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  1580. int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1,
  1581. void *p2),
  1582. int (*ctrl_str)(EVP_PKEY_CTX *ctx,
  1583. const char *type,
  1584. const char *value))
  1585. {
  1586. pmeth->ctrl = ctrl;
  1587. pmeth->ctrl_str = ctrl_str;
  1588. }
  1589. void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
  1590. int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1591. const unsigned char *tbs, size_t tbslen))
  1592. {
  1593. pmeth->digestsign = digestsign;
  1594. }
  1595. void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
  1596. int (*digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
  1597. size_t siglen, const unsigned char *tbs,
  1598. size_t tbslen))
  1599. {
  1600. pmeth->digestverify = digestverify;
  1601. }
  1602. void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
  1603. int (*check)(EVP_PKEY *pkey))
  1604. {
  1605. pmeth->check = check;
  1606. }
  1607. void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
  1608. int (*check)(EVP_PKEY *pkey))
  1609. {
  1610. pmeth->public_check = check;
  1611. }
  1612. void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
  1613. int (*check)(EVP_PKEY *pkey))
  1614. {
  1615. pmeth->param_check = check;
  1616. }
  1617. void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
  1618. int (*digest_custom)(EVP_PKEY_CTX *ctx,
  1619. EVP_MD_CTX *mctx))
  1620. {
  1621. pmeth->digest_custom = digest_custom;
  1622. }
  1623. void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
  1624. int (**pinit)(EVP_PKEY_CTX *ctx))
  1625. {
  1626. *pinit = pmeth->init;
  1627. }
  1628. void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
  1629. int (**pcopy)(EVP_PKEY_CTX *dst,
  1630. const EVP_PKEY_CTX *src))
  1631. {
  1632. *pcopy = pmeth->copy;
  1633. }
  1634. void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
  1635. void (**pcleanup)(EVP_PKEY_CTX *ctx))
  1636. {
  1637. *pcleanup = pmeth->cleanup;
  1638. }
  1639. void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
  1640. int (**pparamgen_init)(EVP_PKEY_CTX *ctx),
  1641. int (**pparamgen)(EVP_PKEY_CTX *ctx,
  1642. EVP_PKEY *pkey))
  1643. {
  1644. if (pparamgen_init)
  1645. *pparamgen_init = pmeth->paramgen_init;
  1646. if (pparamgen)
  1647. *pparamgen = pmeth->paramgen;
  1648. }
  1649. void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
  1650. int (**pkeygen_init)(EVP_PKEY_CTX *ctx),
  1651. int (**pkeygen)(EVP_PKEY_CTX *ctx,
  1652. EVP_PKEY *pkey))
  1653. {
  1654. if (pkeygen_init)
  1655. *pkeygen_init = pmeth->keygen_init;
  1656. if (pkeygen)
  1657. *pkeygen = pmeth->keygen;
  1658. }
  1659. void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
  1660. int (**psign_init)(EVP_PKEY_CTX *ctx),
  1661. int (**psign)(EVP_PKEY_CTX *ctx,
  1662. unsigned char *sig, size_t *siglen,
  1663. const unsigned char *tbs,
  1664. size_t tbslen))
  1665. {
  1666. if (psign_init)
  1667. *psign_init = pmeth->sign_init;
  1668. if (psign)
  1669. *psign = pmeth->sign;
  1670. }
  1671. void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
  1672. int (**pverify_init)(EVP_PKEY_CTX *ctx),
  1673. int (**pverify)(EVP_PKEY_CTX *ctx,
  1674. const unsigned char *sig,
  1675. size_t siglen,
  1676. const unsigned char *tbs,
  1677. size_t tbslen))
  1678. {
  1679. if (pverify_init)
  1680. *pverify_init = pmeth->verify_init;
  1681. if (pverify)
  1682. *pverify = pmeth->verify;
  1683. }
  1684. void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
  1685. int (**pverify_recover_init)(EVP_PKEY_CTX
  1686. *ctx),
  1687. int (**pverify_recover)(EVP_PKEY_CTX
  1688. *ctx,
  1689. unsigned char
  1690. *sig,
  1691. size_t *siglen,
  1692. const unsigned char *tbs,
  1693. size_t tbslen))
  1694. {
  1695. if (pverify_recover_init)
  1696. *pverify_recover_init = pmeth->verify_recover_init;
  1697. if (pverify_recover)
  1698. *pverify_recover = pmeth->verify_recover;
  1699. }
  1700. void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
  1701. int (**psignctx_init)(EVP_PKEY_CTX *ctx,
  1702. EVP_MD_CTX *mctx),
  1703. int (**psignctx)(EVP_PKEY_CTX *ctx,
  1704. unsigned char *sig,
  1705. size_t *siglen,
  1706. EVP_MD_CTX *mctx))
  1707. {
  1708. if (psignctx_init)
  1709. *psignctx_init = pmeth->signctx_init;
  1710. if (psignctx)
  1711. *psignctx = pmeth->signctx;
  1712. }
  1713. void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
  1714. int (**pverifyctx_init)(EVP_PKEY_CTX *ctx,
  1715. EVP_MD_CTX *mctx),
  1716. int (**pverifyctx)(EVP_PKEY_CTX *ctx,
  1717. const unsigned char *sig,
  1718. int siglen,
  1719. EVP_MD_CTX *mctx))
  1720. {
  1721. if (pverifyctx_init)
  1722. *pverifyctx_init = pmeth->verifyctx_init;
  1723. if (pverifyctx)
  1724. *pverifyctx = pmeth->verifyctx;
  1725. }
  1726. void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
  1727. int (**pencrypt_init)(EVP_PKEY_CTX *ctx),
  1728. int (**pencryptfn)(EVP_PKEY_CTX *ctx,
  1729. unsigned char *out,
  1730. size_t *outlen,
  1731. const unsigned char *in,
  1732. size_t inlen))
  1733. {
  1734. if (pencrypt_init)
  1735. *pencrypt_init = pmeth->encrypt_init;
  1736. if (pencryptfn)
  1737. *pencryptfn = pmeth->encrypt;
  1738. }
  1739. void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
  1740. int (**pdecrypt_init)(EVP_PKEY_CTX *ctx),
  1741. int (**pdecrypt)(EVP_PKEY_CTX *ctx,
  1742. unsigned char *out,
  1743. size_t *outlen,
  1744. const unsigned char *in,
  1745. size_t inlen))
  1746. {
  1747. if (pdecrypt_init)
  1748. *pdecrypt_init = pmeth->decrypt_init;
  1749. if (pdecrypt)
  1750. *pdecrypt = pmeth->decrypt;
  1751. }
  1752. void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
  1753. int (**pderive_init)(EVP_PKEY_CTX *ctx),
  1754. int (**pderive)(EVP_PKEY_CTX *ctx,
  1755. unsigned char *key,
  1756. size_t *keylen))
  1757. {
  1758. if (pderive_init)
  1759. *pderive_init = pmeth->derive_init;
  1760. if (pderive)
  1761. *pderive = pmeth->derive;
  1762. }
  1763. void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
  1764. int (**pctrl)(EVP_PKEY_CTX *ctx, int type, int p1,
  1765. void *p2),
  1766. int (**pctrl_str)(EVP_PKEY_CTX *ctx,
  1767. const char *type,
  1768. const char *value))
  1769. {
  1770. if (pctrl)
  1771. *pctrl = pmeth->ctrl;
  1772. if (pctrl_str)
  1773. *pctrl_str = pmeth->ctrl_str;
  1774. }
  1775. void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
  1776. int (**digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1777. const unsigned char *tbs, size_t tbslen))
  1778. {
  1779. if (digestsign)
  1780. *digestsign = pmeth->digestsign;
  1781. }
  1782. void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
  1783. int (**digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
  1784. size_t siglen, const unsigned char *tbs,
  1785. size_t tbslen))
  1786. {
  1787. if (digestverify)
  1788. *digestverify = pmeth->digestverify;
  1789. }
  1790. void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
  1791. int (**pcheck)(EVP_PKEY *pkey))
  1792. {
  1793. if (pcheck != NULL)
  1794. *pcheck = pmeth->check;
  1795. }
  1796. void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
  1797. int (**pcheck)(EVP_PKEY *pkey))
  1798. {
  1799. if (pcheck != NULL)
  1800. *pcheck = pmeth->public_check;
  1801. }
  1802. void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
  1803. int (**pcheck)(EVP_PKEY *pkey))
  1804. {
  1805. if (pcheck != NULL)
  1806. *pcheck = pmeth->param_check;
  1807. }
  1808. void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
  1809. int (**pdigest_custom)(EVP_PKEY_CTX *ctx,
  1810. EVP_MD_CTX *mctx))
  1811. {
  1812. if (pdigest_custom != NULL)
  1813. *pdigest_custom = pmeth->digest_custom;
  1814. }
  1815. #endif /* FIPS_MODULE */