pmeth_lib.c 69 KB

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