2 * Copyright 1995-2021 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"
18 #include "internal/cryptlib.h"
19 #include "internal/refcount.h"
20 #include "internal/namemap.h"
21 #include <openssl/bn.h>
22 #include <openssl/err.h>
23 #include <openssl/objects.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509.h>
26 #include <openssl/rsa.h>
27 #include <openssl/dsa.h>
28 #include <openssl/dh.h>
29 #include <openssl/ec.h>
30 #include <openssl/cmac.h>
31 #include <openssl/engine.h>
32 #include <openssl/params.h>
33 #include <openssl/param_build.h>
34 #include <openssl/encoder.h>
35 #include <openssl/core_names.h>
37 #include "internal/numbers.h" /* includes SIZE_MAX */
38 #include "internal/ffc.h"
39 #include "crypto/asn1.h"
40 #include "crypto/evp.h"
41 #include "crypto/dh.h"
42 #include "crypto/dsa.h"
43 #include "crypto/ec.h"
44 #include "crypto/ecx.h"
45 #include "crypto/rsa.h"
46 #include "crypto/x509.h"
47 #include "internal/provider.h"
48 #include "evp_local.h"
50 #include "e_os.h" /* strcasecmp on Windows */
52 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
53 int len, EVP_KEYMGMT *keymgmt);
54 static void evp_pkey_free_it(EVP_PKEY *key);
58 /* The type of parameters selected in key parameter functions */
59 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
61 int EVP_PKEY_get_bits(const EVP_PKEY *pkey)
66 size = pkey->cache.bits;
67 if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
68 size = pkey->ameth->pkey_bits(pkey);
70 return size < 0 ? 0 : size;
73 int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)
78 size = pkey->cache.security_bits;
79 if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
80 size = pkey->ameth->pkey_security_bits(pkey);
82 return size < 0 ? 0 : size;
85 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
87 # ifndef OPENSSL_NO_DSA
88 if (pkey->type == EVP_PKEY_DSA) {
89 int ret = pkey->save_parameters;
92 pkey->save_parameters = mode;
96 # ifndef OPENSSL_NO_EC
97 if (pkey->type == EVP_PKEY_EC) {
98 int ret = pkey->save_parameters;
101 pkey->save_parameters = mode;
108 int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
110 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
113 void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
115 return CRYPTO_get_ex_data(&key->ex_data, idx);
118 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
121 * Clean up legacy stuff from this function when legacy support is gone.
124 EVP_PKEY *downgraded_from = NULL;
128 * If |to| is a legacy key and |from| isn't, we must make a downgraded
129 * copy of |from|. If that fails, this function fails.
131 if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
132 if (!evp_pkey_copy_downgraded(&downgraded_from, from))
134 from = downgraded_from;
138 * Make sure |to| is typed. Content is less important at this early
141 * 1. If |to| is untyped, assign |from|'s key type to it.
142 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
143 * (|from| was already downgraded above)
145 * If |to| is a provided key, there's nothing more to do here, functions
146 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
147 * further down help us find out if they are the same or not.
149 if (evp_pkey_is_blank(to)) {
150 if (evp_pkey_is_legacy(from)) {
151 if (EVP_PKEY_set_type(to, from->type) == 0)
154 if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
157 } else if (evp_pkey_is_legacy(to)) {
158 if (to->type != from->type) {
159 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
164 if (EVP_PKEY_missing_parameters(from)) {
165 ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
169 if (!EVP_PKEY_missing_parameters(to)) {
170 if (EVP_PKEY_parameters_eq(to, from) == 1)
173 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
177 /* For purely provided keys, we just call the keymgmt utility */
178 if (to->keymgmt != NULL && from->keymgmt != NULL) {
179 ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
184 * If |to| is provided, we know that |from| is legacy at this point.
185 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
186 * to copy the appropriate data to |to|'s keydata.
187 * We cannot override existing data so do it only if there is no keydata
190 if (to->keymgmt != NULL && to->keydata == NULL) {
191 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
193 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
197 * If we get a NULL, it could be an internal error, or it could be
198 * that there's a key mismatch. We're pretending the latter...
200 if (from_keydata == NULL)
201 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
203 ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,
205 SELECT_PARAMETERS)) != NULL;
209 /* Both keys are legacy */
210 if (from->ameth != NULL && from->ameth->param_copy != NULL)
211 ok = from->ameth->param_copy(to, from);
213 EVP_PKEY_free(downgraded_from);
217 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
220 if (pkey->keymgmt != NULL)
221 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
222 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
223 return pkey->ameth->param_missing(pkey);
229 * This function is called for any mixture of keys except pure legacy pair.
230 * When legacy keys are gone, we replace a call to this functions with
231 * a call to evp_keymgmt_util_match().
233 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
236 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
237 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
239 /* If none of them are provided, this function shouldn't have been called */
240 if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
243 /* For purely provided keys, we just call the keymgmt utility */
244 if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
245 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
248 * At this point, one of them is provided, the other not. This allows
249 * us to compare types using legacy NIDs.
251 if (evp_pkey_is_legacy(a)
252 && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
253 return -1; /* not the same key type */
254 if (evp_pkey_is_legacy(b)
255 && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
256 return -1; /* not the same key type */
259 * We've determined that they both are the same keytype, so the next
260 * step is to do a bit of cross export to ensure we have keydata for
261 * both keys in the same keymgmt.
263 keymgmt1 = a->keymgmt;
264 keydata1 = a->keydata;
265 keymgmt2 = b->keymgmt;
266 keydata2 = b->keydata;
268 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
270 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
271 if (tmp_keydata != NULL) {
273 keydata1 = tmp_keydata;
276 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
278 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
279 if (tmp_keydata != NULL) {
281 keydata2 = tmp_keydata;
285 /* If we still don't have matching keymgmt implementations, we give up */
286 if (keymgmt1 != keymgmt2)
289 /* If the keymgmt implementations are NULL, the export failed */
290 if (keymgmt1 == NULL)
293 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
296 # ifndef OPENSSL_NO_DEPRECATED_3_0
297 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
299 return EVP_PKEY_parameters_eq(a, b);
303 int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
306 * This will just call evp_keymgmt_util_match when legacy support
310 if (a->keymgmt != NULL || b->keymgmt != NULL)
311 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
313 /* All legacy keys */
314 if (a->type != b->type)
316 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
317 return a->ameth->param_cmp(a, b);
321 # ifndef OPENSSL_NO_DEPRECATED_3_0
322 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
324 return EVP_PKEY_eq(a, b);
328 int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
331 * This will just call evp_keymgmt_util_match when legacy support
335 /* Trivial shortcuts */
338 if (a == NULL || b == NULL)
341 if (a->keymgmt != NULL || b->keymgmt != NULL)
342 return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
343 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY));
345 /* All legacy keys */
346 if (a->type != b->type)
349 if (a->ameth != NULL) {
351 /* Compare parameters if the algorithm has them */
352 if (a->ameth->param_cmp != NULL) {
353 ret = a->ameth->param_cmp(a, b);
358 if (a->ameth->pub_cmp != NULL)
359 return a->ameth->pub_cmp(a, b);
366 static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
371 const unsigned char *key,
375 EVP_PKEY *pkey = NULL;
376 EVP_PKEY_CTX *ctx = NULL;
377 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
380 # ifndef OPENSSL_NO_ENGINE
381 /* Check if there is an Engine for this type */
386 ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);
387 else if (nidtype != EVP_PKEY_NONE)
388 ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);
390 /* If tmpe is NULL then no engine is claiming to support this type */
398 if (e == NULL && ameth == NULL) {
400 * No engine is claiming to support this type, so lets see if we have
403 ctx = EVP_PKEY_CTX_new_from_name(libctx,
404 strtype != NULL ? strtype
405 : OBJ_nid2sn(nidtype),
409 /* May fail if no provider available */
411 if (EVP_PKEY_fromdata_init(ctx) == 1) {
412 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
414 ERR_clear_last_mark();
415 params[0] = OSSL_PARAM_construct_octet_string(
416 key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
417 : OSSL_PKEY_PARAM_PUB_KEY,
420 if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
421 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
425 EVP_PKEY_CTX_free(ctx);
430 /* else not supported so fallback to legacy */
433 /* Legacy code path */
435 pkey = EVP_PKEY_new();
437 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
441 if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) {
442 /* EVPerr already called */
446 if (!ossl_assert(pkey->ameth != NULL))
450 if (pkey->ameth->set_priv_key == NULL) {
451 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
455 if (!pkey->ameth->set_priv_key(pkey, key, len)) {
456 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
460 if (pkey->ameth->set_pub_key == NULL) {
461 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
465 if (!pkey->ameth->set_pub_key(pkey, key, len)) {
466 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
477 EVP_PKEY_CTX_free(ctx);
481 EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
484 const unsigned char *priv, size_t len)
486 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,
490 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
491 const unsigned char *priv,
494 return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);
497 EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
498 const char *keytype, const char *propq,
499 const unsigned char *pub, size_t len)
501 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,
505 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
506 const unsigned char *pub,
509 return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);
512 struct raw_key_details_st
519 static OSSL_CALLBACK get_raw_key_details;
520 static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
522 const OSSL_PARAM *p = NULL;
523 struct raw_key_details_st *raw_key = arg;
525 if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
526 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
528 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
529 SIZE_MAX, raw_key->len);
530 } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
531 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
533 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
534 SIZE_MAX, raw_key->len);
540 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
543 if (pkey->keymgmt != NULL) {
544 struct raw_key_details_st raw_key;
546 raw_key.key = priv == NULL ? NULL : &priv;
548 raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
550 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
551 get_raw_key_details, &raw_key);
554 if (pkey->ameth == NULL) {
555 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
559 if (pkey->ameth->get_priv_key == NULL) {
560 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
564 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
565 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
572 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
575 if (pkey->keymgmt != NULL) {
576 struct raw_key_details_st raw_key;
578 raw_key.key = pub == NULL ? NULL : &pub;
580 raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
582 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
583 get_raw_key_details, &raw_key);
586 if (pkey->ameth == NULL) {
587 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
591 if (pkey->ameth->get_pub_key == NULL) {
592 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
596 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
597 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
604 static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
605 const char *cipher_name,
606 const EVP_CIPHER *cipher,
607 OSSL_LIB_CTX *libctx,
608 const char *propq, ENGINE *e)
610 # ifndef OPENSSL_NO_CMAC
611 # ifndef OPENSSL_NO_ENGINE
612 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
614 OSSL_PARAM params[5], *p = params;
615 EVP_PKEY *pkey = NULL;
619 cipher_name = EVP_CIPHER_get0_name(cipher);
621 if (cipher_name == NULL) {
622 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
626 ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
630 if (!EVP_PKEY_fromdata_init(ctx)) {
631 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
635 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
637 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
638 (char *)cipher_name, 0);
640 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
642 # ifndef OPENSSL_NO_ENGINE
643 if (engine_id != NULL)
644 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,
645 (char *)engine_id, 0);
647 *p = OSSL_PARAM_construct_end();
649 if (!EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params)) {
650 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
655 EVP_PKEY_CTX_free(ctx);
659 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
664 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
665 size_t len, const EVP_CIPHER *cipher)
667 return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
670 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
672 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
675 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
677 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
680 # ifndef OPENSSL_NO_ENGINE
681 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
684 if (!ENGINE_init(e)) {
685 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
688 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
690 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
694 ENGINE_finish(pkey->pmeth_engine);
695 pkey->pmeth_engine = e;
699 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
705 # ifndef OPENSSL_NO_DEPRECATED_3_0
706 static void detect_foreign_key(EVP_PKEY *pkey)
708 switch (pkey->type) {
710 pkey->foreign = pkey->pkey.rsa != NULL
711 && ossl_rsa_is_foreign(pkey->pkey.rsa);
713 # ifndef OPENSSL_NO_EC
716 pkey->foreign = pkey->pkey.ec != NULL
717 && ossl_ec_key_is_foreign(pkey->pkey.ec);
720 # ifndef OPENSSL_NO_DSA
722 pkey->foreign = pkey->pkey.dsa != NULL
723 && ossl_dsa_is_foreign(pkey->pkey.dsa);
726 # ifndef OPENSSL_NO_DH
728 pkey->foreign = pkey->pkey.dh != NULL
729 && ossl_dh_is_foreign(pkey->pkey.dh);
738 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
740 # ifndef OPENSSL_NO_EC
743 pktype = EVP_PKEY_type(type);
744 if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
745 const EC_GROUP *group = EC_KEY_get0_group(key);
748 int curve = EC_GROUP_get_curve_name(group);
751 * Regardless of what is requested the SM2 curve must be SM2 type,
752 * and non SM2 curves are EC type.
754 if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
756 else if(curve != NID_sm2 && pktype == EVP_PKEY_SM2)
762 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
765 pkey->pkey.ptr = key;
766 detect_foreign_key(pkey);
768 return (key != NULL);
772 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
777 if (!evp_pkey_is_provided(pkey))
778 return pkey->pkey.ptr;
783 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
785 const ASN1_OCTET_STRING *os = NULL;
786 if (pkey->type != EVP_PKEY_HMAC) {
787 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
790 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
798 # ifndef OPENSSL_NO_POLY1305
799 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
801 const ASN1_OCTET_STRING *os = NULL;
802 if (pkey->type != EVP_PKEY_POLY1305) {
803 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
806 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
815 # ifndef OPENSSL_NO_SIPHASH
816 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
818 const ASN1_OCTET_STRING *os = NULL;
820 if (pkey->type != EVP_PKEY_SIPHASH) {
821 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
824 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
833 # ifndef OPENSSL_NO_DSA
834 static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
836 if (pkey->type != EVP_PKEY_DSA) {
837 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
840 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
843 const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
845 return evp_pkey_get0_DSA_int(pkey);
848 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
850 int ret = EVP_PKEY_assign_DSA(pkey, key);
855 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
857 DSA *ret = evp_pkey_get0_DSA_int(pkey);
863 # endif /* OPENSSL_NO_DSA */
865 # ifndef OPENSSL_NO_EC
866 static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
868 if (EVP_PKEY_get_base_id(pkey) != type) {
869 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
872 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
875 static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
877 ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
879 if (ret != NULL && !ossl_ecx_key_up_ref(ret))
884 # define IMPLEMENT_ECX_VARIANT(NAME) \
885 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
887 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
889 IMPLEMENT_ECX_VARIANT(X25519)
890 IMPLEMENT_ECX_VARIANT(X448)
891 IMPLEMENT_ECX_VARIANT(ED25519)
892 IMPLEMENT_ECX_VARIANT(ED448)
896 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
898 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
903 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
904 * related to ffdhe and modp (which cache q = (p - 1) / 2),
905 * and returns 0 for all other dh parameter generation types including
906 * RFC5114 named groups.
908 * The EVP_PKEY_DH type is used for dh parameter generation types:
909 * - named safe prime groups related to ffdhe and modp
910 * - safe prime generator
912 * The type EVP_PKEY_DHX is used for dh parameter generation types
913 * - fips186-4 and fips186-2
914 * - rfc5114 named groups.
916 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
918 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
919 * q value to be stored.
921 if (ossl_dh_is_named_safe_prime_group(dhkey))
924 type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
926 ret = EVP_PKEY_assign(pkey, type, dhkey);
933 DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
935 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
936 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
939 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
942 const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
944 return evp_pkey_get0_DH_int(pkey);
947 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
949 DH *ret = evp_pkey_get0_DH_int(pkey);
957 int EVP_PKEY_type(int type)
960 const EVP_PKEY_ASN1_METHOD *ameth;
962 ameth = EVP_PKEY_asn1_find(&e, type);
964 ret = ameth->pkey_id;
967 # ifndef OPENSSL_NO_ENGINE
973 int EVP_PKEY_get_id(const EVP_PKEY *pkey)
978 int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
980 return EVP_PKEY_type(pkey->type);
984 * These hard coded cases are pure hackery to get around the fact
985 * that names in crypto/objects/objects.txt are a mess. There is
986 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
987 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
988 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
989 * "DSA" is accurate... but still, better be safe and hard-code
990 * names that we know.
991 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
992 * EVP_PKEY_EC, because of aliasing.
993 * This should be cleaned away along with all other #legacy support.
995 static const OSSL_ITEM standard_name2type[] = {
996 { EVP_PKEY_RSA, "RSA" },
997 { EVP_PKEY_RSA_PSS, "RSA-PSS" },
998 { EVP_PKEY_EC, "EC" },
999 { EVP_PKEY_ED25519, "ED25519" },
1000 { EVP_PKEY_ED448, "ED448" },
1001 { EVP_PKEY_X25519, "X25519" },
1002 { EVP_PKEY_X448, "X448" },
1003 { EVP_PKEY_SM2, "SM2" },
1004 { EVP_PKEY_DH, "DH" },
1005 { EVP_PKEY_DHX, "X9.42 DH" },
1006 { EVP_PKEY_DHX, "DHX" },
1007 { EVP_PKEY_DSA, "DSA" },
1010 int evp_pkey_name2type(const char *name)
1015 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1016 if (strcasecmp(name, standard_name2type[i].ptr) == 0)
1017 return (int)standard_name2type[i].id;
1020 if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1022 return EVP_PKEY_type(OBJ_ln2nid(name));
1025 const char *evp_pkey_type2name(int type)
1029 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1030 if (type == (int)standard_name2type[i].id)
1031 return standard_name2type[i].ptr;
1034 return OBJ_nid2sn(type);
1037 int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
1039 if (pkey->keymgmt == NULL) {
1040 int type = evp_pkey_name2type(name);
1042 return pkey->type == type;
1044 return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1047 int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1048 void (*fn)(const char *name, void *data),
1051 if (!evp_pkey_is_typed(pkey))
1054 if (!evp_pkey_is_provided(pkey)) {
1055 const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
1060 return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1063 int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1065 if (pkey->keymgmt == NULL) {
1066 switch (EVP_PKEY_get_base_id(pkey)) {
1069 # ifndef OPENSSL_NO_DSA
1073 # ifndef OPENSSL_NO_EC
1074 case EVP_PKEY_ED25519:
1075 case EVP_PKEY_ED448:
1077 case EVP_PKEY_EC: /* Including SM2 */
1078 return EC_KEY_can_sign(pkey->pkey.ec);
1084 const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
1085 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1086 const char *supported_sig =
1087 pkey->keymgmt->query_operation_name != NULL
1088 ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
1089 : EVP_KEYMGMT_get0_name(pkey->keymgmt);
1090 EVP_SIGNATURE *signature = NULL;
1092 signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
1093 if (signature != NULL) {
1094 EVP_SIGNATURE_free(signature);
1101 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1103 BIO_set_indent(*out, saved_indent);
1105 BIO *next = BIO_pop(*out);
1113 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1119 long i = BIO_get_indent(*out);
1121 *saved_indent = (i < 0 ? 0 : i);
1122 if (BIO_set_indent(*out, indent) <= 0) {
1123 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
1127 if (BIO_set_indent(*out, indent) <= 0) {
1128 print_reset_indent(out, *pop_f_prefix, *saved_indent);
1135 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1138 return BIO_indent(out, indent, 128)
1139 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1140 kstr, OBJ_nid2ln(pkey->type)) > 0;
1143 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1144 int selection /* For provided encoding */,
1145 const char *propquery /* For provided encoding */,
1146 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1147 int indent, ASN1_PCTX *pctx),
1148 ASN1_PCTX *legacy_pctx /* For legacy print */)
1152 OSSL_ENCODER_CTX *ctx = NULL;
1153 int ret = -2; /* default to unsupported */
1155 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1158 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1160 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1161 ret = OSSL_ENCODER_to_bio(ctx, out);
1162 OSSL_ENCODER_CTX_free(ctx);
1167 /* legacy fallback */
1168 if (legacy_print != NULL)
1169 ret = legacy_print(out, pkey, 0, legacy_pctx);
1171 ret = unsup_alg(out, pkey, 0, "Public Key");
1174 print_reset_indent(&out, pop_f_prefix, saved_indent);
1178 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1179 int indent, ASN1_PCTX *pctx)
1181 return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1182 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1186 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1187 int indent, ASN1_PCTX *pctx)
1189 return print_pkey(pkey, out, indent, EVP_PKEY_KEYPAIR, NULL,
1190 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1194 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1195 int indent, ASN1_PCTX *pctx)
1197 return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1198 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1202 # ifndef OPENSSL_NO_STDIO
1203 int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1204 int indent, ASN1_PCTX *pctx)
1207 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1211 ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1216 int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1217 int indent, ASN1_PCTX *pctx)
1220 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1224 ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1229 int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1230 int indent, ASN1_PCTX *pctx)
1233 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1237 ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1243 static void mdname2nid(const char *mdname, void *data)
1245 int *nid = (int *)data;
1247 if (*nid != NID_undef)
1250 *nid = OBJ_sn2nid(mdname);
1251 if (*nid == NID_undef)
1252 *nid = OBJ_ln2nid(mdname);
1255 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1256 int arg1, void *arg2)
1258 if (pkey->keymgmt == NULL)
1261 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1263 char mdname[80] = "";
1264 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1269 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1270 /* Make sure the MD is in the namemap if available */
1272 OSSL_NAMEMAP *namemap;
1273 int nid = NID_undef;
1275 (void)ERR_set_mark();
1276 md = EVP_MD_fetch(libctx, mdname, NULL);
1277 (void)ERR_pop_to_mark();
1278 namemap = ossl_namemap_stored(libctx);
1281 * The only reason to fetch the MD was to make sure it is in the
1282 * namemap. We can immediately free it.
1285 mdnum = ossl_namemap_name2num(namemap, mdname);
1290 * We have the namemap number - now we need to find the
1293 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1304 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1306 if (pkey->ameth == NULL)
1307 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1308 if (pkey->ameth->pkey_ctrl == NULL)
1310 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1313 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1315 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1318 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1319 char *mdname, size_t mdname_sz)
1321 if (pkey->ameth == NULL)
1322 return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1327 int nid = NID_undef;
1328 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1329 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1332 OPENSSL_strlcpy(mdname, name, mdname_sz);
1337 int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1340 return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1341 gname, gname_sz, gname_len);
1344 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1345 const char *name, const char *propq)
1348 EVP_MD_CTX *ctx = NULL;
1350 if ((ctx = EVP_MD_CTX_new()) == NULL)
1354 rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1358 EVP_MD_CTX_free(ctx);
1362 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1365 if (pkey != NULL && evp_pkey_is_provided(pkey))
1367 EVP_PKEY_set_octet_string_param(pkey,
1368 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1369 (unsigned char *)pub, publen);
1371 if (publen > INT_MAX)
1373 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1374 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
1380 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1384 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
1385 size_t return_size = OSSL_PARAM_UNMODIFIED;
1388 * We know that this is going to fail, but it will give us a size
1391 EVP_PKEY_get_octet_string_param(pkey,
1392 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1393 NULL, 0, &return_size);
1394 if (return_size == OSSL_PARAM_UNMODIFIED)
1397 *ppub = OPENSSL_malloc(return_size);
1401 if (!EVP_PKEY_get_octet_string_param(pkey,
1402 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1403 *ppub, return_size, NULL))
1409 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1415 #endif /* FIPS_MODULE */
1417 /*- All methods below can also be used in FIPS_MODULE */
1419 EVP_PKEY *EVP_PKEY_new(void)
1421 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1424 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1428 ret->type = EVP_PKEY_NONE;
1429 ret->save_type = EVP_PKEY_NONE;
1430 ret->references = 1;
1432 ret->lock = CRYPTO_THREAD_lock_new();
1433 if (ret->lock == NULL) {
1434 EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1439 ret->save_parameters = 1;
1440 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1441 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1448 CRYPTO_THREAD_lock_free(ret->lock);
1454 * Setup a public key management method.
1456 * For legacy keys, either |type| or |str| is expected to have the type
1457 * information. In this case, the setup consists of finding an ASN1 method
1458 * and potentially an ENGINE, and setting those fields in |pkey|.
1460 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1461 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1463 * If pkey is NULL just return 1 or 0 if the key management method exists.
1466 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1467 int len, EVP_KEYMGMT *keymgmt)
1470 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1471 ENGINE **eptr = (e == NULL) ? &e : NULL;
1475 * The setups can't set both legacy and provider side methods.
1478 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1479 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1480 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1488 free_it = free_it || pkey->pkey.ptr != NULL;
1490 free_it = free_it || pkey->keydata != NULL;
1492 evp_pkey_free_it(pkey);
1495 * If key type matches and a method exists then this lookup has
1496 * succeeded once so just indicate success.
1498 if (pkey->type != EVP_PKEY_NONE
1499 && type == pkey->save_type
1500 && pkey->ameth != NULL)
1502 # ifndef OPENSSL_NO_ENGINE
1503 /* If we have ENGINEs release them */
1504 ENGINE_finish(pkey->engine);
1505 pkey->engine = NULL;
1506 ENGINE_finish(pkey->pmeth_engine);
1507 pkey->pmeth_engine = NULL;
1513 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1514 else if (type != EVP_PKEY_NONE)
1515 ameth = EVP_PKEY_asn1_find(eptr, type);
1516 # ifndef OPENSSL_NO_ENGINE
1517 if (pkey == NULL && eptr != NULL)
1527 check = check && ameth == NULL;
1529 check = check && keymgmt == NULL;
1531 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1536 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1537 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1541 pkey->keymgmt = keymgmt;
1543 pkey->save_type = type;
1548 * If the internal "origin" key is provider side, don't save |ameth|.
1549 * The main reason is that |ameth| is one factor to detect that the
1550 * internal "origin" key is a legacy one.
1552 if (keymgmt == NULL)
1553 pkey->ameth = ameth;
1557 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1558 * for any key type that has a legacy implementation, regardless of
1559 * if the internal key is a legacy or a provider side one. When
1560 * there is no legacy implementation for the key, the type becomes
1561 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1562 * with functions that expect legacy internal keys.
1564 if (ameth != NULL) {
1565 if (type == EVP_PKEY_NONE)
1566 pkey->type = ameth->pkey_id;
1568 pkey->type = EVP_PKEY_KEYMGMT;
1576 static void find_ameth(const char *name, void *data)
1578 const char **str = data;
1581 * The error messages from pkey_set_type() are uninteresting here,
1586 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1590 else if (str[1] == NULL)
1598 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1601 # define EVP_PKEY_TYPE_STR str[0]
1602 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1604 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1605 * Ideally, only one should be found. If two (or more) are found, the
1606 * match is ambiguous. This should never happen, but...
1608 const char *str[2] = { NULL, NULL };
1610 if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1611 || str[1] != NULL) {
1612 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1616 # define EVP_PKEY_TYPE_STR NULL
1617 # define EVP_PKEY_TYPE_STRLEN -1
1619 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1620 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1623 #undef EVP_PKEY_TYPE_STR
1624 #undef EVP_PKEY_TYPE_STRLEN
1627 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1631 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1634 REF_PRINT_COUNT("EVP_PKEY", pkey);
1635 REF_ASSERT_ISNT(i < 2);
1636 return ((i > 1) ? 1 : 0);
1640 EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1645 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1649 if ((dup_pk = EVP_PKEY_new()) == NULL)
1652 if (evp_pkey_is_blank(pkey))
1655 if (evp_pkey_is_provided(pkey)) {
1656 if (!evp_keymgmt_util_copy(dup_pk, pkey,
1657 OSSL_KEYMGMT_SELECT_ALL))
1662 if (evp_pkey_is_legacy(pkey)) {
1663 const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1665 if (ameth == NULL || ameth->copy == NULL) {
1666 if (pkey->pkey.ptr == NULL /* empty key, just set type */
1667 && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1669 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1672 if (!ameth->copy(dup_pk, pkey))
1679 /* copy auxiliary data */
1680 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1681 &dup_pk->ex_data, &pkey->ex_data))
1684 if (pkey->attributes != NULL) {
1685 if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1690 EVP_PKEY_free(dup_pk);
1694 void evp_pkey_free_legacy(EVP_PKEY *x)
1696 const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1697 ENGINE *tmpe = NULL;
1699 if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1700 ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
1702 if (ameth != NULL) {
1703 if (x->legacy_cache_pkey.ptr != NULL) {
1705 * We should never have both a legacy origin key, and a key in the
1708 assert(x->pkey.ptr == NULL);
1710 * For the purposes of freeing we make the legacy cache look like
1711 * a legacy origin key.
1713 x->pkey = x->legacy_cache_pkey;
1714 x->legacy_cache_pkey.ptr = NULL;
1716 if (ameth->pkey_free != NULL)
1717 ameth->pkey_free(x);
1720 # ifndef OPENSSL_NO_ENGINE
1721 ENGINE_finish(tmpe);
1722 ENGINE_finish(x->engine);
1724 ENGINE_finish(x->pmeth_engine);
1725 x->pmeth_engine = NULL;
1728 #endif /* FIPS_MODULE */
1730 static void evp_pkey_free_it(EVP_PKEY *x)
1732 /* internal function; x is never NULL */
1733 evp_keymgmt_util_clear_operation_cache(x, 1);
1735 evp_pkey_free_legacy(x);
1738 if (x->keymgmt != NULL) {
1739 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1740 EVP_KEYMGMT_free(x->keymgmt);
1744 x->type = EVP_PKEY_NONE;
1747 void EVP_PKEY_free(EVP_PKEY *x)
1754 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1755 REF_PRINT_COUNT("EVP_PKEY", x);
1758 REF_ASSERT_ISNT(i < 0);
1759 evp_pkey_free_it(x);
1761 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1763 CRYPTO_THREAD_lock_free(x->lock);
1765 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1770 int EVP_PKEY_get_size(const EVP_PKEY *pkey)
1775 size = pkey->cache.size;
1777 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1778 size = pkey->ameth->pkey_size(pkey);
1781 return size < 0 ? 0 : size;
1784 const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
1786 if (!evp_pkey_is_assigned(pkey))
1789 if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1790 return pkey->keymgmt->description;
1792 if (pkey->ameth != NULL)
1793 return pkey->ameth->info;
1798 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1799 EVP_KEYMGMT **keymgmt,
1800 const char *propquery)
1802 EVP_KEYMGMT *allocated_keymgmt = NULL;
1803 EVP_KEYMGMT *tmp_keymgmt = NULL;
1804 void *keydata = NULL;
1810 /* No key data => nothing to export */
1813 check = check && pk->pkey.ptr == NULL;
1815 check = check && pk->keydata == NULL;
1820 if (pk->pkey.ptr != NULL) {
1822 * If the legacy key doesn't have an dirty counter or export function,
1825 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1830 if (keymgmt != NULL) {
1831 tmp_keymgmt = *keymgmt;
1836 * If no keymgmt was given or found, get a default keymgmt. We do so by
1837 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1839 if (tmp_keymgmt == NULL) {
1840 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1842 tmp_keymgmt = ctx->keymgmt;
1843 ctx->keymgmt = NULL;
1844 EVP_PKEY_CTX_free(ctx);
1847 /* If there's still no keymgmt to be had, give up */
1848 if (tmp_keymgmt == NULL)
1852 if (pk->pkey.ptr != NULL) {
1856 * If the legacy "origin" hasn't changed since last time, we try
1857 * to find our keymgmt in the operation cache. If it has changed,
1858 * |i| remains zero, and we will clear the cache further down.
1860 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1861 if (!CRYPTO_THREAD_read_lock(pk->lock))
1863 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1866 * If |tmp_keymgmt| is present in the operation cache, it means
1867 * that export doesn't need to be redone. In that case, we take
1868 * token copies of the cached pointers, to have token success
1871 if (op != NULL && op->keymgmt != NULL) {
1872 keydata = op->keydata;
1873 CRYPTO_THREAD_unlock(pk->lock);
1876 CRYPTO_THREAD_unlock(pk->lock);
1879 /* Make sure that the keymgmt key type matches the legacy NID */
1880 if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1883 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1886 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import,
1887 libctx, propquery)) {
1888 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1894 * If the dirty counter changed since last time, then clear the
1895 * operation cache. In that case, we know that |i| is zero. Just
1896 * in case this is a re-export, we increment then decrement the
1897 * keymgmt reference counter.
1899 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1900 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1905 if (!CRYPTO_THREAD_write_lock(pk->lock))
1907 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
1908 && !evp_keymgmt_util_clear_operation_cache(pk, 0)) {
1909 CRYPTO_THREAD_unlock(pk->lock);
1910 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1912 EVP_KEYMGMT_free(tmp_keymgmt);
1915 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1917 /* Check to make sure some other thread didn't get there first */
1918 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1919 if (op != NULL && op->keymgmt != NULL) {
1920 void *tmp_keydata = op->keydata;
1922 CRYPTO_THREAD_unlock(pk->lock);
1923 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1924 keydata = tmp_keydata;
1928 /* Add the new export to the operation cache */
1929 if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata)) {
1930 CRYPTO_THREAD_unlock(pk->lock);
1931 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1936 /* Synchronize the dirty count */
1937 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1939 CRYPTO_THREAD_unlock(pk->lock);
1942 #endif /* FIPS_MODULE */
1944 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1948 * If nothing was exported, |tmp_keymgmt| might point at a freed
1949 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1950 * the caller either way in that case.
1952 if (keydata == NULL)
1955 if (keymgmt != NULL)
1956 *keymgmt = tmp_keymgmt;
1958 EVP_KEYMGMT_free(allocated_keymgmt);
1963 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
1965 if (!ossl_assert(dest != NULL))
1968 if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
1969 EVP_KEYMGMT *keymgmt = src->keymgmt;
1970 void *keydata = src->keydata;
1971 int type = src->type;
1972 const char *keytype = NULL;
1974 keytype = EVP_KEYMGMT_get0_name(keymgmt);
1977 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1978 * else in our code. If it's not one of the well known EVP_PKEY_xxx
1979 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1980 * The check is kept as a safety measure.
1982 if (!ossl_assert(type != EVP_PKEY_NONE)) {
1983 ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
1984 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1989 /* Prefer the legacy key type name for error reporting */
1990 if (type != EVP_PKEY_KEYMGMT)
1991 keytype = OBJ_nid2sn(type);
1993 /* Make sure we have a clean slate to copy into */
1994 if (*dest == NULL) {
1995 *dest = EVP_PKEY_new();
1996 if (*dest == NULL) {
1997 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
2001 evp_pkey_free_it(*dest);
2004 if (EVP_PKEY_set_type(*dest, type)) {
2005 /* If the key is typed but empty, we're done */
2006 if (keydata == NULL)
2009 if ((*dest)->ameth->import_from == NULL) {
2010 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2011 "key type = %s", keytype);
2014 * We perform the export in the same libctx as the keymgmt
2015 * that we are using.
2017 OSSL_LIB_CTX *libctx =
2018 ossl_provider_libctx(keymgmt->prov);
2019 EVP_PKEY_CTX *pctx =
2020 EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2023 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
2026 && evp_keymgmt_export(keymgmt, keydata,
2027 OSSL_KEYMGMT_SELECT_ALL,
2028 (*dest)->ameth->import_from,
2030 /* Synchronize the dirty count */
2031 (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2033 EVP_PKEY_CTX_free(pctx);
2036 EVP_PKEY_CTX_free(pctx);
2039 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2040 "key type = %s", keytype);
2047 void *evp_pkey_get_legacy(EVP_PKEY *pk)
2049 EVP_PKEY *tmp_copy = NULL;
2052 if (!ossl_assert(pk != NULL))
2056 * If this isn't an assigned provider side key, we just use any existing
2057 * origin legacy key.
2059 if (!evp_pkey_is_assigned(pk))
2061 if (!evp_pkey_is_provided(pk))
2062 return pk->pkey.ptr;
2064 if (!CRYPTO_THREAD_read_lock(pk->lock))
2067 ret = pk->legacy_cache_pkey.ptr;
2069 if (!CRYPTO_THREAD_unlock(pk->lock))
2075 if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2078 if (!CRYPTO_THREAD_write_lock(pk->lock))
2081 /* Check again in case some other thread has updated it in the meantime */
2082 ret = pk->legacy_cache_pkey.ptr;
2084 /* Steal the legacy key reference from the temporary copy */
2085 ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2086 tmp_copy->pkey.ptr = NULL;
2089 if (!CRYPTO_THREAD_unlock(pk->lock)) {
2095 EVP_PKEY_free(tmp_copy);
2099 #endif /* FIPS_MODULE */
2101 int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2105 OSSL_PARAM params[2];
2106 unsigned char buffer[2048];
2107 unsigned char *buf = NULL;
2110 if (key_name == NULL
2114 memset(buffer, 0, sizeof(buffer));
2115 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2116 params[1] = OSSL_PARAM_construct_end();
2117 if (!EVP_PKEY_get_params(pkey, params)) {
2118 if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2120 buf_sz = params[0].return_size;
2122 * If it failed because the buffer was too small then allocate the
2123 * required buffer size and retry.
2125 buf = OPENSSL_zalloc(buf_sz);
2128 params[0].data = buf;
2129 params[0].data_size = buf_sz;
2131 if (!EVP_PKEY_get_params(pkey, params))
2134 /* Fail if the param was not found */
2135 if (!OSSL_PARAM_modified(params))
2137 ret = OSSL_PARAM_get_BN(params, bn);
2143 int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2144 unsigned char *buf, size_t max_buf_sz,
2147 OSSL_PARAM params[2];
2148 int ret1 = 0, ret2 = 0;
2150 if (key_name == NULL)
2153 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2154 params[1] = OSSL_PARAM_construct_end();
2155 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2156 ret2 = OSSL_PARAM_modified(params);
2157 if (ret2 && out_sz != NULL)
2158 *out_sz = params[0].return_size;
2159 return ret1 && ret2;
2162 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2163 char *str, size_t max_buf_sz,
2166 OSSL_PARAM params[2];
2167 int ret1 = 0, ret2 = 0;
2169 if (key_name == NULL)
2172 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2173 params[1] = OSSL_PARAM_construct_end();
2174 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2175 ret2 = OSSL_PARAM_modified(params);
2176 if (ret2 && out_sz != NULL)
2177 *out_sz = params[0].return_size;
2178 return ret1 && ret2;
2181 int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2184 OSSL_PARAM params[2];
2186 if (key_name == NULL)
2189 params[0] = OSSL_PARAM_construct_int(key_name, out);
2190 params[1] = OSSL_PARAM_construct_end();
2191 return EVP_PKEY_get_params(pkey, params)
2192 && OSSL_PARAM_modified(params);
2195 int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2198 OSSL_PARAM params[2];
2200 if (key_name == NULL)
2203 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2204 params[1] = OSSL_PARAM_construct_end();
2205 return EVP_PKEY_get_params(pkey, params)
2206 && OSSL_PARAM_modified(params);
2209 int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2211 OSSL_PARAM params[2];
2213 if (key_name == NULL)
2216 params[0] = OSSL_PARAM_construct_int(key_name, &in);
2217 params[1] = OSSL_PARAM_construct_end();
2218 return EVP_PKEY_set_params(pkey, params);
2221 int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2223 OSSL_PARAM params[2];
2225 if (key_name == NULL)
2228 params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2229 params[1] = OSSL_PARAM_construct_end();
2230 return EVP_PKEY_set_params(pkey, params);
2233 int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2236 OSSL_PARAM params[2];
2237 unsigned char buffer[2048];
2240 if (key_name == NULL
2243 || !evp_pkey_is_assigned(pkey))
2246 bsize = BN_num_bytes(bn);
2247 if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2250 if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2252 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2253 params[1] = OSSL_PARAM_construct_end();
2254 return EVP_PKEY_set_params(pkey, params);
2257 int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2260 OSSL_PARAM params[2];
2262 if (key_name == NULL)
2265 params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2266 params[1] = OSSL_PARAM_construct_end();
2267 return EVP_PKEY_set_params(pkey, params);
2270 int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2271 const unsigned char *buf, size_t bsize)
2273 OSSL_PARAM params[2];
2275 if (key_name == NULL)
2278 params[0] = OSSL_PARAM_construct_octet_string(key_name,
2279 (unsigned char *)buf, bsize);
2280 params[1] = OSSL_PARAM_construct_end();
2281 return EVP_PKEY_set_params(pkey, params);
2284 const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2286 return (pkey != NULL && evp_pkey_is_provided(pkey))
2287 ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2291 int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2294 if (evp_pkey_is_provided(pkey)) {
2296 return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2300 * We will hopefully never find the need to set individual data in
2301 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2302 * sure. This bit of code can be enabled if we find the need. If
2303 * not, it can safely be removed when #legacy support is removed.
2306 else if (evp_pkey_is_legacy(pkey)) {
2307 return evp_pkey_set_params_to_ctrl(pkey, params);
2312 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2316 const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2318 return (pkey != NULL && evp_pkey_is_provided(pkey))
2319 ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2323 int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2326 if (evp_pkey_is_provided(pkey))
2327 return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params);
2329 else if (evp_pkey_is_legacy(pkey))
2330 return evp_pkey_get_params_to_ctrl(pkey, params);
2333 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2338 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2346 if (pkey->keymgmt == NULL
2347 || pkey->keydata == NULL) {
2348 # ifndef OPENSSL_NO_EC
2349 /* Might work through the legacy route */
2350 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2355 return EC_KEY_get_conv_form(ec);
2361 if (!EVP_PKEY_get_utf8_string_param(pkey,
2362 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2363 name, sizeof(name), &name_len))
2366 if (strcmp(name, "uncompressed") == 0)
2367 return POINT_CONVERSION_UNCOMPRESSED;
2369 if (strcmp(name, "compressed") == 0)
2370 return POINT_CONVERSION_COMPRESSED;
2372 if (strcmp(name, "hybrid") == 0)
2373 return POINT_CONVERSION_HYBRID;
2378 int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2386 if (pkey->keymgmt == NULL
2387 || pkey->keydata == NULL) {
2388 # ifndef OPENSSL_NO_EC
2389 /* Might work through the legacy route */
2390 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2391 const EC_GROUP *grp;
2395 grp = EC_KEY_get0_group(ec);
2399 return EC_GROUP_get_field_type(grp);
2405 if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2406 fstr, sizeof(fstr), &fstrlen))
2409 if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2410 return NID_X9_62_prime_field;
2411 else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2412 return NID_X9_62_characteristic_two_field;