Coverage Report

Created: 2026-02-14 07:20

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