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)
89 if (to->type == EVP_PKEY_NONE) {
90 if (EVP_PKEY_set_type(to, from->type) == 0)
92 } else if (to->type != from->type) {
93 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
97 if (EVP_PKEY_missing_parameters(from)) {
98 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
102 if (!EVP_PKEY_missing_parameters(to)) {
103 if (EVP_PKEY_cmp_parameters(to, from) == 1)
105 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
109 if (from->ameth && from->ameth->param_copy)
110 return from->ameth->param_copy(to, from);
115 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
118 if (pkey->keymgmt != NULL)
119 return !evp_keymgmt_util_has((EVP_PKEY *)pkey,
120 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
121 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
122 return pkey->ameth->param_missing(pkey);
128 * This function is called for any mixture of keys except pure legacy pair.
129 * TODO When legacy keys are gone, we replace a call to this functions with
130 * a call to evp_keymgmt_util_match().
132 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
135 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
136 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
138 /* If none of them are provided, this function shouldn't have been called */
139 if (!ossl_assert(a->keymgmt != NULL || b->keymgmt != NULL))
142 /* For purely provided keys, we just call the keymgmt utility */
143 if (a->keymgmt != NULL && b->keymgmt != NULL)
144 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
147 * Here, we know that we have a mixture of legacy and provided keys.
148 * Try cross export and compare the resulting key data.
150 keymgmt1 = a->keymgmt;
151 keydata1 = a->keydata;
152 keymgmt2 = b->keymgmt;
153 keydata2 = b->keydata;
155 if ((keymgmt1 == NULL
156 && !EVP_KEYMGMT_is_a(keymgmt2, OBJ_nid2sn(a->type)))
158 && !EVP_KEYMGMT_is_a(keymgmt1, OBJ_nid2sn(b->type))))
159 return -1; /* not the same key type */
161 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
163 evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
164 if (tmp_keydata != NULL) {
166 keydata1 = tmp_keydata;
169 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
171 evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
172 if (tmp_keydata != NULL) {
174 keydata2 = tmp_keydata;
178 /* If we still don't have matching keymgmt implementations, we give up */
179 if (keymgmt1 != keymgmt2)
182 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
185 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
188 * TODO: clean up legacy stuff from this function when legacy support
192 if (a->keymgmt != NULL || b->keymgmt != NULL)
193 return evp_pkey_cmp_any(a, b, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
195 /* All legacy keys */
196 if (a->type != b->type)
198 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
199 return a->ameth->param_cmp(a, b);
203 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
206 * TODO: clean up legacy stuff from this function when legacy support
210 if (a->keymgmt != NULL || b->keymgmt != NULL)
211 return evp_pkey_cmp_any(a, b,
212 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
213 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
215 /* All legacy keys */
216 if (a->type != b->type)
219 if (a->ameth != NULL) {
221 /* Compare parameters if the algorithm has them */
222 if (a->ameth->param_cmp != NULL) {
223 ret = a->ameth->param_cmp(a, b);
228 if (a->ameth->pub_cmp != NULL)
229 return a->ameth->pub_cmp(a, b);
237 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
238 * is NULL just return 1 or 0 if the algorithm exists.
241 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
244 const EVP_PKEY_ASN1_METHOD *ameth;
245 ENGINE **eptr = (e == NULL) ? &e : NULL;
249 evp_pkey_free_it(pkey);
251 * If key type matches and a method exists then this lookup has
252 * succeeded once so just indicate success.
254 if ((type == pkey->save_type) && pkey->ameth)
256 # ifndef OPENSSL_NO_ENGINE
257 /* If we have ENGINEs release them */
258 ENGINE_finish(pkey->engine);
260 ENGINE_finish(pkey->pmeth_engine);
261 pkey->pmeth_engine = NULL;
265 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
267 ameth = EVP_PKEY_asn1_find(eptr, type);
268 # ifndef OPENSSL_NO_ENGINE
269 if (pkey == NULL && eptr != NULL)
273 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
280 pkey->type = pkey->ameth->pkey_id;
281 pkey->save_type = type;
286 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
287 const unsigned char *priv,
290 EVP_PKEY *ret = EVP_PKEY_new();
293 || !pkey_set_type(ret, e, type, NULL, -1)) {
294 /* EVPerr already called */
298 if (ret->ameth->set_priv_key == NULL) {
299 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
300 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
304 if (!ret->ameth->set_priv_key(ret, priv, len)) {
305 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
316 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
317 const unsigned char *pub,
320 EVP_PKEY *ret = EVP_PKEY_new();
323 || !pkey_set_type(ret, e, type, NULL, -1)) {
324 /* EVPerr already called */
328 if (ret->ameth->set_pub_key == NULL) {
329 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
330 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
334 if (!ret->ameth->set_pub_key(ret, pub, len)) {
335 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
346 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
349 if (pkey->ameth->get_priv_key == NULL) {
350 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
351 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
355 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
356 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
363 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
366 if (pkey->ameth->get_pub_key == NULL) {
367 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
368 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
372 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
373 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
380 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
381 size_t len, const EVP_CIPHER *cipher)
383 # ifndef OPENSSL_NO_CMAC
384 # ifndef OPENSSL_NO_ENGINE
385 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
387 const char *cipher_name = EVP_CIPHER_name(cipher);
388 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
389 OPENSSL_CTX *libctx =
390 prov == NULL ? NULL : ossl_provider_library_context(prov);
391 EVP_PKEY *ret = EVP_PKEY_new();
392 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
393 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
394 OSSL_PARAM params[4];
399 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
400 /* EVPerr already called */
404 # ifndef OPENSSL_NO_ENGINE
405 if (engine_id != NULL)
407 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
411 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
412 (char *)cipher_name, 0);
414 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
416 params[paramsn] = OSSL_PARAM_construct_end();
418 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
419 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
423 ret->pkey.ptr = cmctx;
428 EVP_MAC_CTX_free(cmctx);
432 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
433 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
438 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
440 return pkey_set_type(pkey, NULL, type, NULL, -1);
443 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
445 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
448 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
450 if (pkey->type == type) {
451 return 1; /* it already is that type */
455 * The application is requesting to alias this to a different pkey type,
456 * but not one that resolves to the base type.
458 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
459 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
467 # ifndef OPENSSL_NO_ENGINE
468 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
471 if (!ENGINE_init(e)) {
472 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
475 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
477 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
481 ENGINE_finish(pkey->pmeth_engine);
482 pkey->pmeth_engine = e;
486 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
491 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
495 #ifndef OPENSSL_NO_EC
496 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
497 const EC_GROUP *group = EC_KEY_get0_group(key);
499 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
500 alias = EVP_PKEY_SM2;
504 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
506 if (!EVP_PKEY_set_alias_type(pkey, alias))
508 pkey->pkey.ptr = key;
509 return (key != NULL);
512 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
514 return pkey->pkey.ptr;
517 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
519 ASN1_OCTET_STRING *os = NULL;
520 if (pkey->type != EVP_PKEY_HMAC) {
521 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
524 os = EVP_PKEY_get0(pkey);
529 # ifndef OPENSSL_NO_POLY1305
530 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
532 ASN1_OCTET_STRING *os = NULL;
533 if (pkey->type != EVP_PKEY_POLY1305) {
534 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
537 os = EVP_PKEY_get0(pkey);
543 # ifndef OPENSSL_NO_SIPHASH
544 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
546 ASN1_OCTET_STRING *os = NULL;
548 if (pkey->type != EVP_PKEY_SIPHASH) {
549 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
552 os = EVP_PKEY_get0(pkey);
558 # ifndef OPENSSL_NO_RSA
559 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
561 int ret = EVP_PKEY_assign_RSA(pkey, key);
567 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
569 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
570 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
573 return pkey->pkey.rsa;
576 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
578 RSA *ret = EVP_PKEY_get0_RSA(pkey);
585 # ifndef OPENSSL_NO_DSA
586 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
588 int ret = EVP_PKEY_assign_DSA(pkey, key);
594 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
596 if (pkey->type != EVP_PKEY_DSA) {
597 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
600 return pkey->pkey.dsa;
603 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
605 DSA *ret = EVP_PKEY_get0_DSA(pkey);
612 # ifndef OPENSSL_NO_EC
614 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
616 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
622 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
624 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
625 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
628 return pkey->pkey.ec;
631 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
633 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
640 # ifndef OPENSSL_NO_DH
642 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
644 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
645 int ret = EVP_PKEY_assign(pkey, type, key);
652 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
654 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
655 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
658 return pkey->pkey.dh;
661 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
663 DH *ret = EVP_PKEY_get0_DH(pkey);
670 int EVP_PKEY_type(int type)
673 const EVP_PKEY_ASN1_METHOD *ameth;
675 ameth = EVP_PKEY_asn1_find(&e, type);
677 ret = ameth->pkey_id;
680 # ifndef OPENSSL_NO_ENGINE
686 int EVP_PKEY_id(const EVP_PKEY *pkey)
691 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
693 return EVP_PKEY_type(pkey->type);
697 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
699 BIO_set_indent(*out, saved_indent);
701 BIO *next = BIO_pop(*out);
709 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
715 long i = BIO_get_indent(*out);
717 *saved_indent = (i < 0 ? 0 : i);
718 if (BIO_set_indent(*out, indent) <= 0) {
719 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
723 if (BIO_set_indent(*out, indent) <= 0) {
724 print_reset_indent(out, *pop_f_prefix, *saved_indent);
731 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
734 return BIO_indent(out, indent, 128)
735 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
736 kstr, OBJ_nid2ln(pkey->type)) > 0;
739 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
740 const char *propquery /* For provided serialization */,
741 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
742 int indent, ASN1_PCTX *pctx),
743 ASN1_PCTX *legacy_pctx /* For legacy print */)
747 OSSL_SERIALIZER_CTX *ctx = NULL;
748 int ret = -2; /* default to unsupported */
750 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
753 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
754 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
755 ret = OSSL_SERIALIZER_to_bio(ctx, out);
756 OSSL_SERIALIZER_CTX_free(ctx);
761 /* legacy fallback */
762 if (legacy_print != NULL)
763 ret = legacy_print(out, pkey, 0, legacy_pctx);
765 ret = unsup_alg(out, pkey, 0, "Public Key");
768 print_reset_indent(&out, pop_f_prefix, saved_indent);
772 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
773 int indent, ASN1_PCTX *pctx)
775 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
776 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
780 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
781 int indent, ASN1_PCTX *pctx)
783 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
784 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
788 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
789 int indent, ASN1_PCTX *pctx)
791 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
792 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
796 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
797 int arg1, void *arg2)
799 if (pkey->keymgmt == NULL)
802 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
804 char mdname[80] = "";
806 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
811 nid = OBJ_sn2nid(mdname);
812 if (nid == NID_undef)
813 nid = OBJ_ln2nid(mdname);
814 if (nid == NID_undef)
824 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
826 if (pkey->ameth == NULL)
827 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
828 if (pkey->ameth->pkey_ctrl == NULL)
830 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
833 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
835 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
838 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
839 char *mdname, size_t mdname_sz)
841 if (pkey->ameth == NULL) {
842 OSSL_PARAM params[3];
843 char mddefault[100] = "";
844 char mdmandatory[100] = "";
847 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
848 mddefault, sizeof(mddefault));
850 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
852 sizeof(mdmandatory));
853 params[2] = OSSL_PARAM_construct_end();
854 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
856 if (mdmandatory[0] != '\0') {
857 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
860 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
866 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
867 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
870 OPENSSL_strlcpy(mdname, name, mdname_sz);
875 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
879 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
882 * If there is a mandatory default digest and this isn't it, then
883 * the answer is 'no'.
885 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
887 return (nid == default_nid);
888 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
895 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
896 const unsigned char *pt, size_t ptlen)
900 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
906 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
909 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
915 #endif /* FIPS_MODE */
917 /*- All methods below can also be used in FIPS_MODE */
919 EVP_PKEY *EVP_PKEY_new(void)
921 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
924 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
927 ret->type = EVP_PKEY_NONE;
928 ret->save_type = EVP_PKEY_NONE;
930 ret->save_parameters = 1;
931 ret->lock = CRYPTO_THREAD_lock_new();
932 if (ret->lock == NULL) {
933 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
940 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
944 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
947 REF_PRINT_COUNT("EVP_PKEY", pkey);
948 REF_ASSERT_ISNT(i < 2);
949 return ((i > 1) ? 1 : 0);
953 static void evp_pkey_free_legacy(EVP_PKEY *x)
955 if (x->ameth != NULL) {
956 if (x->ameth->pkey_free)
957 x->ameth->pkey_free(x);
961 # ifndef OPENSSL_NO_ENGINE
962 ENGINE_finish(x->engine);
964 ENGINE_finish(x->pmeth_engine);
965 x->pmeth_engine = NULL;
967 x->type = x->save_type = EVP_PKEY_NONE;
969 #endif /* FIPS_MODE */
971 static void evp_pkey_free_it(EVP_PKEY *x)
973 /* internal function; x is never NULL */
975 evp_keymgmt_util_clear_operation_cache(x);
977 evp_pkey_free_legacy(x);
980 if (x->keymgmt != NULL) {
981 evp_keymgmt_freedata(x->keymgmt, x->keydata);
982 EVP_KEYMGMT_free(x->keymgmt);
988 void EVP_PKEY_free(EVP_PKEY *x)
995 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
996 REF_PRINT_COUNT("EVP_PKEY", x);
999 REF_ASSERT_ISNT(i < 0);
1000 evp_pkey_free_it(x);
1001 CRYPTO_THREAD_lock_free(x->lock);
1003 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1008 int EVP_PKEY_size(const EVP_PKEY *pkey)
1011 if (pkey->ameth == NULL)
1012 return pkey->cache.size;
1013 else if (pkey->ameth->pkey_size != NULL)
1014 return pkey->ameth->pkey_size(pkey);
1019 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1020 EVP_KEYMGMT **keymgmt,
1021 const char *propquery)
1023 EVP_KEYMGMT *allocated_keymgmt = NULL;
1024 EVP_KEYMGMT *tmp_keymgmt = NULL;
1025 void *keydata = NULL;
1031 if (pk->pkey.ptr != NULL) {
1033 * If the legacy key doesn't have an dirty counter or export function,
1036 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1041 if (keymgmt != NULL) {
1042 tmp_keymgmt = *keymgmt;
1046 /* If no keymgmt was given or found, get a default keymgmt */
1047 if (tmp_keymgmt == NULL) {
1048 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1050 if (ctx != NULL && ctx->keytype != NULL)
1051 tmp_keymgmt = allocated_keymgmt =
1052 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
1053 EVP_PKEY_CTX_free(ctx);
1056 /* If there's still no keymgmt to be had, give up */
1057 if (tmp_keymgmt == NULL)
1061 if (pk->pkey.ptr != NULL) {
1065 * If the legacy "origin" hasn't changed since last time, we try
1066 * to find our keymgmt in the operation cache. If it has changed,
1067 * |i| remains zero, and we will clear the cache further down.
1069 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1070 i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
1073 * If |tmp_keymgmt| is present in the operation cache, it means
1074 * that export doesn't need to be redone. In that case, we take
1075 * token copies of the cached pointers, to have token success
1078 if (i < OSSL_NELEM(pk->operation_cache)
1079 && pk->operation_cache[i].keymgmt != NULL) {
1080 keydata = pk->operation_cache[i].keydata;
1086 * TODO(3.0) Right now, we assume we have ample space. We will have
1087 * to think about a cache aging scheme, though, if |i| indexes outside
1090 if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
1093 /* Make sure that the keymgmt key type matches the legacy NID */
1094 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1097 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1100 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) {
1101 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1107 * If the dirty counter changed since last time, then clear the
1108 * operation cache. In that case, we know that |i| is zero. Just
1109 * in case this is a re-export, we increment then decrement the
1110 * keymgmt reference counter.
1112 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1113 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1117 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1118 evp_keymgmt_util_clear_operation_cache(pk);
1119 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1121 /* Add the new export to the operation cache */
1122 if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
1123 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1128 /* Synchronize the dirty count */
1129 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1132 #endif /* FIPS_MODE */
1134 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1138 * If nothing was exported, |tmp_keymgmt| might point at a freed
1139 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1140 * the caller either way in that case.
1142 if (keydata == NULL)
1145 if (keymgmt != NULL)
1146 *keymgmt = tmp_keymgmt;
1148 EVP_KEYMGMT_free(allocated_keymgmt);
1154 * This differs from exporting in that it releases the legacy key and assigns
1155 * the export keymgmt and keydata to the "origin" provider side key instead
1156 * of the operation cache.
1158 void *evp_pkey_upgrade_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1159 EVP_KEYMGMT **keymgmt,
1160 const char *propquery)
1162 EVP_KEYMGMT *allocated_keymgmt = NULL;
1163 EVP_KEYMGMT *tmp_keymgmt = NULL;
1164 void *keydata = NULL;
1170 * If this key is already "upgraded", this function shouldn't have been
1173 if (!ossl_assert(pk->keymgmt == NULL))
1176 if (keymgmt != NULL) {
1177 tmp_keymgmt = *keymgmt;
1181 /* If the key isn't a legacy one, bail out, but with proper values */
1182 if (pk->pkey.ptr == NULL) {
1183 tmp_keymgmt = pk->keymgmt;
1184 keydata = pk->keydata;
1186 /* If the legacy key doesn't have an export function, give up */
1187 if (pk->ameth->export_to == NULL)
1190 /* If no keymgmt was given, get a default keymgmt */
1191 if (tmp_keymgmt == NULL) {
1193 EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1195 if (ctx != NULL && ctx->keytype != NULL)
1196 tmp_keymgmt = allocated_keymgmt =
1197 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
1198 EVP_PKEY_CTX_free(ctx);
1201 /* If we still don't have a keymgmt, give up */
1202 if (tmp_keymgmt == NULL)
1205 /* Make sure that the keymgmt key type matches the legacy NID */
1206 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1209 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1212 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)
1213 || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
1214 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1220 * Clear the operation cache, all the legacy data, as well as the
1223 evp_pkey_free_legacy(pk);
1224 pk->dirty_cnt_copy = 0;
1226 evp_keymgmt_util_clear_operation_cache(pk);
1227 pk->keymgmt = tmp_keymgmt;
1228 pk->keydata = keydata;
1229 evp_keymgmt_util_cache_keyinfo(pk);
1234 * If nothing was upgraded, |tmp_keymgmt| might point at a freed
1235 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1236 * the caller either way in that case.
1238 if (keydata == NULL)
1241 if (keymgmt != NULL)
1242 *keymgmt = tmp_keymgmt;
1244 EVP_KEYMGMT_free(allocated_keymgmt);
1247 #endif /* FIPS_MODE */