Coverage Report

Created: 2025-06-13 06:58

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