1
0

pmeth_lib.c 69 KB

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