Coverage Report

Created: 2025-12-31 06:58

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