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/cms.h>
22 #include <openssl/core_names.h>
23 #include <openssl/param_build.h>
24 #include "crypto/asn1.h"
25 #include "crypto/evp.h"
26 #include "crypto/rsa.h"
27 #include "rsa_local.h"
29 #ifndef OPENSSL_NO_CMS
30 static int rsa_cms_sign(CMS_SignerInfo *si);
31 static int rsa_cms_verify(CMS_SignerInfo *si);
32 static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
33 static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
36 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
37 static int rsa_sync_to_pss_params_30(RSA *rsa);
39 /* Set any parameters associated with pkey */
40 static int rsa_param_encode(const EVP_PKEY *pkey,
41 ASN1_STRING **pstr, int *pstrtype)
43 const RSA *rsa = pkey->pkey.rsa;
46 /* If RSA it's just NULL type */
47 if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != RSA_FLAG_TYPE_RSASSAPSS) {
48 *pstrtype = V_ASN1_NULL;
51 /* If no PSS parameters we omit parameters entirely */
52 if (rsa->pss == NULL) {
53 *pstrtype = V_ASN1_UNDEF;
56 /* Encode PSS parameters */
57 if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
60 *pstrtype = V_ASN1_SEQUENCE;
63 /* Decode any parameters and set them in RSA structure */
64 static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
66 const ASN1_OBJECT *algoid;
70 X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
71 if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
73 if (algptype == V_ASN1_UNDEF)
75 if (algptype != V_ASN1_SEQUENCE) {
76 RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
79 rsa->pss = rsa_pss_decode(alg);
82 if (!rsa_sync_to_pss_params_30(rsa))
87 static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
89 unsigned char *penc = NULL;
94 if (!rsa_param_encode(pkey, &str, &strtype))
96 penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
99 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
100 strtype, str, penc, penclen))
107 static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
109 const unsigned char *p;
114 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
116 if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
117 RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
120 if (!rsa_param_decode(rsa, alg)) {
125 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
126 switch (pkey->ameth->pkey_id) {
128 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
130 case EVP_PKEY_RSA_PSS:
131 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
134 /* Leave the type bits zero */
138 if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
145 static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
148 * Don't check the public/private key, this is mostly for smart
151 if (((RSA_flags(a->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
152 || (RSA_flags(b->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) {
156 if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
157 || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
162 static int old_rsa_priv_decode(EVP_PKEY *pkey,
163 const unsigned char **pder, int derlen)
167 if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
168 RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
171 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
175 static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
177 return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
180 static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
182 unsigned char *rk = NULL;
187 if (!rsa_param_encode(pkey, &str, &strtype))
189 rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
192 RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
193 ASN1_STRING_free(str);
197 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
198 strtype, str, rk, rklen)) {
199 RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
200 ASN1_STRING_free(str);
207 static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
209 const unsigned char *p;
212 const X509_ALGOR *alg;
214 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
216 rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
218 RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
221 if (!rsa_param_decode(rsa, alg)) {
226 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
227 switch (pkey->ameth->pkey_id) {
229 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
231 case EVP_PKEY_RSA_PSS:
232 RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
235 /* Leave the type bits zero */
239 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
243 static int int_rsa_size(const EVP_PKEY *pkey)
245 return RSA_size(pkey->pkey.rsa);
248 static int rsa_bits(const EVP_PKEY *pkey)
250 return BN_num_bits(pkey->pkey.rsa->n);
253 static int rsa_security_bits(const EVP_PKEY *pkey)
255 return RSA_security_bits(pkey->pkey.rsa);
258 static void int_rsa_free(EVP_PKEY *pkey)
260 RSA_free(pkey->pkey.rsa);
263 static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
265 if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
267 return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
271 static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
275 X509_ALGOR *maskHash = NULL;
277 if (!BIO_indent(bp, indent, 128))
281 if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
285 if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
288 } else if (pss == NULL) {
289 if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
293 if (BIO_puts(bp, "\n") <= 0)
297 if (!BIO_indent(bp, indent, 128))
299 if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
302 if (pss->hashAlgorithm) {
303 if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
305 } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
309 if (BIO_puts(bp, "\n") <= 0)
312 if (!BIO_indent(bp, indent, 128))
315 if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
317 if (pss->maskGenAlgorithm) {
318 if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
320 if (BIO_puts(bp, " with ") <= 0)
322 maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
323 if (maskHash != NULL) {
324 if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
326 } else if (BIO_puts(bp, "INVALID") <= 0) {
329 } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
334 if (!BIO_indent(bp, indent, 128))
336 if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
338 if (pss->saltLength) {
339 if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
341 } else if (BIO_puts(bp, "14 (default)") <= 0) {
346 if (!BIO_indent(bp, indent, 128))
348 if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
350 if (pss->trailerField) {
351 if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
353 } else if (BIO_puts(bp, "BC (default)") <= 0) {
361 X509_ALGOR_free(maskHash);
366 static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
368 const RSA *x = pkey->pkey.rsa;
371 int ret = 0, mod_len = 0, ex_primes;
374 mod_len = BN_num_bits(x->n);
375 ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
377 if (!BIO_indent(bp, off, 128))
380 if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0)
384 if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
385 mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
388 s = "publicExponent:";
390 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
395 if (!ASN1_bn_print(bp, str, x->n, NULL, off))
397 if (!ASN1_bn_print(bp, s, x->e, NULL, off))
402 if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
404 if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
406 if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
408 if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
410 if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
412 if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
414 for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
415 /* print multi-prime info */
417 RSA_PRIME_INFO *pinfo;
420 pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
421 for (j = 0; j < 3; j++) {
422 if (!BIO_indent(bp, off, 128))
426 if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
431 if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
436 if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
443 if (!ASN1_bn_print(bp, "", bn, NULL, off))
448 if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
455 static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
458 return pkey_rsa_print(bp, pkey, indent, 0);
461 static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
464 return pkey_rsa_print(bp, pkey, indent, 1);
467 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
471 pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
477 if (pss->maskGenAlgorithm != NULL) {
478 pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
479 if (pss->maskHash == NULL) {
480 RSA_PSS_PARAMS_free(pss);
488 static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
489 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
491 if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
493 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
495 rv = rsa_pss_param_print(bp, 0, pss, indent);
496 RSA_PSS_PARAMS_free(pss);
499 } else if (BIO_puts(bp, "\n") <= 0) {
503 return X509_signature_dump(bp, sig, indent);
507 static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
509 X509_ALGOR *alg = NULL;
511 const EVP_MD *mgf1md;
516 case ASN1_PKEY_CTRL_PKCS7_SIGN:
518 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
521 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
522 if (pkey_is_pss(pkey))
525 PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
527 #ifndef OPENSSL_NO_CMS
528 case ASN1_PKEY_CTRL_CMS_SIGN:
530 return rsa_cms_sign(arg2);
532 return rsa_cms_verify(arg2);
535 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
536 if (pkey_is_pss(pkey))
539 return rsa_cms_encrypt(arg2);
541 return rsa_cms_decrypt(arg2);
544 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
545 if (pkey_is_pss(pkey))
547 *(int *)arg2 = CMS_RECIPINFO_TRANS;
551 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
552 if (pkey->pkey.rsa->pss != NULL) {
553 if (!rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
555 RSAerr(0, ERR_R_INTERNAL_ERROR);
558 *(int *)arg2 = EVP_MD_type(md);
559 /* Return of 2 indicates this MD is mandatory */
562 *(int *)arg2 = NID_sha256;
571 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
577 /* allocate and set algorithm ID from EVP_MD, default SHA1 */
578 static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
580 if (md == NULL || EVP_MD_type(md) == NID_sha1)
582 *palg = X509_ALGOR_new();
585 X509_ALGOR_set_md(*palg, md);
589 /* Allocate and set MGF1 algorithm ID from EVP_MD */
590 static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
592 X509_ALGOR *algtmp = NULL;
593 ASN1_STRING *stmp = NULL;
596 if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
598 /* need to embed algorithm ID inside another */
599 if (!rsa_md_to_algor(&algtmp, mgf1md))
601 if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
603 *palg = X509_ALGOR_new();
606 X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
609 ASN1_STRING_free(stmp);
610 X509_ALGOR_free(algtmp);
616 /* convert algorithm ID to EVP_MD, default SHA1 */
617 static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
623 md = EVP_get_digestbyobj(alg->algorithm);
625 RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
630 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
631 * suitable for setting an AlgorithmIdentifier.
634 static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
636 const EVP_MD *sigmd, *mgf1md;
637 EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
638 RSA *rsa = EVP_PKEY_get0_RSA(pk);
641 if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
643 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
645 if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
648 saltlen = EVP_MD_size(sigmd);
649 } else if (saltlen == -2 || saltlen == -3) {
650 saltlen = RSA_size(rsa) - EVP_MD_size(sigmd) - 2;
651 if ((EVP_PKEY_bits(pk) & 0x7) == 1)
657 return rsa_pss_params_create(sigmd, mgf1md, saltlen);
660 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
661 const EVP_MD *mgf1md, int saltlen)
663 RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
668 pss->saltLength = ASN1_INTEGER_new();
669 if (pss->saltLength == NULL)
671 if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
674 if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
678 if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
680 if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
684 RSA_PSS_PARAMS_free(pss);
688 static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
690 RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
696 os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
697 RSA_PSS_PARAMS_free(pss);
702 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
703 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
704 * passed to pkctx instead.
707 static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
708 const X509_ALGOR *sigalg, EVP_PKEY *pkey)
712 const EVP_MD *mgf1md = NULL, *md = NULL;
715 /* Sanity check: make sure it is PSS */
716 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
717 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
720 /* Decode PSS parameters */
721 pss = rsa_pss_decode(sigalg);
723 if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
724 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
728 /* We have all parameters now set up context */
730 if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
733 const EVP_MD *checkmd;
734 if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
736 if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
737 RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
742 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
745 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
748 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
754 RSA_PSS_PARAMS_free(pss);
758 static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md,
759 int *psaltlen, int *ptrailerField)
761 if (psaltlen != NULL && *psaltlen < 0) {
762 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
766 * low-level routines support only trailer field 0xbc (value 1) and
767 * PKCS#1 says we should reject any other value anyway.
769 if (ptrailerField != NULL && *ptrailerField != 1) {
770 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
776 static int rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
778 const EVP_MD **pmgf1md,
779 int *psaltlen, int *ptrailerField)
781 RSA_PSS_PARAMS_30 pss_params;
783 /* Get the defaults from the ONE place */
784 (void)rsa_pss_params_30_set_defaults(&pss_params);
788 *pmd = rsa_algor_to_md(pss->hashAlgorithm);
791 *pmgf1md = rsa_algor_to_md(pss->maskHash);
792 if (*pmgf1md == NULL)
795 *psaltlen = ASN1_INTEGER_get(pss->saltLength);
797 *psaltlen = rsa_pss_params_30_saltlen(&pss_params);
798 if (pss->trailerField)
799 *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
801 *ptrailerField = rsa_pss_params_30_trailerfield(&pss_params);;
806 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
807 const EVP_MD **pmgf1md, int *psaltlen)
810 * Callers do not care about the trailer field, and yet, we must
811 * pass it from get_param to verify_param, since the latter checks
814 * When callers start caring, it's a simple thing to add another
815 * argument to this function.
817 int trailerField = 0;
819 return rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen,
821 && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField);
824 static int rsa_sync_to_pss_params_30(RSA *rsa)
826 if (rsa != NULL && rsa->pss != NULL) {
827 const EVP_MD *md = NULL, *mgf1md = NULL;
828 int md_nid, mgf1md_nid, saltlen, trailerField;
829 RSA_PSS_PARAMS_30 pss_params;
832 * We don't care about the validity of the fields here, we just
833 * want to synchronise values. Verifying here makes it impossible
834 * to even read a key with invalid values, making it hard to test
837 * Other routines use rsa_pss_get_param(), so the values will be
838 * checked, eventually.
840 if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
841 &saltlen, &trailerField))
843 md_nid = EVP_MD_type(md);
844 mgf1md_nid = EVP_MD_type(mgf1md);
845 if (!rsa_pss_params_30_set_defaults(&pss_params)
846 || !rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
847 || !rsa_pss_params_30_set_maskgenhashalg(&pss_params, mgf1md_nid)
848 || !rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
849 || !rsa_pss_params_30_set_trailerfield(&pss_params, trailerField))
851 rsa->pss_params = pss_params;
856 #ifndef OPENSSL_NO_CMS
857 static int rsa_cms_verify(CMS_SignerInfo *si)
861 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
863 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
864 nid = OBJ_obj2nid(alg->algorithm);
865 if (nid == EVP_PKEY_RSA_PSS)
866 return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
867 /* Only PSS allowed for PSS keys */
868 if (pkey_ctx_is_pss(pkctx)) {
869 RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
872 if (nid == NID_rsaEncryption)
874 /* Workaround for some implementation that use a signature OID */
875 if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
876 if (nid2 == NID_rsaEncryption)
884 * Customised RSA item verification routine. This is called when a signature
885 * is encountered requiring special handling. We currently only handle PSS.
888 static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
889 const void *asn, const X509_ALGOR *sigalg,
890 const ASN1_BIT_STRING *sig, EVP_PKEY *pkey)
892 /* Sanity check: make sure it is PSS */
893 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
894 RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
897 if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
904 #ifndef OPENSSL_NO_CMS
905 static int rsa_cms_sign(CMS_SignerInfo *si)
907 int pad_mode = RSA_PKCS1_PADDING;
909 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
910 ASN1_STRING *os = NULL;
912 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
914 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
917 if (pad_mode == RSA_PKCS1_PADDING) {
918 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
921 /* We don't support it */
922 if (pad_mode != RSA_PKCS1_PSS_PADDING)
924 os = rsa_ctx_to_pss_string(pkctx);
927 X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
932 static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *asn,
933 X509_ALGOR *alg1, X509_ALGOR *alg2,
934 ASN1_BIT_STRING *sig)
937 EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
939 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
941 if (pad_mode == RSA_PKCS1_PADDING)
943 if (pad_mode == RSA_PKCS1_PSS_PADDING) {
944 ASN1_STRING *os1 = NULL;
945 os1 = rsa_ctx_to_pss_string(pkctx);
948 /* Duplicate parameters if we have to */
950 ASN1_STRING *os2 = ASN1_STRING_dup(os1);
952 ASN1_STRING_free(os1);
955 X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
956 V_ASN1_SEQUENCE, os2);
958 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
959 V_ASN1_SEQUENCE, os1);
965 static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
966 const ASN1_STRING *sig)
971 const EVP_MD *mgf1md = NULL, *md = NULL;
975 /* Sanity check: make sure it is PSS */
976 if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
978 /* Decode PSS parameters */
979 pss = rsa_pss_decode(sigalg);
980 if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
982 mdnid = EVP_MD_type(md);
984 * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
985 * match and salt length must equal digest size
987 if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
988 && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
989 flags = X509_SIG_INFO_TLS;
992 /* Note: security bits half number of digest bits */
993 secbits = EVP_MD_size(md) * 4;
995 * SHA1 and MD5 are known to be broken. Reduce security bits so that
996 * they're no longer accepted at security level 1. The real values don't
997 * really matter as long as they're lower than 80, which is our security
999 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
1001 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
1002 * puts a chosen-prefix attack for MD5 at 2^39.
1004 if (mdnid == NID_sha1)
1006 else if (mdnid == NID_md5_sha1)
1008 else if (mdnid == NID_md5)
1010 X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
1014 RSA_PSS_PARAMS_free(pss);
1018 #ifndef OPENSSL_NO_CMS
1019 static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
1021 RSA_OAEP_PARAMS *oaep;
1023 oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
1029 if (oaep->maskGenFunc != NULL) {
1030 oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
1031 if (oaep->maskHash == NULL) {
1032 RSA_OAEP_PARAMS_free(oaep);
1039 static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
1041 EVP_PKEY_CTX *pkctx;
1045 unsigned char *label = NULL;
1047 const EVP_MD *mgf1md = NULL, *md = NULL;
1048 RSA_OAEP_PARAMS *oaep;
1050 pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1053 if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
1055 nid = OBJ_obj2nid(cmsalg->algorithm);
1056 if (nid == NID_rsaEncryption)
1058 if (nid != NID_rsaesOaep) {
1059 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
1062 /* Decode OAEP parameters */
1063 oaep = rsa_oaep_decode(cmsalg);
1066 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
1070 mgf1md = rsa_algor_to_md(oaep->maskHash);
1073 md = rsa_algor_to_md(oaep->hashFunc);
1077 if (oaep->pSourceFunc != NULL) {
1078 X509_ALGOR *plab = oaep->pSourceFunc;
1080 if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
1081 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
1084 if (plab->parameter->type != V_ASN1_OCTET_STRING) {
1085 RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
1089 label = plab->parameter->value.octet_string->data;
1090 /* Stop label being freed when OAEP parameters are freed */
1091 plab->parameter->value.octet_string->data = NULL;
1092 labellen = plab->parameter->value.octet_string->length;
1095 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
1097 if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
1099 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
1102 && EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
1108 RSA_OAEP_PARAMS_free(oaep);
1112 static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
1114 const EVP_MD *md, *mgf1md;
1115 RSA_OAEP_PARAMS *oaep = NULL;
1116 ASN1_STRING *os = NULL;
1118 EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1119 int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
1120 unsigned char *label;
1122 if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
1125 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
1128 if (pad_mode == RSA_PKCS1_PADDING) {
1129 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
1133 if (pad_mode != RSA_PKCS1_OAEP_PADDING)
1135 if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
1137 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
1139 labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
1142 oaep = RSA_OAEP_PARAMS_new();
1145 if (!rsa_md_to_algor(&oaep->hashFunc, md))
1147 if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
1150 ASN1_OCTET_STRING *los;
1151 oaep->pSourceFunc = X509_ALGOR_new();
1152 if (oaep->pSourceFunc == NULL)
1154 los = ASN1_OCTET_STRING_new();
1157 if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
1158 ASN1_OCTET_STRING_free(los);
1161 X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
1162 V_ASN1_OCTET_STRING, los);
1164 /* create string with pss parameter encoding. */
1165 if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
1167 X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
1171 RSA_OAEP_PARAMS_free(oaep);
1172 ASN1_STRING_free(os);
1177 static int rsa_pkey_check(const EVP_PKEY *pkey)
1179 return RSA_check_key_ex(pkey->pkey.rsa, NULL);
1182 static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
1184 return pkey->pkey.rsa->dirty_cnt;
1188 * For the moment, we trust the call path, where keys going through
1189 * rsa_pkey_export_to() match a KEYMGMT for the "RSA" keytype, while
1190 * keys going through rsa_pss_pkey_export_to() match a KEYMGMT for the
1191 * "RSA-PSS" keytype.
1192 * TODO(3.0) Investigate whether we should simply continue to trust the
1193 * call path, or if we should strengthen this function by checking that
1194 * |rsa_type| matches the RSA key subtype. The latter requires ensuring
1195 * that the type flag for the RSA key is properly set by other functions
1198 static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
1199 void *to_keydata, EVP_KEYMGMT *to_keymgmt,
1200 OPENSSL_CTX *libctx, const char *propq)
1202 RSA *rsa = from->pkey.rsa;
1203 OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
1204 OSSL_PARAM *params = NULL;
1211 * If the RSA method is foreign, then we can't be sure of anything, and
1212 * can therefore not export or pretend to export.
1214 if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
1217 /* Public parameters must always be present */
1218 if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
1221 if (!rsa_todata(rsa, tmpl, NULL))
1224 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
1225 if (RSA_get0_d(rsa) != NULL)
1226 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
1228 if (rsa->pss != NULL) {
1229 const EVP_MD *md = NULL, *mgf1md = NULL;
1230 int md_nid, mgf1md_nid, saltlen, trailerfield;
1231 RSA_PSS_PARAMS_30 pss_params;
1233 if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
1234 &saltlen, &trailerfield))
1236 md_nid = EVP_MD_type(md);
1237 mgf1md_nid = EVP_MD_type(mgf1md);
1238 if (!rsa_pss_params_30_set_defaults(&pss_params)
1239 || !rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
1240 || !rsa_pss_params_30_set_maskgenhashalg(&pss_params, mgf1md_nid)
1241 || !rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
1242 || !rsa_pss_params_30_todata(&pss_params, tmpl, NULL))
1244 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
1247 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
1250 /* We export, the provider imports */
1251 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
1254 OSSL_PARAM_BLD_free_params(params);
1255 OSSL_PARAM_BLD_free(tmpl);
1259 static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
1262 EVP_PKEY_CTX *pctx = vpctx;
1263 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
1264 RSA *rsa = rsa_new_with_ctx(pctx->libctx);
1265 RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
1269 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
1273 RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
1274 RSA_set_flags(rsa, rsa_type);
1276 if (!rsa_pss_params_30_fromdata(&rsa_pss_params, params, pctx->libctx))
1280 case RSA_FLAG_TYPE_RSA:
1282 * Were PSS parameters filled in?
1283 * In that case, something's wrong
1285 if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
1288 case RSA_FLAG_TYPE_RSASSAPSS:
1290 * Were PSS parameters filled in? In that case, create the old
1291 * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
1293 if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
1294 /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
1295 int mdnid = rsa_pss_params_30_hashalg(&rsa_pss_params);
1296 int mgf1mdnid = rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
1297 int saltlen = rsa_pss_params_30_saltlen(&rsa_pss_params);
1298 const EVP_MD *md = EVP_get_digestbynid(mdnid);
1299 const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
1301 if ((rsa->pss = rsa_pss_params_create(md, mgf1md, saltlen)) == NULL)
1306 /* RSA key sub-types we don't know how to handle yet */
1310 if (!rsa_fromdata(rsa, params))
1314 case RSA_FLAG_TYPE_RSA:
1315 ok = EVP_PKEY_assign_RSA(pkey, rsa);
1317 case RSA_FLAG_TYPE_RSASSAPSS:
1318 ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
1328 static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1329 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1332 return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
1333 to_keymgmt, libctx, propq);
1336 static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1337 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1340 return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
1341 to_keymgmt, libctx, propq);
1344 static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1346 return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
1349 static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1351 return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
1354 const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
1358 ASN1_PKEY_SIGPARAM_NULL,
1361 "OpenSSL RSA method",
1381 old_rsa_priv_decode,
1382 old_rsa_priv_encode,
1393 rsa_pkey_import_from
1402 const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
1405 ASN1_PKEY_SIGPARAM_NULL,
1408 "OpenSSL RSA-PSS method",
1438 rsa_pss_pkey_export_to,
1439 rsa_pss_pkey_import_from