2
0

exchange.c 19 KB

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