Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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 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
219k
{
41
219k
    const OSSL_PARAM *p = NULL;
42
219k
    int i;
43
44
219k
    if (numbers == NULL)
45
0
        return 0;
46
47
2.33M
    for (i = 0; names[i] != NULL; i++) {
48
2.12M
        p = OSSL_PARAM_locate_const(params, names[i]);
49
2.12M
        if (p != NULL) {
50
365k
            BIGNUM *tmp = NULL;
51
52
365k
            if (!OSSL_PARAM_get_BN(p, &tmp))
53
0
                return 0;
54
365k
            if (sk_BIGNUM_push(numbers, tmp) == 0) {
55
0
                BN_clear_free(tmp);
56
0
                return 0;
57
0
            }
58
365k
        }
59
2.12M
    }
60
61
219k
    return 1;
62
219k
}
63
64
int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private)
65
54.1k
{
66
54.1k
    const OSSL_PARAM *param_n, *param_e, *param_d = NULL;
67
54.1k
    const OSSL_PARAM *param_p, *param_q = NULL;
68
54.1k
    const OSSL_PARAM *param_derive = NULL;
69
54.1k
    BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
70
54.1k
    STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
71
54.1k
    int is_private = 0;
72
54.1k
    int derive_from_pq = 0;
73
54.1k
    BN_CTX *ctx = NULL;
74
75
54.1k
    if (rsa == NULL)
76
0
        return 0;
77
78
54.1k
    param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
79
54.1k
    param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
80
81
54.1k
    if ((param_n == NULL || !OSSL_PARAM_get_BN(param_n, &n))
82
54.1k
        || (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
54.1k
    if (include_private) {
88
89
54.1k
        param_derive = OSSL_PARAM_locate_const(params,
90
54.1k
            OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ);
91
54.1k
        if ((param_derive != NULL)
92
0
            && !OSSL_PARAM_get_int(param_derive, &derive_from_pq))
93
0
            goto err;
94
95
54.1k
        param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
96
54.1k
        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
54.1k
        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
54.1k
    }
116
117
54.1k
    is_private = (d != NULL);
118
119
54.1k
    if (!RSA_set0_key(rsa, n, e, d))
120
0
        goto err;
121
54.1k
    n = e = d = NULL;
122
123
54.1k
    if (is_private) {
124
54.1k
        if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
125
54.1k
                ossl_rsa_mp_factor_names)
126
54.1k
            || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
127
54.1k
                ossl_rsa_mp_exp_names)
128
54.1k
            || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
129
54.1k
                ossl_rsa_mp_coeff_names))
130
0
            goto err;
131
132
54.1k
        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
54.1k
        } 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
54.1k
            if (sk_BIGNUM_num(factors) != 0
216
54.1k
                && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs))
217
0
                goto err;
218
54.1k
        }
219
        /* sanity check to ensure we used everything in our stacks */
220
54.1k
        if (sk_BIGNUM_num(factors) != 0
221
54.1k
            || sk_BIGNUM_num(exps) != 0
222
54.1k
            || 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
54.1k
    }
230
231
54.1k
    if (!ossl_rsa_check_factors(rsa)) {
232
0
        ERR_raise_data(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR,
233
0
            "RSA factors/exponents are too big for for n-modulus\n");
234
0
        goto err;
235
0
    }
236
237
54.1k
    BN_clear_free(p);
238
54.1k
    BN_clear_free(q);
239
54.1k
    sk_BIGNUM_free(factors);
240
54.1k
    sk_BIGNUM_free(exps);
241
54.1k
    sk_BIGNUM_free(coeffs);
242
54.1k
    BN_CTX_free(ctx);
243
54.1k
    return 1;
244
245
0
err:
246
0
    BN_free(n);
247
0
    BN_free(e);
248
0
    BN_free(d);
249
0
    sk_BIGNUM_pop_free(factors, BN_clear_free);
250
0
    sk_BIGNUM_pop_free(exps, BN_clear_free);
251
0
    sk_BIGNUM_pop_free(coeffs, BN_clear_free);
252
0
    BN_CTX_free(ctx);
253
0
    return 0;
254
54.1k
}
255
256
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
257
258
int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
259
    int include_private)
260
227k
{
261
227k
    int ret = 0;
262
227k
    const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
263
227k
    STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
264
227k
    STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
265
227k
    STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
266
267
227k
    if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
268
0
        goto err;
269
270
227k
    RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
271
227k
    ossl_rsa_get0_all_params(rsa, factors, exps, coeffs);
272
273
227k
    if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
274
227k
        || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e))
275
0
        goto err;
276
277
    /* Check private key data integrity */
278
227k
    if (include_private && rsa_d != NULL) {
279
280
99.6k
        if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D,
281
99.6k
                rsa_d)
282
99.6k
            || !ossl_param_build_set_multi_key_bn(bld, params,
283
99.6k
                ossl_rsa_mp_factor_names,
284
99.6k
                factors)
285
99.6k
            || !ossl_param_build_set_multi_key_bn(bld, params,
286
99.6k
                ossl_rsa_mp_exp_names, exps)
287
99.6k
            || !ossl_param_build_set_multi_key_bn(bld, params,
288
99.6k
                ossl_rsa_mp_coeff_names,
289
99.6k
                coeffs))
290
0
            goto err;
291
99.6k
    }
292
293
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
294
    /* The acvp test results are not meant for export so check for bld == NULL */
295
    if (bld == NULL)
296
        ossl_rsa_acvp_test_get_params(rsa, params);
297
#endif
298
227k
    ret = 1;
299
227k
err:
300
227k
    sk_BIGNUM_const_free(factors);
301
227k
    sk_BIGNUM_const_free(exps);
302
227k
    sk_BIGNUM_const_free(coeffs);
303
227k
    return ret;
304
227k
}
305
306
int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss,
307
    OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
308
19.3k
{
309
19.3k
    if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
310
10.0k
        int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss);
311
10.0k
        int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss);
312
10.0k
        int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
313
10.0k
        int saltlen = ossl_rsa_pss_params_30_saltlen(pss);
314
10.0k
        int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL);
315
10.0k
        int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
316
10.0k
        int default_maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(NULL);
317
10.0k
        const char *mdname = (hashalg_nid == default_hashalg_nid
318
10.0k
                ? NULL
319
10.0k
                : ossl_rsa_oaeppss_nid2name(hashalg_nid));
320
10.0k
        const char *mgfname = (maskgenalg_nid == default_maskgenalg_nid
321
10.0k
                ? NULL
322
10.0k
                : ossl_rsa_oaeppss_nid2name(maskgenalg_nid));
323
10.0k
        const char *mgf1mdname = (maskgenhashalg_nid == default_maskgenhashalg_nid
324
10.0k
                ? NULL
325
10.0k
                : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid));
326
10.0k
        const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST;
327
10.0k
        const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC;
328
10.0k
        const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
329
10.0k
        const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
330
331
        /*
332
         * To ensure that the key isn't seen as unrestricted by the recipient,
333
         * we make sure that at least one PSS-related parameter is passed, even
334
         * if it has a default value; saltlen.
335
         */
336
10.0k
        if ((mdname != NULL
337
49
                && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname))
338
10.0k
            || (mgfname != NULL
339
0
                && !ossl_param_build_set_utf8_string(bld, params,
340
0
                    key_mgf, mgfname))
341
10.0k
            || (mgf1mdname != NULL
342
51
                && !ossl_param_build_set_utf8_string(bld, params,
343
51
                    key_mgf1_md, mgf1mdname))
344
10.0k
            || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen)))
345
0
            return 0;
346
10.0k
    }
347
19.3k
    return 1;
348
19.3k
}
349
350
int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params,
351
    int *defaults_set,
352
    const OSSL_PARAM params[],
353
    OSSL_LIB_CTX *libctx)
354
21.6k
{
355
21.6k
    const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen;
356
21.6k
    const OSSL_PARAM *param_propq;
357
21.6k
    const char *propq = NULL;
358
21.6k
    EVP_MD *md = NULL, *mgf1md = NULL;
359
21.6k
    int saltlen;
360
21.6k
    int ret = 0;
361
362
21.6k
    if (pss_params == NULL)
363
0
        return 0;
364
21.6k
    param_propq = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);
365
21.6k
    param_md = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);
366
21.6k
    param_mgf = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);
367
21.6k
    param_mgf1md = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
368
21.6k
    param_saltlen = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);
369
370
21.6k
    if (param_propq != NULL) {
371
0
        if (param_propq->data_type == OSSL_PARAM_UTF8_STRING)
372
0
            propq = param_propq->data;
373
0
    }
374
    /*
375
     * If we get any of the parameters, we know we have at least some
376
     * restrictions, so we start by setting default values, and let each
377
     * parameter override their specific restriction data.
378
     */
379
21.6k
    if (!*defaults_set
380
21.6k
        && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL
381
21.6k
            || param_saltlen != NULL)) {
382
0
        if (!ossl_rsa_pss_params_30_set_defaults(pss_params))
383
0
            return 0;
384
0
        *defaults_set = 1;
385
0
    }
386
387
21.6k
    if (param_mgf != NULL) {
388
0
        int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
389
0
        const char *mgfname = NULL;
390
391
0
        if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING)
392
0
            mgfname = param_mgf->data;
393
0
        else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname))
394
0
            return 0;
395
396
0
        if (OPENSSL_strcasecmp(param_mgf->data,
397
0
                ossl_rsa_mgf_nid2name(default_maskgenalg_nid))
398
0
            != 0)
399
0
            return 0;
400
0
    }
401
402
    /*
403
     * We're only interested in the NIDs that correspond to the MDs, so the
404
     * exact propquery is unimportant in the EVP_MD_fetch() calls below.
405
     */
406
407
21.6k
    if (param_md != NULL) {
408
0
        const char *mdname = NULL;
409
410
0
        if (param_md->data_type == OSSL_PARAM_UTF8_STRING)
411
0
            mdname = param_md->data;
412
0
        else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname))
413
0
            goto err;
414
415
0
        if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL
416
0
            || !ossl_rsa_pss_params_30_set_hashalg(pss_params,
417
0
                ossl_rsa_oaeppss_md2nid(md)))
418
0
            goto err;
419
0
    }
420
421
21.6k
    if (param_mgf1md != NULL) {
422
0
        const char *mgf1mdname = NULL;
423
424
0
        if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING)
425
0
            mgf1mdname = param_mgf1md->data;
426
0
        else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname))
427
0
            goto err;
428
429
0
        if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL
430
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(
431
0
                pss_params, ossl_rsa_oaeppss_md2nid(mgf1md)))
432
0
            goto err;
433
0
    }
434
435
21.6k
    if (param_saltlen != NULL) {
436
0
        if (!OSSL_PARAM_get_int(param_saltlen, &saltlen)
437
0
            || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen))
438
0
            goto err;
439
0
    }
440
441
21.6k
    ret = 1;
442
443
21.6k
err:
444
21.6k
    EVP_MD_free(md);
445
21.6k
    EVP_MD_free(mgf1md);
446
21.6k
    return ret;
447
21.6k
}
448
449
int ossl_rsa_is_foreign(const RSA *rsa)
450
164k
{
451
164k
#ifndef FIPS_MODULE
452
164k
    if (rsa->engine != NULL || RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
453
0
        return 1;
454
164k
#endif
455
164k
    return 0;
456
164k
}
457
458
static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
459
120k
{
460
120k
    if (f != NULL && (*out = BN_dup(f)) == NULL)
461
0
        return 0;
462
120k
    return 1;
463
120k
}
464
465
RSA *ossl_rsa_dup(const RSA *rsa, int selection)
466
3.85k
{
467
3.85k
    RSA *dupkey = NULL;
468
3.85k
#ifndef FIPS_MODULE
469
3.85k
    int pnum, i;
470
3.85k
#endif
471
472
    /* Do not try to duplicate foreign RSA keys */
473
3.85k
    if (ossl_rsa_is_foreign(rsa))
474
0
        return NULL;
475
476
3.85k
    if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)
477
0
        return NULL;
478
479
    /* public key */
480
3.85k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
481
3.85k
        if (!rsa_bn_dup_check(&dupkey->n, rsa->n))
482
0
            goto err;
483
3.85k
        if (!rsa_bn_dup_check(&dupkey->e, rsa->e))
484
0
            goto err;
485
3.85k
    }
486
487
3.85k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
488
489
        /* private key */
490
3.85k
        if (!rsa_bn_dup_check(&dupkey->d, rsa->d))
491
0
            goto err;
492
493
        /* factors and crt params */
494
3.85k
        if (!rsa_bn_dup_check(&dupkey->p, rsa->p))
495
0
            goto err;
496
3.85k
        if (!rsa_bn_dup_check(&dupkey->q, rsa->q))
497
0
            goto err;
498
3.85k
        if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))
499
0
            goto err;
500
3.85k
        if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))
501
0
            goto err;
502
3.85k
        if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))
503
0
            goto err;
504
3.85k
    }
505
506
3.85k
    dupkey->version = rsa->version;
507
3.85k
    dupkey->flags = rsa->flags;
508
    /* we always copy the PSS parameters regardless of selection */
509
3.85k
    dupkey->pss_params = rsa->pss_params;
510
511
3.85k
#ifndef FIPS_MODULE
512
    /* multiprime */
513
3.85k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
514
3.85k
        && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) {
515
384
        dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
516
384
        if (dupkey->prime_infos == NULL)
517
0
            goto err;
518
30.3k
        for (i = 0; i < pnum; i++) {
519
30.0k
            const RSA_PRIME_INFO *pinfo = NULL;
520
30.0k
            RSA_PRIME_INFO *duppinfo = NULL;
521
522
30.0k
            if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL)
523
0
                goto err;
524
            /* push first so cleanup in error case works */
525
30.0k
            (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);
526
527
30.0k
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
528
30.0k
            if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))
529
0
                goto err;
530
30.0k
            if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))
531
0
                goto err;
532
30.0k
            if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))
533
0
                goto err;
534
30.0k
        }
535
384
        if (!ossl_rsa_multip_calc_product(dupkey))
536
0
            goto err;
537
384
    }
538
539
3.85k
    if (rsa->pss != NULL) {
540
13
        dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss);
541
13
        if (rsa->pss->maskGenAlgorithm != NULL
542
0
            && dupkey->pss->maskGenAlgorithm == NULL) {
543
0
            dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);
544
0
            if (dupkey->pss->maskHash == NULL)
545
0
                goto err;
546
0
        }
547
13
    }
548
3.85k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,
549
3.85k
            &dupkey->ex_data, &rsa->ex_data))
550
0
        goto err;
551
3.85k
#endif
552
553
3.85k
    return dupkey;
554
555
0
err:
556
0
    RSA_free(dupkey);
557
0
    return NULL;
558
3.85k
}
559
560
#ifndef FIPS_MODULE
561
RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg)
562
21.5k
{
563
21.5k
    RSA_PSS_PARAMS *pss;
564
565
21.5k
    pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
566
21.5k
        alg->parameter);
567
568
21.5k
    if (pss == NULL)
569
4.26k
        return NULL;
570
571
17.3k
    if (pss->maskGenAlgorithm != NULL) {
572
704
        pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
573
704
        if (pss->maskHash == NULL) {
574
227
            RSA_PSS_PARAMS_free(pss);
575
227
            return NULL;
576
227
        }
577
704
    }
578
579
17.0k
    return pss;
580
17.3k
}
581
582
static int ossl_rsa_sync_to_pss_params_30(RSA *rsa)
583
10.1k
{
584
10.1k
    const RSA_PSS_PARAMS *legacy_pss = NULL;
585
10.1k
    RSA_PSS_PARAMS_30 *pss = NULL;
586
587
10.1k
    if (rsa != NULL
588
10.1k
        && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL
589
10.1k
        && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) {
590
10.1k
        const EVP_MD *md = NULL, *mgf1md = NULL;
591
10.1k
        int md_nid, mgf1md_nid, saltlen, trailerField;
592
10.1k
        RSA_PSS_PARAMS_30 pss_params;
593
594
        /*
595
         * We don't care about the validity of the fields here, we just
596
         * want to synchronise values.  Verifying here makes it impossible
597
         * to even read a key with invalid values, making it hard to test
598
         * a bad situation.
599
         *
600
         * Other routines use ossl_rsa_pss_get_param(), so the values will
601
         * be checked, eventually.
602
         */
603
10.1k
        if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md,
604
10.1k
                &saltlen, &trailerField))
605
29
            return 0;
606
10.1k
        md_nid = EVP_MD_get_type(md);
607
10.1k
        mgf1md_nid = EVP_MD_get_type(mgf1md);
608
10.1k
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
609
10.1k
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
610
10.1k
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
611
10.1k
                mgf1md_nid)
612
10.1k
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
613
10.1k
            || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,
614
10.1k
                trailerField))
615
0
            return 0;
616
10.1k
        *pss = pss_params;
617
10.1k
    }
618
10.1k
    return 1;
619
10.1k
}
620
621
int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
622
    const EVP_MD **pmd, const EVP_MD **pmgf1md,
623
    int *psaltlen, int *ptrailerField)
624
17.3k
{
625
17.3k
    RSA_PSS_PARAMS_30 pss_params;
626
627
    /* Get the defaults from the ONE place */
628
17.3k
    (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);
629
630
17.3k
    if (pss == NULL)
631
1.34k
        return 0;
632
15.9k
    *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm);
633
15.9k
    if (*pmd == NULL)
634
28
        return 0;
635
15.9k
    *pmgf1md = ossl_x509_algor_get_md(pss->maskHash);
636
15.9k
    if (*pmgf1md == NULL)
637
35
        return 0;
638
15.8k
    if (pss->saltLength)
639
113
        *psaltlen = ASN1_INTEGER_get(pss->saltLength);
640
15.7k
    else
641
15.7k
        *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);
642
15.8k
    if (pss->trailerField)
643
24
        *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
644
15.8k
    else
645
15.8k
        *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);
646
647
15.8k
    return 1;
648
15.9k
}
649
650
int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
651
146k
{
652
146k
    RSA_PSS_PARAMS *pss;
653
146k
    const ASN1_OBJECT *algoid;
654
146k
    const void *algp;
655
146k
    int algptype;
656
657
146k
    X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
658
146k
    if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
659
126k
        return 1;
660
20.0k
    if (algptype == V_ASN1_UNDEF)
661
9.23k
        return 1;
662
10.8k
    if (algptype != V_ASN1_SEQUENCE) {
663
591
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
664
591
        return 0;
665
591
    }
666
10.2k
    if ((pss = ossl_rsa_pss_decode(alg)) == NULL
667
10.1k
        || !ossl_rsa_set0_pss_params(rsa, pss)) {
668
79
        RSA_PSS_PARAMS_free(pss);
669
79
        return 0;
670
79
    }
671
10.1k
    if (!ossl_rsa_sync_to_pss_params_30(rsa))
672
29
        return 0;
673
10.1k
    return 1;
674
10.1k
}
675
676
RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
677
    OSSL_LIB_CTX *libctx, const char *propq)
678
21.8k
{
679
21.8k
    const unsigned char *p;
680
21.8k
    RSA *rsa;
681
21.8k
    int pklen;
682
21.8k
    const X509_ALGOR *alg;
683
684
21.8k
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf))
685
0
        return 0;
686
21.8k
    rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
687
21.8k
    if (rsa == NULL) {
688
96
        ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
689
96
        return NULL;
690
96
    }
691
21.7k
    if (!ossl_rsa_param_decode(rsa, alg)) {
692
35
        RSA_free(rsa);
693
35
        return NULL;
694
35
    }
695
696
21.7k
    RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
697
21.7k
    switch (OBJ_obj2nid(alg->algorithm)) {
698
21.6k
    case EVP_PKEY_RSA:
699
21.6k
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
700
21.6k
        break;
701
55
    case EVP_PKEY_RSA_PSS:
702
55
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
703
55
        break;
704
0
    default:
705
        /* Leave the type bits zero */
706
0
        break;
707
21.7k
    }
708
709
21.7k
    return rsa;
710
21.7k
}
711
#endif