2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include "internal/refcount.h"
19 #include <openssl/bn.h>
20 #include <openssl/err.h>
21 #include <openssl/objects.h>
22 #include <openssl/evp.h>
23 #include <openssl/x509.h>
24 #include <openssl/rsa.h>
25 #include <openssl/dsa.h>
26 #include <openssl/dh.h>
27 #include <openssl/cmac.h>
28 #include <openssl/engine.h>
29 #include <openssl/params.h>
30 #include <openssl/serializer.h>
31 #include <openssl/core_names.h>
33 #include "crypto/asn1.h"
34 #include "crypto/evp.h"
35 #include "internal/provider.h"
36 #include "evp_local.h"
38 static void evp_pkey_free_it(EVP_PKEY *key);
42 int EVP_PKEY_bits(const EVP_PKEY *pkey)
45 if (pkey->ameth == NULL)
46 return pkey->cache.bits;
47 else if (pkey->ameth->pkey_bits)
48 return pkey->ameth->pkey_bits(pkey);
53 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
57 if (pkey->ameth == NULL)
58 return pkey->cache.security_bits;
59 if (pkey->ameth->pkey_security_bits == NULL)
61 return pkey->ameth->pkey_security_bits(pkey);
64 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
66 # ifndef OPENSSL_NO_DSA
67 if (pkey->type == EVP_PKEY_DSA) {
68 int ret = pkey->save_parameters;
71 pkey->save_parameters = mode;
75 # ifndef OPENSSL_NO_EC
76 if (pkey->type == EVP_PKEY_EC) {
77 int ret = pkey->save_parameters;
80 pkey->save_parameters = mode;
87 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
90 * TODO: clean up legacy stuff from this function when legacy support
95 * Only check that type match this early when both keys are legacy.
96 * If either of them is provided, we let evp_keymgmt_util_copy()
97 * do this check, after having exported either of them that isn't
100 if (to->keymgmt == NULL && from->keymgmt == NULL) {
101 if (to->type == EVP_PKEY_NONE) {
102 if (EVP_PKEY_set_type(to, from->type) == 0)
104 } else if (to->type != from->type) {
105 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
110 if (EVP_PKEY_missing_parameters(from)) {
111 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
115 if (!EVP_PKEY_missing_parameters(to)) {
116 if (EVP_PKEY_cmp_parameters(to, from) == 1)
118 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
123 * If |from| is provided, we upgrade |to| to be provided as well.
124 * This drops the legacy key from |to|.
125 * evp_pkey_upgrade_to_provider() checks if |to| is already provided,
126 * we don't need to do that here.
128 * TODO(3.0) We should investigate if that's too aggressive and make
129 * this scenario unsupported instead.
131 if (from->keymgmt != NULL) {
132 EVP_KEYMGMT *tmp_keymgmt = from->keymgmt;
135 * The returned pointer is known to be cached, so we don't have to
136 * save it. However, if it's NULL, something went wrong and we can't
139 if (evp_pkey_upgrade_to_provider(to, NULL,
140 &tmp_keymgmt, NULL) == NULL) {
141 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
146 /* For purely provided keys, we just call the keymgmt utility */
147 if (to->keymgmt != NULL && from->keymgmt != NULL)
148 return evp_keymgmt_util_copy(to, (EVP_PKEY *)from,
149 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
152 * If |to| is provided, we know that |from| is legacy at this point.
153 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
154 * to copy the appropriate data to |to|'s keydata.
156 if (to->keymgmt != NULL) {
157 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
159 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
162 if (from_keydata == NULL) {
163 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
166 return evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata,
167 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
170 /* Both keys are legacy */
171 if (from->ameth != NULL && from->ameth->param_copy != NULL)
172 return from->ameth->param_copy(to, from);
177 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
180 if (pkey->keymgmt != NULL)
181 return !evp_keymgmt_util_has((EVP_PKEY *)pkey,
182 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
183 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
184 return pkey->ameth->param_missing(pkey);
190 * This function is called for any mixture of keys except pure legacy pair.
191 * TODO When legacy keys are gone, we replace a call to this functions with
192 * a call to evp_keymgmt_util_match().
194 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
197 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
198 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
200 /* If none of them are provided, this function shouldn't have been called */
201 if (!ossl_assert(a->keymgmt != NULL || b->keymgmt != NULL))
204 /* For purely provided keys, we just call the keymgmt utility */
205 if (a->keymgmt != NULL && b->keymgmt != NULL)
206 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
209 * Here, we know that we have a mixture of legacy and provided keys.
210 * Try cross export and compare the resulting key data.
212 keymgmt1 = a->keymgmt;
213 keydata1 = a->keydata;
214 keymgmt2 = b->keymgmt;
215 keydata2 = b->keydata;
217 if ((keymgmt1 == NULL
218 && !EVP_KEYMGMT_is_a(keymgmt2, OBJ_nid2sn(a->type)))
220 && !EVP_KEYMGMT_is_a(keymgmt1, OBJ_nid2sn(b->type))))
221 return -1; /* not the same key type */
223 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
225 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
226 if (tmp_keydata != NULL) {
228 keydata1 = tmp_keydata;
231 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
233 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
234 if (tmp_keydata != NULL) {
236 keydata2 = tmp_keydata;
240 /* If we still don't have matching keymgmt implementations, we give up */
241 if (keymgmt1 != keymgmt2)
244 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
247 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
250 * TODO: clean up legacy stuff from this function when legacy support
254 if (a->keymgmt != NULL || b->keymgmt != NULL)
255 return evp_pkey_cmp_any(a, b, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
257 /* All legacy keys */
258 if (a->type != b->type)
260 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
261 return a->ameth->param_cmp(a, b);
265 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
268 * TODO: clean up legacy stuff from this function when legacy support
272 if (a->keymgmt != NULL || b->keymgmt != NULL)
273 return evp_pkey_cmp_any(a, b,
274 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
275 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
277 /* All legacy keys */
278 if (a->type != b->type)
281 if (a->ameth != NULL) {
283 /* Compare parameters if the algorithm has them */
284 if (a->ameth->param_cmp != NULL) {
285 ret = a->ameth->param_cmp(a, b);
290 if (a->ameth->pub_cmp != NULL)
291 return a->ameth->pub_cmp(a, b);
299 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
300 * is NULL just return 1 or 0 if the algorithm exists.
303 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
306 const EVP_PKEY_ASN1_METHOD *ameth;
307 ENGINE **eptr = (e == NULL) ? &e : NULL;
311 evp_pkey_free_it(pkey);
313 * If key type matches and a method exists then this lookup has
314 * succeeded once so just indicate success.
316 if ((type == pkey->save_type) && pkey->ameth)
318 # ifndef OPENSSL_NO_ENGINE
319 /* If we have ENGINEs release them */
320 ENGINE_finish(pkey->engine);
322 ENGINE_finish(pkey->pmeth_engine);
323 pkey->pmeth_engine = NULL;
327 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
329 ameth = EVP_PKEY_asn1_find(eptr, type);
330 # ifndef OPENSSL_NO_ENGINE
331 if (pkey == NULL && eptr != NULL)
335 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
342 pkey->type = pkey->ameth->pkey_id;
343 pkey->save_type = type;
348 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
349 const unsigned char *priv,
352 EVP_PKEY *ret = EVP_PKEY_new();
355 || !pkey_set_type(ret, e, type, NULL, -1)) {
356 /* EVPerr already called */
360 if (ret->ameth->set_priv_key == NULL) {
361 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
362 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
366 if (!ret->ameth->set_priv_key(ret, priv, len)) {
367 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
378 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
379 const unsigned char *pub,
382 EVP_PKEY *ret = EVP_PKEY_new();
385 || !pkey_set_type(ret, e, type, NULL, -1)) {
386 /* EVPerr already called */
390 if (ret->ameth->set_pub_key == NULL) {
391 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
392 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
396 if (!ret->ameth->set_pub_key(ret, pub, len)) {
397 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
408 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
411 if (pkey->ameth->get_priv_key == NULL) {
412 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
413 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
417 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
418 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
425 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
428 if (pkey->ameth->get_pub_key == NULL) {
429 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
430 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
434 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
435 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
442 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
443 size_t len, const EVP_CIPHER *cipher)
445 # ifndef OPENSSL_NO_CMAC
446 # ifndef OPENSSL_NO_ENGINE
447 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
449 const char *cipher_name = EVP_CIPHER_name(cipher);
450 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
451 OPENSSL_CTX *libctx =
452 prov == NULL ? NULL : ossl_provider_library_context(prov);
453 EVP_PKEY *ret = EVP_PKEY_new();
454 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
455 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
456 OSSL_PARAM params[4];
461 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
462 /* EVPerr already called */
466 # ifndef OPENSSL_NO_ENGINE
467 if (engine_id != NULL)
469 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
473 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
474 (char *)cipher_name, 0);
476 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
478 params[paramsn] = OSSL_PARAM_construct_end();
480 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
481 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
485 ret->pkey.ptr = cmctx;
490 EVP_MAC_CTX_free(cmctx);
494 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
495 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
500 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
502 return pkey_set_type(pkey, NULL, type, NULL, -1);
505 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
507 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
510 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
512 if (pkey->type == type) {
513 return 1; /* it already is that type */
517 * The application is requesting to alias this to a different pkey type,
518 * but not one that resolves to the base type.
520 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
521 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
529 # ifndef OPENSSL_NO_ENGINE
530 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
533 if (!ENGINE_init(e)) {
534 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
537 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
539 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
543 ENGINE_finish(pkey->pmeth_engine);
544 pkey->pmeth_engine = e;
548 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
553 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
557 #ifndef OPENSSL_NO_EC
558 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
559 const EC_GROUP *group = EC_KEY_get0_group(key);
561 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
562 alias = EVP_PKEY_SM2;
566 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
568 if (!EVP_PKEY_set_alias_type(pkey, alias))
570 pkey->pkey.ptr = key;
571 return (key != NULL);
574 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
576 return pkey->pkey.ptr;
579 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
581 ASN1_OCTET_STRING *os = NULL;
582 if (pkey->type != EVP_PKEY_HMAC) {
583 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
586 os = EVP_PKEY_get0(pkey);
591 # ifndef OPENSSL_NO_POLY1305
592 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
594 ASN1_OCTET_STRING *os = NULL;
595 if (pkey->type != EVP_PKEY_POLY1305) {
596 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
599 os = EVP_PKEY_get0(pkey);
605 # ifndef OPENSSL_NO_SIPHASH
606 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
608 ASN1_OCTET_STRING *os = NULL;
610 if (pkey->type != EVP_PKEY_SIPHASH) {
611 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
614 os = EVP_PKEY_get0(pkey);
620 # ifndef OPENSSL_NO_RSA
621 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
623 int ret = EVP_PKEY_assign_RSA(pkey, key);
629 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
631 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
632 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
635 return pkey->pkey.rsa;
638 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
640 RSA *ret = EVP_PKEY_get0_RSA(pkey);
647 # ifndef OPENSSL_NO_DSA
648 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
650 int ret = EVP_PKEY_assign_DSA(pkey, key);
656 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
658 if (pkey->type != EVP_PKEY_DSA) {
659 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
662 return pkey->pkey.dsa;
665 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
667 DSA *ret = EVP_PKEY_get0_DSA(pkey);
674 # ifndef OPENSSL_NO_EC
676 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
678 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
684 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
686 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
687 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
690 return pkey->pkey.ec;
693 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
695 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
702 # ifndef OPENSSL_NO_DH
704 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
706 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
707 int ret = EVP_PKEY_assign(pkey, type, key);
714 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
716 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
717 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
720 return pkey->pkey.dh;
723 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
725 DH *ret = EVP_PKEY_get0_DH(pkey);
732 int EVP_PKEY_type(int type)
735 const EVP_PKEY_ASN1_METHOD *ameth;
737 ameth = EVP_PKEY_asn1_find(&e, type);
739 ret = ameth->pkey_id;
742 # ifndef OPENSSL_NO_ENGINE
748 int EVP_PKEY_id(const EVP_PKEY *pkey)
753 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
755 return EVP_PKEY_type(pkey->type);
759 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
761 BIO_set_indent(*out, saved_indent);
763 BIO *next = BIO_pop(*out);
771 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
777 long i = BIO_get_indent(*out);
779 *saved_indent = (i < 0 ? 0 : i);
780 if (BIO_set_indent(*out, indent) <= 0) {
781 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
785 if (BIO_set_indent(*out, indent) <= 0) {
786 print_reset_indent(out, *pop_f_prefix, *saved_indent);
793 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
796 return BIO_indent(out, indent, 128)
797 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
798 kstr, OBJ_nid2ln(pkey->type)) > 0;
801 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
802 const char *propquery /* For provided serialization */,
803 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
804 int indent, ASN1_PCTX *pctx),
805 ASN1_PCTX *legacy_pctx /* For legacy print */)
809 OSSL_SERIALIZER_CTX *ctx = NULL;
810 int ret = -2; /* default to unsupported */
812 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
815 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
816 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
817 ret = OSSL_SERIALIZER_to_bio(ctx, out);
818 OSSL_SERIALIZER_CTX_free(ctx);
823 /* legacy fallback */
824 if (legacy_print != NULL)
825 ret = legacy_print(out, pkey, 0, legacy_pctx);
827 ret = unsup_alg(out, pkey, 0, "Public Key");
830 print_reset_indent(&out, pop_f_prefix, saved_indent);
834 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
835 int indent, ASN1_PCTX *pctx)
837 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
838 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
842 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
843 int indent, ASN1_PCTX *pctx)
845 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
846 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
850 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
851 int indent, ASN1_PCTX *pctx)
853 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
854 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
858 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
859 int arg1, void *arg2)
861 if (pkey->keymgmt == NULL)
864 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
866 char mdname[80] = "";
868 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
873 nid = OBJ_sn2nid(mdname);
874 if (nid == NID_undef)
875 nid = OBJ_ln2nid(mdname);
876 if (nid == NID_undef)
886 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
888 if (pkey->ameth == NULL)
889 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
890 if (pkey->ameth->pkey_ctrl == NULL)
892 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
895 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
897 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
900 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
901 char *mdname, size_t mdname_sz)
903 if (pkey->ameth == NULL) {
904 OSSL_PARAM params[3];
905 char mddefault[100] = "";
906 char mdmandatory[100] = "";
909 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
910 mddefault, sizeof(mddefault));
912 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
914 sizeof(mdmandatory));
915 params[2] = OSSL_PARAM_construct_end();
916 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
918 if (mdmandatory[0] != '\0') {
919 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
922 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
928 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
929 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
932 OPENSSL_strlcpy(mdname, name, mdname_sz);
937 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
941 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
944 * If there is a mandatory default digest and this isn't it, then
945 * the answer is 'no'.
947 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
949 return (nid == default_nid);
950 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
957 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
958 const unsigned char *pt, size_t ptlen)
962 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
968 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
971 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
977 #endif /* FIPS_MODE */
979 /*- All methods below can also be used in FIPS_MODE */
981 EVP_PKEY *EVP_PKEY_new(void)
983 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
986 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
989 ret->type = EVP_PKEY_NONE;
990 ret->save_type = EVP_PKEY_NONE;
992 ret->save_parameters = 1;
993 ret->lock = CRYPTO_THREAD_lock_new();
994 if (ret->lock == NULL) {
995 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
1002 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1006 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1009 REF_PRINT_COUNT("EVP_PKEY", pkey);
1010 REF_ASSERT_ISNT(i < 2);
1011 return ((i > 1) ? 1 : 0);
1015 void evp_pkey_free_legacy(EVP_PKEY *x)
1017 if (x->ameth != NULL) {
1018 if (x->ameth->pkey_free != NULL)
1019 x->ameth->pkey_free(x);
1023 # ifndef OPENSSL_NO_ENGINE
1024 ENGINE_finish(x->engine);
1026 ENGINE_finish(x->pmeth_engine);
1027 x->pmeth_engine = NULL;
1029 x->type = x->save_type = EVP_PKEY_NONE;
1031 #endif /* FIPS_MODE */
1033 static void evp_pkey_free_it(EVP_PKEY *x)
1035 /* internal function; x is never NULL */
1037 evp_keymgmt_util_clear_operation_cache(x);
1039 evp_pkey_free_legacy(x);
1042 if (x->keymgmt != NULL) {
1043 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1044 EVP_KEYMGMT_free(x->keymgmt);
1050 void EVP_PKEY_free(EVP_PKEY *x)
1057 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1058 REF_PRINT_COUNT("EVP_PKEY", x);
1061 REF_ASSERT_ISNT(i < 0);
1062 evp_pkey_free_it(x);
1063 CRYPTO_THREAD_lock_free(x->lock);
1065 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1070 int EVP_PKEY_size(const EVP_PKEY *pkey)
1073 if (pkey->ameth == NULL)
1074 return pkey->cache.size;
1075 else if (pkey->ameth->pkey_size != NULL)
1076 return pkey->ameth->pkey_size(pkey);
1081 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1082 EVP_KEYMGMT **keymgmt,
1083 const char *propquery)
1085 EVP_KEYMGMT *allocated_keymgmt = NULL;
1086 EVP_KEYMGMT *tmp_keymgmt = NULL;
1087 void *keydata = NULL;
1093 if (pk->pkey.ptr != NULL) {
1095 * If the legacy key doesn't have an dirty counter or export function,
1098 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1103 if (keymgmt != NULL) {
1104 tmp_keymgmt = *keymgmt;
1108 /* If no keymgmt was given or found, get a default keymgmt */
1109 if (tmp_keymgmt == NULL) {
1110 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1112 if (ctx != NULL && ctx->keytype != NULL)
1113 tmp_keymgmt = allocated_keymgmt =
1114 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
1115 EVP_PKEY_CTX_free(ctx);
1118 /* If there's still no keymgmt to be had, give up */
1119 if (tmp_keymgmt == NULL)
1123 if (pk->pkey.ptr != NULL) {
1127 * If the legacy "origin" hasn't changed since last time, we try
1128 * to find our keymgmt in the operation cache. If it has changed,
1129 * |i| remains zero, and we will clear the cache further down.
1131 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1132 i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
1135 * If |tmp_keymgmt| is present in the operation cache, it means
1136 * that export doesn't need to be redone. In that case, we take
1137 * token copies of the cached pointers, to have token success
1140 if (i < OSSL_NELEM(pk->operation_cache)
1141 && pk->operation_cache[i].keymgmt != NULL) {
1142 keydata = pk->operation_cache[i].keydata;
1148 * TODO(3.0) Right now, we assume we have ample space. We will have
1149 * to think about a cache aging scheme, though, if |i| indexes outside
1152 if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
1155 /* Make sure that the keymgmt key type matches the legacy NID */
1156 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1159 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1162 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) {
1163 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1169 * If the dirty counter changed since last time, then clear the
1170 * operation cache. In that case, we know that |i| is zero. Just
1171 * in case this is a re-export, we increment then decrement the
1172 * keymgmt reference counter.
1174 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1175 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1179 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1180 evp_keymgmt_util_clear_operation_cache(pk);
1181 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1183 /* Add the new export to the operation cache */
1184 if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
1185 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1190 /* Synchronize the dirty count */
1191 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1194 #endif /* FIPS_MODE */
1196 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1200 * If nothing was exported, |tmp_keymgmt| might point at a freed
1201 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1202 * the caller either way in that case.
1204 if (keydata == NULL)
1207 if (keymgmt != NULL)
1208 *keymgmt = tmp_keymgmt;
1210 EVP_KEYMGMT_free(allocated_keymgmt);
1216 * This differs from exporting in that it releases the legacy key and assigns
1217 * the export keymgmt and keydata to the "origin" provider side key instead
1218 * of the operation cache.
1220 void *evp_pkey_upgrade_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1221 EVP_KEYMGMT **keymgmt,
1222 const char *propquery)
1224 EVP_KEYMGMT *allocated_keymgmt = NULL;
1225 EVP_KEYMGMT *tmp_keymgmt = NULL;
1226 void *keydata = NULL;
1232 * If this key is already "upgraded", this function shouldn't have been
1235 if (!ossl_assert(pk->keymgmt == NULL))
1238 if (keymgmt != NULL) {
1239 tmp_keymgmt = *keymgmt;
1243 /* If the key isn't a legacy one, bail out, but with proper values */
1244 if (pk->pkey.ptr == NULL) {
1245 tmp_keymgmt = pk->keymgmt;
1246 keydata = pk->keydata;
1248 /* If the legacy key doesn't have an export function, give up */
1249 if (pk->ameth->export_to == NULL)
1252 /* If no keymgmt was given, get a default keymgmt */
1253 if (tmp_keymgmt == NULL) {
1255 EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1257 if (ctx != NULL && ctx->keytype != NULL)
1258 tmp_keymgmt = allocated_keymgmt =
1259 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
1260 EVP_PKEY_CTX_free(ctx);
1263 /* If we still don't have a keymgmt, give up */
1264 if (tmp_keymgmt == NULL)
1267 /* Make sure that the keymgmt key type matches the legacy NID */
1268 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1271 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1274 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)
1275 || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
1276 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1282 * Clear the operation cache, all the legacy data, as well as the
1285 evp_pkey_free_legacy(pk);
1286 pk->dirty_cnt_copy = 0;
1288 evp_keymgmt_util_clear_operation_cache(pk);
1289 pk->keymgmt = tmp_keymgmt;
1290 pk->keydata = keydata;
1291 evp_keymgmt_util_cache_keyinfo(pk);
1296 * If nothing was upgraded, |tmp_keymgmt| might point at a freed
1297 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1298 * the caller either way in that case.
1300 if (keydata == NULL)
1303 if (keymgmt != NULL)
1304 *keymgmt = tmp_keymgmt;
1306 EVP_KEYMGMT_free(allocated_keymgmt);
1309 #endif /* FIPS_MODE */