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