Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/crypto/rsa/rsa_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2024 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
482k
{
36
482k
    return rsa_new_intern(NULL, NULL);
37
482k
}
38
39
const RSA_METHOD *RSA_get_method(const RSA *rsa)
40
185k
{
41
185k
    return rsa->meth;
42
185k
}
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
61.7k
{
72
61.7k
    return rsa_new_intern(NULL, libctx);
73
61.7k
}
74
75
static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
76
340k
{
77
340k
    RSA *ret = OPENSSL_zalloc(sizeof(*ret));
78
79
340k
    if (ret == NULL)
80
0
        return NULL;
81
82
340k
    ret->lock = CRYPTO_THREAD_lock_new();
83
340k
    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
340k
    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
340k
    ret->libctx = libctx;
96
340k
    ret->meth = RSA_get_default_method();
97
340k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
98
340k
    ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
99
340k
    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
340k
    } else {
106
340k
        ret->engine = ENGINE_get_default_RSA();
107
340k
    }
108
340k
    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
340k
#endif
116
117
340k
    ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
118
340k
#ifndef FIPS_MODULE
119
340k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
120
0
        goto err;
121
0
    }
122
340k
#endif
123
124
340k
    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
340k
    return ret;
130
131
0
 err:
132
0
    RSA_free(ret);
133
0
    return NULL;
134
340k
}
135
136
void RSA_free(RSA *r)
137
1.55M
{
138
1.55M
    int i;
139
140
1.55M
    if (r == NULL)
141
759k
        return;
142
143
794k
    CRYPTO_DOWN_REF(&r->references, &i);
144
794k
    REF_PRINT_COUNT("RSA", i, r);
145
794k
    if (i > 0)
146
250k
        return;
147
544k
    REF_ASSERT_ISNT(i < 0);
148
149
544k
    if (r->meth != NULL && r->meth->finish != NULL)
150
544k
        r->meth->finish(r);
151
544k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
152
544k
    ENGINE_finish(r->engine);
153
544k
#endif
154
155
544k
#ifndef FIPS_MODULE
156
544k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
157
544k
#endif
158
159
544k
    CRYPTO_THREAD_lock_free(r->lock);
160
544k
    CRYPTO_FREE_REF(&r->references);
161
162
544k
    BN_free(r->n);
163
544k
    BN_free(r->e);
164
544k
    BN_clear_free(r->d);
165
544k
    BN_clear_free(r->p);
166
544k
    BN_clear_free(r->q);
167
544k
    BN_clear_free(r->dmp1);
168
544k
    BN_clear_free(r->dmq1);
169
544k
    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
544k
#ifndef FIPS_MODULE
176
544k
    RSA_PSS_PARAMS_free(r->pss);
177
544k
    sk_RSA_PRIME_INFO_pop_free(r->prime_infos, ossl_rsa_multip_info_free);
178
544k
#endif
179
544k
    BN_BLINDING_free(r->blinding);
180
544k
    BN_BLINDING_free(r->mt_blinding);
181
544k
    OPENSSL_free(r);
182
544k
}
183
184
int RSA_up_ref(RSA *r)
185
250k
{
186
250k
    int i;
187
188
250k
    if (CRYPTO_UP_REF(&r->references, &i) <= 0)
189
0
        return 0;
190
191
250k
    REF_PRINT_COUNT("RSA", i, r);
192
250k
    REF_ASSERT_ISNT(i < 2);
193
250k
    return i > 1 ? 1 : 0;
194
250k
}
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
142k
{
203
142k
    r->libctx = libctx;
204
142k
}
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
1.17M
{
240
1.17M
    return a * b / scale;
241
1.17M
}
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
55.8k
{
252
55.8k
    uint64_t r = 0;
253
55.8k
    uint64_t b;
254
55.8k
    int s;
255
256
1.28M
    for (s = 63; s >= 0; s -= 3) {
257
1.22M
        r <<= 1;
258
1.22M
        b = 3 * r * (r + 1) + 1;
259
1.22M
        if ((x >> s) >= b) {
260
314k
            x -= b << s;
261
314k
            r++;
262
314k
        }
263
1.22M
    }
264
55.8k
    return r * cbrt_scale;
265
55.8k
}
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
55.8k
{
276
55.8k
    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
505k
    while (v >= 2 * scale) {
286
450k
        v >>= 1;
287
450k
        r += scale;
288
450k
    }
289
1.06M
    for (i = scale / 2; i != 0; i /= 2) {
290
1.00M
        v = mul2(v, v);
291
1.00M
        if (v >= 2 * scale) {
292
505k
            v >>= 1;
293
505k
            r += i;
294
505k
        }
295
1.00M
    }
296
55.8k
    r = (r * (uint64_t)scale) / log_e;
297
55.8k
    return r;
298
55.8k
}
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
145k
{
319
145k
    uint64_t x;
320
145k
    uint32_t lx;
321
145k
    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
145k
    switch (n) {
329
80.7k
    case 2048:      /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
330
80.7k
        return 112;
331
321
    case 3072:      /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
332
321
        return 128;
333
391
    case 4096:      /* SP 800-56B rev 2 Appendix D */
334
391
        return 152;
335
127
    case 6144:      /* SP 800-56B rev 2 Appendix D */
336
127
        return 176;
337
22
    case 7680:      /* FIPS 140-2 IG 7.5 */
338
22
        return 192;
339
465
    case 8192:      /* SP 800-56B rev 2 Appendix D */
340
465
        return 200;
341
24
    case 15360:     /* FIPS 140-2 IG 7.5 */
342
24
        return 256;
343
145k
    }
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
63.8k
    if (n >= 687737)
352
24
        return 1200;
353
63.7k
    if (n < 8)
354
7.94k
        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
55.8k
    if (n <= 7680)
362
54.4k
        cap = 192;
363
1.42k
    else if (n <= 15360)
364
713
        cap = 256;
365
710
    else
366
710
        cap = 1200;
367
368
55.8k
    x = n * (uint64_t)log_2;
369
55.8k
    lx = ilog_e(x);
370
55.8k
    y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
371
55.8k
                   / log_2);
372
55.8k
    y = (y + 4) & ~7;
373
55.8k
    if (y > cap)
374
113
        y = cap;
375
55.8k
    return y;
376
63.7k
}
377
378
379
380
int RSA_security_bits(const RSA *rsa)
381
145k
{
382
145k
    int bits = BN_num_bits(rsa->n);
383
384
145k
#ifndef FIPS_MODULE
385
145k
    if (rsa->version == RSA_ASN1_VERSION_MULTI) {
386
        /* This ought to mean that we have private key at hand. */
387
1.54k
        int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
388
389
1.54k
        if (ex_primes <= 0 || (ex_primes + 2) > ossl_rsa_multip_cap(bits))
390
1.18k
            return 0;
391
1.54k
    }
392
144k
#endif
393
144k
    return ossl_ifc_ffc_compute_security_bits(bits);
394
145k
}
395
396
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
397
60.6k
{
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
60.6k
    if ((r->n == NULL && n == NULL)
403
60.6k
        || (r->e == NULL && e == NULL))
404
0
        return 0;
405
406
60.6k
    if (n != NULL) {
407
60.6k
        BN_free(r->n);
408
60.6k
        r->n = n;
409
60.6k
    }
410
60.6k
    if (e != NULL) {
411
60.6k
        BN_free(r->e);
412
60.6k
        r->e = e;
413
60.6k
    }
414
60.6k
    if (d != NULL) {
415
59.1k
        BN_clear_free(r->d);
416
59.1k
        r->d = d;
417
59.1k
        BN_set_flags(r->d, BN_FLG_CONSTTIME);
418
59.1k
    }
419
60.6k
    r->dirty_cnt++;
420
421
60.6k
    return 1;
422
60.6k
}
423
424
int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
425
59.1k
{
426
    /* If the fields p and q in r are NULL, the corresponding input
427
     * parameters MUST be non-NULL.
428
     */
429
59.1k
    if ((r->p == NULL && p == NULL)
430
59.1k
        || (r->q == NULL && q == NULL))
431
0
        return 0;
432
433
59.1k
    if (p != NULL) {
434
59.1k
        BN_clear_free(r->p);
435
59.1k
        r->p = p;
436
59.1k
        BN_set_flags(r->p, BN_FLG_CONSTTIME);
437
59.1k
    }
438
59.1k
    if (q != NULL) {
439
59.1k
        BN_clear_free(r->q);
440
59.1k
        r->q = q;
441
59.1k
        BN_set_flags(r->q, BN_FLG_CONSTTIME);
442
59.1k
    }
443
59.1k
    r->dirty_cnt++;
444
445
59.1k
    return 1;
446
59.1k
}
447
448
int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
449
59.1k
{
450
    /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
451
     * parameters MUST be non-NULL.
452
     */
453
59.1k
    if ((r->dmp1 == NULL && dmp1 == NULL)
454
59.1k
        || (r->dmq1 == NULL && dmq1 == NULL)
455
59.1k
        || (r->iqmp == NULL && iqmp == NULL))
456
0
        return 0;
457
458
59.1k
    if (dmp1 != NULL) {
459
59.1k
        BN_clear_free(r->dmp1);
460
59.1k
        r->dmp1 = dmp1;
461
59.1k
        BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
462
59.1k
    }
463
59.1k
    if (dmq1 != NULL) {
464
59.1k
        BN_clear_free(r->dmq1);
465
59.1k
        r->dmq1 = dmq1;
466
59.1k
        BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
467
59.1k
    }
468
59.1k
    if (iqmp != NULL) {
469
59.1k
        BN_clear_free(r->iqmp);
470
59.1k
        r->iqmp = iqmp;
471
59.1k
        BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
472
59.1k
    }
473
59.1k
    r->dirty_cnt++;
474
475
59.1k
    return 1;
476
59.1k
}
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
221k
{
552
221k
    if (n != NULL)
553
221k
        *n = r->n;
554
221k
    if (e != NULL)
555
221k
        *e = r->e;
556
221k
    if (d != NULL)
557
221k
        *d = r->d;
558
221k
}
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
136k
{
571
136k
    int pnum;
572
573
136k
    pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
574
136k
    if (pnum <= 0)
575
129k
        pnum = 0;
576
136k
    return pnum;
577
136k
}
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
568k
{
642
568k
    return r->n;
643
568k
}
644
645
const BIGNUM *RSA_get0_e(const RSA *r)
646
276k
{
647
276k
    return r->e;
648
276k
}
649
650
const BIGNUM *RSA_get0_d(const RSA *r)
651
219k
{
652
219k
    return r->d;
653
219k
}
654
655
const BIGNUM *RSA_get0_p(const RSA *r)
656
467k
{
657
467k
    return r->p;
658
467k
}
659
660
const BIGNUM *RSA_get0_q(const RSA *r)
661
136k
{
662
136k
    return r->q;
663
136k
}
664
665
const BIGNUM *RSA_get0_dmp1(const RSA *r)
666
136k
{
667
136k
    return r->dmp1;
668
136k
}
669
670
const BIGNUM *RSA_get0_dmq1(const RSA *r)
671
136k
{
672
136k
    return r->dmq1;
673
136k
}
674
675
const BIGNUM *RSA_get0_iqmp(const RSA *r)
676
136k
{
677
136k
    return r->iqmp;
678
136k
}
679
680
const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
681
14.5k
{
682
#ifdef FIPS_MODULE
683
    return NULL;
684
#else
685
14.5k
    return r->pss;
686
14.5k
#endif
687
14.5k
}
688
689
/* Internal */
690
int ossl_rsa_set0_pss_params(RSA *r, RSA_PSS_PARAMS *pss)
691
14.5k
{
692
#ifdef FIPS_MODULE
693
    return 0;
694
#else
695
14.5k
    RSA_PSS_PARAMS_free(r->pss);
696
14.5k
    r->pss = pss;
697
14.5k
    return 1;
698
14.5k
#endif
699
14.5k
}
700
701
/* Internal */
702
RSA_PSS_PARAMS_30 *ossl_rsa_get0_pss_params_30(RSA *r)
703
193k
{
704
193k
    return &r->pss_params;
705
193k
}
706
707
void RSA_clear_flags(RSA *r, int flags)
708
195k
{
709
195k
    r->flags &= ~flags;
710
195k
}
711
712
int RSA_test_flags(const RSA *r, int flags)
713
587k
{
714
587k
    return r->flags & flags;
715
587k
}
716
717
void RSA_set_flags(RSA *r, int flags)
718
195k
{
719
195k
    r->flags |= flags;
720
195k
}
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
59.7k
{
736
    /* If key type not RSA or RSA-PSS return error */
737
59.7k
    if (ctx != NULL && ctx->pmeth != NULL
738
59.7k
        && ctx->pmeth->pkey_id != EVP_PKEY_RSA
739
59.7k
        && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
740
0
        return -1;
741
59.7k
     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
742
59.7k
}
743
#endif
744
745
DEFINE_STACK_OF(BIGNUM)
746
747
/*
748
 * Note: This function deletes values from the parameter
749
 * stack values as they are consumed and set in the RSA key.
750
 */
751
int ossl_rsa_set0_all_params(RSA *r, STACK_OF(BIGNUM) *primes,
752
                             STACK_OF(BIGNUM) *exps,
753
                             STACK_OF(BIGNUM) *coeffs)
754
58.2k
{
755
58.2k
#ifndef FIPS_MODULE
756
58.2k
    STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
757
58.2k
#endif
758
58.2k
    int pnum;
759
760
58.2k
    if (primes == NULL || exps == NULL || coeffs == NULL)
761
0
        return 0;
762
763
58.2k
    pnum = sk_BIGNUM_num(primes);
764
765
    /* we need at least 2 primes */
766
58.2k
    if (pnum < 2)
767
0
        return 0;
768
769
58.2k
    if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
770
58.2k
                          sk_BIGNUM_value(primes, 1)))
771
0
        return 0;
772
773
    /*
774
     * if we managed to set everything above, remove those elements from the
775
     * stack
776
     * Note, we do this after the above all to ensure that we have taken
777
     * ownership of all the elements in the RSA key to avoid memory leaks
778
     * we also use delete 0 here as we are grabbing items from the end of the
779
     * stack rather than the start, otherwise we could use pop
780
     */
781
58.2k
    sk_BIGNUM_delete(primes, 0);
782
58.2k
    sk_BIGNUM_delete(primes, 0);
783
784
58.2k
    if (pnum == sk_BIGNUM_num(exps)
785
58.2k
        && pnum == sk_BIGNUM_num(coeffs) + 1) {
786
787
58.2k
        if (!RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
788
58.2k
                                 sk_BIGNUM_value(exps, 1),
789
58.2k
                                 sk_BIGNUM_value(coeffs, 0)))
790
0
        return 0;
791
792
        /* as above, once we consume the above params, delete them from the list */
793
58.2k
        sk_BIGNUM_delete(exps, 0);
794
58.2k
        sk_BIGNUM_delete(exps, 0);
795
58.2k
        sk_BIGNUM_delete(coeffs, 0);
796
58.2k
    }
797
798
58.2k
#ifndef FIPS_MODULE
799
58.2k
    old_infos = r->prime_infos;
800
58.2k
#endif
801
802
58.2k
    if (pnum > 2) {
803
0
#ifndef FIPS_MODULE
804
0
        int i;
805
806
0
        prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
807
0
        if (prime_infos == NULL)
808
0
            return 0;
809
810
0
        for (i = 2; i < pnum; i++) {
811
0
            BIGNUM *prime = sk_BIGNUM_pop(primes);
812
0
            BIGNUM *exp = sk_BIGNUM_pop(exps);
813
0
            BIGNUM *coeff = sk_BIGNUM_pop(coeffs);
814
0
            RSA_PRIME_INFO *pinfo = NULL;
815
816
0
            if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
817
0
                goto err;
818
819
            /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */
820
0
            if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL)
821
0
                goto err;
822
823
0
            pinfo->r = prime;
824
0
            pinfo->d = exp;
825
0
            pinfo->t = coeff;
826
0
            BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
827
0
            BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
828
0
            BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
829
0
            (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
830
0
        }
831
832
0
        r->prime_infos = prime_infos;
833
834
0
        if (!ossl_rsa_multip_calc_product(r)) {
835
0
            r->prime_infos = old_infos;
836
0
            goto err;
837
0
        }
838
#else
839
        return 0;
840
#endif
841
0
    }
842
843
58.2k
#ifndef FIPS_MODULE
844
58.2k
    if (old_infos != NULL) {
845
        /*
846
         * This is hard to deal with, since the old infos could
847
         * also be set by this function and r, d, t should not
848
         * be freed in that case. So currently, stay consistent
849
         * with other *set0* functions: just free it...
850
         */
851
0
        sk_RSA_PRIME_INFO_pop_free(old_infos, ossl_rsa_multip_info_free);
852
0
    }
853
58.2k
#endif
854
855
58.2k
    r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
856
58.2k
    r->dirty_cnt++;
857
858
58.2k
    return 1;
859
0
#ifndef FIPS_MODULE
860
0
 err:
861
    /* r, d, t should not be freed */
862
0
    sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex);
863
0
    return 0;
864
58.2k
#endif
865
58.2k
}
866
867
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
868
869
int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
870
                             STACK_OF(BIGNUM_const) *exps,
871
                             STACK_OF(BIGNUM_const) *coeffs)
872
330k
{
873
330k
#ifndef FIPS_MODULE
874
330k
    RSA_PRIME_INFO *pinfo;
875
330k
    int i, pnum;
876
330k
#endif
877
878
330k
    if (r == NULL)
879
0
        return 0;
880
881
    /* If |p| is NULL, there are no CRT parameters */
882
330k
    if (RSA_get0_p(r) == NULL)
883
194k
        return 1;
884
885
136k
    sk_BIGNUM_const_push(primes, RSA_get0_p(r));
886
136k
    sk_BIGNUM_const_push(primes, RSA_get0_q(r));
887
136k
    sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
888
136k
    sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
889
136k
    sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
890
891
136k
#ifndef FIPS_MODULE
892
136k
    pnum = RSA_get_multi_prime_extra_count(r);
893
401k
    for (i = 0; i < pnum; i++) {
894
264k
        pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
895
264k
        sk_BIGNUM_const_push(primes, pinfo->r);
896
264k
        sk_BIGNUM_const_push(exps, pinfo->d);
897
264k
        sk_BIGNUM_const_push(coeffs, pinfo->t);
898
264k
    }
899
136k
#endif
900
901
136k
    return 1;
902
330k
}
903
904
#ifndef FIPS_MODULE
905
/* Helpers to set or get diverse hash algorithm names */
906
static int int_set_rsa_md_name(EVP_PKEY_CTX *ctx,
907
                               /* For checks */
908
                               int keytype, int optype,
909
                               /* For EVP_PKEY_CTX_set_params() */
910
                               const char *mdkey, const char *mdname,
911
                               const char *propkey, const char *mdprops)
912
0
{
913
0
    OSSL_PARAM params[3], *p = params;
914
915
0
    if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
916
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
917
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
918
0
        return -2;
919
0
    }
920
921
    /* If key type not RSA return error */
922
0
    switch (keytype) {
923
0
    case -1:
924
0
        if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
925
0
            && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
926
0
            return -1;
927
0
        break;
928
0
    default:
929
0
        if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
930
0
            return -1;
931
0
        break;
932
0
    }
933
934
    /* Cast away the const. This is read only so should be safe */
935
0
    *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, 0);
936
0
    if (evp_pkey_ctx_is_provided(ctx) && mdprops != NULL) {
937
        /* Cast away the const. This is read only so should be safe */
938
0
        *p++ = OSSL_PARAM_construct_utf8_string(propkey, (char *)mdprops, 0);
939
0
    }
940
0
    *p++ = OSSL_PARAM_construct_end();
941
942
0
    return evp_pkey_ctx_set_params_strict(ctx, params);
943
0
}
944
945
/* Helpers to set or get diverse hash algorithm names */
946
static int int_get_rsa_md_name(EVP_PKEY_CTX *ctx,
947
                               /* For checks */
948
                               int keytype, int optype,
949
                               /* For EVP_PKEY_CTX_get_params() */
950
                               const char *mdkey,
951
                               char *mdname, size_t mdnamesize)
952
0
{
953
0
    OSSL_PARAM params[2], *p = params;
954
955
0
    if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
956
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
957
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
958
0
        return -2;
959
0
    }
960
961
    /* If key type not RSA return error */
962
0
    switch (keytype) {
963
0
    case -1:
964
0
        if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
965
0
            && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
966
0
            return -1;
967
0
        break;
968
0
    default:
969
0
        if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
970
0
            return -1;
971
0
        break;
972
0
    }
973
974
    /* Cast away the const. This is read only so should be safe */
975
0
    *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, mdnamesize);
976
0
    *p++ = OSSL_PARAM_construct_end();
977
978
0
    return evp_pkey_ctx_get_params_strict(ctx, params);
979
0
}
980
981
/*
982
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
983
 * simply because that's easier.
984
 */
985
int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
986
32.8k
{
987
32.8k
    return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING,
988
32.8k
                             pad_mode, NULL);
989
32.8k
}
990
991
/*
992
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
993
 * simply because that's easier.
994
 */
995
int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
996
0
{
997
0
    return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
998
0
                             0, pad_mode);
999
0
}
1000
1001
/*
1002
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1003
 * simply because that's easier.
1004
 */
1005
int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1006
0
{
1007
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1008
0
                             EVP_PKEY_CTRL_MD, 0, (void *)(md));
1009
0
}
1010
1011
int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX *ctx,
1012
                                            const char *mdname,
1013
                                            const char *mdprops)
1014
0
{
1015
0
    return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1016
0
                               OSSL_PKEY_PARAM_RSA_DIGEST, mdname,
1017
0
                               OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, mdprops);
1018
0
}
1019
1020
/*
1021
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1022
 * simply because that's easier.
1023
 */
1024
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1025
0
{
1026
    /* If key type not RSA return error */
1027
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1028
0
        return -1;
1029
1030
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1031
0
                             EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md));
1032
0
}
1033
1034
int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1035
                                      const char *mdprops)
1036
0
{
1037
0
    return
1038
0
        int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1039
0
                            OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname,
1040
0
                            OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops);
1041
0
}
1042
1043
int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
1044
                                      size_t namesize)
1045
0
{
1046
0
    return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1047
0
                               OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
1048
0
                               name, namesize);
1049
0
}
1050
1051
/*
1052
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1053
 * simply because that's easier.
1054
 */
1055
int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1056
0
{
1057
    /* If key type not RSA return error */
1058
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1059
0
        return -1;
1060
1061
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1062
0
                             EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
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_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1070
4.87k
{
1071
4.87k
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1072
4.87k
                             EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
1073
4.87k
}
1074
1075
int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1076
                                      const char *mdprops)
1077
0
{
1078
0
    return int_set_rsa_md_name(ctx, -1,
1079
0
                               EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1080
0
                               OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
1081
0
                               OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops);
1082
0
}
1083
1084
int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1085
                                      size_t namesize)
1086
0
{
1087
0
    return int_get_rsa_md_name(ctx, -1,
1088
0
                               EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1089
0
                               OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize);
1090
0
}
1091
1092
/*
1093
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1094
 * simply because that's easier.
1095
 */
1096
int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1097
0
{
1098
0
    return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1099
0
                             EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
1100
0
}
1101
1102
int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
1103
                                                 const char *mdname)
1104
0
{
1105
0
    return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1106
0
                               OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
1107
0
                               NULL, NULL);
1108
0
}
1109
1110
/*
1111
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1112
 * simply because that's easier.
1113
 */
1114
int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1115
0
{
1116
0
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1117
0
                             EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md));
1118
0
}
1119
1120
int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1121
0
{
1122
0
    OSSL_PARAM rsa_params[2], *p = rsa_params;
1123
0
    const char *empty = "";
1124
    /*
1125
     * Needed as we swap label with empty if it is NULL, and label is
1126
     * freed at the end of this function.
1127
     */
1128
0
    void *plabel = label;
1129
0
    int ret;
1130
1131
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1132
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1133
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1134
0
        return -2;
1135
0
    }
1136
1137
    /* If key type not RSA return error */
1138
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1139
0
        return -1;
1140
1141
    /* Accept NULL for backward compatibility */
1142
0
    if (label == NULL && llen == 0)
1143
0
        plabel = (void *)empty;
1144
1145
    /* Cast away the const. This is read only so should be safe */
1146
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1147
0
                                             (void *)plabel, (size_t)llen);
1148
0
    *p++ = OSSL_PARAM_construct_end();
1149
1150
0
    ret = evp_pkey_ctx_set_params_strict(ctx, rsa_params);
1151
0
    if (ret <= 0)
1152
0
        return ret;
1153
1154
    /* Ownership is supposed to be transferred to the callee. */
1155
0
    OPENSSL_free(label);
1156
0
    return 1;
1157
0
}
1158
1159
int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1160
0
{
1161
0
    OSSL_PARAM rsa_params[2], *p = rsa_params;
1162
0
    size_t labellen;
1163
1164
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1165
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1166
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1167
0
        return -2;
1168
0
    }
1169
1170
    /* If key type not RSA return error */
1171
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1172
0
        return -1;
1173
1174
0
    *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1175
0
                                          (void **)label, 0);
1176
0
    *p++ = OSSL_PARAM_construct_end();
1177
1178
0
    if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1179
0
        return -1;
1180
1181
0
    labellen = rsa_params[0].return_size;
1182
0
    if (labellen > INT_MAX)
1183
0
        return -1;
1184
1185
0
    return (int)labellen;
1186
0
}
1187
1188
/*
1189
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1190
 * simply because that's easier.
1191
 */
1192
int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1193
21.9k
{
1194
    /*
1195
     * For some reason, the optype was set to this:
1196
     *
1197
     * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1198
     *
1199
     * However, we do use RSA-PSS with the whole gamut of diverse signature
1200
     * and verification operations, so the optype gets upgraded to this:
1201
     *
1202
     * EVP_PKEY_OP_TYPE_SIG
1203
     */
1204
21.9k
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
1205
21.9k
                             EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL);
1206
21.9k
}
1207
1208
/*
1209
 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1210
 * simply because that's easier.
1211
 */
1212
int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1213
0
{
1214
    /*
1215
     * Because of circumstances, the optype is updated from:
1216
     *
1217
     * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1218
     *
1219
     * to:
1220
     *
1221
     * EVP_PKEY_OP_TYPE_SIG
1222
     */
1223
0
    return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
1224
0
                             EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen);
1225
0
}
1226
1227
int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1228
0
{
1229
0
    OSSL_PARAM pad_params[2], *p = pad_params;
1230
1231
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1232
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1233
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1234
0
        return -2;
1235
0
    }
1236
1237
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1238
0
        return -1;
1239
1240
0
    *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
1241
0
                                    &saltlen);
1242
0
    *p++ = OSSL_PARAM_construct_end();
1243
1244
0
    return evp_pkey_ctx_set_params_strict(ctx, pad_params);
1245
0
}
1246
1247
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1248
0
{
1249
0
    OSSL_PARAM params[2], *p = params;
1250
0
    size_t bits2 = bits;
1251
1252
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1253
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1254
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1255
0
        return -2;
1256
0
    }
1257
1258
    /* If key type not RSA return error */
1259
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
1260
0
        && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1261
0
        return -1;
1262
1263
0
    *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1264
0
    *p++ = OSSL_PARAM_construct_end();
1265
1266
0
    return evp_pkey_ctx_set_params_strict(ctx, params);
1267
0
}
1268
1269
int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1270
0
{
1271
0
    int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN,
1272
0
                                EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1273
1274
    /*
1275
     * Satisfy memory semantics for pre-3.0 callers of
1276
     * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1277
     * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1278
     */
1279
0
    if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) {
1280
0
        BN_free(ctx->rsa_pubexp);
1281
0
        ctx->rsa_pubexp = pubexp;
1282
0
    }
1283
1284
0
    return ret;
1285
0
}
1286
1287
int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1288
0
{
1289
0
    int ret = 0;
1290
1291
    /*
1292
     * When we're dealing with a provider, there's no need to duplicate
1293
     * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
1294
     */
1295
0
    if (evp_pkey_ctx_is_legacy(ctx)) {
1296
0
        pubexp = BN_dup(pubexp);
1297
0
        if (pubexp == NULL)
1298
0
            return 0;
1299
0
    }
1300
0
    ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1301
0
                            EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1302
0
    if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0)
1303
0
        BN_free(pubexp);
1304
0
    return ret;
1305
0
}
1306
1307
int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1308
0
{
1309
0
    OSSL_PARAM params[2], *p = params;
1310
0
    size_t primes2 = primes;
1311
1312
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1313
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1314
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1315
0
        return -2;
1316
0
    }
1317
1318
    /* If key type not RSA return error */
1319
0
    if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
1320
0
        && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1321
0
        return -1;
1322
1323
0
    *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1324
0
    *p++ = OSSL_PARAM_construct_end();
1325
1326
0
    return evp_pkey_ctx_set_params_strict(ctx, params);
1327
0
}
1328
#endif