pmeth_lib.c 69 KB

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