2 * Copyright 2006-2021 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 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/bn.h>
21 #include <openssl/core_names.h>
22 #include <openssl/param_build.h>
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
25 #include "crypto/rsa.h"
26 #include "rsa_local.h"
28 static RSA_PSS_PARAMS
*rsa_pss_decode(const X509_ALGOR
*alg
);
29 static int rsa_sync_to_pss_params_30(RSA
*rsa
);
31 /* Set any parameters associated with pkey */
32 static int rsa_param_encode(const EVP_PKEY
*pkey
,
33 ASN1_STRING
**pstr
, int *pstrtype
)
35 const RSA
*rsa
= pkey
->pkey
.rsa
;
38 /* If RSA it's just NULL type */
39 if (RSA_test_flags(rsa
, RSA_FLAG_TYPE_MASK
) != RSA_FLAG_TYPE_RSASSAPSS
) {
40 *pstrtype
= V_ASN1_NULL
;
43 /* If no PSS parameters we omit parameters entirely */
44 if (rsa
->pss
== NULL
) {
45 *pstrtype
= V_ASN1_UNDEF
;
48 /* Encode PSS parameters */
49 if (ASN1_item_pack(rsa
->pss
, ASN1_ITEM_rptr(RSA_PSS_PARAMS
), pstr
) == NULL
)
52 *pstrtype
= V_ASN1_SEQUENCE
;
55 /* Decode any parameters and set them in RSA structure */
56 static int rsa_param_decode(RSA
*rsa
, const X509_ALGOR
*alg
)
58 const ASN1_OBJECT
*algoid
;
62 X509_ALGOR_get0(&algoid
, &algptype
, &algp
, alg
);
63 if (OBJ_obj2nid(algoid
) != EVP_PKEY_RSA_PSS
)
65 if (algptype
== V_ASN1_UNDEF
)
67 if (algptype
!= V_ASN1_SEQUENCE
) {
68 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_PARAMETERS
);
71 rsa
->pss
= rsa_pss_decode(alg
);
74 if (!rsa_sync_to_pss_params_30(rsa
))
79 static int rsa_pub_encode(X509_PUBKEY
*pk
, const EVP_PKEY
*pkey
)
81 unsigned char *penc
= NULL
;
86 if (!rsa_param_encode(pkey
, &str
, &strtype
))
88 penclen
= i2d_RSAPublicKey(pkey
->pkey
.rsa
, &penc
);
91 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(pkey
->ameth
->pkey_id
),
92 strtype
, str
, penc
, penclen
))
99 static int rsa_pub_decode(EVP_PKEY
*pkey
, const X509_PUBKEY
*pubkey
)
101 const unsigned char *p
;
106 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &alg
, pubkey
))
108 if ((rsa
= d2i_RSAPublicKey(NULL
, &p
, pklen
)) == NULL
)
110 if (!rsa_param_decode(rsa
, alg
)) {
115 RSA_clear_flags(rsa
, RSA_FLAG_TYPE_MASK
);
116 switch (pkey
->ameth
->pkey_id
) {
118 RSA_set_flags(rsa
, RSA_FLAG_TYPE_RSA
);
120 case EVP_PKEY_RSA_PSS
:
121 RSA_set_flags(rsa
, RSA_FLAG_TYPE_RSASSAPSS
);
124 /* Leave the type bits zero */
128 if (!EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, rsa
)) {
135 static int rsa_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
138 * Don't check the public/private key, this is mostly for smart
141 if (((RSA_flags(a
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
))
142 || (RSA_flags(b
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
)) {
146 if (BN_cmp(b
->pkey
.rsa
->n
, a
->pkey
.rsa
->n
) != 0
147 || BN_cmp(b
->pkey
.rsa
->e
, a
->pkey
.rsa
->e
) != 0)
152 static int old_rsa_priv_decode(EVP_PKEY
*pkey
,
153 const unsigned char **pder
, int derlen
)
157 if ((rsa
= d2i_RSAPrivateKey(NULL
, pder
, derlen
)) == NULL
)
159 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, rsa
);
163 static int old_rsa_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
165 return i2d_RSAPrivateKey(pkey
->pkey
.rsa
, pder
);
168 static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
170 unsigned char *rk
= NULL
;
175 if (!rsa_param_encode(pkey
, &str
, &strtype
))
177 rklen
= i2d_RSAPrivateKey(pkey
->pkey
.rsa
, &rk
);
180 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
181 ASN1_STRING_free(str
);
185 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(pkey
->ameth
->pkey_id
), 0,
186 strtype
, str
, rk
, rklen
)) {
187 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
188 ASN1_STRING_free(str
);
195 static int rsa_priv_decode(EVP_PKEY
*pkey
, const PKCS8_PRIV_KEY_INFO
*p8
)
197 const unsigned char *p
;
200 const X509_ALGOR
*alg
;
202 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &alg
, p8
))
204 rsa
= d2i_RSAPrivateKey(NULL
, &p
, pklen
);
206 ERR_raise(ERR_LIB_RSA
, ERR_R_RSA_LIB
);
209 if (!rsa_param_decode(rsa
, alg
)) {
214 RSA_clear_flags(rsa
, RSA_FLAG_TYPE_MASK
);
215 switch (pkey
->ameth
->pkey_id
) {
217 RSA_set_flags(rsa
, RSA_FLAG_TYPE_RSA
);
219 case EVP_PKEY_RSA_PSS
:
220 RSA_set_flags(rsa
, RSA_FLAG_TYPE_RSASSAPSS
);
223 /* Leave the type bits zero */
227 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, rsa
);
231 static int int_rsa_size(const EVP_PKEY
*pkey
)
233 return RSA_size(pkey
->pkey
.rsa
);
236 static int rsa_bits(const EVP_PKEY
*pkey
)
238 return BN_num_bits(pkey
->pkey
.rsa
->n
);
241 static int rsa_security_bits(const EVP_PKEY
*pkey
)
243 return RSA_security_bits(pkey
->pkey
.rsa
);
246 static void int_rsa_free(EVP_PKEY
*pkey
)
248 RSA_free(pkey
->pkey
.rsa
);
251 static int rsa_pss_param_print(BIO
*bp
, int pss_key
, RSA_PSS_PARAMS
*pss
,
255 X509_ALGOR
*maskHash
= NULL
;
257 if (!BIO_indent(bp
, indent
, 128))
261 if (BIO_puts(bp
, "No PSS parameter restrictions\n") <= 0)
265 if (BIO_puts(bp
, "PSS parameter restrictions:") <= 0)
268 } else if (pss
== NULL
) {
269 if (BIO_puts(bp
,"(INVALID PSS PARAMETERS)\n") <= 0)
273 if (BIO_puts(bp
, "\n") <= 0)
277 if (!BIO_indent(bp
, indent
, 128))
279 if (BIO_puts(bp
, "Hash Algorithm: ") <= 0)
282 if (pss
->hashAlgorithm
) {
283 if (i2a_ASN1_OBJECT(bp
, pss
->hashAlgorithm
->algorithm
) <= 0)
285 } else if (BIO_puts(bp
, "sha1 (default)") <= 0) {
289 if (BIO_puts(bp
, "\n") <= 0)
292 if (!BIO_indent(bp
, indent
, 128))
295 if (BIO_puts(bp
, "Mask Algorithm: ") <= 0)
297 if (pss
->maskGenAlgorithm
) {
298 if (i2a_ASN1_OBJECT(bp
, pss
->maskGenAlgorithm
->algorithm
) <= 0)
300 if (BIO_puts(bp
, " with ") <= 0)
302 maskHash
= x509_algor_mgf1_decode(pss
->maskGenAlgorithm
);
303 if (maskHash
!= NULL
) {
304 if (i2a_ASN1_OBJECT(bp
, maskHash
->algorithm
) <= 0)
306 } else if (BIO_puts(bp
, "INVALID") <= 0) {
309 } else if (BIO_puts(bp
, "mgf1 with sha1 (default)") <= 0) {
314 if (!BIO_indent(bp
, indent
, 128))
316 if (BIO_printf(bp
, "%s Salt Length: 0x", pss_key
? "Minimum" : "") <= 0)
318 if (pss
->saltLength
) {
319 if (i2a_ASN1_INTEGER(bp
, pss
->saltLength
) <= 0)
321 } else if (BIO_puts(bp
, "14 (default)") <= 0) {
326 if (!BIO_indent(bp
, indent
, 128))
328 if (BIO_puts(bp
, "Trailer Field: 0x") <= 0)
330 if (pss
->trailerField
) {
331 if (i2a_ASN1_INTEGER(bp
, pss
->trailerField
) <= 0)
333 } else if (BIO_puts(bp
, "BC (default)") <= 0) {
341 X509_ALGOR_free(maskHash
);
346 static int pkey_rsa_print(BIO
*bp
, const EVP_PKEY
*pkey
, int off
, int priv
)
348 const RSA
*x
= pkey
->pkey
.rsa
;
351 int ret
= 0, mod_len
= 0, ex_primes
;
354 mod_len
= BN_num_bits(x
->n
);
355 ex_primes
= sk_RSA_PRIME_INFO_num(x
->prime_infos
);
357 if (!BIO_indent(bp
, off
, 128))
360 if (BIO_printf(bp
, "%s ", pkey_is_pss(pkey
) ? "RSA-PSS" : "RSA") <= 0)
364 if (BIO_printf(bp
, "Private-Key: (%d bit, %d primes)\n",
365 mod_len
, ex_primes
<= 0 ? 2 : ex_primes
+ 2) <= 0)
368 s
= "publicExponent:";
370 if (BIO_printf(bp
, "Public-Key: (%d bit)\n", mod_len
) <= 0)
375 if (!ASN1_bn_print(bp
, str
, x
->n
, NULL
, off
))
377 if (!ASN1_bn_print(bp
, s
, x
->e
, NULL
, off
))
382 if (!ASN1_bn_print(bp
, "privateExponent:", x
->d
, NULL
, off
))
384 if (!ASN1_bn_print(bp
, "prime1:", x
->p
, NULL
, off
))
386 if (!ASN1_bn_print(bp
, "prime2:", x
->q
, NULL
, off
))
388 if (!ASN1_bn_print(bp
, "exponent1:", x
->dmp1
, NULL
, off
))
390 if (!ASN1_bn_print(bp
, "exponent2:", x
->dmq1
, NULL
, off
))
392 if (!ASN1_bn_print(bp
, "coefficient:", x
->iqmp
, NULL
, off
))
394 for (i
= 0; i
< sk_RSA_PRIME_INFO_num(x
->prime_infos
); i
++) {
395 /* print multi-prime info */
397 RSA_PRIME_INFO
*pinfo
;
400 pinfo
= sk_RSA_PRIME_INFO_value(x
->prime_infos
, i
);
401 for (j
= 0; j
< 3; j
++) {
402 if (!BIO_indent(bp
, off
, 128))
406 if (BIO_printf(bp
, "prime%d:", i
+ 3) <= 0)
411 if (BIO_printf(bp
, "exponent%d:", i
+ 3) <= 0)
416 if (BIO_printf(bp
, "coefficient%d:", i
+ 3) <= 0)
423 if (!ASN1_bn_print(bp
, "", bn
, NULL
, off
))
428 if (pkey_is_pss(pkey
) && !rsa_pss_param_print(bp
, 1, x
->pss
, off
))
435 static int rsa_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
438 return pkey_rsa_print(bp
, pkey
, indent
, 0);
441 static int rsa_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
444 return pkey_rsa_print(bp
, pkey
, indent
, 1);
447 static RSA_PSS_PARAMS
*rsa_pss_decode(const X509_ALGOR
*alg
)
451 pss
= ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS
),
457 if (pss
->maskGenAlgorithm
!= NULL
) {
458 pss
->maskHash
= x509_algor_mgf1_decode(pss
->maskGenAlgorithm
);
459 if (pss
->maskHash
== NULL
) {
460 RSA_PSS_PARAMS_free(pss
);
468 static int rsa_sig_print(BIO
*bp
, const X509_ALGOR
*sigalg
,
469 const ASN1_STRING
*sig
, int indent
, ASN1_PCTX
*pctx
)
471 if (OBJ_obj2nid(sigalg
->algorithm
) == EVP_PKEY_RSA_PSS
) {
473 RSA_PSS_PARAMS
*pss
= rsa_pss_decode(sigalg
);
475 rv
= rsa_pss_param_print(bp
, 0, pss
, indent
);
476 RSA_PSS_PARAMS_free(pss
);
479 } else if (BIO_puts(bp
, "\n") <= 0) {
483 return X509_signature_dump(bp
, sig
, indent
);
487 static int rsa_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
489 X509_ALGOR
*alg
= NULL
;
491 const EVP_MD
*mgf1md
;
495 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
496 if (pkey
->pkey
.rsa
->pss
!= NULL
) {
497 if (!rsa_pss_get_param(pkey
->pkey
.rsa
->pss
, &md
, &mgf1md
,
499 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
502 *(int *)arg2
= EVP_MD_type(md
);
503 /* Return of 2 indicates this MD is mandatory */
506 *(int *)arg2
= NID_sha256
;
515 X509_ALGOR_set0(alg
, OBJ_nid2obj(NID_rsaEncryption
), V_ASN1_NULL
, 0);
522 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
523 * suitable for setting an AlgorithmIdentifier.
526 static RSA_PSS_PARAMS
*rsa_ctx_to_pss(EVP_PKEY_CTX
*pkctx
)
528 const EVP_MD
*sigmd
, *mgf1md
;
529 EVP_PKEY
*pk
= EVP_PKEY_CTX_get0_pkey(pkctx
);
532 if (EVP_PKEY_CTX_get_signature_md(pkctx
, &sigmd
) <= 0)
534 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx
, &mgf1md
) <= 0)
536 if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx
, &saltlen
))
539 saltlen
= EVP_MD_size(sigmd
);
540 } else if (saltlen
== -2 || saltlen
== -3) {
541 saltlen
= EVP_PKEY_size(pk
) - EVP_MD_size(sigmd
) - 2;
542 if ((EVP_PKEY_bits(pk
) & 0x7) == 1)
548 return rsa_pss_params_create(sigmd
, mgf1md
, saltlen
);
551 RSA_PSS_PARAMS
*rsa_pss_params_create(const EVP_MD
*sigmd
,
552 const EVP_MD
*mgf1md
, int saltlen
)
554 RSA_PSS_PARAMS
*pss
= RSA_PSS_PARAMS_new();
559 pss
->saltLength
= ASN1_INTEGER_new();
560 if (pss
->saltLength
== NULL
)
562 if (!ASN1_INTEGER_set(pss
->saltLength
, saltlen
))
565 if (!x509_algor_new_from_md(&pss
->hashAlgorithm
, sigmd
))
569 if (!x509_algor_md_to_mgf1(&pss
->maskGenAlgorithm
, mgf1md
))
571 if (!x509_algor_new_from_md(&pss
->maskHash
, mgf1md
))
575 RSA_PSS_PARAMS_free(pss
);
579 ASN1_STRING
*ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX
*pkctx
)
581 RSA_PSS_PARAMS
*pss
= rsa_ctx_to_pss(pkctx
);
587 os
= ASN1_item_pack(pss
, ASN1_ITEM_rptr(RSA_PSS_PARAMS
), NULL
);
588 RSA_PSS_PARAMS_free(pss
);
593 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
594 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
595 * passed to pkctx instead.
598 int ossl_rsa_pss_to_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pkctx
,
599 const X509_ALGOR
*sigalg
, EVP_PKEY
*pkey
)
603 const EVP_MD
*mgf1md
= NULL
, *md
= NULL
;
606 /* Sanity check: make sure it is PSS */
607 if (OBJ_obj2nid(sigalg
->algorithm
) != EVP_PKEY_RSA_PSS
) {
608 ERR_raise(ERR_LIB_RSA
, RSA_R_UNSUPPORTED_SIGNATURE_TYPE
);
611 /* Decode PSS parameters */
612 pss
= rsa_pss_decode(sigalg
);
614 if (!rsa_pss_get_param(pss
, &md
, &mgf1md
, &saltlen
)) {
615 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_PARAMETERS
);
619 /* We have all parameters now set up context */
621 if (!EVP_DigestVerifyInit(ctx
, &pkctx
, md
, NULL
, pkey
))
624 const EVP_MD
*checkmd
;
625 if (EVP_PKEY_CTX_get_signature_md(pkctx
, &checkmd
) <= 0)
627 if (EVP_MD_type(md
) != EVP_MD_type(checkmd
)) {
628 ERR_raise(ERR_LIB_RSA
, RSA_R_DIGEST_DOES_NOT_MATCH
);
633 if (EVP_PKEY_CTX_set_rsa_padding(pkctx
, RSA_PKCS1_PSS_PADDING
) <= 0)
636 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx
, saltlen
) <= 0)
639 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx
, mgf1md
) <= 0)
645 RSA_PSS_PARAMS_free(pss
);
649 static int rsa_pss_verify_param(const EVP_MD
**pmd
, const EVP_MD
**pmgf1md
,
650 int *psaltlen
, int *ptrailerField
)
652 if (psaltlen
!= NULL
&& *psaltlen
< 0) {
653 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_SALT_LENGTH
);
657 * low-level routines support only trailer field 0xbc (value 1) and
658 * PKCS#1 says we should reject any other value anyway.
660 if (ptrailerField
!= NULL
&& *ptrailerField
!= 1) {
661 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_TRAILER
);
667 static int rsa_pss_get_param_unverified(const RSA_PSS_PARAMS
*pss
,
669 const EVP_MD
**pmgf1md
,
670 int *psaltlen
, int *ptrailerField
)
672 RSA_PSS_PARAMS_30 pss_params
;
674 /* Get the defaults from the ONE place */
675 (void)ossl_rsa_pss_params_30_set_defaults(&pss_params
);
679 *pmd
= x509_algor_get_md(pss
->hashAlgorithm
);
682 *pmgf1md
= x509_algor_get_md(pss
->maskHash
);
683 if (*pmgf1md
== NULL
)
686 *psaltlen
= ASN1_INTEGER_get(pss
->saltLength
);
688 *psaltlen
= ossl_rsa_pss_params_30_saltlen(&pss_params
);
689 if (pss
->trailerField
)
690 *ptrailerField
= ASN1_INTEGER_get(pss
->trailerField
);
692 *ptrailerField
= ossl_rsa_pss_params_30_trailerfield(&pss_params
);;
697 int rsa_pss_get_param(const RSA_PSS_PARAMS
*pss
, const EVP_MD
**pmd
,
698 const EVP_MD
**pmgf1md
, int *psaltlen
)
701 * Callers do not care about the trailer field, and yet, we must
702 * pass it from get_param to verify_param, since the latter checks
705 * When callers start caring, it's a simple thing to add another
706 * argument to this function.
708 int trailerField
= 0;
710 return rsa_pss_get_param_unverified(pss
, pmd
, pmgf1md
, psaltlen
,
712 && rsa_pss_verify_param(pmd
, pmgf1md
, psaltlen
, &trailerField
);
715 static int rsa_sync_to_pss_params_30(RSA
*rsa
)
717 if (rsa
!= NULL
&& rsa
->pss
!= NULL
) {
718 const EVP_MD
*md
= NULL
, *mgf1md
= NULL
;
719 int md_nid
, mgf1md_nid
, saltlen
, trailerField
;
720 RSA_PSS_PARAMS_30 pss_params
;
723 * We don't care about the validity of the fields here, we just
724 * want to synchronise values. Verifying here makes it impossible
725 * to even read a key with invalid values, making it hard to test
728 * Other routines use rsa_pss_get_param(), so the values will be
729 * checked, eventually.
731 if (!rsa_pss_get_param_unverified(rsa
->pss
, &md
, &mgf1md
,
732 &saltlen
, &trailerField
))
734 md_nid
= EVP_MD_type(md
);
735 mgf1md_nid
= EVP_MD_type(mgf1md
);
736 if (!ossl_rsa_pss_params_30_set_defaults(&pss_params
)
737 || !ossl_rsa_pss_params_30_set_hashalg(&pss_params
, md_nid
)
738 || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params
,
740 || !ossl_rsa_pss_params_30_set_saltlen(&pss_params
, saltlen
)
741 || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params
,
744 rsa
->pss_params
= pss_params
;
750 * Customised RSA item verification routine. This is called when a signature
751 * is encountered requiring special handling. We currently only handle PSS.
754 static int rsa_item_verify(EVP_MD_CTX
*ctx
, const ASN1_ITEM
*it
,
755 const void *asn
, const X509_ALGOR
*sigalg
,
756 const ASN1_BIT_STRING
*sig
, EVP_PKEY
*pkey
)
758 /* Sanity check: make sure it is PSS */
759 if (OBJ_obj2nid(sigalg
->algorithm
) != EVP_PKEY_RSA_PSS
) {
760 ERR_raise(ERR_LIB_RSA
, RSA_R_UNSUPPORTED_SIGNATURE_TYPE
);
763 if (ossl_rsa_pss_to_ctx(ctx
, NULL
, sigalg
, pkey
) > 0) {
770 static int rsa_item_sign(EVP_MD_CTX
*ctx
, const ASN1_ITEM
*it
, const void *asn
,
771 X509_ALGOR
*alg1
, X509_ALGOR
*alg2
,
772 ASN1_BIT_STRING
*sig
)
775 EVP_PKEY_CTX
*pkctx
= EVP_MD_CTX_pkey_ctx(ctx
);
777 if (EVP_PKEY_CTX_get_rsa_padding(pkctx
, &pad_mode
) <= 0)
779 if (pad_mode
== RSA_PKCS1_PADDING
)
781 if (pad_mode
== RSA_PKCS1_PSS_PADDING
) {
782 ASN1_STRING
*os1
= NULL
;
783 os1
= ossl_rsa_ctx_to_pss_string(pkctx
);
786 /* Duplicate parameters if we have to */
788 ASN1_STRING
*os2
= ASN1_STRING_dup(os1
);
790 ASN1_STRING_free(os1
);
793 X509_ALGOR_set0(alg2
, OBJ_nid2obj(EVP_PKEY_RSA_PSS
),
794 V_ASN1_SEQUENCE
, os2
);
796 X509_ALGOR_set0(alg1
, OBJ_nid2obj(EVP_PKEY_RSA_PSS
),
797 V_ASN1_SEQUENCE
, os1
);
803 static int rsa_sig_info_set(X509_SIG_INFO
*siginf
, const X509_ALGOR
*sigalg
,
804 const ASN1_STRING
*sig
)
809 const EVP_MD
*mgf1md
= NULL
, *md
= NULL
;
813 /* Sanity check: make sure it is PSS */
814 if (OBJ_obj2nid(sigalg
->algorithm
) != EVP_PKEY_RSA_PSS
)
816 /* Decode PSS parameters */
817 pss
= rsa_pss_decode(sigalg
);
818 if (!rsa_pss_get_param(pss
, &md
, &mgf1md
, &saltlen
))
820 mdnid
= EVP_MD_type(md
);
822 * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
823 * match and salt length must equal digest size
825 if ((mdnid
== NID_sha256
|| mdnid
== NID_sha384
|| mdnid
== NID_sha512
)
826 && mdnid
== EVP_MD_type(mgf1md
) && saltlen
== EVP_MD_size(md
))
827 flags
= X509_SIG_INFO_TLS
;
830 /* Note: security bits half number of digest bits */
831 secbits
= EVP_MD_size(md
) * 4;
833 * SHA1 and MD5 are known to be broken. Reduce security bits so that
834 * they're no longer accepted at security level 1. The real values don't
835 * really matter as long as they're lower than 80, which is our security
837 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
839 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
840 * puts a chosen-prefix attack for MD5 at 2^39.
842 if (mdnid
== NID_sha1
)
844 else if (mdnid
== NID_md5_sha1
)
846 else if (mdnid
== NID_md5
)
848 X509_SIG_INFO_set(siginf
, mdnid
, EVP_PKEY_RSA_PSS
, secbits
,
852 RSA_PSS_PARAMS_free(pss
);
856 static int rsa_pkey_check(const EVP_PKEY
*pkey
)
858 return RSA_check_key_ex(pkey
->pkey
.rsa
, NULL
);
861 static size_t rsa_pkey_dirty_cnt(const EVP_PKEY
*pkey
)
863 return pkey
->pkey
.rsa
->dirty_cnt
;
867 * For the moment, we trust the call path, where keys going through
868 * rsa_pkey_export_to() match a KEYMGMT for the "RSA" keytype, while
869 * keys going through rsa_pss_pkey_export_to() match a KEYMGMT for the
871 * TODO(3.0) Investigate whether we should simply continue to trust the
872 * call path, or if we should strengthen this function by checking that
873 * |rsa_type| matches the RSA key subtype. The latter requires ensuring
874 * that the type flag for the RSA key is properly set by other functions
877 static int rsa_int_export_to(const EVP_PKEY
*from
, int rsa_type
,
878 void *to_keydata
, EVP_KEYMGMT
*to_keymgmt
,
879 OSSL_LIB_CTX
*libctx
, const char *propq
)
881 RSA
*rsa
= from
->pkey
.rsa
;
882 OSSL_PARAM_BLD
*tmpl
= OSSL_PARAM_BLD_new();
883 OSSL_PARAM
*params
= NULL
;
890 * If the RSA method is foreign, then we can't be sure of anything, and
891 * can therefore not export or pretend to export.
893 if (RSA_get_method(rsa
) != RSA_PKCS1_OpenSSL())
896 /* Public parameters must always be present */
897 if (RSA_get0_n(rsa
) == NULL
|| RSA_get0_e(rsa
) == NULL
)
900 if (!ossl_rsa_todata(rsa
, tmpl
, NULL
))
903 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
904 if (RSA_get0_d(rsa
) != NULL
)
905 selection
|= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
907 if (rsa
->pss
!= NULL
) {
908 const EVP_MD
*md
= NULL
, *mgf1md
= NULL
;
909 int md_nid
, mgf1md_nid
, saltlen
, trailerfield
;
910 RSA_PSS_PARAMS_30 pss_params
;
912 if (!rsa_pss_get_param_unverified(rsa
->pss
, &md
, &mgf1md
,
913 &saltlen
, &trailerfield
))
915 md_nid
= EVP_MD_type(md
);
916 mgf1md_nid
= EVP_MD_type(mgf1md
);
917 if (!ossl_rsa_pss_params_30_set_defaults(&pss_params
)
918 || !ossl_rsa_pss_params_30_set_hashalg(&pss_params
, md_nid
)
919 || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params
,
921 || !ossl_rsa_pss_params_30_set_saltlen(&pss_params
, saltlen
)
922 || !ossl_rsa_pss_params_30_todata(&pss_params
, tmpl
, NULL
))
924 selection
|= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
;
927 if ((params
= OSSL_PARAM_BLD_to_param(tmpl
)) == NULL
)
930 /* We export, the provider imports */
931 rv
= evp_keymgmt_import(to_keymgmt
, to_keydata
, selection
, params
);
934 OSSL_PARAM_BLD_free_params(params
);
935 OSSL_PARAM_BLD_free(tmpl
);
939 static int rsa_int_import_from(const OSSL_PARAM params
[], void *vpctx
,
942 EVP_PKEY_CTX
*pctx
= vpctx
;
943 EVP_PKEY
*pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
944 RSA
*rsa
= ossl_rsa_new_with_ctx(pctx
->libctx
);
945 RSA_PSS_PARAMS_30 rsa_pss_params
= { 0, };
949 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
953 RSA_clear_flags(rsa
, RSA_FLAG_TYPE_MASK
);
954 RSA_set_flags(rsa
, rsa_type
);
956 if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params
, params
, pctx
->libctx
))
960 case RSA_FLAG_TYPE_RSA
:
962 * Were PSS parameters filled in?
963 * In that case, something's wrong
965 if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params
))
968 case RSA_FLAG_TYPE_RSASSAPSS
:
970 * Were PSS parameters filled in? In that case, create the old
971 * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
973 if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params
)) {
974 /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
975 int mdnid
= ossl_rsa_pss_params_30_hashalg(&rsa_pss_params
);
976 int mgf1mdnid
= ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params
);
977 int saltlen
= ossl_rsa_pss_params_30_saltlen(&rsa_pss_params
);
978 const EVP_MD
*md
= EVP_get_digestbynid(mdnid
);
979 const EVP_MD
*mgf1md
= EVP_get_digestbynid(mgf1mdnid
);
981 if ((rsa
->pss
= rsa_pss_params_create(md
, mgf1md
, saltlen
)) == NULL
)
986 /* RSA key sub-types we don't know how to handle yet */
990 if (!ossl_rsa_fromdata(rsa
, params
))
994 case RSA_FLAG_TYPE_RSA
:
995 ok
= EVP_PKEY_assign_RSA(pkey
, rsa
);
997 case RSA_FLAG_TYPE_RSASSAPSS
:
998 ok
= EVP_PKEY_assign(pkey
, EVP_PKEY_RSA_PSS
, rsa
);
1008 static int rsa_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
1009 EVP_KEYMGMT
*to_keymgmt
, OSSL_LIB_CTX
*libctx
,
1012 return rsa_int_export_to(from
, RSA_FLAG_TYPE_RSA
, to_keydata
,
1013 to_keymgmt
, libctx
, propq
);
1016 static int rsa_pss_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
1017 EVP_KEYMGMT
*to_keymgmt
, OSSL_LIB_CTX
*libctx
,
1020 return rsa_int_export_to(from
, RSA_FLAG_TYPE_RSASSAPSS
, to_keydata
,
1021 to_keymgmt
, libctx
, propq
);
1024 static int rsa_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
1026 return rsa_int_import_from(params
, vpctx
, RSA_FLAG_TYPE_RSA
);
1029 static int rsa_pss_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
1031 return rsa_int_import_from(params
, vpctx
, RSA_FLAG_TYPE_RSASSAPSS
);
1034 const EVP_PKEY_ASN1_METHOD rsa_asn1_meths
[2] = {
1038 ASN1_PKEY_SIGPARAM_NULL
,
1041 "OpenSSL RSA method",
1061 old_rsa_priv_decode
,
1062 old_rsa_priv_encode
,
1073 rsa_pkey_import_from
1082 const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth
= {
1085 ASN1_PKEY_SIGPARAM_NULL
,
1088 "OpenSSL RSA-PSS method",
1118 rsa_pss_pkey_export_to
,
1119 rsa_pss_pkey_import_from