Coverage Report

Created: 2024-05-21 06:52

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