Coverage Report

Created: 2026-04-08 06:20

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
0
{
41
0
    const OSSL_PARAM *p = NULL;
42
0
    int i;
43
44
0
    if (numbers == NULL)
45
0
        return 0;
46
47
0
    for (i = 0; names[i] != NULL; i++) {
48
0
        p = OSSL_PARAM_locate_const(params, names[i]);
49
0
        if (p != NULL) {
50
0
            BIGNUM *tmp = NULL;
51
52
0
            if (!OSSL_PARAM_get_BN(p, &tmp))
53
0
                return 0;
54
0
            if (sk_BIGNUM_push(numbers, tmp) == 0) {
55
0
                BN_clear_free(tmp);
56
0
                return 0;
57
0
            }
58
0
        }
59
0
    }
60
61
0
    return 1;
62
0
}
63
64
int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private)
65
0
{
66
0
    const OSSL_PARAM *param_n, *param_e, *param_d = NULL;
67
0
    const OSSL_PARAM *param_derive = NULL;
68
0
    BIGNUM *n = NULL, *e = NULL, *d = NULL;
69
0
    STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
70
0
    int is_private = 0;
71
0
    int derive_from_pq = 0;
72
0
    BN_CTX *ctx = NULL;
73
74
0
    if (rsa == NULL)
75
0
        return 0;
76
77
0
    param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
78
0
    param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
79
80
0
    if ((param_n == NULL || !OSSL_PARAM_get_BN(param_n, &n))
81
0
        || (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
0
    if (include_private) {
87
88
0
        param_derive = OSSL_PARAM_locate_const(params,
89
0
            OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ);
90
0
        if ((param_derive != NULL)
91
0
            && !OSSL_PARAM_get_int(param_derive, &derive_from_pq))
92
0
            goto err;
93
94
0
        param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
95
0
        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
0
        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
0
    }
113
114
0
    is_private = (d != NULL);
115
116
0
    if (!RSA_set0_key(rsa, n, e, d))
117
0
        goto err;
118
0
    n = e = d = NULL;
119
120
0
    if (is_private) {
121
0
        if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
122
0
                ossl_rsa_mp_factor_names)
123
0
            || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
124
0
                ossl_rsa_mp_exp_names)
125
0
            || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
126
0
                ossl_rsa_mp_coeff_names))
127
0
            goto err;
128
129
0
        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
0
        } 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
0
            if (sk_BIGNUM_num(factors) != 0
213
0
                && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs))
214
0
                goto err;
215
0
        }
216
        /* sanity check to ensure we used everything in our stacks */
217
0
        if (sk_BIGNUM_num(factors) != 0
218
0
            || sk_BIGNUM_num(exps) != 0
219
0
            || 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
0
    }
227
228
0
    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 for n-modulus\n");
231
0
        goto err;
232
0
    }
233
234
0
    sk_BIGNUM_free(factors);
235
0
    sk_BIGNUM_free(exps);
236
0
    sk_BIGNUM_free(coeffs);
237
0
    BN_CTX_free(ctx);
238
0
    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
0
}
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
0
{
256
0
    int ret = 0;
257
0
    const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
258
0
    STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
259
0
    STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
260
0
    STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
261
262
0
    if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
263
0
        goto err;
264
265
0
    RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
266
0
    ossl_rsa_get0_all_params(rsa, factors, exps, coeffs);
267
268
0
    if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
269
0
        || !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
0
    if (include_private && rsa_d != NULL) {
274
275
0
        if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D,
276
0
                rsa_d)
277
0
            || !ossl_param_build_set_multi_key_bn(bld, params,
278
0
                ossl_rsa_mp_factor_names,
279
0
                factors)
280
0
            || !ossl_param_build_set_multi_key_bn(bld, params,
281
0
                ossl_rsa_mp_exp_names, exps)
282
0
            || !ossl_param_build_set_multi_key_bn(bld, params,
283
0
                ossl_rsa_mp_coeff_names,
284
0
                coeffs))
285
0
            goto err;
286
0
    }
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
0
    ret = 1;
294
0
err:
295
0
    sk_BIGNUM_const_free(factors);
296
0
    sk_BIGNUM_const_free(exps);
297
0
    sk_BIGNUM_const_free(coeffs);
298
0
    return ret;
299
0
}
300
301
int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss,
302
    OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
303
0
{
304
0
    if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
305
0
        int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss);
306
0
        int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss);
307
0
        int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
308
0
        int saltlen = ossl_rsa_pss_params_30_saltlen(pss);
309
0
        int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL);
310
0
        int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);
311
0
        int default_maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(NULL);
312
0
        const char *mdname = (hashalg_nid == default_hashalg_nid
313
0
                ? NULL
314
0
                : ossl_rsa_oaeppss_nid2name(hashalg_nid));
315
0
        const char *mgfname = (maskgenalg_nid == default_maskgenalg_nid
316
0
                ? NULL
317
0
                : ossl_rsa_oaeppss_nid2name(maskgenalg_nid));
318
0
        const char *mgf1mdname = (maskgenhashalg_nid == default_maskgenhashalg_nid
319
0
                ? NULL
320
0
                : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid));
321
0
        const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST;
322
0
        const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC;
323
0
        const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
324
0
        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
0
        if ((mdname != NULL
332
0
                && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname))
333
0
            || (mgfname != NULL
334
0
                && !ossl_param_build_set_utf8_string(bld, params,
335
0
                    key_mgf, mgfname))
336
0
            || (mgf1mdname != NULL
337
0
                && !ossl_param_build_set_utf8_string(bld, params,
338
0
                    key_mgf1_md, mgf1mdname))
339
0
            || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen)))
340
0
            return 0;
341
0
    }
342
0
    return 1;
343
0
}
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
0
{
350
0
    const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen;
351
0
    const OSSL_PARAM *param_propq;
352
0
    const char *propq = NULL;
353
0
    EVP_MD *md = NULL, *mgf1md = NULL;
354
0
    int saltlen;
355
0
    int ret = 0;
356
357
0
    if (pss_params == NULL)
358
0
        return 0;
359
0
    param_propq = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);
360
0
    param_md = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);
361
0
    param_mgf = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);
362
0
    param_mgf1md = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
363
0
    param_saltlen = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);
364
365
0
    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
0
    if (!*defaults_set
375
0
        && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL
376
0
            || 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
0
    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
0
    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
0
    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
0
    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
0
    ret = 1;
437
438
0
err:
439
0
    EVP_MD_free(md);
440
0
    EVP_MD_free(mgf1md);
441
0
    return ret;
442
0
}
443
444
int ossl_rsa_is_foreign(const RSA *rsa)
445
0
{
446
0
#ifndef FIPS_MODULE
447
0
    if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
448
0
        return 1;
449
0
#endif
450
0
    return 0;
451
0
}
452
453
static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
454
0
{
455
0
    if (f != NULL && (*out = BN_dup(f)) == NULL)
456
0
        return 0;
457
0
    return 1;
458
0
}
459
460
RSA *ossl_rsa_dup(const RSA *rsa, int selection)
461
0
{
462
0
    RSA *dupkey = NULL;
463
0
#ifndef FIPS_MODULE
464
0
    int pnum, i;
465
0
#endif
466
467
    /* Do not try to duplicate foreign RSA keys */
468
0
    if (ossl_rsa_is_foreign(rsa))
469
0
        return NULL;
470
471
0
    if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)
472
0
        return NULL;
473
474
    /* public key */
475
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
476
0
        if (!rsa_bn_dup_check(&dupkey->n, rsa->n))
477
0
            goto err;
478
0
        if (!rsa_bn_dup_check(&dupkey->e, rsa->e))
479
0
            goto err;
480
0
    }
481
482
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
483
484
        /* private key */
485
0
        if (!rsa_bn_dup_check(&dupkey->d, rsa->d))
486
0
            goto err;
487
488
        /* factors and crt params */
489
0
        if (!rsa_bn_dup_check(&dupkey->p, rsa->p))
490
0
            goto err;
491
0
        if (!rsa_bn_dup_check(&dupkey->q, rsa->q))
492
0
            goto err;
493
0
        if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))
494
0
            goto err;
495
0
        if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))
496
0
            goto err;
497
0
        if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))
498
0
            goto err;
499
0
    }
500
501
0
    dupkey->version = rsa->version;
502
0
    dupkey->flags = rsa->flags;
503
    /* we always copy the PSS parameters regardless of selection */
504
0
    dupkey->pss_params = rsa->pss_params;
505
506
0
#ifndef FIPS_MODULE
507
    /* multiprime */
508
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
509
0
        && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) {
510
0
        dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
511
0
        if (dupkey->prime_infos == NULL)
512
0
            goto err;
513
0
        for (i = 0; i < pnum; i++) {
514
0
            const RSA_PRIME_INFO *pinfo = NULL;
515
0
            RSA_PRIME_INFO *duppinfo = NULL;
516
517
0
            if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL)
518
0
                goto err;
519
            /* push first so cleanup in error case works */
520
0
            (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);
521
522
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
523
0
            if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))
524
0
                goto err;
525
0
            if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))
526
0
                goto err;
527
0
            if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))
528
0
                goto err;
529
0
        }
530
0
        if (!ossl_rsa_multip_calc_product(dupkey))
531
0
            goto err;
532
0
    }
533
534
0
    if (rsa->pss != NULL) {
535
0
        dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss);
536
0
        if (rsa->pss->maskGenAlgorithm != NULL
537
0
            && dupkey->pss->maskGenAlgorithm == NULL) {
538
0
            dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);
539
0
            if (dupkey->pss->maskHash == NULL)
540
0
                goto err;
541
0
        }
542
0
    }
543
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,
544
0
            &dupkey->ex_data, &rsa->ex_data))
545
0
        goto err;
546
0
#endif
547
548
0
    return dupkey;
549
550
0
err:
551
0
    RSA_free(dupkey);
552
0
    return NULL;
553
0
}
554
555
#ifndef FIPS_MODULE
556
RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg)
557
0
{
558
0
    RSA_PSS_PARAMS *pss;
559
560
0
    pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
561
0
        alg->parameter);
562
563
0
    if (pss == NULL)
564
0
        return NULL;
565
566
0
    if (pss->maskGenAlgorithm != NULL) {
567
0
        pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
568
0
        if (pss->maskHash == NULL) {
569
0
            RSA_PSS_PARAMS_free(pss);
570
0
            return NULL;
571
0
        }
572
0
    }
573
574
0
    return pss;
575
0
}
576
577
static int ossl_rsa_sync_to_pss_params_30(RSA *rsa)
578
0
{
579
0
    const RSA_PSS_PARAMS *legacy_pss = NULL;
580
0
    RSA_PSS_PARAMS_30 *pss = NULL;
581
582
0
    if (rsa != NULL
583
0
        && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL
584
0
        && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) {
585
0
        const EVP_MD *md = NULL, *mgf1md = NULL;
586
0
        int md_nid, mgf1md_nid, saltlen, trailerField;
587
0
        RSA_PSS_PARAMS_30 pss_params;
588
589
        /*
590
         * We don't care about the validity of the fields here, we just
591
         * want to synchronise values.  Verifying here makes it impossible
592
         * to even read a key with invalid values, making it hard to test
593
         * a bad situation.
594
         *
595
         * Other routines use ossl_rsa_pss_get_param(), so the values will
596
         * be checked, eventually.
597
         */
598
0
        if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md,
599
0
                &saltlen, &trailerField))
600
0
            return 0;
601
0
        md_nid = EVP_MD_get_type(md);
602
0
        mgf1md_nid = EVP_MD_get_type(mgf1md);
603
0
        if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
604
0
            || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
605
0
            || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
606
0
                mgf1md_nid)
607
0
            || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
608
0
            || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,
609
0
                trailerField))
610
0
            return 0;
611
0
        *pss = pss_params;
612
0
    }
613
0
    return 1;
614
0
}
615
616
int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
617
    const EVP_MD **pmd, const EVP_MD **pmgf1md,
618
    int *psaltlen, int *ptrailerField)
619
0
{
620
0
    RSA_PSS_PARAMS_30 pss_params;
621
622
    /* Get the defaults from the ONE place */
623
0
    (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);
624
625
0
    if (pss == NULL)
626
0
        return 0;
627
0
    *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm);
628
0
    if (*pmd == NULL)
629
0
        return 0;
630
0
    *pmgf1md = ossl_x509_algor_get_md(pss->maskHash);
631
0
    if (*pmgf1md == NULL)
632
0
        return 0;
633
0
    if (pss->saltLength)
634
0
        *psaltlen = ASN1_INTEGER_get(pss->saltLength);
635
0
    else
636
0
        *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);
637
0
    if (pss->trailerField)
638
0
        *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
639
0
    else
640
0
        *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);
641
642
0
    return 1;
643
0
}
644
645
int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
646
0
{
647
0
    RSA_PSS_PARAMS *pss;
648
0
    const ASN1_OBJECT *algoid;
649
0
    const void *algp;
650
0
    int algptype;
651
652
0
    X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
653
0
    if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
654
0
        return 1;
655
0
    if (algptype == V_ASN1_UNDEF)
656
0
        return 1;
657
0
    if (algptype != V_ASN1_SEQUENCE) {
658
0
        ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
659
0
        return 0;
660
0
    }
661
0
    if ((pss = ossl_rsa_pss_decode(alg)) == NULL
662
0
        || !ossl_rsa_set0_pss_params(rsa, pss)) {
663
0
        RSA_PSS_PARAMS_free(pss);
664
0
        return 0;
665
0
    }
666
0
    if (!ossl_rsa_sync_to_pss_params_30(rsa))
667
0
        return 0;
668
0
    return 1;
669
0
}
670
671
RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
672
    OSSL_LIB_CTX *libctx, const char *propq)
673
0
{
674
0
    const unsigned char *p;
675
0
    RSA *rsa;
676
0
    int pklen;
677
0
    const X509_ALGOR *alg;
678
679
0
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf))
680
0
        return 0;
681
0
    rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
682
0
    if (rsa == NULL) {
683
0
        ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
684
0
        return NULL;
685
0
    }
686
0
    if (!ossl_rsa_param_decode(rsa, alg)) {
687
0
        RSA_free(rsa);
688
0
        return NULL;
689
0
    }
690
691
0
    RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
692
0
    switch (OBJ_obj2nid(alg->algorithm)) {
693
0
    case EVP_PKEY_RSA:
694
0
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
695
0
        break;
696
0
    case EVP_PKEY_RSA_PSS:
697
0
        RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
698
0
        break;
699
0
    default:
700
        /* Leave the type bits zero */
701
0
        break;
702
0
    }
703
704
0
    return rsa;
705
0
}
706
#endif