pmeth_lib.c 69 KB

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