Coverage Report

Created: 2025-12-10 06:24

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