pmeth_lib.c 68 KB

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