pmeth_lib.c 66 KB

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