2 * Copyright 2006-2022 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 /* Set any parameters associated with pkey */
29 static int rsa_param_encode(const EVP_PKEY
*pkey
,
30 ASN1_STRING
**pstr
, int *pstrtype
)
32 const RSA
*rsa
= pkey
->pkey
.rsa
;
35 /* If RSA it's just NULL type */
36 if (RSA_test_flags(rsa
, RSA_FLAG_TYPE_MASK
) != RSA_FLAG_TYPE_RSASSAPSS
) {
37 *pstrtype
= V_ASN1_NULL
;
40 /* If no PSS parameters we omit parameters entirely */
41 if (rsa
->pss
== NULL
) {
42 *pstrtype
= V_ASN1_UNDEF
;
45 /* Encode PSS parameters */
46 if (ASN1_item_pack(rsa
->pss
, ASN1_ITEM_rptr(RSA_PSS_PARAMS
), pstr
) == NULL
)
49 *pstrtype
= V_ASN1_SEQUENCE
;
52 /* Decode any parameters and set them in RSA structure */
53 static int rsa_pub_encode(X509_PUBKEY
*pk
, const EVP_PKEY
*pkey
)
55 unsigned char *penc
= NULL
;
60 if (!rsa_param_encode(pkey
, &str
, &strtype
))
62 penclen
= i2d_RSAPublicKey(pkey
->pkey
.rsa
, &penc
);
65 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(pkey
->ameth
->pkey_id
),
66 strtype
, str
, penc
, penclen
))
73 static int rsa_pub_decode(EVP_PKEY
*pkey
, const X509_PUBKEY
*pubkey
)
75 const unsigned char *p
;
80 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &alg
, pubkey
))
82 if ((rsa
= d2i_RSAPublicKey(NULL
, &p
, pklen
)) == NULL
)
84 if (!ossl_rsa_param_decode(rsa
, alg
)) {
89 RSA_clear_flags(rsa
, RSA_FLAG_TYPE_MASK
);
90 switch (pkey
->ameth
->pkey_id
) {
92 RSA_set_flags(rsa
, RSA_FLAG_TYPE_RSA
);
94 case EVP_PKEY_RSA_PSS
:
95 RSA_set_flags(rsa
, RSA_FLAG_TYPE_RSASSAPSS
);
98 /* Leave the type bits zero */
102 if (!EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, rsa
)) {
109 static int rsa_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
112 * Don't check the public/private key, this is mostly for smart
115 if (((RSA_flags(a
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
))
116 || (RSA_flags(b
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
)) {
120 if (BN_cmp(b
->pkey
.rsa
->n
, a
->pkey
.rsa
->n
) != 0
121 || BN_cmp(b
->pkey
.rsa
->e
, a
->pkey
.rsa
->e
) != 0)
126 static int old_rsa_priv_decode(EVP_PKEY
*pkey
,
127 const unsigned char **pder
, int derlen
)
131 if ((rsa
= d2i_RSAPrivateKey(NULL
, pder
, derlen
)) == NULL
)
133 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, rsa
);
137 static int old_rsa_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
139 return i2d_RSAPrivateKey(pkey
->pkey
.rsa
, pder
);
142 static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
144 unsigned char *rk
= NULL
;
149 if (!rsa_param_encode(pkey
, &str
, &strtype
))
151 rklen
= i2d_RSAPrivateKey(pkey
->pkey
.rsa
, &rk
);
154 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
155 ASN1_STRING_free(str
);
159 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(pkey
->ameth
->pkey_id
), 0,
160 strtype
, str
, rk
, rklen
)) {
161 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
162 ASN1_STRING_free(str
);
169 static int rsa_priv_decode(EVP_PKEY
*pkey
, const PKCS8_PRIV_KEY_INFO
*p8
)
172 RSA
*rsa
= ossl_rsa_key_from_pkcs8(p8
, NULL
, NULL
);
176 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, rsa
);
181 static int int_rsa_size(const EVP_PKEY
*pkey
)
183 return RSA_size(pkey
->pkey
.rsa
);
186 static int rsa_bits(const EVP_PKEY
*pkey
)
188 return BN_num_bits(pkey
->pkey
.rsa
->n
);
191 static int rsa_security_bits(const EVP_PKEY
*pkey
)
193 return RSA_security_bits(pkey
->pkey
.rsa
);
196 static void int_rsa_free(EVP_PKEY
*pkey
)
198 RSA_free(pkey
->pkey
.rsa
);
201 static int rsa_pss_param_print(BIO
*bp
, int pss_key
, RSA_PSS_PARAMS
*pss
,
205 X509_ALGOR
*maskHash
= NULL
;
207 if (!BIO_indent(bp
, indent
, 128))
211 if (BIO_puts(bp
, "No PSS parameter restrictions\n") <= 0)
215 if (BIO_puts(bp
, "PSS parameter restrictions:") <= 0)
218 } else if (pss
== NULL
) {
219 if (BIO_puts(bp
, "(INVALID PSS PARAMETERS)\n") <= 0)
223 if (BIO_puts(bp
, "\n") <= 0)
227 if (!BIO_indent(bp
, indent
, 128))
229 if (BIO_puts(bp
, "Hash Algorithm: ") <= 0)
232 if (pss
->hashAlgorithm
) {
233 if (i2a_ASN1_OBJECT(bp
, pss
->hashAlgorithm
->algorithm
) <= 0)
235 } else if (BIO_puts(bp
, "sha1 (default)") <= 0) {
239 if (BIO_puts(bp
, "\n") <= 0)
242 if (!BIO_indent(bp
, indent
, 128))
245 if (BIO_puts(bp
, "Mask Algorithm: ") <= 0)
247 if (pss
->maskGenAlgorithm
) {
248 if (i2a_ASN1_OBJECT(bp
, pss
->maskGenAlgorithm
->algorithm
) <= 0)
250 if (BIO_puts(bp
, " with ") <= 0)
252 maskHash
= ossl_x509_algor_mgf1_decode(pss
->maskGenAlgorithm
);
253 if (maskHash
!= NULL
) {
254 if (i2a_ASN1_OBJECT(bp
, maskHash
->algorithm
) <= 0)
256 } else if (BIO_puts(bp
, "INVALID") <= 0) {
259 } else if (BIO_puts(bp
, "mgf1 with sha1 (default)") <= 0) {
264 if (!BIO_indent(bp
, indent
, 128))
266 if (BIO_printf(bp
, "%s Salt Length: 0x", pss_key
? "Minimum" : "") <= 0)
268 if (pss
->saltLength
) {
269 if (i2a_ASN1_INTEGER(bp
, pss
->saltLength
) <= 0)
271 } else if (BIO_puts(bp
, "14 (default)") <= 0) {
276 if (!BIO_indent(bp
, indent
, 128))
278 if (BIO_puts(bp
, "Trailer Field: 0x") <= 0)
280 if (pss
->trailerField
) {
281 if (i2a_ASN1_INTEGER(bp
, pss
->trailerField
) <= 0)
283 } else if (BIO_puts(bp
, "01 (default)") <= 0) {
291 X509_ALGOR_free(maskHash
);
296 static int pkey_rsa_print(BIO
*bp
, const EVP_PKEY
*pkey
, int off
, int priv
)
298 const RSA
*x
= pkey
->pkey
.rsa
;
301 int ret
= 0, mod_len
= 0, ex_primes
;
304 mod_len
= BN_num_bits(x
->n
);
305 ex_primes
= sk_RSA_PRIME_INFO_num(x
->prime_infos
);
307 if (!BIO_indent(bp
, off
, 128))
310 if (BIO_printf(bp
, "%s ", pkey_is_pss(pkey
) ? "RSA-PSS" : "RSA") <= 0)
314 if (BIO_printf(bp
, "Private-Key: (%d bit, %d primes)\n",
315 mod_len
, ex_primes
<= 0 ? 2 : ex_primes
+ 2) <= 0)
318 s
= "publicExponent:";
320 if (BIO_printf(bp
, "Public-Key: (%d bit)\n", mod_len
) <= 0)
325 if (!ASN1_bn_print(bp
, str
, x
->n
, NULL
, off
))
327 if (!ASN1_bn_print(bp
, s
, x
->e
, NULL
, off
))
332 if (!ASN1_bn_print(bp
, "privateExponent:", x
->d
, NULL
, off
))
334 if (!ASN1_bn_print(bp
, "prime1:", x
->p
, NULL
, off
))
336 if (!ASN1_bn_print(bp
, "prime2:", x
->q
, NULL
, off
))
338 if (!ASN1_bn_print(bp
, "exponent1:", x
->dmp1
, NULL
, off
))
340 if (!ASN1_bn_print(bp
, "exponent2:", x
->dmq1
, NULL
, off
))
342 if (!ASN1_bn_print(bp
, "coefficient:", x
->iqmp
, NULL
, off
))
344 for (i
= 0; i
< sk_RSA_PRIME_INFO_num(x
->prime_infos
); i
++) {
345 /* print multi-prime info */
347 RSA_PRIME_INFO
*pinfo
;
350 pinfo
= sk_RSA_PRIME_INFO_value(x
->prime_infos
, i
);
351 for (j
= 0; j
< 3; j
++) {
352 if (!BIO_indent(bp
, off
, 128))
356 if (BIO_printf(bp
, "prime%d:", i
+ 3) <= 0)
361 if (BIO_printf(bp
, "exponent%d:", i
+ 3) <= 0)
366 if (BIO_printf(bp
, "coefficient%d:", i
+ 3) <= 0)
373 if (!ASN1_bn_print(bp
, "", bn
, NULL
, off
))
378 if (pkey_is_pss(pkey
) && !rsa_pss_param_print(bp
, 1, x
->pss
, off
))
385 static int rsa_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
388 return pkey_rsa_print(bp
, pkey
, indent
, 0);
391 static int rsa_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
394 return pkey_rsa_print(bp
, pkey
, indent
, 1);
397 static int rsa_sig_print(BIO
*bp
, const X509_ALGOR
*sigalg
,
398 const ASN1_STRING
*sig
, int indent
, ASN1_PCTX
*pctx
)
400 if (OBJ_obj2nid(sigalg
->algorithm
) == EVP_PKEY_RSA_PSS
) {
402 RSA_PSS_PARAMS
*pss
= ossl_rsa_pss_decode(sigalg
);
404 rv
= rsa_pss_param_print(bp
, 0, pss
, indent
);
405 RSA_PSS_PARAMS_free(pss
);
408 } else if (BIO_puts(bp
, "\n") <= 0) {
412 return X509_signature_dump(bp
, sig
, indent
);
416 static int rsa_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
419 const EVP_MD
*mgf1md
;
423 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
424 if (pkey
->pkey
.rsa
->pss
!= NULL
) {
425 if (!ossl_rsa_pss_get_param(pkey
->pkey
.rsa
->pss
, &md
, &mgf1md
,
427 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
430 *(int *)arg2
= EVP_MD_get_type(md
);
431 /* Return of 2 indicates this MD is mandatory */
434 *(int *)arg2
= NID_sha256
;
443 * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
444 * suitable for setting an AlgorithmIdentifier.
447 static RSA_PSS_PARAMS
*rsa_ctx_to_pss(EVP_PKEY_CTX
*pkctx
)
449 const EVP_MD
*sigmd
, *mgf1md
;
450 EVP_PKEY
*pk
= EVP_PKEY_CTX_get0_pkey(pkctx
);
453 if (EVP_PKEY_CTX_get_signature_md(pkctx
, &sigmd
) <= 0)
455 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx
, &mgf1md
) <= 0)
457 if (EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx
, &saltlen
) <= 0)
460 saltlen
= EVP_MD_get_size(sigmd
);
461 } else if (saltlen
== -2 || saltlen
== -3) {
462 saltlen
= EVP_PKEY_get_size(pk
) - EVP_MD_get_size(sigmd
) - 2;
463 if ((EVP_PKEY_get_bits(pk
) & 0x7) == 1)
469 return ossl_rsa_pss_params_create(sigmd
, mgf1md
, saltlen
);
472 RSA_PSS_PARAMS
*ossl_rsa_pss_params_create(const EVP_MD
*sigmd
,
473 const EVP_MD
*mgf1md
, int saltlen
)
475 RSA_PSS_PARAMS
*pss
= RSA_PSS_PARAMS_new();
480 pss
->saltLength
= ASN1_INTEGER_new();
481 if (pss
->saltLength
== NULL
)
483 if (!ASN1_INTEGER_set(pss
->saltLength
, saltlen
))
486 if (!ossl_x509_algor_new_from_md(&pss
->hashAlgorithm
, sigmd
))
490 if (!ossl_x509_algor_md_to_mgf1(&pss
->maskGenAlgorithm
, mgf1md
))
492 if (!ossl_x509_algor_new_from_md(&pss
->maskHash
, mgf1md
))
496 RSA_PSS_PARAMS_free(pss
);
500 ASN1_STRING
*ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX
*pkctx
)
502 RSA_PSS_PARAMS
*pss
= rsa_ctx_to_pss(pkctx
);
508 os
= ASN1_item_pack(pss
, ASN1_ITEM_rptr(RSA_PSS_PARAMS
), NULL
);
509 RSA_PSS_PARAMS_free(pss
);
514 * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
515 * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
516 * passed to pkctx instead.
519 int ossl_rsa_pss_to_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pkctx
,
520 const X509_ALGOR
*sigalg
, EVP_PKEY
*pkey
)
524 const EVP_MD
*mgf1md
= NULL
, *md
= NULL
;
527 /* Sanity check: make sure it is PSS */
528 if (OBJ_obj2nid(sigalg
->algorithm
) != EVP_PKEY_RSA_PSS
) {
529 ERR_raise(ERR_LIB_RSA
, RSA_R_UNSUPPORTED_SIGNATURE_TYPE
);
532 /* Decode PSS parameters */
533 pss
= ossl_rsa_pss_decode(sigalg
);
535 if (!ossl_rsa_pss_get_param(pss
, &md
, &mgf1md
, &saltlen
)) {
536 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_PARAMETERS
);
540 /* We have all parameters now set up context */
542 if (!EVP_DigestVerifyInit(ctx
, &pkctx
, md
, NULL
, pkey
))
545 const EVP_MD
*checkmd
;
546 if (EVP_PKEY_CTX_get_signature_md(pkctx
, &checkmd
) <= 0)
548 if (EVP_MD_get_type(md
) != EVP_MD_get_type(checkmd
)) {
549 ERR_raise(ERR_LIB_RSA
, RSA_R_DIGEST_DOES_NOT_MATCH
);
554 if (EVP_PKEY_CTX_set_rsa_padding(pkctx
, RSA_PKCS1_PSS_PADDING
) <= 0)
557 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx
, saltlen
) <= 0)
560 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx
, mgf1md
) <= 0)
566 RSA_PSS_PARAMS_free(pss
);
570 static int rsa_pss_verify_param(const EVP_MD
**pmd
, const EVP_MD
**pmgf1md
,
571 int *psaltlen
, int *ptrailerField
)
573 if (psaltlen
!= NULL
&& *psaltlen
< 0) {
574 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_SALT_LENGTH
);
578 * low-level routines support only trailer field 0xbc (value 1) and
579 * PKCS#1 says we should reject any other value anyway.
581 if (ptrailerField
!= NULL
&& *ptrailerField
!= 1) {
582 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_TRAILER
);
588 int ossl_rsa_pss_get_param(const RSA_PSS_PARAMS
*pss
, const EVP_MD
**pmd
,
589 const EVP_MD
**pmgf1md
, int *psaltlen
)
592 * Callers do not care about the trailer field, and yet, we must
593 * pass it from get_param to verify_param, since the latter checks
596 * When callers start caring, it's a simple thing to add another
597 * argument to this function.
599 int trailerField
= 0;
601 return ossl_rsa_pss_get_param_unverified(pss
, pmd
, pmgf1md
, psaltlen
,
603 && rsa_pss_verify_param(pmd
, pmgf1md
, psaltlen
, &trailerField
);
607 * Customised RSA item verification routine. This is called when a signature
608 * is encountered requiring special handling. We currently only handle PSS.
611 static int rsa_item_verify(EVP_MD_CTX
*ctx
, const ASN1_ITEM
*it
,
612 const void *asn
, const X509_ALGOR
*sigalg
,
613 const ASN1_BIT_STRING
*sig
, EVP_PKEY
*pkey
)
615 /* Sanity check: make sure it is PSS */
616 if (OBJ_obj2nid(sigalg
->algorithm
) != EVP_PKEY_RSA_PSS
) {
617 ERR_raise(ERR_LIB_RSA
, RSA_R_UNSUPPORTED_SIGNATURE_TYPE
);
620 if (ossl_rsa_pss_to_ctx(ctx
, NULL
, sigalg
, pkey
) > 0) {
627 static int rsa_item_sign(EVP_MD_CTX
*ctx
, const ASN1_ITEM
*it
, const void *asn
,
628 X509_ALGOR
*alg1
, X509_ALGOR
*alg2
,
629 ASN1_BIT_STRING
*sig
)
632 EVP_PKEY_CTX
*pkctx
= EVP_MD_CTX_get_pkey_ctx(ctx
);
634 if (EVP_PKEY_CTX_get_rsa_padding(pkctx
, &pad_mode
) <= 0)
636 if (pad_mode
== RSA_PKCS1_PADDING
)
638 if (pad_mode
== RSA_PKCS1_PSS_PADDING
) {
639 ASN1_STRING
*os1
= ossl_rsa_ctx_to_pss_string(pkctx
);
643 /* Duplicate parameters if we have to */
645 ASN1_STRING
*os2
= ASN1_STRING_dup(os1
);
649 if (!X509_ALGOR_set0(alg2
, OBJ_nid2obj(EVP_PKEY_RSA_PSS
),
650 V_ASN1_SEQUENCE
, os2
)) {
651 ASN1_STRING_free(os2
);
655 if (!X509_ALGOR_set0(alg1
, OBJ_nid2obj(EVP_PKEY_RSA_PSS
),
656 V_ASN1_SEQUENCE
, os1
))
660 ASN1_STRING_free(os1
);
666 static int rsa_sig_info_set(X509_SIG_INFO
*siginf
, const X509_ALGOR
*sigalg
,
667 const ASN1_STRING
*sig
)
672 const EVP_MD
*mgf1md
= NULL
, *md
= NULL
;
676 /* Sanity check: make sure it is PSS */
677 if (OBJ_obj2nid(sigalg
->algorithm
) != EVP_PKEY_RSA_PSS
)
679 /* Decode PSS parameters */
680 pss
= ossl_rsa_pss_decode(sigalg
);
681 if (!ossl_rsa_pss_get_param(pss
, &md
, &mgf1md
, &saltlen
))
683 mdnid
= EVP_MD_get_type(md
);
685 * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
686 * match and salt length must equal digest size
688 if ((mdnid
== NID_sha256
|| mdnid
== NID_sha384
|| mdnid
== NID_sha512
)
689 && mdnid
== EVP_MD_get_type(mgf1md
)
690 && saltlen
== EVP_MD_get_size(md
))
691 flags
= X509_SIG_INFO_TLS
;
694 /* Note: security bits half number of digest bits */
695 secbits
= EVP_MD_get_size(md
) * 4;
697 * SHA1 and MD5 are known to be broken. Reduce security bits so that
698 * they're no longer accepted at security level 1. The real values don't
699 * really matter as long as they're lower than 80, which is our security
701 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
703 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
704 * puts a chosen-prefix attack for MD5 at 2^39.
706 if (mdnid
== NID_sha1
)
708 else if (mdnid
== NID_md5_sha1
)
710 else if (mdnid
== NID_md5
)
712 X509_SIG_INFO_set(siginf
, mdnid
, EVP_PKEY_RSA_PSS
, secbits
,
716 RSA_PSS_PARAMS_free(pss
);
720 static int rsa_pkey_check(const EVP_PKEY
*pkey
)
722 return RSA_check_key_ex(pkey
->pkey
.rsa
, NULL
);
725 static size_t rsa_pkey_dirty_cnt(const EVP_PKEY
*pkey
)
727 return pkey
->pkey
.rsa
->dirty_cnt
;
731 * There is no need to do RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS)
732 * checks in this method since the caller tests EVP_KEYMGMT_is_a() first.
734 static int rsa_int_export_to(const EVP_PKEY
*from
, int rsa_type
,
736 OSSL_FUNC_keymgmt_import_fn
*importer
,
737 OSSL_LIB_CTX
*libctx
, const char *propq
)
739 RSA
*rsa
= from
->pkey
.rsa
;
740 OSSL_PARAM_BLD
*tmpl
= OSSL_PARAM_BLD_new();
741 OSSL_PARAM
*params
= NULL
;
747 /* Public parameters must always be present */
748 if (RSA_get0_n(rsa
) == NULL
|| RSA_get0_e(rsa
) == NULL
)
751 if (!ossl_rsa_todata(rsa
, tmpl
, NULL
, 1))
754 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
755 if (RSA_get0_d(rsa
) != NULL
)
756 selection
|= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
758 if (rsa
->pss
!= NULL
) {
759 const EVP_MD
*md
= NULL
, *mgf1md
= NULL
;
760 int md_nid
, mgf1md_nid
, saltlen
, trailerfield
;
761 RSA_PSS_PARAMS_30 pss_params
;
763 if (!ossl_rsa_pss_get_param_unverified(rsa
->pss
, &md
, &mgf1md
,
764 &saltlen
, &trailerfield
))
766 md_nid
= EVP_MD_get_type(md
);
767 mgf1md_nid
= EVP_MD_get_type(mgf1md
);
768 if (!ossl_rsa_pss_params_30_set_defaults(&pss_params
)
769 || !ossl_rsa_pss_params_30_set_hashalg(&pss_params
, md_nid
)
770 || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params
,
772 || !ossl_rsa_pss_params_30_set_saltlen(&pss_params
, saltlen
)
773 || !ossl_rsa_pss_params_30_todata(&pss_params
, tmpl
, NULL
))
775 selection
|= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
;
778 if ((params
= OSSL_PARAM_BLD_to_param(tmpl
)) == NULL
)
781 /* We export, the provider imports */
782 rv
= importer(to_keydata
, selection
, params
);
785 OSSL_PARAM_free(params
);
786 OSSL_PARAM_BLD_free(tmpl
);
790 static int rsa_int_import_from(const OSSL_PARAM params
[], void *vpctx
,
793 EVP_PKEY_CTX
*pctx
= vpctx
;
794 EVP_PKEY
*pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
795 RSA
*rsa
= ossl_rsa_new_with_ctx(pctx
->libctx
);
796 RSA_PSS_PARAMS_30 rsa_pss_params
= { 0, };
797 int pss_defaults_set
= 0;
801 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
805 RSA_clear_flags(rsa
, RSA_FLAG_TYPE_MASK
);
806 RSA_set_flags(rsa
, rsa_type
);
808 if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params
, &pss_defaults_set
,
809 params
, pctx
->libctx
))
813 case RSA_FLAG_TYPE_RSA
:
815 * Were PSS parameters filled in?
816 * In that case, something's wrong
818 if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params
))
821 case RSA_FLAG_TYPE_RSASSAPSS
:
823 * Were PSS parameters filled in? In that case, create the old
824 * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
826 if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params
)) {
827 /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
828 int mdnid
= ossl_rsa_pss_params_30_hashalg(&rsa_pss_params
);
829 int mgf1mdnid
= ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params
);
830 int saltlen
= ossl_rsa_pss_params_30_saltlen(&rsa_pss_params
);
831 const EVP_MD
*md
= EVP_get_digestbynid(mdnid
);
832 const EVP_MD
*mgf1md
= EVP_get_digestbynid(mgf1mdnid
);
834 if ((rsa
->pss
= ossl_rsa_pss_params_create(md
, mgf1md
,
840 /* RSA key sub-types we don't know how to handle yet */
844 if (!ossl_rsa_fromdata(rsa
, params
, 1))
848 case RSA_FLAG_TYPE_RSA
:
849 ok
= EVP_PKEY_assign_RSA(pkey
, rsa
);
851 case RSA_FLAG_TYPE_RSASSAPSS
:
852 ok
= EVP_PKEY_assign(pkey
, EVP_PKEY_RSA_PSS
, rsa
);
862 static int rsa_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
863 OSSL_FUNC_keymgmt_import_fn
*importer
,
864 OSSL_LIB_CTX
*libctx
, const char *propq
)
866 return rsa_int_export_to(from
, RSA_FLAG_TYPE_RSA
, to_keydata
,
867 importer
, libctx
, propq
);
870 static int rsa_pss_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
871 OSSL_FUNC_keymgmt_import_fn
*importer
,
872 OSSL_LIB_CTX
*libctx
, const char *propq
)
874 return rsa_int_export_to(from
, RSA_FLAG_TYPE_RSASSAPSS
, to_keydata
,
875 importer
, libctx
, propq
);
878 static int rsa_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
880 return rsa_int_import_from(params
, vpctx
, RSA_FLAG_TYPE_RSA
);
883 static int rsa_pss_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
885 return rsa_int_import_from(params
, vpctx
, RSA_FLAG_TYPE_RSASSAPSS
);
888 static int rsa_pkey_copy(EVP_PKEY
*to
, EVP_PKEY
*from
)
890 RSA
*rsa
= from
->pkey
.rsa
;
895 dupkey
= ossl_rsa_dup(rsa
, OSSL_KEYMGMT_SELECT_ALL
);
900 ret
= EVP_PKEY_assign(to
, from
->type
, dupkey
);
906 const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths
[2] = {
910 ASN1_PKEY_SIGPARAM_NULL
,
913 "OpenSSL RSA method",
945 rsa_pkey_import_from
,
955 const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth
= {
958 ASN1_PKEY_SIGPARAM_NULL
,
961 "OpenSSL RSA-PSS method",
991 rsa_pss_pkey_export_to
,
992 rsa_pss_pkey_import_from
,