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 #include "internal/cryptlib.h"
12 #include "internal/refcount.h"
13 #include <openssl/bn.h>
14 #include <openssl/err.h>
15 #include <openssl/objects.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509.h>
18 #include <openssl/rsa.h>
19 #include <openssl/dsa.h>
20 #include <openssl/dh.h>
21 #include <openssl/cmac.h>
22 #include <openssl/engine.h>
23 #include <openssl/params.h>
24 #include <openssl/serializer.h>
25 #include <openssl/core_names.h>
27 #include "crypto/asn1.h"
28 #include "crypto/evp.h"
29 #include "internal/provider.h"
30 #include "evp_local.h"
32 static void evp_pkey_free_it(EVP_PKEY *key);
36 int EVP_PKEY_bits(const EVP_PKEY *pkey)
39 if (pkey->ameth == NULL)
40 return pkey->cache.bits;
41 else if (pkey->ameth->pkey_bits)
42 return pkey->ameth->pkey_bits(pkey);
47 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
51 if (pkey->ameth == NULL)
52 return pkey->cache.security_bits;
53 if (pkey->ameth->pkey_security_bits == NULL)
55 return pkey->ameth->pkey_security_bits(pkey);
58 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
60 # ifndef OPENSSL_NO_DSA
61 if (pkey->type == EVP_PKEY_DSA) {
62 int ret = pkey->save_parameters;
65 pkey->save_parameters = mode;
69 # ifndef OPENSSL_NO_EC
70 if (pkey->type == EVP_PKEY_EC) {
71 int ret = pkey->save_parameters;
74 pkey->save_parameters = mode;
81 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
83 if (to->type == EVP_PKEY_NONE) {
84 if (EVP_PKEY_set_type(to, from->type) == 0)
86 } else if (to->type != from->type) {
87 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
91 if (EVP_PKEY_missing_parameters(from)) {
92 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
96 if (!EVP_PKEY_missing_parameters(to)) {
97 if (EVP_PKEY_cmp_parameters(to, from) == 1)
99 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
103 if (from->ameth && from->ameth->param_copy)
104 return from->ameth->param_copy(to, from);
109 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
111 if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing)
112 return pkey->ameth->param_missing(pkey);
116 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
118 if (a->type != b->type)
120 if (a->ameth && a->ameth->param_cmp)
121 return a->ameth->param_cmp(a, b);
125 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
127 if (a->type != b->type)
132 /* Compare parameters if the algorithm has them */
133 if (a->ameth->param_cmp) {
134 ret = a->ameth->param_cmp(a, b);
139 if (a->ameth->pub_cmp)
140 return a->ameth->pub_cmp(a, b);
148 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
149 * is NULL just return 1 or 0 if the algorithm exists.
152 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
155 const EVP_PKEY_ASN1_METHOD *ameth;
156 ENGINE **eptr = (e == NULL) ? &e : NULL;
160 evp_pkey_free_it(pkey);
162 * If key type matches and a method exists then this lookup has
163 * succeeded once so just indicate success.
165 if ((type == pkey->save_type) && pkey->ameth)
167 # ifndef OPENSSL_NO_ENGINE
168 /* If we have ENGINEs release them */
169 ENGINE_finish(pkey->engine);
171 ENGINE_finish(pkey->pmeth_engine);
172 pkey->pmeth_engine = NULL;
176 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
178 ameth = EVP_PKEY_asn1_find(eptr, type);
179 # ifndef OPENSSL_NO_ENGINE
180 if (pkey == NULL && eptr != NULL)
184 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
191 pkey->type = pkey->ameth->pkey_id;
192 pkey->save_type = type;
197 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
198 const unsigned char *priv,
201 EVP_PKEY *ret = EVP_PKEY_new();
204 || !pkey_set_type(ret, e, type, NULL, -1)) {
205 /* EVPerr already called */
209 if (ret->ameth->set_priv_key == NULL) {
210 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
211 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
215 if (!ret->ameth->set_priv_key(ret, priv, len)) {
216 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
227 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
228 const unsigned char *pub,
231 EVP_PKEY *ret = EVP_PKEY_new();
234 || !pkey_set_type(ret, e, type, NULL, -1)) {
235 /* EVPerr already called */
239 if (ret->ameth->set_pub_key == NULL) {
240 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
241 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
245 if (!ret->ameth->set_pub_key(ret, pub, len)) {
246 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
257 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
260 if (pkey->ameth->get_priv_key == NULL) {
261 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
262 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
266 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
267 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
274 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
277 if (pkey->ameth->get_pub_key == NULL) {
278 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
279 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
283 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
284 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
291 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
292 size_t len, const EVP_CIPHER *cipher)
294 # ifndef OPENSSL_NO_CMAC
295 # ifndef OPENSSL_NO_ENGINE
296 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
298 const char *cipher_name = EVP_CIPHER_name(cipher);
299 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
300 OPENSSL_CTX *libctx =
301 prov == NULL ? NULL : ossl_provider_library_context(prov);
302 EVP_PKEY *ret = EVP_PKEY_new();
303 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
304 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
305 OSSL_PARAM params[4];
310 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
311 /* EVPerr already called */
315 # ifndef OPENSSL_NO_ENGINE
316 if (engine_id != NULL)
318 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
322 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
323 (char *)cipher_name, 0);
325 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
327 params[paramsn] = OSSL_PARAM_construct_end();
329 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
330 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
334 ret->pkey.ptr = cmctx;
339 EVP_MAC_CTX_free(cmctx);
343 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
344 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
349 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
351 return pkey_set_type(pkey, NULL, type, NULL, -1);
354 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
356 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
359 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
361 if (pkey->type == type) {
362 return 1; /* it already is that type */
366 * The application is requesting to alias this to a different pkey type,
367 * but not one that resolves to the base type.
369 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
370 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
378 # ifndef OPENSSL_NO_ENGINE
379 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
382 if (!ENGINE_init(e)) {
383 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
386 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
388 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
392 ENGINE_finish(pkey->pmeth_engine);
393 pkey->pmeth_engine = e;
397 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
402 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
406 #ifndef OPENSSL_NO_EC
407 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
408 const EC_GROUP *group = EC_KEY_get0_group(key);
410 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
411 alias = EVP_PKEY_SM2;
415 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
417 if (!EVP_PKEY_set_alias_type(pkey, alias))
419 pkey->pkey.ptr = key;
420 return (key != NULL);
423 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
425 return pkey->pkey.ptr;
428 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
430 ASN1_OCTET_STRING *os = NULL;
431 if (pkey->type != EVP_PKEY_HMAC) {
432 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
435 os = EVP_PKEY_get0(pkey);
440 # ifndef OPENSSL_NO_POLY1305
441 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
443 ASN1_OCTET_STRING *os = NULL;
444 if (pkey->type != EVP_PKEY_POLY1305) {
445 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
448 os = EVP_PKEY_get0(pkey);
454 # ifndef OPENSSL_NO_SIPHASH
455 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
457 ASN1_OCTET_STRING *os = NULL;
459 if (pkey->type != EVP_PKEY_SIPHASH) {
460 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
463 os = EVP_PKEY_get0(pkey);
469 # ifndef OPENSSL_NO_RSA
470 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
472 int ret = EVP_PKEY_assign_RSA(pkey, key);
478 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
480 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
481 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
484 return pkey->pkey.rsa;
487 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
489 RSA *ret = EVP_PKEY_get0_RSA(pkey);
496 # ifndef OPENSSL_NO_DSA
497 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
499 int ret = EVP_PKEY_assign_DSA(pkey, key);
505 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
507 if (pkey->type != EVP_PKEY_DSA) {
508 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
511 return pkey->pkey.dsa;
514 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
516 DSA *ret = EVP_PKEY_get0_DSA(pkey);
523 # ifndef OPENSSL_NO_EC
525 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
527 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
533 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
535 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
536 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
539 return pkey->pkey.ec;
542 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
544 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
551 # ifndef OPENSSL_NO_DH
553 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
555 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
556 int ret = EVP_PKEY_assign(pkey, type, key);
563 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
565 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
566 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
569 return pkey->pkey.dh;
572 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
574 DH *ret = EVP_PKEY_get0_DH(pkey);
581 int EVP_PKEY_type(int type)
584 const EVP_PKEY_ASN1_METHOD *ameth;
586 ameth = EVP_PKEY_asn1_find(&e, type);
588 ret = ameth->pkey_id;
591 # ifndef OPENSSL_NO_ENGINE
597 int EVP_PKEY_id(const EVP_PKEY *pkey)
602 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
604 return EVP_PKEY_type(pkey->type);
608 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
610 BIO_set_indent(*out, saved_indent);
612 BIO *next = BIO_pop(*out);
620 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
626 long i = BIO_get_indent(*out);
628 *saved_indent = (i < 0 ? 0 : i);
629 if (BIO_set_indent(*out, indent) <= 0) {
630 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
634 if (BIO_set_indent(*out, indent) <= 0) {
635 print_reset_indent(out, *pop_f_prefix, *saved_indent);
642 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
645 return BIO_indent(out, indent, 128)
646 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
647 kstr, OBJ_nid2ln(pkey->type)) > 0;
650 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
651 const char *propquery /* For provided serialization */,
652 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
653 int indent, ASN1_PCTX *pctx),
654 ASN1_PCTX *legacy_pctx /* For legacy print */)
658 OSSL_SERIALIZER_CTX *ctx = NULL;
659 int ret = -2; /* default to unsupported */
661 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
664 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
665 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
666 ret = OSSL_SERIALIZER_to_bio(ctx, out);
667 OSSL_SERIALIZER_CTX_free(ctx);
672 /* legacy fallback */
673 if (legacy_print != NULL)
674 ret = legacy_print(out, pkey, 0, legacy_pctx);
676 ret = unsup_alg(out, pkey, 0, "Public Key");
679 print_reset_indent(&out, pop_f_prefix, saved_indent);
683 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
684 int indent, ASN1_PCTX *pctx)
686 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
687 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
691 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
692 int indent, ASN1_PCTX *pctx)
694 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
695 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
699 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
700 int indent, ASN1_PCTX *pctx)
702 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
703 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
707 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
708 int arg1, void *arg2)
710 if (pkey->pkeys[0].keymgmt == NULL)
713 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
715 char mdname[80] = "";
717 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
722 nid = OBJ_sn2nid(mdname);
723 if (nid == NID_undef)
724 nid = OBJ_ln2nid(mdname);
725 if (nid == NID_undef)
735 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
737 if (pkey->ameth == NULL)
738 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
739 if (pkey->ameth->pkey_ctrl == NULL)
741 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
744 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
746 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
749 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
750 char *mdname, size_t mdname_sz)
752 if (pkey->ameth == NULL) {
753 OSSL_PARAM params[3];
754 char mddefault[100] = "";
755 char mdmandatory[100] = "";
758 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
759 mddefault, sizeof(mddefault));
761 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
763 sizeof(mdmandatory));
764 params[2] = OSSL_PARAM_construct_end();
765 if (!evp_keymgmt_get_params(pkey->pkeys[0].keymgmt,
766 pkey->pkeys[0].keydata,
769 if (mdmandatory[0] != '\0') {
770 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
773 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
779 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
780 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
783 OPENSSL_strlcpy(mdname, name, mdname_sz);
788 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
792 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
795 * If there is a mandatory default digest and this isn't it, then
796 * the answer is 'no'.
798 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
800 return (nid == default_nid);
801 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
808 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
809 const unsigned char *pt, size_t ptlen)
813 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
819 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
822 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
828 #endif /* FIPS_MODE */
830 /*- All methods below can also be used in FIPS_MODE */
832 EVP_PKEY *EVP_PKEY_new(void)
834 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
837 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
840 ret->type = EVP_PKEY_NONE;
841 ret->save_type = EVP_PKEY_NONE;
843 ret->save_parameters = 1;
844 ret->lock = CRYPTO_THREAD_lock_new();
845 if (ret->lock == NULL) {
846 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
853 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
857 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
860 REF_PRINT_COUNT("EVP_PKEY", pkey);
861 REF_ASSERT_ISNT(i < 2);
862 return ((i > 1) ? 1 : 0);
865 static void evp_pkey_free_it(EVP_PKEY *x)
867 /* internal function; x is never NULL */
869 evp_keymgmt_util_clear_pkey_cache(x);
871 if (x->ameth && x->ameth->pkey_free) {
872 x->ameth->pkey_free(x);
875 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
876 ENGINE_finish(x->engine);
878 ENGINE_finish(x->pmeth_engine);
879 x->pmeth_engine = NULL;
883 void EVP_PKEY_free(EVP_PKEY *x)
890 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
891 REF_PRINT_COUNT("EVP_PKEY", x);
894 REF_ASSERT_ISNT(i < 0);
896 CRYPTO_THREAD_lock_free(x->lock);
898 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
903 int EVP_PKEY_size(const EVP_PKEY *pkey)
906 if (pkey->ameth == NULL)
907 return pkey->cache.size;
908 else if (pkey->ameth->pkey_size != NULL)
909 return pkey->ameth->pkey_size(pkey);
914 void *evp_pkey_make_provided(EVP_PKEY *pk, OPENSSL_CTX *libctx,
915 EVP_KEYMGMT **keymgmt, const char *propquery)
917 EVP_KEYMGMT *allocated_keymgmt = NULL;
918 EVP_KEYMGMT *tmp_keymgmt = NULL;
919 void *keydata = NULL;
924 if (keymgmt != NULL) {
925 tmp_keymgmt = *keymgmt;
929 if (tmp_keymgmt == NULL) {
930 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
932 if (ctx != NULL && ctx->keytype != NULL)
933 tmp_keymgmt = allocated_keymgmt =
934 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
935 EVP_PKEY_CTX_free(ctx);
938 if (tmp_keymgmt != NULL)
940 evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
943 * If nothing was exported, |tmp_keymgmt| might point at a freed
944 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
945 * the caller either way in that case.
951 *keymgmt = tmp_keymgmt;
953 EVP_KEYMGMT_free(allocated_keymgmt);