Coverage Report

Created: 2023-09-25 06:41

/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
381
{
36
381
    return rsa_new_intern(NULL, NULL);
37
381
}
38
39
const RSA_METHOD *RSA_get_method(const RSA *rsa)
40
194
{
41
194
    return rsa->meth;
42
194
}
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
381
{
77
381
    RSA *ret = OPENSSL_zalloc(sizeof(*ret));
78
79
381
    if (ret == NULL)
80
0
        return NULL;
81
82
381
    ret->lock = CRYPTO_THREAD_lock_new();
83
381
    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
381
    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
381
    ret->libctx = libctx;
96
381
    ret->meth = RSA_get_default_method();
97
381
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
98
381
    ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
99
381
    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
381
    } else {
106
381
        ret->engine = ENGINE_get_default_RSA();
107
381
    }
108
381
    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
381
#endif
116
117
381
    ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
118
381
#ifndef FIPS_MODULE
119
381
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
120
0
        goto err;
121
0
    }
122
381
#endif
123
124
381
    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
381
    return ret;
130
131
0
 err:
132
0
    RSA_free(ret);
133
0
    return NULL;
134
381
}
135
136
void RSA_free(RSA *r)
137
1.24k
{
138
1.24k
    int i;
139
140
1.24k
    if (r == NULL)
141
672
        return;
142
143
575
    CRYPTO_DOWN_REF(&r->references, &i);
144
575
    REF_PRINT_COUNT("RSA", r);
145
575
    if (i > 0)
146
194
        return;
147
381
    REF_ASSERT_ISNT(i < 0);
148
149
381
    if (r->meth != NULL && r->meth->finish != NULL)
150
381
        r->meth->finish(r);
151
381
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
152
381
    ENGINE_finish(r->engine);
153
381
#endif
154
155
381
#ifndef FIPS_MODULE
156
381
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
157
381
#endif
158
159
381
    CRYPTO_THREAD_lock_free(r->lock);
160
381
    CRYPTO_FREE_REF(&r->references);
161
162
381
    BN_free(r->n);
163
381
    BN_free(r->e);
164
381
    BN_clear_free(r->d);
165
381
    BN_clear_free(r->p);
166
381
    BN_clear_free(r->q);
167
381
    BN_clear_free(r->dmp1);
168
381
    BN_clear_free(r->dmq1);
169
381
    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
381
#ifndef FIPS_MODULE
176
381
    RSA_PSS_PARAMS_free(r->pss);
177
381
    sk_RSA_PRIME_INFO_pop_free(r->prime_infos, ossl_rsa_multip_info_free);
178
381
#endif
179
381
    BN_BLINDING_free(r->blinding);
180
381
    BN_BLINDING_free(r->mt_blinding);
181
381
    OPENSSL_free(r);
182
381
}
183
184
int RSA_up_ref(RSA *r)
185
194
{
186
194
    int i;
187
188
194
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
189
0
        return 0;
190
191
194
    REF_PRINT_COUNT("RSA", r);
192
194
    REF_ASSERT_ISNT(i < 2);
193
194
    return i > 1 ? 1 : 0;
194
194
}
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
194
{
203
194
    r->libctx = libctx;
204
194
}
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
2.22k
{
240
2.22k
    return a * b / scale;
241
2.22k
}
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
106
{
252
106
    uint64_t r = 0;
253
106
    uint64_t b;
254
106
    int s;
255
256
2.43k
    for (s = 63; s >= 0; s -= 3) {
257
2.33k
        r <<= 1;
258
2.33k
        b = 3 * r * (r + 1) + 1;
259
2.33k
        if ((x >> s) >= b) {
260
572
            x -= b << s;
261
572
            r++;
262
572
        }
263
2.33k
    }
264
106
    return r * cbrt_scale;
265
106
}
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
106
{
276
106
    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
1.02k
    while (v >= 2 * scale) {
286
920
        v >>= 1;
287
920
        r += scale;
288
920
    }
289
2.01k
    for (i = scale / 2; i != 0; i /= 2) {
290
1.90k
        v = mul2(v, v);
291
1.90k
        if (v >= 2 * scale) {
292
1.00k
            v >>= 1;
293
1.00k
            r += i;
294
1.00k
        }
295
1.90k
    }
296
106
    r = (r * (uint64_t)scale) / log_e;
297
106
    return r;
298
106
}
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
194
{
319
194
    uint64_t x;
320
194
    uint32_t lx;
321
194
    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
194
    switch (n) {
329
87
    case 2048:      /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
330
87
        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
0
    case 4096:      /* SP 800-56B rev 2 Appendix D */
334
0
        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
194
    }
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
107
    if (n >= 687737)
352
0
        return 1200;
353
107
    if (n < 8)
354
1
        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
106
    if (n <= 7680)
362
106
        cap = 192;
363
0
    else if (n <= 15360)
364
0
        cap = 256;
365
0
    else
366
0
        cap = 1200;
367
368
106
    x = n * (uint64_t)log_2;
369
106
    lx = ilog_e(x);
370
106
    y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
371
106
                   / log_2);
372
106
    y = (y + 4) & ~7;
373
106
    if (y > cap)
374
0
        y = cap;
375
106
    return y;
376
107
}
377
378
379
380
int RSA_security_bits(const RSA *rsa)
381
194
{
382
194
    int bits = BN_num_bits(rsa->n);
383
384
194
#ifndef FIPS_MODULE
385
194
    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
194
#endif
393
194
    return ossl_ifc_ffc_compute_security_bits(bits);
394
194
}
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
194
{
552
194
    if (n != NULL)
553
194
        *n = r->n;
554
194
    if (e != NULL)
555
194
        *e = r->e;
556
194
    if (d != NULL)
557
194
        *d = r->d;
558
194
}
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
194
{
642
194
    return r->n;
643
194
}
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
194
{
657
194
    return r->p;
658
194
}
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
194
{
704
194
    return &r->pss_params;
705
194
}
706
707
void RSA_clear_flags(RSA *r, int flags)
708
194
{
709
194
    r->flags &= ~flags;
710
194
}
711
712
int RSA_test_flags(const RSA *r, int flags)
713
582
{
714
582
    return r->flags & flags;
715
582
}
716
717
void RSA_set_flags(RSA *r, int flags)
718
194
{
719
194
    r->flags |= flags;
720
194
}
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
        || pnum != sk_BIGNUM_num(exps)
762
0
        || pnum != sk_BIGNUM_num(coeffs) + 1)
763
0
        return 0;
764
765
0
    if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
766
0
                          sk_BIGNUM_value(primes, 1))
767
0
        || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
768
0
                                sk_BIGNUM_value(exps, 1),
769
0
                                sk_BIGNUM_value(coeffs, 0)))
770
0
        return 0;
771
772
0
#ifndef FIPS_MODULE
773
0
    old_infos = r->prime_infos;
774
0
#endif
775
776
0
    if (pnum > 2) {
777
0
#ifndef FIPS_MODULE
778
0
        int i;
779
780
0
        prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
781
0
        if (prime_infos == NULL)
782
0
            return 0;
783
784
0
        for (i = 2; i < pnum; i++) {
785
0
            BIGNUM *prime = sk_BIGNUM_value(primes, i);
786
0
            BIGNUM *exp = sk_BIGNUM_value(exps, i);
787
0
            BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
788
0
            RSA_PRIME_INFO *pinfo = NULL;
789
790
0
            if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
791
0
                goto err;
792
793
            /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */
794
0
            if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL)
795
0
                goto err;
796
797
0
            pinfo->r = prime;
798
0
            pinfo->d = exp;
799
0
            pinfo->t = coeff;
800
0
            BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
801
0
            BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
802
0
            BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
803
0
            (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
804
0
        }
805
806
0
        r->prime_infos = prime_infos;
807
808
0
        if (!ossl_rsa_multip_calc_product(r)) {
809
0
            r->prime_infos = old_infos;
810
0
            goto err;
811
0
        }
812
#else
813
        return 0;
814
#endif
815
0
    }
816
817
0
#ifndef FIPS_MODULE
818
0
    if (old_infos != NULL) {
819
        /*
820
         * This is hard to deal with, since the old infos could
821
         * also be set by this function and r, d, t should not
822
         * be freed in that case. So currently, stay consistent
823
         * with other *set0* functions: just free it...
824
         */
825
0
        sk_RSA_PRIME_INFO_pop_free(old_infos, ossl_rsa_multip_info_free);
826
0
    }
827
0
#endif
828
829
0
    r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
830
0
    r->dirty_cnt++;
831
832
0
    return 1;
833
0
#ifndef FIPS_MODULE
834
0
 err:
835
    /* r, d, t should not be freed */
836
0
    sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex);
837
0
    return 0;
838
0
#endif
839
0
}
840
841
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
842
843
int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
844
                             STACK_OF(BIGNUM_const) *exps,
845
                             STACK_OF(BIGNUM_const) *coeffs)
846
194
{
847
194
#ifndef FIPS_MODULE
848
194
    RSA_PRIME_INFO *pinfo;
849
194
    int i, pnum;
850
194
#endif
851
852
194
    if (r == NULL)
853
0
        return 0;
854
855
    /* If |p| is NULL, there are no CRT parameters */
856
194
    if (RSA_get0_p(r) == NULL)
857
194
        return 1;
858
859
0
    sk_BIGNUM_const_push(primes, RSA_get0_p(r));
860
0
    sk_BIGNUM_const_push(primes, RSA_get0_q(r));
861
0
    sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
862
0
    sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
863
0
    sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
864
865
0
#ifndef FIPS_MODULE
866
0
    pnum = RSA_get_multi_prime_extra_count(r);
867
0
    for (i = 0; i < pnum; i++) {
868
0
        pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
869
0
        sk_BIGNUM_const_push(primes, pinfo->r);
870
0
        sk_BIGNUM_const_push(exps, pinfo->d);
871
0
        sk_BIGNUM_const_push(coeffs, pinfo->t);
872
0
    }
873
0
#endif
874
875
0
    return 1;
876
194
}
877
878
#ifndef FIPS_MODULE
879
/* Helpers to set or get diverse hash algorithm names */
880
static int int_set_rsa_md_name(EVP_PKEY_CTX *ctx,
881
                               /* For checks */
882
                               int keytype, int optype,
883
                               /* For EVP_PKEY_CTX_set_params() */
884
                               const char *mdkey, const char *mdname,
885
                               const char *propkey, const char *mdprops)
886
0
{
887
0
    OSSL_PARAM params[3], *p = params;
888
889
0
    if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
890
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
891
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
892
0
        return -2;
893
0
    }
894
895
    /* If key type not RSA return error */
896
0
    switch (keytype) {
897
0
    case -1:
898
0
        if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
899
0
            && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
900
0
            return -1;
901
0
        break;
902
0
    default:
903
0
        if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
904
0
            return -1;
905
0
        break;
906
0
    }
907
908
    /* Cast away the const. This is read only so should be safe */
909
0
    *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, 0);
910
0
    if (evp_pkey_ctx_is_provided(ctx) && mdprops != NULL) {
911
        /* Cast away the const. This is read only so should be safe */
912
0
        *p++ = OSSL_PARAM_construct_utf8_string(propkey, (char *)mdprops, 0);
913
0
    }
914
0
    *p++ = OSSL_PARAM_construct_end();
915
916
0
    return evp_pkey_ctx_set_params_strict(ctx, params);
917
0
}
918
919
/* Helpers to set or get diverse hash algorithm names */
920
static int int_get_rsa_md_name(EVP_PKEY_CTX *ctx,
921
                               /* For checks */
922
                               int keytype, int optype,
923
                               /* For EVP_PKEY_CTX_get_params() */
924
                               const char *mdkey,
925
                               char *mdname, size_t mdnamesize)
926
0
{
927
0
    OSSL_PARAM params[2], *p = params;
928
929
0
    if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
930
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
931
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
932
0
        return -2;
933
0
    }
934
935
    /* If key type not RSA return error */
936
0
    switch (keytype) {
937
0
    case -1:
938
0
        if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
939
0
            && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
940
0
            return -1;
941
0
        break;
942
0
    default:
943
0
        if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
944
0
            return -1;
945
0
        break;
946
0
    }
947
948
    /* Cast away the const. This is read only so should be safe */
949
0
    *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, mdnamesize);
950
0
    *p++ = OSSL_PARAM_construct_end();
951
952
0
    return evp_pkey_ctx_get_params_strict(ctx, params);
953
0
}
954
955
/*
956
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
957
 * simply because that's easier.
958
 */
959
int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
960
0
{
961
0
    return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING,
962
0
                             pad_mode, NULL);
963
0
}
964
965
/*
966
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
967
 * simply because that's easier.
968
 */
969
int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
970
0
{
971
0
    return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
972
0
                             0, pad_mode);
973
0
}
974
975
/*
976
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
977
 * simply because that's easier.
978
 */
979
int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
980
0
{
981
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
982
0
                             EVP_PKEY_CTRL_MD, 0, (void *)(md));
983
0
}
984
985
int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX *ctx,
986
                                            const char *mdname,
987
                                            const char *mdprops)
988
0
{
989
0
    return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
990
0
                               OSSL_PKEY_PARAM_RSA_DIGEST, mdname,
991
0
                               OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, mdprops);
992
0
}
993
994
/*
995
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
996
 * simply because that's easier.
997
 */
998
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
999
0
{
1000
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1001
0
                             EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md));
1002
0
}
1003
1004
int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1005
                                      const char *mdprops)
1006
0
{
1007
0
    return
1008
0
        int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1009
0
                            OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname,
1010
0
                            OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops);
1011
0
}
1012
1013
int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
1014
                                      size_t namesize)
1015
0
{
1016
0
    return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1017
0
                               OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
1018
0
                               name, namesize);
1019
0
}
1020
1021
/*
1022
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1023
 * simply because that's easier.
1024
 */
1025
int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1026
0
{
1027
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1028
0
                             EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
1029
0
}
1030
1031
/*
1032
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1033
 * simply because that's easier.
1034
 */
1035
int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1036
0
{
1037
0
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1038
0
                             EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
1039
0
}
1040
1041
int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1042
                                      const char *mdprops)
1043
0
{
1044
0
    return int_set_rsa_md_name(ctx, -1,
1045
0
                               EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1046
0
                               OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
1047
0
                               OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops);
1048
0
}
1049
1050
int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1051
                                      size_t namesize)
1052
0
{
1053
0
    return int_get_rsa_md_name(ctx, -1,
1054
0
                               EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1055
0
                               OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize);
1056
0
}
1057
1058
/*
1059
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1060
 * simply because that's easier.
1061
 */
1062
int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1063
0
{
1064
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1065
0
                             EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
1066
0
}
1067
1068
int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
1069
                                                 const char *mdname)
1070
0
{
1071
0
    return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1072
0
                               OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
1073
0
                               NULL, NULL);
1074
0
}
1075
1076
/*
1077
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1078
 * simply because that's easier.
1079
 */
1080
int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1081
0
{
1082
0
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1083
0
                             EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md));
1084
0
}
1085
1086
int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1087
0
{
1088
0
    OSSL_PARAM rsa_params[2], *p = rsa_params;
1089
0
    int ret;
1090
1091
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1092
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1093
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1094
0
        return -2;
1095
0
    }
1096
1097
    /* If key type not RSA return error */
1098
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1099
0
        return -1;
1100
1101
    /* Cast away the const. This is read only so should be safe */
1102
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1103
0
                                             (void *)label, (size_t)llen);
1104
0
    *p++ = OSSL_PARAM_construct_end();
1105
1106
0
    ret = evp_pkey_ctx_set_params_strict(ctx, rsa_params);
1107
0
    if (ret <= 0)
1108
0
        return ret;
1109
1110
    /* Ownership is supposed to be transferred to the callee. */
1111
0
    OPENSSL_free(label);
1112
0
    return 1;
1113
0
}
1114
1115
int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1116
0
{
1117
0
    OSSL_PARAM rsa_params[2], *p = rsa_params;
1118
0
    size_t labellen;
1119
1120
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1121
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1122
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1123
0
        return -2;
1124
0
    }
1125
1126
    /* If key type not RSA return error */
1127
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1128
0
        return -1;
1129
1130
0
    *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1131
0
                                          (void **)label, 0);
1132
0
    *p++ = OSSL_PARAM_construct_end();
1133
1134
0
    if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1135
0
        return -1;
1136
1137
0
    labellen = rsa_params[0].return_size;
1138
0
    if (labellen > INT_MAX)
1139
0
        return -1;
1140
1141
0
    return (int)labellen;
1142
0
}
1143
1144
/*
1145
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1146
 * simply because that's easier.
1147
 */
1148
int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1149
0
{
1150
    /*
1151
     * For some reason, the optype was set to this:
1152
     *
1153
     * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1154
     *
1155
     * However, we do use RSA-PSS with the whole gamut of diverse signature
1156
     * and verification operations, so the optype gets upgraded to this:
1157
     *
1158
     * EVP_PKEY_OP_TYPE_SIG
1159
     */
1160
0
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
1161
0
                             EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL);
1162
0
}
1163
1164
/*
1165
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1166
 * simply because that's easier.
1167
 */
1168
int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1169
0
{
1170
    /*
1171
     * Because of circumstances, the optype is updated from:
1172
     *
1173
     * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1174
     *
1175
     * to:
1176
     *
1177
     * EVP_PKEY_OP_TYPE_SIG
1178
     */
1179
0
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
1180
0
                             EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen);
1181
0
}
1182
1183
int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1184
0
{
1185
0
    OSSL_PARAM pad_params[2], *p = pad_params;
1186
1187
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1188
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1189
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1190
0
        return -2;
1191
0
    }
1192
1193
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1194
0
        return -1;
1195
1196
0
    *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
1197
0
                                    &saltlen);
1198
0
    *p++ = OSSL_PARAM_construct_end();
1199
1200
0
    return evp_pkey_ctx_set_params_strict(ctx, pad_params);
1201
0
}
1202
1203
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1204
0
{
1205
0
    OSSL_PARAM params[2], *p = params;
1206
0
    size_t bits2 = bits;
1207
1208
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1209
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1210
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1211
0
        return -2;
1212
0
    }
1213
1214
    /* If key type not RSA return error */
1215
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
1216
0
        && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1217
0
        return -1;
1218
1219
0
    *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1220
0
    *p++ = OSSL_PARAM_construct_end();
1221
1222
0
    return evp_pkey_ctx_set_params_strict(ctx, params);
1223
0
}
1224
1225
int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1226
0
{
1227
0
    int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN,
1228
0
                                EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1229
1230
    /*
1231
     * Satisfy memory semantics for pre-3.0 callers of
1232
     * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1233
     * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1234
     */
1235
0
    if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) {
1236
0
        BN_free(ctx->rsa_pubexp);
1237
0
        ctx->rsa_pubexp = pubexp;
1238
0
    }
1239
1240
0
    return ret;
1241
0
}
1242
1243
int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1244
0
{
1245
0
    int ret = 0;
1246
1247
    /*
1248
     * When we're dealing with a provider, there's no need to duplicate
1249
     * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
1250
     */
1251
0
    if (evp_pkey_ctx_is_legacy(ctx)) {
1252
0
        pubexp = BN_dup(pubexp);
1253
0
        if (pubexp == NULL)
1254
0
            return 0;
1255
0
    }
1256
0
    ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1257
0
                            EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1258
0
    if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0)
1259
0
        BN_free(pubexp);
1260
0
    return ret;
1261
0
}
1262
1263
int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1264
0
{
1265
0
    OSSL_PARAM params[2], *p = params;
1266
0
    size_t primes2 = primes;
1267
1268
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1269
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1270
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1271
0
        return -2;
1272
0
    }
1273
1274
    /* If key type not RSA return error */
1275
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
1276
0
        && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1277
0
        return -1;
1278
1279
0
    *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1280
0
    *p++ = OSSL_PARAM_construct_end();
1281
1282
0
    return evp_pkey_ctx_set_params_strict(ctx, params);
1283
0
}
1284
#endif