2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 <openssl/bn.h>
13 #include <openssl/err.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/rsa.h>
18 #include <openssl/dsa.h>
19 #include <openssl/dh.h>
20 #include <openssl/engine.h>
22 #include "internal/asn1_int.h"
23 #include "internal/evp_int.h"
25 static void EVP_PKEY_free_it(EVP_PKEY
*x
);
27 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
29 if (pkey
&& pkey
->ameth
&& pkey
->ameth
->pkey_bits
)
30 return pkey
->ameth
->pkey_bits(pkey
);
34 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
38 if (!pkey
->ameth
|| !pkey
->ameth
->pkey_security_bits
)
40 return pkey
->ameth
->pkey_security_bits(pkey
);
43 int EVP_PKEY_size(EVP_PKEY
*pkey
)
45 if (pkey
&& pkey
->ameth
&& pkey
->ameth
->pkey_size
)
46 return pkey
->ameth
->pkey_size(pkey
);
50 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
52 #ifndef OPENSSL_NO_DSA
53 if (pkey
->type
== EVP_PKEY_DSA
) {
54 int ret
= pkey
->save_parameters
;
57 pkey
->save_parameters
= mode
;
62 if (pkey
->type
== EVP_PKEY_EC
) {
63 int ret
= pkey
->save_parameters
;
66 pkey
->save_parameters
= mode
;
73 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
75 if (to
->type
== EVP_PKEY_NONE
) {
76 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
78 } else if (to
->type
!= from
->type
) {
79 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_KEY_TYPES
);
83 if (EVP_PKEY_missing_parameters(from
)) {
84 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_MISSING_PARAMETERS
);
88 if (!EVP_PKEY_missing_parameters(to
)) {
89 if (EVP_PKEY_cmp_parameters(to
, from
) == 1)
91 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_PARAMETERS
);
95 if (from
->ameth
&& from
->ameth
->param_copy
)
96 return from
->ameth
->param_copy(to
, from
);
101 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
103 if (pkey
->ameth
&& pkey
->ameth
->param_missing
)
104 return pkey
->ameth
->param_missing(pkey
);
108 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
110 if (a
->type
!= b
->type
)
112 if (a
->ameth
&& a
->ameth
->param_cmp
)
113 return a
->ameth
->param_cmp(a
, b
);
117 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
119 if (a
->type
!= b
->type
)
124 /* Compare parameters if the algorithm has them */
125 if (a
->ameth
->param_cmp
) {
126 ret
= a
->ameth
->param_cmp(a
, b
);
131 if (a
->ameth
->pub_cmp
)
132 return a
->ameth
->pub_cmp(a
, b
);
138 EVP_PKEY
*EVP_PKEY_new(void)
140 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
143 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
146 ret
->type
= EVP_PKEY_NONE
;
147 ret
->save_type
= EVP_PKEY_NONE
;
149 ret
->save_parameters
= 1;
150 ret
->lock
= CRYPTO_THREAD_lock_new();
151 if (ret
->lock
== NULL
) {
152 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
159 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
163 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
166 REF_PRINT_COUNT("EVP_PKEY", pkey
);
167 REF_ASSERT_ISNT(i
< 2);
168 return ((i
> 1) ? 1 : 0);
172 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
173 * is NULL just return 1 or 0 if the algorithm exists.
176 static int pkey_set_type(EVP_PKEY
*pkey
, int type
, const char *str
, int len
)
178 const EVP_PKEY_ASN1_METHOD
*ameth
;
182 EVP_PKEY_free_it(pkey
);
184 * If key type matches and a method exists then this lookup has
185 * succeeded once so just indicate success.
187 if ((type
== pkey
->save_type
) && pkey
->ameth
)
189 #ifndef OPENSSL_NO_ENGINE
190 /* If we have an ENGINE release it */
191 ENGINE_finish(pkey
->engine
);
196 ameth
= EVP_PKEY_asn1_find_str(&e
, str
, len
);
198 ameth
= EVP_PKEY_asn1_find(&e
, type
);
199 #ifndef OPENSSL_NO_ENGINE
204 EVPerr(EVP_F_PKEY_SET_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
211 pkey
->type
= pkey
->ameth
->pkey_id
;
212 pkey
->save_type
= type
;
217 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
219 return pkey_set_type(pkey
, type
, NULL
, -1);
222 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
224 return pkey_set_type(pkey
, EVP_PKEY_NONE
, str
, len
);
227 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
229 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
231 pkey
->pkey
.ptr
= key
;
232 return (key
!= NULL
);
235 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
237 return pkey
->pkey
.ptr
;
240 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
242 ASN1_OCTET_STRING
*os
= NULL
;
243 if (pkey
->type
!= EVP_PKEY_HMAC
) {
244 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC
, EVP_R_EXPECTING_AN_HMAC_KEY
);
247 os
= EVP_PKEY_get0(pkey
);
252 #ifndef OPENSSL_NO_POLY1305
253 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
255 ASN1_OCTET_STRING
*os
= NULL
;
256 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
257 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305
, EVP_R_EXPECTING_A_POLY1305_KEY
);
260 os
= EVP_PKEY_get0(pkey
);
266 #ifndef OPENSSL_NO_SIPHASH
267 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
269 ASN1_OCTET_STRING
*os
= NULL
;
271 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
272 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
275 os
= EVP_PKEY_get0(pkey
);
281 #ifndef OPENSSL_NO_RSA
282 int EVP_PKEY_set1_RSA(EVP_PKEY
*pkey
, RSA
*key
)
284 int ret
= EVP_PKEY_assign_RSA(pkey
, key
);
290 RSA
*EVP_PKEY_get0_RSA(EVP_PKEY
*pkey
)
292 if (pkey
->type
!= EVP_PKEY_RSA
) {
293 EVPerr(EVP_F_EVP_PKEY_GET0_RSA
, EVP_R_EXPECTING_AN_RSA_KEY
);
296 return pkey
->pkey
.rsa
;
299 RSA
*EVP_PKEY_get1_RSA(EVP_PKEY
*pkey
)
301 RSA
*ret
= EVP_PKEY_get0_RSA(pkey
);
308 #ifndef OPENSSL_NO_DSA
309 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
311 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
317 DSA
*EVP_PKEY_get0_DSA(EVP_PKEY
*pkey
)
319 if (pkey
->type
!= EVP_PKEY_DSA
) {
320 EVPerr(EVP_F_EVP_PKEY_GET0_DSA
, EVP_R_EXPECTING_A_DSA_KEY
);
323 return pkey
->pkey
.dsa
;
326 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
328 DSA
*ret
= EVP_PKEY_get0_DSA(pkey
);
335 #ifndef OPENSSL_NO_EC
337 int EVP_PKEY_set1_EC_KEY(EVP_PKEY
*pkey
, EC_KEY
*key
)
339 int ret
= EVP_PKEY_assign_EC_KEY(pkey
, key
);
345 EC_KEY
*EVP_PKEY_get0_EC_KEY(EVP_PKEY
*pkey
)
347 if (pkey
->type
!= EVP_PKEY_EC
) {
348 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY
, EVP_R_EXPECTING_A_EC_KEY
);
351 return pkey
->pkey
.ec
;
354 EC_KEY
*EVP_PKEY_get1_EC_KEY(EVP_PKEY
*pkey
)
356 EC_KEY
*ret
= EVP_PKEY_get0_EC_KEY(pkey
);
363 #ifndef OPENSSL_NO_DH
365 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
367 int ret
= EVP_PKEY_assign_DH(pkey
, key
);
373 DH
*EVP_PKEY_get0_DH(EVP_PKEY
*pkey
)
375 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
376 EVPerr(EVP_F_EVP_PKEY_GET0_DH
, EVP_R_EXPECTING_A_DH_KEY
);
379 return pkey
->pkey
.dh
;
382 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
384 DH
*ret
= EVP_PKEY_get0_DH(pkey
);
391 int EVP_PKEY_type(int type
)
394 const EVP_PKEY_ASN1_METHOD
*ameth
;
396 ameth
= EVP_PKEY_asn1_find(&e
, type
);
398 ret
= ameth
->pkey_id
;
401 #ifndef OPENSSL_NO_ENGINE
407 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
412 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
414 return EVP_PKEY_type(pkey
->type
);
417 void EVP_PKEY_free(EVP_PKEY
*x
)
424 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
425 REF_PRINT_COUNT("EVP_PKEY", x
);
428 REF_ASSERT_ISNT(i
< 0);
430 CRYPTO_THREAD_lock_free(x
->lock
);
431 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
435 static void EVP_PKEY_free_it(EVP_PKEY
*x
)
437 /* internal function; x is never NULL */
438 if (x
->ameth
&& x
->ameth
->pkey_free
) {
439 x
->ameth
->pkey_free(x
);
442 #ifndef OPENSSL_NO_ENGINE
443 ENGINE_finish(x
->engine
);
448 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
451 BIO_indent(out
, indent
, 128);
452 BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
453 kstr
, OBJ_nid2ln(pkey
->type
));
457 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
458 int indent
, ASN1_PCTX
*pctx
)
460 if (pkey
->ameth
&& pkey
->ameth
->pub_print
)
461 return pkey
->ameth
->pub_print(out
, pkey
, indent
, pctx
);
463 return unsup_alg(out
, pkey
, indent
, "Public Key");
466 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
467 int indent
, ASN1_PCTX
*pctx
)
469 if (pkey
->ameth
&& pkey
->ameth
->priv_print
)
470 return pkey
->ameth
->priv_print(out
, pkey
, indent
, pctx
);
472 return unsup_alg(out
, pkey
, indent
, "Private Key");
475 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
476 int indent
, ASN1_PCTX
*pctx
)
478 if (pkey
->ameth
&& pkey
->ameth
->param_print
)
479 return pkey
->ameth
->param_print(out
, pkey
, indent
, pctx
);
480 return unsup_alg(out
, pkey
, indent
, "Parameters");
483 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
485 if (pkey
->ameth
== NULL
|| pkey
->ameth
->pkey_ctrl
== NULL
)
487 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
490 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
492 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
495 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY
*pkey
,
496 const unsigned char *pt
, size_t ptlen
)
500 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, ptlen
,
506 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY
*pkey
, unsigned char **ppt
)
509 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppt
);