pmeth_lib.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /*
  2. * Copyright 2006-2022 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. rctx->op.kex.algctx
  454. = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
  455. if (rctx->op.kex.algctx == NULL) {
  456. EVP_KEYEXCH_free(rctx->op.kex.exchange);
  457. rctx->op.kex.exchange = NULL;
  458. goto err;
  459. }
  460. return rctx;
  461. }
  462. } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
  463. if (pctx->op.sig.signature != NULL) {
  464. rctx->op.sig.signature = pctx->op.sig.signature;
  465. if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
  466. goto err;
  467. }
  468. if (pctx->op.sig.algctx != NULL) {
  469. if (!ossl_assert(pctx->op.sig.signature != NULL))
  470. goto err;
  471. rctx->op.sig.algctx
  472. = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
  473. if (rctx->op.sig.algctx == NULL) {
  474. EVP_SIGNATURE_free(rctx->op.sig.signature);
  475. rctx->op.sig.signature = NULL;
  476. goto err;
  477. }
  478. return rctx;
  479. }
  480. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
  481. if (pctx->op.ciph.cipher != NULL) {
  482. rctx->op.ciph.cipher = pctx->op.ciph.cipher;
  483. if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
  484. goto err;
  485. }
  486. if (pctx->op.ciph.algctx != NULL) {
  487. if (!ossl_assert(pctx->op.ciph.cipher != NULL))
  488. goto err;
  489. rctx->op.ciph.algctx
  490. = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
  491. if (rctx->op.ciph.algctx == NULL) {
  492. EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
  493. rctx->op.ciph.cipher = NULL;
  494. goto err;
  495. }
  496. return rctx;
  497. }
  498. } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
  499. if (pctx->op.encap.kem != NULL) {
  500. rctx->op.encap.kem = pctx->op.encap.kem;
  501. if (!EVP_KEM_up_ref(rctx->op.encap.kem))
  502. goto err;
  503. }
  504. if (pctx->op.encap.algctx != NULL) {
  505. if (!ossl_assert(pctx->op.encap.kem != NULL))
  506. goto err;
  507. rctx->op.encap.algctx
  508. = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
  509. if (rctx->op.encap.algctx == NULL) {
  510. EVP_KEM_free(rctx->op.encap.kem);
  511. rctx->op.encap.kem = NULL;
  512. goto err;
  513. }
  514. return rctx;
  515. }
  516. } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
  517. /* Not supported - This would need a gen_dupctx() to work */
  518. goto err;
  519. }
  520. rctx->pmeth = pctx->pmeth;
  521. # ifndef OPENSSL_NO_ENGINE
  522. rctx->engine = pctx->engine;
  523. # endif
  524. if (pctx->peerkey != NULL)
  525. EVP_PKEY_up_ref(pctx->peerkey);
  526. rctx->peerkey = pctx->peerkey;
  527. if (pctx->pmeth == NULL) {
  528. if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
  529. EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
  530. void *provkey;
  531. provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
  532. &tmp_keymgmt, pctx->propquery);
  533. if (provkey == NULL)
  534. goto err;
  535. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
  536. goto err;
  537. EVP_KEYMGMT_free(rctx->keymgmt);
  538. rctx->keymgmt = tmp_keymgmt;
  539. return rctx;
  540. }
  541. } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
  542. return rctx;
  543. }
  544. err:
  545. rctx->pmeth = NULL;
  546. EVP_PKEY_CTX_free(rctx);
  547. return NULL;
  548. }
  549. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  550. {
  551. if (app_pkey_methods == NULL) {
  552. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  553. if (app_pkey_methods == NULL) {
  554. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  555. return 0;
  556. }
  557. }
  558. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
  559. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  560. return 0;
  561. }
  562. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  563. return 1;
  564. }
  565. void evp_app_cleanup_int(void)
  566. {
  567. if (app_pkey_methods != NULL)
  568. sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
  569. }
  570. int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
  571. {
  572. const EVP_PKEY_METHOD *ret;
  573. ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
  574. return ret == NULL ? 0 : 1;
  575. }
  576. size_t EVP_PKEY_meth_get_count(void)
  577. {
  578. size_t rv = OSSL_NELEM(standard_methods);
  579. if (app_pkey_methods)
  580. rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
  581. return rv;
  582. }
  583. const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
  584. {
  585. if (idx < OSSL_NELEM(standard_methods))
  586. return (standard_methods[idx])();
  587. if (app_pkey_methods == NULL)
  588. return NULL;
  589. idx -= OSSL_NELEM(standard_methods);
  590. if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
  591. return NULL;
  592. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  593. }
  594. #endif
  595. int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
  596. {
  597. #ifndef FIPS_MODULE
  598. if (evp_pkey_ctx_is_legacy(ctx))
  599. return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
  600. #endif
  601. return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
  602. }
  603. int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
  604. {
  605. switch (evp_pkey_ctx_state(ctx)) {
  606. case EVP_PKEY_STATE_PROVIDER:
  607. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  608. && ctx->op.kex.exchange != NULL
  609. && ctx->op.kex.exchange->set_ctx_params != NULL)
  610. return
  611. ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
  612. params);
  613. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  614. && ctx->op.sig.signature != NULL
  615. && ctx->op.sig.signature->set_ctx_params != NULL)
  616. return
  617. ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
  618. params);
  619. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  620. && ctx->op.ciph.cipher != NULL
  621. && ctx->op.ciph.cipher->set_ctx_params != NULL)
  622. return
  623. ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
  624. params);
  625. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  626. && ctx->keymgmt != NULL
  627. && ctx->keymgmt->gen_set_params != NULL)
  628. return
  629. evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
  630. params);
  631. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  632. && ctx->op.encap.kem != NULL
  633. && ctx->op.encap.kem->set_ctx_params != NULL)
  634. return
  635. ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
  636. params);
  637. break;
  638. #ifndef FIPS_MODULE
  639. case EVP_PKEY_STATE_UNKNOWN:
  640. case EVP_PKEY_STATE_LEGACY:
  641. return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
  642. #endif
  643. }
  644. return 0;
  645. }
  646. int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  647. {
  648. switch (evp_pkey_ctx_state(ctx)) {
  649. case EVP_PKEY_STATE_PROVIDER:
  650. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  651. && ctx->op.kex.exchange != NULL
  652. && ctx->op.kex.exchange->get_ctx_params != NULL)
  653. return
  654. ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
  655. params);
  656. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  657. && ctx->op.sig.signature != NULL
  658. && ctx->op.sig.signature->get_ctx_params != NULL)
  659. return
  660. ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
  661. params);
  662. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  663. && ctx->op.ciph.cipher != NULL
  664. && ctx->op.ciph.cipher->get_ctx_params != NULL)
  665. return
  666. ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
  667. params);
  668. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  669. && ctx->op.encap.kem != NULL
  670. && ctx->op.encap.kem->get_ctx_params != NULL)
  671. return
  672. ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
  673. params);
  674. break;
  675. #ifndef FIPS_MODULE
  676. case EVP_PKEY_STATE_UNKNOWN:
  677. case EVP_PKEY_STATE_LEGACY:
  678. return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
  679. #endif
  680. }
  681. return 0;
  682. }
  683. #ifndef FIPS_MODULE
  684. const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
  685. {
  686. void *provctx;
  687. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  688. && ctx->op.kex.exchange != NULL
  689. && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
  690. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
  691. return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
  692. provctx);
  693. }
  694. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  695. && ctx->op.sig.signature != NULL
  696. && ctx->op.sig.signature->gettable_ctx_params != NULL) {
  697. provctx = ossl_provider_ctx(
  698. EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
  699. return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
  700. provctx);
  701. }
  702. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  703. && ctx->op.ciph.cipher != NULL
  704. && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
  705. provctx = ossl_provider_ctx(
  706. EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
  707. return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
  708. provctx);
  709. }
  710. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  711. && ctx->op.encap.kem != NULL
  712. && ctx->op.encap.kem->gettable_ctx_params != NULL) {
  713. provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
  714. return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
  715. provctx);
  716. }
  717. return NULL;
  718. }
  719. const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
  720. {
  721. void *provctx;
  722. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  723. && ctx->op.kex.exchange != NULL
  724. && ctx->op.kex.exchange->settable_ctx_params != NULL) {
  725. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
  726. return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
  727. provctx);
  728. }
  729. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  730. && ctx->op.sig.signature != NULL
  731. && ctx->op.sig.signature->settable_ctx_params != NULL) {
  732. provctx = ossl_provider_ctx(
  733. EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
  734. return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
  735. provctx);
  736. }
  737. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  738. && ctx->op.ciph.cipher != NULL
  739. && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
  740. provctx = ossl_provider_ctx(
  741. EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
  742. return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
  743. provctx);
  744. }
  745. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  746. && ctx->keymgmt != NULL
  747. && ctx->keymgmt->gen_settable_params != NULL) {
  748. provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
  749. return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
  750. provctx);
  751. }
  752. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  753. && ctx->op.encap.kem != NULL
  754. && ctx->op.encap.kem->settable_ctx_params != NULL) {
  755. provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
  756. return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
  757. provctx);
  758. }
  759. return NULL;
  760. }
  761. /*
  762. * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
  763. *
  764. * Return 1 on success, 0 or negative for errors.
  765. *
  766. * In particular they return -2 if any of the params is not supported.
  767. *
  768. * They are not available in FIPS_MODULE as they depend on
  769. * - EVP_PKEY_CTX_{get,set}_params()
  770. * - EVP_PKEY_CTX_{gettable,settable}_params()
  771. *
  772. */
  773. int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  774. {
  775. if (ctx == NULL || params == NULL)
  776. return 0;
  777. /*
  778. * We only check for provider side EVP_PKEY_CTX. For #legacy, we
  779. * depend on the translation that happens in EVP_PKEY_CTX_set_params()
  780. * call, and that the resulting ctrl call will return -2 if it doesn't
  781. * known the ctrl command number.
  782. */
  783. if (evp_pkey_ctx_is_provided(ctx)) {
  784. const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
  785. const OSSL_PARAM *p;
  786. for (p = params; p->key != NULL; p++) {
  787. /* Check the ctx actually understands this parameter */
  788. if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
  789. return -2;
  790. }
  791. }
  792. return EVP_PKEY_CTX_set_params(ctx, params);
  793. }
  794. int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  795. {
  796. if (ctx == NULL || params == NULL)
  797. return 0;
  798. /*
  799. * We only check for provider side EVP_PKEY_CTX. For #legacy, we
  800. * depend on the translation that happens in EVP_PKEY_CTX_get_params()
  801. * call, and that the resulting ctrl call will return -2 if it doesn't
  802. * known the ctrl command number.
  803. */
  804. if (evp_pkey_ctx_is_provided(ctx)) {
  805. const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
  806. const OSSL_PARAM *p;
  807. for (p = params; p->key != NULL; p++) {
  808. /* Check the ctx actually understands this parameter */
  809. if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
  810. return -2;
  811. }
  812. }
  813. return EVP_PKEY_CTX_get_params(ctx, params);
  814. }
  815. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  816. {
  817. OSSL_PARAM sig_md_params[2], *p = sig_md_params;
  818. /* 80 should be big enough */
  819. char name[80] = "";
  820. const EVP_MD *tmp;
  821. if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  822. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  823. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  824. return -2;
  825. }
  826. if (ctx->op.sig.algctx == NULL)
  827. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
  828. EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
  829. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  830. name,
  831. sizeof(name));
  832. *p = OSSL_PARAM_construct_end();
  833. if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
  834. return 0;
  835. tmp = evp_get_digestbyname_ex(ctx->libctx, name);
  836. if (tmp == NULL)
  837. return 0;
  838. *md = tmp;
  839. return 1;
  840. }
  841. static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
  842. int fallback, const char *param, int op,
  843. int ctrl)
  844. {
  845. OSSL_PARAM md_params[2], *p = md_params;
  846. const char *name;
  847. if (ctx == NULL || (ctx->operation & op) == 0) {
  848. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  849. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  850. return -2;
  851. }
  852. if (fallback)
  853. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
  854. if (md == NULL) {
  855. name = "";
  856. } else {
  857. name = EVP_MD_get0_name(md);
  858. }
  859. *p++ = OSSL_PARAM_construct_utf8_string(param,
  860. /*
  861. * Cast away the const. This is read
  862. * only so should be safe
  863. */
  864. (char *)name, 0);
  865. *p = OSSL_PARAM_construct_end();
  866. return EVP_PKEY_CTX_set_params(ctx, md_params);
  867. }
  868. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  869. {
  870. return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
  871. OSSL_SIGNATURE_PARAM_DIGEST,
  872. EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
  873. }
  874. int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  875. {
  876. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  877. OSSL_KDF_PARAM_DIGEST,
  878. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
  879. }
  880. static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
  881. const char *param, int op, int ctrl,
  882. const unsigned char *data,
  883. int datalen)
  884. {
  885. OSSL_PARAM octet_string_params[2], *p = octet_string_params;
  886. if (ctx == NULL || (ctx->operation & op) == 0) {
  887. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  888. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  889. return -2;
  890. }
  891. /* Code below to be removed when legacy support is dropped. */
  892. if (fallback)
  893. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
  894. /* end of legacy support */
  895. if (datalen < 0) {
  896. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
  897. return 0;
  898. }
  899. *p++ = OSSL_PARAM_construct_octet_string(param,
  900. /*
  901. * Cast away the const. This is read
  902. * only so should be safe
  903. */
  904. (unsigned char *)data,
  905. (size_t)datalen);
  906. *p = OSSL_PARAM_construct_end();
  907. return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
  908. }
  909. int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
  910. const unsigned char *sec, int seclen)
  911. {
  912. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  913. OSSL_KDF_PARAM_SECRET,
  914. EVP_PKEY_OP_DERIVE,
  915. EVP_PKEY_CTRL_TLS_SECRET,
  916. sec, seclen);
  917. }
  918. int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
  919. const unsigned char *seed, int seedlen)
  920. {
  921. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  922. OSSL_KDF_PARAM_SEED,
  923. EVP_PKEY_OP_DERIVE,
  924. EVP_PKEY_CTRL_TLS_SEED,
  925. seed, seedlen);
  926. }
  927. int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  928. {
  929. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  930. OSSL_KDF_PARAM_DIGEST,
  931. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
  932. }
  933. int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
  934. const unsigned char *salt, int saltlen)
  935. {
  936. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  937. OSSL_KDF_PARAM_SALT,
  938. EVP_PKEY_OP_DERIVE,
  939. EVP_PKEY_CTRL_HKDF_SALT,
  940. salt, saltlen);
  941. }
  942. int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
  943. const unsigned char *key, int keylen)
  944. {
  945. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  946. OSSL_KDF_PARAM_KEY,
  947. EVP_PKEY_OP_DERIVE,
  948. EVP_PKEY_CTRL_HKDF_KEY,
  949. key, keylen);
  950. }
  951. int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
  952. const unsigned char *info, int infolen)
  953. {
  954. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  955. OSSL_KDF_PARAM_INFO,
  956. EVP_PKEY_OP_DERIVE,
  957. EVP_PKEY_CTRL_HKDF_INFO,
  958. info, infolen);
  959. }
  960. int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
  961. {
  962. OSSL_PARAM int_params[2], *p = int_params;
  963. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  964. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  965. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  966. return -2;
  967. }
  968. /* Code below to be removed when legacy support is dropped. */
  969. if (ctx->op.kex.algctx == NULL)
  970. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
  971. EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
  972. /* end of legacy support */
  973. if (mode < 0) {
  974. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  975. return 0;
  976. }
  977. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
  978. *p = OSSL_PARAM_construct_end();
  979. return EVP_PKEY_CTX_set_params(ctx, int_params);
  980. }
  981. int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
  982. int passlen)
  983. {
  984. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  985. OSSL_KDF_PARAM_PASSWORD,
  986. EVP_PKEY_OP_DERIVE,
  987. EVP_PKEY_CTRL_PASS,
  988. (const unsigned char *)pass, passlen);
  989. }
  990. int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
  991. const unsigned char *salt, int saltlen)
  992. {
  993. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  994. OSSL_KDF_PARAM_SALT,
  995. EVP_PKEY_OP_DERIVE,
  996. EVP_PKEY_CTRL_SCRYPT_SALT,
  997. salt, saltlen);
  998. }
  999. static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
  1000. int op, int ctrl, uint64_t val)
  1001. {
  1002. OSSL_PARAM uint64_params[2], *p = uint64_params;
  1003. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1004. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1005. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1006. return -2;
  1007. }
  1008. /* Code below to be removed when legacy support is dropped. */
  1009. if (ctx->op.kex.algctx == NULL)
  1010. return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
  1011. /* end of legacy support */
  1012. *p++ = OSSL_PARAM_construct_uint64(param, &val);
  1013. *p = OSSL_PARAM_construct_end();
  1014. return EVP_PKEY_CTX_set_params(ctx, uint64_params);
  1015. }
  1016. int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
  1017. {
  1018. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
  1019. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
  1020. n);
  1021. }
  1022. int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
  1023. {
  1024. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
  1025. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
  1026. r);
  1027. }
  1028. int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
  1029. {
  1030. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
  1031. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
  1032. p);
  1033. }
  1034. int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
  1035. uint64_t maxmem_bytes)
  1036. {
  1037. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
  1038. EVP_PKEY_OP_DERIVE,
  1039. EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
  1040. maxmem_bytes);
  1041. }
  1042. int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
  1043. int keylen)
  1044. {
  1045. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
  1046. OSSL_PKEY_PARAM_PRIV_KEY,
  1047. EVP_PKEY_OP_KEYGEN,
  1048. EVP_PKEY_CTRL_SET_MAC_KEY,
  1049. key, keylen);
  1050. }
  1051. int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
  1052. {
  1053. OSSL_PARAM params[2], *p = params;
  1054. if (ctx == NULL || op == NULL) {
  1055. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  1056. return 0;
  1057. }
  1058. if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1059. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1060. return -2;
  1061. }
  1062. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
  1063. (char *)op, 0);
  1064. *p = OSSL_PARAM_construct_end();
  1065. return EVP_PKEY_CTX_set_params(ctx, params);
  1066. }
  1067. int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
  1068. {
  1069. OSSL_PARAM params[2], *p = params;
  1070. int ret;
  1071. if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1072. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1073. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1074. return -2;
  1075. }
  1076. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
  1077. /*
  1078. * Cast away the const. This is
  1079. * read only so should be safe
  1080. */
  1081. (void *)id, (size_t)len);
  1082. *p++ = OSSL_PARAM_construct_end();
  1083. ret = evp_pkey_ctx_set_params_strict(ctx, params);
  1084. if (ret == -2)
  1085. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1086. return ret;
  1087. }
  1088. int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
  1089. {
  1090. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1091. EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
  1092. }
  1093. static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
  1094. {
  1095. int ret;
  1096. void *tmp_id = NULL;
  1097. OSSL_PARAM params[2], *p = params;
  1098. if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1099. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1100. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1101. return -2;
  1102. }
  1103. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
  1104. &tmp_id, 0);
  1105. *p++ = OSSL_PARAM_construct_end();
  1106. ret = evp_pkey_ctx_get_params_strict(ctx, params);
  1107. if (ret == -2) {
  1108. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1109. } else if (ret > 0) {
  1110. size_t tmp_id_len = params[0].return_size;
  1111. if (id != NULL)
  1112. memcpy(id, tmp_id, tmp_id_len);
  1113. if (id_len != NULL)
  1114. *id_len = tmp_id_len;
  1115. }
  1116. return ret;
  1117. }
  1118. int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
  1119. {
  1120. return get1_id_data(ctx, id, NULL);
  1121. }
  1122. int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
  1123. {
  1124. return get1_id_data(ctx, NULL, id_len);
  1125. }
  1126. int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
  1127. {
  1128. return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
  1129. }
  1130. int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
  1131. {
  1132. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1133. EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
  1134. }
  1135. static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1136. int cmd, int p1, void *p2)
  1137. {
  1138. int ret = 0;
  1139. /*
  1140. * If the method has a |digest_custom| function, we can relax the
  1141. * operation type check, since this can be called before the operation
  1142. * is initialized.
  1143. */
  1144. if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
  1145. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  1146. ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
  1147. return -1;
  1148. }
  1149. if ((optype != -1) && !(ctx->operation & optype)) {
  1150. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1151. return -1;
  1152. }
  1153. }
  1154. switch (evp_pkey_ctx_state(ctx)) {
  1155. case EVP_PKEY_STATE_PROVIDER:
  1156. return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
  1157. case EVP_PKEY_STATE_UNKNOWN:
  1158. case EVP_PKEY_STATE_LEGACY:
  1159. if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
  1160. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1161. return -2;
  1162. }
  1163. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  1164. return -1;
  1165. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  1166. if (ret == -2)
  1167. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1168. break;
  1169. }
  1170. return ret;
  1171. }
  1172. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1173. int cmd, int p1, void *p2)
  1174. {
  1175. int ret = 0;
  1176. if (ctx == NULL) {
  1177. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1178. return -2;
  1179. }
  1180. /* If unsupported, we don't want that reported here */
  1181. ERR_set_mark();
  1182. ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
  1183. cmd, NULL, p2, p1);
  1184. if (ret == -2) {
  1185. ERR_pop_to_mark();
  1186. } else {
  1187. ERR_clear_last_mark();
  1188. /*
  1189. * If there was an error, there was an error.
  1190. * If the operation isn't initialized yet, we also return, as
  1191. * the saved values will be used then anyway.
  1192. */
  1193. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1194. return ret;
  1195. }
  1196. return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
  1197. }
  1198. int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1199. int cmd, uint64_t value)
  1200. {
  1201. return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
  1202. }
  1203. static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
  1204. const char *name, const char *value)
  1205. {
  1206. int ret = 0;
  1207. if (ctx == NULL) {
  1208. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1209. return -2;
  1210. }
  1211. switch (evp_pkey_ctx_state(ctx)) {
  1212. case EVP_PKEY_STATE_PROVIDER:
  1213. return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
  1214. case EVP_PKEY_STATE_UNKNOWN:
  1215. case EVP_PKEY_STATE_LEGACY:
  1216. if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
  1217. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1218. return -2;
  1219. }
  1220. if (strcmp(name, "digest") == 0)
  1221. ret = EVP_PKEY_CTX_md(ctx,
  1222. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  1223. EVP_PKEY_CTRL_MD, value);
  1224. else
  1225. ret = ctx->pmeth->ctrl_str(ctx, name, value);
  1226. break;
  1227. }
  1228. return ret;
  1229. }
  1230. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  1231. const char *name, const char *value)
  1232. {
  1233. int ret = 0;
  1234. /* If unsupported, we don't want that reported here */
  1235. ERR_set_mark();
  1236. ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
  1237. name, value, strlen(value) + 1);
  1238. if (ret == -2) {
  1239. ERR_pop_to_mark();
  1240. } else {
  1241. ERR_clear_last_mark();
  1242. /*
  1243. * If there was an error, there was an error.
  1244. * If the operation isn't initialized yet, we also return, as
  1245. * the saved values will be used then anyway.
  1246. */
  1247. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1248. return ret;
  1249. }
  1250. return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
  1251. }
  1252. static int decode_cmd(int cmd, const char *name)
  1253. {
  1254. if (cmd == -1) {
  1255. /*
  1256. * The consequence of the assertion not being true is that this
  1257. * function will return -1, which will cause the calling functions
  1258. * to signal that the command is unsupported... in non-debug mode.
  1259. */
  1260. if (ossl_assert(name != NULL))
  1261. if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
  1262. cmd = EVP_PKEY_CTRL_SET1_ID;
  1263. }
  1264. return cmd;
  1265. }
  1266. static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
  1267. int keytype, int optype,
  1268. int cmd, const char *name,
  1269. const void *data, size_t data_len)
  1270. {
  1271. /*
  1272. * Check that it's one of the supported commands. The ctrl commands
  1273. * number cases here must correspond to the cases in the bottom switch
  1274. * in this function.
  1275. */
  1276. switch (cmd = decode_cmd(cmd, name)) {
  1277. case EVP_PKEY_CTRL_SET1_ID:
  1278. break;
  1279. default:
  1280. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1281. return -2;
  1282. }
  1283. if (keytype != -1) {
  1284. switch (evp_pkey_ctx_state(ctx)) {
  1285. case EVP_PKEY_STATE_PROVIDER:
  1286. if (ctx->keymgmt == NULL) {
  1287. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1288. return -2;
  1289. }
  1290. if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
  1291. evp_pkey_type2name(keytype))) {
  1292. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1293. return -1;
  1294. }
  1295. break;
  1296. case EVP_PKEY_STATE_UNKNOWN:
  1297. case EVP_PKEY_STATE_LEGACY:
  1298. if (ctx->pmeth == NULL) {
  1299. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1300. return -2;
  1301. }
  1302. if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
  1303. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1304. return -1;
  1305. }
  1306. break;
  1307. }
  1308. }
  1309. if (optype != -1 && (ctx->operation & optype) == 0) {
  1310. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1311. return -1;
  1312. }
  1313. switch (cmd) {
  1314. case EVP_PKEY_CTRL_SET1_ID:
  1315. evp_pkey_ctx_free_cached_data(ctx, cmd, name);
  1316. if (name != NULL) {
  1317. ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
  1318. if (ctx->cached_parameters.dist_id_name == NULL) {
  1319. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1320. return 0;
  1321. }
  1322. }
  1323. if (data_len > 0) {
  1324. ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
  1325. if (ctx->cached_parameters.dist_id == NULL) {
  1326. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1327. return 0;
  1328. }
  1329. }
  1330. ctx->cached_parameters.dist_id_set = 1;
  1331. ctx->cached_parameters.dist_id_len = data_len;
  1332. break;
  1333. }
  1334. return 1;
  1335. }
  1336. static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
  1337. int cmd, const char *name)
  1338. {
  1339. cmd = decode_cmd(cmd, name);
  1340. switch (cmd) {
  1341. case EVP_PKEY_CTRL_SET1_ID:
  1342. OPENSSL_free(ctx->cached_parameters.dist_id);
  1343. OPENSSL_free(ctx->cached_parameters.dist_id_name);
  1344. ctx->cached_parameters.dist_id = NULL;
  1345. ctx->cached_parameters.dist_id_name = NULL;
  1346. break;
  1347. }
  1348. }
  1349. static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
  1350. {
  1351. evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
  1352. }
  1353. int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
  1354. {
  1355. int ret = 1;
  1356. if (ret && ctx->cached_parameters.dist_id_set) {
  1357. const char *name = ctx->cached_parameters.dist_id_name;
  1358. const void *val = ctx->cached_parameters.dist_id;
  1359. size_t len = ctx->cached_parameters.dist_id_len;
  1360. if (name != NULL)
  1361. ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
  1362. else
  1363. ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
  1364. EVP_PKEY_CTRL_SET1_ID,
  1365. (int)len, (void *)val);
  1366. }
  1367. return ret;
  1368. }
  1369. OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
  1370. {
  1371. return ctx->libctx;
  1372. }
  1373. const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
  1374. {
  1375. return ctx->propquery;
  1376. }
  1377. const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
  1378. {
  1379. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1380. if (ctx->op.sig.signature != NULL)
  1381. return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
  1382. } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1383. if (ctx->op.kex.exchange != NULL)
  1384. return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
  1385. } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1386. if (ctx->op.encap.kem != NULL)
  1387. return EVP_KEM_get0_provider(ctx->op.encap.kem);
  1388. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  1389. if (ctx->op.ciph.cipher != NULL)
  1390. return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
  1391. } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1392. if (ctx->keymgmt != NULL)
  1393. return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
  1394. }
  1395. return NULL;
  1396. }
  1397. /* Utility functions to send a string of hex string to a ctrl */
  1398. int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
  1399. {
  1400. size_t len;
  1401. len = strlen(str);
  1402. if (len > INT_MAX)
  1403. return -1;
  1404. return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
  1405. }
  1406. int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
  1407. {
  1408. unsigned char *bin;
  1409. long binlen;
  1410. int rv = -1;
  1411. bin = OPENSSL_hexstr2buf(hex, &binlen);
  1412. if (bin == NULL)
  1413. return 0;
  1414. if (binlen <= INT_MAX)
  1415. rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
  1416. OPENSSL_free(bin);
  1417. return rv;
  1418. }
  1419. /* Pass a message digest to a ctrl */
  1420. int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
  1421. {
  1422. const EVP_MD *m;
  1423. if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
  1424. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
  1425. return 0;
  1426. }
  1427. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
  1428. }
  1429. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  1430. {
  1431. return ctx->operation;
  1432. }
  1433. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  1434. {
  1435. ctx->keygen_info = dat;
  1436. ctx->keygen_info_count = datlen;
  1437. }
  1438. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  1439. {
  1440. ctx->data = data;
  1441. }
  1442. void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
  1443. {
  1444. return ctx->data;
  1445. }
  1446. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  1447. {
  1448. return ctx->pkey;
  1449. }
  1450. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  1451. {
  1452. return ctx->peerkey;
  1453. }
  1454. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  1455. {
  1456. ctx->app_data = data;
  1457. }
  1458. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  1459. {
  1460. return ctx->app_data;
  1461. }
  1462. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  1463. int (*init) (EVP_PKEY_CTX *ctx))
  1464. {
  1465. pmeth->init = init;
  1466. }
  1467. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  1468. int (*copy) (EVP_PKEY_CTX *dst,
  1469. const EVP_PKEY_CTX *src))
  1470. {
  1471. pmeth->copy = copy;
  1472. }
  1473. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  1474. void (*cleanup) (EVP_PKEY_CTX *ctx))
  1475. {
  1476. pmeth->cleanup = cleanup;
  1477. }
  1478. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  1479. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  1480. int (*paramgen) (EVP_PKEY_CTX *ctx,
  1481. EVP_PKEY *pkey))
  1482. {
  1483. pmeth->paramgen_init = paramgen_init;
  1484. pmeth->paramgen = paramgen;
  1485. }
  1486. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  1487. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  1488. int (*keygen) (EVP_PKEY_CTX *ctx,
  1489. EVP_PKEY *pkey))
  1490. {
  1491. pmeth->keygen_init = keygen_init;
  1492. pmeth->keygen = keygen;
  1493. }
  1494. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  1495. int (*sign_init) (EVP_PKEY_CTX *ctx),
  1496. int (*sign) (EVP_PKEY_CTX *ctx,
  1497. unsigned char *sig, size_t *siglen,
  1498. const unsigned char *tbs,
  1499. size_t tbslen))
  1500. {
  1501. pmeth->sign_init = sign_init;
  1502. pmeth->sign = sign;
  1503. }
  1504. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  1505. int (*verify_init) (EVP_PKEY_CTX *ctx),
  1506. int (*verify) (EVP_PKEY_CTX *ctx,
  1507. const unsigned char *sig,
  1508. size_t siglen,
  1509. const unsigned char *tbs,
  1510. size_t tbslen))
  1511. {
  1512. pmeth->verify_init = verify_init;
  1513. pmeth->verify = verify;
  1514. }
  1515. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  1516. int (*verify_recover_init) (EVP_PKEY_CTX
  1517. *ctx),
  1518. int (*verify_recover) (EVP_PKEY_CTX
  1519. *ctx,
  1520. unsigned char
  1521. *sig,
  1522. size_t *siglen,
  1523. const unsigned
  1524. char *tbs,
  1525. size_t tbslen))
  1526. {
  1527. pmeth->verify_recover_init = verify_recover_init;
  1528. pmeth->verify_recover = verify_recover;
  1529. }
  1530. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  1531. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  1532. EVP_MD_CTX *mctx),
  1533. int (*signctx) (EVP_PKEY_CTX *ctx,
  1534. unsigned char *sig,
  1535. size_t *siglen,
  1536. EVP_MD_CTX *mctx))
  1537. {
  1538. pmeth->signctx_init = signctx_init;
  1539. pmeth->signctx = signctx;
  1540. }
  1541. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  1542. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  1543. EVP_MD_CTX *mctx),
  1544. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  1545. const unsigned char *sig,
  1546. int siglen,
  1547. EVP_MD_CTX *mctx))
  1548. {
  1549. pmeth->verifyctx_init = verifyctx_init;
  1550. pmeth->verifyctx = verifyctx;
  1551. }
  1552. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  1553. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  1554. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  1555. unsigned char *out,
  1556. size_t *outlen,
  1557. const unsigned char *in,
  1558. size_t inlen))
  1559. {
  1560. pmeth->encrypt_init = encrypt_init;
  1561. pmeth->encrypt = encryptfn;
  1562. }
  1563. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  1564. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  1565. int (*decrypt) (EVP_PKEY_CTX *ctx,
  1566. unsigned char *out,
  1567. size_t *outlen,
  1568. const unsigned char *in,
  1569. size_t inlen))
  1570. {
  1571. pmeth->decrypt_init = decrypt_init;
  1572. pmeth->decrypt = decrypt;
  1573. }
  1574. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  1575. int (*derive_init) (EVP_PKEY_CTX *ctx),
  1576. int (*derive) (EVP_PKEY_CTX *ctx,
  1577. unsigned char *key,
  1578. size_t *keylen))
  1579. {
  1580. pmeth->derive_init = derive_init;
  1581. pmeth->derive = derive;
  1582. }
  1583. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  1584. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  1585. void *p2),
  1586. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  1587. const char *type,
  1588. const char *value))
  1589. {
  1590. pmeth->ctrl = ctrl;
  1591. pmeth->ctrl_str = ctrl_str;
  1592. }
  1593. void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
  1594. int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1595. const unsigned char *tbs, size_t tbslen))
  1596. {
  1597. pmeth->digestsign = digestsign;
  1598. }
  1599. void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
  1600. int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  1601. size_t siglen, const unsigned char *tbs,
  1602. size_t tbslen))
  1603. {
  1604. pmeth->digestverify = digestverify;
  1605. }
  1606. void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
  1607. int (*check) (EVP_PKEY *pkey))
  1608. {
  1609. pmeth->check = check;
  1610. }
  1611. void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
  1612. int (*check) (EVP_PKEY *pkey))
  1613. {
  1614. pmeth->public_check = check;
  1615. }
  1616. void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
  1617. int (*check) (EVP_PKEY *pkey))
  1618. {
  1619. pmeth->param_check = check;
  1620. }
  1621. void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
  1622. int (*digest_custom) (EVP_PKEY_CTX *ctx,
  1623. EVP_MD_CTX *mctx))
  1624. {
  1625. pmeth->digest_custom = digest_custom;
  1626. }
  1627. void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
  1628. int (**pinit) (EVP_PKEY_CTX *ctx))
  1629. {
  1630. *pinit = pmeth->init;
  1631. }
  1632. void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
  1633. int (**pcopy) (EVP_PKEY_CTX *dst,
  1634. const EVP_PKEY_CTX *src))
  1635. {
  1636. *pcopy = pmeth->copy;
  1637. }
  1638. void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
  1639. void (**pcleanup) (EVP_PKEY_CTX *ctx))
  1640. {
  1641. *pcleanup = pmeth->cleanup;
  1642. }
  1643. void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
  1644. int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
  1645. int (**pparamgen) (EVP_PKEY_CTX *ctx,
  1646. EVP_PKEY *pkey))
  1647. {
  1648. if (pparamgen_init)
  1649. *pparamgen_init = pmeth->paramgen_init;
  1650. if (pparamgen)
  1651. *pparamgen = pmeth->paramgen;
  1652. }
  1653. void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
  1654. int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
  1655. int (**pkeygen) (EVP_PKEY_CTX *ctx,
  1656. EVP_PKEY *pkey))
  1657. {
  1658. if (pkeygen_init)
  1659. *pkeygen_init = pmeth->keygen_init;
  1660. if (pkeygen)
  1661. *pkeygen = pmeth->keygen;
  1662. }
  1663. void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
  1664. int (**psign_init) (EVP_PKEY_CTX *ctx),
  1665. int (**psign) (EVP_PKEY_CTX *ctx,
  1666. unsigned char *sig, size_t *siglen,
  1667. const unsigned char *tbs,
  1668. size_t tbslen))
  1669. {
  1670. if (psign_init)
  1671. *psign_init = pmeth->sign_init;
  1672. if (psign)
  1673. *psign = pmeth->sign;
  1674. }
  1675. void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
  1676. int (**pverify_init) (EVP_PKEY_CTX *ctx),
  1677. int (**pverify) (EVP_PKEY_CTX *ctx,
  1678. const unsigned char *sig,
  1679. size_t siglen,
  1680. const unsigned char *tbs,
  1681. size_t tbslen))
  1682. {
  1683. if (pverify_init)
  1684. *pverify_init = pmeth->verify_init;
  1685. if (pverify)
  1686. *pverify = pmeth->verify;
  1687. }
  1688. void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
  1689. int (**pverify_recover_init) (EVP_PKEY_CTX
  1690. *ctx),
  1691. int (**pverify_recover) (EVP_PKEY_CTX
  1692. *ctx,
  1693. unsigned char
  1694. *sig,
  1695. size_t *siglen,
  1696. const unsigned
  1697. char *tbs,
  1698. size_t tbslen))
  1699. {
  1700. if (pverify_recover_init)
  1701. *pverify_recover_init = pmeth->verify_recover_init;
  1702. if (pverify_recover)
  1703. *pverify_recover = pmeth->verify_recover;
  1704. }
  1705. void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
  1706. int (**psignctx_init) (EVP_PKEY_CTX *ctx,
  1707. EVP_MD_CTX *mctx),
  1708. int (**psignctx) (EVP_PKEY_CTX *ctx,
  1709. unsigned char *sig,
  1710. size_t *siglen,
  1711. EVP_MD_CTX *mctx))
  1712. {
  1713. if (psignctx_init)
  1714. *psignctx_init = pmeth->signctx_init;
  1715. if (psignctx)
  1716. *psignctx = pmeth->signctx;
  1717. }
  1718. void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
  1719. int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
  1720. EVP_MD_CTX *mctx),
  1721. int (**pverifyctx) (EVP_PKEY_CTX *ctx,
  1722. const unsigned char *sig,
  1723. int siglen,
  1724. EVP_MD_CTX *mctx))
  1725. {
  1726. if (pverifyctx_init)
  1727. *pverifyctx_init = pmeth->verifyctx_init;
  1728. if (pverifyctx)
  1729. *pverifyctx = pmeth->verifyctx;
  1730. }
  1731. void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
  1732. int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
  1733. int (**pencryptfn) (EVP_PKEY_CTX *ctx,
  1734. unsigned char *out,
  1735. size_t *outlen,
  1736. const unsigned char *in,
  1737. size_t inlen))
  1738. {
  1739. if (pencrypt_init)
  1740. *pencrypt_init = pmeth->encrypt_init;
  1741. if (pencryptfn)
  1742. *pencryptfn = pmeth->encrypt;
  1743. }
  1744. void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
  1745. int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
  1746. int (**pdecrypt) (EVP_PKEY_CTX *ctx,
  1747. unsigned char *out,
  1748. size_t *outlen,
  1749. const unsigned char *in,
  1750. size_t inlen))
  1751. {
  1752. if (pdecrypt_init)
  1753. *pdecrypt_init = pmeth->decrypt_init;
  1754. if (pdecrypt)
  1755. *pdecrypt = pmeth->decrypt;
  1756. }
  1757. void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
  1758. int (**pderive_init) (EVP_PKEY_CTX *ctx),
  1759. int (**pderive) (EVP_PKEY_CTX *ctx,
  1760. unsigned char *key,
  1761. size_t *keylen))
  1762. {
  1763. if (pderive_init)
  1764. *pderive_init = pmeth->derive_init;
  1765. if (pderive)
  1766. *pderive = pmeth->derive;
  1767. }
  1768. void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
  1769. int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  1770. void *p2),
  1771. int (**pctrl_str) (EVP_PKEY_CTX *ctx,
  1772. const char *type,
  1773. const char *value))
  1774. {
  1775. if (pctrl)
  1776. *pctrl = pmeth->ctrl;
  1777. if (pctrl_str)
  1778. *pctrl_str = pmeth->ctrl_str;
  1779. }
  1780. void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
  1781. int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1782. const unsigned char *tbs, size_t tbslen))
  1783. {
  1784. if (digestsign)
  1785. *digestsign = pmeth->digestsign;
  1786. }
  1787. void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
  1788. int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  1789. size_t siglen, const unsigned char *tbs,
  1790. size_t tbslen))
  1791. {
  1792. if (digestverify)
  1793. *digestverify = pmeth->digestverify;
  1794. }
  1795. void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
  1796. int (**pcheck) (EVP_PKEY *pkey))
  1797. {
  1798. if (pcheck != NULL)
  1799. *pcheck = pmeth->check;
  1800. }
  1801. void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
  1802. int (**pcheck) (EVP_PKEY *pkey))
  1803. {
  1804. if (pcheck != NULL)
  1805. *pcheck = pmeth->public_check;
  1806. }
  1807. void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
  1808. int (**pcheck) (EVP_PKEY *pkey))
  1809. {
  1810. if (pcheck != NULL)
  1811. *pcheck = pmeth->param_check;
  1812. }
  1813. void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
  1814. int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
  1815. EVP_MD_CTX *mctx))
  1816. {
  1817. if (pdigest_custom != NULL)
  1818. *pdigest_custom = pmeth->digest_custom;
  1819. }
  1820. #endif /* FIPS_MODULE */