pmeth_lib.c 67 KB

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