pmeth_lib.c 67 KB

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