pmeth_lib.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  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. 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. rctx->op.sig.algctx
  466. = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
  467. if (rctx->op.sig.algctx == NULL) {
  468. EVP_SIGNATURE_free(rctx->op.sig.signature);
  469. rctx->op.sig.signature = NULL;
  470. goto err;
  471. }
  472. return rctx;
  473. }
  474. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
  475. if (pctx->op.ciph.cipher != NULL) {
  476. rctx->op.ciph.cipher = pctx->op.ciph.cipher;
  477. if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
  478. goto err;
  479. }
  480. if (pctx->op.ciph.algctx != NULL) {
  481. if (!ossl_assert(pctx->op.ciph.cipher != NULL))
  482. goto err;
  483. rctx->op.ciph.algctx
  484. = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
  485. if (rctx->op.ciph.algctx == NULL) {
  486. EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
  487. rctx->op.ciph.cipher = NULL;
  488. goto err;
  489. }
  490. return rctx;
  491. }
  492. } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
  493. if (pctx->op.encap.kem != NULL) {
  494. rctx->op.encap.kem = pctx->op.encap.kem;
  495. if (!EVP_KEM_up_ref(rctx->op.encap.kem))
  496. goto err;
  497. }
  498. if (pctx->op.encap.algctx != NULL) {
  499. if (!ossl_assert(pctx->op.encap.kem != NULL))
  500. goto err;
  501. rctx->op.encap.algctx
  502. = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
  503. if (rctx->op.encap.algctx == NULL) {
  504. EVP_KEM_free(rctx->op.encap.kem);
  505. rctx->op.encap.kem = NULL;
  506. goto err;
  507. }
  508. return rctx;
  509. }
  510. } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
  511. /* Not supported - This would need a gen_dupctx() to work */
  512. goto err;
  513. }
  514. rctx->pmeth = pctx->pmeth;
  515. # ifndef OPENSSL_NO_ENGINE
  516. rctx->engine = pctx->engine;
  517. # endif
  518. if (pctx->peerkey != NULL)
  519. EVP_PKEY_up_ref(pctx->peerkey);
  520. rctx->peerkey = pctx->peerkey;
  521. if (pctx->pmeth == NULL) {
  522. if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
  523. EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
  524. void *provkey;
  525. provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
  526. &tmp_keymgmt, pctx->propquery);
  527. if (provkey == NULL)
  528. goto err;
  529. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
  530. goto err;
  531. EVP_KEYMGMT_free(rctx->keymgmt);
  532. rctx->keymgmt = tmp_keymgmt;
  533. return rctx;
  534. }
  535. } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
  536. return rctx;
  537. }
  538. err:
  539. rctx->pmeth = NULL;
  540. EVP_PKEY_CTX_free(rctx);
  541. return NULL;
  542. }
  543. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  544. {
  545. if (app_pkey_methods == NULL) {
  546. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  547. if (app_pkey_methods == NULL) {
  548. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  549. return 0;
  550. }
  551. }
  552. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
  553. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  554. return 0;
  555. }
  556. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  557. return 1;
  558. }
  559. void evp_app_cleanup_int(void)
  560. {
  561. if (app_pkey_methods != NULL)
  562. sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
  563. }
  564. int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
  565. {
  566. const EVP_PKEY_METHOD *ret;
  567. ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
  568. return ret == NULL ? 0 : 1;
  569. }
  570. size_t EVP_PKEY_meth_get_count(void)
  571. {
  572. size_t rv = OSSL_NELEM(standard_methods);
  573. if (app_pkey_methods)
  574. rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
  575. return rv;
  576. }
  577. const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
  578. {
  579. if (idx < OSSL_NELEM(standard_methods))
  580. return (standard_methods[idx])();
  581. if (app_pkey_methods == NULL)
  582. return NULL;
  583. idx -= OSSL_NELEM(standard_methods);
  584. if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
  585. return NULL;
  586. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  587. }
  588. #endif
  589. int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
  590. {
  591. #ifndef FIPS_MODULE
  592. if (evp_pkey_ctx_is_legacy(ctx))
  593. return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
  594. #endif
  595. return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
  596. }
  597. int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
  598. {
  599. switch (evp_pkey_ctx_state(ctx)) {
  600. case EVP_PKEY_STATE_PROVIDER:
  601. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  602. && ctx->op.kex.exchange != NULL
  603. && ctx->op.kex.exchange->set_ctx_params != NULL)
  604. return
  605. ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
  606. params);
  607. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  608. && ctx->op.sig.signature != NULL
  609. && ctx->op.sig.signature->set_ctx_params != NULL)
  610. return
  611. ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
  612. params);
  613. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  614. && ctx->op.ciph.cipher != NULL
  615. && ctx->op.ciph.cipher->set_ctx_params != NULL)
  616. return
  617. ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
  618. params);
  619. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  620. && ctx->keymgmt != NULL
  621. && ctx->keymgmt->gen_set_params != NULL)
  622. return
  623. evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
  624. params);
  625. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  626. && ctx->op.encap.kem != NULL
  627. && ctx->op.encap.kem->set_ctx_params != NULL)
  628. return
  629. ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
  630. params);
  631. break;
  632. #ifndef FIPS_MODULE
  633. case EVP_PKEY_STATE_UNKNOWN:
  634. case EVP_PKEY_STATE_LEGACY:
  635. return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
  636. #endif
  637. }
  638. return 0;
  639. }
  640. int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  641. {
  642. switch (evp_pkey_ctx_state(ctx)) {
  643. case EVP_PKEY_STATE_PROVIDER:
  644. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  645. && ctx->op.kex.exchange != NULL
  646. && ctx->op.kex.exchange->get_ctx_params != NULL)
  647. return
  648. ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
  649. params);
  650. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  651. && ctx->op.sig.signature != NULL
  652. && ctx->op.sig.signature->get_ctx_params != NULL)
  653. return
  654. ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
  655. params);
  656. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  657. && ctx->op.ciph.cipher != NULL
  658. && ctx->op.ciph.cipher->get_ctx_params != NULL)
  659. return
  660. ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
  661. params);
  662. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  663. && ctx->op.encap.kem != NULL
  664. && ctx->op.encap.kem->get_ctx_params != NULL)
  665. return
  666. ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
  667. params);
  668. break;
  669. #ifndef FIPS_MODULE
  670. case EVP_PKEY_STATE_UNKNOWN:
  671. case EVP_PKEY_STATE_LEGACY:
  672. return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
  673. #endif
  674. }
  675. return 0;
  676. }
  677. #ifndef FIPS_MODULE
  678. const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
  679. {
  680. void *provctx;
  681. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  682. && ctx->op.kex.exchange != NULL
  683. && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
  684. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
  685. return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
  686. provctx);
  687. }
  688. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  689. && ctx->op.sig.signature != NULL
  690. && ctx->op.sig.signature->gettable_ctx_params != NULL) {
  691. provctx = ossl_provider_ctx(
  692. EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
  693. return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
  694. provctx);
  695. }
  696. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  697. && ctx->op.ciph.cipher != NULL
  698. && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
  699. provctx = ossl_provider_ctx(
  700. EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
  701. return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
  702. provctx);
  703. }
  704. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  705. && ctx->op.encap.kem != NULL
  706. && ctx->op.encap.kem->gettable_ctx_params != NULL) {
  707. provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
  708. return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
  709. provctx);
  710. }
  711. return NULL;
  712. }
  713. const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
  714. {
  715. void *provctx;
  716. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  717. && ctx->op.kex.exchange != NULL
  718. && ctx->op.kex.exchange->settable_ctx_params != NULL) {
  719. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
  720. return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
  721. provctx);
  722. }
  723. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  724. && ctx->op.sig.signature != NULL
  725. && ctx->op.sig.signature->settable_ctx_params != NULL) {
  726. provctx = ossl_provider_ctx(
  727. EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
  728. return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
  729. provctx);
  730. }
  731. if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  732. && ctx->op.ciph.cipher != NULL
  733. && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
  734. provctx = ossl_provider_ctx(
  735. EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
  736. return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
  737. provctx);
  738. }
  739. if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
  740. && ctx->keymgmt != NULL
  741. && ctx->keymgmt->gen_settable_params != NULL) {
  742. provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
  743. return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
  744. provctx);
  745. }
  746. if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
  747. && ctx->op.encap.kem != NULL
  748. && ctx->op.encap.kem->settable_ctx_params != NULL) {
  749. provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
  750. return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
  751. provctx);
  752. }
  753. return NULL;
  754. }
  755. /*
  756. * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
  757. *
  758. * Return 1 on success, 0 or negative for errors.
  759. *
  760. * In particular they return -2 if any of the params is not supported.
  761. *
  762. * They are not available in FIPS_MODULE as they depend on
  763. * - EVP_PKEY_CTX_{get,set}_params()
  764. * - EVP_PKEY_CTX_{gettable,settable}_params()
  765. *
  766. */
  767. int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  768. {
  769. if (ctx == NULL || params == NULL)
  770. return 0;
  771. /*
  772. * We only check for provider side EVP_PKEY_CTX. For #legacy, we
  773. * depend on the translation that happens in EVP_PKEY_CTX_set_params()
  774. * call, and that the resulting ctrl call will return -2 if it doesn't
  775. * known the ctrl command number.
  776. */
  777. if (evp_pkey_ctx_is_provided(ctx)) {
  778. const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
  779. const OSSL_PARAM *p;
  780. for (p = params; p->key != NULL; p++) {
  781. /* Check the ctx actually understands this parameter */
  782. if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
  783. return -2;
  784. }
  785. }
  786. return EVP_PKEY_CTX_set_params(ctx, params);
  787. }
  788. int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  789. {
  790. if (ctx == NULL || params == NULL)
  791. return 0;
  792. /*
  793. * We only check for provider side EVP_PKEY_CTX. For #legacy, we
  794. * depend on the translation that happens in EVP_PKEY_CTX_get_params()
  795. * call, and that the resulting ctrl call will return -2 if it doesn't
  796. * known the ctrl command number.
  797. */
  798. if (evp_pkey_ctx_is_provided(ctx)) {
  799. const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
  800. const OSSL_PARAM *p;
  801. for (p = params; p->key != NULL; p++) {
  802. /* Check the ctx actually understands this parameter */
  803. if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
  804. return -2;
  805. }
  806. }
  807. return EVP_PKEY_CTX_get_params(ctx, params);
  808. }
  809. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  810. {
  811. OSSL_PARAM sig_md_params[2], *p = sig_md_params;
  812. /* 80 should be big enough */
  813. char name[80] = "";
  814. const EVP_MD *tmp;
  815. if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  816. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  817. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  818. return -2;
  819. }
  820. if (ctx->op.sig.algctx == NULL)
  821. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
  822. EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
  823. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  824. name,
  825. sizeof(name));
  826. *p = OSSL_PARAM_construct_end();
  827. if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
  828. return 0;
  829. tmp = evp_get_digestbyname_ex(ctx->libctx, name);
  830. if (tmp == NULL)
  831. return 0;
  832. *md = tmp;
  833. return 1;
  834. }
  835. static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
  836. int fallback, const char *param, int op,
  837. int ctrl)
  838. {
  839. OSSL_PARAM md_params[2], *p = md_params;
  840. const char *name;
  841. if (ctx == NULL || (ctx->operation & op) == 0) {
  842. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  843. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  844. return -2;
  845. }
  846. if (fallback)
  847. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
  848. if (md == NULL) {
  849. name = "";
  850. } else {
  851. name = EVP_MD_get0_name(md);
  852. }
  853. *p++ = OSSL_PARAM_construct_utf8_string(param,
  854. /*
  855. * Cast away the const. This is read
  856. * only so should be safe
  857. */
  858. (char *)name, 0);
  859. *p = OSSL_PARAM_construct_end();
  860. return EVP_PKEY_CTX_set_params(ctx, md_params);
  861. }
  862. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  863. {
  864. return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
  865. OSSL_SIGNATURE_PARAM_DIGEST,
  866. EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
  867. }
  868. int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  869. {
  870. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  871. OSSL_KDF_PARAM_DIGEST,
  872. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
  873. }
  874. static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
  875. const char *param, int op, int ctrl,
  876. const unsigned char *data,
  877. int datalen)
  878. {
  879. OSSL_PARAM octet_string_params[2], *p = octet_string_params;
  880. if (ctx == NULL || (ctx->operation & op) == 0) {
  881. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  882. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  883. return -2;
  884. }
  885. /* Code below to be removed when legacy support is dropped. */
  886. if (fallback)
  887. return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
  888. /* end of legacy support */
  889. if (datalen < 0) {
  890. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
  891. return 0;
  892. }
  893. *p++ = OSSL_PARAM_construct_octet_string(param,
  894. /*
  895. * Cast away the const. This is read
  896. * only so should be safe
  897. */
  898. (unsigned char *)data,
  899. (size_t)datalen);
  900. *p = OSSL_PARAM_construct_end();
  901. return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
  902. }
  903. int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
  904. const unsigned char *sec, int seclen)
  905. {
  906. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  907. OSSL_KDF_PARAM_SECRET,
  908. EVP_PKEY_OP_DERIVE,
  909. EVP_PKEY_CTRL_TLS_SECRET,
  910. sec, seclen);
  911. }
  912. int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
  913. const unsigned char *seed, int seedlen)
  914. {
  915. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  916. OSSL_KDF_PARAM_SEED,
  917. EVP_PKEY_OP_DERIVE,
  918. EVP_PKEY_CTRL_TLS_SEED,
  919. seed, seedlen);
  920. }
  921. int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  922. {
  923. return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
  924. OSSL_KDF_PARAM_DIGEST,
  925. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
  926. }
  927. int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
  928. const unsigned char *salt, int saltlen)
  929. {
  930. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  931. OSSL_KDF_PARAM_SALT,
  932. EVP_PKEY_OP_DERIVE,
  933. EVP_PKEY_CTRL_HKDF_SALT,
  934. salt, saltlen);
  935. }
  936. int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
  937. const unsigned char *key, int keylen)
  938. {
  939. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  940. OSSL_KDF_PARAM_KEY,
  941. EVP_PKEY_OP_DERIVE,
  942. EVP_PKEY_CTRL_HKDF_KEY,
  943. key, keylen);
  944. }
  945. int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
  946. const unsigned char *info, int infolen)
  947. {
  948. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  949. OSSL_KDF_PARAM_INFO,
  950. EVP_PKEY_OP_DERIVE,
  951. EVP_PKEY_CTRL_HKDF_INFO,
  952. info, infolen);
  953. }
  954. int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
  955. {
  956. OSSL_PARAM int_params[2], *p = int_params;
  957. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  958. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  959. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  960. return -2;
  961. }
  962. /* Code below to be removed when legacy support is dropped. */
  963. if (ctx->op.kex.algctx == NULL)
  964. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
  965. EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
  966. /* end of legacy support */
  967. if (mode < 0) {
  968. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  969. return 0;
  970. }
  971. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
  972. *p = OSSL_PARAM_construct_end();
  973. return EVP_PKEY_CTX_set_params(ctx, int_params);
  974. }
  975. int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
  976. int passlen)
  977. {
  978. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  979. OSSL_KDF_PARAM_PASSWORD,
  980. EVP_PKEY_OP_DERIVE,
  981. EVP_PKEY_CTRL_PASS,
  982. (const unsigned char *)pass, passlen);
  983. }
  984. int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
  985. const unsigned char *salt, int saltlen)
  986. {
  987. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
  988. OSSL_KDF_PARAM_SALT,
  989. EVP_PKEY_OP_DERIVE,
  990. EVP_PKEY_CTRL_SCRYPT_SALT,
  991. salt, saltlen);
  992. }
  993. static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
  994. int op, int ctrl, uint64_t val)
  995. {
  996. OSSL_PARAM uint64_params[2], *p = uint64_params;
  997. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  998. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  999. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1000. return -2;
  1001. }
  1002. /* Code below to be removed when legacy support is dropped. */
  1003. if (ctx->op.kex.algctx == NULL)
  1004. return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
  1005. /* end of legacy support */
  1006. *p++ = OSSL_PARAM_construct_uint64(param, &val);
  1007. *p = OSSL_PARAM_construct_end();
  1008. return EVP_PKEY_CTX_set_params(ctx, uint64_params);
  1009. }
  1010. int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
  1011. {
  1012. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
  1013. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
  1014. n);
  1015. }
  1016. int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
  1017. {
  1018. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
  1019. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
  1020. r);
  1021. }
  1022. int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
  1023. {
  1024. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
  1025. EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
  1026. p);
  1027. }
  1028. int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
  1029. uint64_t maxmem_bytes)
  1030. {
  1031. return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
  1032. EVP_PKEY_OP_DERIVE,
  1033. EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
  1034. maxmem_bytes);
  1035. }
  1036. int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
  1037. int keylen)
  1038. {
  1039. return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
  1040. OSSL_PKEY_PARAM_PRIV_KEY,
  1041. EVP_PKEY_OP_KEYGEN,
  1042. EVP_PKEY_CTRL_SET_MAC_KEY,
  1043. key, keylen);
  1044. }
  1045. int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
  1046. {
  1047. OSSL_PARAM params[2], *p = params;
  1048. if (ctx == NULL || op == NULL) {
  1049. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  1050. return 0;
  1051. }
  1052. if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1053. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1054. return -2;
  1055. }
  1056. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
  1057. (char *)op, 0);
  1058. *p = OSSL_PARAM_construct_end();
  1059. return EVP_PKEY_CTX_set_params(ctx, params);
  1060. }
  1061. int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
  1062. {
  1063. OSSL_PARAM params[2], *p = params;
  1064. int ret;
  1065. if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1066. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1067. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1068. return -2;
  1069. }
  1070. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
  1071. /*
  1072. * Cast away the const. This is
  1073. * read only so should be safe
  1074. */
  1075. (void *)id, (size_t)len);
  1076. *p++ = OSSL_PARAM_construct_end();
  1077. ret = evp_pkey_ctx_set_params_strict(ctx, params);
  1078. if (ret == -2)
  1079. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1080. return ret;
  1081. }
  1082. int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
  1083. {
  1084. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1085. EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
  1086. }
  1087. static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
  1088. {
  1089. int ret;
  1090. void *tmp_id = NULL;
  1091. OSSL_PARAM params[2], *p = params;
  1092. if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1093. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1094. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1095. return -2;
  1096. }
  1097. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
  1098. &tmp_id, 0);
  1099. *p++ = OSSL_PARAM_construct_end();
  1100. ret = evp_pkey_ctx_get_params_strict(ctx, params);
  1101. if (ret == -2) {
  1102. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1103. } else if (ret > 0) {
  1104. size_t tmp_id_len = params[0].return_size;
  1105. if (id != NULL)
  1106. memcpy(id, tmp_id, tmp_id_len);
  1107. if (id_len != NULL)
  1108. *id_len = tmp_id_len;
  1109. }
  1110. return ret;
  1111. }
  1112. int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
  1113. {
  1114. return get1_id_data(ctx, id, NULL);
  1115. }
  1116. int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
  1117. {
  1118. return get1_id_data(ctx, NULL, id_len);
  1119. }
  1120. int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
  1121. {
  1122. return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
  1123. }
  1124. int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
  1125. {
  1126. return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
  1127. EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
  1128. }
  1129. static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1130. int cmd, int p1, void *p2)
  1131. {
  1132. int ret = 0;
  1133. /*
  1134. * If the method has a |digest_custom| function, we can relax the
  1135. * operation type check, since this can be called before the operation
  1136. * is initialized.
  1137. */
  1138. if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
  1139. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  1140. ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
  1141. return -1;
  1142. }
  1143. if ((optype != -1) && !(ctx->operation & optype)) {
  1144. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1145. return -1;
  1146. }
  1147. }
  1148. switch (evp_pkey_ctx_state(ctx)) {
  1149. case EVP_PKEY_STATE_PROVIDER:
  1150. return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
  1151. case EVP_PKEY_STATE_UNKNOWN:
  1152. case EVP_PKEY_STATE_LEGACY:
  1153. if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
  1154. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1155. return -2;
  1156. }
  1157. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  1158. return -1;
  1159. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  1160. if (ret == -2)
  1161. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1162. break;
  1163. }
  1164. return ret;
  1165. }
  1166. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1167. int cmd, int p1, void *p2)
  1168. {
  1169. int ret = 0;
  1170. if (ctx == NULL) {
  1171. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1172. return -2;
  1173. }
  1174. /* If unsupported, we don't want that reported here */
  1175. ERR_set_mark();
  1176. ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
  1177. cmd, NULL, p2, p1);
  1178. if (ret == -2) {
  1179. ERR_pop_to_mark();
  1180. } else {
  1181. ERR_clear_last_mark();
  1182. /*
  1183. * If there was an error, there was an error.
  1184. * If the operation isn't initialized yet, we also return, as
  1185. * the saved values will be used then anyway.
  1186. */
  1187. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1188. return ret;
  1189. }
  1190. return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
  1191. }
  1192. int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
  1193. int cmd, uint64_t value)
  1194. {
  1195. return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
  1196. }
  1197. static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
  1198. const char *name, const char *value)
  1199. {
  1200. int ret = 0;
  1201. if (ctx == NULL) {
  1202. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1203. return -2;
  1204. }
  1205. switch (evp_pkey_ctx_state(ctx)) {
  1206. case EVP_PKEY_STATE_PROVIDER:
  1207. return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
  1208. case EVP_PKEY_STATE_UNKNOWN:
  1209. case EVP_PKEY_STATE_LEGACY:
  1210. if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
  1211. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1212. return -2;
  1213. }
  1214. if (strcmp(name, "digest") == 0)
  1215. ret = EVP_PKEY_CTX_md(ctx,
  1216. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  1217. EVP_PKEY_CTRL_MD, value);
  1218. else
  1219. ret = ctx->pmeth->ctrl_str(ctx, name, value);
  1220. break;
  1221. }
  1222. return ret;
  1223. }
  1224. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  1225. const char *name, const char *value)
  1226. {
  1227. int ret = 0;
  1228. /* If unsupported, we don't want that reported here */
  1229. ERR_set_mark();
  1230. ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
  1231. name, value, strlen(value) + 1);
  1232. if (ret == -2) {
  1233. ERR_pop_to_mark();
  1234. } else {
  1235. ERR_clear_last_mark();
  1236. /*
  1237. * If there was an error, there was an error.
  1238. * If the operation isn't initialized yet, we also return, as
  1239. * the saved values will be used then anyway.
  1240. */
  1241. if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
  1242. return ret;
  1243. }
  1244. return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
  1245. }
  1246. static int decode_cmd(int cmd, const char *name)
  1247. {
  1248. if (cmd == -1) {
  1249. /*
  1250. * The consequence of the assertion not being true is that this
  1251. * function will return -1, which will cause the calling functions
  1252. * to signal that the command is unsupported... in non-debug mode.
  1253. */
  1254. if (ossl_assert(name != NULL))
  1255. if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
  1256. cmd = EVP_PKEY_CTRL_SET1_ID;
  1257. }
  1258. return cmd;
  1259. }
  1260. static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
  1261. int keytype, int optype,
  1262. int cmd, const char *name,
  1263. const void *data, size_t data_len)
  1264. {
  1265. /*
  1266. * Check that it's one of the supported commands. The ctrl commands
  1267. * number cases here must correspond to the cases in the bottom switch
  1268. * in this function.
  1269. */
  1270. switch (cmd = decode_cmd(cmd, name)) {
  1271. case EVP_PKEY_CTRL_SET1_ID:
  1272. break;
  1273. default:
  1274. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1275. return -2;
  1276. }
  1277. if (keytype != -1) {
  1278. switch (evp_pkey_ctx_state(ctx)) {
  1279. case EVP_PKEY_STATE_PROVIDER:
  1280. if (ctx->keymgmt == NULL) {
  1281. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1282. return -2;
  1283. }
  1284. if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
  1285. evp_pkey_type2name(keytype))) {
  1286. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1287. return -1;
  1288. }
  1289. break;
  1290. case EVP_PKEY_STATE_UNKNOWN:
  1291. case EVP_PKEY_STATE_LEGACY:
  1292. if (ctx->pmeth == NULL) {
  1293. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1294. return -2;
  1295. }
  1296. if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
  1297. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1298. return -1;
  1299. }
  1300. break;
  1301. }
  1302. }
  1303. if (optype != -1 && (ctx->operation & optype) == 0) {
  1304. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  1305. return -1;
  1306. }
  1307. switch (cmd) {
  1308. case EVP_PKEY_CTRL_SET1_ID:
  1309. evp_pkey_ctx_free_cached_data(ctx, cmd, name);
  1310. if (name != NULL) {
  1311. ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
  1312. if (ctx->cached_parameters.dist_id_name == NULL)
  1313. return 0;
  1314. }
  1315. if (data_len > 0) {
  1316. ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
  1317. if (ctx->cached_parameters.dist_id == NULL)
  1318. return 0;
  1319. }
  1320. ctx->cached_parameters.dist_id_set = 1;
  1321. ctx->cached_parameters.dist_id_len = data_len;
  1322. break;
  1323. }
  1324. return 1;
  1325. }
  1326. static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
  1327. int cmd, const char *name)
  1328. {
  1329. cmd = decode_cmd(cmd, name);
  1330. switch (cmd) {
  1331. case EVP_PKEY_CTRL_SET1_ID:
  1332. OPENSSL_free(ctx->cached_parameters.dist_id);
  1333. OPENSSL_free(ctx->cached_parameters.dist_id_name);
  1334. ctx->cached_parameters.dist_id = NULL;
  1335. ctx->cached_parameters.dist_id_name = NULL;
  1336. break;
  1337. }
  1338. }
  1339. static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
  1340. {
  1341. evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
  1342. }
  1343. int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
  1344. {
  1345. int ret = 1;
  1346. if (ret && ctx->cached_parameters.dist_id_set) {
  1347. const char *name = ctx->cached_parameters.dist_id_name;
  1348. const void *val = ctx->cached_parameters.dist_id;
  1349. size_t len = ctx->cached_parameters.dist_id_len;
  1350. if (name != NULL)
  1351. ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
  1352. else
  1353. ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
  1354. EVP_PKEY_CTRL_SET1_ID,
  1355. (int)len, (void *)val);
  1356. }
  1357. return ret;
  1358. }
  1359. OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
  1360. {
  1361. return ctx->libctx;
  1362. }
  1363. const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
  1364. {
  1365. return ctx->propquery;
  1366. }
  1367. const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
  1368. {
  1369. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  1370. if (ctx->op.sig.signature != NULL)
  1371. return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
  1372. } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  1373. if (ctx->op.kex.exchange != NULL)
  1374. return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
  1375. } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
  1376. if (ctx->op.encap.kem != NULL)
  1377. return EVP_KEM_get0_provider(ctx->op.encap.kem);
  1378. } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  1379. if (ctx->op.ciph.cipher != NULL)
  1380. return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
  1381. } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1382. if (ctx->keymgmt != NULL)
  1383. return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
  1384. }
  1385. return NULL;
  1386. }
  1387. /* Utility functions to send a string of hex string to a ctrl */
  1388. int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
  1389. {
  1390. size_t len;
  1391. len = strlen(str);
  1392. if (len > INT_MAX)
  1393. return -1;
  1394. return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
  1395. }
  1396. int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
  1397. {
  1398. unsigned char *bin;
  1399. long binlen;
  1400. int rv = -1;
  1401. bin = OPENSSL_hexstr2buf(hex, &binlen);
  1402. if (bin == NULL)
  1403. return 0;
  1404. if (binlen <= INT_MAX)
  1405. rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
  1406. OPENSSL_free(bin);
  1407. return rv;
  1408. }
  1409. /* Pass a message digest to a ctrl */
  1410. int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
  1411. {
  1412. const EVP_MD *m;
  1413. if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
  1414. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
  1415. return 0;
  1416. }
  1417. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
  1418. }
  1419. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  1420. {
  1421. return ctx->operation;
  1422. }
  1423. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  1424. {
  1425. ctx->keygen_info = dat;
  1426. ctx->keygen_info_count = datlen;
  1427. }
  1428. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  1429. {
  1430. ctx->data = data;
  1431. }
  1432. void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
  1433. {
  1434. return ctx->data;
  1435. }
  1436. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  1437. {
  1438. return ctx->pkey;
  1439. }
  1440. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  1441. {
  1442. return ctx->peerkey;
  1443. }
  1444. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  1445. {
  1446. ctx->app_data = data;
  1447. }
  1448. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  1449. {
  1450. return ctx->app_data;
  1451. }
  1452. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  1453. int (*init) (EVP_PKEY_CTX *ctx))
  1454. {
  1455. pmeth->init = init;
  1456. }
  1457. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  1458. int (*copy) (EVP_PKEY_CTX *dst,
  1459. const EVP_PKEY_CTX *src))
  1460. {
  1461. pmeth->copy = copy;
  1462. }
  1463. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  1464. void (*cleanup) (EVP_PKEY_CTX *ctx))
  1465. {
  1466. pmeth->cleanup = cleanup;
  1467. }
  1468. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  1469. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  1470. int (*paramgen) (EVP_PKEY_CTX *ctx,
  1471. EVP_PKEY *pkey))
  1472. {
  1473. pmeth->paramgen_init = paramgen_init;
  1474. pmeth->paramgen = paramgen;
  1475. }
  1476. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  1477. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  1478. int (*keygen) (EVP_PKEY_CTX *ctx,
  1479. EVP_PKEY *pkey))
  1480. {
  1481. pmeth->keygen_init = keygen_init;
  1482. pmeth->keygen = keygen;
  1483. }
  1484. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  1485. int (*sign_init) (EVP_PKEY_CTX *ctx),
  1486. int (*sign) (EVP_PKEY_CTX *ctx,
  1487. unsigned char *sig, size_t *siglen,
  1488. const unsigned char *tbs,
  1489. size_t tbslen))
  1490. {
  1491. pmeth->sign_init = sign_init;
  1492. pmeth->sign = sign;
  1493. }
  1494. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  1495. int (*verify_init) (EVP_PKEY_CTX *ctx),
  1496. int (*verify) (EVP_PKEY_CTX *ctx,
  1497. const unsigned char *sig,
  1498. size_t siglen,
  1499. const unsigned char *tbs,
  1500. size_t tbslen))
  1501. {
  1502. pmeth->verify_init = verify_init;
  1503. pmeth->verify = verify;
  1504. }
  1505. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  1506. int (*verify_recover_init) (EVP_PKEY_CTX
  1507. *ctx),
  1508. int (*verify_recover) (EVP_PKEY_CTX
  1509. *ctx,
  1510. unsigned char
  1511. *sig,
  1512. size_t *siglen,
  1513. const unsigned
  1514. char *tbs,
  1515. size_t tbslen))
  1516. {
  1517. pmeth->verify_recover_init = verify_recover_init;
  1518. pmeth->verify_recover = verify_recover;
  1519. }
  1520. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  1521. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  1522. EVP_MD_CTX *mctx),
  1523. int (*signctx) (EVP_PKEY_CTX *ctx,
  1524. unsigned char *sig,
  1525. size_t *siglen,
  1526. EVP_MD_CTX *mctx))
  1527. {
  1528. pmeth->signctx_init = signctx_init;
  1529. pmeth->signctx = signctx;
  1530. }
  1531. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  1532. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  1533. EVP_MD_CTX *mctx),
  1534. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  1535. const unsigned char *sig,
  1536. int siglen,
  1537. EVP_MD_CTX *mctx))
  1538. {
  1539. pmeth->verifyctx_init = verifyctx_init;
  1540. pmeth->verifyctx = verifyctx;
  1541. }
  1542. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  1543. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  1544. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  1545. unsigned char *out,
  1546. size_t *outlen,
  1547. const unsigned char *in,
  1548. size_t inlen))
  1549. {
  1550. pmeth->encrypt_init = encrypt_init;
  1551. pmeth->encrypt = encryptfn;
  1552. }
  1553. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  1554. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  1555. int (*decrypt) (EVP_PKEY_CTX *ctx,
  1556. unsigned char *out,
  1557. size_t *outlen,
  1558. const unsigned char *in,
  1559. size_t inlen))
  1560. {
  1561. pmeth->decrypt_init = decrypt_init;
  1562. pmeth->decrypt = decrypt;
  1563. }
  1564. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  1565. int (*derive_init) (EVP_PKEY_CTX *ctx),
  1566. int (*derive) (EVP_PKEY_CTX *ctx,
  1567. unsigned char *key,
  1568. size_t *keylen))
  1569. {
  1570. pmeth->derive_init = derive_init;
  1571. pmeth->derive = derive;
  1572. }
  1573. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  1574. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  1575. void *p2),
  1576. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  1577. const char *type,
  1578. const char *value))
  1579. {
  1580. pmeth->ctrl = ctrl;
  1581. pmeth->ctrl_str = ctrl_str;
  1582. }
  1583. void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
  1584. int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1585. const unsigned char *tbs, size_t tbslen))
  1586. {
  1587. pmeth->digestsign = digestsign;
  1588. }
  1589. void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
  1590. int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  1591. size_t siglen, const unsigned char *tbs,
  1592. size_t tbslen))
  1593. {
  1594. pmeth->digestverify = digestverify;
  1595. }
  1596. void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
  1597. int (*check) (EVP_PKEY *pkey))
  1598. {
  1599. pmeth->check = check;
  1600. }
  1601. void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
  1602. int (*check) (EVP_PKEY *pkey))
  1603. {
  1604. pmeth->public_check = check;
  1605. }
  1606. void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
  1607. int (*check) (EVP_PKEY *pkey))
  1608. {
  1609. pmeth->param_check = check;
  1610. }
  1611. void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
  1612. int (*digest_custom) (EVP_PKEY_CTX *ctx,
  1613. EVP_MD_CTX *mctx))
  1614. {
  1615. pmeth->digest_custom = digest_custom;
  1616. }
  1617. void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
  1618. int (**pinit) (EVP_PKEY_CTX *ctx))
  1619. {
  1620. *pinit = pmeth->init;
  1621. }
  1622. void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
  1623. int (**pcopy) (EVP_PKEY_CTX *dst,
  1624. const EVP_PKEY_CTX *src))
  1625. {
  1626. *pcopy = pmeth->copy;
  1627. }
  1628. void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
  1629. void (**pcleanup) (EVP_PKEY_CTX *ctx))
  1630. {
  1631. *pcleanup = pmeth->cleanup;
  1632. }
  1633. void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
  1634. int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
  1635. int (**pparamgen) (EVP_PKEY_CTX *ctx,
  1636. EVP_PKEY *pkey))
  1637. {
  1638. if (pparamgen_init)
  1639. *pparamgen_init = pmeth->paramgen_init;
  1640. if (pparamgen)
  1641. *pparamgen = pmeth->paramgen;
  1642. }
  1643. void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
  1644. int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
  1645. int (**pkeygen) (EVP_PKEY_CTX *ctx,
  1646. EVP_PKEY *pkey))
  1647. {
  1648. if (pkeygen_init)
  1649. *pkeygen_init = pmeth->keygen_init;
  1650. if (pkeygen)
  1651. *pkeygen = pmeth->keygen;
  1652. }
  1653. void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
  1654. int (**psign_init) (EVP_PKEY_CTX *ctx),
  1655. int (**psign) (EVP_PKEY_CTX *ctx,
  1656. unsigned char *sig, size_t *siglen,
  1657. const unsigned char *tbs,
  1658. size_t tbslen))
  1659. {
  1660. if (psign_init)
  1661. *psign_init = pmeth->sign_init;
  1662. if (psign)
  1663. *psign = pmeth->sign;
  1664. }
  1665. void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
  1666. int (**pverify_init) (EVP_PKEY_CTX *ctx),
  1667. int (**pverify) (EVP_PKEY_CTX *ctx,
  1668. const unsigned char *sig,
  1669. size_t siglen,
  1670. const unsigned char *tbs,
  1671. size_t tbslen))
  1672. {
  1673. if (pverify_init)
  1674. *pverify_init = pmeth->verify_init;
  1675. if (pverify)
  1676. *pverify = pmeth->verify;
  1677. }
  1678. void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
  1679. int (**pverify_recover_init) (EVP_PKEY_CTX
  1680. *ctx),
  1681. int (**pverify_recover) (EVP_PKEY_CTX
  1682. *ctx,
  1683. unsigned char
  1684. *sig,
  1685. size_t *siglen,
  1686. const unsigned
  1687. char *tbs,
  1688. size_t tbslen))
  1689. {
  1690. if (pverify_recover_init)
  1691. *pverify_recover_init = pmeth->verify_recover_init;
  1692. if (pverify_recover)
  1693. *pverify_recover = pmeth->verify_recover;
  1694. }
  1695. void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
  1696. int (**psignctx_init) (EVP_PKEY_CTX *ctx,
  1697. EVP_MD_CTX *mctx),
  1698. int (**psignctx) (EVP_PKEY_CTX *ctx,
  1699. unsigned char *sig,
  1700. size_t *siglen,
  1701. EVP_MD_CTX *mctx))
  1702. {
  1703. if (psignctx_init)
  1704. *psignctx_init = pmeth->signctx_init;
  1705. if (psignctx)
  1706. *psignctx = pmeth->signctx;
  1707. }
  1708. void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
  1709. int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
  1710. EVP_MD_CTX *mctx),
  1711. int (**pverifyctx) (EVP_PKEY_CTX *ctx,
  1712. const unsigned char *sig,
  1713. int siglen,
  1714. EVP_MD_CTX *mctx))
  1715. {
  1716. if (pverifyctx_init)
  1717. *pverifyctx_init = pmeth->verifyctx_init;
  1718. if (pverifyctx)
  1719. *pverifyctx = pmeth->verifyctx;
  1720. }
  1721. void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
  1722. int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
  1723. int (**pencryptfn) (EVP_PKEY_CTX *ctx,
  1724. unsigned char *out,
  1725. size_t *outlen,
  1726. const unsigned char *in,
  1727. size_t inlen))
  1728. {
  1729. if (pencrypt_init)
  1730. *pencrypt_init = pmeth->encrypt_init;
  1731. if (pencryptfn)
  1732. *pencryptfn = pmeth->encrypt;
  1733. }
  1734. void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
  1735. int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
  1736. int (**pdecrypt) (EVP_PKEY_CTX *ctx,
  1737. unsigned char *out,
  1738. size_t *outlen,
  1739. const unsigned char *in,
  1740. size_t inlen))
  1741. {
  1742. if (pdecrypt_init)
  1743. *pdecrypt_init = pmeth->decrypt_init;
  1744. if (pdecrypt)
  1745. *pdecrypt = pmeth->decrypt;
  1746. }
  1747. void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
  1748. int (**pderive_init) (EVP_PKEY_CTX *ctx),
  1749. int (**pderive) (EVP_PKEY_CTX *ctx,
  1750. unsigned char *key,
  1751. size_t *keylen))
  1752. {
  1753. if (pderive_init)
  1754. *pderive_init = pmeth->derive_init;
  1755. if (pderive)
  1756. *pderive = pmeth->derive;
  1757. }
  1758. void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
  1759. int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  1760. void *p2),
  1761. int (**pctrl_str) (EVP_PKEY_CTX *ctx,
  1762. const char *type,
  1763. const char *value))
  1764. {
  1765. if (pctrl)
  1766. *pctrl = pmeth->ctrl;
  1767. if (pctrl_str)
  1768. *pctrl_str = pmeth->ctrl_str;
  1769. }
  1770. void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
  1771. int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  1772. const unsigned char *tbs, size_t tbslen))
  1773. {
  1774. if (digestsign)
  1775. *digestsign = pmeth->digestsign;
  1776. }
  1777. void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
  1778. int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  1779. size_t siglen, const unsigned char *tbs,
  1780. size_t tbslen))
  1781. {
  1782. if (digestverify)
  1783. *digestverify = pmeth->digestverify;
  1784. }
  1785. void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
  1786. int (**pcheck) (EVP_PKEY *pkey))
  1787. {
  1788. if (pcheck != NULL)
  1789. *pcheck = pmeth->check;
  1790. }
  1791. void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
  1792. int (**pcheck) (EVP_PKEY *pkey))
  1793. {
  1794. if (pcheck != NULL)
  1795. *pcheck = pmeth->public_check;
  1796. }
  1797. void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
  1798. int (**pcheck) (EVP_PKEY *pkey))
  1799. {
  1800. if (pcheck != NULL)
  1801. *pcheck = pmeth->param_check;
  1802. }
  1803. void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
  1804. int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
  1805. EVP_MD_CTX *mctx))
  1806. {
  1807. if (pdigest_custom != NULL)
  1808. *pdigest_custom = pmeth->digest_custom;
  1809. }
  1810. #endif /* FIPS_MODULE */