Coverage Report

Created: 2026-02-14 07:20

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