Coverage Report

Created: 2025-12-31 06:58

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