d1fee590a8b72b782f3b47a7251e63fe1b135a1b
[openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2  * Copyright 1995-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 <openssl/crypto.h>
18 #include <openssl/core_names.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "openssl/param_build.h"
24 #include "crypto/bn.h"
25 #include "crypto/evp.h"
26 #include "crypto/rsa.h"
27 #include "crypto/security_bits.h"
28 #include "rsa_local.h"
29
30 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
31
32 #ifndef FIPS_MODULE
33 RSA *RSA_new(void)
34 {
35     return rsa_new_intern(NULL, NULL);
36 }
37
38 const RSA_METHOD *RSA_get_method(const RSA *rsa)
39 {
40     return rsa->meth;
41 }
42
43 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
44 {
45     /*
46      * NB: The caller is specifically setting a method, so it's not up to us
47      * to deal with which ENGINE it comes from.
48      */
49     const RSA_METHOD *mtmp;
50     mtmp = rsa->meth;
51     if (mtmp->finish)
52         mtmp->finish(rsa);
53 #ifndef OPENSSL_NO_ENGINE
54     ENGINE_finish(rsa->engine);
55     rsa->engine = NULL;
56 #endif
57     rsa->meth = meth;
58     if (meth->init)
59         meth->init(rsa);
60     return 1;
61 }
62
63 RSA *RSA_new_method(ENGINE *engine)
64 {
65     return rsa_new_intern(engine, NULL);
66 }
67 #endif
68
69 RSA *rsa_new_with_ctx(OPENSSL_CTX *libctx)
70 {
71     return rsa_new_intern(NULL, libctx);
72 }
73
74 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
75 {
76     RSA *ret = OPENSSL_zalloc(sizeof(*ret));
77
78     if (ret == NULL) {
79         RSAerr(0, ERR_R_MALLOC_FAILURE);
80         return NULL;
81     }
82
83     ret->references = 1;
84     ret->lock = CRYPTO_THREAD_lock_new();
85     if (ret->lock == NULL) {
86         RSAerr(0, ERR_R_MALLOC_FAILURE);
87         OPENSSL_free(ret);
88         return NULL;
89     }
90
91     ret->libctx = libctx;
92     ret->meth = RSA_get_default_method();
93 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
94     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
95     if (engine) {
96         if (!ENGINE_init(engine)) {
97             RSAerr(0, ERR_R_ENGINE_LIB);
98             goto err;
99         }
100         ret->engine = engine;
101     } else {
102         ret->engine = ENGINE_get_default_RSA();
103     }
104     if (ret->engine) {
105         ret->meth = ENGINE_get_RSA(ret->engine);
106         if (ret->meth == NULL) {
107             RSAerr(0, ERR_R_ENGINE_LIB);
108             goto err;
109         }
110     }
111 #endif
112
113     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
114 #ifndef FIPS_MODULE
115     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
116         goto err;
117     }
118 #endif
119
120     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
121         RSAerr(0, ERR_R_INIT_FAIL);
122         goto err;
123     }
124
125     return ret;
126
127  err:
128     RSA_free(ret);
129     return NULL;
130 }
131
132 void RSA_free(RSA *r)
133 {
134     int i;
135
136     if (r == NULL)
137         return;
138
139     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
140     REF_PRINT_COUNT("RSA", r);
141     if (i > 0)
142         return;
143     REF_ASSERT_ISNT(i < 0);
144
145     if (r->meth != NULL && r->meth->finish != NULL)
146         r->meth->finish(r);
147 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
148     ENGINE_finish(r->engine);
149 #endif
150
151 #ifndef FIPS_MODULE
152     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
153 #endif
154
155     CRYPTO_THREAD_lock_free(r->lock);
156
157     BN_free(r->n);
158     BN_free(r->e);
159     BN_clear_free(r->d);
160     BN_clear_free(r->p);
161     BN_clear_free(r->q);
162     BN_clear_free(r->dmp1);
163     BN_clear_free(r->dmq1);
164     BN_clear_free(r->iqmp);
165 #ifndef FIPS_MODULE
166     RSA_PSS_PARAMS_free(r->pss);
167     sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
168 #endif
169     BN_BLINDING_free(r->blinding);
170     BN_BLINDING_free(r->mt_blinding);
171     OPENSSL_free(r->bignum_data);
172     OPENSSL_free(r);
173 }
174
175 int RSA_up_ref(RSA *r)
176 {
177     int i;
178
179     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
180         return 0;
181
182     REF_PRINT_COUNT("RSA", r);
183     REF_ASSERT_ISNT(i < 2);
184     return i > 1 ? 1 : 0;
185 }
186
187 #ifndef FIPS_MODULE
188 int RSA_set_ex_data(RSA *r, int idx, void *arg)
189 {
190     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
191 }
192
193 void *RSA_get_ex_data(const RSA *r, int idx)
194 {
195     return CRYPTO_get_ex_data(&r->ex_data, idx);
196 }
197 #endif
198
199 /*
200  * Define a scaling constant for our fixed point arithmetic.
201  * This value must be a power of two because the base two logarithm code
202  * makes this assumption.  The exponent must also be a multiple of three so
203  * that the scale factor has an exact cube root.  Finally, the scale factor
204  * should not be so large that a multiplication of two scaled numbers
205  * overflows a 64 bit unsigned integer.
206  */
207 static const unsigned int scale = 1 << 18;
208 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
209
210 /* Define some constants, none exceed 32 bits */
211 static const unsigned int log_2  = 0x02c5c8;    /* scale * log(2) */
212 static const unsigned int log_e  = 0x05c551;    /* scale * log2(M_E) */
213 static const unsigned int c1_923 = 0x07b126;    /* scale * 1.923 */
214 static const unsigned int c4_690 = 0x12c28f;    /* scale * 4.690 */
215
216 /*
217  * Multiply two scaled integers together and rescale the result.
218  */
219 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
220 {
221     return a * b / scale;
222 }
223
224 /*
225  * Calculate the cube root of a 64 bit scaled integer.
226  * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
227  * integer, this is not guaranteed after scaling, so this function has a
228  * 64 bit return.  This uses the shifting nth root algorithm with some
229  * algebraic simplifications.
230  */
231 static uint64_t icbrt64(uint64_t x)
232 {
233     uint64_t r = 0;
234     uint64_t b;
235     int s;
236
237     for (s = 63; s >= 0; s -= 3) {
238         r <<= 1;
239         b = 3 * r * (r + 1) + 1;
240         if ((x >> s) >= b) {
241             x -= b << s;
242             r++;
243         }
244     }
245     return r * cbrt_scale;
246 }
247
248 /*
249  * Calculate the natural logarithm of a 64 bit scaled integer.
250  * This is done by calculating a base two logarithm and scaling.
251  * The maximum logarithm (base 2) is 64 and this reduces base e, so
252  * a 32 bit result should not overflow.  The argument passed must be
253  * greater than unity so we don't need to handle negative results.
254  */
255 static uint32_t ilog_e(uint64_t v)
256 {
257     uint32_t i, r = 0;
258
259     /*
260      * Scale down the value into the range 1 .. 2.
261      *
262      * If fractional numbers need to be processed, another loop needs
263      * to go here that checks v < scale and if so multiplies it by 2 and
264      * reduces r by scale.  This also means making r signed.
265      */
266     while (v >= 2 * scale) {
267         v >>= 1;
268         r += scale;
269     }
270     for (i = scale / 2; i != 0; i /= 2) {
271         v = mul2(v, v);
272         if (v >= 2 * scale) {
273             v >>= 1;
274             r += i;
275         }
276     }
277     r = (r * (uint64_t)scale) / log_e;
278     return r;
279 }
280
281 /*
282  * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
283  * Modulus Lengths.
284  *
285  * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
286  * for FFC safe prime groups for modp and ffdhe.
287  * After Table 25 and Table 26 it refers to
288  * "The maximum security strength estimates were calculated using the formula in
289  * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
290  * bits".
291  *
292  * The formula is:
293  *
294  * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
295  *           \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
296  * The two cube roots are merged together here.
297  */
298 uint16_t ifc_ffc_compute_security_bits(int n)
299 {
300     uint64_t x;
301     uint32_t lx;
302     uint16_t y;
303
304     /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
305     switch (n) {
306     case 2048:
307         return 112;
308     case 3072:
309         return 128;
310     case 4096:
311         return 152;
312     case 6144:
313         return 176;
314     case 8192:
315         return 200;
316     }
317     /*
318      * The first incorrect result (i.e. not accurate or off by one low) occurs
319      * for n = 699668.  The true value here is 1200.  Instead of using this n
320      * as the check threshold, the smallest n such that the correct result is
321      * 1200 is used instead.
322      */
323     if (n >= 687737)
324         return 1200;
325     if (n < 8)
326         return 0;
327
328     x = n * (uint64_t)log_2;
329     lx = ilog_e(x);
330     y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
331                    / log_2);
332     return (y + 4) & ~7;
333 }
334
335
336
337 int RSA_security_bits(const RSA *rsa)
338 {
339     int bits = BN_num_bits(rsa->n);
340
341 #ifndef FIPS_MODULE
342     if (rsa->version == RSA_ASN1_VERSION_MULTI) {
343         /* This ought to mean that we have private key at hand. */
344         int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
345
346         if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
347             return 0;
348     }
349 #endif
350     return ifc_ffc_compute_security_bits(bits);
351 }
352
353 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
354 {
355     /* If the fields n and e in r are NULL, the corresponding input
356      * parameters MUST be non-NULL for n and e.  d may be
357      * left NULL (in case only the public key is used).
358      */
359     if ((r->n == NULL && n == NULL)
360         || (r->e == NULL && e == NULL))
361         return 0;
362
363     if (n != NULL) {
364         BN_free(r->n);
365         r->n = n;
366     }
367     if (e != NULL) {
368         BN_free(r->e);
369         r->e = e;
370     }
371     if (d != NULL) {
372         BN_clear_free(r->d);
373         r->d = d;
374         BN_set_flags(r->d, BN_FLG_CONSTTIME);
375     }
376     r->dirty_cnt++;
377
378     return 1;
379 }
380
381 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
382 {
383     /* If the fields p and q in r are NULL, the corresponding input
384      * parameters MUST be non-NULL.
385      */
386     if ((r->p == NULL && p == NULL)
387         || (r->q == NULL && q == NULL))
388         return 0;
389
390     if (p != NULL) {
391         BN_clear_free(r->p);
392         r->p = p;
393         BN_set_flags(r->p, BN_FLG_CONSTTIME);
394     }
395     if (q != NULL) {
396         BN_clear_free(r->q);
397         r->q = q;
398         BN_set_flags(r->q, BN_FLG_CONSTTIME);
399     }
400     r->dirty_cnt++;
401
402     return 1;
403 }
404
405 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
406 {
407     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
408      * parameters MUST be non-NULL.
409      */
410     if ((r->dmp1 == NULL && dmp1 == NULL)
411         || (r->dmq1 == NULL && dmq1 == NULL)
412         || (r->iqmp == NULL && iqmp == NULL))
413         return 0;
414
415     if (dmp1 != NULL) {
416         BN_clear_free(r->dmp1);
417         r->dmp1 = dmp1;
418         BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
419     }
420     if (dmq1 != NULL) {
421         BN_clear_free(r->dmq1);
422         r->dmq1 = dmq1;
423         BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
424     }
425     if (iqmp != NULL) {
426         BN_clear_free(r->iqmp);
427         r->iqmp = iqmp;
428         BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
429     }
430     r->dirty_cnt++;
431
432     return 1;
433 }
434
435 #ifndef FIPS_MODULE
436 /*
437  * Is it better to export RSA_PRIME_INFO structure
438  * and related functions to let user pass a triplet?
439  */
440 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
441                                 BIGNUM *coeffs[], int pnum)
442 {
443     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
444     RSA_PRIME_INFO *pinfo;
445     int i;
446
447     if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
448         return 0;
449
450     prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
451     if (prime_infos == NULL)
452         return 0;
453
454     if (r->prime_infos != NULL)
455         old = r->prime_infos;
456
457     for (i = 0; i < pnum; i++) {
458         pinfo = rsa_multip_info_new();
459         if (pinfo == NULL)
460             goto err;
461         if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
462             BN_clear_free(pinfo->r);
463             BN_clear_free(pinfo->d);
464             BN_clear_free(pinfo->t);
465             pinfo->r = primes[i];
466             pinfo->d = exps[i];
467             pinfo->t = coeffs[i];
468             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
469             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
470             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
471         } else {
472             rsa_multip_info_free(pinfo);
473             goto err;
474         }
475         (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
476     }
477
478     r->prime_infos = prime_infos;
479
480     if (!rsa_multip_calc_product(r)) {
481         r->prime_infos = old;
482         goto err;
483     }
484
485     if (old != NULL) {
486         /*
487          * This is hard to deal with, since the old infos could
488          * also be set by this function and r, d, t should not
489          * be freed in that case. So currently, stay consistent
490          * with other *set0* functions: just free it...
491          */
492         sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
493     }
494
495     r->version = RSA_ASN1_VERSION_MULTI;
496     r->dirty_cnt++;
497
498     return 1;
499  err:
500     /* r, d, t should not be freed */
501     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
502     return 0;
503 }
504 #endif
505
506 void RSA_get0_key(const RSA *r,
507                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
508 {
509     if (n != NULL)
510         *n = r->n;
511     if (e != NULL)
512         *e = r->e;
513     if (d != NULL)
514         *d = r->d;
515 }
516
517 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
518 {
519     if (p != NULL)
520         *p = r->p;
521     if (q != NULL)
522         *q = r->q;
523 }
524
525 #ifndef FIPS_MODULE
526 int RSA_get_multi_prime_extra_count(const RSA *r)
527 {
528     int pnum;
529
530     pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
531     if (pnum <= 0)
532         pnum = 0;
533     return pnum;
534 }
535
536 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
537 {
538     int pnum, i;
539     RSA_PRIME_INFO *pinfo;
540
541     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
542         return 0;
543
544     /*
545      * return other primes
546      * it's caller's responsibility to allocate oth_primes[pnum]
547      */
548     for (i = 0; i < pnum; i++) {
549         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
550         primes[i] = pinfo->r;
551     }
552
553     return 1;
554 }
555 #endif
556
557 void RSA_get0_crt_params(const RSA *r,
558                          const BIGNUM **dmp1, const BIGNUM **dmq1,
559                          const BIGNUM **iqmp)
560 {
561     if (dmp1 != NULL)
562         *dmp1 = r->dmp1;
563     if (dmq1 != NULL)
564         *dmq1 = r->dmq1;
565     if (iqmp != NULL)
566         *iqmp = r->iqmp;
567 }
568
569 #ifndef FIPS_MODULE
570 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
571                                     const BIGNUM *coeffs[])
572 {
573     int pnum;
574
575     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
576         return 0;
577
578     /* return other primes */
579     if (exps != NULL || coeffs != NULL) {
580         RSA_PRIME_INFO *pinfo;
581         int i;
582
583         /* it's the user's job to guarantee the buffer length */
584         for (i = 0; i < pnum; i++) {
585             pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
586             if (exps != NULL)
587                 exps[i] = pinfo->d;
588             if (coeffs != NULL)
589                 coeffs[i] = pinfo->t;
590         }
591     }
592
593     return 1;
594 }
595 #endif
596
597 const BIGNUM *RSA_get0_n(const RSA *r)
598 {
599     return r->n;
600 }
601
602 const BIGNUM *RSA_get0_e(const RSA *r)
603 {
604     return r->e;
605 }
606
607 const BIGNUM *RSA_get0_d(const RSA *r)
608 {
609     return r->d;
610 }
611
612 const BIGNUM *RSA_get0_p(const RSA *r)
613 {
614     return r->p;
615 }
616
617 const BIGNUM *RSA_get0_q(const RSA *r)
618 {
619     return r->q;
620 }
621
622 const BIGNUM *RSA_get0_dmp1(const RSA *r)
623 {
624     return r->dmp1;
625 }
626
627 const BIGNUM *RSA_get0_dmq1(const RSA *r)
628 {
629     return r->dmq1;
630 }
631
632 const BIGNUM *RSA_get0_iqmp(const RSA *r)
633 {
634     return r->iqmp;
635 }
636
637 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
638 {
639 #ifdef FIPS_MODULE
640     return NULL;
641 #else
642     return r->pss;
643 #endif
644 }
645
646 /* Internal */
647 RSA_PSS_PARAMS_30 *rsa_get0_pss_params_30(RSA *r)
648 {
649     return &r->pss_params;
650 }
651
652 void RSA_clear_flags(RSA *r, int flags)
653 {
654     r->flags &= ~flags;
655 }
656
657 int RSA_test_flags(const RSA *r, int flags)
658 {
659     return r->flags & flags;
660 }
661
662 void RSA_set_flags(RSA *r, int flags)
663 {
664     r->flags |= flags;
665 }
666
667 int RSA_get_version(RSA *r)
668 {
669     /* { two-prime(0), multi(1) } */
670     return r->version;
671 }
672
673 #ifndef FIPS_MODULE
674 ENGINE *RSA_get0_engine(const RSA *r)
675 {
676     return r->engine;
677 }
678
679 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
680 {
681     /* If key type not RSA or RSA-PSS return error */
682     if (ctx != NULL && ctx->pmeth != NULL
683         && ctx->pmeth->pkey_id != EVP_PKEY_RSA
684         && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
685         return -1;
686      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
687 }
688 #endif
689
690 DEFINE_STACK_OF(BIGNUM)
691
692 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
693                         const STACK_OF(BIGNUM) *exps,
694                         const STACK_OF(BIGNUM) *coeffs)
695 {
696 #ifndef FIPS_MODULE
697     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
698 #endif
699     int pnum;
700
701     if (primes == NULL || exps == NULL || coeffs == NULL)
702         return 0;
703
704     pnum = sk_BIGNUM_num(primes);
705     if (pnum < 2
706         || pnum != sk_BIGNUM_num(exps)
707         || pnum != sk_BIGNUM_num(coeffs) + 1)
708         return 0;
709
710     if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
711                           sk_BIGNUM_value(primes, 1))
712         || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
713                                 sk_BIGNUM_value(exps, 1),
714                                 sk_BIGNUM_value(coeffs, 0)))
715         return 0;
716
717 #ifndef FIPS_MODULE
718     old_infos = r->prime_infos;
719 #endif
720
721     if (pnum > 2) {
722 #ifndef FIPS_MODULE
723         int i;
724
725         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
726         if (prime_infos == NULL)
727             return 0;
728
729         for (i = 2; i < pnum; i++) {
730             BIGNUM *prime = sk_BIGNUM_value(primes, i);
731             BIGNUM *exp = sk_BIGNUM_value(exps, i);
732             BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
733             RSA_PRIME_INFO *pinfo = NULL;
734
735             if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
736                 goto err;
737
738             /* Using rsa_multip_info_new() is wasteful, so allocate directly */
739             if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
740                 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
741                 goto err;
742             }
743
744             pinfo->r = prime;
745             pinfo->d = exp;
746             pinfo->t = coeff;
747             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
748             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
749             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
750             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
751         }
752
753         r->prime_infos = prime_infos;
754
755         if (!rsa_multip_calc_product(r)) {
756             r->prime_infos = old_infos;
757             goto err;
758         }
759 #else
760         return 0;
761 #endif
762     }
763
764 #ifndef FIPS_MODULE
765     if (old_infos != NULL) {
766         /*
767          * This is hard to deal with, since the old infos could
768          * also be set by this function and r, d, t should not
769          * be freed in that case. So currently, stay consistent
770          * with other *set0* functions: just free it...
771          */
772         sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
773     }
774 #endif
775
776     r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
777     r->dirty_cnt++;
778
779     return 1;
780 #ifndef FIPS_MODULE
781  err:
782     /* r, d, t should not be freed */
783     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
784     return 0;
785 #endif
786 }
787
788 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
789
790 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
791                         STACK_OF(BIGNUM_const) *exps,
792                         STACK_OF(BIGNUM_const) *coeffs)
793 {
794 #ifndef FIPS_MODULE
795     RSA_PRIME_INFO *pinfo;
796     int i, pnum;
797 #endif
798
799     if (r == NULL)
800         return 0;
801
802     /* If |p| is NULL, there are no CRT parameters */
803     if (RSA_get0_p(r) == NULL)
804         return 1;
805
806     sk_BIGNUM_const_push(primes, RSA_get0_p(r));
807     sk_BIGNUM_const_push(primes, RSA_get0_q(r));
808     sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
809     sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
810     sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
811
812 #ifndef FIPS_MODULE
813     pnum = RSA_get_multi_prime_extra_count(r);
814     for (i = 0; i < pnum; i++) {
815         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
816         sk_BIGNUM_const_push(primes, pinfo->r);
817         sk_BIGNUM_const_push(exps, pinfo->d);
818         sk_BIGNUM_const_push(coeffs, pinfo->t);
819     }
820 #endif
821
822     return 1;
823 }
824
825 #ifndef FIPS_MODULE
826 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
827 {
828     OSSL_PARAM pad_params[2], *p = pad_params;
829
830     if (ctx == NULL) {
831         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
832         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
833         return -2;
834     }
835
836     /* If key type not RSA or RSA-PSS return error */
837     if (ctx->pmeth != NULL
838             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
839             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
840         return -1;
841
842     /* TODO(3.0): Remove this eventually when no more legacy */
843     if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
844          || ctx->op.ciph.ciphprovctx == NULL)
845         && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
846             || ctx->op.sig.sigprovctx == NULL))
847         return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
848                                  pad_mode, NULL);
849
850     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
851     *p++ = OSSL_PARAM_construct_end();
852
853     return EVP_PKEY_CTX_set_params(ctx, pad_params);
854 }
855
856 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
857 {
858     OSSL_PARAM pad_params[2], *p = pad_params;
859
860     if (ctx == NULL || pad_mode == NULL) {
861         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
862         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
863         return -2;
864     }
865
866     /* If key type not RSA or RSA-PSS return error */
867     if (ctx->pmeth != NULL
868             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
869             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
870         return -1;
871
872     /* TODO(3.0): Remove this eventually when no more legacy */
873     if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
874          || ctx->op.ciph.ciphprovctx == NULL)
875         && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
876             || ctx->op.sig.sigprovctx == NULL))
877         return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
878                                  pad_mode);
879
880     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
881     *p++ = OSSL_PARAM_construct_end();
882
883     if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
884         return 0;
885
886     return 1;
887
888 }
889
890 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
891 {
892     const char *name;
893
894     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
895         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
896         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
897         return -2;
898     }
899
900     /* If key type not RSA return error */
901     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
902         return -1;
903
904     /* TODO(3.0): Remove this eventually when no more legacy */
905     if (ctx->op.ciph.ciphprovctx == NULL)
906         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
907                                  EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
908
909     name = (md == NULL) ? "" : EVP_MD_name(md);
910
911     return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
912 }
913
914 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
915                                       const char *mdprops)
916 {
917     OSSL_PARAM rsa_params[3], *p = rsa_params;
918
919     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
920         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
921         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
922         return -2;
923     }
924
925     /* If key type not RSA return error */
926     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
927         return -1;
928
929
930     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
931                                             /*
932                                              * Cast away the const. This is read
933                                              * only so should be safe
934                                              */
935                                             (char *)mdname, 0);
936     if (mdprops != NULL) {
937         *p++ = OSSL_PARAM_construct_utf8_string(
938                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
939                     /*
940                      * Cast away the const. This is read
941                      * only so should be safe
942                      */
943                     (char *)mdprops, 0);
944     }
945     *p++ = OSSL_PARAM_construct_end();
946
947     return EVP_PKEY_CTX_set_params(ctx, rsa_params);
948 }
949
950 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
951                                       size_t namelen)
952 {
953     OSSL_PARAM rsa_params[2], *p = rsa_params;
954
955     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
956         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
957         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
958         return -2;
959     }
960
961     /* If key type not RSA return error */
962     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
963         return -1;
964
965     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
966                                             name, namelen);
967     *p++ = OSSL_PARAM_construct_end();
968
969     if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
970         return -1;
971
972     return 1;
973 }
974
975 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
976 {
977     /* 80 should be big enough */
978     char name[80] = "";
979
980     if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
981         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
982         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
983         return -2;
984     }
985
986     /* If key type not RSA return error */
987     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
988         return -1;
989
990     /* TODO(3.0): Remove this eventually when no more legacy */
991     if (ctx->op.ciph.ciphprovctx == NULL)
992         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
993                                  EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
994
995     if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
996         return -1;
997
998     /* May be NULL meaning "unknown" */
999     *md = EVP_get_digestbyname(name);
1000
1001     return 1;
1002 }
1003
1004 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1005 {
1006     const char *name;
1007
1008     if (ctx == NULL
1009             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1010                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1011         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1012         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1013         return -2;
1014     }
1015
1016     /* If key type not RSA return error */
1017     if (ctx->pmeth != NULL
1018             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1019             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1020         return -1;
1021
1022     /* TODO(3.0): Remove this eventually when no more legacy */
1023     if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1024                 && ctx->op.ciph.ciphprovctx == NULL)
1025             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1026                 && ctx->op.sig.sigprovctx == NULL))
1027         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1028                                  EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1029                                  EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1030
1031     name = (md == NULL) ? "" : EVP_MD_name(md);
1032
1033     return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1034 }
1035
1036 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1037                                       const char *mdprops)
1038 {
1039     OSSL_PARAM rsa_params[3], *p = rsa_params;
1040
1041     if (ctx == NULL
1042             || mdname == NULL
1043             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1044                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1045         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1046         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1047         return -2;
1048     }
1049
1050     /* If key type not RSA return error */
1051     if (ctx->pmeth != NULL
1052             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1053             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1054         return -1;
1055
1056     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1057                                             /*
1058                                              * Cast away the const. This is
1059                                              * read only so should be safe
1060                                              */
1061                                             (char *)mdname, 0);
1062     if (mdprops != NULL) {
1063         *p++ =
1064             OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1065                                              /*
1066                                               * Cast away the const. This is
1067                                               * read only so should be safe
1068                                               */
1069                                              (char *)mdprops, 0);
1070     }
1071     *p++ = OSSL_PARAM_construct_end();
1072
1073     return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1074 }
1075
1076 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1077                                       size_t namelen)
1078 {
1079     OSSL_PARAM rsa_params[2], *p = rsa_params;
1080
1081     if (ctx == NULL
1082             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1083                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1084         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1085         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1086         return -2;
1087     }
1088
1089     /* If key type not RSA or RSA-PSS return error */
1090     if (ctx->pmeth != NULL
1091             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1092             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1093         return -1;
1094
1095     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1096                                             name, namelen);
1097     *p++ = OSSL_PARAM_construct_end();
1098
1099     if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1100         return -1;
1101
1102     return 1;
1103 }
1104
1105 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1106 {
1107     /* 80 should be big enough */
1108     char name[80] = "";
1109
1110     if (ctx == NULL
1111             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1112                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1113         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1114         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1115         return -2;
1116     }
1117
1118     /* If key type not RSA or RSA-PSS return error */
1119     if (ctx->pmeth != NULL
1120             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1121             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1122         return -1;
1123
1124     /* TODO(3.0): Remove this eventually when no more legacy */
1125     if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1126                 && ctx->op.ciph.ciphprovctx == NULL)
1127             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1128                 && ctx->op.sig.sigprovctx == NULL))
1129         return EVP_PKEY_CTX_ctrl(ctx, -1,
1130                                  EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1131                                  EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1132
1133     if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1134         return -1;
1135
1136     /* May be NULL meaning "unknown" */
1137     *md = EVP_get_digestbyname(name);
1138
1139     return 1;
1140 }
1141
1142 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1143 {
1144     OSSL_PARAM rsa_params[2], *p = rsa_params;
1145
1146     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1147         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1148         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1149         return -2;
1150     }
1151
1152     /* If key type not RSA return error */
1153     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1154         return -1;
1155
1156     /* TODO(3.0): Remove this eventually when no more legacy */
1157     if (ctx->op.ciph.ciphprovctx == NULL)
1158         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1159                                  EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1160                                  (void *)label);
1161
1162     *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1163                                              /*
1164                                               * Cast away the const. This is
1165                                               * read only so should be safe
1166                                               */
1167                                              (void *)label,
1168                                              (size_t)llen);
1169     *p++ = OSSL_PARAM_construct_end();
1170
1171     if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1172         return 0;
1173
1174     OPENSSL_free(label);
1175     return 1;
1176 }
1177
1178 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1179 {
1180     OSSL_PARAM rsa_params[3], *p = rsa_params;
1181     size_t labellen;
1182
1183     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1184         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1185         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1186         return -2;
1187     }
1188
1189     /* If key type not RSA return error */
1190     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1191         return -1;
1192
1193     /* TODO(3.0): Remove this eventually when no more legacy */
1194     if (ctx->op.ciph.ciphprovctx == NULL)
1195         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1196                                  EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1197                                  (void *)label);
1198
1199     *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1200                                           (void **)label, 0);
1201     *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1202                                        &labellen);
1203     *p++ = OSSL_PARAM_construct_end();
1204
1205     if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1206         return -1;
1207
1208     if (labellen > INT_MAX)
1209         return -1;
1210
1211     return (int)labellen;
1212 }
1213
1214 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1215 {
1216     OSSL_PARAM pad_params[2], *p = pad_params;
1217
1218     if (ctx == NULL) {
1219         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1220         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1221         return -2;
1222     }
1223
1224     /* If key type not RSA or RSA-PSS return error */
1225     if (ctx->pmeth != NULL
1226             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1227             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1228         return -1;
1229
1230     /* TODO(3.0): Remove this eventually when no more legacy */
1231     if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1232         || ctx->op.sig.sigprovctx == NULL)
1233         return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1234                                  saltlen, NULL);
1235
1236     *p++ =
1237         OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1238     *p++ = OSSL_PARAM_construct_end();
1239
1240     return EVP_PKEY_CTX_set_params(ctx, pad_params);
1241 }
1242
1243 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1244 {
1245     OSSL_PARAM pad_params[2], *p = pad_params;
1246
1247     if (ctx == NULL || saltlen == NULL) {
1248         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1249         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1250         return -2;
1251     }
1252
1253     /* If key type not RSA or RSA-PSS return error */
1254     if (ctx->pmeth != NULL
1255             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1256             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1257         return -1;
1258
1259     /* TODO(3.0): Remove this eventually when no more legacy */
1260     if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1261         || ctx->op.sig.sigprovctx == NULL)
1262         return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1263                                  EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1264                                  0, saltlen);
1265
1266     *p++ =
1267         OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1268     *p++ = OSSL_PARAM_construct_end();
1269
1270     if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1271         return 0;
1272
1273     return 1;
1274
1275 }
1276
1277 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1278 {
1279     OSSL_PARAM params[2], *p = params;
1280     size_t bits2 = bits;
1281
1282     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1283         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1284         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1285         return -2;
1286     }
1287
1288     /* If key type not RSA return error */
1289     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1290         return -1;
1291
1292     /* TODO(3.0): Remove this eventually when no more legacy */
1293     if (ctx->op.keymgmt.genctx == NULL)
1294         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1295                                  EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1296
1297     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1298     *p++ = OSSL_PARAM_construct_end();
1299
1300     if (!EVP_PKEY_CTX_set_params(ctx, params))
1301         return 0;
1302
1303     return 1;
1304 }
1305
1306 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1307 {
1308     OSSL_PARAM_BLD *tmpl;
1309     OSSL_PARAM *params;
1310     int ret;
1311
1312     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1313         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1314         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1315         return -2;
1316     }
1317
1318     /* If key type not RSA return error */
1319     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1320         return -1;
1321
1322     /* TODO(3.0): Remove this eventually when no more legacy */
1323     if (ctx->op.keymgmt.genctx == NULL)
1324         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1325                                  EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1326
1327     if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
1328         return 0;
1329     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1330         || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
1331         OSSL_PARAM_BLD_free(tmpl);
1332         return 0;
1333     }
1334     OSSL_PARAM_BLD_free(tmpl);
1335
1336     ret = EVP_PKEY_CTX_set_params(ctx, params);
1337     OSSL_PARAM_BLD_free_params(params);
1338     return ret;
1339 }
1340
1341 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1342 {
1343     OSSL_PARAM params[2], *p = params;
1344     size_t primes2 = primes;
1345
1346     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1347         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1348         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1349         return -2;
1350     }
1351
1352     /* If key type not RSA return error */
1353     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1354         return -1;
1355
1356     /* TODO(3.0): Remove this eventually when no more legacy */
1357     if (ctx->op.keymgmt.genctx == NULL)
1358         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1359                                  EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1360                                  NULL);
1361
1362     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1363     *p++ = OSSL_PARAM_construct_end();
1364
1365     if (!EVP_PKEY_CTX_set_params(ctx, params))
1366         return 0;
1367
1368     return 1;
1369 }
1370 #endif