2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include "internal/refcount.h"
19 #include <openssl/bn.h>
20 #include <openssl/err.h>
21 #include <openssl/objects.h>
22 #include <openssl/evp.h>
23 #include <openssl/x509.h>
24 #include <openssl/rsa.h>
25 #include <openssl/dsa.h>
26 #include <openssl/dh.h>
27 #include <openssl/ec.h>
28 #include <openssl/cmac.h>
29 #include <openssl/engine.h>
30 #include <openssl/params.h>
31 #include <openssl/serializer.h>
32 #include <openssl/core_names.h>
34 #include "crypto/asn1.h"
35 #include "crypto/evp.h"
36 #include "internal/evp.h"
37 #include "internal/provider.h"
38 #include "evp_local.h"
39 DEFINE_STACK_OF(X509_ATTRIBUTE)
41 #include "crypto/ec.h"
43 /* TODO remove this when the EVP_PKEY_is_a() #legacy support hack is removed */
44 #include "e_os.h" /* strcasecmp on Windows */
46 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
47 int len, EVP_KEYMGMT *keymgmt);
48 static void evp_pkey_free_it(EVP_PKEY *key);
52 /* The type of parameters selected in key parameter functions */
53 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
55 int EVP_PKEY_bits(const EVP_PKEY *pkey)
58 if (pkey->ameth == NULL)
59 return pkey->cache.bits;
60 else if (pkey->ameth->pkey_bits)
61 return pkey->ameth->pkey_bits(pkey);
66 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
70 if (pkey->ameth == NULL)
71 return pkey->cache.security_bits;
72 if (pkey->ameth->pkey_security_bits == NULL)
74 return pkey->ameth->pkey_security_bits(pkey);
77 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
79 # ifndef OPENSSL_NO_DSA
80 if (pkey->type == EVP_PKEY_DSA) {
81 int ret = pkey->save_parameters;
84 pkey->save_parameters = mode;
88 # ifndef OPENSSL_NO_EC
89 if (pkey->type == EVP_PKEY_EC) {
90 int ret = pkey->save_parameters;
93 pkey->save_parameters = mode;
100 int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
102 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
105 void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
107 return CRYPTO_get_ex_data(&key->ex_data, idx);
110 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
113 * TODO: clean up legacy stuff from this function when legacy support
118 * If |to| is a legacy key and |from| isn't, we must downgrade |from|.
119 * If that fails, this function fails.
121 if (to->type != EVP_PKEY_NONE && from->keymgmt != NULL)
122 if (!evp_pkey_downgrade((EVP_PKEY *)from))
126 * Make sure |to| is typed. Content is less important at this early
129 * 1. If |to| is untyped, assign |from|'s key type to it.
130 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
131 * (|from| was already downgraded above)
133 * If |to| is a provided key, there's nothing more to do here, functions
134 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
135 * further down help us find out if they are the same or not.
137 if (to->type == EVP_PKEY_NONE && to->keymgmt == NULL) {
138 if (from->type != EVP_PKEY_NONE) {
139 if (EVP_PKEY_set_type(to, from->type) == 0)
142 if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
145 } else if (to->type != EVP_PKEY_NONE) {
146 if (to->type != from->type) {
147 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
152 if (EVP_PKEY_missing_parameters(from)) {
153 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
157 if (!EVP_PKEY_missing_parameters(to)) {
158 if (EVP_PKEY_cmp_parameters(to, from) == 1)
160 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
164 /* For purely provided keys, we just call the keymgmt utility */
165 if (to->keymgmt != NULL && from->keymgmt != NULL)
166 return evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
169 * If |to| is provided, we know that |from| is legacy at this point.
170 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
171 * to copy the appropriate data to |to|'s keydata.
173 if (to->keymgmt != NULL) {
174 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
176 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
180 * If we get a NULL, it could be an internal error, or it could be
181 * that there's a key mismatch. We're pretending the latter...
183 if (from_keydata == NULL) {
184 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
187 return evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata,
191 /* Both keys are legacy */
192 if (from->ameth != NULL && from->ameth->param_copy != NULL)
193 return from->ameth->param_copy(to, from);
198 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
201 if (pkey->keymgmt != NULL)
202 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
203 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
204 return pkey->ameth->param_missing(pkey);
210 * This function is called for any mixture of keys except pure legacy pair.
211 * TODO When legacy keys are gone, we replace a call to this functions with
212 * a call to evp_keymgmt_util_match().
214 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
217 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
218 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
220 /* If none of them are provided, this function shouldn't have been called */
221 if (!ossl_assert(a->keymgmt != NULL || b->keymgmt != NULL))
224 /* For purely provided keys, we just call the keymgmt utility */
225 if (a->keymgmt != NULL && b->keymgmt != NULL)
226 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
229 * At this point, one of them is provided, the other not. This allows
230 * us to compare types using legacy NIDs.
232 if ((a->type != EVP_PKEY_NONE
233 && (b->keymgmt == NULL
234 || !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type))))
235 || (b->type != EVP_PKEY_NONE
236 && (a->keymgmt == NULL
237 || !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))))
238 return -1; /* not the same key type */
241 * We've determined that they both are the same keytype, so the next
242 * step is to do a bit of cross export to ensure we have keydata for
243 * both keys in the same keymgmt.
245 keymgmt1 = a->keymgmt;
246 keydata1 = a->keydata;
247 keymgmt2 = b->keymgmt;
248 keydata2 = b->keydata;
250 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
252 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
253 if (tmp_keydata != NULL) {
255 keydata1 = tmp_keydata;
258 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
260 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
261 if (tmp_keydata != NULL) {
263 keydata2 = tmp_keydata;
267 /* If we still don't have matching keymgmt implementations, we give up */
268 if (keymgmt1 != keymgmt2)
271 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
274 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
277 * TODO: clean up legacy stuff from this function when legacy support
281 if (a->keymgmt != NULL || b->keymgmt != NULL)
282 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
284 /* All legacy keys */
285 if (a->type != b->type)
287 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
288 return a->ameth->param_cmp(a, b);
292 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
295 * TODO: clean up legacy stuff from this function when legacy support
299 if (a->keymgmt != NULL || b->keymgmt != NULL)
300 return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
301 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY));
303 /* All legacy keys */
304 if (a->type != b->type)
307 if (a->ameth != NULL) {
309 /* Compare parameters if the algorithm has them */
310 if (a->ameth->param_cmp != NULL) {
311 ret = a->ameth->param_cmp(a, b);
316 if (a->ameth->pub_cmp != NULL)
317 return a->ameth->pub_cmp(a, b);
323 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
324 const unsigned char *priv,
327 EVP_PKEY *ret = EVP_PKEY_new();
330 || !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
331 /* EVPerr already called */
335 if (ret->ameth->set_priv_key == NULL) {
336 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
337 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
341 if (!ret->ameth->set_priv_key(ret, priv, len)) {
342 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
353 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
354 const unsigned char *pub,
357 EVP_PKEY *ret = EVP_PKEY_new();
360 || !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
361 /* EVPerr already called */
365 if (ret->ameth->set_pub_key == NULL) {
366 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
367 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
371 if (!ret->ameth->set_pub_key(ret, pub, len)) {
372 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
383 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
386 /* TODO(3.0) Do we need to do anything about provider side keys? */
387 if (pkey->ameth->get_priv_key == NULL) {
388 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
389 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
393 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
394 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
401 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
404 /* TODO(3.0) Do we need to do anything about provider side keys? */
405 if (pkey->ameth->get_pub_key == NULL) {
406 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
407 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
411 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
412 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
419 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
420 size_t len, const EVP_CIPHER *cipher)
422 # ifndef OPENSSL_NO_CMAC
423 # ifndef OPENSSL_NO_ENGINE
424 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
426 const char *cipher_name = EVP_CIPHER_name(cipher);
427 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
428 OPENSSL_CTX *libctx =
429 prov == NULL ? NULL : ossl_provider_library_context(prov);
430 EVP_PKEY *ret = EVP_PKEY_new();
431 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
432 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
433 OSSL_PARAM params[4];
438 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1, NULL)) {
439 /* EVPerr already called */
443 # ifndef OPENSSL_NO_ENGINE
444 if (engine_id != NULL)
446 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
450 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
451 (char *)cipher_name, 0);
453 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
455 params[paramsn] = OSSL_PARAM_construct_end();
457 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
458 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
462 ret->pkey.ptr = cmctx;
467 EVP_MAC_CTX_free(cmctx);
471 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
472 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
477 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
479 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
482 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
484 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
487 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
489 if (pkey->type == type) {
490 return 1; /* it already is that type */
494 * The application is requesting to alias this to a different pkey type,
495 * but not one that resolves to the base type.
497 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
498 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
506 # ifndef OPENSSL_NO_ENGINE
507 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
510 if (!ENGINE_init(e)) {
511 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
514 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
516 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
520 ENGINE_finish(pkey->pmeth_engine);
521 pkey->pmeth_engine = e;
525 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
530 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
534 #ifndef OPENSSL_NO_EC
535 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
536 const EC_GROUP *group = EC_KEY_get0_group(key);
538 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
539 alias = EVP_PKEY_SM2;
543 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
545 if (!EVP_PKEY_set_alias_type(pkey, alias))
547 pkey->pkey.ptr = key;
548 return (key != NULL);
551 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
553 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
554 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
557 return pkey->pkey.ptr;
560 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
562 ASN1_OCTET_STRING *os = NULL;
563 if (pkey->type != EVP_PKEY_HMAC) {
564 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
567 os = EVP_PKEY_get0(pkey);
572 # ifndef OPENSSL_NO_POLY1305
573 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
575 ASN1_OCTET_STRING *os = NULL;
576 if (pkey->type != EVP_PKEY_POLY1305) {
577 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
580 os = EVP_PKEY_get0(pkey);
586 # ifndef OPENSSL_NO_SIPHASH
587 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
589 ASN1_OCTET_STRING *os = NULL;
591 if (pkey->type != EVP_PKEY_SIPHASH) {
592 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
595 os = EVP_PKEY_get0(pkey);
601 # ifndef OPENSSL_NO_RSA
602 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
604 int ret = EVP_PKEY_assign_RSA(pkey, key);
610 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
612 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
613 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
616 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
617 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
620 return pkey->pkey.rsa;
623 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
625 RSA *ret = EVP_PKEY_get0_RSA(pkey);
632 # ifndef OPENSSL_NO_DSA
633 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
635 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
636 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
639 if (pkey->type != EVP_PKEY_DSA) {
640 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
643 return pkey->pkey.dsa;
646 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
648 int ret = EVP_PKEY_assign_DSA(pkey, key);
653 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
655 DSA *ret = EVP_PKEY_get0_DSA(pkey);
660 # endif /* OPENSSL_NO_DSA */
661 #endif /* FIPS_MODULE */
664 # ifndef OPENSSL_NO_EC
665 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
667 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
673 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
675 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
676 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
679 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
680 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
683 return pkey->pkey.ec;
686 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
688 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
695 # ifndef OPENSSL_NO_DH
697 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
699 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
700 int ret = EVP_PKEY_assign(pkey, type, key);
707 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
709 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
710 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
713 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
714 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
717 return pkey->pkey.dh;
720 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
722 DH *ret = EVP_PKEY_get0_DH(pkey);
729 int EVP_PKEY_type(int type)
732 const EVP_PKEY_ASN1_METHOD *ameth;
734 ameth = EVP_PKEY_asn1_find(&e, type);
736 ret = ameth->pkey_id;
739 # ifndef OPENSSL_NO_ENGINE
745 int EVP_PKEY_id(const EVP_PKEY *pkey)
750 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
752 return EVP_PKEY_type(pkey->type);
755 int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
758 if (pkey->keymgmt == NULL) {
760 * These hard coded cases are pure hackery to get around the fact
761 * that names in crypto/objects/objects.txt are a mess. There is
762 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
763 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
764 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
765 * "DSA" is accurate... but still, better be safe and hard-code
766 * names that we know.
767 * TODO Clean this away along with all other #legacy support.
771 if (strcasecmp(name, "RSA") == 0)
773 #ifndef OPENSSL_NO_EC
774 else if (strcasecmp(name, "EC") == 0)
777 #ifndef OPENSSL_NO_DSA
778 else if (strcasecmp(name, "DSA") == 0)
782 type = EVP_PKEY_type(OBJ_sn2nid(name));
783 return EVP_PKEY_type(pkey->type) == type;
786 return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
789 int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
791 if (pkey->keymgmt == NULL) {
792 switch (EVP_PKEY_base_id(pkey)) {
795 #ifndef OPENSSL_NO_DSA
799 #ifndef OPENSSL_NO_EC
800 case EVP_PKEY_ED25519:
803 case EVP_PKEY_EC: /* Including SM2 */
804 return EC_KEY_can_sign(pkey->pkey.ec);
810 const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt);
811 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
812 const char *supported_sig =
813 pkey->keymgmt->query_operation_name != NULL
814 ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
815 : evp_first_name(prov, pkey->keymgmt->name_id);
816 EVP_SIGNATURE *signature = NULL;
818 signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
819 if (signature != NULL) {
820 EVP_SIGNATURE_free(signature);
827 #ifndef OPENSSL_NO_EC
829 * TODO rewrite when we have proper data extraction functions
830 * Note: an octet pointer would be desirable!
832 static OSSL_CALLBACK get_ec_curve_name_cb;
833 static int get_ec_curve_name_cb(const OSSL_PARAM params[], void *arg)
835 const OSSL_PARAM *p = NULL;
837 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME)) != NULL)
838 return OSSL_PARAM_get_utf8_string(p, arg, 0);
840 /* If there is no curve name, this is not an EC key */
844 int evp_pkey_get_EC_KEY_curve_nid(const EVP_PKEY *pkey)
848 if (pkey->keymgmt == NULL) {
849 if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
850 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
852 ret = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
854 } else if (EVP_PKEY_is_a(pkey, "EC") || EVP_PKEY_is_a(pkey, "SM2")) {
855 char *curve_name = NULL;
857 ret = evp_keymgmt_export(pkey->keymgmt, pkey->keydata,
858 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
859 get_ec_curve_name_cb, &curve_name);
861 ret = ec_curve_name2nid(curve_name);
862 OPENSSL_free(curve_name);
869 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
871 BIO_set_indent(*out, saved_indent);
873 BIO *next = BIO_pop(*out);
881 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
887 long i = BIO_get_indent(*out);
889 *saved_indent = (i < 0 ? 0 : i);
890 if (BIO_set_indent(*out, indent) <= 0) {
891 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
895 if (BIO_set_indent(*out, indent) <= 0) {
896 print_reset_indent(out, *pop_f_prefix, *saved_indent);
903 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
906 return BIO_indent(out, indent, 128)
907 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
908 kstr, OBJ_nid2ln(pkey->type)) > 0;
911 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
912 const char *propquery /* For provided serialization */,
913 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
914 int indent, ASN1_PCTX *pctx),
915 ASN1_PCTX *legacy_pctx /* For legacy print */)
919 OSSL_SERIALIZER_CTX *ctx = NULL;
920 int ret = -2; /* default to unsupported */
922 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
925 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
926 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
927 ret = OSSL_SERIALIZER_to_bio(ctx, out);
928 OSSL_SERIALIZER_CTX_free(ctx);
933 /* legacy fallback */
934 if (legacy_print != NULL)
935 ret = legacy_print(out, pkey, 0, legacy_pctx);
937 ret = unsup_alg(out, pkey, 0, "Public Key");
940 print_reset_indent(&out, pop_f_prefix, saved_indent);
944 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
945 int indent, ASN1_PCTX *pctx)
947 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
948 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
952 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
953 int indent, ASN1_PCTX *pctx)
955 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
956 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
960 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
961 int indent, ASN1_PCTX *pctx)
963 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
964 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
968 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
969 int arg1, void *arg2)
971 if (pkey->keymgmt == NULL)
974 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
976 char mdname[80] = "";
978 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
983 nid = OBJ_sn2nid(mdname);
984 if (nid == NID_undef)
985 nid = OBJ_ln2nid(mdname);
986 if (nid == NID_undef)
996 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
998 if (pkey->ameth == NULL)
999 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1000 if (pkey->ameth->pkey_ctrl == NULL)
1002 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1005 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1007 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1010 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1011 char *mdname, size_t mdname_sz)
1013 if (pkey->ameth == NULL)
1014 return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1019 int nid = NID_undef;
1020 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1021 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1024 OPENSSL_strlcpy(mdname, name, mdname_sz);
1029 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
1031 int rv, default_nid;
1033 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
1036 * If there is a mandatory default digest and this isn't it, then
1037 * the answer is 'no'.
1039 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
1041 return (nid == default_nid);
1042 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
1049 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
1050 const unsigned char *pt, size_t ptlen)
1052 if (ptlen > INT_MAX)
1054 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
1060 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
1063 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
1069 #endif /* FIPS_MODULE */
1071 /*- All methods below can also be used in FIPS_MODULE */
1073 EVP_PKEY *EVP_PKEY_new(void)
1075 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1078 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
1081 ret->type = EVP_PKEY_NONE;
1082 ret->save_type = EVP_PKEY_NONE;
1083 ret->references = 1;
1084 ret->save_parameters = 1;
1085 ret->lock = CRYPTO_THREAD_lock_new();
1086 if (ret->lock == NULL) {
1087 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
1091 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1092 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
1099 CRYPTO_THREAD_lock_free(ret->lock);
1105 * Setup a public key management method.
1107 * For legacy keys, either |type| or |str| is expected to have the type
1108 * information. In this case, the setup consists of finding an ASN1 method
1109 * and potentially an ENGINE, and setting those fields in |pkey|.
1111 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1112 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1114 * If pkey is NULL just return 1 or 0 if the key management method exists.
1117 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1118 int len, EVP_KEYMGMT *keymgmt)
1121 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1122 ENGINE **eptr = (e == NULL) ? &e : NULL;
1126 * The setups can't set both legacy and provider side methods.
1129 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1130 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1131 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1139 free_it = free_it || pkey->pkey.ptr != NULL;
1141 free_it = free_it || pkey->keydata != NULL;
1143 evp_pkey_free_it(pkey);
1146 * If key type matches and a method exists then this lookup has
1147 * succeeded once so just indicate success.
1149 if (pkey->type != EVP_PKEY_NONE
1150 && type == pkey->save_type
1151 && pkey->ameth != NULL)
1153 # ifndef OPENSSL_NO_ENGINE
1154 /* If we have ENGINEs release them */
1155 ENGINE_finish(pkey->engine);
1156 pkey->engine = NULL;
1157 ENGINE_finish(pkey->pmeth_engine);
1158 pkey->pmeth_engine = NULL;
1164 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1165 else if (type != EVP_PKEY_NONE)
1166 ameth = EVP_PKEY_asn1_find(eptr, type);
1167 # ifndef OPENSSL_NO_ENGINE
1168 if (pkey == NULL && eptr != NULL)
1178 check = check && ameth == NULL;
1180 check = check && keymgmt == NULL;
1182 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
1187 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1188 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1192 pkey->keymgmt = keymgmt;
1194 pkey->save_type = type;
1199 * If the internal "origin" key is provider side, don't save |ameth|.
1200 * The main reason is that |ameth| is one factor to detect that the
1201 * internal "origin" key is a legacy one.
1203 if (keymgmt == NULL)
1204 pkey->ameth = ameth;
1208 * The EVP_PKEY_ASN1_METHOD |pkey_id| serves different purposes,
1209 * depending on if we're setting this key to contain a legacy or
1210 * a provider side "origin" key. For a legacy key, we assign it
1211 * to the |type| field, but for a provider side key, we assign it
1212 * to the |save_type| field, because |type| is supposed to be set
1213 * to EVP_PKEY_NONE in that case.
1215 if (ameth != NULL) {
1216 if (keymgmt != NULL)
1217 pkey->save_type = ameth->pkey_id;
1218 else if (pkey->ameth != NULL)
1219 pkey->type = ameth->pkey_id;
1227 static void find_ameth(const char *name, void *data)
1229 const char **str = data;
1232 * The error messages from pkey_set_type() are uninteresting here,
1237 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1241 else if (str[1] == NULL)
1249 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1252 # define EVP_PKEY_TYPE_STR str[0]
1253 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1255 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1256 * Ideally, only one should be found. If two (or more) are found, the
1257 * match is ambiguous. This should never happen, but...
1259 const char *str[2] = { NULL, NULL };
1261 EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str);
1262 if (str[1] != NULL) {
1263 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1267 # define EVP_PKEY_TYPE_STR NULL
1268 # define EVP_PKEY_TYPE_STRLEN -1
1270 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1271 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1274 #undef EVP_PKEY_TYPE_STR
1275 #undef EVP_PKEY_TYPE_STRLEN
1278 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1282 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1285 REF_PRINT_COUNT("EVP_PKEY", pkey);
1286 REF_ASSERT_ISNT(i < 2);
1287 return ((i > 1) ? 1 : 0);
1291 void evp_pkey_free_legacy(EVP_PKEY *x)
1293 if (x->ameth != NULL) {
1294 if (x->ameth->pkey_free != NULL)
1295 x->ameth->pkey_free(x);
1298 # ifndef OPENSSL_NO_ENGINE
1299 ENGINE_finish(x->engine);
1301 ENGINE_finish(x->pmeth_engine);
1302 x->pmeth_engine = NULL;
1304 x->type = EVP_PKEY_NONE;
1306 #endif /* FIPS_MODULE */
1308 static void evp_pkey_free_it(EVP_PKEY *x)
1310 /* internal function; x is never NULL */
1312 evp_keymgmt_util_clear_operation_cache(x);
1314 evp_pkey_free_legacy(x);
1317 if (x->keymgmt != NULL) {
1318 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1319 EVP_KEYMGMT_free(x->keymgmt);
1325 void EVP_PKEY_free(EVP_PKEY *x)
1332 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1333 REF_PRINT_COUNT("EVP_PKEY", x);
1336 REF_ASSERT_ISNT(i < 0);
1337 evp_pkey_free_it(x);
1339 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1341 CRYPTO_THREAD_lock_free(x->lock);
1343 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1348 int EVP_PKEY_size(const EVP_PKEY *pkey)
1353 size = pkey->cache.size;
1355 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1356 size = pkey->ameth->pkey_size(pkey);
1362 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1363 EVP_KEYMGMT **keymgmt,
1364 const char *propquery)
1366 EVP_KEYMGMT *allocated_keymgmt = NULL;
1367 EVP_KEYMGMT *tmp_keymgmt = NULL;
1368 void *keydata = NULL;
1374 /* No key data => nothing to export */
1377 check = check && pk->pkey.ptr == NULL;
1379 check = check && pk->keydata == NULL;
1384 if (pk->pkey.ptr != NULL) {
1386 * If the legacy key doesn't have an dirty counter or export function,
1389 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1394 if (keymgmt != NULL) {
1395 tmp_keymgmt = *keymgmt;
1400 * If no keymgmt was given or found, get a default keymgmt. We do so by
1401 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1403 if (tmp_keymgmt == NULL) {
1404 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1406 tmp_keymgmt = ctx->keymgmt;
1407 ctx->keymgmt = NULL;
1408 EVP_PKEY_CTX_free(ctx);
1411 /* If there's still no keymgmt to be had, give up */
1412 if (tmp_keymgmt == NULL)
1416 if (pk->pkey.ptr != NULL) {
1420 * If the legacy "origin" hasn't changed since last time, we try
1421 * to find our keymgmt in the operation cache. If it has changed,
1422 * |i| remains zero, and we will clear the cache further down.
1424 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1425 i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
1428 * If |tmp_keymgmt| is present in the operation cache, it means
1429 * that export doesn't need to be redone. In that case, we take
1430 * token copies of the cached pointers, to have token success
1433 if (i < OSSL_NELEM(pk->operation_cache)
1434 && pk->operation_cache[i].keymgmt != NULL) {
1435 keydata = pk->operation_cache[i].keydata;
1441 * TODO(3.0) Right now, we assume we have ample space. We will have
1442 * to think about a cache aging scheme, though, if |i| indexes outside
1445 if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
1448 /* Make sure that the keymgmt key type matches the legacy NID */
1449 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1452 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1455 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt, libctx, propquery)) {
1456 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1462 * If the dirty counter changed since last time, then clear the
1463 * operation cache. In that case, we know that |i| is zero. Just
1464 * in case this is a re-export, we increment then decrement the
1465 * keymgmt reference counter.
1467 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1468 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1472 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1473 evp_keymgmt_util_clear_operation_cache(pk);
1474 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1476 /* Add the new export to the operation cache */
1477 if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
1478 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1483 /* Synchronize the dirty count */
1484 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1487 #endif /* FIPS_MODULE */
1489 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1493 * If nothing was exported, |tmp_keymgmt| might point at a freed
1494 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1495 * the caller either way in that case.
1497 if (keydata == NULL)
1500 if (keymgmt != NULL)
1501 *keymgmt = tmp_keymgmt;
1503 EVP_KEYMGMT_free(allocated_keymgmt);
1508 int evp_pkey_downgrade(EVP_PKEY *pk)
1510 EVP_KEYMGMT *keymgmt = pk->keymgmt;
1511 void *keydata = pk->keydata;
1512 int type = pk->save_type;
1513 const char *keytype = NULL;
1515 /* If this isn't a provider side key, we're done */
1516 if (keymgmt == NULL)
1519 /* Get the key type name for error reporting */
1520 if (type != EVP_PKEY_NONE)
1521 keytype = OBJ_nid2sn(type);
1524 evp_first_name(EVP_KEYMGMT_provider(keymgmt), keymgmt->name_id);
1527 * |save_type| was set when any of the EVP_PKEY_set_type functions
1528 * was called. It was set to EVP_PKEY_NONE if the key type wasn't
1529 * recognised to be any of the legacy key types, and the downgrade
1532 if (type == EVP_PKEY_NONE) {
1533 ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_KEY_TYPE,
1534 "key type = %s, can't downgrade", keytype);
1539 * To be able to downgrade, we steal the provider side "origin" keymgmt
1540 * and keydata. We've already grabbed the pointers, so all we need to
1541 * do is clear those pointers in |pk| and then call evp_pkey_free_it().
1542 * That way, we can restore |pk| if we need to.
1546 evp_pkey_free_it(pk);
1547 if (EVP_PKEY_set_type(pk, type)) {
1548 /* If the key is typed but empty, we're done */
1549 if (keydata == NULL) {
1550 /* We're dropping the EVP_KEYMGMT */
1551 EVP_KEYMGMT_free(keymgmt);
1555 if (pk->ameth->import_from == NULL) {
1556 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
1557 "key type = %s", keytype);
1560 * We perform the export in the same libctx as the keymgmt that we
1563 OPENSSL_CTX *libctx = ossl_provider_library_context(keymgmt->prov);
1564 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, NULL);
1566 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1569 && evp_keymgmt_export(keymgmt, keydata,
1570 OSSL_KEYMGMT_SELECT_ALL,
1571 pk->ameth->import_from, pctx)) {
1573 * Save the provider side data in the operation cache, so they'll
1574 * find it again. evp_pkey_free_it() cleared the cache, so it's
1575 * safe to assume slot zero is free.
1576 * Note that evp_keymgmt_util_cache_keydata() increments keymgmt's
1579 evp_keymgmt_util_cache_keydata(pk, 0, keymgmt, keydata);
1580 EVP_PKEY_CTX_free(pctx);
1582 /* Synchronize the dirty count */
1583 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1585 /* evp_keymgmt_export() increased the refcount... */
1586 EVP_KEYMGMT_free(keymgmt);
1589 EVP_PKEY_CTX_free(pctx);
1592 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
1593 "key type = %s", keytype);
1597 * Something went wrong. This could for example happen if the keymgmt
1598 * turns out to be an HSM implementation that refuses to let go of some
1599 * of the key data, typically the private bits. In this case, we restore
1600 * the provider side internal "origin" and leave it at that.
1602 if (!ossl_assert(EVP_PKEY_set_type_by_keymgmt(pk, keymgmt))) {
1603 /* This should not be impossible */
1604 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1607 /* EVP_PKEY_set_type_by_keymgmt() increased the refcount... */
1608 EVP_KEYMGMT_free(keymgmt);
1609 pk->keydata = keydata;
1610 evp_keymgmt_util_cache_keyinfo(pk);
1611 return 0; /* No downgrade, but at least the key is restored */
1613 #endif /* FIPS_MODULE */
1615 const OSSL_PARAM *EVP_PKEY_gettable_params(EVP_PKEY *pkey)
1618 || pkey->keymgmt == NULL
1619 || pkey->keydata == NULL)
1621 return evp_keymgmt_gettable_params(pkey->keymgmt);
1624 int EVP_PKEY_get_bn_param(EVP_PKEY *pkey, const char *key_name, BIGNUM **bn)
1627 OSSL_PARAM params[2];
1628 unsigned char buffer[2048];
1629 unsigned char *buf = NULL;
1633 || pkey->keymgmt == NULL
1634 || pkey->keydata == NULL
1639 memset(buffer, 0, sizeof(buffer));
1640 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
1641 params[1] = OSSL_PARAM_construct_end();
1642 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) {
1643 if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
1645 buf_sz = params[0].return_size;
1647 * If it failed because the buffer was too small then allocate the
1648 * required buffer size and retry.
1650 buf = OPENSSL_zalloc(buf_sz);
1653 params[0].data = buf;
1654 params[0].data_size = buf_sz;
1656 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
1659 /* Fail if the param was not found */
1660 if (!OSSL_PARAM_modified(params))
1662 ret = OSSL_PARAM_get_BN(params, bn);
1668 int EVP_PKEY_get_octet_string_param(EVP_PKEY *pkey, const char *key_name,
1669 unsigned char *buf, size_t max_buf_sz,
1672 OSSL_PARAM params[2];
1675 || pkey->keymgmt == NULL
1676 || pkey->keydata == NULL
1677 || key_name == NULL)
1680 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
1681 params[1] = OSSL_PARAM_construct_end();
1682 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
1683 || !OSSL_PARAM_modified(params))
1686 *out_sz = params[0].return_size;
1690 int EVP_PKEY_get_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
1691 char *str, size_t max_buf_sz,
1694 OSSL_PARAM params[2];
1697 || pkey->keymgmt == NULL
1698 || pkey->keydata == NULL
1699 || key_name == NULL)
1702 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
1703 params[1] = OSSL_PARAM_construct_end();
1704 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
1705 || !OSSL_PARAM_modified(params))
1708 *out_sz = params[0].return_size;
1712 int EVP_PKEY_get_int_param(EVP_PKEY *pkey, const char *key_name, int *out)
1714 OSSL_PARAM params[2];
1717 || pkey->keymgmt == NULL
1718 || pkey->keydata == NULL
1719 || key_name == NULL)
1722 params[0] = OSSL_PARAM_construct_int(key_name, out);
1723 params[1] = OSSL_PARAM_construct_end();
1724 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
1725 || !OSSL_PARAM_modified(params))
1730 int EVP_PKEY_get_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t *out)
1732 OSSL_PARAM params[2];
1735 || pkey->keymgmt == NULL
1736 || pkey->keydata == NULL
1737 || key_name == NULL)
1740 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
1741 params[1] = OSSL_PARAM_construct_end();
1742 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
1743 || !OSSL_PARAM_modified(params))