2
0

pmeth_lib.c 68 KB

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