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"
17 #include "internal/cryptlib.h"
18 #include "internal/refcount.h"
19 #include "internal/namemap.h"
20 #include <openssl/bn.h>
21 #include <openssl/err.h>
22 #include <openssl/objects.h>
23 #include <openssl/evp.h>
24 #include <openssl/x509.h>
25 #include <openssl/rsa.h>
26 #include <openssl/dsa.h>
27 #include <openssl/dh.h>
28 #include <openssl/ec.h>
29 #include <openssl/cmac.h>
30 #include <openssl/engine.h>
31 #include <openssl/params.h>
32 #include <openssl/param_build.h>
33 #include <openssl/encoder.h>
34 #include <openssl/core_names.h>
36 #include "internal/ffc.h"
37 #include "crypto/asn1.h"
38 #include "crypto/evp.h"
39 #include "crypto/ec.h"
40 #include "crypto/ecx.h"
41 #include "internal/provider.h"
42 #include "evp_local.h"
44 #include "crypto/ec.h"
46 #include "e_os.h" /* strcasecmp on Windows */
48 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
49 int len, EVP_KEYMGMT *keymgmt);
50 static void evp_pkey_free_it(EVP_PKEY *key);
54 /* The type of parameters selected in key parameter functions */
55 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
57 int EVP_PKEY_bits(const EVP_PKEY *pkey)
62 size = pkey->cache.bits;
63 if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
64 size = pkey->ameth->pkey_bits(pkey);
66 return size < 0 ? 0 : size;
69 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
74 size = pkey->cache.security_bits;
75 if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
76 size = pkey->ameth->pkey_security_bits(pkey);
78 return size < 0 ? 0 : size;
81 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
83 # ifndef OPENSSL_NO_DSA
84 if (pkey->type == EVP_PKEY_DSA) {
85 int ret = pkey->save_parameters;
88 pkey->save_parameters = mode;
92 # ifndef OPENSSL_NO_EC
93 if (pkey->type == EVP_PKEY_EC) {
94 int ret = pkey->save_parameters;
97 pkey->save_parameters = mode;
104 int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
106 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
109 void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
111 return CRYPTO_get_ex_data(&key->ex_data, idx);
114 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
117 * Clean up legacy stuff from this function when legacy support is gone.
120 EVP_PKEY *downgraded_from = NULL;
124 * If |to| is a legacy key and |from| isn't, we must make a downgraded
125 * copy of |from|. If that fails, this function fails.
127 if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
128 if (!evp_pkey_copy_downgraded(&downgraded_from, from))
130 from = downgraded_from;
134 * Make sure |to| is typed. Content is less important at this early
137 * 1. If |to| is untyped, assign |from|'s key type to it.
138 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
139 * (|from| was already downgraded above)
141 * If |to| is a provided key, there's nothing more to do here, functions
142 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
143 * further down help us find out if they are the same or not.
145 if (evp_pkey_is_blank(to)) {
146 if (evp_pkey_is_legacy(from)) {
147 if (EVP_PKEY_set_type(to, from->type) == 0)
150 if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
153 } else if (evp_pkey_is_legacy(to)) {
154 if (to->type != from->type) {
155 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
160 if (EVP_PKEY_missing_parameters(from)) {
161 ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
165 if (!EVP_PKEY_missing_parameters(to)) {
166 if (EVP_PKEY_parameters_eq(to, from) == 1)
169 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
173 /* For purely provided keys, we just call the keymgmt utility */
174 if (to->keymgmt != NULL && from->keymgmt != NULL) {
175 ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
180 * If |to| is provided, we know that |from| is legacy at this point.
181 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
182 * to copy the appropriate data to |to|'s keydata.
184 if (to->keymgmt != NULL) {
185 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
187 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
191 * If we get a NULL, it could be an internal error, or it could be
192 * that there's a key mismatch. We're pretending the latter...
194 if (from_keydata == NULL)
195 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
197 ok = evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata,
202 /* Both keys are legacy */
203 if (from->ameth != NULL && from->ameth->param_copy != NULL)
204 ok = from->ameth->param_copy(to, from);
206 EVP_PKEY_free(downgraded_from);
210 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
213 if (pkey->keymgmt != NULL)
214 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
215 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
216 return pkey->ameth->param_missing(pkey);
222 * This function is called for any mixture of keys except pure legacy pair.
223 * TODO When legacy keys are gone, we replace a call to this functions with
224 * a call to evp_keymgmt_util_match().
226 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
229 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
230 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
232 /* If none of them are provided, this function shouldn't have been called */
233 if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
236 /* For purely provided keys, we just call the keymgmt utility */
237 if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
238 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
241 * At this point, one of them is provided, the other not. This allows
242 * us to compare types using legacy NIDs.
244 if (evp_pkey_is_legacy(a)
245 && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
246 return -1; /* not the same key type */
247 if (evp_pkey_is_legacy(b)
248 && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
249 return -1; /* not the same key type */
252 * We've determined that they both are the same keytype, so the next
253 * step is to do a bit of cross export to ensure we have keydata for
254 * both keys in the same keymgmt.
256 keymgmt1 = a->keymgmt;
257 keydata1 = a->keydata;
258 keymgmt2 = b->keymgmt;
259 keydata2 = b->keydata;
261 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
263 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
264 if (tmp_keydata != NULL) {
266 keydata1 = tmp_keydata;
269 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
271 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
272 if (tmp_keydata != NULL) {
274 keydata2 = tmp_keydata;
278 /* If we still don't have matching keymgmt implementations, we give up */
279 if (keymgmt1 != keymgmt2)
282 /* If the keymgmt implementations are NULL, the export failed */
283 if (keymgmt1 == NULL)
286 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
289 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
291 return EVP_PKEY_parameters_eq(a, b);
294 int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
297 * TODO: clean up legacy stuff from this function when legacy support
301 if (a->keymgmt != NULL || b->keymgmt != NULL)
302 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
304 /* All legacy keys */
305 if (a->type != b->type)
307 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
308 return a->ameth->param_cmp(a, b);
312 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
314 return EVP_PKEY_eq(a, b);
317 int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
320 * TODO: clean up legacy stuff from this function when legacy support
324 if (a->keymgmt != NULL || b->keymgmt != NULL)
325 return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
326 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY));
328 /* All legacy keys */
329 if (a->type != b->type)
332 if (a->ameth != NULL) {
334 /* Compare parameters if the algorithm has them */
335 if (a->ameth->param_cmp != NULL) {
336 ret = a->ameth->param_cmp(a, b);
341 if (a->ameth->pub_cmp != NULL)
342 return a->ameth->pub_cmp(a, b);
349 static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
354 const unsigned char *key,
358 EVP_PKEY *pkey = NULL;
359 EVP_PKEY_CTX *ctx = NULL;
360 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
363 # ifndef OPENSSL_NO_ENGINE
364 /* Check if there is an Engine for this type */
369 ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);
370 else if (nidtype != EVP_PKEY_NONE)
371 ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);
373 /* If tmpe is NULL then no engine is claiming to support this type */
381 if (e == NULL && ameth == NULL) {
383 * No engine is claiming to support this type, so lets see if we have
386 ctx = EVP_PKEY_CTX_new_from_name(libctx,
387 strtype != NULL ? strtype
388 : OBJ_nid2sn(nidtype),
392 /* May fail if no provider available */
394 if (EVP_PKEY_fromdata_init(ctx) == 1) {
395 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
397 ERR_clear_last_mark();
398 params[0] = OSSL_PARAM_construct_octet_string(
399 key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
400 : OSSL_PKEY_PARAM_PUB_KEY,
403 if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
404 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
408 EVP_PKEY_CTX_free(ctx);
413 /* else not supported so fallback to legacy */
416 /* Legacy code path */
418 pkey = EVP_PKEY_new();
420 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
424 if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) {
425 /* EVPerr already called */
429 if (!ossl_assert(pkey->ameth != NULL))
433 if (pkey->ameth->set_priv_key == NULL) {
434 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
438 if (!pkey->ameth->set_priv_key(pkey, key, len)) {
439 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
443 if (pkey->ameth->set_pub_key == NULL) {
444 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
448 if (!pkey->ameth->set_pub_key(pkey, key, len)) {
449 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
460 EVP_PKEY_CTX_free(ctx);
464 EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
467 const unsigned char *priv, size_t len)
469 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,
473 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
474 const unsigned char *priv,
477 return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);
480 EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
481 const char *keytype, const char *propq,
482 const unsigned char *pub, size_t len)
484 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,
488 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
489 const unsigned char *pub,
492 return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);
495 struct raw_key_details_st
502 static OSSL_CALLBACK get_raw_key_details;
503 static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
505 const OSSL_PARAM *p = NULL;
506 struct raw_key_details_st *raw_key = arg;
508 if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
509 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
511 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
512 SIZE_MAX, raw_key->len);
513 } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
514 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
516 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
517 SIZE_MAX, raw_key->len);
523 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
526 if (pkey->keymgmt != NULL) {
527 struct raw_key_details_st raw_key;
529 raw_key.key = priv == NULL ? NULL : &priv;
531 raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
533 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
534 get_raw_key_details, &raw_key);
537 if (pkey->ameth == NULL) {
538 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
542 if (pkey->ameth->get_priv_key == NULL) {
543 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
547 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
548 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
555 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
558 if (pkey->keymgmt != NULL) {
559 struct raw_key_details_st raw_key;
561 raw_key.key = pub == NULL ? NULL : &pub;
563 raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
565 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
566 get_raw_key_details, &raw_key);
569 if (pkey->ameth == NULL) {
570 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
574 if (pkey->ameth->get_pub_key == NULL) {
575 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
579 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
580 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
587 static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
588 const char *cipher_name,
589 const EVP_CIPHER *cipher,
590 OSSL_LIB_CTX *libctx,
591 const char *propq, ENGINE *e)
593 # ifndef OPENSSL_NO_CMAC
594 # ifndef OPENSSL_NO_ENGINE
595 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
597 OSSL_PARAM params[5], *p = params;
598 EVP_PKEY *pkey = NULL;
602 cipher_name = EVP_CIPHER_name(cipher);
604 if (cipher_name == NULL) {
605 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
609 ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
613 if (!EVP_PKEY_fromdata_init(ctx)) {
614 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
618 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
620 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
621 (char *)cipher_name, 0);
623 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
625 # ifndef OPENSSL_NO_ENGINE
626 if (engine_id != NULL)
627 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,
628 (char *)engine_id, 0);
630 *p = OSSL_PARAM_construct_end();
632 if (!EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params)) {
633 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
638 EVP_PKEY_CTX_free(ctx);
642 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
647 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
648 size_t len, const EVP_CIPHER *cipher)
650 return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
653 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
655 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
658 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
660 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
663 #ifndef OPENSSL_NO_DEPRECATED_3_0
664 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
666 if (!evp_pkey_is_legacy(pkey)) {
667 const char *name = OBJ_nid2sn(type);
669 if (name != NULL && EVP_PKEY_is_a(pkey, name))
672 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
676 if (pkey->type == type) {
677 return 1; /* it already is that type */
681 * The application is requesting to alias this to a different pkey type,
682 * but not one that resolves to the base type.
684 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
685 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
694 # ifndef OPENSSL_NO_ENGINE
695 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
698 if (!ENGINE_init(e)) {
699 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
702 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
704 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
708 ENGINE_finish(pkey->pmeth_engine);
709 pkey->pmeth_engine = e;
713 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
718 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
722 #ifndef OPENSSL_NO_EC
723 if ((key != NULL) && (EVP_PKEY_type(type) == EVP_PKEY_EC)) {
724 const EC_GROUP *group = EC_KEY_get0_group(key);
726 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
727 alias = EVP_PKEY_SM2;
731 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
733 if (!EVP_PKEY_set_alias_type(pkey, alias))
735 pkey->pkey.ptr = key;
736 return (key != NULL);
739 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
743 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
744 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
747 return pkey->pkey.ptr;
750 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
752 ASN1_OCTET_STRING *os = NULL;
753 if (pkey->type != EVP_PKEY_HMAC) {
754 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
757 os = EVP_PKEY_get0(pkey);
762 # ifndef OPENSSL_NO_POLY1305
763 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
765 ASN1_OCTET_STRING *os = NULL;
766 if (pkey->type != EVP_PKEY_POLY1305) {
767 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
770 os = EVP_PKEY_get0(pkey);
776 # ifndef OPENSSL_NO_SIPHASH
777 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
779 ASN1_OCTET_STRING *os = NULL;
781 if (pkey->type != EVP_PKEY_SIPHASH) {
782 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
785 os = EVP_PKEY_get0(pkey);
791 # ifndef OPENSSL_NO_DSA
792 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
794 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
795 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
798 if (pkey->type != EVP_PKEY_DSA) {
799 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
802 return pkey->pkey.dsa;
805 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
807 int ret = EVP_PKEY_assign_DSA(pkey, key);
812 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
814 DSA *ret = EVP_PKEY_get0_DSA(pkey);
819 # endif /* OPENSSL_NO_DSA */
820 #endif /* FIPS_MODULE */
823 # ifndef OPENSSL_NO_EC
824 static ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
826 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
827 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
830 if (EVP_PKEY_base_id(pkey) != type) {
831 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
834 return pkey->pkey.ecx;
837 static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
839 ECX_KEY *ret = evp_pkey_get0_ECX_KEY(pkey, type);
841 ossl_ecx_key_up_ref(ret);
845 # define IMPLEMENT_ECX_VARIANT(NAME) \
846 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
848 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
850 IMPLEMENT_ECX_VARIANT(X25519)
851 IMPLEMENT_ECX_VARIANT(X448)
852 IMPLEMENT_ECX_VARIANT(ED25519)
853 IMPLEMENT_ECX_VARIANT(ED448)
857 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
859 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
861 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
862 int ret = EVP_PKEY_assign(pkey, type, key);
869 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
871 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
872 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
875 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
876 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
879 return pkey->pkey.dh;
882 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
884 DH *ret = EVP_PKEY_get0_DH(pkey);
891 int EVP_PKEY_type(int type)
894 const EVP_PKEY_ASN1_METHOD *ameth;
896 ameth = EVP_PKEY_asn1_find(&e, type);
898 ret = ameth->pkey_id;
901 # ifndef OPENSSL_NO_ENGINE
907 int EVP_PKEY_id(const EVP_PKEY *pkey)
912 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
914 return EVP_PKEY_type(pkey->type);
919 * These hard coded cases are pure hackery to get around the fact
920 * that names in crypto/objects/objects.txt are a mess. There is
921 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
922 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
923 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
924 * "DSA" is accurate... but still, better be safe and hard-code
925 * names that we know.
926 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
927 * EVP_PKEY_EC, because of aliasing.
928 * TODO Clean this away along with all other #legacy support.
930 static const OSSL_ITEM standard_name2type[] = {
931 { EVP_PKEY_RSA, "RSA" },
932 { EVP_PKEY_RSA_PSS, "RSA-PSS" },
933 { EVP_PKEY_EC, "EC" },
934 { EVP_PKEY_ED25519, "ED25519" },
935 { EVP_PKEY_ED448, "ED448" },
936 { EVP_PKEY_X25519, "X25519" },
937 { EVP_PKEY_X448, "X448" },
938 { EVP_PKEY_SM2, "SM2" },
939 { EVP_PKEY_DH, "DH" },
940 { EVP_PKEY_DHX, "X9.42 DH" },
941 { EVP_PKEY_DHX, "DHX" },
942 { EVP_PKEY_DSA, "DSA" },
945 int evp_pkey_name2type(const char *name)
950 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
951 if (strcasecmp(name, standard_name2type[i].ptr) == 0)
952 return (int)standard_name2type[i].id;
955 if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
957 return EVP_PKEY_type(OBJ_ln2nid(name));
960 const char *evp_pkey_type2name(int type)
964 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
965 if (type == (int)standard_name2type[i].id)
966 return standard_name2type[i].ptr;
969 return OBJ_nid2sn(type);
973 int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
976 if (pkey->keymgmt == NULL) {
977 int type = evp_pkey_name2type(name);
979 return pkey->type == type;
982 return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
985 int EVP_PKEY_typenames_do_all(const EVP_PKEY *pkey,
986 void (*fn)(const char *name, void *data),
989 if (!evp_pkey_is_typed(pkey))
992 if (!evp_pkey_is_provided(pkey)) {
993 const char *name = OBJ_nid2sn(EVP_PKEY_id(pkey));
998 return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1001 int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1003 if (pkey->keymgmt == NULL) {
1004 switch (EVP_PKEY_base_id(pkey)) {
1007 #ifndef OPENSSL_NO_DSA
1011 #ifndef OPENSSL_NO_EC
1012 case EVP_PKEY_ED25519:
1013 case EVP_PKEY_ED448:
1015 case EVP_PKEY_EC: /* Including SM2 */
1016 return EC_KEY_can_sign(pkey->pkey.ec);
1022 const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt);
1023 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1024 const char *supported_sig =
1025 pkey->keymgmt->query_operation_name != NULL
1026 ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
1027 : evp_first_name(prov, pkey->keymgmt->name_id);
1028 EVP_SIGNATURE *signature = NULL;
1030 signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
1031 if (signature != NULL) {
1032 EVP_SIGNATURE_free(signature);
1039 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1041 BIO_set_indent(*out, saved_indent);
1043 BIO *next = BIO_pop(*out);
1051 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1057 long i = BIO_get_indent(*out);
1059 *saved_indent = (i < 0 ? 0 : i);
1060 if (BIO_set_indent(*out, indent) <= 0) {
1061 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
1065 if (BIO_set_indent(*out, indent) <= 0) {
1066 print_reset_indent(out, *pop_f_prefix, *saved_indent);
1073 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1076 return BIO_indent(out, indent, 128)
1077 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1078 kstr, OBJ_nid2ln(pkey->type)) > 0;
1081 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1082 int selection /* For provided encoding */,
1083 const char *propquery /* For provided encoding */,
1084 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1085 int indent, ASN1_PCTX *pctx),
1086 ASN1_PCTX *legacy_pctx /* For legacy print */)
1090 OSSL_ENCODER_CTX *ctx = NULL;
1091 int ret = -2; /* default to unsupported */
1093 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1096 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1098 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1099 ret = OSSL_ENCODER_to_bio(ctx, out);
1100 OSSL_ENCODER_CTX_free(ctx);
1105 /* legacy fallback */
1106 if (legacy_print != NULL)
1107 ret = legacy_print(out, pkey, 0, legacy_pctx);
1109 ret = unsup_alg(out, pkey, 0, "Public Key");
1112 print_reset_indent(&out, pop_f_prefix, saved_indent);
1116 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1117 int indent, ASN1_PCTX *pctx)
1119 return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1120 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1124 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1125 int indent, ASN1_PCTX *pctx)
1127 return print_pkey(pkey, out, indent, EVP_PKEY_KEYPAIR, NULL,
1128 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1132 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1133 int indent, ASN1_PCTX *pctx)
1135 return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1136 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1140 static void mdname2nid(const char *mdname, void *data)
1142 int *nid = (int *)data;
1144 if (*nid != NID_undef)
1147 *nid = OBJ_sn2nid(mdname);
1148 if (*nid == NID_undef)
1149 *nid = OBJ_ln2nid(mdname);
1152 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1153 int arg1, void *arg2)
1155 if (pkey->keymgmt == NULL)
1158 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1160 char mdname[80] = "";
1161 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1166 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1167 /* Make sure the MD is in the namemap if available */
1168 EVP_MD *md = EVP_MD_fetch(libctx, mdname, NULL);
1169 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
1170 int nid = NID_undef;
1173 * The only reason to fetch the MD was to make sure it is in the
1174 * namemap. We can immediately free it.
1177 mdnum = ossl_namemap_name2num(namemap, mdname);
1182 * We have the namemap number - now we need to find the
1185 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1196 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1198 if (pkey->ameth == NULL)
1199 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1200 if (pkey->ameth->pkey_ctrl == NULL)
1202 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1205 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1207 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1210 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1211 char *mdname, size_t mdname_sz)
1213 if (pkey->ameth == NULL)
1214 return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1219 int nid = NID_undef;
1220 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1221 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1224 OPENSSL_strlcpy(mdname, name, mdname_sz);
1229 int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1232 return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1233 gname, gname_sz, gname_len);
1236 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
1238 int rv, default_nid;
1240 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
1243 * If there is a mandatory default digest and this isn't it, then
1244 * the answer is 'no'.
1246 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
1248 return (nid == default_nid);
1249 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
1256 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1259 if (pkey != NULL && evp_pkey_is_provided(pkey))
1261 EVP_PKEY_set_octet_string_param(pkey,
1262 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1263 (unsigned char *)pub, publen);
1265 if (publen > INT_MAX)
1267 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1268 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
1274 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1278 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
1279 size_t return_size = OSSL_PARAM_UNMODIFIED;
1282 * We know that this is going to fail, but it will give us a size
1285 EVP_PKEY_get_octet_string_param(pkey,
1286 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1287 NULL, 0, &return_size);
1288 if (return_size == OSSL_PARAM_UNMODIFIED)
1291 *ppub = OPENSSL_malloc(return_size);
1295 if (!EVP_PKEY_get_octet_string_param(pkey,
1296 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1297 *ppub, return_size, NULL))
1303 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1309 #endif /* FIPS_MODULE */
1311 /*- All methods below can also be used in FIPS_MODULE */
1314 * This reset function must be used very carefully, as it literally throws
1315 * away everything in an EVP_PKEY without freeing them, and may cause leaks
1316 * of memory, what have you.
1317 * The only reason we have this is to have the same code for EVP_PKEY_new()
1318 * and evp_pkey_downgrade().
1320 static int evp_pkey_reset_unlocked(EVP_PKEY *pk)
1325 if (pk->lock != NULL) {
1326 const size_t offset = (unsigned char *)&pk->lock - (unsigned char *)pk;
1328 memset(pk, 0, offset);
1329 memset((unsigned char *)pk + offset + sizeof(pk->lock),
1331 sizeof(*pk) - offset - sizeof(pk->lock));
1333 /* EVP_PKEY_new uses zalloc so no need to call memset if pk->lock is NULL */
1335 pk->type = EVP_PKEY_NONE;
1336 pk->save_type = EVP_PKEY_NONE;
1338 pk->save_parameters = 1;
1343 EVP_PKEY *EVP_PKEY_new(void)
1345 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1348 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1352 if (!evp_pkey_reset_unlocked(ret))
1355 ret->lock = CRYPTO_THREAD_lock_new();
1356 if (ret->lock == NULL) {
1357 EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1362 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1363 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1370 CRYPTO_THREAD_lock_free(ret->lock);
1376 * Setup a public key management method.
1378 * For legacy keys, either |type| or |str| is expected to have the type
1379 * information. In this case, the setup consists of finding an ASN1 method
1380 * and potentially an ENGINE, and setting those fields in |pkey|.
1382 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1383 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1385 * If pkey is NULL just return 1 or 0 if the key management method exists.
1388 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1389 int len, EVP_KEYMGMT *keymgmt)
1392 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1393 ENGINE **eptr = (e == NULL) ? &e : NULL;
1397 * The setups can't set both legacy and provider side methods.
1400 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1401 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1402 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1410 free_it = free_it || pkey->pkey.ptr != NULL;
1412 free_it = free_it || pkey->keydata != NULL;
1414 evp_pkey_free_it(pkey);
1417 * If key type matches and a method exists then this lookup has
1418 * succeeded once so just indicate success.
1420 if (pkey->type != EVP_PKEY_NONE
1421 && type == pkey->save_type
1422 && pkey->ameth != NULL)
1424 # ifndef OPENSSL_NO_ENGINE
1425 /* If we have ENGINEs release them */
1426 ENGINE_finish(pkey->engine);
1427 pkey->engine = NULL;
1428 ENGINE_finish(pkey->pmeth_engine);
1429 pkey->pmeth_engine = NULL;
1435 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1436 else if (type != EVP_PKEY_NONE)
1437 ameth = EVP_PKEY_asn1_find(eptr, type);
1438 # ifndef OPENSSL_NO_ENGINE
1439 if (pkey == NULL && eptr != NULL)
1449 check = check && ameth == NULL;
1451 check = check && keymgmt == NULL;
1453 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1458 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1459 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1463 pkey->keymgmt = keymgmt;
1465 pkey->save_type = type;
1470 * If the internal "origin" key is provider side, don't save |ameth|.
1471 * The main reason is that |ameth| is one factor to detect that the
1472 * internal "origin" key is a legacy one.
1474 if (keymgmt == NULL)
1475 pkey->ameth = ameth;
1479 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1480 * for any key type that has a legacy implementation, regardless of
1481 * if the internal key is a legacy or a provider side one. When
1482 * there is no legacy implementation for the key, the type becomes
1483 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1484 * with functions that expect legacy internal keys.
1487 pkey->type = ameth->pkey_id;
1489 pkey->type = EVP_PKEY_KEYMGMT;
1496 static void find_ameth(const char *name, void *data)
1498 const char **str = data;
1501 * The error messages from pkey_set_type() are uninteresting here,
1506 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1510 else if (str[1] == NULL)
1518 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1521 # define EVP_PKEY_TYPE_STR str[0]
1522 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1524 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1525 * Ideally, only one should be found. If two (or more) are found, the
1526 * match is ambiguous. This should never happen, but...
1528 const char *str[2] = { NULL, NULL };
1530 if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1531 || str[1] != NULL) {
1532 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1536 # define EVP_PKEY_TYPE_STR NULL
1537 # define EVP_PKEY_TYPE_STRLEN -1
1539 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1540 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1543 #undef EVP_PKEY_TYPE_STR
1544 #undef EVP_PKEY_TYPE_STRLEN
1547 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1551 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1554 REF_PRINT_COUNT("EVP_PKEY", pkey);
1555 REF_ASSERT_ISNT(i < 2);
1556 return ((i > 1) ? 1 : 0);
1560 void evp_pkey_free_legacy(EVP_PKEY *x)
1562 if (x->ameth != NULL) {
1563 if (x->ameth->pkey_free != NULL)
1564 x->ameth->pkey_free(x);
1567 # ifndef OPENSSL_NO_ENGINE
1568 ENGINE_finish(x->engine);
1570 ENGINE_finish(x->pmeth_engine);
1571 x->pmeth_engine = NULL;
1574 #endif /* FIPS_MODULE */
1576 static void evp_pkey_free_it(EVP_PKEY *x)
1578 /* internal function; x is never NULL */
1579 evp_keymgmt_util_clear_operation_cache(x, 1);
1581 evp_pkey_free_legacy(x);
1584 if (x->keymgmt != NULL) {
1585 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1586 EVP_KEYMGMT_free(x->keymgmt);
1590 x->type = EVP_PKEY_NONE;
1593 void EVP_PKEY_free(EVP_PKEY *x)
1600 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1601 REF_PRINT_COUNT("EVP_PKEY", x);
1604 REF_ASSERT_ISNT(i < 0);
1605 evp_pkey_free_it(x);
1607 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1609 CRYPTO_THREAD_lock_free(x->lock);
1611 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1616 int EVP_PKEY_size(const EVP_PKEY *pkey)
1621 size = pkey->cache.size;
1623 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1624 size = pkey->ameth->pkey_size(pkey);
1627 return size < 0 ? 0 : size;
1630 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1631 EVP_KEYMGMT **keymgmt,
1632 const char *propquery)
1634 EVP_KEYMGMT *allocated_keymgmt = NULL;
1635 EVP_KEYMGMT *tmp_keymgmt = NULL;
1636 void *keydata = NULL;
1642 /* No key data => nothing to export */
1645 check = check && pk->pkey.ptr == NULL;
1647 check = check && pk->keydata == NULL;
1652 if (pk->pkey.ptr != NULL) {
1654 * If the legacy key doesn't have an dirty counter or export function,
1657 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1662 if (keymgmt != NULL) {
1663 tmp_keymgmt = *keymgmt;
1668 * If no keymgmt was given or found, get a default keymgmt. We do so by
1669 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1671 if (tmp_keymgmt == NULL) {
1672 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1674 tmp_keymgmt = ctx->keymgmt;
1675 ctx->keymgmt = NULL;
1676 EVP_PKEY_CTX_free(ctx);
1679 /* If there's still no keymgmt to be had, give up */
1680 if (tmp_keymgmt == NULL)
1684 if (pk->pkey.ptr != NULL) {
1688 * If the legacy "origin" hasn't changed since last time, we try
1689 * to find our keymgmt in the operation cache. If it has changed,
1690 * |i| remains zero, and we will clear the cache further down.
1692 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1693 if (!CRYPTO_THREAD_read_lock(pk->lock))
1695 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1698 * If |tmp_keymgmt| is present in the operation cache, it means
1699 * that export doesn't need to be redone. In that case, we take
1700 * token copies of the cached pointers, to have token success
1703 if (op != NULL && op->keymgmt != NULL) {
1704 keydata = op->keydata;
1705 CRYPTO_THREAD_unlock(pk->lock);
1708 CRYPTO_THREAD_unlock(pk->lock);
1711 /* Make sure that the keymgmt key type matches the legacy NID */
1712 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1715 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1718 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt, libctx, propquery)) {
1719 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1725 * If the dirty counter changed since last time, then clear the
1726 * operation cache. In that case, we know that |i| is zero. Just
1727 * in case this is a re-export, we increment then decrement the
1728 * keymgmt reference counter.
1730 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1731 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1736 if (!CRYPTO_THREAD_write_lock(pk->lock))
1738 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
1739 && !evp_keymgmt_util_clear_operation_cache(pk, 0)) {
1740 CRYPTO_THREAD_unlock(pk->lock);
1741 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1743 EVP_KEYMGMT_free(tmp_keymgmt);
1746 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1748 /* Check to make sure some other thread didn't get there first */
1749 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1750 if (op != NULL && op->keymgmt != NULL) {
1751 void *tmp_keydata = op->keydata;
1753 CRYPTO_THREAD_unlock(pk->lock);
1754 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1755 keydata = tmp_keydata;
1759 /* Add the new export to the operation cache */
1760 if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata)) {
1761 CRYPTO_THREAD_unlock(pk->lock);
1762 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1767 /* Synchronize the dirty count */
1768 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1770 CRYPTO_THREAD_unlock(pk->lock);
1773 #endif /* FIPS_MODULE */
1775 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1779 * If nothing was exported, |tmp_keymgmt| might point at a freed
1780 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1781 * the caller either way in that case.
1783 if (keydata == NULL)
1786 if (keymgmt != NULL)
1787 *keymgmt = tmp_keymgmt;
1789 EVP_KEYMGMT_free(allocated_keymgmt);
1794 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
1796 if (!ossl_assert(dest != NULL))
1799 if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
1800 EVP_KEYMGMT *keymgmt = src->keymgmt;
1801 void *keydata = src->keydata;
1802 int type = src->type;
1803 const char *keytype = NULL;
1805 keytype = evp_first_name(EVP_KEYMGMT_provider(keymgmt),
1809 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1810 * else in our code. If it's not one of the well known EVP_PKEY_xxx
1811 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1812 * TODO(3.0) remove this check when we're confident that the rest
1813 * of the code treats this correctly.
1815 if (!ossl_assert(type != EVP_PKEY_NONE)) {
1816 ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
1817 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1822 /* Prefer the legacy key type name for error reporting */
1823 if (type != EVP_PKEY_KEYMGMT)
1824 keytype = OBJ_nid2sn(type);
1826 /* Make sure we have a clean slate to copy into */
1828 *dest = EVP_PKEY_new();
1830 evp_pkey_free_it(*dest);
1832 if (EVP_PKEY_set_type(*dest, type)) {
1833 /* If the key is typed but empty, we're done */
1834 if (keydata == NULL)
1837 if ((*dest)->ameth->import_from == NULL) {
1838 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
1839 "key type = %s", keytype);
1842 * We perform the export in the same libctx as the keymgmt
1843 * that we are using.
1845 OSSL_LIB_CTX *libctx =
1846 ossl_provider_libctx(keymgmt->prov);
1847 EVP_PKEY_CTX *pctx =
1848 EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
1851 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1854 && evp_keymgmt_export(keymgmt, keydata,
1855 OSSL_KEYMGMT_SELECT_ALL,
1856 (*dest)->ameth->import_from,
1858 /* Synchronize the dirty count */
1859 (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
1861 EVP_PKEY_CTX_free(pctx);
1864 EVP_PKEY_CTX_free(pctx);
1867 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
1868 "key type = %s", keytype);
1875 int evp_pkey_downgrade(EVP_PKEY *pk)
1877 EVP_PKEY tmp_copy; /* Stack allocated! */
1880 if (!ossl_assert(pk != NULL))
1884 * Throughout this whole function, we must ensure that we lock / unlock
1885 * the exact same lock. Note that we do pass it around a bit.
1887 if (!CRYPTO_THREAD_write_lock(pk->lock))
1890 /* If this isn't an assigned provider side key, we're done */
1891 if (!evp_pkey_is_assigned(pk) || !evp_pkey_is_provided(pk)) {
1897 * To be able to downgrade, we steal the contents of |pk|, then reset
1898 * it, and finally try to make it a downgraded copy. If any of that
1899 * fails, we restore the copied contents into |pk|.
1901 tmp_copy = *pk; /* |tmp_copy| now owns THE lock */
1903 if (evp_pkey_reset_unlocked(pk)
1904 && evp_pkey_copy_downgraded(&pk, &tmp_copy)) {
1906 /* Restore the common attributes, then empty |tmp_copy| */
1907 pk->references = tmp_copy.references;
1908 pk->attributes = tmp_copy.attributes;
1909 pk->save_parameters = tmp_copy.save_parameters;
1910 pk->ex_data = tmp_copy.ex_data;
1912 /* Ensure that stuff we've copied won't be freed */
1913 tmp_copy.lock = NULL;
1914 tmp_copy.attributes = NULL;
1915 memset(&tmp_copy.ex_data, 0, sizeof(tmp_copy.ex_data));
1918 * Save the provider side data in the operation cache, so they'll
1919 * find it again. |pk| is new, so it's safe to assume slot zero
1921 * Note that evp_keymgmt_util_cache_keydata() increments keymgmt's
1922 * reference count, so we need to decrement it, or there will be a
1925 evp_keymgmt_util_cache_keydata(pk, tmp_copy.keymgmt,
1927 EVP_KEYMGMT_free(tmp_copy.keymgmt);
1930 * Clear keymgmt and keydata from |tmp_copy|, or they'll get
1931 * inadvertently freed.
1933 tmp_copy.keymgmt = NULL;
1934 tmp_copy.keydata = NULL;
1936 evp_pkey_free_it(&tmp_copy);
1939 /* Restore the original key */
1944 if (!CRYPTO_THREAD_unlock(pk->lock))
1948 #endif /* FIPS_MODULE */
1950 int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
1954 OSSL_PARAM params[2];
1955 unsigned char buffer[2048];
1956 unsigned char *buf = NULL;
1959 if (key_name == NULL
1962 || !evp_pkey_is_provided(pkey))
1965 memset(buffer, 0, sizeof(buffer));
1966 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
1967 params[1] = OSSL_PARAM_construct_end();
1968 if (!EVP_PKEY_get_params(pkey, params)) {
1969 if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
1971 buf_sz = params[0].return_size;
1973 * If it failed because the buffer was too small then allocate the
1974 * required buffer size and retry.
1976 buf = OPENSSL_zalloc(buf_sz);
1979 params[0].data = buf;
1980 params[0].data_size = buf_sz;
1982 if (!EVP_PKEY_get_params(pkey, params))
1985 /* Fail if the param was not found */
1986 if (!OSSL_PARAM_modified(params))
1988 ret = OSSL_PARAM_get_BN(params, bn);
1994 int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
1995 unsigned char *buf, size_t max_buf_sz,
1998 OSSL_PARAM params[2];
1999 int ret1 = 0, ret2 = 0;
2001 if (key_name == NULL
2003 || !evp_pkey_is_provided(pkey))
2006 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2007 params[1] = OSSL_PARAM_construct_end();
2008 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2009 ret2 = OSSL_PARAM_modified(params);
2010 if (ret2 && out_sz != NULL)
2011 *out_sz = params[0].return_size;
2012 return ret1 && ret2;
2015 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2016 char *str, size_t max_buf_sz,
2019 OSSL_PARAM params[2];
2020 int ret1 = 0, ret2 = 0;
2022 if (key_name == NULL)
2025 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2026 params[1] = OSSL_PARAM_construct_end();
2027 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2028 ret2 = OSSL_PARAM_modified(params);
2029 if (ret2 && out_sz != NULL)
2030 *out_sz = params[0].return_size;
2031 return ret1 && ret2;
2034 int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2037 OSSL_PARAM params[2];
2039 if (key_name == NULL)
2042 params[0] = OSSL_PARAM_construct_int(key_name, out);
2043 params[1] = OSSL_PARAM_construct_end();
2044 return EVP_PKEY_get_params(pkey, params)
2045 && OSSL_PARAM_modified(params);
2048 int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2051 OSSL_PARAM params[2];
2053 if (key_name == NULL)
2056 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2057 params[1] = OSSL_PARAM_construct_end();
2058 return EVP_PKEY_get_params(pkey, params)
2059 && OSSL_PARAM_modified(params);
2062 int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2064 OSSL_PARAM params[2];
2066 if (key_name == NULL)
2069 params[0] = OSSL_PARAM_construct_int(key_name, &in);
2070 params[1] = OSSL_PARAM_construct_end();
2071 return EVP_PKEY_set_params(pkey, params);
2074 int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2076 OSSL_PARAM params[2];
2078 if (key_name == NULL)
2081 params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2082 params[1] = OSSL_PARAM_construct_end();
2083 return EVP_PKEY_set_params(pkey, params);
2086 int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2089 OSSL_PARAM params[2];
2090 unsigned char buffer[2048];
2093 if (key_name == NULL
2096 || !evp_pkey_is_assigned(pkey))
2099 bsize = BN_num_bytes(bn);
2100 if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2103 if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2105 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2106 params[1] = OSSL_PARAM_construct_end();
2107 return EVP_PKEY_set_params(pkey, params);
2110 int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2113 OSSL_PARAM params[2];
2115 if (key_name == NULL)
2118 params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2119 params[1] = OSSL_PARAM_construct_end();
2120 return EVP_PKEY_set_params(pkey, params);
2123 int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2124 const unsigned char *buf, size_t bsize)
2126 OSSL_PARAM params[2];
2128 if (key_name == NULL)
2131 params[0] = OSSL_PARAM_construct_octet_string(key_name,
2132 (unsigned char *)buf, bsize);
2133 params[1] = OSSL_PARAM_construct_end();
2134 return EVP_PKEY_set_params(pkey, params);
2137 const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2139 return (pkey != NULL && evp_pkey_is_provided(pkey))
2140 ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2144 int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2147 if (evp_pkey_is_provided(pkey)) {
2149 return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2154 * We will hopefully never find the need to set individual data in
2155 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2156 * sure. This bit of code can be enabled if we find the need. If
2157 * not, it can safely be removed when #legacy support is removed.
2160 else if (evp_pkey_is_legacy(pkey)) {
2161 return evp_pkey_set_params_to_ctrl(pkey, params);
2166 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2170 const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2172 return (pkey != NULL && evp_pkey_is_provided(pkey))
2173 ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2177 int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2180 if (evp_pkey_is_provided(pkey))
2181 return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params);
2183 else if (evp_pkey_is_legacy(pkey))
2184 return evp_pkey_get_params_to_ctrl(pkey, params);
2187 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2192 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2200 if (pkey->keymgmt == NULL
2201 || pkey->keydata == NULL) {
2202 #ifndef OPENSSL_NO_EC
2203 /* Might work through the legacy route */
2204 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2209 return EC_KEY_get_conv_form(ec);
2215 if (!EVP_PKEY_get_utf8_string_param(pkey,
2216 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2217 name, sizeof(name), &name_len))
2220 if (strcmp(name, "uncompressed") == 0)
2221 return POINT_CONVERSION_UNCOMPRESSED;
2223 if (strcmp(name, "compressed") == 0)
2224 return POINT_CONVERSION_COMPRESSED;
2226 if (strcmp(name, "hybrid") == 0)
2227 return POINT_CONVERSION_HYBRID;
2232 int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2240 if (pkey->keymgmt == NULL
2241 || pkey->keydata == NULL) {
2242 #ifndef OPENSSL_NO_EC
2243 /* Might work through the legacy route */
2244 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2245 const EC_GROUP *grp;
2249 grp = EC_KEY_get0_group(ec);
2253 return EC_GROUP_get_field_type(grp);
2259 if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2260 fstr, sizeof(fstr), &fstrlen))
2263 if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2264 return NID_X9_62_prime_field;
2265 else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2266 return NID_X9_62_characteristic_two_field;