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 int EVP_PKEY_bits(const EVP_PKEY *pkey)
47 if (pkey->ameth == NULL)
48 return pkey->cache.bits;
49 else if (pkey->ameth->pkey_bits)
50 return pkey->ameth->pkey_bits(pkey);
55 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
59 if (pkey->ameth == NULL)
60 return pkey->cache.security_bits;
61 if (pkey->ameth->pkey_security_bits == NULL)
63 return pkey->ameth->pkey_security_bits(pkey);
66 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
68 # ifndef OPENSSL_NO_DSA
69 if (pkey->type == EVP_PKEY_DSA) {
70 int ret = pkey->save_parameters;
73 pkey->save_parameters = mode;
77 # ifndef OPENSSL_NO_EC
78 if (pkey->type == EVP_PKEY_EC) {
79 int ret = pkey->save_parameters;
82 pkey->save_parameters = mode;
89 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
92 * TODO: clean up legacy stuff from this function when legacy support
97 * Only check that type match this early when both keys are legacy.
98 * If either of them is provided, we let evp_keymgmt_util_copy()
99 * do this check, after having exported either of them that isn't
102 if (to->keymgmt == NULL && from->keymgmt == NULL) {
103 if (to->type == EVP_PKEY_NONE) {
104 if (EVP_PKEY_set_type(to, from->type) == 0)
106 } else if (to->type != from->type) {
107 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
112 if (EVP_PKEY_missing_parameters(from)) {
113 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
117 if (!EVP_PKEY_missing_parameters(to)) {
118 if (EVP_PKEY_cmp_parameters(to, from) == 1)
120 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
125 * If |from| is provided, we upgrade |to| to be provided as well.
126 * This drops the legacy key from |to|.
127 * evp_pkey_upgrade_to_provider() checks if |to| is already provided,
128 * we don't need to do that here.
130 * TODO(3.0) We should investigate if that's too aggressive and make
131 * this scenario unsupported instead.
133 if (from->keymgmt != NULL) {
134 EVP_KEYMGMT *tmp_keymgmt = from->keymgmt;
137 * The returned pointer is known to be cached, so we don't have to
138 * save it. However, if it's NULL, something went wrong and we can't
141 if (evp_pkey_upgrade_to_provider(to, NULL,
142 &tmp_keymgmt, NULL) == NULL) {
143 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
148 /* For purely provided keys, we just call the keymgmt utility */
149 if (to->keymgmt != NULL && from->keymgmt != NULL)
150 return evp_keymgmt_util_copy(to, (EVP_PKEY *)from,
151 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
154 * If |to| is provided, we know that |from| is legacy at this point.
155 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
156 * to copy the appropriate data to |to|'s keydata.
158 if (to->keymgmt != NULL) {
159 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
161 evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
164 if (from_keydata == NULL) {
165 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
168 return evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata,
169 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
172 /* Both keys are legacy */
173 if (from->ameth != NULL && from->ameth->param_copy != NULL)
174 return from->ameth->param_copy(to, from);
179 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
182 if (pkey->keymgmt != NULL)
183 return !evp_keymgmt_util_has((EVP_PKEY *)pkey,
184 OSSL_KEYMGMT_SELECT_ALL_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 * Here, we know that we have a mixture of legacy and provided keys.
212 * Try cross export and compare the resulting key data.
214 keymgmt1 = a->keymgmt;
215 keydata1 = a->keydata;
216 keymgmt2 = b->keymgmt;
217 keydata2 = b->keydata;
219 if ((keymgmt1 == NULL
220 && !EVP_KEYMGMT_is_a(keymgmt2, OBJ_nid2sn(a->type)))
222 && !EVP_KEYMGMT_is_a(keymgmt1, OBJ_nid2sn(b->type))))
223 return -1; /* not the same key type */
225 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
227 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
228 if (tmp_keydata != NULL) {
230 keydata1 = tmp_keydata;
233 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
235 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
236 if (tmp_keydata != NULL) {
238 keydata2 = tmp_keydata;
242 /* If we still don't have matching keymgmt implementations, we give up */
243 if (keymgmt1 != keymgmt2)
246 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
249 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
252 * TODO: clean up legacy stuff from this function when legacy support
256 if (a->keymgmt != NULL || b->keymgmt != NULL)
257 return evp_pkey_cmp_any(a, b, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
259 /* All legacy keys */
260 if (a->type != b->type)
262 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
263 return a->ameth->param_cmp(a, b);
267 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
270 * TODO: clean up legacy stuff from this function when legacy support
274 if (a->keymgmt != NULL || b->keymgmt != NULL)
275 return evp_pkey_cmp_any(a, b,
276 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
277 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
279 /* All legacy keys */
280 if (a->type != b->type)
283 if (a->ameth != NULL) {
285 /* Compare parameters if the algorithm has them */
286 if (a->ameth->param_cmp != NULL) {
287 ret = a->ameth->param_cmp(a, b);
292 if (a->ameth->pub_cmp != NULL)
293 return a->ameth->pub_cmp(a, b);
299 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
300 const unsigned char *priv,
303 EVP_PKEY *ret = EVP_PKEY_new();
306 || !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
307 /* EVPerr already called */
311 if (ret->ameth->set_priv_key == NULL) {
312 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
313 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
317 if (!ret->ameth->set_priv_key(ret, priv, len)) {
318 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
329 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
330 const unsigned char *pub,
333 EVP_PKEY *ret = EVP_PKEY_new();
336 || !pkey_set_type(ret, e, type, NULL, -1, NULL)) {
337 /* EVPerr already called */
341 if (ret->ameth->set_pub_key == NULL) {
342 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
343 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
347 if (!ret->ameth->set_pub_key(ret, pub, len)) {
348 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
359 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
362 if (pkey->ameth->get_priv_key == NULL) {
363 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
364 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
368 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
369 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
376 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
379 if (pkey->ameth->get_pub_key == NULL) {
380 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
381 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
385 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
386 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
393 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
394 size_t len, const EVP_CIPHER *cipher)
396 # ifndef OPENSSL_NO_CMAC
397 # ifndef OPENSSL_NO_ENGINE
398 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
400 const char *cipher_name = EVP_CIPHER_name(cipher);
401 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
402 OPENSSL_CTX *libctx =
403 prov == NULL ? NULL : ossl_provider_library_context(prov);
404 EVP_PKEY *ret = EVP_PKEY_new();
405 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
406 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
407 OSSL_PARAM params[4];
412 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1, NULL)) {
413 /* EVPerr already called */
417 # ifndef OPENSSL_NO_ENGINE
418 if (engine_id != NULL)
420 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
424 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
425 (char *)cipher_name, 0);
427 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
429 params[paramsn] = OSSL_PARAM_construct_end();
431 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
432 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
436 ret->pkey.ptr = cmctx;
441 EVP_MAC_CTX_free(cmctx);
445 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
446 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
451 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
453 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
456 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
458 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
461 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
463 if (pkey->type == type) {
464 return 1; /* it already is that type */
468 * The application is requesting to alias this to a different pkey type,
469 * but not one that resolves to the base type.
471 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
472 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
480 # ifndef OPENSSL_NO_ENGINE
481 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
484 if (!ENGINE_init(e)) {
485 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
488 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
490 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
494 ENGINE_finish(pkey->pmeth_engine);
495 pkey->pmeth_engine = e;
499 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
504 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
508 #ifndef OPENSSL_NO_EC
509 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
510 const EC_GROUP *group = EC_KEY_get0_group(key);
512 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
513 alias = EVP_PKEY_SM2;
517 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
519 if (!EVP_PKEY_set_alias_type(pkey, alias))
521 pkey->pkey.ptr = key;
522 return (key != NULL);
525 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
527 return pkey->pkey.ptr;
530 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
532 ASN1_OCTET_STRING *os = NULL;
533 if (pkey->type != EVP_PKEY_HMAC) {
534 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
537 os = EVP_PKEY_get0(pkey);
542 # ifndef OPENSSL_NO_POLY1305
543 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
545 ASN1_OCTET_STRING *os = NULL;
546 if (pkey->type != EVP_PKEY_POLY1305) {
547 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
550 os = EVP_PKEY_get0(pkey);
556 # ifndef OPENSSL_NO_SIPHASH
557 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
559 ASN1_OCTET_STRING *os = NULL;
561 if (pkey->type != EVP_PKEY_SIPHASH) {
562 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
565 os = EVP_PKEY_get0(pkey);
571 # ifndef OPENSSL_NO_RSA
572 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
574 int ret = EVP_PKEY_assign_RSA(pkey, key);
580 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
582 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
583 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
586 return pkey->pkey.rsa;
589 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
591 RSA *ret = EVP_PKEY_get0_RSA(pkey);
598 # ifndef OPENSSL_NO_DSA
599 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
601 int ret = EVP_PKEY_assign_DSA(pkey, key);
607 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
609 if (pkey->type != EVP_PKEY_DSA) {
610 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
613 return pkey->pkey.dsa;
616 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
618 DSA *ret = EVP_PKEY_get0_DSA(pkey);
625 # ifndef OPENSSL_NO_EC
627 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
629 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
635 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
637 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
638 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
641 return pkey->pkey.ec;
644 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
646 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
653 # ifndef OPENSSL_NO_DH
655 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
657 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
658 int ret = EVP_PKEY_assign(pkey, type, key);
665 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
667 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
668 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
671 return pkey->pkey.dh;
674 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
676 DH *ret = EVP_PKEY_get0_DH(pkey);
683 int EVP_PKEY_type(int type)
686 const EVP_PKEY_ASN1_METHOD *ameth;
688 ameth = EVP_PKEY_asn1_find(&e, type);
690 ret = ameth->pkey_id;
693 # ifndef OPENSSL_NO_ENGINE
699 int EVP_PKEY_id(const EVP_PKEY *pkey)
704 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
706 return EVP_PKEY_type(pkey->type);
710 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
712 BIO_set_indent(*out, saved_indent);
714 BIO *next = BIO_pop(*out);
722 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
728 long i = BIO_get_indent(*out);
730 *saved_indent = (i < 0 ? 0 : i);
731 if (BIO_set_indent(*out, indent) <= 0) {
732 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
736 if (BIO_set_indent(*out, indent) <= 0) {
737 print_reset_indent(out, *pop_f_prefix, *saved_indent);
744 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
747 return BIO_indent(out, indent, 128)
748 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
749 kstr, OBJ_nid2ln(pkey->type)) > 0;
752 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
753 const char *propquery /* For provided serialization */,
754 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
755 int indent, ASN1_PCTX *pctx),
756 ASN1_PCTX *legacy_pctx /* For legacy print */)
760 OSSL_SERIALIZER_CTX *ctx = NULL;
761 int ret = -2; /* default to unsupported */
763 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
766 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
767 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
768 ret = OSSL_SERIALIZER_to_bio(ctx, out);
769 OSSL_SERIALIZER_CTX_free(ctx);
774 /* legacy fallback */
775 if (legacy_print != NULL)
776 ret = legacy_print(out, pkey, 0, legacy_pctx);
778 ret = unsup_alg(out, pkey, 0, "Public Key");
781 print_reset_indent(&out, pop_f_prefix, saved_indent);
785 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
786 int indent, ASN1_PCTX *pctx)
788 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
789 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
793 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
794 int indent, ASN1_PCTX *pctx)
796 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
797 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
801 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
802 int indent, ASN1_PCTX *pctx)
804 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
805 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
809 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
810 int arg1, void *arg2)
812 if (pkey->keymgmt == NULL)
815 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
817 char mdname[80] = "";
819 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
824 nid = OBJ_sn2nid(mdname);
825 if (nid == NID_undef)
826 nid = OBJ_ln2nid(mdname);
827 if (nid == NID_undef)
837 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
839 if (pkey->ameth == NULL)
840 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
841 if (pkey->ameth->pkey_ctrl == NULL)
843 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
846 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
848 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
851 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
852 char *mdname, size_t mdname_sz)
854 if (pkey->ameth == NULL) {
855 OSSL_PARAM params[3];
856 char mddefault[100] = "";
857 char mdmandatory[100] = "";
860 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
861 mddefault, sizeof(mddefault));
863 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
865 sizeof(mdmandatory));
866 params[2] = OSSL_PARAM_construct_end();
867 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
869 if (mdmandatory[0] != '\0') {
870 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
873 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
879 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
880 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
883 OPENSSL_strlcpy(mdname, name, mdname_sz);
888 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
892 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
895 * If there is a mandatory default digest and this isn't it, then
896 * the answer is 'no'.
898 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
900 return (nid == default_nid);
901 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
908 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
909 const unsigned char *pt, size_t ptlen)
913 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
919 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
922 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
928 #endif /* FIPS_MODE */
930 /*- All methods below can also be used in FIPS_MODE */
932 EVP_PKEY *EVP_PKEY_new(void)
934 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
937 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
940 ret->type = EVP_PKEY_NONE;
941 ret->save_type = EVP_PKEY_NONE;
943 ret->save_parameters = 1;
944 ret->lock = CRYPTO_THREAD_lock_new();
945 if (ret->lock == NULL) {
946 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
954 * Setup a public key management method.
956 * For legacy keys, either |type| or |str| is expected to have the type
957 * information. In this case, the setup consists of finding an ASN1 method
958 * and potentially an ENGINE, and setting those fields in |pkey|.
960 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
961 * case, the setup consists of setting the |keymgmt| field in |pkey|.
963 * If pkey is NULL just return 1 or 0 if the key management method exists.
966 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
967 int len, EVP_KEYMGMT *keymgmt)
970 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
971 ENGINE **eptr = (e == NULL) ? &e : NULL;
975 * The setups can't set both legacy and provider side methods.
978 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
979 || !ossl_assert(e == NULL || keymgmt == NULL)) {
980 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
988 free_it = free_it || pkey->pkey.ptr != NULL;
990 free_it = free_it || pkey->keydata != NULL;
992 evp_pkey_free_it(pkey);
995 * If key type matches and a method exists then this lookup has
996 * succeeded once so just indicate success.
998 if (pkey->type != EVP_PKEY_NONE
999 && type == pkey->save_type
1000 && pkey->ameth != NULL)
1002 # ifndef OPENSSL_NO_ENGINE
1003 /* If we have ENGINEs release them */
1004 ENGINE_finish(pkey->engine);
1005 pkey->engine = NULL;
1006 ENGINE_finish(pkey->pmeth_engine);
1007 pkey->pmeth_engine = NULL;
1013 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1014 else if (type != EVP_PKEY_NONE)
1015 ameth = EVP_PKEY_asn1_find(eptr, type);
1016 # ifndef OPENSSL_NO_ENGINE
1017 if (pkey == NULL && eptr != NULL)
1027 check = check && ameth == NULL;
1029 check = check && keymgmt == NULL;
1031 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
1036 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1037 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1041 pkey->keymgmt = keymgmt;
1043 pkey->save_type = type;
1048 * If the internal "origin" key is provider side, don't save |ameth|.
1049 * The main reason is that |ameth| is one factor to detect that the
1050 * internal "origin" key is a legacy one.
1052 if (keymgmt == NULL)
1053 pkey->ameth = ameth;
1057 * The EVP_PKEY_ASN1_METHOD |pkey_id| serves different purposes,
1058 * depending on if we're setting this key to contain a legacy or
1059 * a provider side "origin" key. For a legacy key, we assign it
1060 * to the |type| field, but for a provider side key, we assign it
1061 * to the |save_type| field, because |type| is supposed to be set
1062 * to EVP_PKEY_NONE in that case.
1064 if (keymgmt != NULL)
1065 pkey->save_type = ameth->pkey_id;
1066 else if (pkey->ameth != NULL)
1067 pkey->type = ameth->pkey_id;
1074 static void find_ameth(const char *name, void *data)
1076 const char **str = data;
1079 * The error messages from pkey_set_type() are uninteresting here,
1084 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1088 else if (str[1] == NULL)
1096 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1099 # define EVP_PKEY_TYPE_STR str[0]
1100 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1102 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1103 * Ideally, only one should be found. If two (or more) are found, the
1104 * match is ambiguous. This should never happen, but...
1106 const char *str[2] = { NULL, NULL };
1108 EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str);
1109 if (str[1] != NULL) {
1110 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1114 # define EVP_PKEY_TYPE_STR NULL
1115 # define EVP_PKEY_TYPE_STRLEN -1
1117 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1118 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1121 #undef EVP_PKEY_TYPE_STR
1122 #undef EVP_PKEY_TYPE_STRLEN
1125 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1129 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1132 REF_PRINT_COUNT("EVP_PKEY", pkey);
1133 REF_ASSERT_ISNT(i < 2);
1134 return ((i > 1) ? 1 : 0);
1138 void evp_pkey_free_legacy(EVP_PKEY *x)
1140 if (x->ameth != NULL) {
1141 if (x->ameth->pkey_free != NULL)
1142 x->ameth->pkey_free(x);
1145 # ifndef OPENSSL_NO_ENGINE
1146 ENGINE_finish(x->engine);
1148 ENGINE_finish(x->pmeth_engine);
1149 x->pmeth_engine = NULL;
1151 x->type = EVP_PKEY_NONE;
1153 #endif /* FIPS_MODE */
1155 static void evp_pkey_free_it(EVP_PKEY *x)
1157 /* internal function; x is never NULL */
1159 evp_keymgmt_util_clear_operation_cache(x);
1161 evp_pkey_free_legacy(x);
1164 if (x->keymgmt != NULL) {
1165 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1166 EVP_KEYMGMT_free(x->keymgmt);
1172 void EVP_PKEY_free(EVP_PKEY *x)
1179 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1180 REF_PRINT_COUNT("EVP_PKEY", x);
1183 REF_ASSERT_ISNT(i < 0);
1184 evp_pkey_free_it(x);
1185 CRYPTO_THREAD_lock_free(x->lock);
1187 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1192 int EVP_PKEY_size(const EVP_PKEY *pkey)
1197 size = pkey->cache.size;
1199 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1200 size = pkey->ameth->pkey_size(pkey);
1206 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1207 EVP_KEYMGMT **keymgmt,
1208 const char *propquery)
1210 EVP_KEYMGMT *allocated_keymgmt = NULL;
1211 EVP_KEYMGMT *tmp_keymgmt = NULL;
1212 void *keydata = NULL;
1218 /* No key data => nothing to export */
1221 check = check && pk->pkey.ptr == NULL;
1223 check = check && pk->keydata == NULL;
1228 if (pk->pkey.ptr != NULL) {
1230 * If the legacy key doesn't have an dirty counter or export function,
1233 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1238 if (keymgmt != NULL) {
1239 tmp_keymgmt = *keymgmt;
1244 * If no keymgmt was given or found, get a default keymgmt. We do so by
1245 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1247 if (tmp_keymgmt == NULL) {
1248 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1250 tmp_keymgmt = ctx->keymgmt;
1251 ctx->keymgmt = NULL;
1252 EVP_PKEY_CTX_free(ctx);
1255 /* If there's still no keymgmt to be had, give up */
1256 if (tmp_keymgmt == NULL)
1260 if (pk->pkey.ptr != NULL) {
1264 * If the legacy "origin" hasn't changed since last time, we try
1265 * to find our keymgmt in the operation cache. If it has changed,
1266 * |i| remains zero, and we will clear the cache further down.
1268 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1269 i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
1272 * If |tmp_keymgmt| is present in the operation cache, it means
1273 * that export doesn't need to be redone. In that case, we take
1274 * token copies of the cached pointers, to have token success
1277 if (i < OSSL_NELEM(pk->operation_cache)
1278 && pk->operation_cache[i].keymgmt != NULL) {
1279 keydata = pk->operation_cache[i].keydata;
1285 * TODO(3.0) Right now, we assume we have ample space. We will have
1286 * to think about a cache aging scheme, though, if |i| indexes outside
1289 if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
1292 /* Make sure that the keymgmt key type matches the legacy NID */
1293 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1296 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1299 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) {
1300 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1306 * If the dirty counter changed since last time, then clear the
1307 * operation cache. In that case, we know that |i| is zero. Just
1308 * in case this is a re-export, we increment then decrement the
1309 * keymgmt reference counter.
1311 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1312 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1316 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1317 evp_keymgmt_util_clear_operation_cache(pk);
1318 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1320 /* Add the new export to the operation cache */
1321 if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
1322 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1327 /* Synchronize the dirty count */
1328 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1331 #endif /* FIPS_MODE */
1333 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1337 * If nothing was exported, |tmp_keymgmt| might point at a freed
1338 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1339 * the caller either way in that case.
1341 if (keydata == NULL)
1344 if (keymgmt != NULL)
1345 *keymgmt = tmp_keymgmt;
1347 EVP_KEYMGMT_free(allocated_keymgmt);
1353 * This differs from exporting in that it releases the legacy key and assigns
1354 * the export keymgmt and keydata to the "origin" provider side key instead
1355 * of the operation cache.
1357 void *evp_pkey_upgrade_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1358 EVP_KEYMGMT **keymgmt,
1359 const char *propquery)
1361 EVP_KEYMGMT *allocated_keymgmt = NULL;
1362 EVP_KEYMGMT *tmp_keymgmt = NULL;
1363 void *keydata = NULL;
1369 * If this key is already "upgraded", this function shouldn't have been
1372 if (!ossl_assert(pk->keymgmt == NULL))
1375 if (keymgmt != NULL) {
1376 tmp_keymgmt = *keymgmt;
1380 /* If the key isn't a legacy one, bail out, but with proper values */
1381 if (pk->pkey.ptr == NULL) {
1382 tmp_keymgmt = pk->keymgmt;
1383 keydata = pk->keydata;
1385 /* If the legacy key doesn't have an export function, give up */
1386 if (pk->ameth->export_to == NULL)
1390 * If no keymgmt was given or found, get a default keymgmt. We do
1391 * so by letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we
1394 if (tmp_keymgmt == NULL) {
1396 EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1398 tmp_keymgmt = ctx->keymgmt;
1399 ctx->keymgmt = NULL;
1400 EVP_PKEY_CTX_free(ctx);
1403 /* If we still don't have a keymgmt, give up */
1404 if (tmp_keymgmt == NULL)
1407 /* Make sure that the keymgmt key type matches the legacy NID */
1408 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1411 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1414 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)
1415 || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
1416 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1422 * Clear the operation cache, all the legacy data, as well as the
1425 evp_pkey_free_legacy(pk);
1426 pk->dirty_cnt_copy = 0;
1428 evp_keymgmt_util_clear_operation_cache(pk);
1429 pk->keymgmt = tmp_keymgmt;
1430 pk->keydata = keydata;
1431 evp_keymgmt_util_cache_keyinfo(pk);
1436 * If nothing was upgraded, |tmp_keymgmt| might point at a freed
1437 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1438 * the caller either way in that case.
1440 if (keydata == NULL)
1443 if (keymgmt != NULL)
1444 *keymgmt = tmp_keymgmt;
1446 EVP_KEYMGMT_free(allocated_keymgmt);
1449 #endif /* FIPS_MODE */