pmeth_lib.c 69 KB

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