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"
31 static void evp_pkey_free_it(EVP_PKEY
*key
);
35 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
38 if (pkey
->ameth
== NULL
)
39 return pkey
->cache
.bits
;
40 else if (pkey
->ameth
->pkey_bits
)
41 return pkey
->ameth
->pkey_bits(pkey
);
46 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
50 if (pkey
->ameth
== NULL
)
51 return pkey
->cache
.security_bits
;
52 if (pkey
->ameth
->pkey_security_bits
== NULL
)
54 return pkey
->ameth
->pkey_security_bits(pkey
);
57 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
59 # ifndef OPENSSL_NO_DSA
60 if (pkey
->type
== EVP_PKEY_DSA
) {
61 int ret
= pkey
->save_parameters
;
64 pkey
->save_parameters
= mode
;
68 # ifndef OPENSSL_NO_EC
69 if (pkey
->type
== EVP_PKEY_EC
) {
70 int ret
= pkey
->save_parameters
;
73 pkey
->save_parameters
= mode
;
80 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
82 if (to
->type
== EVP_PKEY_NONE
) {
83 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
85 } else if (to
->type
!= from
->type
) {
86 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_KEY_TYPES
);
90 if (EVP_PKEY_missing_parameters(from
)) {
91 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_MISSING_PARAMETERS
);
95 if (!EVP_PKEY_missing_parameters(to
)) {
96 if (EVP_PKEY_cmp_parameters(to
, from
) == 1)
98 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_PARAMETERS
);
102 if (from
->ameth
&& from
->ameth
->param_copy
)
103 return from
->ameth
->param_copy(to
, from
);
108 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
110 if (pkey
!= NULL
&& pkey
->ameth
&& pkey
->ameth
->param_missing
)
111 return pkey
->ameth
->param_missing(pkey
);
115 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
117 if (a
->type
!= b
->type
)
119 if (a
->ameth
&& a
->ameth
->param_cmp
)
120 return a
->ameth
->param_cmp(a
, b
);
124 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
126 if (a
->type
!= b
->type
)
131 /* Compare parameters if the algorithm has them */
132 if (a
->ameth
->param_cmp
) {
133 ret
= a
->ameth
->param_cmp(a
, b
);
138 if (a
->ameth
->pub_cmp
)
139 return a
->ameth
->pub_cmp(a
, b
);
147 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
148 * is NULL just return 1 or 0 if the algorithm exists.
151 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
154 const EVP_PKEY_ASN1_METHOD
*ameth
;
155 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
159 evp_pkey_free_it(pkey
);
161 * If key type matches and a method exists then this lookup has
162 * succeeded once so just indicate success.
164 if ((type
== pkey
->save_type
) && pkey
->ameth
)
166 # ifndef OPENSSL_NO_ENGINE
167 /* If we have ENGINEs release them */
168 ENGINE_finish(pkey
->engine
);
170 ENGINE_finish(pkey
->pmeth_engine
);
171 pkey
->pmeth_engine
= NULL
;
175 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
177 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
178 # ifndef OPENSSL_NO_ENGINE
179 if (pkey
== NULL
&& eptr
!= NULL
)
183 EVPerr(EVP_F_PKEY_SET_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
190 pkey
->type
= pkey
->ameth
->pkey_id
;
191 pkey
->save_type
= type
;
196 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
197 const unsigned char *priv
,
200 EVP_PKEY
*ret
= EVP_PKEY_new();
203 || !pkey_set_type(ret
, e
, type
, NULL
, -1)) {
204 /* EVPerr already called */
208 if (ret
->ameth
->set_priv_key
== NULL
) {
209 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
,
210 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
214 if (!ret
->ameth
->set_priv_key(ret
, priv
, len
)) {
215 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
, EVP_R_KEY_SETUP_FAILED
);
226 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
227 const unsigned char *pub
,
230 EVP_PKEY
*ret
= EVP_PKEY_new();
233 || !pkey_set_type(ret
, e
, type
, NULL
, -1)) {
234 /* EVPerr already called */
238 if (ret
->ameth
->set_pub_key
== NULL
) {
239 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
,
240 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
244 if (!ret
->ameth
->set_pub_key(ret
, pub
, len
)) {
245 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
, EVP_R_KEY_SETUP_FAILED
);
256 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
259 if (pkey
->ameth
->get_priv_key
== NULL
) {
260 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
,
261 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
265 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
266 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
273 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
276 if (pkey
->ameth
->get_pub_key
== NULL
) {
277 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
,
278 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
282 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
283 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
290 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
291 size_t len
, const EVP_CIPHER
*cipher
)
293 # ifndef OPENSSL_NO_CMAC
294 # ifndef OPENSSL_NO_ENGINE
295 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
297 const char *cipher_name
= EVP_CIPHER_name(cipher
);
298 const OSSL_PROVIDER
*prov
= EVP_CIPHER_provider(cipher
);
299 OPENSSL_CTX
*libctx
=
300 prov
== NULL
? NULL
: ossl_provider_library_context(prov
);
301 EVP_PKEY
*ret
= EVP_PKEY_new();
302 EVP_MAC
*cmac
= EVP_MAC_fetch(libctx
, OSSL_MAC_NAME_CMAC
, NULL
);
303 EVP_MAC_CTX
*cmctx
= cmac
!= NULL
? EVP_MAC_CTX_new(cmac
) : NULL
;
304 OSSL_PARAM params
[4];
309 || !pkey_set_type(ret
, e
, EVP_PKEY_CMAC
, NULL
, -1)) {
310 /* EVPerr already called */
314 # ifndef OPENSSL_NO_ENGINE
315 if (engine_id
!= NULL
)
317 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id
, 0);
321 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER
,
322 (char *)cipher_name
, 0);
324 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
326 params
[paramsn
] = OSSL_PARAM_construct_end();
328 if (!EVP_MAC_CTX_set_params(cmctx
, params
)) {
329 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
, EVP_R_KEY_SETUP_FAILED
);
333 ret
->pkey
.ptr
= cmctx
;
338 EVP_MAC_CTX_free(cmctx
);
342 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
,
343 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
348 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
350 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1);
353 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
355 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
);
358 int EVP_PKEY_set_alias_type(EVP_PKEY
*pkey
, int type
)
360 if (pkey
->type
== type
) {
361 return 1; /* it already is that type */
365 * The application is requesting to alias this to a different pkey type,
366 * but not one that resolves to the base type.
368 if (EVP_PKEY_type(type
) != EVP_PKEY_base_id(pkey
)) {
369 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
377 # ifndef OPENSSL_NO_ENGINE
378 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
381 if (!ENGINE_init(e
)) {
382 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, ERR_R_ENGINE_LIB
);
385 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
387 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, EVP_R_UNSUPPORTED_ALGORITHM
);
391 ENGINE_finish(pkey
->pmeth_engine
);
392 pkey
->pmeth_engine
= e
;
396 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
401 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
403 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
405 pkey
->pkey
.ptr
= key
;
406 return (key
!= NULL
);
409 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
411 return pkey
->pkey
.ptr
;
414 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
416 ASN1_OCTET_STRING
*os
= NULL
;
417 if (pkey
->type
!= EVP_PKEY_HMAC
) {
418 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC
, EVP_R_EXPECTING_AN_HMAC_KEY
);
421 os
= EVP_PKEY_get0(pkey
);
426 # ifndef OPENSSL_NO_POLY1305
427 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
429 ASN1_OCTET_STRING
*os
= NULL
;
430 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
431 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305
, EVP_R_EXPECTING_A_POLY1305_KEY
);
434 os
= EVP_PKEY_get0(pkey
);
440 # ifndef OPENSSL_NO_SIPHASH
441 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
443 ASN1_OCTET_STRING
*os
= NULL
;
445 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
446 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
449 os
= EVP_PKEY_get0(pkey
);
455 # ifndef OPENSSL_NO_RSA
456 int EVP_PKEY_set1_RSA(EVP_PKEY
*pkey
, RSA
*key
)
458 int ret
= EVP_PKEY_assign_RSA(pkey
, key
);
464 RSA
*EVP_PKEY_get0_RSA(const EVP_PKEY
*pkey
)
466 if (pkey
->type
!= EVP_PKEY_RSA
&& pkey
->type
!= EVP_PKEY_RSA_PSS
) {
467 EVPerr(EVP_F_EVP_PKEY_GET0_RSA
, EVP_R_EXPECTING_AN_RSA_KEY
);
470 return pkey
->pkey
.rsa
;
473 RSA
*EVP_PKEY_get1_RSA(EVP_PKEY
*pkey
)
475 RSA
*ret
= EVP_PKEY_get0_RSA(pkey
);
482 # ifndef OPENSSL_NO_DSA
483 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
485 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
491 DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
493 if (pkey
->type
!= EVP_PKEY_DSA
) {
494 EVPerr(EVP_F_EVP_PKEY_GET0_DSA
, EVP_R_EXPECTING_A_DSA_KEY
);
497 return pkey
->pkey
.dsa
;
500 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
502 DSA
*ret
= EVP_PKEY_get0_DSA(pkey
);
509 # ifndef OPENSSL_NO_EC
511 int EVP_PKEY_set1_EC_KEY(EVP_PKEY
*pkey
, EC_KEY
*key
)
513 int ret
= EVP_PKEY_assign_EC_KEY(pkey
, key
);
519 EC_KEY
*EVP_PKEY_get0_EC_KEY(const EVP_PKEY
*pkey
)
521 if (pkey
->type
!= EVP_PKEY_EC
) {
522 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY
, EVP_R_EXPECTING_A_EC_KEY
);
525 return pkey
->pkey
.ec
;
528 EC_KEY
*EVP_PKEY_get1_EC_KEY(EVP_PKEY
*pkey
)
530 EC_KEY
*ret
= EVP_PKEY_get0_EC_KEY(pkey
);
537 # ifndef OPENSSL_NO_DH
539 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
541 int type
= DH_get0_q(key
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
542 int ret
= EVP_PKEY_assign(pkey
, type
, key
);
549 DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
551 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
552 EVPerr(EVP_F_EVP_PKEY_GET0_DH
, EVP_R_EXPECTING_A_DH_KEY
);
555 return pkey
->pkey
.dh
;
558 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
560 DH
*ret
= EVP_PKEY_get0_DH(pkey
);
567 int EVP_PKEY_type(int type
)
570 const EVP_PKEY_ASN1_METHOD
*ameth
;
572 ameth
= EVP_PKEY_asn1_find(&e
, type
);
574 ret
= ameth
->pkey_id
;
577 # ifndef OPENSSL_NO_ENGINE
583 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
588 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
590 return EVP_PKEY_type(pkey
->type
);
594 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
596 BIO_set_indent(*out
, saved_indent
);
598 BIO
*next
= BIO_pop(*out
);
606 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
612 long i
= BIO_get_indent(*out
);
614 *saved_indent
= (i
< 0 ? 0 : i
);
615 if (BIO_set_indent(*out
, indent
) <= 0) {
616 if ((*out
= BIO_push(BIO_new(BIO_f_prefix()), *out
)) == NULL
)
620 if (BIO_set_indent(*out
, indent
) <= 0) {
621 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
628 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
631 return BIO_indent(out
, indent
, 128)
632 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
633 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
636 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
637 const char *propquery
/* For provided serialization */,
638 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
639 int indent
, ASN1_PCTX
*pctx
),
640 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
644 OSSL_SERIALIZER_CTX
*ctx
= NULL
;
645 int ret
= -2; /* default to unsupported */
647 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
650 ctx
= OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey
, propquery
);
651 if (OSSL_SERIALIZER_CTX_get_serializer(ctx
) != NULL
)
652 ret
= OSSL_SERIALIZER_to_bio(ctx
, out
);
653 OSSL_SERIALIZER_CTX_free(ctx
);
658 /* legacy fallback */
659 if (legacy_print
!= NULL
)
660 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
662 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
665 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
669 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
670 int indent
, ASN1_PCTX
*pctx
)
672 return print_pkey(pkey
, out
, indent
, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ
,
673 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
677 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
678 int indent
, ASN1_PCTX
*pctx
)
680 return print_pkey(pkey
, out
, indent
, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ
,
681 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
685 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
686 int indent
, ASN1_PCTX
*pctx
)
688 return print_pkey(pkey
, out
, indent
, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ
,
689 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
693 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
695 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
697 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
700 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
702 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
705 int EVP_PKEY_supports_digest_nid(EVP_PKEY
*pkey
, int nid
)
709 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SUPPORTS_MD_NID
, nid
, NULL
);
712 * If there is a mandatory default digest and this isn't it, then
713 * the answer is 'no'.
715 rv
= EVP_PKEY_get_default_digest_nid(pkey
, &default_nid
);
717 return (nid
== default_nid
);
718 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
725 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY
*pkey
,
726 const unsigned char *pt
, size_t ptlen
)
730 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, ptlen
,
736 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY
*pkey
, unsigned char **ppt
)
739 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppt
);
745 #endif /* FIPS_MODE */
747 /*- All methods below can also be used in FIPS_MODE */
749 EVP_PKEY
*EVP_PKEY_new(void)
751 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
754 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
757 ret
->type
= EVP_PKEY_NONE
;
758 ret
->save_type
= EVP_PKEY_NONE
;
760 ret
->save_parameters
= 1;
761 ret
->lock
= CRYPTO_THREAD_lock_new();
762 if (ret
->lock
== NULL
) {
763 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
770 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
774 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
777 REF_PRINT_COUNT("EVP_PKEY", pkey
);
778 REF_ASSERT_ISNT(i
< 2);
779 return ((i
> 1) ? 1 : 0);
782 static void evp_pkey_free_it(EVP_PKEY
*x
)
784 /* internal function; x is never NULL */
786 evp_keymgmt_clear_pkey_cache(x
);
788 if (x
->ameth
&& x
->ameth
->pkey_free
) {
789 x
->ameth
->pkey_free(x
);
792 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
793 ENGINE_finish(x
->engine
);
795 ENGINE_finish(x
->pmeth_engine
);
796 x
->pmeth_engine
= NULL
;
800 void EVP_PKEY_free(EVP_PKEY
*x
)
807 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
808 REF_PRINT_COUNT("EVP_PKEY", x
);
811 REF_ASSERT_ISNT(i
< 0);
813 CRYPTO_THREAD_lock_free(x
->lock
);
815 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
820 int EVP_PKEY_size(const EVP_PKEY
*pkey
)
823 if (pkey
->ameth
== NULL
)
824 return pkey
->cache
.size
;
825 else if (pkey
->ameth
->pkey_size
!= NULL
)
826 return pkey
->ameth
->pkey_size(pkey
);