exchange.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/crypto.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/err.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/refcount.h"
  14. #include "internal/provider.h"
  15. #include "internal/core.h"
  16. #include "internal/numbers.h" /* includes SIZE_MAX */
  17. #include "crypto/evp.h"
  18. #include "evp_local.h"
  19. static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
  20. {
  21. EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
  22. if (exchange == NULL)
  23. return NULL;
  24. exchange->lock = CRYPTO_THREAD_lock_new();
  25. if (exchange->lock == NULL) {
  26. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  27. OPENSSL_free(exchange);
  28. return NULL;
  29. }
  30. exchange->prov = prov;
  31. ossl_provider_up_ref(prov);
  32. exchange->refcnt = 1;
  33. return exchange;
  34. }
  35. static void *evp_keyexch_from_algorithm(int name_id,
  36. const OSSL_ALGORITHM *algodef,
  37. OSSL_PROVIDER *prov)
  38. {
  39. const OSSL_DISPATCH *fns = algodef->implementation;
  40. EVP_KEYEXCH *exchange = NULL;
  41. int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0;
  42. if ((exchange = evp_keyexch_new(prov)) == NULL) {
  43. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  44. goto err;
  45. }
  46. exchange->name_id = name_id;
  47. if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
  48. goto err;
  49. exchange->description = algodef->algorithm_description;
  50. for (; fns->function_id != 0; fns++) {
  51. switch (fns->function_id) {
  52. case OSSL_FUNC_KEYEXCH_NEWCTX:
  53. if (exchange->newctx != NULL)
  54. break;
  55. exchange->newctx = OSSL_FUNC_keyexch_newctx(fns);
  56. fncnt++;
  57. break;
  58. case OSSL_FUNC_KEYEXCH_INIT:
  59. if (exchange->init != NULL)
  60. break;
  61. exchange->init = OSSL_FUNC_keyexch_init(fns);
  62. fncnt++;
  63. break;
  64. case OSSL_FUNC_KEYEXCH_SET_PEER:
  65. if (exchange->set_peer != NULL)
  66. break;
  67. exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns);
  68. break;
  69. case OSSL_FUNC_KEYEXCH_DERIVE:
  70. if (exchange->derive != NULL)
  71. break;
  72. exchange->derive = OSSL_FUNC_keyexch_derive(fns);
  73. fncnt++;
  74. break;
  75. case OSSL_FUNC_KEYEXCH_FREECTX:
  76. if (exchange->freectx != NULL)
  77. break;
  78. exchange->freectx = OSSL_FUNC_keyexch_freectx(fns);
  79. fncnt++;
  80. break;
  81. case OSSL_FUNC_KEYEXCH_DUPCTX:
  82. if (exchange->dupctx != NULL)
  83. break;
  84. exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns);
  85. break;
  86. case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS:
  87. if (exchange->get_ctx_params != NULL)
  88. break;
  89. exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns);
  90. gparamfncnt++;
  91. break;
  92. case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS:
  93. if (exchange->gettable_ctx_params != NULL)
  94. break;
  95. exchange->gettable_ctx_params
  96. = OSSL_FUNC_keyexch_gettable_ctx_params(fns);
  97. gparamfncnt++;
  98. break;
  99. case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
  100. if (exchange->set_ctx_params != NULL)
  101. break;
  102. exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns);
  103. sparamfncnt++;
  104. break;
  105. case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
  106. if (exchange->settable_ctx_params != NULL)
  107. break;
  108. exchange->settable_ctx_params
  109. = OSSL_FUNC_keyexch_settable_ctx_params(fns);
  110. sparamfncnt++;
  111. break;
  112. }
  113. }
  114. if (fncnt != 4
  115. || (gparamfncnt != 0 && gparamfncnt != 2)
  116. || (sparamfncnt != 0 && sparamfncnt != 2)) {
  117. /*
  118. * In order to be a consistent set of functions we must have at least
  119. * a complete set of "exchange" functions: init, derive, newctx,
  120. * and freectx. The set_ctx_params and settable_ctx_params functions are
  121. * optional, but if one of them is present then the other one must also
  122. * be present. Same goes for get_ctx_params and gettable_ctx_params.
  123. * The dupctx and set_peer functions are optional.
  124. */
  125. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  126. goto err;
  127. }
  128. return exchange;
  129. err:
  130. EVP_KEYEXCH_free(exchange);
  131. return NULL;
  132. }
  133. void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
  134. {
  135. int i;
  136. if (exchange == NULL)
  137. return;
  138. CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
  139. if (i > 0)
  140. return;
  141. OPENSSL_free(exchange->type_name);
  142. ossl_provider_free(exchange->prov);
  143. CRYPTO_THREAD_lock_free(exchange->lock);
  144. OPENSSL_free(exchange);
  145. }
  146. int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
  147. {
  148. int ref = 0;
  149. CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
  150. return 1;
  151. }
  152. OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange)
  153. {
  154. return exchange->prov;
  155. }
  156. EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  157. const char *properties)
  158. {
  159. return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
  160. evp_keyexch_from_algorithm,
  161. (int (*)(void *))EVP_KEYEXCH_up_ref,
  162. (void (*)(void *))EVP_KEYEXCH_free);
  163. }
  164. EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
  165. const char *algorithm,
  166. const char *properties)
  167. {
  168. return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH,
  169. algorithm, properties,
  170. evp_keyexch_from_algorithm,
  171. (int (*)(void *))EVP_KEYEXCH_up_ref,
  172. (void (*)(void *))EVP_KEYEXCH_free);
  173. }
  174. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
  175. {
  176. return EVP_PKEY_derive_init_ex(ctx, NULL);
  177. }
  178. int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
  179. {
  180. int ret;
  181. void *provkey = NULL;
  182. EVP_KEYEXCH *exchange = NULL;
  183. EVP_KEYMGMT *tmp_keymgmt = NULL;
  184. const OSSL_PROVIDER *tmp_prov = NULL;
  185. const char *supported_exch = NULL;
  186. int iter;
  187. if (ctx == NULL) {
  188. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  189. return -2;
  190. }
  191. evp_pkey_ctx_free_old_ops(ctx);
  192. ctx->operation = EVP_PKEY_OP_DERIVE;
  193. ERR_set_mark();
  194. if (evp_pkey_ctx_is_legacy(ctx))
  195. goto legacy;
  196. /*
  197. * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create
  198. * a blank one.
  199. */
  200. if (ctx->pkey == NULL) {
  201. EVP_PKEY *pkey = EVP_PKEY_new();
  202. if (pkey == NULL
  203. || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt)
  204. || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) {
  205. ERR_clear_last_mark();
  206. EVP_PKEY_free(pkey);
  207. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  208. goto err;
  209. }
  210. ctx->pkey = pkey;
  211. }
  212. /*
  213. * Try to derive the supported exch from |ctx->keymgmt|.
  214. */
  215. if (!ossl_assert(ctx->pkey->keymgmt == NULL
  216. || ctx->pkey->keymgmt == ctx->keymgmt)) {
  217. ERR_clear_last_mark();
  218. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  219. goto err;
  220. }
  221. supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
  222. OSSL_OP_KEYEXCH);
  223. if (supported_exch == NULL) {
  224. ERR_clear_last_mark();
  225. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  226. goto err;
  227. }
  228. /*
  229. * We perform two iterations:
  230. *
  231. * 1. Do the normal exchange fetch, using the fetching data given by
  232. * the EVP_PKEY_CTX.
  233. * 2. Do the provider specific exchange fetch, from the same provider
  234. * as |ctx->keymgmt|
  235. *
  236. * We then try to fetch the keymgmt from the same provider as the
  237. * exchange, and try to export |ctx->pkey| to that keymgmt (when
  238. * this keymgmt happens to be the same as |ctx->keymgmt|, the export
  239. * is a no-op, but we call it anyway to not complicate the code even
  240. * more).
  241. * If the export call succeeds (returns a non-NULL provider key pointer),
  242. * we're done and can perform the operation itself. If not, we perform
  243. * the second iteration, or jump to legacy.
  244. */
  245. for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
  246. EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
  247. /*
  248. * If we're on the second iteration, free the results from the first.
  249. * They are NULL on the first iteration, so no need to check what
  250. * iteration we're on.
  251. */
  252. EVP_KEYEXCH_free(exchange);
  253. EVP_KEYMGMT_free(tmp_keymgmt);
  254. switch (iter) {
  255. case 1:
  256. exchange =
  257. EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery);
  258. if (exchange != NULL)
  259. tmp_prov = EVP_KEYEXCH_get0_provider(exchange);
  260. break;
  261. case 2:
  262. tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
  263. exchange =
  264. evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
  265. supported_exch, ctx->propquery);
  266. if (exchange == NULL)
  267. goto legacy;
  268. break;
  269. }
  270. if (exchange == NULL)
  271. continue;
  272. /*
  273. * Ensure that the key is provided, either natively, or as a cached
  274. * export. We start by fetching the keymgmt with the same name as
  275. * |ctx->keymgmt|, but from the provider of the exchange method, using
  276. * the same property query as when fetching the exchange method.
  277. * With the keymgmt we found (if we did), we try to export |ctx->pkey|
  278. * to it (evp_pkey_export_to_provider() is smart enough to only actually
  279. * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
  280. */
  281. tmp_keymgmt_tofree = tmp_keymgmt =
  282. evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
  283. EVP_KEYMGMT_get0_name(ctx->keymgmt),
  284. ctx->propquery);
  285. if (tmp_keymgmt != NULL)
  286. provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
  287. &tmp_keymgmt, ctx->propquery);
  288. if (tmp_keymgmt == NULL)
  289. EVP_KEYMGMT_free(tmp_keymgmt_tofree);
  290. }
  291. if (provkey == NULL) {
  292. EVP_KEYEXCH_free(exchange);
  293. goto legacy;
  294. }
  295. ERR_pop_to_mark();
  296. /* No more legacy from here down to legacy: */
  297. /* A Coverity false positive with up_ref/down_ref and free */
  298. /* coverity[use_after_free] */
  299. ctx->op.kex.exchange = exchange;
  300. /* A Coverity false positive with up_ref/down_ref and free */
  301. /* coverity[deref_arg] */
  302. ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
  303. if (ctx->op.kex.algctx == NULL) {
  304. /* The provider key can stay in the cache */
  305. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  306. goto err;
  307. }
  308. ret = exchange->init(ctx->op.kex.algctx, provkey, params);
  309. EVP_KEYMGMT_free(tmp_keymgmt);
  310. return ret ? 1 : 0;
  311. err:
  312. evp_pkey_ctx_free_old_ops(ctx);
  313. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  314. EVP_KEYMGMT_free(tmp_keymgmt);
  315. return 0;
  316. legacy:
  317. /*
  318. * If we don't have the full support we need with provided methods,
  319. * let's go see if legacy does.
  320. */
  321. ERR_pop_to_mark();
  322. #ifdef FIPS_MODULE
  323. return 0;
  324. #else
  325. if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
  326. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  327. return -2;
  328. }
  329. if (ctx->pmeth->derive_init == NULL)
  330. return 1;
  331. ret = ctx->pmeth->derive_init(ctx);
  332. if (ret <= 0)
  333. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  334. EVP_KEYMGMT_free(tmp_keymgmt);
  335. return ret;
  336. #endif
  337. }
  338. int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
  339. int validate_peer)
  340. {
  341. int ret = 0, check;
  342. void *provkey = NULL;
  343. EVP_PKEY_CTX *check_ctx = NULL;
  344. EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL;
  345. if (ctx == NULL) {
  346. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  347. return -1;
  348. }
  349. if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL)
  350. goto legacy;
  351. if (ctx->op.kex.exchange->set_peer == NULL) {
  352. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  353. return -2;
  354. }
  355. if (validate_peer) {
  356. check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery);
  357. if (check_ctx == NULL)
  358. return -1;
  359. check = EVP_PKEY_public_check(check_ctx);
  360. EVP_PKEY_CTX_free(check_ctx);
  361. if (check <= 0)
  362. return -1;
  363. }
  364. /*
  365. * Ensure that the |peer| is provided, either natively, or as a cached
  366. * export. We start by fetching the keymgmt with the same name as
  367. * |ctx->keymgmt|, but from the provider of the exchange method, using
  368. * the same property query as when fetching the exchange method.
  369. * With the keymgmt we found (if we did), we try to export |peer|
  370. * to it (evp_pkey_export_to_provider() is smart enough to only actually
  371. * export it if |tmp_keymgmt| is different from |peer|'s keymgmt)
  372. */
  373. tmp_keymgmt_tofree = tmp_keymgmt =
  374. evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)
  375. EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange),
  376. EVP_KEYMGMT_get0_name(ctx->keymgmt),
  377. ctx->propquery);
  378. if (tmp_keymgmt != NULL)
  379. /* A Coverity issue with up_ref/down_ref and free */
  380. /* coverity[pass_freed_arg] */
  381. provkey = evp_pkey_export_to_provider(peer, ctx->libctx,
  382. &tmp_keymgmt, ctx->propquery);
  383. EVP_KEYMGMT_free(tmp_keymgmt_tofree);
  384. /*
  385. * If making the key provided wasn't possible, legacy may be able to pick
  386. * it up
  387. */
  388. if (provkey == NULL)
  389. goto legacy;
  390. return ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey);
  391. legacy:
  392. #ifdef FIPS_MODULE
  393. return ret;
  394. #else
  395. if (ctx->pmeth == NULL
  396. || !(ctx->pmeth->derive != NULL
  397. || ctx->pmeth->encrypt != NULL
  398. || ctx->pmeth->decrypt != NULL)
  399. || ctx->pmeth->ctrl == NULL) {
  400. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  401. return -2;
  402. }
  403. if (ctx->operation != EVP_PKEY_OP_DERIVE
  404. && ctx->operation != EVP_PKEY_OP_ENCRYPT
  405. && ctx->operation != EVP_PKEY_OP_DECRYPT) {
  406. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  407. return -1;
  408. }
  409. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  410. if (ret <= 0)
  411. return ret;
  412. if (ret == 2)
  413. return 1;
  414. if (ctx->pkey == NULL) {
  415. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  416. return -1;
  417. }
  418. if (ctx->pkey->type != peer->type) {
  419. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
  420. return -1;
  421. }
  422. /*
  423. * For clarity. The error is if parameters in peer are
  424. * present (!missing) but don't match. EVP_PKEY_parameters_eq may return
  425. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  426. * (different key types) is impossible here because it is checked earlier.
  427. * -2 is OK for us here, as well as 1, so we can check for 0 only.
  428. */
  429. if (!EVP_PKEY_missing_parameters(peer) &&
  430. !EVP_PKEY_parameters_eq(ctx->pkey, peer)) {
  431. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
  432. return -1;
  433. }
  434. EVP_PKEY_free(ctx->peerkey);
  435. ctx->peerkey = peer;
  436. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  437. if (ret <= 0) {
  438. ctx->peerkey = NULL;
  439. return ret;
  440. }
  441. EVP_PKEY_up_ref(peer);
  442. return 1;
  443. #endif
  444. }
  445. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
  446. {
  447. return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1);
  448. }
  449. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
  450. {
  451. int ret;
  452. if (ctx == NULL || pkeylen == NULL) {
  453. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  454. return -1;
  455. }
  456. if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  457. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  458. return -1;
  459. }
  460. if (ctx->op.kex.algctx == NULL)
  461. goto legacy;
  462. ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen,
  463. key != NULL ? *pkeylen : 0);
  464. return ret;
  465. legacy:
  466. if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
  467. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  468. return -2;
  469. }
  470. M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
  471. return ctx->pmeth->derive(ctx, key, pkeylen);
  472. }
  473. int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch)
  474. {
  475. return keyexch->name_id;
  476. }
  477. const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch)
  478. {
  479. return keyexch->type_name;
  480. }
  481. const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch)
  482. {
  483. return keyexch->description;
  484. }
  485. int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
  486. {
  487. return keyexch != NULL
  488. && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name);
  489. }
  490. void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx,
  491. void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
  492. void *arg)
  493. {
  494. evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
  495. (void (*)(void *, void *))fn, arg,
  496. evp_keyexch_from_algorithm,
  497. (int (*)(void *))EVP_KEYEXCH_up_ref,
  498. (void (*)(void *))EVP_KEYEXCH_free);
  499. }
  500. int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
  501. void (*fn)(const char *name, void *data),
  502. void *data)
  503. {
  504. if (keyexch->prov != NULL)
  505. return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
  506. return 1;
  507. }
  508. const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch)
  509. {
  510. void *provctx;
  511. if (keyexch == NULL || keyexch->gettable_ctx_params == NULL)
  512. return NULL;
  513. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
  514. return keyexch->gettable_ctx_params(NULL, provctx);
  515. }
  516. const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch)
  517. {
  518. void *provctx;
  519. if (keyexch == NULL || keyexch->settable_ctx_params == NULL)
  520. return NULL;
  521. provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch));
  522. return keyexch->settable_ctx_params(NULL, provctx);
  523. }