Change rsa gen so it can use the propq from OSSL_PKEY_PARAM_RSA_DIGEST
[openssl.git] / crypto / rsa / rsa_ameth.c
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.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"
28
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);
34 #endif
35
36 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
37 static int rsa_sync_to_pss_params_30(RSA *rsa);
38
39 /* Set any parameters associated with pkey */
40 static int rsa_param_encode(const EVP_PKEY *pkey,
41                             ASN1_STRING **pstr, int *pstrtype)
42 {
43     const RSA *rsa = pkey->pkey.rsa;
44
45     *pstr = NULL;
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;
49         return 1;
50     }
51     /* If no PSS parameters we omit parameters entirely */
52     if (rsa->pss == NULL) {
53         *pstrtype = V_ASN1_UNDEF;
54         return 1;
55     }
56     /* Encode PSS parameters */
57     if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
58         return 0;
59
60     *pstrtype = V_ASN1_SEQUENCE;
61     return 1;
62 }
63 /* Decode any parameters and set them in RSA structure */
64 static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
65 {
66     const ASN1_OBJECT *algoid;
67     const void *algp;
68     int algptype;
69
70     X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
71     if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
72         return 1;
73     if (algptype == V_ASN1_UNDEF)
74         return 1;
75     if (algptype != V_ASN1_SEQUENCE) {
76         RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
77         return 0;
78     }
79     rsa->pss = rsa_pss_decode(alg);
80     if (rsa->pss == NULL)
81         return 0;
82     if (!rsa_sync_to_pss_params_30(rsa))
83         return 0;
84     return 1;
85 }
86
87 static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
88 {
89     unsigned char *penc = NULL;
90     int penclen;
91     ASN1_STRING *str;
92     int strtype;
93
94     if (!rsa_param_encode(pkey, &str, &strtype))
95         return 0;
96     penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
97     if (penclen <= 0)
98         return 0;
99     if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
100                                strtype, str, penc, penclen))
101         return 1;
102
103     OPENSSL_free(penc);
104     return 0;
105 }
106
107 static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
108 {
109     const unsigned char *p;
110     int pklen;
111     X509_ALGOR *alg;
112     RSA *rsa = NULL;
113
114     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
115         return 0;
116     if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
117         RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
118         return 0;
119     }
120     if (!rsa_param_decode(rsa, alg)) {
121         RSA_free(rsa);
122         return 0;
123     }
124
125     RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
126     switch (pkey->ameth->pkey_id) {
127     case EVP_PKEY_RSA:
128         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
129         break;
130     case EVP_PKEY_RSA_PSS:
131         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
132         break;
133     default:
134         /* Leave the type bits zero */
135         break;
136     }
137
138     if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
139         RSA_free(rsa);
140         return 0;
141     }
142     return 1;
143 }
144
145 static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
146 {
147     /*
148      * Don't check the public/private key, this is mostly for smart
149      * cards.
150      */
151     if (((RSA_flags(a->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
152             || (RSA_flags(b->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) {
153         return 1;
154     }
155
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)
158         return 0;
159     return 1;
160 }
161
162 static int old_rsa_priv_decode(EVP_PKEY *pkey,
163                                const unsigned char **pder, int derlen)
164 {
165     RSA *rsa;
166
167     if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
168         RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
169         return 0;
170     }
171     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
172     return 1;
173 }
174
175 static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
176 {
177     return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
178 }
179
180 static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
181 {
182     unsigned char *rk = NULL;
183     int rklen;
184     ASN1_STRING *str;
185     int strtype;
186
187     if (!rsa_param_encode(pkey, &str, &strtype))
188         return 0;
189     rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
190
191     if (rklen <= 0) {
192         RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
193         ASN1_STRING_free(str);
194         return 0;
195     }
196
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);
201         return 0;
202     }
203
204     return 1;
205 }
206
207 static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
208 {
209     const unsigned char *p;
210     RSA *rsa;
211     int pklen;
212     const X509_ALGOR *alg;
213
214     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
215         return 0;
216     rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
217     if (rsa == NULL) {
218         RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
219         return 0;
220     }
221     if (!rsa_param_decode(rsa, alg)) {
222         RSA_free(rsa);
223         return 0;
224     }
225
226     RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
227     switch (pkey->ameth->pkey_id) {
228     case EVP_PKEY_RSA:
229         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
230         break;
231     case EVP_PKEY_RSA_PSS:
232         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
233         break;
234     default:
235         /* Leave the type bits zero */
236         break;
237     }
238
239     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
240     return 1;
241 }
242
243 static int int_rsa_size(const EVP_PKEY *pkey)
244 {
245     return RSA_size(pkey->pkey.rsa);
246 }
247
248 static int rsa_bits(const EVP_PKEY *pkey)
249 {
250     return BN_num_bits(pkey->pkey.rsa->n);
251 }
252
253 static int rsa_security_bits(const EVP_PKEY *pkey)
254 {
255     return RSA_security_bits(pkey->pkey.rsa);
256 }
257
258 static void int_rsa_free(EVP_PKEY *pkey)
259 {
260     RSA_free(pkey->pkey.rsa);
261 }
262
263 static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
264 {
265     if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
266         return NULL;
267     return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
268                                      alg->parameter);
269 }
270
271 static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
272                                int indent)
273 {
274     int rv = 0;
275     X509_ALGOR *maskHash = NULL;
276
277     if (!BIO_indent(bp, indent, 128))
278         goto err;
279     if (pss_key) {
280         if (pss == NULL) {
281             if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
282                 return 0;
283             return 1;
284         } else {
285             if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
286                 return 0;
287         }
288     } else if (pss == NULL) {
289         if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
290             return 0;
291         return 1;
292     }
293     if (BIO_puts(bp, "\n") <= 0)
294         goto err;
295     if (pss_key)
296         indent += 2;
297     if (!BIO_indent(bp, indent, 128))
298         goto err;
299     if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
300         goto err;
301
302     if (pss->hashAlgorithm) {
303         if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
304             goto err;
305     } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
306         goto err;
307     }
308
309     if (BIO_puts(bp, "\n") <= 0)
310         goto err;
311
312     if (!BIO_indent(bp, indent, 128))
313         goto err;
314
315     if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
316         goto err;
317     if (pss->maskGenAlgorithm) {
318         if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
319             goto err;
320         if (BIO_puts(bp, " with ") <= 0)
321             goto err;
322         maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
323         if (maskHash != NULL) {
324             if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
325                 goto err;
326         } else if (BIO_puts(bp, "INVALID") <= 0) {
327             goto err;
328         }
329     } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
330         goto err;
331     }
332     BIO_puts(bp, "\n");
333
334     if (!BIO_indent(bp, indent, 128))
335         goto err;
336     if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
337         goto err;
338     if (pss->saltLength) {
339         if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
340             goto err;
341     } else if (BIO_puts(bp, "14 (default)") <= 0) {
342         goto err;
343     }
344     BIO_puts(bp, "\n");
345
346     if (!BIO_indent(bp, indent, 128))
347         goto err;
348     if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
349         goto err;
350     if (pss->trailerField) {
351         if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
352             goto err;
353     } else if (BIO_puts(bp, "BC (default)") <= 0) {
354         goto err;
355     }
356     BIO_puts(bp, "\n");
357
358     rv = 1;
359
360  err:
361     X509_ALGOR_free(maskHash);
362     return rv;
363
364 }
365
366 static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
367 {
368     const RSA *x = pkey->pkey.rsa;
369     char *str;
370     const char *s;
371     int ret = 0, mod_len = 0, ex_primes;
372
373     if (x->n != NULL)
374         mod_len = BN_num_bits(x->n);
375     ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
376
377     if (!BIO_indent(bp, off, 128))
378         goto err;
379
380     if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ?  "RSA-PSS" : "RSA") <= 0)
381         goto err;
382
383     if (priv && x->d) {
384         if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
385                        mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
386             goto err;
387         str = "modulus:";
388         s = "publicExponent:";
389     } else {
390         if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
391             goto err;
392         str = "Modulus:";
393         s = "Exponent:";
394     }
395     if (!ASN1_bn_print(bp, str, x->n, NULL, off))
396         goto err;
397     if (!ASN1_bn_print(bp, s, x->e, NULL, off))
398         goto err;
399     if (priv) {
400         int i;
401
402         if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
403             goto err;
404         if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
405             goto err;
406         if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
407             goto err;
408         if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
409             goto err;
410         if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
411             goto err;
412         if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
413             goto err;
414         for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
415             /* print multi-prime info */
416             BIGNUM *bn = NULL;
417             RSA_PRIME_INFO *pinfo;
418             int j;
419
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))
423                     goto err;
424                 switch (j) {
425                 case 0:
426                     if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
427                         goto err;
428                     bn = pinfo->r;
429                     break;
430                 case 1:
431                     if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
432                         goto err;
433                     bn = pinfo->d;
434                     break;
435                 case 2:
436                     if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
437                         goto err;
438                     bn = pinfo->t;
439                     break;
440                 default:
441                     break;
442                 }
443                 if (!ASN1_bn_print(bp, "", bn, NULL, off))
444                     goto err;
445             }
446         }
447     }
448     if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
449         goto err;
450     ret = 1;
451  err:
452     return ret;
453 }
454
455 static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
456                          ASN1_PCTX *ctx)
457 {
458     return pkey_rsa_print(bp, pkey, indent, 0);
459 }
460
461 static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
462                           ASN1_PCTX *ctx)
463 {
464     return pkey_rsa_print(bp, pkey, indent, 1);
465 }
466
467 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
468 {
469     RSA_PSS_PARAMS *pss;
470
471     pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
472                                     alg->parameter);
473
474     if (pss == NULL)
475         return NULL;
476
477     if (pss->maskGenAlgorithm != NULL) {
478         pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
479         if (pss->maskHash == NULL) {
480             RSA_PSS_PARAMS_free(pss);
481             return NULL;
482         }
483     }
484
485     return pss;
486 }
487
488 static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
489                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
490 {
491     if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
492         int rv;
493         RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
494
495         rv = rsa_pss_param_print(bp, 0, pss, indent);
496         RSA_PSS_PARAMS_free(pss);
497         if (!rv)
498             return 0;
499     } else if (BIO_puts(bp, "\n") <= 0) {
500         return 0;
501     }
502     if (sig)
503         return X509_signature_dump(bp, sig, indent);
504     return 1;
505 }
506
507 static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
508 {
509     X509_ALGOR *alg = NULL;
510     const EVP_MD *md;
511     const EVP_MD *mgf1md;
512     int min_saltlen;
513
514     switch (op) {
515
516     case ASN1_PKEY_CTRL_PKCS7_SIGN:
517         if (arg1 == 0)
518             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
519         break;
520
521     case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
522         if (pkey_is_pss(pkey))
523             return -2;
524         if (arg1 == 0)
525             PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
526         break;
527 #ifndef OPENSSL_NO_CMS
528     case ASN1_PKEY_CTRL_CMS_SIGN:
529         if (arg1 == 0)
530             return rsa_cms_sign(arg2);
531         else if (arg1 == 1)
532             return rsa_cms_verify(arg2);
533         break;
534
535     case ASN1_PKEY_CTRL_CMS_ENVELOPE:
536         if (pkey_is_pss(pkey))
537             return -2;
538         if (arg1 == 0)
539             return rsa_cms_encrypt(arg2);
540         else if (arg1 == 1)
541             return rsa_cms_decrypt(arg2);
542         break;
543
544     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
545         if (pkey_is_pss(pkey))
546             return -2;
547         *(int *)arg2 = CMS_RECIPINFO_TRANS;
548         return 1;
549 #endif
550
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,
554                                    &min_saltlen)) {
555                 RSAerr(0, ERR_R_INTERNAL_ERROR);
556                 return 0;
557             }
558             *(int *)arg2 = EVP_MD_type(md);
559             /* Return of 2 indicates this MD is mandatory */
560             return 2;
561         }
562         *(int *)arg2 = NID_sha256;
563         return 1;
564
565     default:
566         return -2;
567
568     }
569
570     if (alg)
571         X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
572
573     return 1;
574
575 }
576
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)
579 {
580     if (md == NULL || EVP_MD_type(md) == NID_sha1)
581         return 1;
582     *palg = X509_ALGOR_new();
583     if (*palg == NULL)
584         return 0;
585     X509_ALGOR_set_md(*palg, md);
586     return 1;
587 }
588
589 /* Allocate and set MGF1 algorithm ID from EVP_MD */
590 static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
591 {
592     X509_ALGOR *algtmp = NULL;
593     ASN1_STRING *stmp = NULL;
594
595     *palg = NULL;
596     if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
597         return 1;
598     /* need to embed algorithm ID inside another */
599     if (!rsa_md_to_algor(&algtmp, mgf1md))
600         goto err;
601     if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
602          goto err;
603     *palg = X509_ALGOR_new();
604     if (*palg == NULL)
605         goto err;
606     X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
607     stmp = NULL;
608  err:
609     ASN1_STRING_free(stmp);
610     X509_ALGOR_free(algtmp);
611     if (*palg)
612         return 1;
613     return 0;
614 }
615
616 /* convert algorithm ID to EVP_MD, default SHA1 */
617 static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
618 {
619     const EVP_MD *md;
620
621     if (!alg)
622         return EVP_sha1();
623     md = EVP_get_digestbyobj(alg->algorithm);
624     if (md == NULL)
625         RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
626     return md;
627 }
628
629 /*
630  * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
631  * suitable for setting an AlgorithmIdentifier.
632  */
633
634 static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
635 {
636     const EVP_MD *sigmd, *mgf1md;
637     EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
638     RSA *rsa = EVP_PKEY_get0_RSA(pk);
639     int saltlen;
640
641     if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
642         return NULL;
643     if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
644         return NULL;
645     if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
646         return NULL;
647     if (saltlen == -1) {
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)
652             saltlen--;
653         if (saltlen < 0)
654             return NULL;
655     }
656
657     return rsa_pss_params_create(sigmd, mgf1md, saltlen);
658 }
659
660 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
661                                       const EVP_MD *mgf1md, int saltlen)
662 {
663     RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
664
665     if (pss == NULL)
666         goto err;
667     if (saltlen != 20) {
668         pss->saltLength = ASN1_INTEGER_new();
669         if (pss->saltLength == NULL)
670             goto err;
671         if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
672             goto err;
673     }
674     if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
675         goto err;
676     if (mgf1md == NULL)
677         mgf1md = sigmd;
678     if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
679         goto err;
680     if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
681         goto err;
682     return pss;
683  err:
684     RSA_PSS_PARAMS_free(pss);
685     return NULL;
686 }
687
688 static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
689 {
690     RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
691     ASN1_STRING *os;
692
693     if (pss == NULL)
694         return NULL;
695
696     os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
697     RSA_PSS_PARAMS_free(pss);
698     return os;
699 }
700
701 /*
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.
705  */
706
707 static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
708                           const X509_ALGOR *sigalg, EVP_PKEY *pkey)
709 {
710     int rv = -1;
711     int saltlen;
712     const EVP_MD *mgf1md = NULL, *md = NULL;
713     RSA_PSS_PARAMS *pss;
714
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);
718         return -1;
719     }
720     /* Decode PSS parameters */
721     pss = rsa_pss_decode(sigalg);
722
723     if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
724         RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
725         goto err;
726     }
727
728     /* We have all parameters now set up context */
729     if (pkey) {
730         if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
731             goto err;
732     } else {
733         const EVP_MD *checkmd;
734         if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
735             goto err;
736         if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
737             RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
738             goto err;
739         }
740     }
741
742     if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
743         goto err;
744
745     if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
746         goto err;
747
748     if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
749         goto err;
750     /* Carry on */
751     rv = 1;
752
753  err:
754     RSA_PSS_PARAMS_free(pss);
755     return rv;
756 }
757
758 static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md,
759                                 int *psaltlen, int *ptrailerField)
760 {
761     if (psaltlen != NULL && *psaltlen < 0) {
762         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
763         return 0;
764     }
765     /*
766      * low-level routines support only trailer field 0xbc (value 1) and
767      * PKCS#1 says we should reject any other value anyway.
768      */
769     if (ptrailerField != NULL && *ptrailerField != 1) {
770         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
771         return 0;
772     }
773     return 1;
774 }
775
776 static int rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
777                                         const EVP_MD **pmd,
778                                         const EVP_MD **pmgf1md,
779                                         int *psaltlen, int *ptrailerField)
780 {
781     RSA_PSS_PARAMS_30 pss_params;
782
783     /* Get the defaults from the ONE place */
784     (void)rsa_pss_params_30_set_defaults(&pss_params);
785
786     if (pss == NULL)
787         return 0;
788     *pmd = rsa_algor_to_md(pss->hashAlgorithm);
789     if (*pmd == NULL)
790         return 0;
791     *pmgf1md = rsa_algor_to_md(pss->maskHash);
792     if (*pmgf1md == NULL)
793         return 0;
794     if (pss->saltLength)
795         *psaltlen = ASN1_INTEGER_get(pss->saltLength);
796     else
797         *psaltlen = rsa_pss_params_30_saltlen(&pss_params);
798     if (pss->trailerField)
799         *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
800     else
801         *ptrailerField = rsa_pss_params_30_trailerfield(&pss_params);;
802
803     return 1;
804 }
805
806 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
807                       const EVP_MD **pmgf1md, int *psaltlen)
808 {
809     /*
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
812      * its value.
813      *
814      * When callers start caring, it's a simple thing to add another
815      * argument to this function.
816      */
817     int trailerField = 0;
818
819     return rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen,
820                                         &trailerField)
821         && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField);
822 }
823
824 static int rsa_sync_to_pss_params_30(RSA *rsa)
825 {
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;
830
831         /*
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
835          * a bad situation.
836          *
837          * Other routines use rsa_pss_get_param(), so the values will be
838          * checked, eventually.
839          */
840         if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
841                                           &saltlen, &trailerField))
842             return 0;
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))
850             return 0;
851         rsa->pss_params = pss_params;
852     }
853     return 1;
854 }
855
856 #ifndef OPENSSL_NO_CMS
857 static int rsa_cms_verify(CMS_SignerInfo *si)
858 {
859     int nid, nid2;
860     X509_ALGOR *alg;
861     EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
862
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);
870         return 0;
871     }
872     if (nid == NID_rsaEncryption)
873         return 1;
874     /* Workaround for some implementation that use a signature OID */
875     if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
876         if (nid2 == NID_rsaEncryption)
877             return 1;
878     }
879     return 0;
880 }
881 #endif
882
883 /*
884  * Customised RSA item verification routine. This is called when a signature
885  * is encountered requiring special handling. We currently only handle PSS.
886  */
887
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)
891 {
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);
895         return -1;
896     }
897     if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
898         /* Carry on */
899         return 2;
900     }
901     return -1;
902 }
903
904 #ifndef OPENSSL_NO_CMS
905 static int rsa_cms_sign(CMS_SignerInfo *si)
906 {
907     int pad_mode = RSA_PKCS1_PADDING;
908     X509_ALGOR *alg;
909     EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
910     ASN1_STRING *os = NULL;
911
912     CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
913     if (pkctx) {
914         if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
915             return 0;
916     }
917     if (pad_mode == RSA_PKCS1_PADDING) {
918         X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
919         return 1;
920     }
921     /* We don't support it */
922     if (pad_mode != RSA_PKCS1_PSS_PADDING)
923         return 0;
924     os = rsa_ctx_to_pss_string(pkctx);
925     if (!os)
926         return 0;
927     X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
928     return 1;
929 }
930 #endif
931
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)
935 {
936     int pad_mode;
937     EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
938
939     if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
940         return 0;
941     if (pad_mode == RSA_PKCS1_PADDING)
942         return 2;
943     if (pad_mode == RSA_PKCS1_PSS_PADDING) {
944         ASN1_STRING *os1 = NULL;
945         os1 = rsa_ctx_to_pss_string(pkctx);
946         if (!os1)
947             return 0;
948         /* Duplicate parameters if we have to */
949         if (alg2) {
950             ASN1_STRING *os2 = ASN1_STRING_dup(os1);
951             if (!os2) {
952                 ASN1_STRING_free(os1);
953                 return 0;
954             }
955             X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
956                             V_ASN1_SEQUENCE, os2);
957         }
958         X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
959                         V_ASN1_SEQUENCE, os1);
960         return 3;
961     }
962     return 2;
963 }
964
965 static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
966                             const ASN1_STRING *sig)
967 {
968     int rv = 0;
969     int mdnid, saltlen;
970     uint32_t flags;
971     const EVP_MD *mgf1md = NULL, *md = NULL;
972     RSA_PSS_PARAMS *pss;
973     int secbits;
974
975     /* Sanity check: make sure it is PSS */
976     if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
977         return 0;
978     /* Decode PSS parameters */
979     pss = rsa_pss_decode(sigalg);
980     if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
981         goto err;
982     mdnid = EVP_MD_type(md);
983     /*
984      * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
985      * match and salt length must equal digest size
986      */
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;
990     else
991         flags = 0;
992     /* Note: security bits half number of digest bits */
993     secbits = EVP_MD_size(md) * 4;
994     /*
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
998      * level 1.
999      * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
1000      * 2^63.4
1001      * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
1002      * puts a chosen-prefix attack for MD5 at 2^39.
1003      */
1004     if (mdnid == NID_sha1)
1005         secbits = 64;
1006     else if (mdnid == NID_md5_sha1)
1007         secbits = 68;
1008     else if (mdnid == NID_md5)
1009         secbits = 39;
1010     X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
1011                       flags);
1012     rv = 1;
1013     err:
1014     RSA_PSS_PARAMS_free(pss);
1015     return rv;
1016 }
1017
1018 #ifndef OPENSSL_NO_CMS
1019 static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
1020 {
1021     RSA_OAEP_PARAMS *oaep;
1022
1023     oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
1024                                      alg->parameter);
1025
1026     if (oaep == NULL)
1027         return NULL;
1028
1029     if (oaep->maskGenFunc != NULL) {
1030         oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
1031         if (oaep->maskHash == NULL) {
1032             RSA_OAEP_PARAMS_free(oaep);
1033             return NULL;
1034         }
1035     }
1036     return oaep;
1037 }
1038
1039 static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
1040 {
1041     EVP_PKEY_CTX *pkctx;
1042     X509_ALGOR *cmsalg;
1043     int nid;
1044     int rv = -1;
1045     unsigned char *label = NULL;
1046     int labellen = 0;
1047     const EVP_MD *mgf1md = NULL, *md = NULL;
1048     RSA_OAEP_PARAMS *oaep;
1049
1050     pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1051     if (pkctx == NULL)
1052         return 0;
1053     if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
1054         return -1;
1055     nid = OBJ_obj2nid(cmsalg->algorithm);
1056     if (nid == NID_rsaEncryption)
1057         return 1;
1058     if (nid != NID_rsaesOaep) {
1059         RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
1060         return -1;
1061     }
1062     /* Decode OAEP parameters */
1063     oaep = rsa_oaep_decode(cmsalg);
1064
1065     if (oaep == NULL) {
1066         RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
1067         goto err;
1068     }
1069
1070     mgf1md = rsa_algor_to_md(oaep->maskHash);
1071     if (mgf1md == NULL)
1072         goto err;
1073     md = rsa_algor_to_md(oaep->hashFunc);
1074     if (md == NULL)
1075         goto err;
1076
1077     if (oaep->pSourceFunc != NULL) {
1078         X509_ALGOR *plab = oaep->pSourceFunc;
1079
1080         if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
1081             RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
1082             goto err;
1083         }
1084         if (plab->parameter->type != V_ASN1_OCTET_STRING) {
1085             RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
1086             goto err;
1087         }
1088
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;
1093     }
1094
1095     if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
1096         goto err;
1097     if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
1098         goto err;
1099     if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
1100         goto err;
1101     if (label != NULL
1102             && EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
1103         goto err;
1104     /* Carry on */
1105     rv = 1;
1106
1107  err:
1108     RSA_OAEP_PARAMS_free(oaep);
1109     return rv;
1110 }
1111
1112 static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
1113 {
1114     const EVP_MD *md, *mgf1md;
1115     RSA_OAEP_PARAMS *oaep = NULL;
1116     ASN1_STRING *os = NULL;
1117     X509_ALGOR *alg;
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;
1121
1122     if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
1123         return 0;
1124     if (pkctx) {
1125         if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
1126             return 0;
1127     }
1128     if (pad_mode == RSA_PKCS1_PADDING) {
1129         X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
1130         return 1;
1131     }
1132     /* Not supported */
1133     if (pad_mode != RSA_PKCS1_OAEP_PADDING)
1134         return 0;
1135     if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
1136         goto err;
1137     if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
1138         goto err;
1139     labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
1140     if (labellen < 0)
1141         goto err;
1142     oaep = RSA_OAEP_PARAMS_new();
1143     if (oaep == NULL)
1144         goto err;
1145     if (!rsa_md_to_algor(&oaep->hashFunc, md))
1146         goto err;
1147     if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
1148         goto err;
1149     if (labellen > 0) {
1150         ASN1_OCTET_STRING *los;
1151         oaep->pSourceFunc = X509_ALGOR_new();
1152         if (oaep->pSourceFunc == NULL)
1153             goto err;
1154         los = ASN1_OCTET_STRING_new();
1155         if (los == NULL)
1156             goto err;
1157         if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
1158             ASN1_OCTET_STRING_free(los);
1159             goto err;
1160         }
1161         X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
1162                         V_ASN1_OCTET_STRING, los);
1163     }
1164     /* create string with pss parameter encoding. */
1165     if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
1166          goto err;
1167     X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
1168     os = NULL;
1169     rv = 1;
1170  err:
1171     RSA_OAEP_PARAMS_free(oaep);
1172     ASN1_STRING_free(os);
1173     return rv;
1174 }
1175 #endif
1176
1177 static int rsa_pkey_check(const EVP_PKEY *pkey)
1178 {
1179     return RSA_check_key_ex(pkey->pkey.rsa, NULL);
1180 }
1181
1182 static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
1183 {
1184     return pkey->pkey.rsa->dirty_cnt;
1185 }
1186
1187 /*
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
1196  * in this file.
1197  */
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)
1201 {
1202     RSA *rsa = from->pkey.rsa;
1203     OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
1204     OSSL_PARAM *params = NULL;
1205     int selection = 0;
1206     int rv = 0;
1207
1208     if (tmpl == NULL)
1209         return 0;
1210     /*
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.
1213      */
1214     if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
1215         goto err;
1216
1217     /* Public parameters must always be present */
1218     if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
1219         goto err;
1220
1221     if (!rsa_todata(rsa, tmpl, NULL))
1222         goto err;
1223
1224     selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
1225     if (RSA_get0_d(rsa) != NULL)
1226         selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
1227
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;
1232
1233         if (!rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
1234                                           &saltlen, &trailerfield))
1235             goto err;
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))
1243             goto err;
1244         selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
1245     }
1246
1247     if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
1248         goto err;
1249
1250     /* We export, the provider imports */
1251     rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
1252
1253  err:
1254     OSSL_PARAM_BLD_free_params(params);
1255     OSSL_PARAM_BLD_free(tmpl);
1256     return rv;
1257 }
1258
1259 static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
1260                                int rsa_type)
1261 {
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, };
1266     int ok = 0;
1267
1268     if (rsa == NULL) {
1269         ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
1270         return 0;
1271     }
1272
1273     RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
1274     RSA_set_flags(rsa, rsa_type);
1275
1276     if (!rsa_pss_params_30_fromdata(&rsa_pss_params, params, pctx->libctx))
1277         goto err;
1278
1279     switch (rsa_type) {
1280     case RSA_FLAG_TYPE_RSA:
1281         /*
1282          * Were PSS parameters filled in?
1283          * In that case, something's wrong
1284          */
1285         if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
1286             goto err;
1287         break;
1288     case RSA_FLAG_TYPE_RSASSAPSS:
1289         /*
1290          * Were PSS parameters filled in?  In that case, create the old
1291          * RSA_PSS_PARAMS structure.  Otherwise, this is an unrestricted key.
1292          */
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);
1300
1301             if ((rsa->pss = rsa_pss_params_create(md, mgf1md, saltlen)) == NULL)
1302                 goto err;
1303         }
1304         break;
1305     default:
1306         /* RSA key sub-types we don't know how to handle yet */
1307         goto err;
1308     }
1309
1310     if (!rsa_fromdata(rsa, params))
1311         goto err;
1312
1313     switch (rsa_type) {
1314     case RSA_FLAG_TYPE_RSA:
1315         ok = EVP_PKEY_assign_RSA(pkey, rsa);
1316         break;
1317     case RSA_FLAG_TYPE_RSASSAPSS:
1318         ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
1319         break;
1320     }
1321
1322  err:
1323     if (!ok)
1324         RSA_free(rsa);
1325     return ok;
1326 }
1327
1328 static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1329                               EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1330                               const char *propq)
1331 {
1332     return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
1333                              to_keymgmt, libctx, propq);
1334 }
1335
1336 static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
1337                                   EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
1338                                   const char *propq)
1339 {
1340     return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
1341                              to_keymgmt, libctx, propq);
1342 }
1343
1344 static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1345 {
1346     return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
1347 }
1348
1349 static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
1350 {
1351     return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
1352 }
1353
1354 const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
1355     {
1356      EVP_PKEY_RSA,
1357      EVP_PKEY_RSA,
1358      ASN1_PKEY_SIGPARAM_NULL,
1359
1360      "RSA",
1361      "OpenSSL RSA method",
1362
1363      rsa_pub_decode,
1364      rsa_pub_encode,
1365      rsa_pub_cmp,
1366      rsa_pub_print,
1367
1368      rsa_priv_decode,
1369      rsa_priv_encode,
1370      rsa_priv_print,
1371
1372      int_rsa_size,
1373      rsa_bits,
1374      rsa_security_bits,
1375
1376      0, 0, 0, 0, 0, 0,
1377
1378      rsa_sig_print,
1379      int_rsa_free,
1380      rsa_pkey_ctrl,
1381      old_rsa_priv_decode,
1382      old_rsa_priv_encode,
1383      rsa_item_verify,
1384      rsa_item_sign,
1385      rsa_sig_info_set,
1386      rsa_pkey_check,
1387
1388      0, 0,
1389      0, 0, 0, 0,
1390
1391      rsa_pkey_dirty_cnt,
1392      rsa_pkey_export_to,
1393      rsa_pkey_import_from
1394     },
1395
1396     {
1397      EVP_PKEY_RSA2,
1398      EVP_PKEY_RSA,
1399      ASN1_PKEY_ALIAS}
1400 };
1401
1402 const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
1403      EVP_PKEY_RSA_PSS,
1404      EVP_PKEY_RSA_PSS,
1405      ASN1_PKEY_SIGPARAM_NULL,
1406
1407      "RSA-PSS",
1408      "OpenSSL RSA-PSS method",
1409
1410      rsa_pub_decode,
1411      rsa_pub_encode,
1412      rsa_pub_cmp,
1413      rsa_pub_print,
1414
1415      rsa_priv_decode,
1416      rsa_priv_encode,
1417      rsa_priv_print,
1418
1419      int_rsa_size,
1420      rsa_bits,
1421      rsa_security_bits,
1422
1423      0, 0, 0, 0, 0, 0,
1424
1425      rsa_sig_print,
1426      int_rsa_free,
1427      rsa_pkey_ctrl,
1428      0, 0,
1429      rsa_item_verify,
1430      rsa_item_sign,
1431      0,
1432      rsa_pkey_check,
1433
1434      0, 0,
1435      0, 0, 0, 0,
1436
1437      rsa_pkey_dirty_cnt,
1438      rsa_pss_pkey_export_to,
1439      rsa_pss_pkey_import_from
1440 };