2 * Copyright 1995-2020 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/ec.h>
28 #include <openssl/cmac.h>
29 #include <openssl/engine.h>
30 #include <openssl/params.h>
31 #include <openssl/serializer.h>
32 #include <openssl/core_names.h>
34 #include "crypto/asn1.h"
35 #include "crypto/evp.h"
36 #include "internal/evp.h"
37 #include "internal/provider.h"
38 #include "evp_local.h"
39 DEFINE_STACK_OF(X509_ATTRIBUTE
)
41 #include "crypto/ec.h"
43 /* TODO remove this when the EVP_PKEY_is_a() #legacy support hack is removed */
44 #include "e_os.h" /* strcasecmp on Windows */
46 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
47 int len
, EVP_KEYMGMT
*keymgmt
);
48 static void evp_pkey_free_it(EVP_PKEY
*key
);
52 /* The type of parameters selected in key parameter functions */
53 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
55 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
58 if (pkey
->ameth
== NULL
)
59 return pkey
->cache
.bits
;
60 else if (pkey
->ameth
->pkey_bits
)
61 return pkey
->ameth
->pkey_bits(pkey
);
66 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
70 if (pkey
->ameth
== NULL
)
71 return pkey
->cache
.security_bits
;
72 if (pkey
->ameth
->pkey_security_bits
== NULL
)
74 return pkey
->ameth
->pkey_security_bits(pkey
);
77 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
79 # ifndef OPENSSL_NO_DSA
80 if (pkey
->type
== EVP_PKEY_DSA
) {
81 int ret
= pkey
->save_parameters
;
84 pkey
->save_parameters
= mode
;
88 # ifndef OPENSSL_NO_EC
89 if (pkey
->type
== EVP_PKEY_EC
) {
90 int ret
= pkey
->save_parameters
;
93 pkey
->save_parameters
= mode
;
100 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
102 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
105 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
107 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
110 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
113 * TODO: clean up legacy stuff from this function when legacy support
118 * If |to| is a legacy key and |from| isn't, we must downgrade |from|.
119 * If that fails, this function fails.
121 if (to
->type
!= EVP_PKEY_NONE
&& from
->keymgmt
!= NULL
)
122 if (!evp_pkey_downgrade((EVP_PKEY
*)from
))
126 * Make sure |to| is typed. Content is less important at this early
129 * 1. If |to| is untyped, assign |from|'s key type to it.
130 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
131 * (|from| was already downgraded above)
133 * If |to| is a provided key, there's nothing more to do here, functions
134 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
135 * further down help us find out if they are the same or not.
137 if (to
->type
== EVP_PKEY_NONE
&& to
->keymgmt
== NULL
) {
138 if (from
->type
!= EVP_PKEY_NONE
) {
139 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
142 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
145 } else if (to
->type
!= EVP_PKEY_NONE
) {
146 if (to
->type
!= from
->type
) {
147 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_KEY_TYPES
);
152 if (EVP_PKEY_missing_parameters(from
)) {
153 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_MISSING_PARAMETERS
);
157 if (!EVP_PKEY_missing_parameters(to
)) {
158 if (EVP_PKEY_cmp_parameters(to
, from
) == 1)
160 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS
, EVP_R_DIFFERENT_PARAMETERS
);
164 /* For purely provided keys, we just call the keymgmt utility */
165 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
)
166 return evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
169 * If |to| is provided, we know that |from| is legacy at this point.
170 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
171 * to copy the appropriate data to |to|'s keydata.
173 if (to
->keymgmt
!= NULL
) {
174 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
176 evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
180 * If we get a NULL, it could be an internal error, or it could be
181 * that there's a key mismatch. We're pretending the latter...
183 if (from_keydata
== NULL
) {
184 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
187 return evp_keymgmt_copy(to
->keymgmt
, to
->keydata
, from_keydata
,
191 /* Both keys are legacy */
192 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
193 return from
->ameth
->param_copy(to
, from
);
198 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
201 if (pkey
->keymgmt
!= NULL
)
202 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
203 else if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
204 return pkey
->ameth
->param_missing(pkey
);
210 * This function is called for any mixture of keys except pure legacy pair.
211 * TODO When legacy keys are gone, we replace a call to this functions with
212 * a call to evp_keymgmt_util_match().
214 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
217 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
218 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
220 /* If none of them are provided, this function shouldn't have been called */
221 if (!ossl_assert(a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
))
224 /* For purely provided keys, we just call the keymgmt utility */
225 if (a
->keymgmt
!= NULL
&& b
->keymgmt
!= NULL
)
226 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
229 * At this point, one of them is provided, the other not. This allows
230 * us to compare types using legacy NIDs.
232 if ((a
->type
!= EVP_PKEY_NONE
233 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
234 || (b
->type
!= EVP_PKEY_NONE
235 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
))))
236 return -1; /* not the same key type */
239 * We've determined that they both are the same keytype, so the next
240 * step is to do a bit of cross export to ensure we have keydata for
241 * both keys in the same keymgmt.
243 keymgmt1
= a
->keymgmt
;
244 keydata1
= a
->keydata
;
245 keymgmt2
= b
->keymgmt
;
246 keydata2
= b
->keydata
;
248 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
250 evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
251 if (tmp_keydata
!= NULL
) {
253 keydata1
= tmp_keydata
;
256 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
258 evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
259 if (tmp_keydata
!= NULL
) {
261 keydata2
= tmp_keydata
;
265 /* If we still don't have matching keymgmt implementations, we give up */
266 if (keymgmt1
!= keymgmt2
)
269 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
272 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
275 * TODO: clean up legacy stuff from this function when legacy support
279 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
280 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
282 /* All legacy keys */
283 if (a
->type
!= b
->type
)
285 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
286 return a
->ameth
->param_cmp(a
, b
);
290 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
293 * TODO: clean up legacy stuff from this function when legacy support
297 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
298 return evp_pkey_cmp_any(a
, b
, (SELECT_PARAMETERS
299 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY
));
301 /* All legacy keys */
302 if (a
->type
!= b
->type
)
305 if (a
->ameth
!= NULL
) {
307 /* Compare parameters if the algorithm has them */
308 if (a
->ameth
->param_cmp
!= NULL
) {
309 ret
= a
->ameth
->param_cmp(a
, b
);
314 if (a
->ameth
->pub_cmp
!= NULL
)
315 return a
->ameth
->pub_cmp(a
, b
);
321 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
322 const unsigned char *priv
,
325 EVP_PKEY
*ret
= EVP_PKEY_new();
328 || !pkey_set_type(ret
, e
, type
, NULL
, -1, NULL
)) {
329 /* EVPerr already called */
333 if (ret
->ameth
->set_priv_key
== NULL
) {
334 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
,
335 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
339 if (!ret
->ameth
->set_priv_key(ret
, priv
, len
)) {
340 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY
, EVP_R_KEY_SETUP_FAILED
);
351 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
352 const unsigned char *pub
,
355 EVP_PKEY
*ret
= EVP_PKEY_new();
358 || !pkey_set_type(ret
, e
, type
, NULL
, -1, NULL
)) {
359 /* EVPerr already called */
363 if (ret
->ameth
->set_pub_key
== NULL
) {
364 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
,
365 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
369 if (!ret
->ameth
->set_pub_key(ret
, pub
, len
)) {
370 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY
, EVP_R_KEY_SETUP_FAILED
);
381 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
384 /* TODO(3.0) Do we need to do anything about provider side keys? */
385 if (pkey
->ameth
->get_priv_key
== NULL
) {
386 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
,
387 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
391 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
392 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
399 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
402 /* TODO(3.0) Do we need to do anything about provider side keys? */
403 if (pkey
->ameth
->get_pub_key
== NULL
) {
404 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
,
405 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
409 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
410 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY
, EVP_R_GET_RAW_KEY_FAILED
);
417 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
418 size_t len
, const EVP_CIPHER
*cipher
)
420 # ifndef OPENSSL_NO_CMAC
421 # ifndef OPENSSL_NO_ENGINE
422 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
424 const char *cipher_name
= EVP_CIPHER_name(cipher
);
425 const OSSL_PROVIDER
*prov
= EVP_CIPHER_provider(cipher
);
426 OPENSSL_CTX
*libctx
=
427 prov
== NULL
? NULL
: ossl_provider_library_context(prov
);
428 EVP_PKEY
*ret
= EVP_PKEY_new();
429 EVP_MAC
*cmac
= EVP_MAC_fetch(libctx
, OSSL_MAC_NAME_CMAC
, NULL
);
430 EVP_MAC_CTX
*cmctx
= cmac
!= NULL
? EVP_MAC_CTX_new(cmac
) : NULL
;
431 OSSL_PARAM params
[4];
436 || !pkey_set_type(ret
, e
, EVP_PKEY_CMAC
, NULL
, -1, NULL
)) {
437 /* EVPerr already called */
441 # ifndef OPENSSL_NO_ENGINE
442 if (engine_id
!= NULL
)
444 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id
, 0);
448 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER
,
449 (char *)cipher_name
, 0);
451 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
453 params
[paramsn
] = OSSL_PARAM_construct_end();
455 if (!EVP_MAC_CTX_set_params(cmctx
, params
)) {
456 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
, EVP_R_KEY_SETUP_FAILED
);
460 ret
->pkey
.ptr
= cmctx
;
465 EVP_MAC_CTX_free(cmctx
);
469 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY
,
470 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
475 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
477 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1, NULL
);
480 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
482 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
, NULL
);
485 int EVP_PKEY_set_alias_type(EVP_PKEY
*pkey
, int type
)
487 if (pkey
->type
== type
) {
488 return 1; /* it already is that type */
492 * The application is requesting to alias this to a different pkey type,
493 * but not one that resolves to the base type.
495 if (EVP_PKEY_type(type
) != EVP_PKEY_base_id(pkey
)) {
496 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
504 # ifndef OPENSSL_NO_ENGINE
505 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
508 if (!ENGINE_init(e
)) {
509 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, ERR_R_ENGINE_LIB
);
512 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
514 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE
, EVP_R_UNSUPPORTED_ALGORITHM
);
518 ENGINE_finish(pkey
->pmeth_engine
);
519 pkey
->pmeth_engine
= e
;
523 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
528 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
532 #ifndef OPENSSL_NO_EC
533 if (EVP_PKEY_type(type
) == EVP_PKEY_EC
) {
534 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
536 if (group
!= NULL
&& EC_GROUP_get_curve_name(group
) == NID_sm2
)
537 alias
= EVP_PKEY_SM2
;
541 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
543 if (!EVP_PKEY_set_alias_type(pkey
, alias
))
545 pkey
->pkey
.ptr
= key
;
546 return (key
!= NULL
);
549 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
551 if (!evp_pkey_downgrade((EVP_PKEY
*)pkey
)) {
552 ERR_raise(ERR_LIB_EVP
, EVP_R_INACCESSIBLE_KEY
);
555 return pkey
->pkey
.ptr
;
558 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
560 ASN1_OCTET_STRING
*os
= NULL
;
561 if (pkey
->type
!= EVP_PKEY_HMAC
) {
562 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC
, EVP_R_EXPECTING_AN_HMAC_KEY
);
565 os
= EVP_PKEY_get0(pkey
);
570 # ifndef OPENSSL_NO_POLY1305
571 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
573 ASN1_OCTET_STRING
*os
= NULL
;
574 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
575 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305
, EVP_R_EXPECTING_A_POLY1305_KEY
);
578 os
= EVP_PKEY_get0(pkey
);
584 # ifndef OPENSSL_NO_SIPHASH
585 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
587 ASN1_OCTET_STRING
*os
= NULL
;
589 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
590 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
593 os
= EVP_PKEY_get0(pkey
);
599 # ifndef OPENSSL_NO_RSA
600 int EVP_PKEY_set1_RSA(EVP_PKEY
*pkey
, RSA
*key
)
602 int ret
= EVP_PKEY_assign_RSA(pkey
, key
);
608 RSA
*EVP_PKEY_get0_RSA(const EVP_PKEY
*pkey
)
610 if (!evp_pkey_downgrade((EVP_PKEY
*)pkey
)) {
611 ERR_raise(ERR_LIB_EVP
, EVP_R_INACCESSIBLE_KEY
);
614 if (pkey
->type
!= EVP_PKEY_RSA
&& pkey
->type
!= EVP_PKEY_RSA_PSS
) {
615 EVPerr(EVP_F_EVP_PKEY_GET0_RSA
, EVP_R_EXPECTING_AN_RSA_KEY
);
618 return pkey
->pkey
.rsa
;
621 RSA
*EVP_PKEY_get1_RSA(EVP_PKEY
*pkey
)
623 RSA
*ret
= EVP_PKEY_get0_RSA(pkey
);
630 # ifndef OPENSSL_NO_DSA
631 DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
633 if (!evp_pkey_downgrade((EVP_PKEY
*)pkey
)) {
634 ERR_raise(ERR_LIB_EVP
, EVP_R_INACCESSIBLE_KEY
);
637 if (pkey
->type
!= EVP_PKEY_DSA
) {
638 EVPerr(EVP_F_EVP_PKEY_GET0_DSA
, EVP_R_EXPECTING_A_DSA_KEY
);
641 return pkey
->pkey
.dsa
;
644 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
646 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
651 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
653 DSA
*ret
= EVP_PKEY_get0_DSA(pkey
);
658 # endif /* OPENSSL_NO_DSA */
659 #endif /* FIPS_MODE */
662 # ifndef OPENSSL_NO_EC
663 int EVP_PKEY_set1_EC_KEY(EVP_PKEY
*pkey
, EC_KEY
*key
)
665 int ret
= EVP_PKEY_assign_EC_KEY(pkey
, key
);
671 EC_KEY
*EVP_PKEY_get0_EC_KEY(const EVP_PKEY
*pkey
)
673 if (!evp_pkey_downgrade((EVP_PKEY
*)pkey
)) {
674 ERR_raise(ERR_LIB_EVP
, EVP_R_INACCESSIBLE_KEY
);
677 if (EVP_PKEY_base_id(pkey
) != EVP_PKEY_EC
) {
678 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY
, EVP_R_EXPECTING_A_EC_KEY
);
681 return pkey
->pkey
.ec
;
684 EC_KEY
*EVP_PKEY_get1_EC_KEY(EVP_PKEY
*pkey
)
686 EC_KEY
*ret
= EVP_PKEY_get0_EC_KEY(pkey
);
693 # ifndef OPENSSL_NO_DH
695 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
697 int type
= DH_get0_q(key
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
698 int ret
= EVP_PKEY_assign(pkey
, type
, key
);
705 DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
707 if (!evp_pkey_downgrade((EVP_PKEY
*)pkey
)) {
708 ERR_raise(ERR_LIB_EVP
, EVP_R_INACCESSIBLE_KEY
);
711 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
712 EVPerr(EVP_F_EVP_PKEY_GET0_DH
, EVP_R_EXPECTING_A_DH_KEY
);
715 return pkey
->pkey
.dh
;
718 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
720 DH
*ret
= EVP_PKEY_get0_DH(pkey
);
727 int EVP_PKEY_type(int type
)
730 const EVP_PKEY_ASN1_METHOD
*ameth
;
732 ameth
= EVP_PKEY_asn1_find(&e
, type
);
734 ret
= ameth
->pkey_id
;
737 # ifndef OPENSSL_NO_ENGINE
743 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
748 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
750 return EVP_PKEY_type(pkey
->type
);
753 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
756 if (pkey
->keymgmt
== NULL
) {
758 * These hard coded cases are pure hackery to get around the fact
759 * that names in crypto/objects/objects.txt are a mess. There is
760 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
761 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
762 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
763 * "DSA" is accurate... but still, better be safe and hard-code
764 * names that we know.
765 * TODO Clean this away along with all other #legacy support.
769 if (strcasecmp(name
, "RSA") == 0)
771 #ifndef OPENSSL_NO_EC
772 else if (strcasecmp(name
, "EC") == 0)
775 #ifndef OPENSSL_NO_DSA
776 else if (strcasecmp(name
, "DSA") == 0)
780 type
= EVP_PKEY_type(OBJ_sn2nid(name
));
781 return EVP_PKEY_type(pkey
->type
) == type
;
784 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
787 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
789 if (pkey
->keymgmt
== NULL
) {
790 switch (EVP_PKEY_base_id(pkey
)) {
793 #ifndef OPENSSL_NO_DSA
797 #ifndef OPENSSL_NO_EC
798 case EVP_PKEY_ED25519
:
801 case EVP_PKEY_EC
: /* Including SM2 */
802 return EC_KEY_can_sign(pkey
->pkey
.ec
);
808 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_provider(pkey
->keymgmt
);
809 OPENSSL_CTX
*libctx
= ossl_provider_library_context(prov
);
810 const char *supported_sig
=
811 pkey
->keymgmt
->query_operation_name
!= NULL
812 ? pkey
->keymgmt
->query_operation_name(OSSL_OP_SIGNATURE
)
813 : evp_first_name(prov
, pkey
->keymgmt
->name_id
);
814 EVP_SIGNATURE
*signature
= NULL
;
816 signature
= EVP_SIGNATURE_fetch(libctx
, supported_sig
, NULL
);
817 if (signature
!= NULL
) {
818 EVP_SIGNATURE_free(signature
);
825 #ifndef OPENSSL_NO_EC
827 * TODO rewrite when we have proper data extraction functions
828 * Note: an octet pointer would be desirable!
830 static OSSL_CALLBACK get_ec_curve_name_cb
;
831 static int get_ec_curve_name_cb(const OSSL_PARAM params
[], void *arg
)
833 const OSSL_PARAM
*p
= NULL
;
835 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_EC_NAME
)) != NULL
)
836 return OSSL_PARAM_get_utf8_string(p
, arg
, 0);
838 /* If there is no curve name, this is not an EC key */
842 int evp_pkey_get_EC_KEY_curve_nid(const EVP_PKEY
*pkey
)
846 if (pkey
->keymgmt
== NULL
) {
847 if (EVP_PKEY_base_id(pkey
) == EVP_PKEY_EC
) {
848 EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
850 ret
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
852 } else if (EVP_PKEY_is_a(pkey
, "EC") || EVP_PKEY_is_a(pkey
, "SM2")) {
853 char *curve_name
= NULL
;
855 ret
= evp_keymgmt_export(pkey
->keymgmt
, pkey
->keydata
,
856 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
857 get_ec_curve_name_cb
, &curve_name
);
859 ret
= ec_curve_name2nid(curve_name
);
860 OPENSSL_free(curve_name
);
867 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
869 BIO_set_indent(*out
, saved_indent
);
871 BIO
*next
= BIO_pop(*out
);
879 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
885 long i
= BIO_get_indent(*out
);
887 *saved_indent
= (i
< 0 ? 0 : i
);
888 if (BIO_set_indent(*out
, indent
) <= 0) {
889 if ((*out
= BIO_push(BIO_new(BIO_f_prefix()), *out
)) == NULL
)
893 if (BIO_set_indent(*out
, indent
) <= 0) {
894 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
901 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
904 return BIO_indent(out
, indent
, 128)
905 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
906 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
909 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
910 const char *propquery
/* For provided serialization */,
911 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
912 int indent
, ASN1_PCTX
*pctx
),
913 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
917 OSSL_SERIALIZER_CTX
*ctx
= NULL
;
918 int ret
= -2; /* default to unsupported */
920 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
923 ctx
= OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey
, propquery
);
924 if (OSSL_SERIALIZER_CTX_get_serializer(ctx
) != NULL
)
925 ret
= OSSL_SERIALIZER_to_bio(ctx
, out
);
926 OSSL_SERIALIZER_CTX_free(ctx
);
931 /* legacy fallback */
932 if (legacy_print
!= NULL
)
933 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
935 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
938 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
942 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
943 int indent
, ASN1_PCTX
*pctx
)
945 return print_pkey(pkey
, out
, indent
, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ
,
946 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
950 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
951 int indent
, ASN1_PCTX
*pctx
)
953 return print_pkey(pkey
, out
, indent
, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ
,
954 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
958 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
959 int indent
, ASN1_PCTX
*pctx
)
961 return print_pkey(pkey
, out
, indent
, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ
,
962 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
966 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
967 int arg1
, void *arg2
)
969 if (pkey
->keymgmt
== NULL
)
972 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
974 char mdname
[80] = "";
976 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
981 nid
= OBJ_sn2nid(mdname
);
982 if (nid
== NID_undef
)
983 nid
= OBJ_ln2nid(mdname
);
984 if (nid
== NID_undef
)
994 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
996 if (pkey
->ameth
== NULL
)
997 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
998 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1000 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1003 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1005 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1008 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1009 char *mdname
, size_t mdname_sz
)
1011 if (pkey
->ameth
== NULL
)
1012 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1017 int nid
= NID_undef
;
1018 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1019 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1022 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1027 int EVP_PKEY_supports_digest_nid(EVP_PKEY
*pkey
, int nid
)
1029 int rv
, default_nid
;
1031 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SUPPORTS_MD_NID
, nid
, NULL
);
1034 * If there is a mandatory default digest and this isn't it, then
1035 * the answer is 'no'.
1037 rv
= EVP_PKEY_get_default_digest_nid(pkey
, &default_nid
);
1039 return (nid
== default_nid
);
1040 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
1047 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY
*pkey
,
1048 const unsigned char *pt
, size_t ptlen
)
1050 if (ptlen
> INT_MAX
)
1052 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, ptlen
,
1058 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY
*pkey
, unsigned char **ppt
)
1061 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppt
);
1067 #endif /* FIPS_MODE */
1069 /*- All methods below can also be used in FIPS_MODE */
1071 EVP_PKEY
*EVP_PKEY_new(void)
1073 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1076 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
1079 ret
->type
= EVP_PKEY_NONE
;
1080 ret
->save_type
= EVP_PKEY_NONE
;
1081 ret
->references
= 1;
1082 ret
->save_parameters
= 1;
1083 ret
->lock
= CRYPTO_THREAD_lock_new();
1084 if (ret
->lock
== NULL
) {
1085 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
1089 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1090 EVPerr(EVP_F_EVP_PKEY_NEW
, ERR_R_MALLOC_FAILURE
);
1097 CRYPTO_THREAD_lock_free(ret
->lock
);
1103 * Setup a public key management method.
1105 * For legacy keys, either |type| or |str| is expected to have the type
1106 * information. In this case, the setup consists of finding an ASN1 method
1107 * and potentially an ENGINE, and setting those fields in |pkey|.
1109 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1110 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1112 * If pkey is NULL just return 1 or 0 if the key management method exists.
1115 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
1116 int len
, EVP_KEYMGMT
*keymgmt
)
1119 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1120 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
1124 * The setups can't set both legacy and provider side methods.
1127 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)
1128 || !ossl_assert(e
== NULL
|| keymgmt
== NULL
)) {
1129 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1137 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1139 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1141 evp_pkey_free_it(pkey
);
1144 * If key type matches and a method exists then this lookup has
1145 * succeeded once so just indicate success.
1147 if (pkey
->type
!= EVP_PKEY_NONE
1148 && type
== pkey
->save_type
1149 && pkey
->ameth
!= NULL
)
1151 # ifndef OPENSSL_NO_ENGINE
1152 /* If we have ENGINEs release them */
1153 ENGINE_finish(pkey
->engine
);
1154 pkey
->engine
= NULL
;
1155 ENGINE_finish(pkey
->pmeth_engine
);
1156 pkey
->pmeth_engine
= NULL
;
1162 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
1163 else if (type
!= EVP_PKEY_NONE
)
1164 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
1165 # ifndef OPENSSL_NO_ENGINE
1166 if (pkey
== NULL
&& eptr
!= NULL
)
1176 check
= check
&& ameth
== NULL
;
1178 check
= check
&& keymgmt
== NULL
;
1180 EVPerr(EVP_F_PKEY_SET_TYPE
, EVP_R_UNSUPPORTED_ALGORITHM
);
1185 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1186 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1190 pkey
->keymgmt
= keymgmt
;
1192 pkey
->save_type
= type
;
1197 * If the internal "origin" key is provider side, don't save |ameth|.
1198 * The main reason is that |ameth| is one factor to detect that the
1199 * internal "origin" key is a legacy one.
1201 if (keymgmt
== NULL
)
1202 pkey
->ameth
= ameth
;
1206 * The EVP_PKEY_ASN1_METHOD |pkey_id| serves different purposes,
1207 * depending on if we're setting this key to contain a legacy or
1208 * a provider side "origin" key. For a legacy key, we assign it
1209 * to the |type| field, but for a provider side key, we assign it
1210 * to the |save_type| field, because |type| is supposed to be set
1211 * to EVP_PKEY_NONE in that case.
1213 if (keymgmt
!= NULL
)
1214 pkey
->save_type
= ameth
->pkey_id
;
1215 else if (pkey
->ameth
!= NULL
)
1216 pkey
->type
= ameth
->pkey_id
;
1223 static void find_ameth(const char *name
, void *data
)
1225 const char **str
= data
;
1228 * The error messages from pkey_set_type() are uninteresting here,
1233 if (pkey_set_type(NULL
, NULL
, EVP_PKEY_NONE
, name
, strlen(name
),
1237 else if (str
[1] == NULL
)
1245 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1248 # define EVP_PKEY_TYPE_STR str[0]
1249 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1251 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1252 * Ideally, only one should be found. If two (or more) are found, the
1253 * match is ambiguous. This should never happen, but...
1255 const char *str
[2] = { NULL
, NULL
};
1257 EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
);
1258 if (str
[1] != NULL
) {
1259 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1263 # define EVP_PKEY_TYPE_STR NULL
1264 # define EVP_PKEY_TYPE_STRLEN -1
1266 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
,
1267 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1270 #undef EVP_PKEY_TYPE_STR
1271 #undef EVP_PKEY_TYPE_STRLEN
1274 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1278 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
1281 REF_PRINT_COUNT("EVP_PKEY", pkey
);
1282 REF_ASSERT_ISNT(i
< 2);
1283 return ((i
> 1) ? 1 : 0);
1287 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1289 if (x
->ameth
!= NULL
) {
1290 if (x
->ameth
->pkey_free
!= NULL
)
1291 x
->ameth
->pkey_free(x
);
1294 # ifndef OPENSSL_NO_ENGINE
1295 ENGINE_finish(x
->engine
);
1297 ENGINE_finish(x
->pmeth_engine
);
1298 x
->pmeth_engine
= NULL
;
1300 x
->type
= EVP_PKEY_NONE
;
1302 #endif /* FIPS_MODE */
1304 static void evp_pkey_free_it(EVP_PKEY
*x
)
1306 /* internal function; x is never NULL */
1308 evp_keymgmt_util_clear_operation_cache(x
);
1310 evp_pkey_free_legacy(x
);
1313 if (x
->keymgmt
!= NULL
) {
1314 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1315 EVP_KEYMGMT_free(x
->keymgmt
);
1321 void EVP_PKEY_free(EVP_PKEY
*x
)
1328 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
1329 REF_PRINT_COUNT("EVP_PKEY", x
);
1332 REF_ASSERT_ISNT(i
< 0);
1333 evp_pkey_free_it(x
);
1335 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1337 CRYPTO_THREAD_lock_free(x
->lock
);
1339 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1344 int EVP_PKEY_size(const EVP_PKEY
*pkey
)
1349 size
= pkey
->cache
.size
;
1351 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1352 size
= pkey
->ameth
->pkey_size(pkey
);
1358 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OPENSSL_CTX
*libctx
,
1359 EVP_KEYMGMT
**keymgmt
,
1360 const char *propquery
)
1362 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1363 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1364 void *keydata
= NULL
;
1370 /* No key data => nothing to export */
1373 check
= check
&& pk
->pkey
.ptr
== NULL
;
1375 check
= check
&& pk
->keydata
== NULL
;
1380 if (pk
->pkey
.ptr
!= NULL
) {
1382 * If the legacy key doesn't have an dirty counter or export function,
1385 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1390 if (keymgmt
!= NULL
) {
1391 tmp_keymgmt
= *keymgmt
;
1396 * If no keymgmt was given or found, get a default keymgmt. We do so by
1397 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1399 if (tmp_keymgmt
== NULL
) {
1400 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1402 tmp_keymgmt
= ctx
->keymgmt
;
1403 ctx
->keymgmt
= NULL
;
1404 EVP_PKEY_CTX_free(ctx
);
1407 /* If there's still no keymgmt to be had, give up */
1408 if (tmp_keymgmt
== NULL
)
1412 if (pk
->pkey
.ptr
!= NULL
) {
1416 * If the legacy "origin" hasn't changed since last time, we try
1417 * to find our keymgmt in the operation cache. If it has changed,
1418 * |i| remains zero, and we will clear the cache further down.
1420 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1421 i
= evp_keymgmt_util_find_operation_cache_index(pk
, tmp_keymgmt
);
1424 * If |tmp_keymgmt| is present in the operation cache, it means
1425 * that export doesn't need to be redone. In that case, we take
1426 * token copies of the cached pointers, to have token success
1429 if (i
< OSSL_NELEM(pk
->operation_cache
)
1430 && pk
->operation_cache
[i
].keymgmt
!= NULL
) {
1431 keydata
= pk
->operation_cache
[i
].keydata
;
1437 * TODO(3.0) Right now, we assume we have ample space. We will have
1438 * to think about a cache aging scheme, though, if |i| indexes outside
1441 if (!ossl_assert(i
< OSSL_NELEM(pk
->operation_cache
)))
1444 /* Make sure that the keymgmt key type matches the legacy NID */
1445 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
))))
1448 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
1451 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
, libctx
, propquery
)) {
1452 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1458 * If the dirty counter changed since last time, then clear the
1459 * operation cache. In that case, we know that |i| is zero. Just
1460 * in case this is a re-export, we increment then decrement the
1461 * keymgmt reference counter.
1463 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
1464 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1468 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
)
1469 evp_keymgmt_util_clear_operation_cache(pk
);
1470 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
1472 /* Add the new export to the operation cache */
1473 if (!evp_keymgmt_util_cache_keydata(pk
, i
, tmp_keymgmt
, keydata
)) {
1474 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1479 /* Synchronize the dirty count */
1480 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1483 #endif /* FIPS_MODE */
1485 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
);
1489 * If nothing was exported, |tmp_keymgmt| might point at a freed
1490 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1491 * the caller either way in that case.
1493 if (keydata
== NULL
)
1496 if (keymgmt
!= NULL
)
1497 *keymgmt
= tmp_keymgmt
;
1499 EVP_KEYMGMT_free(allocated_keymgmt
);
1504 int evp_pkey_downgrade(EVP_PKEY
*pk
)
1506 EVP_KEYMGMT
*keymgmt
= pk
->keymgmt
;
1507 void *keydata
= pk
->keydata
;
1508 int type
= pk
->save_type
;
1509 const char *keytype
= NULL
;
1511 /* If this isn't a provider side key, we're done */
1512 if (keymgmt
== NULL
)
1515 /* Get the key type name for error reporting */
1516 if (type
!= EVP_PKEY_NONE
)
1517 keytype
= OBJ_nid2sn(type
);
1520 evp_first_name(EVP_KEYMGMT_provider(keymgmt
), keymgmt
->name_id
);
1523 * |save_type| was set when any of the EVP_PKEY_set_type functions
1524 * was called. It was set to EVP_PKEY_NONE if the key type wasn't
1525 * recognised to be any of the legacy key types, and the downgrade
1528 if (type
== EVP_PKEY_NONE
) {
1529 ERR_raise_data(ERR_LIB_EVP
, EVP_R_UNKNOWN_KEY_TYPE
,
1530 "key type = %s, can't downgrade", keytype
);
1535 * To be able to downgrade, we steal the provider side "origin" keymgmt
1536 * and keydata. We've already grabbed the pointers, so all we need to
1537 * do is clear those pointers in |pk| and then call evp_pkey_free_it().
1538 * That way, we can restore |pk| if we need to.
1542 evp_pkey_free_it(pk
);
1543 if (EVP_PKEY_set_type(pk
, type
)) {
1544 /* If the key is typed but empty, we're done */
1545 if (keydata
== NULL
) {
1546 /* We're dropping the EVP_KEYMGMT */
1547 EVP_KEYMGMT_free(keymgmt
);
1551 if (pk
->ameth
->import_from
== NULL
) {
1552 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
1553 "key type = %s", keytype
);
1556 * We perform the export in the same libctx as the keymgmt that we
1559 OPENSSL_CTX
*libctx
= ossl_provider_library_context(keymgmt
->prov
);
1560 EVP_PKEY_CTX
*pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, NULL
);
1562 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1565 && evp_keymgmt_export(keymgmt
, keydata
,
1566 OSSL_KEYMGMT_SELECT_ALL
,
1567 pk
->ameth
->import_from
, pctx
)) {
1569 * Save the provider side data in the operation cache, so they'll
1570 * find it again. evp_pkey_free_it() cleared the cache, so it's
1571 * safe to assume slot zero is free.
1572 * Note that evp_keymgmt_util_cache_keydata() increments keymgmt's
1575 evp_keymgmt_util_cache_keydata(pk
, 0, keymgmt
, keydata
);
1576 EVP_PKEY_CTX_free(pctx
);
1578 /* Synchronize the dirty count */
1579 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1581 /* evp_keymgmt_export() increased the refcount... */
1582 EVP_KEYMGMT_free(keymgmt
);
1585 EVP_PKEY_CTX_free(pctx
);
1588 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
1589 "key type = %s", keytype
);
1593 * Something went wrong. This could for example happen if the keymgmt
1594 * turns out to be an HSM implementation that refuses to let go of some
1595 * of the key data, typically the private bits. In this case, we restore
1596 * the provider side internal "origin" and leave it at that.
1598 if (!ossl_assert(EVP_PKEY_set_type_by_keymgmt(pk
, keymgmt
))) {
1599 /* This should not be impossible */
1600 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1603 /* EVP_PKEY_set_type_by_keymgmt() increased the refcount... */
1604 EVP_KEYMGMT_free(keymgmt
);
1605 pk
->keydata
= keydata
;
1606 evp_keymgmt_util_cache_keyinfo(pk
);
1607 return 0; /* No downgrade, but at least the key is restored */
1609 #endif /* FIPS_MODE */
1611 const OSSL_PARAM
*EVP_PKEY_gettable_params(EVP_PKEY
*pkey
)
1614 || pkey
->keymgmt
== NULL
1615 || pkey
->keydata
== NULL
)
1617 return evp_keymgmt_gettable_params(pkey
->keymgmt
);
1621 * For the following methods param->return_size is set to a value
1622 * larger than can be returned by the call to evp_keymgmt_get_params().
1623 * If it is still this value then the parameter was ignored - and in this
1624 * case it returns an error..
1627 int EVP_PKEY_get_bn_param(EVP_PKEY
*pkey
, const char *key_name
, BIGNUM
**bn
)
1630 OSSL_PARAM params
[2];
1631 unsigned char buffer
[2048];
1633 * Use -1 as the terminator here instead of sizeof(buffer) + 1 since
1634 * -1 is less likely to be a valid value.
1636 const size_t not_set
= (size_t)-1;
1637 unsigned char *buf
= NULL
;
1641 || pkey
->keymgmt
== NULL
1642 || pkey
->keydata
== NULL
1647 memset(buffer
, 0, sizeof(buffer
));
1648 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
1649 /* If the return_size is still not_set then we know it was not found */
1650 params
[0].return_size
= not_set
;
1651 params
[1] = OSSL_PARAM_construct_end();
1652 if (!evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
)) {
1653 if (params
[0].return_size
== not_set
1654 || params
[0].return_size
== 0)
1656 buf_sz
= params
[0].return_size
;
1658 * If it failed because the buffer was too small then allocate the
1659 * required buffer size and retry.
1661 buf
= OPENSSL_zalloc(buf_sz
);
1664 params
[0].data
= buf
;
1665 params
[0].data_size
= buf_sz
;
1667 if (!evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
))
1670 /* Fail if the param was not found */
1671 if (params
[0].return_size
== not_set
)
1673 ret
= OSSL_PARAM_get_BN(params
, bn
);
1679 int EVP_PKEY_get_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
1680 unsigned char *buf
, size_t max_buf_sz
,
1683 OSSL_PARAM params
[2];
1684 const size_t not_set
= max_buf_sz
+ 1;
1687 || pkey
->keymgmt
== NULL
1688 || pkey
->keydata
== NULL
1689 || key_name
== NULL
)
1692 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
1693 params
[0].return_size
= not_set
;
1694 params
[1] = OSSL_PARAM_construct_end();
1695 if (!evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
))
1697 if (params
[0].return_size
== not_set
)
1700 *out_sz
= params
[0].return_size
;
1704 int EVP_PKEY_get_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
1705 char *str
, size_t max_buf_sz
,
1708 OSSL_PARAM params
[2];
1709 const size_t not_set
= max_buf_sz
+ 1;
1712 || pkey
->keymgmt
== NULL
1713 || pkey
->keydata
== NULL
1714 || key_name
== NULL
)
1717 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
1718 params
[0].return_size
= not_set
;
1719 params
[1] = OSSL_PARAM_construct_end();
1720 if (!evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
))
1722 if (params
[0].return_size
== not_set
)
1725 *out_sz
= params
[0].return_size
;
1729 int EVP_PKEY_get_int_param(EVP_PKEY
*pkey
, const char *key_name
, int *out
)
1731 OSSL_PARAM params
[2];
1732 const size_t not_set
= sizeof(int) + 1;
1735 || pkey
->keymgmt
== NULL
1736 || pkey
->keydata
== NULL
1737 || key_name
== NULL
)
1740 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
1741 params
[0].return_size
= not_set
;
1742 params
[1] = OSSL_PARAM_construct_end();
1743 if (!evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
))
1745 if (params
[0].return_size
== not_set
)
1750 int EVP_PKEY_get_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t *out
)
1752 OSSL_PARAM params
[2];
1753 const size_t not_set
= sizeof(size_t) + 1;
1756 || pkey
->keymgmt
== NULL
1757 || pkey
->keydata
== NULL
1758 || key_name
== NULL
)
1761 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
1762 params
[0].return_size
= not_set
;
1763 params
[1] = OSSL_PARAM_construct_end();
1764 if (!evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
))
1766 if (params
[0].return_size
== not_set
)