Coverage Report

Created: 2026-06-18 06:34

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