Coverage Report

Created: 2026-05-24 07:14

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