Coverage Report

Created: 2026-09-17 06:47

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