VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/providers/implementations/keymgmt/rsa_kmgmt.c@ 94403

Last change on this file since 94403 was 94320, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: Export to OSE and fix copyright headers in Makefiles, bugref:10128

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette