asymcipher.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright 2006-2025 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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/evp.h>
  13. #include "internal/cryptlib.h"
  14. #include "internal/provider.h"
  15. #include "internal/core.h"
  16. #include "crypto/evp.h"
  17. #include "evp_local.h"
  18. static void evp_asym_cipher_free(void *data)
  19. {
  20. EVP_ASYM_CIPHER_free(data);
  21. }
  22. static int evp_asym_cipher_up_ref(void *data)
  23. {
  24. return EVP_ASYM_CIPHER_up_ref(data);
  25. }
  26. static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation,
  27. const OSSL_PARAM params[])
  28. {
  29. int ret = 0;
  30. void *provkey = NULL;
  31. EVP_ASYM_CIPHER *cipher = NULL;
  32. const char *desc;
  33. EVP_KEYMGMT *tmp_keymgmt = NULL;
  34. const OSSL_PROVIDER *tmp_prov = NULL;
  35. const char *supported_ciph = NULL;
  36. int iter;
  37. if (ctx == NULL) {
  38. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  39. return -2;
  40. }
  41. evp_pkey_ctx_free_old_ops(ctx);
  42. ctx->operation = operation;
  43. ERR_set_mark();
  44. if (evp_pkey_ctx_is_legacy(ctx))
  45. goto legacy;
  46. if (ctx->pkey == NULL) {
  47. ERR_clear_last_mark();
  48. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  49. goto err;
  50. }
  51. /*
  52. * Try to derive the supported asym cipher from |ctx->keymgmt|.
  53. */
  54. if (!ossl_assert(ctx->pkey->keymgmt == NULL
  55. || ctx->pkey->keymgmt == ctx->keymgmt)) {
  56. ERR_clear_last_mark();
  57. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  58. goto err;
  59. }
  60. supported_ciph
  61. = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
  62. OSSL_OP_ASYM_CIPHER);
  63. if (supported_ciph == NULL) {
  64. ERR_clear_last_mark();
  65. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  66. goto err;
  67. }
  68. /*
  69. * We perform two iterations:
  70. *
  71. * 1. Do the normal asym cipher fetch, using the fetching data given by
  72. * the EVP_PKEY_CTX.
  73. * 2. Do the provider specific asym cipher fetch, from the same provider
  74. * as |ctx->keymgmt|
  75. *
  76. * We then try to fetch the keymgmt from the same provider as the
  77. * asym cipher, and try to export |ctx->pkey| to that keymgmt (when
  78. * this keymgmt happens to be the same as |ctx->keymgmt|, the export
  79. * is a no-op, but we call it anyway to not complicate the code even
  80. * more).
  81. * If the export call succeeds (returns a non-NULL provider key pointer),
  82. * we're done and can perform the operation itself. If not, we perform
  83. * the second iteration, or jump to legacy.
  84. */
  85. for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
  86. EVP_KEYMGMT *tmp_keymgmt_tofree;
  87. /*
  88. * If we're on the second iteration, free the results from the first.
  89. * They are NULL on the first iteration, so no need to check what
  90. * iteration we're on.
  91. */
  92. EVP_ASYM_CIPHER_free(cipher);
  93. EVP_KEYMGMT_free(tmp_keymgmt);
  94. switch (iter) {
  95. case 1:
  96. cipher = EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph,
  97. ctx->propquery);
  98. if (cipher != NULL)
  99. tmp_prov = EVP_ASYM_CIPHER_get0_provider(cipher);
  100. break;
  101. case 2:
  102. tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
  103. cipher =
  104. evp_asym_cipher_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
  105. supported_ciph, ctx->propquery);
  106. if (cipher == NULL)
  107. goto legacy;
  108. break;
  109. }
  110. if (cipher == NULL)
  111. continue;
  112. /*
  113. * Ensure that the key is provided, either natively, or as a cached
  114. * export. We start by fetching the keymgmt with the same name as
  115. * |ctx->pkey|, but from the provider of the asym cipher method, using
  116. * the same property query as when fetching the asym cipher method.
  117. * With the keymgmt we found (if we did), we try to export |ctx->pkey|
  118. * to it (evp_pkey_export_to_provider() is smart enough to only actually
  119. * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
  120. */
  121. tmp_keymgmt_tofree = tmp_keymgmt
  122. = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
  123. EVP_KEYMGMT_get0_name(ctx->keymgmt),
  124. ctx->propquery);
  125. if (tmp_keymgmt != NULL)
  126. provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
  127. &tmp_keymgmt, ctx->propquery);
  128. if (tmp_keymgmt == NULL)
  129. EVP_KEYMGMT_free(tmp_keymgmt_tofree);
  130. }
  131. if (provkey == NULL) {
  132. EVP_ASYM_CIPHER_free(cipher);
  133. goto legacy;
  134. }
  135. ERR_pop_to_mark();
  136. /* No more legacy from here down to legacy: */
  137. ctx->op.ciph.cipher = cipher;
  138. ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov));
  139. if (ctx->op.ciph.algctx == NULL) {
  140. /* The provider key can stay in the cache */
  141. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  142. goto err;
  143. }
  144. desc = cipher->description != NULL ? cipher->description : "";
  145. switch (operation) {
  146. case EVP_PKEY_OP_ENCRYPT:
  147. if (cipher->encrypt_init == NULL) {
  148. ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_NOT_SUPPORTED,
  149. "%s encrypt_init:%s", cipher->type_name, desc);
  150. ret = -2;
  151. goto err;
  152. }
  153. ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params);
  154. break;
  155. case EVP_PKEY_OP_DECRYPT:
  156. if (cipher->decrypt_init == NULL) {
  157. ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_NOT_SUPPORTED,
  158. "%s decrypt_init:%s", cipher->type_name, desc);
  159. ret = -2;
  160. goto err;
  161. }
  162. ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params);
  163. break;
  164. default:
  165. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  166. goto err;
  167. }
  168. if (ret <= 0)
  169. goto err;
  170. EVP_KEYMGMT_free(tmp_keymgmt);
  171. return 1;
  172. legacy:
  173. /*
  174. * If we don't have the full support we need with provided methods,
  175. * let's go see if legacy does.
  176. */
  177. ERR_pop_to_mark();
  178. EVP_KEYMGMT_free(tmp_keymgmt);
  179. tmp_keymgmt = NULL;
  180. if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
  181. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  182. return -2;
  183. }
  184. switch (ctx->operation) {
  185. case EVP_PKEY_OP_ENCRYPT:
  186. if (ctx->pmeth->encrypt_init == NULL)
  187. return 1;
  188. ret = ctx->pmeth->encrypt_init(ctx);
  189. break;
  190. case EVP_PKEY_OP_DECRYPT:
  191. if (ctx->pmeth->decrypt_init == NULL)
  192. return 1;
  193. ret = ctx->pmeth->decrypt_init(ctx);
  194. break;
  195. default:
  196. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  197. ret = -1;
  198. }
  199. err:
  200. if (ret <= 0) {
  201. evp_pkey_ctx_free_old_ops(ctx);
  202. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  203. }
  204. EVP_KEYMGMT_free(tmp_keymgmt);
  205. return ret;
  206. }
  207. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
  208. {
  209. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL);
  210. }
  211. int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
  212. {
  213. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params);
  214. }
  215. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
  216. unsigned char *out, size_t *outlen,
  217. const unsigned char *in, size_t inlen)
  218. {
  219. EVP_ASYM_CIPHER *cipher;
  220. const char *desc;
  221. int ret;
  222. if (ctx == NULL) {
  223. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  224. return -2;
  225. }
  226. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  227. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  228. return -1;
  229. }
  230. if (ctx->op.ciph.algctx == NULL)
  231. goto legacy;
  232. cipher = ctx->op.ciph.cipher;
  233. desc = cipher->description != NULL ? cipher->description : "";
  234. ERR_set_mark();
  235. ret = cipher->encrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen);
  236. if (ret <= 0 && ERR_count_to_mark() == 0)
  237. ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE,
  238. "%s encrypt:%s", cipher->type_name, desc);
  239. ERR_clear_last_mark();
  240. return ret;
  241. legacy:
  242. if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) {
  243. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  244. return -2;
  245. }
  246. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
  247. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  248. }
  249. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
  250. {
  251. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);
  252. }
  253. int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
  254. {
  255. return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);
  256. }
  257. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
  258. unsigned char *out, size_t *outlen,
  259. const unsigned char *in, size_t inlen)
  260. {
  261. EVP_ASYM_CIPHER *cipher;
  262. const char *desc;
  263. int ret;
  264. if (ctx == NULL) {
  265. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  266. return -2;
  267. }
  268. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  269. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  270. return -1;
  271. }
  272. if (ctx->op.ciph.algctx == NULL)
  273. goto legacy;
  274. cipher = ctx->op.ciph.cipher;
  275. desc = cipher->description != NULL ? cipher->description : "";
  276. ERR_set_mark();
  277. ret = cipher->decrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen);
  278. if (ret <= 0 && ERR_count_to_mark() == 0)
  279. ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE,
  280. "%s decrypt:%s", cipher->type_name, desc);
  281. ERR_clear_last_mark();
  282. return ret;
  283. legacy:
  284. if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) {
  285. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  286. return -2;
  287. }
  288. M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
  289. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  290. }
  291. /* decrypt to new buffer of dynamic size, checking any pre-determined size */
  292. int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp,
  293. size_t *outlenp, size_t expected_outlen,
  294. const unsigned char *in, size_t inlen)
  295. {
  296. if (EVP_PKEY_decrypt(ctx, NULL, outlenp, in, inlen) <= 0
  297. || (*outp = OPENSSL_malloc(*outlenp)) == NULL)
  298. return -1;
  299. if (EVP_PKEY_decrypt(ctx, *outp, outlenp, in, inlen) <= 0
  300. || *outlenp == 0
  301. || (expected_outlen != 0 && *outlenp != expected_outlen)) {
  302. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  303. OPENSSL_clear_free(*outp, *outlenp);
  304. *outp = NULL;
  305. return 0;
  306. }
  307. return 1;
  308. }
  309. static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)
  310. {
  311. EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));
  312. if (cipher == NULL)
  313. return NULL;
  314. if (!CRYPTO_NEW_REF(&cipher->refcnt, 1)
  315. || !ossl_provider_up_ref(prov)) {
  316. CRYPTO_FREE_REF(&cipher->refcnt);
  317. OPENSSL_free(cipher);
  318. return NULL;
  319. }
  320. cipher->prov = prov;
  321. return cipher;
  322. }
  323. static void *evp_asym_cipher_from_algorithm(int name_id,
  324. const OSSL_ALGORITHM *algodef,
  325. OSSL_PROVIDER *prov)
  326. {
  327. const OSSL_DISPATCH *fns = algodef->implementation;
  328. EVP_ASYM_CIPHER *cipher = NULL;
  329. int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
  330. int gparamfncnt = 0, sparamfncnt = 0;
  331. if ((cipher = evp_asym_cipher_new(prov)) == NULL) {
  332. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  333. goto err;
  334. }
  335. cipher->name_id = name_id;
  336. if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
  337. goto err;
  338. cipher->description = algodef->algorithm_description;
  339. for (; fns->function_id != 0; fns++) {
  340. switch (fns->function_id) {
  341. case OSSL_FUNC_ASYM_CIPHER_NEWCTX:
  342. if (cipher->newctx != NULL)
  343. break;
  344. cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);
  345. ctxfncnt++;
  346. break;
  347. case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:
  348. if (cipher->encrypt_init != NULL)
  349. break;
  350. cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);
  351. encfncnt++;
  352. break;
  353. case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:
  354. if (cipher->encrypt != NULL)
  355. break;
  356. cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);
  357. encfncnt++;
  358. break;
  359. case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:
  360. if (cipher->decrypt_init != NULL)
  361. break;
  362. cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);
  363. decfncnt++;
  364. break;
  365. case OSSL_FUNC_ASYM_CIPHER_DECRYPT:
  366. if (cipher->decrypt != NULL)
  367. break;
  368. cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);
  369. decfncnt++;
  370. break;
  371. case OSSL_FUNC_ASYM_CIPHER_FREECTX:
  372. if (cipher->freectx != NULL)
  373. break;
  374. cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);
  375. ctxfncnt++;
  376. break;
  377. case OSSL_FUNC_ASYM_CIPHER_DUPCTX:
  378. if (cipher->dupctx != NULL)
  379. break;
  380. cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);
  381. break;
  382. case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:
  383. if (cipher->get_ctx_params != NULL)
  384. break;
  385. cipher->get_ctx_params
  386. = OSSL_FUNC_asym_cipher_get_ctx_params(fns);
  387. gparamfncnt++;
  388. break;
  389. case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:
  390. if (cipher->gettable_ctx_params != NULL)
  391. break;
  392. cipher->gettable_ctx_params
  393. = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);
  394. gparamfncnt++;
  395. break;
  396. case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:
  397. if (cipher->set_ctx_params != NULL)
  398. break;
  399. cipher->set_ctx_params
  400. = OSSL_FUNC_asym_cipher_set_ctx_params(fns);
  401. sparamfncnt++;
  402. break;
  403. case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:
  404. if (cipher->settable_ctx_params != NULL)
  405. break;
  406. cipher->settable_ctx_params
  407. = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);
  408. sparamfncnt++;
  409. break;
  410. }
  411. }
  412. if (ctxfncnt != 2
  413. || (encfncnt != 0 && encfncnt != 2)
  414. || (decfncnt != 0 && decfncnt != 2)
  415. || (encfncnt != 2 && decfncnt != 2)
  416. || (gparamfncnt != 0 && gparamfncnt != 2)
  417. || (sparamfncnt != 0 && sparamfncnt != 2)) {
  418. /*
  419. * In order to be a consistent set of functions we must have at least
  420. * a set of context functions (newctx and freectx) as well as a pair of
  421. * "cipher" functions: (encrypt_init, encrypt) or
  422. * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are
  423. * optional, but if one of them is present then the other one must also
  424. * be present. The same applies to get_ctx_params and
  425. * gettable_ctx_params. The dupctx function is optional.
  426. */
  427. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  428. goto err;
  429. }
  430. return cipher;
  431. err:
  432. EVP_ASYM_CIPHER_free(cipher);
  433. return NULL;
  434. }
  435. void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)
  436. {
  437. int i;
  438. if (cipher == NULL)
  439. return;
  440. CRYPTO_DOWN_REF(&cipher->refcnt, &i);
  441. if (i > 0)
  442. return;
  443. OPENSSL_free(cipher->type_name);
  444. ossl_provider_free(cipher->prov);
  445. CRYPTO_FREE_REF(&cipher->refcnt);
  446. OPENSSL_free(cipher);
  447. }
  448. int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)
  449. {
  450. int ref = 0;
  451. CRYPTO_UP_REF(&cipher->refcnt, &ref);
  452. return 1;
  453. }
  454. OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)
  455. {
  456. return cipher->prov;
  457. }
  458. EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  459. const char *properties)
  460. {
  461. return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,
  462. evp_asym_cipher_from_algorithm,
  463. evp_asym_cipher_up_ref,
  464. evp_asym_cipher_free);
  465. }
  466. EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
  467. const char *algorithm,
  468. const char *properties)
  469. {
  470. return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER,
  471. algorithm, properties,
  472. evp_asym_cipher_from_algorithm,
  473. evp_asym_cipher_up_ref,
  474. evp_asym_cipher_free);
  475. }
  476. int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)
  477. {
  478. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  479. }
  480. int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)
  481. {
  482. return cipher->name_id;
  483. }
  484. const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)
  485. {
  486. return cipher->type_name;
  487. }
  488. const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)
  489. {
  490. return cipher->description;
  491. }
  492. void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
  493. void (*fn)(EVP_ASYM_CIPHER *cipher,
  494. void *arg),
  495. void *arg)
  496. {
  497. evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,
  498. (void (*)(void *, void *))fn, arg,
  499. evp_asym_cipher_from_algorithm,
  500. evp_asym_cipher_up_ref,
  501. evp_asym_cipher_free);
  502. }
  503. int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,
  504. void (*fn)(const char *name, void *data),
  505. void *data)
  506. {
  507. if (cipher->prov != NULL)
  508. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  509. return 1;
  510. }
  511. const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)
  512. {
  513. void *provctx;
  514. if (cip == NULL || cip->gettable_ctx_params == NULL)
  515. return NULL;
  516. provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
  517. return cip->gettable_ctx_params(NULL, provctx);
  518. }
  519. const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)
  520. {
  521. void *provctx;
  522. if (cip == NULL || cip->settable_ctx_params == NULL)
  523. return NULL;
  524. provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));
  525. return cip->settable_ctx_params(NULL, provctx);
  526. }