pmeth_lib.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. /*
  2. * Copyright 2006-2024 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. static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
  907. const char *param, int op, int ctrl,
  908. const unsigned char *data,
  909. int datalen)
  910. {
  911. OSSL_PARAM os_params[2];
  912. const OSSL_PARAM *gettables;
  913. unsigned char *info = NULL;
  914. size_t info_len = 0;
  915. size_t info_alloc = 0;
  916. int ret = 0;
  917. if (ctx == NULL || (ctx->operation & op) == 0) {
  918. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  919. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  920. return -2;
  921. }
  922. /* Code below to be removed when legacy support is dropped. */
  923. if (fallback)
  924. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
  925. /* end of legacy support */
  926. if (datalen < 0) {
  927. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
  928. return 0;
  929. } else if (datalen == 0) {
  930. return 1;
  931. }
  932. /* Check for older provider that doesn't support getting this parameter */
  933. gettables = EVP_PKEY_CTX_gettable_params(ctx);
  934. if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
  935. return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
  936. data, datalen);
  937. /* Get the original value length */
  938. os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
  939. os_params[1] = OSSL_PARAM_construct_end();
  940. if (!EVP_PKEY_CTX_get_params(ctx, os_params))
  941. return 0;
  942. /* This should not happen but check to be sure. */
  943. if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
  944. return 0;
  945. info_alloc = os_params[0].return_size + datalen;
  946. if (info_alloc == 0)
  947. return 0;
  948. info = OPENSSL_zalloc(info_alloc);
  949. if (info == NULL)
  950. return 0;
  951. info_len = os_params[0].return_size;
  952. os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
  953. /* if we have data, then go get it */
  954. if (info_len > 0) {
  955. if (!EVP_PKEY_CTX_get_params(ctx, os_params))
  956. goto error;
  957. }
  958. /* Copy the input data */
  959. memcpy(&info[info_len], data, datalen);
  960. ret = EVP_PKEY_CTX_set_params(ctx, os_params);
  961. error:
  962. OPENSSL_clear_free(info, info_alloc);
  963. return ret;
  964. }
  965. int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
  966. const unsigned char *sec, int seclen)
  967. {
  968. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  969. OSSL_KDF_PARAM_SECRET,
  970. EVP_PKEY_OP_DERIVE,
  971. EVP_PKEY_CTRL_TLS_SECRET,
  972. sec, seclen);
  973. }
  974. int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
  975. const unsigned char *seed, int seedlen)
  976. {
  977. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  978. OSSL_KDF_PARAM_SEED,
  979. EVP_PKEY_OP_DERIVE,
  980. EVP_PKEY_CTRL_TLS_SEED,
  981. seed, seedlen);
  982. }
  983. int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  984. {
  985. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  986. OSSL_KDF_PARAM_DIGEST,
  987. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
  988. }
  989. int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
  990. const unsigned char *salt, int saltlen)
  991. {
  992. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  993. OSSL_KDF_PARAM_SALT,
  994. EVP_PKEY_OP_DERIVE,
  995. EVP_PKEY_CTRL_HKDF_SALT,
  996. salt, saltlen);
  997. }
  998. int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
  999. const unsigned char *key, int keylen)
  1000. {
  1001. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1002. OSSL_KDF_PARAM_KEY,
  1003. EVP_PKEY_OP_DERIVE,
  1004. EVP_PKEY_CTRL_HKDF_KEY,
  1005. key, keylen);
  1006. }
  1007. int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
  1008. const unsigned char *info, int infolen)
  1009. {
  1010. return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1011. OSSL_KDF_PARAM_INFO,
  1012. EVP_PKEY_OP_DERIVE,
  1013. EVP_PKEY_CTRL_HKDF_INFO,
  1014. info, infolen);
  1015. }
  1016. int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
  1017. {
  1018. OSSL_PARAM int_params[2], *p = int_params;
  1019. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1020. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1021. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1022. return -2;
  1023. }
  1024. /* Code below to be removed when legacy support is dropped. */
  1025. if (ctx->op.kex.algctx == NULL)
  1026. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
  1027. EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
  1028. /* end of legacy support */
  1029. if (mode < 0) {
  1030. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  1031. return 0;
  1032. }
  1033. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
  1034. *p = OSSL_PARAM_construct_end();
  1035. return EVP_PKEY_CTX_set_params(ctx, int_params);
  1036. }
  1037. int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
  1038. int passlen)
  1039. {
  1040. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1041. OSSL_KDF_PARAM_PASSWORD,
  1042. EVP_PKEY_OP_DERIVE,
  1043. EVP_PKEY_CTRL_PASS,
  1044. (const unsigned char *)pass, passlen);
  1045. }
  1046. int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
  1047. const unsigned char *salt, int saltlen)
  1048. {
  1049. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  1050. OSSL_KDF_PARAM_SALT,
  1051. EVP_PKEY_OP_DERIVE,
  1052. EVP_PKEY_CTRL_SCRYPT_SALT,
  1053. salt, saltlen);
  1054. }
  1055. static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
  1056. int op, int ctrl, uint64_t val)
  1057. {
  1058. OSSL_PARAM uint64_params[2], *p = uint64_params;
  1059. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1060. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1061. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1062. return -2;
  1063. }
  1064. /* Code below to be removed when legacy support is dropped. */
  1065. if (ctx->op.kex.algctx == NULL)
  1066. return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
  1067. /* end of legacy support */
  1068. *p++ = OSSL_PARAM_construct_uint64(param, &val);
  1069. *p = OSSL_PARAM_construct_end();
  1070. return EVP_PKEY_CTX_set_params(ctx, uint64_params);
  1071. }
  1072. int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
  1073. {
  1074. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
  1075. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
  1076. n);
  1077. }
  1078. int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
  1079. {
  1080. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
  1081. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
  1082. r);
  1083. }
  1084. int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
  1085. {
  1086. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
  1087. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
  1088. p);
  1089. }
  1090. int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
  1091. uint64_t maxmem_bytes)
  1092. {
  1093. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
  1094. EVP_PKEY_OP_DERIVE,
  1095. EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
  1096. maxmem_bytes);
  1097. }
  1098. int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
  1099. int keylen)
  1100. {
  1101. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
  1102. OSSL_PKEY_PARAM_PRIV_KEY,
  1103. EVP_PKEY_OP_KEYGEN,
  1104. EVP_PKEY_CTRL_SET_MAC_KEY,
  1105. key, keylen);
  1106. }
  1107. int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
  1108. {
  1109. OSSL_PARAM params[2], *p = params;
  1110. if (ctx == NULL || op == NULL) {
  1111. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  1112. return 0;
  1113. }
  1114. if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1115. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1116. return -2;
  1117. }
  1118. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
  1119. (char *)op, 0);
  1120. *p = OSSL_PARAM_construct_end();
  1121. return EVP_PKEY_CTX_set_params(ctx, params);
  1122. }
  1123. int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
  1124. {
  1125. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1126. EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
  1127. }
  1128. int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
  1129. {
  1130. return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
  1131. }
  1132. int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
  1133. {
  1134. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1135. EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
  1136. }
  1137. static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1138. int cmd, int p1, void *p2)
  1139. {
  1140. int ret = 0;
  1141. /*
  1142. * If the method has a |digest_custom| function, we can relax the
  1143. * operation type check, since this can be called before the operation
  1144. * is initialized.
  1145. */
  1146. if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
  1147. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  1148. ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
  1149. return -1;
  1150. }
  1151. if ((optype != -1) && !(ctx->operation & optype)) {
  1152. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1153. return -1;
  1154. }
  1155. }
  1156. switch (evp_pkey_ctx_state(ctx)) {
  1157. case EVP_PKEY_STATE_PROVIDER:
  1158. return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
  1159. case EVP_PKEY_STATE_UNKNOWN:
  1160. case EVP_PKEY_STATE_LEGACY:
  1161. if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
  1162. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1163. return -2;
  1164. }
  1165. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  1166. return -1;
  1167. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  1168. if (ret == -2)
  1169. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1170. break;
  1171. }
  1172. return ret;
  1173. }
  1174. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1175. int cmd, int p1, void *p2)
  1176. {
  1177. int ret = 0;
  1178. if (ctx == NULL) {
  1179. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1180. return -2;
  1181. }
  1182. /* If unsupported, we don't want that reported here */
  1183. ERR_set_mark();
  1184. ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
  1185. cmd, NULL, p2, p1);
  1186. if (ret == -2) {
  1187. ERR_pop_to_mark();
  1188. } else {
  1189. ERR_clear_last_mark();
  1190. /*
  1191. * If there was an error, there was an error.
  1192. * If the operation isn't initialized yet, we also return, as
  1193. * the saved values will be used then anyway.
  1194. */
  1195. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1196. return ret;
  1197. }
  1198. return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
  1199. }
  1200. int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1201. int cmd, uint64_t value)
  1202. {
  1203. return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
  1204. }
  1205. static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
  1206. const char *name, const char *value)
  1207. {
  1208. int ret = 0;
  1209. if (ctx == NULL) {
  1210. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1211. return -2;
  1212. }
  1213. switch (evp_pkey_ctx_state(ctx)) {
  1214. case EVP_PKEY_STATE_PROVIDER:
  1215. return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
  1216. case EVP_PKEY_STATE_UNKNOWN:
  1217. case EVP_PKEY_STATE_LEGACY:
  1218. if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
  1219. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1220. return -2;
  1221. }
  1222. if (strcmp(name, "digest") == 0)
  1223. ret = EVP_PKEY_CTX_md(ctx,
  1224. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  1225. EVP_PKEY_CTRL_MD, value);
  1226. else
  1227. ret = ctx->pmeth->ctrl_str(ctx, name, value);
  1228. break;
  1229. }
  1230. return ret;
  1231. }
  1232. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  1233. const char *name, const char *value)
  1234. {
  1235. int ret = 0;
  1236. /* If unsupported, we don't want that reported here */
  1237. ERR_set_mark();
  1238. ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
  1239. name, value, strlen(value) + 1);
  1240. if (ret == -2) {
  1241. ERR_pop_to_mark();
  1242. } else {
  1243. ERR_clear_last_mark();
  1244. /*
  1245. * If there was an error, there was an error.
  1246. * If the operation isn't initialized yet, we also return, as
  1247. * the saved values will be used then anyway.
  1248. */
  1249. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1250. return ret;
  1251. }
  1252. return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
  1253. }
  1254. static int decode_cmd(int cmd, const char *name)
  1255. {
  1256. if (cmd == -1) {
  1257. /*
  1258. * The consequence of the assertion not being true is that this
  1259. * function will return -1, which will cause the calling functions
  1260. * to signal that the command is unsupported... in non-debug mode.
  1261. */
  1262. if (ossl_assert(name != NULL))
  1263. if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
  1264. cmd = EVP_PKEY_CTRL_SET1_ID;
  1265. }
  1266. return cmd;
  1267. }
  1268. static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
  1269. int keytype, int optype,
  1270. int cmd, const char *name,
  1271. const void *data, size_t data_len)
  1272. {
  1273. /*
  1274. * Check that it's one of the supported commands. The ctrl commands
  1275. * number cases here must correspond to the cases in the bottom switch
  1276. * in this function.
  1277. */
  1278. switch (cmd = decode_cmd(cmd, name)) {
  1279. case EVP_PKEY_CTRL_SET1_ID:
  1280. break;
  1281. default:
  1282. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1283. return -2;
  1284. }
  1285. if (keytype != -1) {
  1286. switch (evp_pkey_ctx_state(ctx)) {
  1287. case EVP_PKEY_STATE_PROVIDER:
  1288. if (ctx->keymgmt == NULL) {
  1289. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1290. return -2;
  1291. }
  1292. if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
  1293. evp_pkey_type2name(keytype))) {
  1294. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1295. return -1;
  1296. }
  1297. break;
  1298. case EVP_PKEY_STATE_UNKNOWN:
  1299. case EVP_PKEY_STATE_LEGACY:
  1300. if (ctx->pmeth == NULL) {
  1301. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1302. return -2;
  1303. }
  1304. if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
  1305. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1306. return -1;
  1307. }
  1308. break;
  1309. }
  1310. }
  1311. if (optype != -1 && (ctx->operation & optype) == 0) {
  1312. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1313. return -1;
  1314. }
  1315. switch (cmd) {
  1316. case EVP_PKEY_CTRL_SET1_ID:
  1317. evp_pkey_ctx_free_cached_data(ctx, cmd, name);
  1318. if (name != NULL) {
  1319. ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
  1320. if (ctx->cached_parameters.dist_id_name == NULL)
  1321. return 0;
  1322. }
  1323. if (data_len > 0) {
  1324. ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
  1325. if (ctx->cached_parameters.dist_id == NULL)
  1326. return 0;
  1327. }
  1328. ctx->cached_parameters.dist_id_set = 1;
  1329. ctx->cached_parameters.dist_id_len = data_len;
  1330. break;
  1331. }
  1332. return 1;
  1333. }
  1334. static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
  1335. int cmd, const char *name)
  1336. {
  1337. cmd = decode_cmd(cmd, name);
  1338. switch (cmd) {
  1339. case EVP_PKEY_CTRL_SET1_ID:
  1340. OPENSSL_free(ctx->cached_parameters.dist_id);
  1341. OPENSSL_free(ctx->cached_parameters.dist_id_name);
  1342. ctx->cached_parameters.dist_id = NULL;
  1343. ctx->cached_parameters.dist_id_name = NULL;
  1344. break;
  1345. }
  1346. }
  1347. static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
  1348. {
  1349. evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
  1350. }
  1351. int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
  1352. {
  1353. int ret = 1;
  1354. if (ret && ctx->cached_parameters.dist_id_set) {
  1355. const char *name = ctx->cached_parameters.dist_id_name;
  1356. const void *val = ctx->cached_parameters.dist_id;
  1357. size_t len = ctx->cached_parameters.dist_id_len;
  1358. if (name != NULL)
  1359. ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
  1360. else
  1361. ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
  1362. EVP_PKEY_CTRL_SET1_ID,
  1363. (int)len, (void *)val);
  1364. }
  1365. return ret;
  1366. }
  1367. OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
  1368. {
  1369. return ctx->libctx;
  1370. }
  1371. const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
  1372. {
  1373. return ctx->propquery;
  1374. }
  1375. const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
  1376. {
  1377. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1378. if (ctx->op.sig.signature != NULL)
  1379. return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
  1380. } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1381. if (ctx->op.kex.exchange != NULL)
  1382. return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
  1383. } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1384. if (ctx->op.encap.kem != NULL)
  1385. return EVP_KEM_get0_provider(ctx->op.encap.kem);
  1386. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  1387. if (ctx->op.ciph.cipher != NULL)
  1388. return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
  1389. } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1390. if (ctx->keymgmt != NULL)
  1391. return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
  1392. }
  1393. return NULL;
  1394. }
  1395. /* Utility functions to send a string of hex string to a ctrl */
  1396. int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
  1397. {
  1398. size_t len;
  1399. len = strlen(str);
  1400. if (len > INT_MAX)
  1401. return -1;
  1402. return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
  1403. }
  1404. int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
  1405. {
  1406. unsigned char *bin;
  1407. long binlen;
  1408. int rv = -1;
  1409. bin = OPENSSL_hexstr2buf(hex, &binlen);
  1410. if (bin == NULL)
  1411. return 0;
  1412. if (binlen <= INT_MAX)
  1413. rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
  1414. OPENSSL_free(bin);
  1415. return rv;
  1416. }
  1417. /* Pass a message digest to a ctrl */
  1418. int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
  1419. {
  1420. const EVP_MD *m;
  1421. if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
  1422. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
  1423. return 0;
  1424. }
  1425. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
  1426. }
  1427. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  1428. {
  1429. return ctx->operation;
  1430. }
  1431. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  1432. {
  1433. ctx->keygen_info = dat;
  1434. ctx->keygen_info_count = datlen;
  1435. }
  1436. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  1437. {
  1438. ctx->data = data;
  1439. }
  1440. void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
  1441. {
  1442. return ctx->data;
  1443. }
  1444. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  1445. {
  1446. return ctx->pkey;
  1447. }
  1448. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  1449. {
  1450. return ctx->peerkey;
  1451. }
  1452. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  1453. {
  1454. ctx->app_data = data;
  1455. }
  1456. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  1457. {
  1458. return ctx->app_data;
  1459. }
  1460. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  1461. int (*init) (EVP_PKEY_CTX *ctx))
  1462. {
  1463. pmeth->init = init;
  1464. }
  1465. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  1466. int (*copy) (EVP_PKEY_CTX *dst,
  1467. const EVP_PKEY_CTX *src))
  1468. {
  1469. pmeth->copy = copy;
  1470. }
  1471. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  1472. void (*cleanup) (EVP_PKEY_CTX *ctx))
  1473. {
  1474. pmeth->cleanup = cleanup;
  1475. }
  1476. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  1477. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  1478. int (*paramgen) (EVP_PKEY_CTX *ctx,
  1479. EVP_PKEY *pkey))
  1480. {
  1481. pmeth->paramgen_init = paramgen_init;
  1482. pmeth->paramgen = paramgen;
  1483. }
  1484. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  1485. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  1486. int (*keygen) (EVP_PKEY_CTX *ctx,
  1487. EVP_PKEY *pkey))
  1488. {
  1489. pmeth->keygen_init = keygen_init;
  1490. pmeth->keygen = keygen;
  1491. }
  1492. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  1493. int (*sign_init) (EVP_PKEY_CTX *ctx),
  1494. int (*sign) (EVP_PKEY_CTX *ctx,
  1495. unsigned char *sig, size_t *siglen,
  1496. const unsigned char *tbs,
  1497. size_t tbslen))
  1498. {
  1499. pmeth->sign_init = sign_init;
  1500. pmeth->sign = sign;
  1501. }
  1502. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  1503. int (*verify_init) (EVP_PKEY_CTX *ctx),
  1504. int (*verify) (EVP_PKEY_CTX *ctx,
  1505. const unsigned char *sig,
  1506. size_t siglen,
  1507. const unsigned char *tbs,
  1508. size_t tbslen))
  1509. {
  1510. pmeth->verify_init = verify_init;
  1511. pmeth->verify = verify;
  1512. }
  1513. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  1514. int (*verify_recover_init) (EVP_PKEY_CTX
  1515. *ctx),
  1516. int (*verify_recover) (EVP_PKEY_CTX
  1517. *ctx,
  1518. unsigned char
  1519. *sig,
  1520. size_t *siglen,
  1521. const unsigned
  1522. char *tbs,
  1523. size_t tbslen))
  1524. {
  1525. pmeth->verify_recover_init = verify_recover_init;
  1526. pmeth->verify_recover = verify_recover;
  1527. }
  1528. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  1529. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  1530. EVP_MD_CTX *mctx),
  1531. int (*signctx) (EVP_PKEY_CTX *ctx,
  1532. unsigned char *sig,
  1533. size_t *siglen,
  1534. EVP_MD_CTX *mctx))
  1535. {
  1536. pmeth->signctx_init = signctx_init;
  1537. pmeth->signctx = signctx;
  1538. }
  1539. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  1540. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  1541. EVP_MD_CTX *mctx),
  1542. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  1543. const unsigned char *sig,
  1544. int siglen,
  1545. EVP_MD_CTX *mctx))
  1546. {
  1547. pmeth->verifyctx_init = verifyctx_init;
  1548. pmeth->verifyctx = verifyctx;
  1549. }
  1550. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  1551. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  1552. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  1553. unsigned char *out,
  1554. size_t *outlen,
  1555. const unsigned char *in,
  1556. size_t inlen))
  1557. {
  1558. pmeth->encrypt_init = encrypt_init;
  1559. pmeth->encrypt = encryptfn;
  1560. }
  1561. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  1562. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  1563. int (*decrypt) (EVP_PKEY_CTX *ctx,
  1564. unsigned char *out,
  1565. size_t *outlen,
  1566. const unsigned char *in,
  1567. size_t inlen))
  1568. {
  1569. pmeth->decrypt_init = decrypt_init;
  1570. pmeth->decrypt = decrypt;
  1571. }
  1572. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  1573. int (*derive_init) (EVP_PKEY_CTX *ctx),
  1574. int (*derive) (EVP_PKEY_CTX *ctx,
  1575. unsigned char *key,
  1576. size_t *keylen))
  1577. {
  1578. pmeth->derive_init = derive_init;
  1579. pmeth->derive = derive;
  1580. }
  1581. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  1582. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  1583. void *p2),
  1584. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  1585. const char *type,
  1586. const char *value))
  1587. {
  1588. pmeth->ctrl = ctrl;
  1589. pmeth->ctrl_str = ctrl_str;
  1590. }
  1591. void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
  1592. int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1593. const unsigned char *tbs, size_t tbslen))
  1594. {
  1595. pmeth->digestsign = digestsign;
  1596. }
  1597. void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
  1598. int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  1599. size_t siglen, const unsigned char *tbs,
  1600. size_t tbslen))
  1601. {
  1602. pmeth->digestverify = digestverify;
  1603. }
  1604. void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
  1605. int (*check) (EVP_PKEY *pkey))
  1606. {
  1607. pmeth->check = check;
  1608. }
  1609. void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
  1610. int (*check) (EVP_PKEY *pkey))
  1611. {
  1612. pmeth->public_check = check;
  1613. }
  1614. void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
  1615. int (*check) (EVP_PKEY *pkey))
  1616. {
  1617. pmeth->param_check = check;
  1618. }
  1619. void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
  1620. int (*digest_custom) (EVP_PKEY_CTX *ctx,
  1621. EVP_MD_CTX *mctx))
  1622. {
  1623. pmeth->digest_custom = digest_custom;
  1624. }
  1625. void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
  1626. int (**pinit) (EVP_PKEY_CTX *ctx))
  1627. {
  1628. *pinit = pmeth->init;
  1629. }
  1630. void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
  1631. int (**pcopy) (EVP_PKEY_CTX *dst,
  1632. const EVP_PKEY_CTX *src))
  1633. {
  1634. *pcopy = pmeth->copy;
  1635. }
  1636. void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
  1637. void (**pcleanup) (EVP_PKEY_CTX *ctx))
  1638. {
  1639. *pcleanup = pmeth->cleanup;
  1640. }
  1641. void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
  1642. int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
  1643. int (**pparamgen) (EVP_PKEY_CTX *ctx,
  1644. EVP_PKEY *pkey))
  1645. {
  1646. if (pparamgen_init)
  1647. *pparamgen_init = pmeth->paramgen_init;
  1648. if (pparamgen)
  1649. *pparamgen = pmeth->paramgen;
  1650. }
  1651. void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
  1652. int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
  1653. int (**pkeygen) (EVP_PKEY_CTX *ctx,
  1654. EVP_PKEY *pkey))
  1655. {
  1656. if (pkeygen_init)
  1657. *pkeygen_init = pmeth->keygen_init;
  1658. if (pkeygen)
  1659. *pkeygen = pmeth->keygen;
  1660. }
  1661. void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
  1662. int (**psign_init) (EVP_PKEY_CTX *ctx),
  1663. int (**psign) (EVP_PKEY_CTX *ctx,
  1664. unsigned char *sig, size_t *siglen,
  1665. const unsigned char *tbs,
  1666. size_t tbslen))
  1667. {
  1668. if (psign_init)
  1669. *psign_init = pmeth->sign_init;
  1670. if (psign)
  1671. *psign = pmeth->sign;
  1672. }
  1673. void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
  1674. int (**pverify_init) (EVP_PKEY_CTX *ctx),
  1675. int (**pverify) (EVP_PKEY_CTX *ctx,
  1676. const unsigned char *sig,
  1677. size_t siglen,
  1678. const unsigned char *tbs,
  1679. size_t tbslen))
  1680. {
  1681. if (pverify_init)
  1682. *pverify_init = pmeth->verify_init;
  1683. if (pverify)
  1684. *pverify = pmeth->verify;
  1685. }
  1686. void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
  1687. int (**pverify_recover_init) (EVP_PKEY_CTX
  1688. *ctx),
  1689. int (**pverify_recover) (EVP_PKEY_CTX
  1690. *ctx,
  1691. unsigned char
  1692. *sig,
  1693. size_t *siglen,
  1694. const unsigned
  1695. char *tbs,
  1696. size_t tbslen))
  1697. {
  1698. if (pverify_recover_init)
  1699. *pverify_recover_init = pmeth->verify_recover_init;
  1700. if (pverify_recover)
  1701. *pverify_recover = pmeth->verify_recover;
  1702. }
  1703. void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
  1704. int (**psignctx_init) (EVP_PKEY_CTX *ctx,
  1705. EVP_MD_CTX *mctx),
  1706. int (**psignctx) (EVP_PKEY_CTX *ctx,
  1707. unsigned char *sig,
  1708. size_t *siglen,
  1709. EVP_MD_CTX *mctx))
  1710. {
  1711. if (psignctx_init)
  1712. *psignctx_init = pmeth->signctx_init;
  1713. if (psignctx)
  1714. *psignctx = pmeth->signctx;
  1715. }
  1716. void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
  1717. int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
  1718. EVP_MD_CTX *mctx),
  1719. int (**pverifyctx) (EVP_PKEY_CTX *ctx,
  1720. const unsigned char *sig,
  1721. int siglen,
  1722. EVP_MD_CTX *mctx))
  1723. {
  1724. if (pverifyctx_init)
  1725. *pverifyctx_init = pmeth->verifyctx_init;
  1726. if (pverifyctx)
  1727. *pverifyctx = pmeth->verifyctx;
  1728. }
  1729. void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
  1730. int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
  1731. int (**pencryptfn) (EVP_PKEY_CTX *ctx,
  1732. unsigned char *out,
  1733. size_t *outlen,
  1734. const unsigned char *in,
  1735. size_t inlen))
  1736. {
  1737. if (pencrypt_init)
  1738. *pencrypt_init = pmeth->encrypt_init;
  1739. if (pencryptfn)
  1740. *pencryptfn = pmeth->encrypt;
  1741. }
  1742. void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
  1743. int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
  1744. int (**pdecrypt) (EVP_PKEY_CTX *ctx,
  1745. unsigned char *out,
  1746. size_t *outlen,
  1747. const unsigned char *in,
  1748. size_t inlen))
  1749. {
  1750. if (pdecrypt_init)
  1751. *pdecrypt_init = pmeth->decrypt_init;
  1752. if (pdecrypt)
  1753. *pdecrypt = pmeth->decrypt;
  1754. }
  1755. void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
  1756. int (**pderive_init) (EVP_PKEY_CTX *ctx),
  1757. int (**pderive) (EVP_PKEY_CTX *ctx,
  1758. unsigned char *key,
  1759. size_t *keylen))
  1760. {
  1761. if (pderive_init)
  1762. *pderive_init = pmeth->derive_init;
  1763. if (pderive)
  1764. *pderive = pmeth->derive;
  1765. }
  1766. void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
  1767. int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  1768. void *p2),
  1769. int (**pctrl_str) (EVP_PKEY_CTX *ctx,
  1770. const char *type,
  1771. const char *value))
  1772. {
  1773. if (pctrl)
  1774. *pctrl = pmeth->ctrl;
  1775. if (pctrl_str)
  1776. *pctrl_str = pmeth->ctrl_str;
  1777. }
  1778. void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
  1779. int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1780. const unsigned char *tbs, size_t tbslen))
  1781. {
  1782. if (digestsign)
  1783. *digestsign = pmeth->digestsign;
  1784. }
  1785. void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
  1786. int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  1787. size_t siglen, const unsigned char *tbs,
  1788. size_t tbslen))
  1789. {
  1790. if (digestverify)
  1791. *digestverify = pmeth->digestverify;
  1792. }
  1793. void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
  1794. int (**pcheck) (EVP_PKEY *pkey))
  1795. {
  1796. if (pcheck != NULL)
  1797. *pcheck = pmeth->check;
  1798. }
  1799. void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
  1800. int (**pcheck) (EVP_PKEY *pkey))
  1801. {
  1802. if (pcheck != NULL)
  1803. *pcheck = pmeth->public_check;
  1804. }
  1805. void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
  1806. int (**pcheck) (EVP_PKEY *pkey))
  1807. {
  1808. if (pcheck != NULL)
  1809. *pcheck = pmeth->param_check;
  1810. }
  1811. void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
  1812. int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
  1813. EVP_MD_CTX *mctx))
  1814. {
  1815. if (pdigest_custom != NULL)
  1816. *pdigest_custom = pmeth->digest_custom;
  1817. }
  1818. #endif /* FIPS_MODULE */