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/ffc.h"
38 #include "crypto/asn1.h"
39 #include "crypto/evp.h"
40 #include "crypto/dh.h"
41 #include "crypto/dsa.h"
42 #include "crypto/ec.h"
43 #include "crypto/ecx.h"
44 #include "crypto/rsa.h"
45 #include "crypto/x509.h"
46 #include "internal/provider.h"
47 #include "evp_local.h"
49 #include "e_os.h" /* strcasecmp on Windows */
51 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
52 int len, EVP_KEYMGMT *keymgmt);
53 static void evp_pkey_free_it(EVP_PKEY *key);
57 /* The type of parameters selected in key parameter functions */
58 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
60 int EVP_PKEY_bits(const EVP_PKEY *pkey)
65 size = pkey->cache.bits;
66 if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
67 size = pkey->ameth->pkey_bits(pkey);
69 return size < 0 ? 0 : size;
72 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
77 size = pkey->cache.security_bits;
78 if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
79 size = pkey->ameth->pkey_security_bits(pkey);
81 return size < 0 ? 0 : size;
84 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
86 # ifndef OPENSSL_NO_DSA
87 if (pkey->type == EVP_PKEY_DSA) {
88 int ret = pkey->save_parameters;
91 pkey->save_parameters = mode;
95 # ifndef OPENSSL_NO_EC
96 if (pkey->type == EVP_PKEY_EC) {
97 int ret = pkey->save_parameters;
100 pkey->save_parameters = mode;
107 int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
109 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
112 void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
114 return CRYPTO_get_ex_data(&key->ex_data, idx);
117 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
120 * Clean up legacy stuff from this function when legacy support is gone.
123 EVP_PKEY *downgraded_from = NULL;
127 * If |to| is a legacy key and |from| isn't, we must make a downgraded
128 * copy of |from|. If that fails, this function fails.
130 if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
131 if (!evp_pkey_copy_downgraded(&downgraded_from, from))
133 from = downgraded_from;
137 * Make sure |to| is typed. Content is less important at this early
140 * 1. If |to| is untyped, assign |from|'s key type to it.
141 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
142 * (|from| was already downgraded above)
144 * If |to| is a provided key, there's nothing more to do here, functions
145 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
146 * further down help us find out if they are the same or not.
148 if (evp_pkey_is_blank(to)) {
149 if (evp_pkey_is_legacy(from)) {
150 if (EVP_PKEY_set_type(to, from->type) == 0)
153 if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
156 } else if (evp_pkey_is_legacy(to)) {
157 if (to->type != from->type) {
158 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
163 if (EVP_PKEY_missing_parameters(from)) {
164 ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
168 if (!EVP_PKEY_missing_parameters(to)) {
169 if (EVP_PKEY_parameters_eq(to, from) == 1)
172 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
176 /* For purely provided keys, we just call the keymgmt utility */
177 if (to->keymgmt != NULL && from->keymgmt != NULL) {
178 ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
183 * If |to| is provided, we know that |from| is legacy at this point.
184 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
185 * to copy the appropriate data to |to|'s keydata.
186 * We cannot override existing data so do it only if there is no keydata
189 if (to->keymgmt != NULL && to->keydata == NULL) {
190 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
192 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
196 * If we get a NULL, it could be an internal error, or it could be
197 * that there's a key mismatch. We're pretending the latter...
199 if (from_keydata == NULL)
200 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
202 ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,
204 SELECT_PARAMETERS)) != NULL;
208 /* Both keys are legacy */
209 if (from->ameth != NULL && from->ameth->param_copy != NULL)
210 ok = from->ameth->param_copy(to, from);
212 EVP_PKEY_free(downgraded_from);
216 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
219 if (pkey->keymgmt != NULL)
220 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
221 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
222 return pkey->ameth->param_missing(pkey);
228 * This function is called for any mixture of keys except pure legacy pair.
229 * When legacy keys are gone, we replace a call to this functions with
230 * a call to evp_keymgmt_util_match().
232 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
235 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
236 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
238 /* If none of them are provided, this function shouldn't have been called */
239 if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
242 /* For purely provided keys, we just call the keymgmt utility */
243 if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
244 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
247 * At this point, one of them is provided, the other not. This allows
248 * us to compare types using legacy NIDs.
250 if (evp_pkey_is_legacy(a)
251 && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
252 return -1; /* not the same key type */
253 if (evp_pkey_is_legacy(b)
254 && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
255 return -1; /* not the same key type */
258 * We've determined that they both are the same keytype, so the next
259 * step is to do a bit of cross export to ensure we have keydata for
260 * both keys in the same keymgmt.
262 keymgmt1 = a->keymgmt;
263 keydata1 = a->keydata;
264 keymgmt2 = b->keymgmt;
265 keydata2 = b->keydata;
267 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
269 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
270 if (tmp_keydata != NULL) {
272 keydata1 = tmp_keydata;
275 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
277 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
278 if (tmp_keydata != NULL) {
280 keydata2 = tmp_keydata;
284 /* If we still don't have matching keymgmt implementations, we give up */
285 if (keymgmt1 != keymgmt2)
288 /* If the keymgmt implementations are NULL, the export failed */
289 if (keymgmt1 == NULL)
292 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
295 # ifndef OPENSSL_NO_DEPRECATED_3_0
296 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
298 return EVP_PKEY_parameters_eq(a, b);
302 int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
305 * This will just call evp_keymgmt_util_match when legacy support
309 if (a->keymgmt != NULL || b->keymgmt != NULL)
310 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
312 /* All legacy keys */
313 if (a->type != b->type)
315 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
316 return a->ameth->param_cmp(a, b);
320 # ifndef OPENSSL_NO_DEPRECATED_3_0
321 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
323 return EVP_PKEY_eq(a, b);
327 int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
330 * This will just call evp_keymgmt_util_match when legacy support
334 /* Trivial shortcuts */
337 if (a == NULL || b == NULL)
340 if (a->keymgmt != NULL || b->keymgmt != NULL)
341 return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
342 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY));
344 /* All legacy keys */
345 if (a->type != b->type)
348 if (a->ameth != NULL) {
350 /* Compare parameters if the algorithm has them */
351 if (a->ameth->param_cmp != NULL) {
352 ret = a->ameth->param_cmp(a, b);
357 if (a->ameth->pub_cmp != NULL)
358 return a->ameth->pub_cmp(a, b);
365 static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
370 const unsigned char *key,
374 EVP_PKEY *pkey = NULL;
375 EVP_PKEY_CTX *ctx = NULL;
376 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
379 # ifndef OPENSSL_NO_ENGINE
380 /* Check if there is an Engine for this type */
385 ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);
386 else if (nidtype != EVP_PKEY_NONE)
387 ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);
389 /* If tmpe is NULL then no engine is claiming to support this type */
397 if (e == NULL && ameth == NULL) {
399 * No engine is claiming to support this type, so lets see if we have
402 ctx = EVP_PKEY_CTX_new_from_name(libctx,
403 strtype != NULL ? strtype
404 : OBJ_nid2sn(nidtype),
408 /* May fail if no provider available */
410 if (EVP_PKEY_fromdata_init(ctx) == 1) {
411 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
413 ERR_clear_last_mark();
414 params[0] = OSSL_PARAM_construct_octet_string(
415 key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
416 : OSSL_PKEY_PARAM_PUB_KEY,
419 if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
420 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
424 EVP_PKEY_CTX_free(ctx);
429 /* else not supported so fallback to legacy */
432 /* Legacy code path */
434 pkey = EVP_PKEY_new();
436 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
440 if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) {
441 /* EVPerr already called */
445 if (!ossl_assert(pkey->ameth != NULL))
449 if (pkey->ameth->set_priv_key == NULL) {
450 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
454 if (!pkey->ameth->set_priv_key(pkey, key, len)) {
455 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
459 if (pkey->ameth->set_pub_key == NULL) {
460 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
464 if (!pkey->ameth->set_pub_key(pkey, key, len)) {
465 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
476 EVP_PKEY_CTX_free(ctx);
480 EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
483 const unsigned char *priv, size_t len)
485 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,
489 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
490 const unsigned char *priv,
493 return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);
496 EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
497 const char *keytype, const char *propq,
498 const unsigned char *pub, size_t len)
500 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,
504 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
505 const unsigned char *pub,
508 return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);
511 struct raw_key_details_st
518 static OSSL_CALLBACK get_raw_key_details;
519 static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
521 const OSSL_PARAM *p = NULL;
522 struct raw_key_details_st *raw_key = arg;
524 if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
525 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
527 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
528 SIZE_MAX, raw_key->len);
529 } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
530 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
532 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
533 SIZE_MAX, raw_key->len);
539 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
542 if (pkey->keymgmt != NULL) {
543 struct raw_key_details_st raw_key;
545 raw_key.key = priv == NULL ? NULL : &priv;
547 raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
549 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
550 get_raw_key_details, &raw_key);
553 if (pkey->ameth == NULL) {
554 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
558 if (pkey->ameth->get_priv_key == NULL) {
559 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
563 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
564 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
571 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
574 if (pkey->keymgmt != NULL) {
575 struct raw_key_details_st raw_key;
577 raw_key.key = pub == NULL ? NULL : &pub;
579 raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
581 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
582 get_raw_key_details, &raw_key);
585 if (pkey->ameth == NULL) {
586 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
590 if (pkey->ameth->get_pub_key == NULL) {
591 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
595 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
596 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
603 static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
604 const char *cipher_name,
605 const EVP_CIPHER *cipher,
606 OSSL_LIB_CTX *libctx,
607 const char *propq, ENGINE *e)
609 # ifndef OPENSSL_NO_CMAC
610 # ifndef OPENSSL_NO_ENGINE
611 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
613 OSSL_PARAM params[5], *p = params;
614 EVP_PKEY *pkey = NULL;
618 cipher_name = EVP_CIPHER_name(cipher);
620 if (cipher_name == NULL) {
621 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
625 ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
629 if (!EVP_PKEY_fromdata_init(ctx)) {
630 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
634 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
636 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
637 (char *)cipher_name, 0);
639 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
641 # ifndef OPENSSL_NO_ENGINE
642 if (engine_id != NULL)
643 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,
644 (char *)engine_id, 0);
646 *p = OSSL_PARAM_construct_end();
648 if (!EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params)) {
649 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
654 EVP_PKEY_CTX_free(ctx);
658 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
663 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
664 size_t len, const EVP_CIPHER *cipher)
666 return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
669 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
671 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
674 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
676 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
679 # ifndef OPENSSL_NO_ENGINE
680 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
683 if (!ENGINE_init(e)) {
684 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
687 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
689 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
693 ENGINE_finish(pkey->pmeth_engine);
694 pkey->pmeth_engine = e;
698 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
704 # ifndef OPENSSL_NO_DEPRECATED_3_0
705 static void detect_foreign_key(EVP_PKEY *pkey)
707 switch (pkey->type) {
709 pkey->foreign = pkey->pkey.rsa != NULL
710 && ossl_rsa_is_foreign(pkey->pkey.rsa);
712 # ifndef OPENSSL_NO_EC
715 pkey->foreign = pkey->pkey.ec != NULL
716 && ossl_ec_key_is_foreign(pkey->pkey.ec);
719 # ifndef OPENSSL_NO_DSA
721 pkey->foreign = pkey->pkey.dsa != NULL
722 && ossl_dsa_is_foreign(pkey->pkey.dsa);
725 # ifndef OPENSSL_NO_DH
727 pkey->foreign = pkey->pkey.dh != NULL
728 && ossl_dh_is_foreign(pkey->pkey.dh);
737 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
739 # ifndef OPENSSL_NO_EC
742 pktype = EVP_PKEY_type(type);
743 if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
744 const EC_GROUP *group = EC_KEY_get0_group(key);
747 int curve = EC_GROUP_get_curve_name(group);
750 * Regardless of what is requested the SM2 curve must be SM2 type,
751 * and non SM2 curves are EC type.
753 if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
755 else if(curve != NID_sm2 && pktype == EVP_PKEY_SM2)
761 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
764 pkey->pkey.ptr = key;
765 detect_foreign_key(pkey);
767 return (key != NULL);
771 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
776 if (!evp_pkey_is_provided(pkey))
777 return pkey->pkey.ptr;
782 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
784 const ASN1_OCTET_STRING *os = NULL;
785 if (pkey->type != EVP_PKEY_HMAC) {
786 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
789 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
797 # ifndef OPENSSL_NO_POLY1305
798 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
800 const ASN1_OCTET_STRING *os = NULL;
801 if (pkey->type != EVP_PKEY_POLY1305) {
802 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
805 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
814 # ifndef OPENSSL_NO_SIPHASH
815 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
817 const ASN1_OCTET_STRING *os = NULL;
819 if (pkey->type != EVP_PKEY_SIPHASH) {
820 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
823 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
832 # ifndef OPENSSL_NO_DSA
833 static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
835 if (pkey->type != EVP_PKEY_DSA) {
836 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
839 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
842 const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
844 return evp_pkey_get0_DSA_int(pkey);
847 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
849 int ret = EVP_PKEY_assign_DSA(pkey, key);
854 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
856 DSA *ret = evp_pkey_get0_DSA_int(pkey);
862 # endif /* OPENSSL_NO_DSA */
864 # ifndef OPENSSL_NO_EC
865 static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
867 if (EVP_PKEY_base_id(pkey) != type) {
868 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
871 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
874 static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
876 ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
878 if (ret != NULL && !ossl_ecx_key_up_ref(ret))
883 # define IMPLEMENT_ECX_VARIANT(NAME) \
884 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
886 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
888 IMPLEMENT_ECX_VARIANT(X25519)
889 IMPLEMENT_ECX_VARIANT(X448)
890 IMPLEMENT_ECX_VARIANT(ED25519)
891 IMPLEMENT_ECX_VARIANT(ED448)
895 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
897 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
902 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
903 * related to ffdhe and modp (which cache q = (p - 1) / 2),
904 * and returns 0 for all other dh parameter generation types including
905 * RFC5114 named groups.
907 * The EVP_PKEY_DH type is used for dh parameter generation types:
908 * - named safe prime groups related to ffdhe and modp
909 * - safe prime generator
911 * The type EVP_PKEY_DHX is used for dh parameter generation types
912 * - fips186-4 and fips186-2
913 * - rfc5114 named groups.
915 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
917 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
918 * q value to be stored.
920 if (ossl_dh_is_named_safe_prime_group(dhkey))
923 type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
925 ret = EVP_PKEY_assign(pkey, type, dhkey);
932 DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
934 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
935 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
938 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
941 const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
943 return evp_pkey_get0_DH_int(pkey);
946 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
948 DH *ret = evp_pkey_get0_DH_int(pkey);
956 int EVP_PKEY_type(int type)
959 const EVP_PKEY_ASN1_METHOD *ameth;
961 ameth = EVP_PKEY_asn1_find(&e, type);
963 ret = ameth->pkey_id;
966 # ifndef OPENSSL_NO_ENGINE
972 int EVP_PKEY_id(const EVP_PKEY *pkey)
977 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
979 return EVP_PKEY_type(pkey->type);
983 * These hard coded cases are pure hackery to get around the fact
984 * that names in crypto/objects/objects.txt are a mess. There is
985 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
986 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
987 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
988 * "DSA" is accurate... but still, better be safe and hard-code
989 * names that we know.
990 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
991 * EVP_PKEY_EC, because of aliasing.
992 * This should be cleaned away along with all other #legacy support.
994 static const OSSL_ITEM standard_name2type[] = {
995 { EVP_PKEY_RSA, "RSA" },
996 { EVP_PKEY_RSA_PSS, "RSA-PSS" },
997 { EVP_PKEY_EC, "EC" },
998 { EVP_PKEY_ED25519, "ED25519" },
999 { EVP_PKEY_ED448, "ED448" },
1000 { EVP_PKEY_X25519, "X25519" },
1001 { EVP_PKEY_X448, "X448" },
1002 { EVP_PKEY_SM2, "SM2" },
1003 { EVP_PKEY_DH, "DH" },
1004 { EVP_PKEY_DHX, "X9.42 DH" },
1005 { EVP_PKEY_DHX, "DHX" },
1006 { EVP_PKEY_DSA, "DSA" },
1009 int evp_pkey_name2type(const char *name)
1014 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1015 if (strcasecmp(name, standard_name2type[i].ptr) == 0)
1016 return (int)standard_name2type[i].id;
1019 if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1021 return EVP_PKEY_type(OBJ_ln2nid(name));
1024 const char *evp_pkey_type2name(int type)
1028 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1029 if (type == (int)standard_name2type[i].id)
1030 return standard_name2type[i].ptr;
1033 return OBJ_nid2sn(type);
1036 int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
1038 if (pkey->keymgmt == NULL) {
1039 int type = evp_pkey_name2type(name);
1041 return pkey->type == type;
1043 return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1046 int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1047 void (*fn)(const char *name, void *data),
1050 if (!evp_pkey_is_typed(pkey))
1053 if (!evp_pkey_is_provided(pkey)) {
1054 const char *name = OBJ_nid2sn(EVP_PKEY_id(pkey));
1059 return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1062 int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1064 if (pkey->keymgmt == NULL) {
1065 switch (EVP_PKEY_base_id(pkey)) {
1068 # ifndef OPENSSL_NO_DSA
1072 # ifndef OPENSSL_NO_EC
1073 case EVP_PKEY_ED25519:
1074 case EVP_PKEY_ED448:
1076 case EVP_PKEY_EC: /* Including SM2 */
1077 return EC_KEY_can_sign(pkey->pkey.ec);
1083 const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt);
1084 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1085 const char *supported_sig =
1086 pkey->keymgmt->query_operation_name != NULL
1087 ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
1088 : EVP_KEYMGMT_name(pkey->keymgmt);
1089 EVP_SIGNATURE *signature = NULL;
1091 signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
1092 if (signature != NULL) {
1093 EVP_SIGNATURE_free(signature);
1100 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1102 BIO_set_indent(*out, saved_indent);
1104 BIO *next = BIO_pop(*out);
1112 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1118 long i = BIO_get_indent(*out);
1120 *saved_indent = (i < 0 ? 0 : i);
1121 if (BIO_set_indent(*out, indent) <= 0) {
1122 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
1126 if (BIO_set_indent(*out, indent) <= 0) {
1127 print_reset_indent(out, *pop_f_prefix, *saved_indent);
1134 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1137 return BIO_indent(out, indent, 128)
1138 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1139 kstr, OBJ_nid2ln(pkey->type)) > 0;
1142 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1143 int selection /* For provided encoding */,
1144 const char *propquery /* For provided encoding */,
1145 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1146 int indent, ASN1_PCTX *pctx),
1147 ASN1_PCTX *legacy_pctx /* For legacy print */)
1151 OSSL_ENCODER_CTX *ctx = NULL;
1152 int ret = -2; /* default to unsupported */
1154 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1157 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1159 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1160 ret = OSSL_ENCODER_to_bio(ctx, out);
1161 OSSL_ENCODER_CTX_free(ctx);
1166 /* legacy fallback */
1167 if (legacy_print != NULL)
1168 ret = legacy_print(out, pkey, 0, legacy_pctx);
1170 ret = unsup_alg(out, pkey, 0, "Public Key");
1173 print_reset_indent(&out, pop_f_prefix, saved_indent);
1177 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1178 int indent, ASN1_PCTX *pctx)
1180 return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1181 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1185 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1186 int indent, ASN1_PCTX *pctx)
1188 return print_pkey(pkey, out, indent, EVP_PKEY_KEYPAIR, NULL,
1189 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1193 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1194 int indent, ASN1_PCTX *pctx)
1196 return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1197 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1201 # ifndef OPENSSL_NO_STDIO
1202 int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1203 int indent, ASN1_PCTX *pctx)
1206 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1210 ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1215 int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1216 int indent, ASN1_PCTX *pctx)
1219 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1223 ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1228 int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1229 int indent, ASN1_PCTX *pctx)
1232 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1236 ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1242 static void mdname2nid(const char *mdname, void *data)
1244 int *nid = (int *)data;
1246 if (*nid != NID_undef)
1249 *nid = OBJ_sn2nid(mdname);
1250 if (*nid == NID_undef)
1251 *nid = OBJ_ln2nid(mdname);
1254 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1255 int arg1, void *arg2)
1257 if (pkey->keymgmt == NULL)
1260 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1262 char mdname[80] = "";
1263 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1268 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1269 /* Make sure the MD is in the namemap if available */
1270 EVP_MD *md = EVP_MD_fetch(libctx, mdname, NULL);
1271 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
1272 int nid = NID_undef;
1275 * The only reason to fetch the MD was to make sure it is in the
1276 * namemap. We can immediately free it.
1279 mdnum = ossl_namemap_name2num(namemap, mdname);
1284 * We have the namemap number - now we need to find the
1287 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1298 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1300 if (pkey->ameth == NULL)
1301 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1302 if (pkey->ameth->pkey_ctrl == NULL)
1304 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1307 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1309 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1312 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1313 char *mdname, size_t mdname_sz)
1315 if (pkey->ameth == NULL)
1316 return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1321 int nid = NID_undef;
1322 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1323 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1326 OPENSSL_strlcpy(mdname, name, mdname_sz);
1331 int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1334 return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1335 gname, gname_sz, gname_len);
1338 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
1340 int rv = -2, default_nid;
1344 * If there is a mandatory default digest and this isn't it, then
1345 * the answer is 'no'.
1347 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
1349 return (nid == default_nid);
1350 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
1357 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1360 if (pkey != NULL && evp_pkey_is_provided(pkey))
1362 EVP_PKEY_set_octet_string_param(pkey,
1363 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1364 (unsigned char *)pub, publen);
1366 if (publen > INT_MAX)
1368 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1369 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
1375 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1379 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
1380 size_t return_size = OSSL_PARAM_UNMODIFIED;
1383 * We know that this is going to fail, but it will give us a size
1386 EVP_PKEY_get_octet_string_param(pkey,
1387 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1388 NULL, 0, &return_size);
1389 if (return_size == OSSL_PARAM_UNMODIFIED)
1392 *ppub = OPENSSL_malloc(return_size);
1396 if (!EVP_PKEY_get_octet_string_param(pkey,
1397 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1398 *ppub, return_size, NULL))
1404 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1410 #endif /* FIPS_MODULE */
1412 /*- All methods below can also be used in FIPS_MODULE */
1414 EVP_PKEY *EVP_PKEY_new(void)
1416 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1419 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1423 ret->type = EVP_PKEY_NONE;
1424 ret->save_type = EVP_PKEY_NONE;
1425 ret->references = 1;
1427 ret->lock = CRYPTO_THREAD_lock_new();
1428 if (ret->lock == NULL) {
1429 EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1434 ret->save_parameters = 1;
1435 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1436 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1443 CRYPTO_THREAD_lock_free(ret->lock);
1449 * Setup a public key management method.
1451 * For legacy keys, either |type| or |str| is expected to have the type
1452 * information. In this case, the setup consists of finding an ASN1 method
1453 * and potentially an ENGINE, and setting those fields in |pkey|.
1455 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1456 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1458 * If pkey is NULL just return 1 or 0 if the key management method exists.
1461 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1462 int len, EVP_KEYMGMT *keymgmt)
1465 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1466 ENGINE **eptr = (e == NULL) ? &e : NULL;
1470 * The setups can't set both legacy and provider side methods.
1473 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1474 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1475 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1483 free_it = free_it || pkey->pkey.ptr != NULL;
1485 free_it = free_it || pkey->keydata != NULL;
1487 evp_pkey_free_it(pkey);
1490 * If key type matches and a method exists then this lookup has
1491 * succeeded once so just indicate success.
1493 if (pkey->type != EVP_PKEY_NONE
1494 && type == pkey->save_type
1495 && pkey->ameth != NULL)
1497 # ifndef OPENSSL_NO_ENGINE
1498 /* If we have ENGINEs release them */
1499 ENGINE_finish(pkey->engine);
1500 pkey->engine = NULL;
1501 ENGINE_finish(pkey->pmeth_engine);
1502 pkey->pmeth_engine = NULL;
1508 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1509 else if (type != EVP_PKEY_NONE)
1510 ameth = EVP_PKEY_asn1_find(eptr, type);
1511 # ifndef OPENSSL_NO_ENGINE
1512 if (pkey == NULL && eptr != NULL)
1522 check = check && ameth == NULL;
1524 check = check && keymgmt == NULL;
1526 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1531 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1532 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1536 pkey->keymgmt = keymgmt;
1538 pkey->save_type = type;
1543 * If the internal "origin" key is provider side, don't save |ameth|.
1544 * The main reason is that |ameth| is one factor to detect that the
1545 * internal "origin" key is a legacy one.
1547 if (keymgmt == NULL)
1548 pkey->ameth = ameth;
1552 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1553 * for any key type that has a legacy implementation, regardless of
1554 * if the internal key is a legacy or a provider side one. When
1555 * there is no legacy implementation for the key, the type becomes
1556 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1557 * with functions that expect legacy internal keys.
1559 if (ameth != NULL) {
1560 if (type == EVP_PKEY_NONE)
1561 pkey->type = ameth->pkey_id;
1563 pkey->type = EVP_PKEY_KEYMGMT;
1571 static void find_ameth(const char *name, void *data)
1573 const char **str = data;
1576 * The error messages from pkey_set_type() are uninteresting here,
1581 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1585 else if (str[1] == NULL)
1593 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1596 # define EVP_PKEY_TYPE_STR str[0]
1597 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1599 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1600 * Ideally, only one should be found. If two (or more) are found, the
1601 * match is ambiguous. This should never happen, but...
1603 const char *str[2] = { NULL, NULL };
1605 if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1606 || str[1] != NULL) {
1607 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1611 # define EVP_PKEY_TYPE_STR NULL
1612 # define EVP_PKEY_TYPE_STRLEN -1
1614 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1615 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1618 #undef EVP_PKEY_TYPE_STR
1619 #undef EVP_PKEY_TYPE_STRLEN
1622 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1626 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1629 REF_PRINT_COUNT("EVP_PKEY", pkey);
1630 REF_ASSERT_ISNT(i < 2);
1631 return ((i > 1) ? 1 : 0);
1635 EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1640 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1644 if ((dup_pk = EVP_PKEY_new()) == NULL)
1647 if (evp_pkey_is_blank(pkey))
1650 if (evp_pkey_is_provided(pkey)) {
1651 if (!evp_keymgmt_util_copy(dup_pk, pkey,
1652 OSSL_KEYMGMT_SELECT_ALL))
1657 if (evp_pkey_is_legacy(pkey)) {
1658 const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1660 if (ameth == NULL || ameth->copy == NULL) {
1661 if (pkey->pkey.ptr == NULL /* empty key, just set type */
1662 && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1664 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1667 if (!ameth->copy(dup_pk, pkey))
1674 /* copy auxiliary data */
1675 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1676 &dup_pk->ex_data, &pkey->ex_data))
1679 if (pkey->attributes != NULL) {
1680 if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1685 EVP_PKEY_free(dup_pk);
1689 void evp_pkey_free_legacy(EVP_PKEY *x)
1691 const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1692 ENGINE *tmpe = NULL;
1694 if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1695 ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
1697 if (ameth != NULL) {
1698 if (x->legacy_cache_pkey.ptr != NULL) {
1700 * We should never have both a legacy origin key, and a key in the
1703 assert(x->pkey.ptr == NULL);
1705 * For the purposes of freeing we make the legacy cache look like
1706 * a legacy origin key.
1708 x->pkey = x->legacy_cache_pkey;
1709 x->legacy_cache_pkey.ptr = NULL;
1711 if (ameth->pkey_free != NULL)
1712 ameth->pkey_free(x);
1715 # ifndef OPENSSL_NO_ENGINE
1716 ENGINE_finish(tmpe);
1717 ENGINE_finish(x->engine);
1719 ENGINE_finish(x->pmeth_engine);
1720 x->pmeth_engine = NULL;
1723 #endif /* FIPS_MODULE */
1725 static void evp_pkey_free_it(EVP_PKEY *x)
1727 /* internal function; x is never NULL */
1728 evp_keymgmt_util_clear_operation_cache(x, 1);
1730 evp_pkey_free_legacy(x);
1733 if (x->keymgmt != NULL) {
1734 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1735 EVP_KEYMGMT_free(x->keymgmt);
1739 x->type = EVP_PKEY_NONE;
1742 void EVP_PKEY_free(EVP_PKEY *x)
1749 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1750 REF_PRINT_COUNT("EVP_PKEY", x);
1753 REF_ASSERT_ISNT(i < 0);
1754 evp_pkey_free_it(x);
1756 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1758 CRYPTO_THREAD_lock_free(x->lock);
1760 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1765 int EVP_PKEY_size(const EVP_PKEY *pkey)
1770 size = pkey->cache.size;
1772 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1773 size = pkey->ameth->pkey_size(pkey);
1776 return size < 0 ? 0 : size;
1779 const char *EVP_PKEY_description(const EVP_PKEY *pkey)
1781 if (!evp_pkey_is_assigned(pkey))
1784 if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1785 return pkey->keymgmt->description;
1787 if (pkey->ameth != NULL)
1788 return pkey->ameth->info;
1793 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1794 EVP_KEYMGMT **keymgmt,
1795 const char *propquery)
1797 EVP_KEYMGMT *allocated_keymgmt = NULL;
1798 EVP_KEYMGMT *tmp_keymgmt = NULL;
1799 void *keydata = NULL;
1805 /* No key data => nothing to export */
1808 check = check && pk->pkey.ptr == NULL;
1810 check = check && pk->keydata == NULL;
1815 if (pk->pkey.ptr != NULL) {
1817 * If the legacy key doesn't have an dirty counter or export function,
1820 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1825 if (keymgmt != NULL) {
1826 tmp_keymgmt = *keymgmt;
1831 * If no keymgmt was given or found, get a default keymgmt. We do so by
1832 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1834 if (tmp_keymgmt == NULL) {
1835 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1837 tmp_keymgmt = ctx->keymgmt;
1838 ctx->keymgmt = NULL;
1839 EVP_PKEY_CTX_free(ctx);
1842 /* If there's still no keymgmt to be had, give up */
1843 if (tmp_keymgmt == NULL)
1847 if (pk->pkey.ptr != NULL) {
1851 * If the legacy "origin" hasn't changed since last time, we try
1852 * to find our keymgmt in the operation cache. If it has changed,
1853 * |i| remains zero, and we will clear the cache further down.
1855 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1856 if (!CRYPTO_THREAD_read_lock(pk->lock))
1858 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1861 * If |tmp_keymgmt| is present in the operation cache, it means
1862 * that export doesn't need to be redone. In that case, we take
1863 * token copies of the cached pointers, to have token success
1866 if (op != NULL && op->keymgmt != NULL) {
1867 keydata = op->keydata;
1868 CRYPTO_THREAD_unlock(pk->lock);
1871 CRYPTO_THREAD_unlock(pk->lock);
1874 /* Make sure that the keymgmt key type matches the legacy NID */
1875 if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1878 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1881 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt, libctx, propquery)) {
1882 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1888 * If the dirty counter changed since last time, then clear the
1889 * operation cache. In that case, we know that |i| is zero. Just
1890 * in case this is a re-export, we increment then decrement the
1891 * keymgmt reference counter.
1893 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1894 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1899 if (!CRYPTO_THREAD_write_lock(pk->lock))
1901 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
1902 && !evp_keymgmt_util_clear_operation_cache(pk, 0)) {
1903 CRYPTO_THREAD_unlock(pk->lock);
1904 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1906 EVP_KEYMGMT_free(tmp_keymgmt);
1909 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1911 /* Check to make sure some other thread didn't get there first */
1912 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1913 if (op != NULL && op->keymgmt != NULL) {
1914 void *tmp_keydata = op->keydata;
1916 CRYPTO_THREAD_unlock(pk->lock);
1917 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1918 keydata = tmp_keydata;
1922 /* Add the new export to the operation cache */
1923 if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata)) {
1924 CRYPTO_THREAD_unlock(pk->lock);
1925 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1930 /* Synchronize the dirty count */
1931 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1933 CRYPTO_THREAD_unlock(pk->lock);
1936 #endif /* FIPS_MODULE */
1938 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1942 * If nothing was exported, |tmp_keymgmt| might point at a freed
1943 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1944 * the caller either way in that case.
1946 if (keydata == NULL)
1949 if (keymgmt != NULL)
1950 *keymgmt = tmp_keymgmt;
1952 EVP_KEYMGMT_free(allocated_keymgmt);
1957 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
1959 if (!ossl_assert(dest != NULL))
1962 if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
1963 EVP_KEYMGMT *keymgmt = src->keymgmt;
1964 void *keydata = src->keydata;
1965 int type = src->type;
1966 const char *keytype = NULL;
1968 keytype = EVP_KEYMGMT_name(keymgmt);
1971 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1972 * else in our code. If it's not one of the well known EVP_PKEY_xxx
1973 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1974 * The check is kept as a safety measure.
1976 if (!ossl_assert(type != EVP_PKEY_NONE)) {
1977 ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
1978 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1983 /* Prefer the legacy key type name for error reporting */
1984 if (type != EVP_PKEY_KEYMGMT)
1985 keytype = OBJ_nid2sn(type);
1987 /* Make sure we have a clean slate to copy into */
1988 if (*dest == NULL) {
1989 *dest = EVP_PKEY_new();
1990 if (*dest == NULL) {
1991 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1995 evp_pkey_free_it(*dest);
1998 if (EVP_PKEY_set_type(*dest, type)) {
1999 /* If the key is typed but empty, we're done */
2000 if (keydata == NULL)
2003 if ((*dest)->ameth->import_from == NULL) {
2004 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2005 "key type = %s", keytype);
2008 * We perform the export in the same libctx as the keymgmt
2009 * that we are using.
2011 OSSL_LIB_CTX *libctx =
2012 ossl_provider_libctx(keymgmt->prov);
2013 EVP_PKEY_CTX *pctx =
2014 EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2017 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
2020 && evp_keymgmt_export(keymgmt, keydata,
2021 OSSL_KEYMGMT_SELECT_ALL,
2022 (*dest)->ameth->import_from,
2024 /* Synchronize the dirty count */
2025 (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2027 EVP_PKEY_CTX_free(pctx);
2030 EVP_PKEY_CTX_free(pctx);
2033 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2034 "key type = %s", keytype);
2041 void *evp_pkey_get_legacy(EVP_PKEY *pk)
2043 EVP_PKEY *tmp_copy = NULL;
2046 if (!ossl_assert(pk != NULL))
2050 * If this isn't an assigned provider side key, we just use any existing
2051 * origin legacy key.
2053 if (!evp_pkey_is_assigned(pk))
2055 if (!evp_pkey_is_provided(pk))
2056 return pk->pkey.ptr;
2058 if (!CRYPTO_THREAD_read_lock(pk->lock))
2061 ret = pk->legacy_cache_pkey.ptr;
2063 if (!CRYPTO_THREAD_unlock(pk->lock))
2069 if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2072 if (!CRYPTO_THREAD_write_lock(pk->lock))
2075 /* Check again in case some other thread has updated it in the meantime */
2076 ret = pk->legacy_cache_pkey.ptr;
2078 /* Steal the legacy key reference from the temporary copy */
2079 ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2080 tmp_copy->pkey.ptr = NULL;
2083 if (!CRYPTO_THREAD_unlock(pk->lock)) {
2089 EVP_PKEY_free(tmp_copy);
2093 #endif /* FIPS_MODULE */
2095 int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2099 OSSL_PARAM params[2];
2100 unsigned char buffer[2048];
2101 unsigned char *buf = NULL;
2104 if (key_name == NULL
2108 memset(buffer, 0, sizeof(buffer));
2109 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2110 params[1] = OSSL_PARAM_construct_end();
2111 if (!EVP_PKEY_get_params(pkey, params)) {
2112 if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2114 buf_sz = params[0].return_size;
2116 * If it failed because the buffer was too small then allocate the
2117 * required buffer size and retry.
2119 buf = OPENSSL_zalloc(buf_sz);
2122 params[0].data = buf;
2123 params[0].data_size = buf_sz;
2125 if (!EVP_PKEY_get_params(pkey, params))
2128 /* Fail if the param was not found */
2129 if (!OSSL_PARAM_modified(params))
2131 ret = OSSL_PARAM_get_BN(params, bn);
2137 int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2138 unsigned char *buf, size_t max_buf_sz,
2141 OSSL_PARAM params[2];
2142 int ret1 = 0, ret2 = 0;
2144 if (key_name == NULL)
2147 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2148 params[1] = OSSL_PARAM_construct_end();
2149 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2150 ret2 = OSSL_PARAM_modified(params);
2151 if (ret2 && out_sz != NULL)
2152 *out_sz = params[0].return_size;
2153 return ret1 && ret2;
2156 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2157 char *str, size_t max_buf_sz,
2160 OSSL_PARAM params[2];
2161 int ret1 = 0, ret2 = 0;
2163 if (key_name == NULL)
2166 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2167 params[1] = OSSL_PARAM_construct_end();
2168 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2169 ret2 = OSSL_PARAM_modified(params);
2170 if (ret2 && out_sz != NULL)
2171 *out_sz = params[0].return_size;
2172 return ret1 && ret2;
2175 int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2178 OSSL_PARAM params[2];
2180 if (key_name == NULL)
2183 params[0] = OSSL_PARAM_construct_int(key_name, out);
2184 params[1] = OSSL_PARAM_construct_end();
2185 return EVP_PKEY_get_params(pkey, params)
2186 && OSSL_PARAM_modified(params);
2189 int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2192 OSSL_PARAM params[2];
2194 if (key_name == NULL)
2197 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2198 params[1] = OSSL_PARAM_construct_end();
2199 return EVP_PKEY_get_params(pkey, params)
2200 && OSSL_PARAM_modified(params);
2203 int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2205 OSSL_PARAM params[2];
2207 if (key_name == NULL)
2210 params[0] = OSSL_PARAM_construct_int(key_name, &in);
2211 params[1] = OSSL_PARAM_construct_end();
2212 return EVP_PKEY_set_params(pkey, params);
2215 int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2217 OSSL_PARAM params[2];
2219 if (key_name == NULL)
2222 params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2223 params[1] = OSSL_PARAM_construct_end();
2224 return EVP_PKEY_set_params(pkey, params);
2227 int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2230 OSSL_PARAM params[2];
2231 unsigned char buffer[2048];
2234 if (key_name == NULL
2237 || !evp_pkey_is_assigned(pkey))
2240 bsize = BN_num_bytes(bn);
2241 if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2244 if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2246 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2247 params[1] = OSSL_PARAM_construct_end();
2248 return EVP_PKEY_set_params(pkey, params);
2251 int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2254 OSSL_PARAM params[2];
2256 if (key_name == NULL)
2259 params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2260 params[1] = OSSL_PARAM_construct_end();
2261 return EVP_PKEY_set_params(pkey, params);
2264 int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2265 const unsigned char *buf, size_t bsize)
2267 OSSL_PARAM params[2];
2269 if (key_name == NULL)
2272 params[0] = OSSL_PARAM_construct_octet_string(key_name,
2273 (unsigned char *)buf, bsize);
2274 params[1] = OSSL_PARAM_construct_end();
2275 return EVP_PKEY_set_params(pkey, params);
2278 const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2280 return (pkey != NULL && evp_pkey_is_provided(pkey))
2281 ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2285 int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2288 if (evp_pkey_is_provided(pkey)) {
2290 return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2294 * We will hopefully never find the need to set individual data in
2295 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2296 * sure. This bit of code can be enabled if we find the need. If
2297 * not, it can safely be removed when #legacy support is removed.
2300 else if (evp_pkey_is_legacy(pkey)) {
2301 return evp_pkey_set_params_to_ctrl(pkey, params);
2306 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2310 const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2312 return (pkey != NULL && evp_pkey_is_provided(pkey))
2313 ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2317 int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2320 if (evp_pkey_is_provided(pkey))
2321 return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params);
2323 else if (evp_pkey_is_legacy(pkey))
2324 return evp_pkey_get_params_to_ctrl(pkey, params);
2327 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2332 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2340 if (pkey->keymgmt == NULL
2341 || pkey->keydata == NULL) {
2342 # ifndef OPENSSL_NO_EC
2343 /* Might work through the legacy route */
2344 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2349 return EC_KEY_get_conv_form(ec);
2355 if (!EVP_PKEY_get_utf8_string_param(pkey,
2356 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2357 name, sizeof(name), &name_len))
2360 if (strcmp(name, "uncompressed") == 0)
2361 return POINT_CONVERSION_UNCOMPRESSED;
2363 if (strcmp(name, "compressed") == 0)
2364 return POINT_CONVERSION_COMPRESSED;
2366 if (strcmp(name, "hybrid") == 0)
2367 return POINT_CONVERSION_HYBRID;
2372 int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2380 if (pkey->keymgmt == NULL
2381 || pkey->keydata == NULL) {
2382 # ifndef OPENSSL_NO_EC
2383 /* Might work through the legacy route */
2384 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2385 const EC_GROUP *grp;
2389 grp = EC_KEY_get0_group(ec);
2393 return EC_GROUP_get_field_type(grp);
2399 if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2400 fstr, sizeof(fstr), &fstrlen))
2403 if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2404 return NID_X9_62_prime_field;
2405 else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2406 return NID_X9_62_characteristic_two_field;