Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/rsa/rsa_backend.c
Line
Count
Source
1
/*
2
 * Copyright 2020-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 <string.h>
17
#include <openssl/core_names.h>
18
#include <openssl/params.h>
19
#include <openssl/err.h>
20
#include <openssl/evp.h>
21
#ifndef FIPS_MODULE
22
#include <openssl/x509.h>
23
#include "crypto/asn1.h"
24
#endif
25
#include "internal/sizes.h"
26
#include "internal/param_build_set.h"
27
#include "crypto/rsa.h"
28
#include "rsa_local.h"
29
30
/*
31
 * The intention with the "backend" source file is to offer backend functions
32
 * for legacy backends (EVP_PKEY_ASN1_METHOD) and provider implementations
33
 * alike.
34
 */
35
36
DEFINE_STACK_OF(BIGNUM)
37
38
static int collect_numbers(STACK_OF(BIGNUM) *numbers,
39
    const OSSL_PARAM params[], const char *names[])
40
184k
{
41
184k
    const OSSL_PARAM *p = NULL;
42
184k
    int i;
43
44
184k
    if (numbers == NULL)
45
0
        return 0;
46
47
1.97M
    for (i = 0; names[i] != NULL; i++) {
48
1.78M
        p = OSSL_PARAM_locate_const(params, names[i]);
49
1.78M
        if (p != NULL) {
50
308k
            BIGNUM *tmp = NULL;
51
52
308k
            if (!OSSL_PARAM_get_BN(p, &tmp))
53
0
                return 0;
54
308k
            if (sk_BIGNUM_push(numbers, tmp) == 0) {
55
0
                BN_clear_free(tmp);
56
0
                return 0;
57
0
            }
58
308k
        }
59
1.78M
    }
60
61
184k
    return 1;
62
184k
}
63
64
int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private)
65
3.85k
{
66
3.85k
    const OSSL_PARAM *param_n, *param_e, *param_d = NULL;
67
3.85k
    const OSSL_PARAM *param_derive = NULL;
68
3.85k
    BIGNUM *n = NULL, *e = NULL, *d = NULL;
69
3.85k
    STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
70
3.85k
    int is_private = 0;
71
3.85k
    int derive_from_pq = 0;
72
3.85k
    BN_CTX *ctx = NULL;
73
74
3.85k
    if (rsa == NULL)
75
0
        return 0;
76
77
3.85k
    param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
78
3.85k
    param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
79
80
3.85k
    if ((param_n == NULL || !OSSL_PARAM_get_BN(param_n, &n))
81
3.85k
        || (param_e == NULL || !OSSL_PARAM_get_BN(param_e, &e))) {
82
0
        ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
83
0
        goto err;
84
0
    }
85
86
3.85k
    if (include_private) {
87
88
3.85k
        param_derive = OSSL_PARAM_locate_const(params,
89
3.85k
            OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ);
90
3.85k
        if ((param_derive != NULL)
91
0
            && !OSSL_PARAM_get_int(param_derive, &derive_from_pq))
92
0
            goto err;
93
94
3.85k
        param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
95
3.85k
        if (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)) {
96
0
            ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
97
0
            goto err;
98
0
        }
99
100
3.85k
        if (derive_from_pq) {
101
0
            ctx = BN_CTX_new_ex(rsa->libctx);
102
0
            if (ctx == NULL)
103
0
                goto err;
104
105
            /* we need at minimum p, q */
106
0
            if (OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR1) == NULL
107
0
                || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR2) == NULL) {
108
0
                ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
109
0
                goto err;
110
0
            }
111
0
        }
112
3.85k
    }
113
114
3.85k
    is_private = (d != NULL);
115
116
3.85k
    if (!RSA_set0_key(rsa, n, e, d))
117
0
        goto err;
118
3.85k
    n = e = d = NULL;
119
120
3.85k
    if (is_private) {
121
3.85k
        if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
122
3.85k
                ossl_rsa_mp_factor_names)
123
3.85k
            || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
124
3.85k
                ossl_rsa_mp_exp_names)
125
3.85k
            || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
126
3.85k
                ossl_rsa_mp_coeff_names))
127
0
            goto err;
128
129
3.85k
        if (derive_from_pq && sk_BIGNUM_num(exps) == 0
130
0
            && sk_BIGNUM_num(coeffs) == 0) {
131
            /*
132
             * If we want to use crt to derive our exponents/coefficients, we
133
             * need to have at least 2 factors
134
             */
135
0
            if (sk_BIGNUM_num(factors) < 2) {
136
0
                ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
137
0
                goto err;
138
0
            }
139
140
            /*
141
             * if we have more than two factors, n and d must also have
142
             * been provided
143
             */
144
0
            if (sk_BIGNUM_num(factors) > 2
145
0
                && (param_n == NULL || param_d == NULL)) {
146
0
                ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);
147
0
                goto err;
148
0
            }
149
150
            /* build our exponents and coefficients here */
151
0
            if (sk_BIGNUM_num(factors) == 2) {
152
                /* for 2 factors we can use the sp800 functions to do this */
153
0
                if (!RSA_set0_factors(rsa, sk_BIGNUM_value(factors, 0),
154
0
                        sk_BIGNUM_value(factors, 1))) {
155
0
                    ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
156
0
                    goto err;
157
0
                }
158
                /*
159
                 * once consumed by RSA_set0_factors, pop those off the stack
160
                 * so we don't free them below
161
                 */
162
0
                sk_BIGNUM_pop(factors);
163
0
                sk_BIGNUM_pop(factors);
164
165
                /*
166
                 * Note: Because we only have 2 factors here, there will be no
167
                 * additional pinfo fields to hold additional factors, and
168
                 * since we set our key and 2 factors above we can skip
169
                 * the call to ossl_rsa_set0_all_params
170
                 */
171
0
                if (!ossl_rsa_sp800_56b_derive_params_from_pq(rsa,
172
0
                        RSA_bits(rsa),
173
0
                        NULL, ctx)) {
174
0
                    ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
175
0
                    goto err;
176
0
                }
177
0
            } else {
178
0
#ifndef FIPS_MODULE
179
                /*
180
                 * in the multiprime case we have to generate exps/coeffs here
181
                 * for each additional prime
182
                 */
183
0
                if (!ossl_rsa_multiprime_derive(rsa, RSA_bits(rsa),
184
0
                        sk_BIGNUM_num(factors),
185
0
                        rsa->e, factors, exps,
186
0
                        coeffs)) {
187
0
                    ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
188
0
                    goto err;
189
0
                }
190
191
                /*
192
                 * Now we should have all our factors, exponents and
193
                 * coefficients
194
                 */
195
0
                if (!ossl_rsa_set0_all_params(rsa, factors, exps, coeffs)) {
196
0
                    ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
197
0
                    goto err;
198
0
                }
199
200
#else
201
                /* multiprime case is disallowed in FIPS mode, raise an error */
202
                ERR_raise(ERR_LIB_RSA, ERR_R_UNSUPPORTED);
203
                goto err;
204
#endif
205
0
            }
206
207
3.85k
        } else {
208
            /*
209
             * It's ok if this private key just has n, e and d
210
             * but only if we're not using derive_from_pq
211
             */
212
3.85k
            if (sk_BIGNUM_num(factors) != 0
213
3.85k
                && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs))
214
0
                goto err;
215
3.85k
        }
216
        /* sanity check to ensure we used everything in our stacks */
217
3.85k
        if (sk_BIGNUM_num(factors) != 0
218
3.85k
            || sk_BIGNUM_num(exps) != 0
219
3.85k
            || sk_BIGNUM_num(coeffs) != 0) {
220
0
            ERR_raise_data(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR,
221
0
                "There are %d, %d, %d elements left on our factors, exps, coeffs stacks\n",
222
0
                sk_BIGNUM_num(factors), sk_BIGNUM_num(exps),
223
0
                sk_BIGNUM_num(coeffs));
224
0
            goto err;
225
0
        }
226
3.85k
    }
227
228
3.85k
    if (!ossl_rsa_check_factors(rsa)) {
229
0
        ERR_raise_data(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR,
230
0
            "RSA factors/exponents are too big for n-modulus\n");
231
0
        goto err;
232
0
    }
233
234
3.85k
    sk_BIGNUM_free(factors);
235
3.85k
    sk_BIGNUM_free(exps);
236
3.85k
    sk_BIGNUM_free(coeffs);
237
3.85k
    BN_CTX_free(ctx);
238
3.85k
    return 1;
239
240
0
err:
241
0
    BN_free(n);
242
0
    BN_free(e);
243
0
    BN_free(d);
244
0
    sk_BIGNUM_pop_free(factors, BN_clear_free);
245
0
    sk_BIGNUM_pop_free(exps, BN_clear_free);
246
0
    sk_BIGNUM_pop_free(coeffs, BN_clear_free);
247
0
    BN_CTX_free(ctx);
248
0
    return 0;
249
3.85k
}
250
251
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
252
253
int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
254
    int include_private)
255
231k
{
256
231k
    int ret = 0;
257
231k
    const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
258
231k
    STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
259
231k
    STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
260
231k
    STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
261
262
231k
    if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
263
0
        goto err;
264
265
231k
    RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
266
231k
    ossl_rsa_get0_all_params(rsa, factors, exps, coeffs);
267
268
231k
    if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
269
231k
        || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e))
270
0
        goto err;
271
272
    /* Check private key data integrity */
273
231k
    if (include_private && rsa_d != NULL) {
274
275
87.0k
        if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D,
276
87.0k
                rsa_d)
277
87.0k
            || !ossl_param_build_set_multi_key_bn(bld, params,
278
87.0k
                ossl_rsa_mp_factor_names,
279
87.0k
                factors)
280
87.0k
            || !ossl_param_build_set_multi_key_bn(bld, params,
281
87.0k
                ossl_rsa_mp_exp_names, exps)
282
87.0k
            || !ossl_param_build_set_multi_key_bn(bld, params,
283
87.0k
                ossl_rsa_mp_coeff_names,
284
87.0k
                coeffs))
285
0
            goto err;
286
87.0k
    }
287
288
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
289
    /* The acvp test results are not meant for export so check for bld == NULL */
290
    if (bld == NULL)
291
        ossl_rsa_acvp_test_get_params(rsa, params);
292
#endif
293
231k
    ret = 1;
294
231k
err:
295
231k
    sk_BIGNUM_const_free(factors);
296
231k
    sk_BIGNUM_const_free(exps);
297
231k
    sk_BIGNUM_const_free(coeffs);
298
231k
    return ret;
299
231k
}
300
301
int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss,
302
    OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
303
32.0k
{
304
32.0k
    if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
305
14.6k
        int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss);
306
14.6k
        int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss);
307
14.6k
        int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
308
14.6k
        int saltlen = ossl_rsa_pss_params_30_saltlen(pss);
309
14.6k
        int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL);
310
14.6k
        int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
311
14.6k
        int default_maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(NULL);
312
14.6k
        const char *mdname = (hashalg_nid == default_hashalg_nid
313
14.6k
                ? NULL
314
14.6k
                : ossl_rsa_oaeppss_nid2name(hashalg_nid));
315
14.6k
        const char *mgfname = (maskgenalg_nid == default_maskgenalg_nid
316
14.6k
                ? NULL
317
14.6k
                : ossl_rsa_oaeppss_nid2name(maskgenalg_nid));
318
14.6k
        const char *mgf1mdname = (maskgenhashalg_nid == default_maskgenhashalg_nid
319
14.6k
                ? NULL
320
14.6k
                : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid));
321
14.6k
        const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST;
322
14.6k
        const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC;
323
14.6k
        const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
324
14.6k
        const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
325
326
        /*
327
         * To ensure that the key isn't seen as unrestricted by the recipient,
328
         * we make sure that at least one PSS-related parameter is passed, even
329
         * if it has a default value; saltlen.
330
         */
331
14.6k
        if ((mdname != NULL
332
127
                && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname))
333
14.6k
            || (mgfname != NULL
334
0
                && !ossl_param_build_set_utf8_string(bld, params,
335
0
                    key_mgf, mgfname))
336
14.6k
            || (mgf1mdname != NULL
337
127
                && !ossl_param_build_set_utf8_string(bld, params,
338
127
                    key_mgf1_md, mgf1mdname))
339
14.6k
            || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen)))
340
0
            return 0;
341
14.6k
    }
342
32.0k
    return 1;
343
32.0k
}
344
345
int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params,
346
    int *defaults_set,
347
    const OSSL_PARAM params[],
348
    OSSL_LIB_CTX *libctx)
349
20.6k
{
350
20.6k
    const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen;
351
20.6k
    const OSSL_PARAM *param_propq;
352
20.6k
    const char *propq = NULL;
353
20.6k
    EVP_MD *md = NULL, *mgf1md = NULL;
354
20.6k
    int saltlen;
355
20.6k
    int ret = 0;
356
357
20.6k
    if (pss_params == NULL)
358
0
        return 0;
359
20.6k
    param_propq = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);
360
20.6k
    param_md = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);
361
20.6k
    param_mgf = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);
362
20.6k
    param_mgf1md = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
363
20.6k
    param_saltlen = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);
364
365
20.6k
    if (param_propq != NULL) {
366
0
        if (param_propq->data_type == OSSL_PARAM_UTF8_STRING)
367
0
            propq = param_propq->data;
368
0
    }
369
    /*
370
     * If we get any of the parameters, we know we have at least some
371
     * restrictions, so we start by setting default values, and let each
372
     * parameter override their specific restriction data.
373
     */
374
20.6k
    if (!*defaults_set
375
20.6k
        && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL
376
20.6k
            || param_saltlen != NULL)) {
377
0
        if (!ossl_rsa_pss_params_30_set_defaults(pss_params))
378
0
            return 0;
379
0
        *defaults_set = 1;
380
0
    }
381
382
20.6k
    if (param_mgf != NULL) {
383
0
        int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
384
0
        const char *mgfname = NULL;
385
386
0
        if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING)
387
0
            mgfname = param_mgf->data;
388
0
        else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname))
389
0
            return 0;
390
391
0
        if (OPENSSL_strcasecmp(param_mgf->data,
392
0
                ossl_rsa_mgf_nid2name(default_maskgenalg_nid))
393
0
            != 0)
394
0
            return 0;
395
0
    }
396
397
    /*
398
     * We're only interested in the NIDs that correspond to the MDs, so the
399
     * exact propquery is unimportant in the EVP_MD_fetch() calls below.
400
     */
401
402
20.6k
    if (param_md != NULL) {
403
0
        const char *mdname = NULL;
404
405
0
        if (param_md->data_type == OSSL_PARAM_UTF8_STRING)
406
0
            mdname = param_md->data;
407
0
        else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname))
408
0
            goto err;
409
410
0
        if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL
411
0
            || !ossl_rsa_pss_params_30_set_hashalg(pss_params,
412
0
                ossl_rsa_oaeppss_md2nid(md)))
413
0
            goto err;
414
0
    }
415
416
20.6k
    if (param_mgf1md != NULL) {
417
0
        const char *mgf1mdname = NULL;
418
419
0
        if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING)
420
0
            mgf1mdname = param_mgf1md->data;
421
0
        else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname))
422
0
            goto err;
423
424
0
        if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL
425
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(
426
0
                pss_params, ossl_rsa_oaeppss_md2nid(mgf1md)))
427
0
            goto err;
428
0
    }
429
430
20.6k
    if (param_saltlen != NULL) {
431
0
        if (!OSSL_PARAM_get_int(param_saltlen, &saltlen)
432
0
            || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen))
433
0
            goto err;
434
0
    }
435
436
20.6k
    ret = 1;
437
438
20.6k
err:
439
20.6k
    EVP_MD_free(md);
440
20.6k
    EVP_MD_free(mgf1md);
441
20.6k
    return ret;
442
20.6k
}
443
444
int ossl_rsa_is_foreign(const RSA *rsa)
445
36.1k
{
446
36.1k
#ifndef FIPS_MODULE
447
36.1k
    if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
448
0
        return 1;
449
36.1k
#endif
450
36.1k
    return 0;
451
36.1k
}
452
453
static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
454
152k
{
455
152k
    if (f != NULL && (*out = BN_dup(f)) == NULL)
456
0
        return 0;
457
152k
    return 1;
458
152k
}
459
460
RSA *ossl_rsa_dup(const RSA *rsa, int selection)
461
773
{
462
773
    RSA *dupkey = NULL;
463
773
#ifndef FIPS_MODULE
464
773
    int pnum, i;
465
773
#endif
466
467
    /* Do not try to duplicate foreign RSA keys */
468
773
    if (ossl_rsa_is_foreign(rsa))
469
0
        return NULL;
470
471
773
    if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)
472
0
        return NULL;
473
474
    /* public key */
475
773
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
476
773
        if (!rsa_bn_dup_check(&dupkey->n, rsa->n))
477
0
            goto err;
478
773
        if (!rsa_bn_dup_check(&dupkey->e, rsa->e))
479
0
            goto err;
480
773
    }
481
482
773
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
483
484
        /* private key */
485
773
        if (!rsa_bn_dup_check(&dupkey->d, rsa->d))
486
0
            goto err;
487
488
        /* factors and crt params */
489
773
        if (!rsa_bn_dup_check(&dupkey->p, rsa->p))
490
0
            goto err;
491
773
        if (!rsa_bn_dup_check(&dupkey->q, rsa->q))
492
0
            goto err;
493
773
        if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))
494
0
            goto err;
495
773
        if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))
496
0
            goto err;
497
773
        if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))
498
0
            goto err;
499
773
    }
500
501
773
    dupkey->version = rsa->version;
502
773
    dupkey->flags = rsa->flags;
503
    /* we always copy the PSS parameters regardless of selection */
504
773
    dupkey->pss_params = rsa->pss_params;
505
506
773
#ifndef FIPS_MODULE
507
    /* multiprime */
508
773
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
509
773
        && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) {
510
46
        dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
511
46
        if (dupkey->prime_infos == NULL)
512
0
            goto err;
513
126
        for (i = 0; i < pnum; i++) {
514
80
            const RSA_PRIME_INFO *pinfo = NULL;
515
80
            RSA_PRIME_INFO *duppinfo = NULL;
516
517
80
            if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL)
518
0
                goto err;
519
            /* push first so cleanup in error case works */
520
80
            (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);
521
522
80
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
523
80
            if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))
524
0
                goto err;
525
80
            if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))
526
0
                goto err;
527
80
            if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))
528
0
                goto err;
529
80
        }
530
46
        if (!ossl_rsa_multip_calc_product(dupkey))
531
0
            goto err;
532
46
    }
533
534
773
    if (rsa->pss != NULL) {
535
3
        if ((dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss)) == NULL)
536
0
            goto err;
537
3
        if (rsa->pss->maskGenAlgorithm != NULL
538
0
            && dupkey->pss->maskGenAlgorithm == NULL) {
539
0
            dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);
540
0
            if (dupkey->pss->maskHash == NULL)
541
0
                goto err;
542
0
        }
543
3
    }
544
773
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,
545
773
            &dupkey->ex_data, &rsa->ex_data))
546
0
        goto err;
547
773
#endif
548
549
773
    return dupkey;
550
551
0
err:
552
0
    RSA_free(dupkey);
553
0
    return NULL;
554
773
}
555
556
#ifndef FIPS_MODULE
557
RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg)
558
25.9k
{
559
25.9k
    RSA_PSS_PARAMS *pss;
560
561
25.9k
    pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
562
25.9k
        alg->parameter);
563
564
25.9k
    if (pss == NULL)
565
3.27k
        return NULL;
566
567
22.6k
    if (pss->maskGenAlgorithm != NULL) {
568
911
        pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
569
911
        if (pss->maskHash == NULL) {
570
256
            RSA_PSS_PARAMS_free(pss);
571
256
            return NULL;
572
256
        }
573
911
    }
574
575
22.3k
    return pss;
576
22.6k
}
577
578
static int ossl_rsa_sync_to_pss_params_30(RSA *rsa)
579
14.7k
{
580
14.7k
    const RSA_PSS_PARAMS *legacy_pss = NULL;
581
14.7k
    RSA_PSS_PARAMS_30 *pss = NULL;
582
583
14.7k
    if (rsa != NULL
584
14.7k
        && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL
585
14.7k
        && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) {
586
14.7k
        const EVP_MD *md = NULL, *mgf1md = NULL;
587
14.7k
        int md_nid, mgf1md_nid, saltlen, trailerField;
588
14.7k
        RSA_PSS_PARAMS_30 pss_params;
589
590
        /*
591
         * We don't care about the validity of the fields here, we just
592
         * want to synchronise values.  Verifying here makes it impossible
593
         * to even read a key with invalid values, making it hard to test
594
         * a bad situation.
595
         *
596
         * Other routines use ossl_rsa_pss_get_param(), so the values will
597
         * be checked, eventually.
598
         */
599
14.7k
        if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md,
600
14.7k
                &saltlen, &trailerField))
601
79
            return 0;
602
14.6k
        md_nid = EVP_MD_get_type(md);
603
14.6k
        mgf1md_nid = EVP_MD_get_type(mgf1md);
604
14.6k
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
605
14.6k
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
606
14.6k
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
607
14.6k
                mgf1md_nid)
608
14.6k
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
609
14.6k
            || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,
610
14.6k
                trailerField))
611
0
            return 0;
612
14.6k
        *pss = pss_params;
613
14.6k
    }
614
14.6k
    return 1;
615
14.7k
}
616
617
int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
618
    const EVP_MD **pmd, const EVP_MD **pmgf1md,
619
    int *psaltlen, int *ptrailerField)
620
21.1k
{
621
21.1k
    RSA_PSS_PARAMS_30 pss_params;
622
623
    /* Get the defaults from the ONE place */
624
21.1k
    (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);
625
626
21.1k
    if (pss == NULL)
627
1.18k
        return 0;
628
20.0k
    *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm);
629
20.0k
    if (*pmd == NULL)
630
63
        return 0;
631
19.9k
    *pmgf1md = ossl_x509_algor_get_md(pss->maskHash);
632
19.9k
    if (*pmgf1md == NULL)
633
53
        return 0;
634
19.8k
    if (pss->saltLength)
635
213
        *psaltlen = ASN1_INTEGER_get(pss->saltLength);
636
19.6k
    else
637
19.6k
        *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);
638
19.8k
    if (pss->trailerField)
639
42
        *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
640
19.8k
    else
641
19.8k
        *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);
642
643
19.8k
    return 1;
644
19.9k
}
645
646
int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
647
163k
{
648
163k
    RSA_PSS_PARAMS *pss;
649
163k
    const ASN1_OBJECT *algoid;
650
163k
    const void *algp;
651
163k
    int algptype;
652
653
163k
    X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
654
163k
    if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
655
129k
        return 1;
656
34.2k
    if (algptype == V_ASN1_UNDEF)
657
17.3k
        return 1;
658
16.8k
    if (algptype != V_ASN1_SEQUENCE) {
659
1.75k
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
660
1.75k
        return 0;
661
1.75k
    }
662
15.0k
    if ((pss = ossl_rsa_pss_decode(alg)) == NULL
663
14.7k
        || !ossl_rsa_set0_pss_params(rsa, pss)) {
664
330
        RSA_PSS_PARAMS_free(pss);
665
330
        return 0;
666
330
    }
667
14.7k
    if (!ossl_rsa_sync_to_pss_params_30(rsa))
668
79
        return 0;
669
14.6k
    return 1;
670
14.7k
}
671
672
RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
673
    OSSL_LIB_CTX *libctx, const char *propq)
674
20.8k
{
675
20.8k
    const unsigned char *p;
676
20.8k
    RSA *rsa;
677
20.8k
    int pklen;
678
20.8k
    const X509_ALGOR *alg;
679
680
20.8k
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf))
681
0
        return 0;
682
20.8k
    rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
683
20.8k
    if (rsa == NULL) {
684
96
        ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
685
96
        return NULL;
686
96
    }
687
20.7k
    if (!ossl_rsa_param_decode(rsa, alg)) {
688
28
        RSA_free(rsa);
689
28
        return NULL;
690
28
    }
691
692
20.7k
    RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
693
20.7k
    switch (OBJ_obj2nid(alg->algorithm)) {
694
20.6k
    case EVP_PKEY_RSA:
695
20.6k
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
696
20.6k
        break;
697
37
    case EVP_PKEY_RSA_PSS:
698
37
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
699
37
        break;
700
0
    default:
701
        /* Leave the type bits zero */
702
0
        break;
703
20.7k
    }
704
705
20.7k
    return rsa;
706
20.7k
}
707
#endif