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 int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
39 int len, EVP_KEYMGMT *keymgmt);
40 static void evp_pkey_free_it(EVP_PKEY *key);
44 /* The type of parameters selected in key parameter functions */
45 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
47 int EVP_PKEY_bits(const EVP_PKEY *pkey)
50 if (pkey->ameth == NULL)
51 return pkey->cache.bits;
52 else if (pkey->ameth->pkey_bits)
53 return pkey->ameth->pkey_bits(pkey);
58 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
62 if (pkey->ameth == NULL)
63 return pkey->cache.security_bits;
64 if (pkey->ameth->pkey_security_bits == NULL)
66 return pkey->ameth->pkey_security_bits(pkey);
69 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
71 # ifndef OPENSSL_NO_DSA
72 if (pkey->type == EVP_PKEY_DSA) {
73 int ret = pkey->save_parameters;
76 pkey->save_parameters = mode;
80 # ifndef OPENSSL_NO_EC
81 if (pkey->type == EVP_PKEY_EC) {
82 int ret = pkey->save_parameters;
85 pkey->save_parameters = mode;
92 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
95 * TODO: clean up legacy stuff from this function when legacy support
100 * If |to| is a legacy key and |from| isn't, we must downgrade |from|.
101 * If that fails, this function fails.
103 if (to->type != EVP_PKEY_NONE && from->keymgmt != NULL)
104 if (!evp_pkey_downgrade((EVP_PKEY *)from))
108 * Make sure |to| is typed. Content is less important at this early
111 * 1. If |to| is untyped, assign |from|'s key type to it.
112 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
113 * (|from| was already downgraded above)
115 * If |to| is a provided key, there's nothing more to do here, functions
116 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
117 * further down help us find out if they are the same or not.
119 if (to->type == EVP_PKEY_NONE && to->keymgmt == NULL) {
120 if (from->type != EVP_PKEY_NONE) {
121 if (EVP_PKEY_set_type(to, from->type) == 0)
124 if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
127 } else if (to->type != EVP_PKEY_NONE) {
128 if (to->type != from->type) {
129 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
134 if (EVP_PKEY_missing_parameters(from)) {
135 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
139 if (!EVP_PKEY_missing_parameters(to)) {
140 if (EVP_PKEY_cmp_parameters(to, from) == 1)
142 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
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, SELECT_PARAMETERS);
151 * If |to| is provided, we know that |from| is legacy at this point.
152 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
153 * to copy the appropriate data to |to|'s keydata.
155 if (to->keymgmt != NULL) {
156 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
158 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
162 * If we get a NULL, it could be an internal error, or it could be
163 * that there's a key mismatch. We're pretending the latter...
165 if (from_keydata == NULL) {
166 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
169 return evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata,
173 /* Both keys are legacy */
174 if (from->ameth != NULL && from->ameth->param_copy != NULL)
175 return from->ameth->param_copy(to, from);
180 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
183 if (pkey->keymgmt != NULL)
184 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
185 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
186 return pkey->ameth->param_missing(pkey);
192 * This function is called for any mixture of keys except pure legacy pair.
193 * TODO When legacy keys are gone, we replace a call to this functions with
194 * a call to evp_keymgmt_util_match().
196 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
199 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
200 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
202 /* If none of them are provided, this function shouldn't have been called */
203 if (!ossl_assert(a->keymgmt != NULL || b->keymgmt != NULL))
206 /* For purely provided keys, we just call the keymgmt utility */
207 if (a->keymgmt != NULL && b->keymgmt != NULL)
208 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
211 * At this point, one of them is provided, the other not. This allows
212 * us to compare types using legacy NIDs.
214 if ((a->type != EVP_PKEY_NONE
215 && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
216 || (b->type != EVP_PKEY_NONE
217 && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type))))
218 return -1; /* not the same key type */
221 * We've determined that they both are the same keytype, so the next
222 * step is to do a bit of cross export to ensure we have keydata for
223 * both keys in the same keymgmt.
225 keymgmt1 = a->keymgmt;
226 keydata1 = a->keydata;
227 keymgmt2 = b->keymgmt;
228 keydata2 = b->keydata;
230 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
232 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
233 if (tmp_keydata != NULL) {
235 keydata1 = tmp_keydata;
238 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
240 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
241 if (tmp_keydata != NULL) {
243 keydata2 = tmp_keydata;
247 /* If we still don't have matching keymgmt implementations, we give up */
248 if (keymgmt1 != keymgmt2)
251 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
254 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
257 * TODO: clean up legacy stuff from this function when legacy support
261 if (a->keymgmt != NULL || b->keymgmt != NULL)
262 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
264 /* All legacy keys */
265 if (a->type != b->type)
267 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
268 return a->ameth->param_cmp(a, b);
272 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
275 * TODO: clean up legacy stuff from this function when legacy support
279 if (a->keymgmt != NULL || b->keymgmt != NULL)
280 return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
281 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY));
283 /* All legacy keys */
284 if (a->type != b->type)
287 if (a->ameth != NULL) {
289 /* Compare parameters if the algorithm has them */
290 if (a->ameth->param_cmp != NULL) {
291 ret = a->ameth->param_cmp(a, b);
296 if (a->ameth->pub_cmp != NULL)
297 return a->ameth->pub_cmp(a, b);
303 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
304 const unsigned char *priv,
307 EVP_PKEY *ret = EVP_PKEY_new();
310 || !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
311 /* EVPerr already called */
315 if (ret->ameth->set_priv_key == NULL) {
316 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
317 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
321 if (!ret->ameth->set_priv_key(ret, priv, len)) {
322 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
333 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
334 const unsigned char *pub,
337 EVP_PKEY *ret = EVP_PKEY_new();
340 || !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
341 /* EVPerr already called */
345 if (ret->ameth->set_pub_key == NULL) {
346 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
347 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
351 if (!ret->ameth->set_pub_key(ret, pub, len)) {
352 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
363 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
366 /* TODO(3.0) Do we need to do anything about provider side keys? */
367 if (pkey->ameth->get_priv_key == NULL) {
368 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
369 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
373 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
374 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
381 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
384 /* TODO(3.0) Do we need to do anything about provider side keys? */
385 if (pkey->ameth->get_pub_key == NULL) {
386 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
387 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
391 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
392 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
399 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
400 size_t len, const EVP_CIPHER *cipher)
402 # ifndef OPENSSL_NO_CMAC
403 # ifndef OPENSSL_NO_ENGINE
404 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
406 const char *cipher_name = EVP_CIPHER_name(cipher);
407 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
408 OPENSSL_CTX *libctx =
409 prov == NULL ? NULL : ossl_provider_library_context(prov);
410 EVP_PKEY *ret = EVP_PKEY_new();
411 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
412 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
413 OSSL_PARAM params[4];
418 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1, NULL)) {
419 /* EVPerr already called */
423 # ifndef OPENSSL_NO_ENGINE
424 if (engine_id != NULL)
426 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
430 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
431 (char *)cipher_name, 0);
433 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
435 params[paramsn] = OSSL_PARAM_construct_end();
437 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
438 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
442 ret->pkey.ptr = cmctx;
447 EVP_MAC_CTX_free(cmctx);
451 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
452 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
457 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
459 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
462 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
464 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
467 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
469 if (pkey->type == type) {
470 return 1; /* it already is that type */
474 * The application is requesting to alias this to a different pkey type,
475 * but not one that resolves to the base type.
477 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
478 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
486 # ifndef OPENSSL_NO_ENGINE
487 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
490 if (!ENGINE_init(e)) {
491 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
494 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
496 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
500 ENGINE_finish(pkey->pmeth_engine);
501 pkey->pmeth_engine = e;
505 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
510 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
514 #ifndef OPENSSL_NO_EC
515 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
516 const EC_GROUP *group = EC_KEY_get0_group(key);
518 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
519 alias = EVP_PKEY_SM2;
523 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
525 if (!EVP_PKEY_set_alias_type(pkey, alias))
527 pkey->pkey.ptr = key;
528 return (key != NULL);
531 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
533 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
534 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
537 return pkey->pkey.ptr;
540 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
542 ASN1_OCTET_STRING *os = NULL;
543 if (pkey->type != EVP_PKEY_HMAC) {
544 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
547 os = EVP_PKEY_get0(pkey);
552 # ifndef OPENSSL_NO_POLY1305
553 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
555 ASN1_OCTET_STRING *os = NULL;
556 if (pkey->type != EVP_PKEY_POLY1305) {
557 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
560 os = EVP_PKEY_get0(pkey);
566 # ifndef OPENSSL_NO_SIPHASH
567 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
569 ASN1_OCTET_STRING *os = NULL;
571 if (pkey->type != EVP_PKEY_SIPHASH) {
572 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
575 os = EVP_PKEY_get0(pkey);
581 # ifndef OPENSSL_NO_RSA
582 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
584 int ret = EVP_PKEY_assign_RSA(pkey, key);
590 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
592 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
593 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
596 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
597 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
600 return pkey->pkey.rsa;
603 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
605 RSA *ret = EVP_PKEY_get0_RSA(pkey);
612 # ifndef OPENSSL_NO_DSA
613 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
615 int ret = EVP_PKEY_assign_DSA(pkey, key);
621 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
623 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
624 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
627 if (pkey->type != EVP_PKEY_DSA) {
628 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
631 return pkey->pkey.dsa;
634 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
636 DSA *ret = EVP_PKEY_get0_DSA(pkey);
643 # ifndef OPENSSL_NO_EC
645 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
647 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
653 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
655 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
656 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
659 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
660 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
663 return pkey->pkey.ec;
666 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
668 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
675 # ifndef OPENSSL_NO_DH
677 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
679 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
680 int ret = EVP_PKEY_assign(pkey, type, key);
687 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
689 if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
690 ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
693 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
694 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
697 return pkey->pkey.dh;
700 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
702 DH *ret = EVP_PKEY_get0_DH(pkey);
709 int EVP_PKEY_type(int type)
712 const EVP_PKEY_ASN1_METHOD *ameth;
714 ameth = EVP_PKEY_asn1_find(&e, type);
716 ret = ameth->pkey_id;
719 # ifndef OPENSSL_NO_ENGINE
725 int EVP_PKEY_id(const EVP_PKEY *pkey)
730 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
732 return EVP_PKEY_type(pkey->type);
736 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
738 BIO_set_indent(*out, saved_indent);
740 BIO *next = BIO_pop(*out);
748 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
754 long i = BIO_get_indent(*out);
756 *saved_indent = (i < 0 ? 0 : i);
757 if (BIO_set_indent(*out, indent) <= 0) {
758 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
762 if (BIO_set_indent(*out, indent) <= 0) {
763 print_reset_indent(out, *pop_f_prefix, *saved_indent);
770 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
773 return BIO_indent(out, indent, 128)
774 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
775 kstr, OBJ_nid2ln(pkey->type)) > 0;
778 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
779 const char *propquery /* For provided serialization */,
780 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
781 int indent, ASN1_PCTX *pctx),
782 ASN1_PCTX *legacy_pctx /* For legacy print */)
786 OSSL_SERIALIZER_CTX *ctx = NULL;
787 int ret = -2; /* default to unsupported */
789 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
792 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
793 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
794 ret = OSSL_SERIALIZER_to_bio(ctx, out);
795 OSSL_SERIALIZER_CTX_free(ctx);
800 /* legacy fallback */
801 if (legacy_print != NULL)
802 ret = legacy_print(out, pkey, 0, legacy_pctx);
804 ret = unsup_alg(out, pkey, 0, "Public Key");
807 print_reset_indent(&out, pop_f_prefix, saved_indent);
811 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
812 int indent, ASN1_PCTX *pctx)
814 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
815 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
819 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
820 int indent, ASN1_PCTX *pctx)
822 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
823 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
827 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
828 int indent, ASN1_PCTX *pctx)
830 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
831 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
835 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
836 int arg1, void *arg2)
838 if (pkey->keymgmt == NULL)
841 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
843 char mdname[80] = "";
845 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
850 nid = OBJ_sn2nid(mdname);
851 if (nid == NID_undef)
852 nid = OBJ_ln2nid(mdname);
853 if (nid == NID_undef)
863 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
865 if (pkey->ameth == NULL)
866 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
867 if (pkey->ameth->pkey_ctrl == NULL)
869 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
872 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
874 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
877 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
878 char *mdname, size_t mdname_sz)
880 if (pkey->ameth == NULL) {
881 OSSL_PARAM params[3];
882 char mddefault[100] = "";
883 char mdmandatory[100] = "";
886 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
887 mddefault, sizeof(mddefault));
889 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
891 sizeof(mdmandatory));
892 params[2] = OSSL_PARAM_construct_end();
893 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
895 if (mdmandatory[0] != '\0') {
896 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
899 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
905 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
906 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
909 OPENSSL_strlcpy(mdname, name, mdname_sz);
914 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
918 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
921 * If there is a mandatory default digest and this isn't it, then
922 * the answer is 'no'.
924 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
926 return (nid == default_nid);
927 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
934 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
935 const unsigned char *pt, size_t ptlen)
939 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
945 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
948 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
954 #endif /* FIPS_MODE */
956 /*- All methods below can also be used in FIPS_MODE */
958 EVP_PKEY *EVP_PKEY_new(void)
960 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
963 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
966 ret->type = EVP_PKEY_NONE;
967 ret->save_type = EVP_PKEY_NONE;
969 ret->save_parameters = 1;
970 ret->lock = CRYPTO_THREAD_lock_new();
971 if (ret->lock == NULL) {
972 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
980 * Setup a public key management method.
982 * For legacy keys, either |type| or |str| is expected to have the type
983 * information. In this case, the setup consists of finding an ASN1 method
984 * and potentially an ENGINE, and setting those fields in |pkey|.
986 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
987 * case, the setup consists of setting the |keymgmt| field in |pkey|.
989 * If pkey is NULL just return 1 or 0 if the key management method exists.
992 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
993 int len, EVP_KEYMGMT *keymgmt)
996 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
997 ENGINE **eptr = (e == NULL) ? &e : NULL;
1001 * The setups can't set both legacy and provider side methods.
1004 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1005 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1006 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1014 free_it = free_it || pkey->pkey.ptr != NULL;
1016 free_it = free_it || pkey->keydata != NULL;
1018 evp_pkey_free_it(pkey);
1021 * If key type matches and a method exists then this lookup has
1022 * succeeded once so just indicate success.
1024 if (pkey->type != EVP_PKEY_NONE
1025 && type == pkey->save_type
1026 && pkey->ameth != NULL)
1028 # ifndef OPENSSL_NO_ENGINE
1029 /* If we have ENGINEs release them */
1030 ENGINE_finish(pkey->engine);
1031 pkey->engine = NULL;
1032 ENGINE_finish(pkey->pmeth_engine);
1033 pkey->pmeth_engine = NULL;
1039 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1040 else if (type != EVP_PKEY_NONE)
1041 ameth = EVP_PKEY_asn1_find(eptr, type);
1042 # ifndef OPENSSL_NO_ENGINE
1043 if (pkey == NULL && eptr != NULL)
1053 check = check && ameth == NULL;
1055 check = check && keymgmt == NULL;
1057 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
1062 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1063 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1067 pkey->keymgmt = keymgmt;
1069 pkey->save_type = type;
1074 * If the internal "origin" key is provider side, don't save |ameth|.
1075 * The main reason is that |ameth| is one factor to detect that the
1076 * internal "origin" key is a legacy one.
1078 if (keymgmt == NULL)
1079 pkey->ameth = ameth;
1083 * The EVP_PKEY_ASN1_METHOD |pkey_id| serves different purposes,
1084 * depending on if we're setting this key to contain a legacy or
1085 * a provider side "origin" key. For a legacy key, we assign it
1086 * to the |type| field, but for a provider side key, we assign it
1087 * to the |save_type| field, because |type| is supposed to be set
1088 * to EVP_PKEY_NONE in that case.
1090 if (keymgmt != NULL)
1091 pkey->save_type = ameth->pkey_id;
1092 else if (pkey->ameth != NULL)
1093 pkey->type = ameth->pkey_id;
1100 static void find_ameth(const char *name, void *data)
1102 const char **str = data;
1105 * The error messages from pkey_set_type() are uninteresting here,
1110 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1114 else if (str[1] == NULL)
1122 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1125 # define EVP_PKEY_TYPE_STR str[0]
1126 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1128 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1129 * Ideally, only one should be found. If two (or more) are found, the
1130 * match is ambiguous. This should never happen, but...
1132 const char *str[2] = { NULL, NULL };
1134 EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str);
1135 if (str[1] != NULL) {
1136 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1140 # define EVP_PKEY_TYPE_STR NULL
1141 # define EVP_PKEY_TYPE_STRLEN -1
1143 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1144 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1147 #undef EVP_PKEY_TYPE_STR
1148 #undef EVP_PKEY_TYPE_STRLEN
1151 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1155 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1158 REF_PRINT_COUNT("EVP_PKEY", pkey);
1159 REF_ASSERT_ISNT(i < 2);
1160 return ((i > 1) ? 1 : 0);
1164 void evp_pkey_free_legacy(EVP_PKEY *x)
1166 if (x->ameth != NULL) {
1167 if (x->ameth->pkey_free != NULL)
1168 x->ameth->pkey_free(x);
1171 # ifndef OPENSSL_NO_ENGINE
1172 ENGINE_finish(x->engine);
1174 ENGINE_finish(x->pmeth_engine);
1175 x->pmeth_engine = NULL;
1177 x->type = EVP_PKEY_NONE;
1179 #endif /* FIPS_MODE */
1181 static void evp_pkey_free_it(EVP_PKEY *x)
1183 /* internal function; x is never NULL */
1185 evp_keymgmt_util_clear_operation_cache(x);
1187 evp_pkey_free_legacy(x);
1190 if (x->keymgmt != NULL) {
1191 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1192 EVP_KEYMGMT_free(x->keymgmt);
1198 void EVP_PKEY_free(EVP_PKEY *x)
1205 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1206 REF_PRINT_COUNT("EVP_PKEY", x);
1209 REF_ASSERT_ISNT(i < 0);
1210 evp_pkey_free_it(x);
1211 CRYPTO_THREAD_lock_free(x->lock);
1213 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1218 int EVP_PKEY_size(const EVP_PKEY *pkey)
1223 size = pkey->cache.size;
1225 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1226 size = pkey->ameth->pkey_size(pkey);
1232 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1233 EVP_KEYMGMT **keymgmt,
1234 const char *propquery)
1236 EVP_KEYMGMT *allocated_keymgmt = NULL;
1237 EVP_KEYMGMT *tmp_keymgmt = NULL;
1238 void *keydata = NULL;
1244 /* No key data => nothing to export */
1247 check = check && pk->pkey.ptr == NULL;
1249 check = check && pk->keydata == NULL;
1254 if (pk->pkey.ptr != NULL) {
1256 * If the legacy key doesn't have an dirty counter or export function,
1259 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1264 if (keymgmt != NULL) {
1265 tmp_keymgmt = *keymgmt;
1270 * If no keymgmt was given or found, get a default keymgmt. We do so by
1271 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1273 if (tmp_keymgmt == NULL) {
1274 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1276 tmp_keymgmt = ctx->keymgmt;
1277 ctx->keymgmt = NULL;
1278 EVP_PKEY_CTX_free(ctx);
1281 /* If there's still no keymgmt to be had, give up */
1282 if (tmp_keymgmt == NULL)
1286 if (pk->pkey.ptr != NULL) {
1290 * If the legacy "origin" hasn't changed since last time, we try
1291 * to find our keymgmt in the operation cache. If it has changed,
1292 * |i| remains zero, and we will clear the cache further down.
1294 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1295 i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
1298 * If |tmp_keymgmt| is present in the operation cache, it means
1299 * that export doesn't need to be redone. In that case, we take
1300 * token copies of the cached pointers, to have token success
1303 if (i < OSSL_NELEM(pk->operation_cache)
1304 && pk->operation_cache[i].keymgmt != NULL) {
1305 keydata = pk->operation_cache[i].keydata;
1311 * TODO(3.0) Right now, we assume we have ample space. We will have
1312 * to think about a cache aging scheme, though, if |i| indexes outside
1315 if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
1318 /* Make sure that the keymgmt key type matches the legacy NID */
1319 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1322 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1325 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) {
1326 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1332 * If the dirty counter changed since last time, then clear the
1333 * operation cache. In that case, we know that |i| is zero. Just
1334 * in case this is a re-export, we increment then decrement the
1335 * keymgmt reference counter.
1337 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1338 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1342 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1343 evp_keymgmt_util_clear_operation_cache(pk);
1344 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1346 /* Add the new export to the operation cache */
1347 if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
1348 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1353 /* Synchronize the dirty count */
1354 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1357 #endif /* FIPS_MODE */
1359 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1363 * If nothing was exported, |tmp_keymgmt| might point at a freed
1364 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1365 * the caller either way in that case.
1367 if (keydata == NULL)
1370 if (keymgmt != NULL)
1371 *keymgmt = tmp_keymgmt;
1373 EVP_KEYMGMT_free(allocated_keymgmt);
1378 int evp_pkey_downgrade(EVP_PKEY *pk)
1380 EVP_KEYMGMT *keymgmt = pk->keymgmt;
1381 void *keydata = pk->keydata;
1382 int type = pk->save_type;
1383 const char *keytype = NULL;
1385 /* If this isn't a provider side key, we're done */
1386 if (keymgmt == NULL)
1389 /* Get the key type name for error reporting */
1390 if (type != EVP_PKEY_NONE)
1391 keytype = OBJ_nid2sn(type);
1394 evp_first_name(EVP_KEYMGMT_provider(keymgmt), keymgmt->name_id);
1397 * |save_type| was set when any of the EVP_PKEY_set_type functions
1398 * was called. It was set to EVP_PKEY_NONE if the key type wasn't
1399 * recognised to be any of the legacy key types, and the downgrade
1402 if (type == EVP_PKEY_NONE) {
1403 ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_KEY_TYPE,
1404 "key type = %s, can't downgrade", keytype);
1409 * To be able to downgrade, we steal the provider side "origin" keymgmt
1410 * and keydata. We've already grabbed the pointers, so all we need to
1411 * do is clear those pointers in |pk| and then call evp_pkey_free_it().
1412 * That way, we can restore |pk| if we need to.
1416 evp_pkey_free_it(pk);
1417 if (EVP_PKEY_set_type(pk, type)) {
1418 /* If the key is typed but empty, we're done */
1419 if (keydata == NULL)
1422 if (pk->ameth->import_from == NULL) {
1423 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
1424 "key type = %s", keytype);
1425 } else if (evp_keymgmt_export(keymgmt, keydata,
1426 OSSL_KEYMGMT_SELECT_ALL,
1427 pk->ameth->import_from, pk)) {
1429 * Save the provider side data in the operation cache, so they'll
1430 * find it again. evp_pkey_free_it() cleared the cache, so it's
1431 * safe to assume slot zero is free.
1432 * Note that evp_keymgmt_util_cache_keydata() increments keymgmt's
1435 evp_keymgmt_util_cache_keydata(pk, 0, keymgmt, keydata);
1437 /* Synchronize the dirty count */
1438 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1442 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
1443 "key type = %s", keytype);
1447 * Something went wrong. This could for example happen if the keymgmt
1448 * turns out to be an HSM implementation that refuses to let go of some
1449 * of the key data, typically the private bits. In this case, we restore
1450 * the provider side internal "origin" and leave it at that.
1452 if (!ossl_assert(EVP_PKEY_set_type_by_keymgmt(pk, keymgmt))) {
1453 /* This should not be impossible */
1454 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1457 pk->keydata = keydata;
1458 evp_keymgmt_util_cache_keyinfo(pk);
1459 return 0; /* No downgrade, but at least the key is restored */
1461 #endif /* FIPS_MODE */
1463 const OSSL_PARAM *EVP_PKEY_gettable_params(EVP_PKEY *pkey)
1466 || pkey->keymgmt == NULL
1467 || pkey->keydata == NULL)
1469 return evp_keymgmt_gettable_params(pkey->keymgmt);
1473 * For the following methods param->return_size is set to a value
1474 * larger than can be returned by the call to evp_keymgmt_get_params().
1475 * If it is still this value then the parameter was ignored - and in this
1476 * case it returns an error..
1479 int EVP_PKEY_get_bn_param(EVP_PKEY *pkey, const char *key_name, BIGNUM **bn)
1482 OSSL_PARAM params[2];
1483 unsigned char buffer[2048];
1485 * Use -1 as the terminator here instead of sizeof(buffer) + 1 since
1486 * -1 is less likely to be a valid value.
1488 const size_t not_set = (size_t)-1;
1489 unsigned char *buf = NULL;
1493 || pkey->keymgmt == NULL
1494 || pkey->keydata == NULL
1499 memset(buffer, 0, sizeof(buffer));
1500 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
1501 /* If the return_size is still not_set then we know it was not found */
1502 params[0].return_size = not_set;
1503 params[1] = OSSL_PARAM_construct_end();
1504 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) {
1505 if (params[0].return_size == not_set
1506 || params[0].return_size == 0)
1508 buf_sz = params[0].return_size;
1510 * If it failed because the buffer was too small then allocate the
1511 * required buffer size and retry.
1513 buf = OPENSSL_zalloc(buf_sz);
1516 params[0].data = buf;
1517 params[0].data_size = buf_sz;
1519 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
1522 /* Fail if the param was not found */
1523 if (params[0].return_size == not_set)
1525 ret = OSSL_PARAM_get_BN(params, bn);
1531 int EVP_PKEY_get_octet_string_param(EVP_PKEY *pkey, const char *key_name,
1532 unsigned char *buf, size_t max_buf_sz,
1535 OSSL_PARAM params[2];
1536 const size_t not_set = max_buf_sz + 1;
1539 || pkey->keymgmt == NULL
1540 || pkey->keydata == NULL
1541 || key_name == NULL)
1544 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
1545 params[0].return_size = not_set;
1546 params[1] = OSSL_PARAM_construct_end();
1547 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
1549 if (params[0].return_size == not_set)
1552 *out_sz = params[0].return_size;
1556 int EVP_PKEY_get_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
1557 char *str, size_t max_buf_sz,
1560 OSSL_PARAM params[2];
1561 const size_t not_set = max_buf_sz + 1;
1564 || pkey->keymgmt == NULL
1565 || pkey->keydata == NULL
1566 || key_name == NULL)
1569 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
1570 params[0].return_size = not_set;
1571 params[1] = OSSL_PARAM_construct_end();
1572 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
1574 if (params[0].return_size == not_set)
1577 *out_sz = params[0].return_size;
1581 int EVP_PKEY_get_int_param(EVP_PKEY *pkey, const char *key_name, int *out)
1583 OSSL_PARAM params[2];
1584 const size_t not_set = sizeof(int) + 1;
1587 || pkey->keymgmt == NULL
1588 || pkey->keydata == NULL
1589 || key_name == NULL)
1592 params[0] = OSSL_PARAM_construct_int(key_name, out);
1593 params[0].return_size = not_set;
1594 params[1] = OSSL_PARAM_construct_end();
1595 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
1597 if (params[0].return_size == not_set)
1602 int EVP_PKEY_get_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t *out)
1604 OSSL_PARAM params[2];
1605 const size_t not_set = sizeof(size_t) + 1;
1608 || pkey->keymgmt == NULL
1609 || pkey->keydata == NULL
1610 || key_name == NULL)
1613 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
1614 params[0].return_size = not_set;
1615 params[1] = OSSL_PARAM_construct_end();
1616 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
1618 if (params[0].return_size == not_set)