Coverage Report

Created: 2026-02-22 06:11

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