pmeth_lib.c 69 KB

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