Coverage Report

Created: 2026-10-16 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/keymgmt/rsa_kmgmt.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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 <openssl/core_dispatch.h>
17
#include <openssl/core_names.h>
18
#include <openssl/bn.h>
19
#include <openssl/err.h>
20
#include <openssl/rsa.h>
21
#include <openssl/evp.h>
22
#include <openssl/proverr.h>
23
#include "prov/implementations.h"
24
#include "prov/providercommon.h"
25
#include "prov/provider_ctx.h"
26
#include "crypto/rsa.h"
27
#include "crypto/rsa_params.h"
28
#include "crypto/cryptlib.h"
29
#include "internal/fips.h"
30
#include "internal/param_build_set.h"
31
#include "fips/fipsindicator.h"
32
#include "providers/implementations/keymgmt/keymgmtcommon.inc"
33
34
static OSSL_FUNC_keymgmt_new_fn rsa_newdata;
35
static OSSL_FUNC_keymgmt_new_ex_fn rsa_newdata_ex;
36
static OSSL_FUNC_keymgmt_new_fn rsapss_newdata;
37
static OSSL_FUNC_keymgmt_new_ex_fn rsapss_newdata_ex;
38
static OSSL_FUNC_keymgmt_gen_init_fn rsa_gen_init;
39
static OSSL_FUNC_keymgmt_gen_init_fn rsapss_gen_init;
40
static OSSL_FUNC_keymgmt_gen_set_params_fn rsa_gen_set_params;
41
static OSSL_FUNC_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
42
static OSSL_FUNC_keymgmt_gen_settable_params_fn rsapss_gen_settable_params;
43
#ifdef FIPS_MODULE
44
static OSSL_FUNC_keymgmt_gen_get_params_fn rsa_gen_get_params;
45
#endif
46
static OSSL_FUNC_keymgmt_gen_fn rsa_gen;
47
static OSSL_FUNC_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
48
static OSSL_FUNC_keymgmt_load_fn rsa_load;
49
static OSSL_FUNC_keymgmt_load_fn rsapss_load;
50
static OSSL_FUNC_keymgmt_free_fn rsa_freedata;
51
static OSSL_FUNC_keymgmt_get_params_fn rsa_get_params;
52
static OSSL_FUNC_keymgmt_get_params_fn rsapss_get_params;
53
static OSSL_FUNC_keymgmt_gettable_params_fn rsa_gettable_params;
54
static OSSL_FUNC_keymgmt_gettable_params_fn rsapss_gettable_params;
55
static OSSL_FUNC_keymgmt_has_fn rsa_has;
56
static OSSL_FUNC_keymgmt_match_fn rsa_match;
57
static OSSL_FUNC_keymgmt_validate_fn rsa_validate;
58
static OSSL_FUNC_keymgmt_import_fn rsa_import;
59
static OSSL_FUNC_keymgmt_import_types_fn rsa_import_types;
60
static OSSL_FUNC_keymgmt_import_types_fn rsapss_import_types;
61
static OSSL_FUNC_keymgmt_export_fn rsa_export;
62
static OSSL_FUNC_keymgmt_export_types_fn rsa_export_types;
63
static OSSL_FUNC_keymgmt_export_types_fn rsapss_export_types;
64
static OSSL_FUNC_keymgmt_query_operation_name_fn rsa_query_operation_name;
65
static OSSL_FUNC_keymgmt_dup_fn rsa_dup;
66
67
0
#define RSA_DEFAULT_MD "SHA256"
68
#define RSA_POSSIBLE_SELECTIONS \
69
0
    (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
70
71
DEFINE_STACK_OF(BIGNUM)
72
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
73
74
static int pss_params_fromdata(RSA_PSS_PARAMS_30 *pss_params, int *defaults_set,
75
    const RSA_PARAMS *p, int rsa_type,
76
    OSSL_LIB_CTX *libctx)
77
0
{
78
0
    if (!ossl_rsa_pss_params_30_fromdata_parsed(pss_params, defaults_set,
79
0
            p, libctx))
80
0
        return 0;
81
82
    /* If not a PSS type RSA, sending us PSS parameters is wrong */
83
0
    if (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
84
0
        && !ossl_rsa_pss_params_30_is_unrestricted(pss_params))
85
0
        return 0;
86
87
0
    return 1;
88
0
}
89
90
/*
91
 * If the application actually created a legacy RSA object and assigned it to
92
 * the EVP_PKEY, then we get hold of that object here. We return 0 if we hit
93
 * a fatal error or 1 otherwise. We may return 1 but with *rsa set to NULL.
94
 */
95
static int get_legacy_rsa_object(OSSL_LIB_CTX *libctx, RSA **rsa, const OSSL_PARAM params[])
96
0
{
97
0
#ifndef FIPS_MODULE
98
0
    const OSSL_PARAM *p;
99
100
0
    if (params == NULL)
101
0
        return 1;
102
0
    p = OSSL_PARAM_locate_const(params, "legacy-object");
103
0
    if (p == NULL)
104
0
        return 1;
105
    /*
106
     * This only works because we are in the default provider. We are not
107
     * normally allowed to pass complex objects across the provider boundary
108
     * like this.
109
     */
110
0
    if (OSSL_PARAM_get_octet_ptr(p, (const void **)rsa, NULL) && *rsa != NULL) {
111
0
        if (ossl_lib_ctx_get_concrete(ossl_rsa_get0_libctx(*rsa)) != ossl_lib_ctx_get_concrete(libctx)) {
112
0
            *rsa = NULL;
113
0
            return 1;
114
0
        }
115
0
        if (!RSA_up_ref(*rsa))
116
0
            return 0;
117
0
    }
118
0
#endif
119
120
0
    return 1;
121
0
}
122
123
static void *rsa_newdata_ex(void *provctx, const OSSL_PARAM params[])
124
0
{
125
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
126
0
    RSA *rsa = NULL;
127
128
0
    if (!ossl_prov_is_running())
129
0
        return NULL;
130
131
0
    if (!get_legacy_rsa_object(libctx, &rsa, params))
132
0
        return NULL;
133
134
0
    if (rsa == NULL) {
135
0
        rsa = ossl_rsa_new_with_ctx(libctx);
136
0
        if (rsa != NULL) {
137
0
            RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
138
0
            RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
139
0
        }
140
0
    }
141
142
0
    return rsa;
143
0
}
144
145
static void *rsa_newdata(void *provctx)
146
0
{
147
0
    return rsa_newdata_ex(provctx, NULL);
148
0
}
149
150
static void *rsapss_newdata_ex(void *provctx, const OSSL_PARAM params[])
151
0
{
152
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
153
0
    RSA *rsa = NULL;
154
155
0
    if (!ossl_prov_is_running())
156
0
        return NULL;
157
158
0
    if (!get_legacy_rsa_object(libctx, &rsa, params))
159
0
        return NULL;
160
161
0
    if (rsa == NULL) {
162
0
        rsa = ossl_rsa_new_with_ctx(libctx);
163
0
        if (rsa != NULL) {
164
0
            RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
165
0
            RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
166
0
        }
167
0
    }
168
169
0
    return rsa;
170
0
}
171
172
static void *rsapss_newdata(void *provctx)
173
0
{
174
0
    return rsapss_newdata_ex(provctx, NULL);
175
0
}
176
177
static void rsa_freedata(void *keydata)
178
0
{
179
0
    RSA_free(keydata);
180
0
}
181
182
static int rsa_has(const void *keydata, int selection)
183
0
{
184
0
    const RSA *rsa = keydata;
185
0
    int ok = 1;
186
187
0
    if (rsa == NULL || !ossl_prov_is_running())
188
0
        return 0;
189
0
    if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
190
0
        return 1; /* the selection is not missing */
191
192
    /* OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS are always available even if empty */
193
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
194
0
        ok = ok && (RSA_get0_n(rsa) != NULL);
195
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
196
0
        ok = ok && (RSA_get0_e(rsa) != NULL);
197
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
198
0
        ok = ok && (RSA_get0_d(rsa) != NULL);
199
0
    return ok;
200
0
}
201
202
static int rsa_match(const void *keydata1, const void *keydata2, int selection)
203
0
{
204
0
    const RSA *rsa1 = keydata1;
205
0
    const RSA *rsa2 = keydata2;
206
0
    int ok = 1;
207
208
0
    if (!ossl_prov_is_running())
209
0
        return 0;
210
211
    /* There is always an |e| */
212
0
    ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0;
213
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
214
0
        int key_checked = 0;
215
216
0
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
217
0
            const BIGNUM *pa = RSA_get0_n(rsa1);
218
0
            const BIGNUM *pb = RSA_get0_n(rsa2);
219
220
0
            if (pa != NULL && pb != NULL) {
221
0
                ok = ok && BN_cmp(pa, pb) == 0;
222
0
                key_checked = 1;
223
0
            }
224
0
        }
225
0
        if (!key_checked
226
0
            && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
227
0
            const BIGNUM *pa = RSA_get0_d(rsa1);
228
0
            const BIGNUM *pb = RSA_get0_d(rsa2);
229
230
0
            if (pa != NULL && pb != NULL) {
231
0
                ok = ok && BN_cmp(pa, pb) == 0;
232
0
                key_checked = 1;
233
0
            }
234
0
        }
235
0
        ok = ok && key_checked;
236
0
    }
237
0
    return ok;
238
0
}
239
240
enum {
241
    RSA_IMEXPORT_TYPE_NONE = 0,
242
    RSA_IMEXPORT_TYPE_OTHER = 1,
243
    RSA_IMEXPORT_TYPE_KEY = 2,
244
    RSA_IMEXPORT_TYPE_ALL = RSA_IMEXPORT_TYPE_OTHER | RSA_IMEXPORT_TYPE_KEY,
245
    RSA_IMEXPORT_TYPE_COUNT
246
};
247
248
typedef int (*rsa_import_types_decoder_fn)(const OSSL_PARAM *, RSA_PARAMS *);
249
250
struct rsa_imexport_types_st {
251
    const OSSL_PARAM *import_types;
252
    rsa_import_types_decoder_fn import_decoder;
253
    const OSSL_PARAM *export_types;
254
};
255
256
static const struct rsa_imexport_types_st
257
    rsa_imexport_types[RSA_IMEXPORT_TYPE_COUNT]
258
    = {
259
          [RSA_IMEXPORT_TYPE_OTHER] = {
260
              rsa_other_import_types_list,
261
              rsa_other_import_types_decoder,
262
              rsa_other_export_types_list,
263
          },
264
          [RSA_IMEXPORT_TYPE_KEY] = {
265
              rsa_key_import_types_list,
266
              rsa_key_import_types_decoder,
267
              rsa_key_export_types_list,
268
          },
269
          [RSA_IMEXPORT_TYPE_ALL] = {
270
              rsa_all_import_types_list,
271
              rsa_all_import_types_decoder,
272
              rsa_all_export_types_list,
273
          },
274
      };
275
276
static const struct rsa_imexport_types_st
277
    rsapss_imexport_types[RSA_IMEXPORT_TYPE_COUNT]
278
    = {
279
          [RSA_IMEXPORT_TYPE_OTHER] = {
280
              rsapss_other_import_types_list,
281
              rsapss_other_import_types_decoder,
282
              rsapss_other_export_types_list,
283
          },
284
          [RSA_IMEXPORT_TYPE_KEY] = {
285
              rsapss_key_import_types_list,
286
              rsapss_key_import_types_decoder,
287
              rsapss_key_export_types_list,
288
          },
289
          [RSA_IMEXPORT_TYPE_ALL] = {
290
              rsapss_all_import_types_list,
291
              rsapss_all_import_types_decoder,
292
              rsapss_all_export_types_list,
293
          },
294
      };
295
296
static int rsa_imexport_type_select(int selection)
297
0
{
298
0
    int type_select = RSA_IMEXPORT_TYPE_NONE;
299
300
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
301
0
        type_select |= RSA_IMEXPORT_TYPE_OTHER;
302
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
303
0
        type_select |= RSA_IMEXPORT_TYPE_KEY;
304
0
    return type_select;
305
0
}
306
307
static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
308
0
{
309
0
    RSA *rsa = keydata;
310
0
    RSA_PARAMS p;
311
0
    const struct rsa_imexport_types_st *types;
312
0
    int type_select;
313
0
    int rsa_type;
314
0
    int ok = 1;
315
0
    int pss_defaults_set = 0;
316
317
0
    if (!ossl_prov_is_running() || rsa == NULL)
318
0
        return 0;
319
320
0
    type_select = rsa_imexport_type_select(selection);
321
0
    rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
322
0
    types = rsa_type == RSA_FLAG_TYPE_RSASSAPSS
323
0
        ? rsapss_imexport_types
324
0
        : rsa_imexport_types;
325
0
    if (type_select == RSA_IMEXPORT_TYPE_NONE
326
0
        || !types[type_select].import_decoder(params, &p))
327
0
        return 0;
328
329
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
330
0
        ok = ok && pss_params_fromdata(ossl_rsa_get0_pss_params_30(rsa), &pss_defaults_set, &p, rsa_type, ossl_rsa_get0_libctx(rsa));
331
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
332
0
        int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
333
334
0
        ok = ok && ossl_rsa_fromdata_parsed(rsa, &p, include_private);
335
0
    }
336
337
0
    return ok;
338
0
}
339
340
static int rsa_export(void *keydata, int selection,
341
    OSSL_CALLBACK *param_callback, void *cbarg)
342
0
{
343
0
    RSA *rsa = keydata;
344
0
    const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30(rsa);
345
0
    OSSL_PARAM_BLD *tmpl;
346
0
    OSSL_PARAM *params = NULL;
347
0
    int type_select;
348
0
    int ok = 1;
349
350
0
    if (!ossl_prov_is_running() || rsa == NULL)
351
0
        return 0;
352
353
0
    type_select = rsa_imexport_type_select(selection);
354
0
    if (type_select == RSA_IMEXPORT_TYPE_NONE)
355
0
        return 0;
356
357
0
    tmpl = OSSL_PARAM_BLD_new();
358
0
    if (tmpl == NULL)
359
0
        return 0;
360
361
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
362
0
        ok = ok && (ossl_rsa_pss_params_30_is_unrestricted(pss_params) || ossl_rsa_pss_params_30_todata(pss_params, tmpl, NULL));
363
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
364
0
        int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
365
366
0
        ok = ok && ossl_rsa_todata(rsa, tmpl, NULL, include_private);
367
0
    }
368
369
0
    if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
370
0
        ok = 0;
371
0
        goto err;
372
0
    }
373
374
0
    ok = param_callback(params, cbarg);
375
0
    OSSL_PARAM_clear_free(params);
376
0
err:
377
0
    OSSL_PARAM_BLD_free(tmpl);
378
0
    return ok;
379
0
}
380
381
static const OSSL_PARAM *rsa_import_types(int selection)
382
0
{
383
0
    return rsa_imexport_types[rsa_imexport_type_select(selection)].import_types;
384
0
}
385
386
static const OSSL_PARAM *rsapss_import_types(int selection)
387
0
{
388
0
    return rsapss_imexport_types[rsa_imexport_type_select(selection)]
389
0
        .import_types;
390
0
}
391
392
static const OSSL_PARAM *rsa_export_types(int selection)
393
0
{
394
0
    return rsa_imexport_types[rsa_imexport_type_select(selection)].export_types;
395
0
}
396
397
static const OSSL_PARAM *rsapss_export_types(int selection)
398
0
{
399
0
    return rsapss_imexport_types[rsa_imexport_type_select(selection)]
400
0
        .export_types;
401
0
}
402
403
typedef int (*rsa_get_params_decoder_fn)(const OSSL_PARAM *, RSA_PARAMS *);
404
405
static int common_get_params(void *key, OSSL_PARAM params[],
406
    rsa_get_params_decoder_fn decoder)
407
0
{
408
0
    RSA *rsa = key;
409
0
    RSA_PARAMS p;
410
0
    const RSA_PSS_PARAMS_30 *pss_params;
411
0
    int rsa_type, empty;
412
413
0
    if (rsa == NULL || !decoder(params, &p))
414
0
        return 0;
415
0
    pss_params = ossl_rsa_get0_pss_params_30(rsa);
416
0
    rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
417
0
    empty = RSA_get0_n(rsa) == NULL;
418
419
0
    if (p.bits != NULL && (empty || !OSSL_PARAM_set_int(p.bits, RSA_bits(rsa))))
420
0
        return 0;
421
0
    if (p.secbits != NULL
422
0
        && (empty || !OSSL_PARAM_set_int(p.secbits, RSA_security_bits(rsa))))
423
0
        return 0;
424
0
    if (p.maxsize != NULL
425
0
        && (empty || !OSSL_PARAM_set_int(p.maxsize, RSA_size(rsa))))
426
0
        return 0;
427
0
    if (p.seccat != NULL)
428
0
        if (!OSSL_PARAM_set_int(p.seccat, 0))
429
0
            return 0;
430
431
    /*
432
     * For restricted RSA-PSS keys, we ignore the default digest request.
433
     * With RSA-OAEP keys, this may need to be amended.
434
     */
435
0
    if (p.default_digest != NULL
436
0
        && (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
437
0
            || ossl_rsa_pss_params_30_is_unrestricted(pss_params))) {
438
0
        if (!OSSL_PARAM_set_utf8_string(p.default_digest, RSA_DEFAULT_MD))
439
0
            return 0;
440
0
    }
441
442
    /*
443
     * For non-RSA-PSS keys, we ignore the mandatory digest request.
444
     * With RSA-OAEP keys, this may need to be amended.
445
     */
446
0
    if (p.mandatory_digest != NULL
447
0
        && rsa_type == RSA_FLAG_TYPE_RSASSAPSS
448
0
        && !ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
449
0
        const char *mdname = ossl_rsa_oaeppss_nid2name(ossl_rsa_pss_params_30_hashalg(pss_params));
450
451
0
        if (mdname == NULL
452
0
            || !OSSL_PARAM_set_utf8_string(p.mandatory_digest, mdname))
453
0
            return 0;
454
0
    }
455
0
    return (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
456
0
               || ossl_rsa_pss_params_30_todata_parsed(pss_params, NULL, &p))
457
0
        && ossl_rsa_todata_parsed(rsa, NULL, &p, 1);
458
0
}
459
460
static int rsa_get_params(void *key, OSSL_PARAM params[])
461
0
{
462
0
    return common_get_params(key, params, rsa_get_params_decoder);
463
0
}
464
465
static int rsapss_get_params(void *key, OSSL_PARAM params[])
466
0
{
467
0
    return common_get_params(key, params, rsapss_get_params_decoder);
468
0
}
469
470
static const OSSL_PARAM *rsa_gettable_params(void *provctx)
471
0
{
472
0
    return rsa_get_params_list;
473
0
}
474
475
static const OSSL_PARAM *rsapss_gettable_params(void *provctx)
476
0
{
477
0
    return rsapss_get_params_list;
478
0
}
479
480
static int rsa_validate(const void *keydata, int selection, int checktype)
481
0
{
482
0
    const RSA *rsa = keydata;
483
0
    int ok = 1;
484
485
0
    if (!ossl_prov_is_running())
486
0
        return 0;
487
488
0
    if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
489
0
        return 1; /* nothing to validate */
490
491
    /* If the whole key is selected, we do a pairwise validation */
492
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
493
0
        == OSSL_KEYMGMT_SELECT_KEYPAIR) {
494
0
        ok = ok && ossl_rsa_validate_pairwise(rsa);
495
0
    } else {
496
0
        if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
497
0
            ok = ok && ossl_rsa_validate_private(rsa);
498
0
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
499
0
            ok = ok && ossl_rsa_validate_public(rsa);
500
0
    }
501
0
    return ok;
502
0
}
503
504
struct rsa_gen_ctx {
505
    OSSL_LIB_CTX *libctx;
506
    const char *propq;
507
508
    int rsa_type;
509
510
    size_t nbits;
511
    BIGNUM *pub_exp;
512
    size_t primes;
513
514
    /* For PSS */
515
    RSA_PSS_PARAMS_30 pss_params;
516
    int pss_defaults_set;
517
518
    /* For generation callback */
519
    OSSL_CALLBACK *cb;
520
    void *cbarg;
521
522
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
523
    /* ACVP test parameters */
524
    OSSL_PARAM *acvp_test_params;
525
#endif
526
    uint32_t a, b;
527
};
528
529
static int rsa_gencb(int p, int n, BN_GENCB *cb)
530
0
{
531
0
    struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
532
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
533
534
0
    params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
535
0
    params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
536
0
    return gctx->cb(params, gctx->cbarg);
537
0
}
538
539
static void *gen_init(void *provctx, int selection, int rsa_type,
540
    const OSSL_PARAM params[])
541
0
{
542
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
543
0
    struct rsa_gen_ctx *gctx = NULL;
544
545
0
    if (!ossl_prov_is_running())
546
0
        return NULL;
547
548
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
549
0
        return NULL;
550
551
0
    if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
552
0
        gctx->libctx = libctx;
553
0
        if ((gctx->pub_exp = BN_new()) == NULL
554
0
            || !BN_set_word(gctx->pub_exp, RSA_F4)) {
555
0
            goto err;
556
0
        }
557
0
        gctx->nbits = 2048;
558
0
        gctx->primes = RSA_DEFAULT_PRIME_NUM;
559
0
        gctx->rsa_type = rsa_type;
560
0
    } else {
561
0
        goto err;
562
0
    }
563
564
0
    if (!rsa_gen_set_params(gctx, params))
565
0
        goto err;
566
0
    return gctx;
567
568
0
err:
569
0
    if (gctx != NULL)
570
0
        BN_free(gctx->pub_exp);
571
0
    OPENSSL_free(gctx);
572
0
    return NULL;
573
0
}
574
575
static void *rsa_gen_init(void *provctx, int selection,
576
    const OSSL_PARAM params[])
577
0
{
578
0
    return gen_init(provctx, selection, RSA_FLAG_TYPE_RSA, params);
579
0
}
580
581
static void *rsapss_gen_init(void *provctx, int selection,
582
    const OSSL_PARAM params[])
583
0
{
584
0
    return gen_init(provctx, selection, RSA_FLAG_TYPE_RSASSAPSS, params);
585
0
}
586
587
/*
588
 * This function is common for all RSA sub-types, to detect possible
589
 * misuse, such as PSS parameters being passed when a plain RSA key
590
 * is generated.
591
 */
592
static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
593
0
{
594
0
    struct rsa_gen_ctx *gctx = genctx;
595
0
    RSA_PARAMS p;
596
597
0
    if (gctx == NULL)
598
0
        return 0;
599
0
    switch (gctx->rsa_type) {
600
0
    case RSA_FLAG_TYPE_RSA:
601
0
        if (!rsa_gen_set_params_decoder(params, &p))
602
0
            return 0;
603
0
        break;
604
0
    case RSA_FLAG_TYPE_RSASSAPSS:
605
0
        if (!rsapss_gen_set_params_decoder(params, &p))
606
0
            return 0;
607
0
        break;
608
0
    default:
609
0
        return 0;
610
0
    }
611
612
0
    if (p.bits != NULL) {
613
0
        if (!OSSL_PARAM_get_size_t(p.bits, &gctx->nbits))
614
0
            return 0;
615
0
        if (gctx->nbits < RSA_MIN_MODULUS_BITS) {
616
0
            ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
617
0
            return 0;
618
0
        }
619
0
    }
620
0
    if (p.primes != NULL && !OSSL_PARAM_get_size_t(p.primes, &gctx->primes))
621
0
        return 0;
622
0
    if (p.e != NULL && !OSSL_PARAM_get_BN(p.e, &gctx->pub_exp))
623
0
        return 0;
624
0
    if (p.a != NULL) {
625
0
        if (!OSSL_PARAM_get_uint32(p.a, &gctx->a))
626
0
            return 0;
627
        /* a is an optional value that should be one of (0, 1, 3, 5, 7) */
628
0
        if (gctx->a != 0 && (gctx->a > 7 || (gctx->a & 1) == 0)) {
629
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
630
0
            return 0;
631
0
        }
632
0
    }
633
0
    if (p.b != NULL) {
634
0
        if (!OSSL_PARAM_get_uint32(p.b, &gctx->b))
635
0
            return 0;
636
        /* b is an optional value that should be one of (0, 1, 3, 5, 7) */
637
0
        if (gctx->b != 0 && (gctx->b > 7 || (gctx->b & 1) == 0)) {
638
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
639
0
            return 0;
640
0
        }
641
0
    }
642
643
    /* Only attempt to get PSS parameters when generating an RSA-PSS key */
644
0
    if (gctx->rsa_type == RSA_FLAG_TYPE_RSASSAPSS
645
0
        && !pss_params_fromdata(&gctx->pss_params, &gctx->pss_defaults_set, &p,
646
0
            gctx->rsa_type, gctx->libctx))
647
0
        return 0;
648
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
649
    /* Any ACVP test related parameters are copied into a params[] */
650
    if (!ossl_rsa_acvp_test_gen_params_new_parsed(&gctx->acvp_test_params, &p))
651
        return 0;
652
#endif
653
0
    return 1;
654
0
}
655
656
static const OSSL_PARAM *rsa_gen_settable_params(ossl_unused void *genctx,
657
    ossl_unused void *provctx)
658
0
{
659
0
    return rsa_gen_set_params_list;
660
0
}
661
662
static const OSSL_PARAM *rsapss_gen_settable_params(ossl_unused void *genctx,
663
    ossl_unused void *provctx)
664
0
{
665
0
    return rsapss_gen_set_params_list;
666
0
}
667
668
#ifdef FIPS_MODULE
669
static int rsa_gen_get_params(void *genctx, OSSL_PARAM params[])
670
{
671
    struct rsa_gen_ctx *gctx = genctx;
672
    struct keymgmt_fips_gen_get_params_st p;
673
    int approved = 1;
674
675
    if (gctx == NULL || !keymgmt_fips_gen_get_params_decoder(params, &p))
676
        return 0;
677
#ifndef OPENSSL_NO_ACVP_TESTS
678
    approved = gctx->acvp_test_params == NULL;
679
#endif
680
    if (!OSSL_FIPS_IND_GET_PARAM_CONDITIONAL(p.ind, approved))
681
        return 0;
682
    return 1;
683
}
684
#endif
685
686
static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
687
0
{
688
0
    struct rsa_gen_ctx *gctx = genctx;
689
0
    RSA *rsa = NULL, *rsa_tmp = NULL;
690
0
    BN_GENCB *gencb = NULL;
691
692
0
    if (!ossl_prov_is_running() || gctx == NULL)
693
0
        return NULL;
694
695
0
    switch (gctx->rsa_type) {
696
0
    case RSA_FLAG_TYPE_RSA:
697
        /* For plain RSA keys, PSS parameters must not be set */
698
0
        if (!ossl_rsa_pss_params_30_is_unrestricted(&gctx->pss_params))
699
0
            goto err;
700
0
        break;
701
0
    case RSA_FLAG_TYPE_RSASSAPSS:
702
        /*
703
         * For plain RSA-PSS keys, PSS parameters may be set but don't have
704
         * to, so not check.
705
         */
706
0
        break;
707
0
    default:
708
        /* Unsupported RSA key sub-type... */
709
0
        return NULL;
710
0
    }
711
712
0
    if ((rsa_tmp = ossl_rsa_new_with_ctx(gctx->libctx)) == NULL)
713
0
        return NULL;
714
715
0
    gctx->cb = osslcb;
716
0
    gctx->cbarg = cbarg;
717
0
    gencb = BN_GENCB_new();
718
0
    if (gencb != NULL)
719
0
        BN_GENCB_set(gencb, rsa_gencb, genctx);
720
721
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
722
    if (gctx->acvp_test_params != NULL) {
723
        if (!ossl_rsa_acvp_test_set_params(rsa_tmp, gctx->acvp_test_params))
724
            goto err;
725
    }
726
#endif
727
728
0
    if (!ossl_rsa_generate_multi_prime_key(rsa_tmp,
729
0
            (int)gctx->nbits, (int)gctx->primes,
730
0
            gctx->pub_exp, gencb,
731
0
            gctx->a, gctx->b))
732
0
        goto err;
733
734
0
    if (!ossl_rsa_pss_params_30_copy(ossl_rsa_get0_pss_params_30(rsa_tmp),
735
0
            &gctx->pss_params))
736
0
        goto err;
737
738
0
    RSA_clear_flags(rsa_tmp, RSA_FLAG_TYPE_MASK);
739
0
    RSA_set_flags(rsa_tmp, gctx->rsa_type);
740
741
0
    rsa = rsa_tmp;
742
0
    rsa_tmp = NULL;
743
0
err:
744
0
    BN_GENCB_free(gencb);
745
0
    RSA_free(rsa_tmp);
746
0
    return rsa;
747
0
}
748
749
static void rsa_gen_cleanup(void *genctx)
750
0
{
751
0
    struct rsa_gen_ctx *gctx = genctx;
752
753
0
    if (gctx == NULL)
754
0
        return;
755
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
756
    ossl_rsa_acvp_test_gen_params_free(gctx->acvp_test_params);
757
    gctx->acvp_test_params = NULL;
758
#endif
759
0
    BN_clear_free(gctx->pub_exp);
760
0
    OPENSSL_free(gctx);
761
0
}
762
763
static void *common_load(const void *reference, size_t reference_sz,
764
    int expected_rsa_type)
765
0
{
766
0
    RSA *rsa = NULL;
767
768
0
    if (ossl_prov_is_running() && reference_sz == sizeof(rsa)) {
769
        /* The contents of the reference is the address to our object */
770
0
        rsa = *(RSA **)reference;
771
772
0
        if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != expected_rsa_type)
773
0
            return NULL;
774
775
        /* We grabbed, so we detach it */
776
0
        *(RSA **)reference = NULL;
777
0
        return rsa;
778
0
    }
779
0
    return NULL;
780
0
}
781
782
static void *rsa_load(const void *reference, size_t reference_sz)
783
0
{
784
0
    return common_load(reference, reference_sz, RSA_FLAG_TYPE_RSA);
785
0
}
786
787
static void *rsapss_load(const void *reference, size_t reference_sz)
788
0
{
789
0
    return common_load(reference, reference_sz, RSA_FLAG_TYPE_RSASSAPSS);
790
0
}
791
792
static void *rsa_dup(const void *keydata_from, int selection)
793
0
{
794
0
    if (ossl_prov_is_running()
795
        /* do not allow creating empty keys by duplication */
796
0
        && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
797
0
        return ossl_rsa_dup(keydata_from, selection);
798
0
    return NULL;
799
0
}
800
801
/* For any RSA key, we use the "RSA" algorithms regardless of sub-type. */
802
static const char *rsa_query_operation_name(int operation_id)
803
0
{
804
0
    return "RSA";
805
0
}
806
807
const OSSL_DISPATCH ossl_rsa_keymgmt_functions[] = {
808
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
809
    { OSSL_FUNC_KEYMGMT_NEW_EX, (void (*)(void))rsa_newdata_ex },
810
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
811
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
812
        (void (*)(void))rsa_gen_set_params },
813
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
814
        (void (*)(void))rsa_gen_settable_params },
815
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
816
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
817
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))rsa_load },
818
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
819
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))rsa_get_params },
820
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))rsa_gettable_params },
821
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
822
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
823
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
824
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
825
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
826
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
827
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
828
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))rsa_dup },
829
    OSSL_KEYMGMT_FIPS_GEN_DISPATCH_END(rsa_gen_get_params)
830
};
831
832
const OSSL_DISPATCH ossl_rsapss_keymgmt_functions[] = {
833
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsapss_newdata },
834
    { OSSL_FUNC_KEYMGMT_NEW_EX, (void (*)(void))rsapss_newdata_ex },
835
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsapss_gen_init },
836
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))rsa_gen_set_params },
837
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
838
        (void (*)(void))rsapss_gen_settable_params },
839
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
840
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
841
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))rsapss_load },
842
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
843
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))rsapss_get_params },
844
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS,
845
        (void (*)(void))rsapss_gettable_params },
846
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
847
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
848
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
849
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
850
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsapss_import_types },
851
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
852
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsapss_export_types },
853
    { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
854
        (void (*)(void))rsa_query_operation_name },
855
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))rsa_dup },
856
    OSSL_KEYMGMT_FIPS_GEN_DISPATCH_END(rsa_gen_get_params)
857
};