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)
117 if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing)
118 return pkey->ameth->param_missing(pkey);
122 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
124 if (a->type != b->type)
126 if (a->ameth && a->ameth->param_cmp)
127 return a->ameth->param_cmp(a, b);
131 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
133 if (a->type != b->type)
138 /* Compare parameters if the algorithm has them */
139 if (a->ameth->param_cmp) {
140 ret = a->ameth->param_cmp(a, b);
145 if (a->ameth->pub_cmp)
146 return a->ameth->pub_cmp(a, b);
154 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
155 * is NULL just return 1 or 0 if the algorithm exists.
158 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
161 const EVP_PKEY_ASN1_METHOD *ameth;
162 ENGINE **eptr = (e == NULL) ? &e : NULL;
166 evp_pkey_free_it(pkey);
168 * If key type matches and a method exists then this lookup has
169 * succeeded once so just indicate success.
171 if ((type == pkey->save_type) && pkey->ameth)
173 # ifndef OPENSSL_NO_ENGINE
174 /* If we have ENGINEs release them */
175 ENGINE_finish(pkey->engine);
177 ENGINE_finish(pkey->pmeth_engine);
178 pkey->pmeth_engine = NULL;
182 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
184 ameth = EVP_PKEY_asn1_find(eptr, type);
185 # ifndef OPENSSL_NO_ENGINE
186 if (pkey == NULL && eptr != NULL)
190 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
197 pkey->type = pkey->ameth->pkey_id;
198 pkey->save_type = type;
203 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
204 const unsigned char *priv,
207 EVP_PKEY *ret = EVP_PKEY_new();
210 || !pkey_set_type(ret, e, type, NULL, -1)) {
211 /* EVPerr already called */
215 if (ret->ameth->set_priv_key == NULL) {
216 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
217 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
221 if (!ret->ameth->set_priv_key(ret, priv, len)) {
222 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
233 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
234 const unsigned char *pub,
237 EVP_PKEY *ret = EVP_PKEY_new();
240 || !pkey_set_type(ret, e, type, NULL, -1)) {
241 /* EVPerr already called */
245 if (ret->ameth->set_pub_key == NULL) {
246 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
247 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
251 if (!ret->ameth->set_pub_key(ret, pub, len)) {
252 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
263 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
266 if (pkey->ameth->get_priv_key == NULL) {
267 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
268 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
272 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
273 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
280 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
283 if (pkey->ameth->get_pub_key == NULL) {
284 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
285 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
289 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
290 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
297 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
298 size_t len, const EVP_CIPHER *cipher)
300 # ifndef OPENSSL_NO_CMAC
301 # ifndef OPENSSL_NO_ENGINE
302 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
304 const char *cipher_name = EVP_CIPHER_name(cipher);
305 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
306 OPENSSL_CTX *libctx =
307 prov == NULL ? NULL : ossl_provider_library_context(prov);
308 EVP_PKEY *ret = EVP_PKEY_new();
309 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
310 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
311 OSSL_PARAM params[4];
316 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
317 /* EVPerr already called */
321 # ifndef OPENSSL_NO_ENGINE
322 if (engine_id != NULL)
324 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
328 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
329 (char *)cipher_name, 0);
331 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
333 params[paramsn] = OSSL_PARAM_construct_end();
335 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
336 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
340 ret->pkey.ptr = cmctx;
345 EVP_MAC_CTX_free(cmctx);
349 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
350 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
355 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
357 return pkey_set_type(pkey, NULL, type, NULL, -1);
360 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
362 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
365 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
367 if (pkey->type == type) {
368 return 1; /* it already is that type */
372 * The application is requesting to alias this to a different pkey type,
373 * but not one that resolves to the base type.
375 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
376 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
384 # ifndef OPENSSL_NO_ENGINE
385 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
388 if (!ENGINE_init(e)) {
389 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
392 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
394 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
398 ENGINE_finish(pkey->pmeth_engine);
399 pkey->pmeth_engine = e;
403 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
408 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
412 #ifndef OPENSSL_NO_EC
413 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
414 const EC_GROUP *group = EC_KEY_get0_group(key);
416 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
417 alias = EVP_PKEY_SM2;
421 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
423 if (!EVP_PKEY_set_alias_type(pkey, alias))
425 pkey->pkey.ptr = key;
426 return (key != NULL);
429 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
431 return pkey->pkey.ptr;
434 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
436 ASN1_OCTET_STRING *os = NULL;
437 if (pkey->type != EVP_PKEY_HMAC) {
438 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
441 os = EVP_PKEY_get0(pkey);
446 # ifndef OPENSSL_NO_POLY1305
447 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
449 ASN1_OCTET_STRING *os = NULL;
450 if (pkey->type != EVP_PKEY_POLY1305) {
451 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
454 os = EVP_PKEY_get0(pkey);
460 # ifndef OPENSSL_NO_SIPHASH
461 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
463 ASN1_OCTET_STRING *os = NULL;
465 if (pkey->type != EVP_PKEY_SIPHASH) {
466 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
469 os = EVP_PKEY_get0(pkey);
475 # ifndef OPENSSL_NO_RSA
476 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
478 int ret = EVP_PKEY_assign_RSA(pkey, key);
484 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
486 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
487 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
490 return pkey->pkey.rsa;
493 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
495 RSA *ret = EVP_PKEY_get0_RSA(pkey);
502 # ifndef OPENSSL_NO_DSA
503 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
505 int ret = EVP_PKEY_assign_DSA(pkey, key);
511 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
513 if (pkey->type != EVP_PKEY_DSA) {
514 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
517 return pkey->pkey.dsa;
520 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
522 DSA *ret = EVP_PKEY_get0_DSA(pkey);
529 # ifndef OPENSSL_NO_EC
531 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
533 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
539 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
541 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
542 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
545 return pkey->pkey.ec;
548 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
550 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
557 # ifndef OPENSSL_NO_DH
559 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
561 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
562 int ret = EVP_PKEY_assign(pkey, type, key);
569 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
571 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
572 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
575 return pkey->pkey.dh;
578 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
580 DH *ret = EVP_PKEY_get0_DH(pkey);
587 int EVP_PKEY_type(int type)
590 const EVP_PKEY_ASN1_METHOD *ameth;
592 ameth = EVP_PKEY_asn1_find(&e, type);
594 ret = ameth->pkey_id;
597 # ifndef OPENSSL_NO_ENGINE
603 int EVP_PKEY_id(const EVP_PKEY *pkey)
608 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
610 return EVP_PKEY_type(pkey->type);
614 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
616 BIO_set_indent(*out, saved_indent);
618 BIO *next = BIO_pop(*out);
626 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
632 long i = BIO_get_indent(*out);
634 *saved_indent = (i < 0 ? 0 : i);
635 if (BIO_set_indent(*out, indent) <= 0) {
636 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
640 if (BIO_set_indent(*out, indent) <= 0) {
641 print_reset_indent(out, *pop_f_prefix, *saved_indent);
648 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
651 return BIO_indent(out, indent, 128)
652 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
653 kstr, OBJ_nid2ln(pkey->type)) > 0;
656 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
657 const char *propquery /* For provided serialization */,
658 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
659 int indent, ASN1_PCTX *pctx),
660 ASN1_PCTX *legacy_pctx /* For legacy print */)
664 OSSL_SERIALIZER_CTX *ctx = NULL;
665 int ret = -2; /* default to unsupported */
667 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
670 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
671 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
672 ret = OSSL_SERIALIZER_to_bio(ctx, out);
673 OSSL_SERIALIZER_CTX_free(ctx);
678 /* legacy fallback */
679 if (legacy_print != NULL)
680 ret = legacy_print(out, pkey, 0, legacy_pctx);
682 ret = unsup_alg(out, pkey, 0, "Public Key");
685 print_reset_indent(&out, pop_f_prefix, saved_indent);
689 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
690 int indent, ASN1_PCTX *pctx)
692 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
693 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
697 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
698 int indent, ASN1_PCTX *pctx)
700 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
701 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
705 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
706 int indent, ASN1_PCTX *pctx)
708 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
709 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
713 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
714 int arg1, void *arg2)
716 if (pkey->pkeys[0].keymgmt == NULL)
719 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
721 char mdname[80] = "";
723 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
728 nid = OBJ_sn2nid(mdname);
729 if (nid == NID_undef)
730 nid = OBJ_ln2nid(mdname);
731 if (nid == NID_undef)
741 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
743 if (pkey->ameth == NULL)
744 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
745 if (pkey->ameth->pkey_ctrl == NULL)
747 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
750 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
752 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
755 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
756 char *mdname, size_t mdname_sz)
758 if (pkey->ameth == NULL) {
759 OSSL_PARAM params[3];
760 char mddefault[100] = "";
761 char mdmandatory[100] = "";
764 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
765 mddefault, sizeof(mddefault));
767 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
769 sizeof(mdmandatory));
770 params[2] = OSSL_PARAM_construct_end();
771 if (!evp_keymgmt_get_params(pkey->pkeys[0].keymgmt,
772 pkey->pkeys[0].keydata,
775 if (mdmandatory[0] != '\0') {
776 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
779 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
785 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
786 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
789 OPENSSL_strlcpy(mdname, name, mdname_sz);
794 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
798 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
801 * If there is a mandatory default digest and this isn't it, then
802 * the answer is 'no'.
804 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
806 return (nid == default_nid);
807 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
814 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
815 const unsigned char *pt, size_t ptlen)
819 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
825 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
828 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
834 #endif /* FIPS_MODE */
836 /*- All methods below can also be used in FIPS_MODE */
838 EVP_PKEY *EVP_PKEY_new(void)
840 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
843 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
846 ret->type = EVP_PKEY_NONE;
847 ret->save_type = EVP_PKEY_NONE;
849 ret->save_parameters = 1;
850 ret->lock = CRYPTO_THREAD_lock_new();
851 if (ret->lock == NULL) {
852 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
859 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
863 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
866 REF_PRINT_COUNT("EVP_PKEY", pkey);
867 REF_ASSERT_ISNT(i < 2);
868 return ((i > 1) ? 1 : 0);
871 static void evp_pkey_free_it(EVP_PKEY *x)
873 /* internal function; x is never NULL */
875 evp_keymgmt_util_clear_pkey_cache(x);
877 if (x->ameth && x->ameth->pkey_free) {
878 x->ameth->pkey_free(x);
881 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
882 ENGINE_finish(x->engine);
884 ENGINE_finish(x->pmeth_engine);
885 x->pmeth_engine = NULL;
889 void EVP_PKEY_free(EVP_PKEY *x)
896 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
897 REF_PRINT_COUNT("EVP_PKEY", x);
900 REF_ASSERT_ISNT(i < 0);
902 CRYPTO_THREAD_lock_free(x->lock);
904 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
909 int EVP_PKEY_size(const EVP_PKEY *pkey)
912 if (pkey->ameth == NULL)
913 return pkey->cache.size;
914 else if (pkey->ameth->pkey_size != NULL)
915 return pkey->ameth->pkey_size(pkey);
920 void *evp_pkey_make_provided(EVP_PKEY *pk, OPENSSL_CTX *libctx,
921 EVP_KEYMGMT **keymgmt, const char *propquery)
923 EVP_KEYMGMT *allocated_keymgmt = NULL;
924 EVP_KEYMGMT *tmp_keymgmt = NULL;
925 void *keydata = NULL;
930 if (keymgmt != NULL) {
931 tmp_keymgmt = *keymgmt;
935 if (tmp_keymgmt == NULL) {
936 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
938 if (ctx != NULL && ctx->keytype != NULL)
939 tmp_keymgmt = allocated_keymgmt =
940 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
941 EVP_PKEY_CTX_free(ctx);
944 if (tmp_keymgmt != NULL)
946 evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
949 * If nothing was exported, |tmp_keymgmt| might point at a freed
950 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
951 * the caller either way in that case.
957 *keymgmt = tmp_keymgmt;
959 EVP_KEYMGMT_free(allocated_keymgmt);