2 * Copyright 2006-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 * 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 RSAerr(RSA_F_RSA_PARAM_DECODE, 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 RSAerr(RSA_F_RSA_PRIV_ENCODE, 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 RSAerr(RSA_F_RSA_PRIV_ENCODE, 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 RSAerr(RSA_F_RSA_PRIV_DECODE, 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;
496 case ASN1_PKEY_CTRL_PKCS7_SIGN:
498 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
501 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
502 if (pkey_is_pss(pkey))
505 PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
508 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
509 if (pkey->pkey.rsa->pss != NULL) {
510 if (!rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
512 RSAerr(0, ERR_R_INTERNAL_ERROR);
515 *(int *)arg2 = EVP_MD_type(md);
516 /* Return of 2 indicates this MD is mandatory */
519 *(int *)arg2 = NID_sha256;
528 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
535 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
536 * suitable for setting an AlgorithmIdentifier.
539 static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
541 const EVP_MD *sigmd, *mgf1md;
542 EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
545 if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
547 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
549 if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
552 saltlen = EVP_MD_size(sigmd);
553 } else if (saltlen == -2 || saltlen == -3) {
554 saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
555 if ((EVP_PKEY_bits(pk) & 0x7) == 1)
561 return rsa_pss_params_create(sigmd, mgf1md, saltlen);
564 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
565 const EVP_MD *mgf1md, int saltlen)
567 RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
572 pss->saltLength = ASN1_INTEGER_new();
573 if (pss->saltLength == NULL)
575 if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
578 if (!x509_algor_new_from_md(&pss->hashAlgorithm, sigmd))
582 if (!x509_algor_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
584 if (!x509_algor_new_from_md(&pss->maskHash, mgf1md))
588 RSA_PSS_PARAMS_free(pss);
592 ASN1_STRING *ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
594 RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
600 os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
601 RSA_PSS_PARAMS_free(pss);
606 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
607 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
608 * passed to pkctx instead.
611 int ossl_rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
612 const X509_ALGOR *sigalg, EVP_PKEY *pkey)
616 const EVP_MD *mgf1md = NULL, *md = NULL;
619 /* Sanity check: make sure it is PSS */
620 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
621 RSAerr(0, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
624 /* Decode PSS parameters */
625 pss = rsa_pss_decode(sigalg);
627 if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
628 RSAerr(0, RSA_R_INVALID_PSS_PARAMETERS);
632 /* We have all parameters now set up context */
634 if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
637 const EVP_MD *checkmd;
638 if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
640 if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
641 RSAerr(0, RSA_R_DIGEST_DOES_NOT_MATCH);
646 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
649 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
652 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
658 RSA_PSS_PARAMS_free(pss);
662 static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md,
663 int *psaltlen, int *ptrailerField)
665 if (psaltlen != NULL && *psaltlen < 0) {
666 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
670 * low-level routines support only trailer field 0xbc (value 1) and
671 * PKCS#1 says we should reject any other value anyway.
673 if (ptrailerField != NULL && *ptrailerField != 1) {
674 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
680 static int rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
682 const EVP_MD **pmgf1md,
683 int *psaltlen, int *ptrailerField)
685 RSA_PSS_PARAMS_30 pss_params;
687 /* Get the defaults from the ONE place */
688 (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);
692 *pmd = x509_algor_get_md(pss->hashAlgorithm);
695 *pmgf1md = x509_algor_get_md(pss->maskHash);
696 if (*pmgf1md == NULL)
699 *psaltlen = ASN1_INTEGER_get(pss->saltLength);
701 *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);
702 if (pss->trailerField)
703 *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
705 *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);;
710 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
711 const EVP_MD **pmgf1md, int *psaltlen)
714 * Callers do not care about the trailer field, and yet, we must
715 * pass it from get_param to verify_param, since the latter checks
718 * When callers start caring, it's a simple thing to add another
719 * argument to this function.
721 int trailerField = 0;
723 return rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen,
725 && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField);
728 static int rsa_sync_to_pss_params_30(RSA *rsa)
730 if (rsa != NULL && rsa->pss != NULL) {
731 const EVP_MD *md = NULL, *mgf1md = NULL;
732 int md_nid, mgf1md_nid, saltlen, trailerField;
733 RSA_PSS_PARAMS_30 pss_params;
736 * We don't care about the validity of the fields here, we just
737 * want to synchronise values. Verifying here makes it impossible
738 * to even read a key with invalid values, making it hard to test
741 * Other routines use rsa_pss_get_param(), so the values will be
742 * checked, eventually.
744 if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
745 &saltlen, &trailerField))
747 md_nid = EVP_MD_type(md);
748 mgf1md_nid = EVP_MD_type(mgf1md);
749 if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
750 || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
751 || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
753 || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
754 || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,
757 rsa->pss_params = pss_params;
763 * Customised RSA item verification routine. This is called when a signature
764 * is encountered requiring special handling. We currently only handle PSS.
767 static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
768 const void *asn, const X509_ALGOR *sigalg,
769 const ASN1_BIT_STRING *sig, EVP_PKEY *pkey)
771 /* Sanity check: make sure it is PSS */
772 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
773 RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
776 if (ossl_rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
783 static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *asn,
784 X509_ALGOR *alg1, X509_ALGOR *alg2,
785 ASN1_BIT_STRING *sig)
788 EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
790 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
792 if (pad_mode == RSA_PKCS1_PADDING)
794 if (pad_mode == RSA_PKCS1_PSS_PADDING) {
795 ASN1_STRING *os1 = NULL;
796 os1 = ossl_rsa_ctx_to_pss_string(pkctx);
799 /* Duplicate parameters if we have to */
801 ASN1_STRING *os2 = ASN1_STRING_dup(os1);
803 ASN1_STRING_free(os1);
806 X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
807 V_ASN1_SEQUENCE, os2);
809 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
810 V_ASN1_SEQUENCE, os1);
816 static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
817 const ASN1_STRING *sig)
822 const EVP_MD *mgf1md = NULL, *md = NULL;
826 /* Sanity check: make sure it is PSS */
827 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
829 /* Decode PSS parameters */
830 pss = rsa_pss_decode(sigalg);
831 if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
833 mdnid = EVP_MD_type(md);
835 * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
836 * match and salt length must equal digest size
838 if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
839 && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
840 flags = X509_SIG_INFO_TLS;
843 /* Note: security bits half number of digest bits */
844 secbits = EVP_MD_size(md) * 4;
846 * SHA1 and MD5 are known to be broken. Reduce security bits so that
847 * they're no longer accepted at security level 1. The real values don't
848 * really matter as long as they're lower than 80, which is our security
850 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
852 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
853 * puts a chosen-prefix attack for MD5 at 2^39.
855 if (mdnid == NID_sha1)
857 else if (mdnid == NID_md5_sha1)
859 else if (mdnid == NID_md5)
861 X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
865 RSA_PSS_PARAMS_free(pss);
869 static int rsa_pkey_check(const EVP_PKEY *pkey)
871 return RSA_check_key_ex(pkey->pkey.rsa, NULL);
874 static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
876 return pkey->pkey.rsa->dirty_cnt;
880 * For the moment, we trust the call path, where keys going through
881 * rsa_pkey_export_to() match a KEYMGMT for the "RSA" keytype, while
882 * keys going through rsa_pss_pkey_export_to() match a KEYMGMT for the
884 * TODO(3.0) Investigate whether we should simply continue to trust the
885 * call path, or if we should strengthen this function by checking that
886 * |rsa_type| matches the RSA key subtype. The latter requires ensuring
887 * that the type flag for the RSA key is properly set by other functions
890 static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
891 void *to_keydata, EVP_KEYMGMT *to_keymgmt,
892 OPENSSL_CTX *libctx, const char *propq)
894 RSA *rsa = from->pkey.rsa;
895 OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
896 OSSL_PARAM *params = NULL;
903 * If the RSA method is foreign, then we can't be sure of anything, and
904 * can therefore not export or pretend to export.
906 if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
909 /* Public parameters must always be present */
910 if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
913 if (!ossl_rsa_todata(rsa, tmpl, NULL))
916 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
917 if (RSA_get0_d(rsa) != NULL)
918 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
920 if (rsa->pss != NULL) {
921 const EVP_MD *md = NULL, *mgf1md = NULL;
922 int md_nid, mgf1md_nid, saltlen, trailerfield;
923 RSA_PSS_PARAMS_30 pss_params;
925 if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
926 &saltlen, &trailerfield))
928 md_nid = EVP_MD_type(md);
929 mgf1md_nid = EVP_MD_type(mgf1md);
930 if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
931 || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
932 || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
934 || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
935 || !ossl_rsa_pss_params_30_todata(&pss_params, tmpl, NULL))
937 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
940 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
943 /* We export, the provider imports */
944 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
947 OSSL_PARAM_BLD_free_params(params);
948 OSSL_PARAM_BLD_free(tmpl);
952 static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
955 EVP_PKEY_CTX *pctx = vpctx;
956 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
957 RSA *rsa = ossl_rsa_new_with_ctx(pctx->libctx);
958 RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
962 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
966 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
967 RSA_set_flags(rsa, rsa_type);
969 if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params, params, pctx->libctx))
973 case RSA_FLAG_TYPE_RSA:
975 * Were PSS parameters filled in?
976 * In that case, something's wrong
978 if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
981 case RSA_FLAG_TYPE_RSASSAPSS:
983 * Were PSS parameters filled in? In that case, create the old
984 * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
986 if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
987 /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
988 int mdnid = ossl_rsa_pss_params_30_hashalg(&rsa_pss_params);
989 int mgf1mdnid = ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
990 int saltlen = ossl_rsa_pss_params_30_saltlen(&rsa_pss_params);
991 const EVP_MD *md = EVP_get_digestbynid(mdnid);
992 const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
994 if ((rsa->pss = rsa_pss_params_create(md, mgf1md, saltlen)) == NULL)
999 /* RSA key sub-types we don't know how to handle yet */
1003 if (!ossl_rsa_fromdata(rsa, params))
1007 case RSA_FLAG_TYPE_RSA:
1008 ok = EVP_PKEY_assign_RSA(pkey, rsa);
1010 case RSA_FLAG_TYPE_RSASSAPSS:
1011 ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
1021 static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1022 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1025 return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
1026 to_keymgmt, libctx, propq);
1029 static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1030 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1033 return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
1034 to_keymgmt, libctx, propq);
1037 static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1039 return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
1042 static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1044 return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
1047 const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
1051 ASN1_PKEY_SIGPARAM_NULL,
1054 "OpenSSL RSA method",
1074 old_rsa_priv_decode,
1075 old_rsa_priv_encode,
1086 rsa_pkey_import_from
1095 const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
1098 ASN1_PKEY_SIGPARAM_NULL,
1101 "OpenSSL RSA-PSS method",
1131 rsa_pss_pkey_export_to,
1132 rsa_pss_pkey_import_from