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>
24 #include "internal/asn1_int.h"
25 #include "internal/evp_int.h"
27 static void EVP_PKEY_free_it(EVP_PKEY
*x
);
29 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
31 if (pkey
&& pkey
->ameth
&& pkey
->ameth
->pkey_bits
)
32 return pkey
->ameth
->pkey_bits(pkey
);
36 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
40 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_security_bits
)
42 return pkey
->ameth
->pkey_security_bits(pkey
);
45 int EVP_PKEY_size(const EVP_PKEY
*pkey
)
47 if (pkey
&& pkey
->ameth
&& pkey
->ameth
->pkey_size
)
48 return pkey
->ameth
->pkey_size(pkey
);
52 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
54 #ifndef OPENSSL_NO_DSA
55 if (pkey
->type
== EVP_PKEY_DSA
) {
56 int ret
= pkey
->save_parameters
;
59 pkey
->save_parameters
= mode
;
64 if (pkey
->type
== EVP_PKEY_EC
) {
65 int ret
= pkey
->save_parameters
;
68 pkey
->save_parameters
= mode
;
75 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
77 if (to
->type
== EVP_PKEY_NONE
) {
78 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
80 } else if (to
->type
!= from
->type
) {
81 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_KEY_TYPES
);
85 if (EVP_PKEY_missing_parameters(from
)) {
86 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_MISSING_PARAMETERS
);
90 if (!EVP_PKEY_missing_parameters(to
)) {
91 if (EVP_PKEY_cmp_parameters(to
, from
) == 1)
93 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_PARAMETERS
);
97 if (from
->ameth
&& from
->ameth
->param_copy
)
98 return from
->ameth
->param_copy(to
, from
);
103 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
105 if (pkey
->ameth
&& pkey
->ameth
->param_missing
)
106 return pkey
->ameth
->param_missing(pkey
);
110 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
112 if (a
->type
!= b
->type
)
114 if (a
->ameth
&& a
->ameth
->param_cmp
)
115 return a
->ameth
->param_cmp(a
, b
);
119 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
121 if (a
->type
!= b
->type
)
126 /* Compare parameters if the algorithm has them */
127 if (a
->ameth
->param_cmp
) {
128 ret
= a
->ameth
->param_cmp(a
, b
);
133 if (a
->ameth
->pub_cmp
)
134 return a
->ameth
->pub_cmp(a
, b
);
140 EVP_PKEY
*EVP_PKEY_new(void)
142 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
145 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
148 ret
->type
= EVP_PKEY_NONE
;
149 ret
->save_type
= EVP_PKEY_NONE
;
151 ret
->save_parameters
= 1;
152 ret
->lock
= CRYPTO_THREAD_lock_new();
153 if (ret
->lock
== NULL
) {
154 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
161 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
165 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
168 REF_PRINT_COUNT("EVP_PKEY", pkey
);
169 REF_ASSERT_ISNT(i
< 2);
170 return ((i
> 1) ? 1 : 0);
174 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
175 * is NULL just return 1 or 0 if the algorithm exists.
178 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
181 const EVP_PKEY_ASN1_METHOD
*ameth
;
182 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
186 EVP_PKEY_free_it(pkey
);
188 * If key type matches and a method exists then this lookup has
189 * succeeded once so just indicate success.
191 if ((type
== pkey
->save_type
) && pkey
->ameth
)
193 #ifndef OPENSSL_NO_ENGINE
194 /* If we have ENGINEs release them */
195 ENGINE_finish(pkey
->engine
);
197 ENGINE_finish(pkey
->pmeth_engine
);
198 pkey
->pmeth_engine
= NULL
;
202 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
204 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
205 #ifndef OPENSSL_NO_ENGINE
206 if (pkey
== NULL
&& eptr
!= NULL
)
210 EVPerr(EVP_F_PKEY_SET_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
217 pkey
->type
= pkey
->ameth
->pkey_id
;
218 pkey
->save_type
= type
;
223 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
224 const unsigned char *priv
,
227 EVP_PKEY
*ret
= EVP_PKEY_new();
230 || !pkey_set_type(ret
, e
, type
, NULL
, -1)) {
231 /* EVPerr already called */
235 if (ret
->ameth
->set_priv_key
== NULL
) {
236 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
,
237 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
241 if (!ret
->ameth
->set_priv_key(ret
, priv
, len
)) {
242 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
, EVP_R_KEY_SETUP_FAILED
);
253 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
254 const unsigned char *pub
,
257 EVP_PKEY
*ret
= EVP_PKEY_new();
260 || !pkey_set_type(ret
, e
, type
, NULL
, -1)) {
261 /* EVPerr already called */
265 if (ret
->ameth
->set_pub_key
== NULL
) {
266 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
,
267 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
271 if (!ret
->ameth
->set_pub_key(ret
, pub
, len
)) {
272 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
, EVP_R_KEY_SETUP_FAILED
);
283 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
286 if (pkey
->ameth
->get_priv_key
== NULL
) {
287 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
,
288 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
292 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
293 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
300 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
303 if (pkey
->ameth
->get_pub_key
== NULL
) {
304 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
,
305 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
309 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
310 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
317 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
318 size_t len
, const EVP_CIPHER
*cipher
)
320 #ifndef OPENSSL_NO_CMAC
321 EVP_PKEY
*ret
= EVP_PKEY_new();
322 EVP_MAC_CTX
*cmctx
= EVP_MAC_CTX_new_id(EVP_MAC_CMAC
);
326 || !pkey_set_type(ret
, e
, EVP_PKEY_CMAC
, NULL
, -1)) {
327 /* EVPerr already called */
331 if (EVP_MAC_ctrl(cmctx
, EVP_MAC_CTRL_SET_ENGINE
, e
) <= 0
332 || EVP_MAC_ctrl(cmctx
, EVP_MAC_CTRL_SET_CIPHER
, cipher
) <= 0
333 || EVP_MAC_ctrl(cmctx
, EVP_MAC_CTRL_SET_KEY
, priv
, len
) <= 0) {
334 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
, EVP_R_KEY_SETUP_FAILED
);
338 ret
->pkey
.ptr
= cmctx
;
343 EVP_MAC_CTX_free(cmctx
);
346 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
,
347 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
352 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
354 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1);
357 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
359 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
);
362 int EVP_PKEY_set_alias_type(EVP_PKEY
*pkey
, int type
)
364 if (pkey
->type
== type
) {
365 return 1; /* it already is that type */
369 * The application is requesting to alias this to a different pkey type,
370 * but not one that resolves to the base type.
372 if (EVP_PKEY_type(type
) != EVP_PKEY_base_id(pkey
)) {
373 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
381 #ifndef OPENSSL_NO_ENGINE
382 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
385 if (!ENGINE_init(e
)) {
386 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, ERR_R_ENGINE_LIB
);
389 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
391 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, EVP_R_UNSUPPORTED_ALGORITHM
);
395 ENGINE_finish(pkey
->pmeth_engine
);
396 pkey
->pmeth_engine
= e
;
400 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
405 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
407 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
409 pkey
->pkey
.ptr
= key
;
410 return (key
!= NULL
);
413 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
415 return pkey
->pkey
.ptr
;
418 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
420 ASN1_OCTET_STRING
*os
= NULL
;
421 if (pkey
->type
!= EVP_PKEY_HMAC
) {
422 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC
, EVP_R_EXPECTING_AN_HMAC_KEY
);
425 os
= EVP_PKEY_get0(pkey
);
430 #ifndef OPENSSL_NO_POLY1305
431 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
433 ASN1_OCTET_STRING
*os
= NULL
;
434 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
435 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305
, EVP_R_EXPECTING_A_POLY1305_KEY
);
438 os
= EVP_PKEY_get0(pkey
);
444 #ifndef OPENSSL_NO_SIPHASH
445 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
447 ASN1_OCTET_STRING
*os
= NULL
;
449 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
450 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
453 os
= EVP_PKEY_get0(pkey
);
459 #ifndef OPENSSL_NO_RSA
460 int EVP_PKEY_set1_RSA(EVP_PKEY
*pkey
, RSA
*key
)
462 int ret
= EVP_PKEY_assign_RSA(pkey
, key
);
468 RSA
*EVP_PKEY_get0_RSA(const EVP_PKEY
*pkey
)
470 if (pkey
->type
!= EVP_PKEY_RSA
) {
471 EVPerr(EVP_F_EVP_PKEY_GET0_RSA
, EVP_R_EXPECTING_AN_RSA_KEY
);
474 return pkey
->pkey
.rsa
;
477 RSA
*EVP_PKEY_get1_RSA(EVP_PKEY
*pkey
)
479 RSA
*ret
= EVP_PKEY_get0_RSA(pkey
);
486 #ifndef OPENSSL_NO_DSA
487 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
489 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
495 DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
497 if (pkey
->type
!= EVP_PKEY_DSA
) {
498 EVPerr(EVP_F_EVP_PKEY_GET0_DSA
, EVP_R_EXPECTING_A_DSA_KEY
);
501 return pkey
->pkey
.dsa
;
504 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
506 DSA
*ret
= EVP_PKEY_get0_DSA(pkey
);
513 #ifndef OPENSSL_NO_EC
515 int EVP_PKEY_set1_EC_KEY(EVP_PKEY
*pkey
, EC_KEY
*key
)
517 int ret
= EVP_PKEY_assign_EC_KEY(pkey
, key
);
523 EC_KEY
*EVP_PKEY_get0_EC_KEY(const EVP_PKEY
*pkey
)
525 if (pkey
->type
!= EVP_PKEY_EC
) {
526 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY
, EVP_R_EXPECTING_A_EC_KEY
);
529 return pkey
->pkey
.ec
;
532 EC_KEY
*EVP_PKEY_get1_EC_KEY(EVP_PKEY
*pkey
)
534 EC_KEY
*ret
= EVP_PKEY_get0_EC_KEY(pkey
);
541 #ifndef OPENSSL_NO_DH
543 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
545 int ret
= EVP_PKEY_assign_DH(pkey
, key
);
551 DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
553 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
554 EVPerr(EVP_F_EVP_PKEY_GET0_DH
, EVP_R_EXPECTING_A_DH_KEY
);
557 return pkey
->pkey
.dh
;
560 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
562 DH
*ret
= EVP_PKEY_get0_DH(pkey
);
569 int EVP_PKEY_type(int type
)
572 const EVP_PKEY_ASN1_METHOD
*ameth
;
574 ameth
= EVP_PKEY_asn1_find(&e
, type
);
576 ret
= ameth
->pkey_id
;
579 #ifndef OPENSSL_NO_ENGINE
585 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
590 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
592 return EVP_PKEY_type(pkey
->type
);
595 void EVP_PKEY_free(EVP_PKEY
*x
)
602 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
603 REF_PRINT_COUNT("EVP_PKEY", x
);
606 REF_ASSERT_ISNT(i
< 0);
608 CRYPTO_THREAD_lock_free(x
->lock
);
609 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
613 static void EVP_PKEY_free_it(EVP_PKEY
*x
)
615 /* internal function; x is never NULL */
616 if (x
->ameth
&& x
->ameth
->pkey_free
) {
617 x
->ameth
->pkey_free(x
);
620 #ifndef OPENSSL_NO_ENGINE
621 ENGINE_finish(x
->engine
);
623 ENGINE_finish(x
->pmeth_engine
);
624 x
->pmeth_engine
= NULL
;
628 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
631 BIO_indent(out
, indent
, 128);
632 BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
633 kstr
, OBJ_nid2ln(pkey
->type
));
637 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
638 int indent
, ASN1_PCTX
*pctx
)
640 if (pkey
->ameth
&& pkey
->ameth
->pub_print
)
641 return pkey
->ameth
->pub_print(out
, pkey
, indent
, pctx
);
643 return unsup_alg(out
, pkey
, indent
, "Public Key");
646 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
647 int indent
, ASN1_PCTX
*pctx
)
649 if (pkey
->ameth
&& pkey
->ameth
->priv_print
)
650 return pkey
->ameth
->priv_print(out
, pkey
, indent
, pctx
);
652 return unsup_alg(out
, pkey
, indent
, "Private Key");
655 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
656 int indent
, ASN1_PCTX
*pctx
)
658 if (pkey
->ameth
&& pkey
->ameth
->param_print
)
659 return pkey
->ameth
->param_print(out
, pkey
, indent
, pctx
);
660 return unsup_alg(out
, pkey
, indent
, "Parameters");
663 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
665 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
667 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
670 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
672 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
675 int EVP_PKEY_supports_digest_nid(EVP_PKEY
*pkey
, int nid
)
679 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SUPPORTS_MD_NID
, nid
, NULL
);
682 * If there is a mandatory default digest and this isn't it, then
683 * the answer is 'no'.
685 rv
= EVP_PKEY_get_default_digest_nid(pkey
, &default_nid
);
687 return (nid
== default_nid
);
688 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
695 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY
*pkey
,
696 const unsigned char *pt
, size_t ptlen
)
700 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, ptlen
,
706 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY
*pkey
, unsigned char **ppt
)
709 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppt
);