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/core_names.h>
26 #include "internal/asn1_int.h"
27 #include "internal/evp_int.h"
28 #include "internal/provider.h"
30 static void EVP_PKEY_free_it(EVP_PKEY
*x
);
32 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
34 if (pkey
&& pkey
->ameth
&& pkey
->ameth
->pkey_bits
)
35 return pkey
->ameth
->pkey_bits(pkey
);
39 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
43 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_security_bits
)
45 return pkey
->ameth
->pkey_security_bits(pkey
);
48 int EVP_PKEY_size(const EVP_PKEY
*pkey
)
50 if (pkey
&& pkey
->ameth
&& pkey
->ameth
->pkey_size
)
51 return pkey
->ameth
->pkey_size(pkey
);
55 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
57 #ifndef OPENSSL_NO_DSA
58 if (pkey
->type
== EVP_PKEY_DSA
) {
59 int ret
= pkey
->save_parameters
;
62 pkey
->save_parameters
= mode
;
67 if (pkey
->type
== EVP_PKEY_EC
) {
68 int ret
= pkey
->save_parameters
;
71 pkey
->save_parameters
= mode
;
78 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
80 if (to
->type
== EVP_PKEY_NONE
) {
81 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
83 } else if (to
->type
!= from
->type
) {
84 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_KEY_TYPES
);
88 if (EVP_PKEY_missing_parameters(from
)) {
89 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_MISSING_PARAMETERS
);
93 if (!EVP_PKEY_missing_parameters(to
)) {
94 if (EVP_PKEY_cmp_parameters(to
, from
) == 1)
96 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_PARAMETERS
);
100 if (from
->ameth
&& from
->ameth
->param_copy
)
101 return from
->ameth
->param_copy(to
, from
);
106 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
108 if (pkey
->ameth
&& pkey
->ameth
->param_missing
)
109 return pkey
->ameth
->param_missing(pkey
);
113 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
115 if (a
->type
!= b
->type
)
117 if (a
->ameth
&& a
->ameth
->param_cmp
)
118 return a
->ameth
->param_cmp(a
, b
);
122 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
124 if (a
->type
!= b
->type
)
129 /* Compare parameters if the algorithm has them */
130 if (a
->ameth
->param_cmp
) {
131 ret
= a
->ameth
->param_cmp(a
, b
);
136 if (a
->ameth
->pub_cmp
)
137 return a
->ameth
->pub_cmp(a
, b
);
143 EVP_PKEY
*EVP_PKEY_new(void)
145 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
148 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
151 ret
->type
= EVP_PKEY_NONE
;
152 ret
->save_type
= EVP_PKEY_NONE
;
154 ret
->save_parameters
= 1;
155 ret
->lock
= CRYPTO_THREAD_lock_new();
156 if (ret
->lock
== NULL
) {
157 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
164 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
168 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
171 REF_PRINT_COUNT("EVP_PKEY", pkey
);
172 REF_ASSERT_ISNT(i
< 2);
173 return ((i
> 1) ? 1 : 0);
177 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
178 * is NULL just return 1 or 0 if the algorithm exists.
181 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
184 const EVP_PKEY_ASN1_METHOD
*ameth
;
185 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
189 EVP_PKEY_free_it(pkey
);
191 * If key type matches and a method exists then this lookup has
192 * succeeded once so just indicate success.
194 if ((type
== pkey
->save_type
) && pkey
->ameth
)
196 #ifndef OPENSSL_NO_ENGINE
197 /* If we have ENGINEs release them */
198 ENGINE_finish(pkey
->engine
);
200 ENGINE_finish(pkey
->pmeth_engine
);
201 pkey
->pmeth_engine
= NULL
;
205 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
207 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
208 #ifndef OPENSSL_NO_ENGINE
209 if (pkey
== NULL
&& eptr
!= NULL
)
213 EVPerr(EVP_F_PKEY_SET_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
220 pkey
->type
= pkey
->ameth
->pkey_id
;
221 pkey
->save_type
= type
;
226 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
227 const unsigned char *priv
,
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_priv_key
== NULL
) {
239 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
,
240 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
244 if (!ret
->ameth
->set_priv_key(ret
, priv
, len
)) {
245 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
, EVP_R_KEY_SETUP_FAILED
);
256 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
257 const unsigned char *pub
,
260 EVP_PKEY
*ret
= EVP_PKEY_new();
263 || !pkey_set_type(ret
, e
, type
, NULL
, -1)) {
264 /* EVPerr already called */
268 if (ret
->ameth
->set_pub_key
== NULL
) {
269 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
,
270 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
274 if (!ret
->ameth
->set_pub_key(ret
, pub
, len
)) {
275 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
, EVP_R_KEY_SETUP_FAILED
);
286 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
289 if (pkey
->ameth
->get_priv_key
== NULL
) {
290 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
,
291 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
295 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
296 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
303 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
306 if (pkey
->ameth
->get_pub_key
== NULL
) {
307 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
,
308 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
312 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
313 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
320 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
321 size_t len
, const EVP_CIPHER
*cipher
)
323 #ifndef OPENSSL_NO_CMAC
324 # ifndef OPENSSL_NO_ENGINE
325 const char *engine_name
= e
!= NULL
? ENGINE_get_name(e
) : NULL
;
327 const char *cipher_name
= EVP_CIPHER_name(cipher
);
328 const OSSL_PROVIDER
*prov
= EVP_CIPHER_provider(cipher
);
329 OPENSSL_CTX
*libctx
=
330 prov
== NULL
? NULL
: ossl_provider_library_context(prov
);
331 EVP_PKEY
*ret
= EVP_PKEY_new();
332 EVP_MAC
*cmac
= EVP_MAC_fetch(libctx
, OSSL_MAC_NAME_CMAC
, NULL
);
333 EVP_MAC_CTX
*cmctx
= cmac
!= NULL
? EVP_MAC_CTX_new(cmac
) : NULL
;
334 OSSL_PARAM params
[4];
339 || !pkey_set_type(ret
, e
, EVP_PKEY_CMAC
, NULL
, -1)) {
340 /* EVPerr already called */
344 # ifndef OPENSSL_NO_ENGINE
345 if (engine_name
!= NULL
)
347 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE
,
349 strlen(engine_name
) + 1);
353 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER
,
355 strlen(cipher_name
) + 1);
357 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
359 params
[paramsn
] = OSSL_PARAM_construct_end();
361 if (!EVP_MAC_CTX_set_params(cmctx
, params
)) {
362 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
, EVP_R_KEY_SETUP_FAILED
);
366 ret
->pkey
.ptr
= cmctx
;
371 EVP_MAC_CTX_free(cmctx
);
375 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
,
376 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
381 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
383 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1);
386 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
388 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
);
391 int EVP_PKEY_set_alias_type(EVP_PKEY
*pkey
, int type
)
393 if (pkey
->type
== type
) {
394 return 1; /* it already is that type */
398 * The application is requesting to alias this to a different pkey type,
399 * but not one that resolves to the base type.
401 if (EVP_PKEY_type(type
) != EVP_PKEY_base_id(pkey
)) {
402 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
410 #ifndef OPENSSL_NO_ENGINE
411 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
414 if (!ENGINE_init(e
)) {
415 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, ERR_R_ENGINE_LIB
);
418 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
420 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, EVP_R_UNSUPPORTED_ALGORITHM
);
424 ENGINE_finish(pkey
->pmeth_engine
);
425 pkey
->pmeth_engine
= e
;
429 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
434 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
436 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
438 pkey
->pkey
.ptr
= key
;
439 return (key
!= NULL
);
442 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
444 return pkey
->pkey
.ptr
;
447 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
449 ASN1_OCTET_STRING
*os
= NULL
;
450 if (pkey
->type
!= EVP_PKEY_HMAC
) {
451 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC
, EVP_R_EXPECTING_AN_HMAC_KEY
);
454 os
= EVP_PKEY_get0(pkey
);
459 #ifndef OPENSSL_NO_POLY1305
460 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
462 ASN1_OCTET_STRING
*os
= NULL
;
463 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
464 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305
, EVP_R_EXPECTING_A_POLY1305_KEY
);
467 os
= EVP_PKEY_get0(pkey
);
473 #ifndef OPENSSL_NO_SIPHASH
474 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
476 ASN1_OCTET_STRING
*os
= NULL
;
478 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
479 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
482 os
= EVP_PKEY_get0(pkey
);
488 #ifndef OPENSSL_NO_RSA
489 int EVP_PKEY_set1_RSA(EVP_PKEY
*pkey
, RSA
*key
)
491 int ret
= EVP_PKEY_assign_RSA(pkey
, key
);
497 RSA
*EVP_PKEY_get0_RSA(const EVP_PKEY
*pkey
)
499 if (pkey
->type
!= EVP_PKEY_RSA
) {
500 EVPerr(EVP_F_EVP_PKEY_GET0_RSA
, EVP_R_EXPECTING_AN_RSA_KEY
);
503 return pkey
->pkey
.rsa
;
506 RSA
*EVP_PKEY_get1_RSA(EVP_PKEY
*pkey
)
508 RSA
*ret
= EVP_PKEY_get0_RSA(pkey
);
515 #ifndef OPENSSL_NO_DSA
516 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
518 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
524 DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
526 if (pkey
->type
!= EVP_PKEY_DSA
) {
527 EVPerr(EVP_F_EVP_PKEY_GET0_DSA
, EVP_R_EXPECTING_A_DSA_KEY
);
530 return pkey
->pkey
.dsa
;
533 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
535 DSA
*ret
= EVP_PKEY_get0_DSA(pkey
);
542 #ifndef OPENSSL_NO_EC
544 int EVP_PKEY_set1_EC_KEY(EVP_PKEY
*pkey
, EC_KEY
*key
)
546 int ret
= EVP_PKEY_assign_EC_KEY(pkey
, key
);
552 EC_KEY
*EVP_PKEY_get0_EC_KEY(const EVP_PKEY
*pkey
)
554 if (pkey
->type
!= EVP_PKEY_EC
) {
555 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY
, EVP_R_EXPECTING_A_EC_KEY
);
558 return pkey
->pkey
.ec
;
561 EC_KEY
*EVP_PKEY_get1_EC_KEY(EVP_PKEY
*pkey
)
563 EC_KEY
*ret
= EVP_PKEY_get0_EC_KEY(pkey
);
570 #ifndef OPENSSL_NO_DH
572 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
574 int ret
= EVP_PKEY_assign_DH(pkey
, key
);
580 DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
582 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
583 EVPerr(EVP_F_EVP_PKEY_GET0_DH
, EVP_R_EXPECTING_A_DH_KEY
);
586 return pkey
->pkey
.dh
;
589 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
591 DH
*ret
= EVP_PKEY_get0_DH(pkey
);
598 int EVP_PKEY_type(int type
)
601 const EVP_PKEY_ASN1_METHOD
*ameth
;
603 ameth
= EVP_PKEY_asn1_find(&e
, type
);
605 ret
= ameth
->pkey_id
;
608 #ifndef OPENSSL_NO_ENGINE
614 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
619 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
621 return EVP_PKEY_type(pkey
->type
);
624 void EVP_PKEY_free(EVP_PKEY
*x
)
631 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
632 REF_PRINT_COUNT("EVP_PKEY", x
);
635 REF_ASSERT_ISNT(i
< 0);
637 CRYPTO_THREAD_lock_free(x
->lock
);
638 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
642 static void EVP_PKEY_free_it(EVP_PKEY
*x
)
644 /* internal function; x is never NULL */
646 evp_keymgmt_clear_pkey_cache(x
);
648 if (x
->ameth
&& x
->ameth
->pkey_free
) {
649 x
->ameth
->pkey_free(x
);
652 #ifndef OPENSSL_NO_ENGINE
653 ENGINE_finish(x
->engine
);
655 ENGINE_finish(x
->pmeth_engine
);
656 x
->pmeth_engine
= NULL
;
660 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
663 BIO_indent(out
, indent
, 128);
664 BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
665 kstr
, OBJ_nid2ln(pkey
->type
));
669 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
670 int indent
, ASN1_PCTX
*pctx
)
672 if (pkey
->ameth
&& pkey
->ameth
->pub_print
)
673 return pkey
->ameth
->pub_print(out
, pkey
, indent
, pctx
);
675 return unsup_alg(out
, pkey
, indent
, "Public Key");
678 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
679 int indent
, ASN1_PCTX
*pctx
)
681 if (pkey
->ameth
&& pkey
->ameth
->priv_print
)
682 return pkey
->ameth
->priv_print(out
, pkey
, indent
, pctx
);
684 return unsup_alg(out
, pkey
, indent
, "Private Key");
687 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
688 int indent
, ASN1_PCTX
*pctx
)
690 if (pkey
->ameth
&& pkey
->ameth
->param_print
)
691 return pkey
->ameth
->param_print(out
, pkey
, indent
, pctx
);
692 return unsup_alg(out
, pkey
, indent
, "Parameters");
695 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
697 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
699 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
702 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
704 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
707 int EVP_PKEY_supports_digest_nid(EVP_PKEY
*pkey
, int nid
)
711 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SUPPORTS_MD_NID
, nid
, NULL
);
714 * If there is a mandatory default digest and this isn't it, then
715 * the answer is 'no'.
717 rv
= EVP_PKEY_get_default_digest_nid(pkey
, &default_nid
);
719 return (nid
== default_nid
);
720 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
727 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY
*pkey
,
728 const unsigned char *pt
, size_t ptlen
)
732 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, ptlen
,
738 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY
*pkey
, unsigned char **ppt
)
741 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppt
);