]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2019-2020 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_numbers.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 "prov/implementations.h" | |
23 | #include "prov/providercommon.h" | |
24 | #include "prov/provider_ctx.h" | |
25 | #include "crypto/rsa.h" | |
26 | #include "internal/param_build_set.h" | |
27 | ||
28 | static OSSL_OP_keymgmt_new_fn rsa_newdata; | |
29 | static OSSL_OP_keymgmt_new_fn rsapss_newdata; | |
30 | static OSSL_OP_keymgmt_gen_init_fn rsa_gen_init; | |
31 | static OSSL_OP_keymgmt_gen_init_fn rsapss_gen_init; | |
32 | static OSSL_OP_keymgmt_gen_set_params_fn rsa_gen_set_params; | |
33 | static OSSL_OP_keymgmt_gen_settable_params_fn rsa_gen_settable_params; | |
34 | static OSSL_OP_keymgmt_gen_settable_params_fn rsapss_gen_settable_params; | |
35 | static OSSL_OP_keymgmt_gen_fn rsa_gen; | |
36 | static OSSL_OP_keymgmt_gen_cleanup_fn rsa_gen_cleanup; | |
37 | static OSSL_OP_keymgmt_free_fn rsa_freedata; | |
38 | static OSSL_OP_keymgmt_get_params_fn rsa_get_params; | |
39 | static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params; | |
40 | static OSSL_OP_keymgmt_has_fn rsa_has; | |
41 | static OSSL_OP_keymgmt_match_fn rsa_match; | |
42 | static OSSL_OP_keymgmt_validate_fn rsa_validate; | |
43 | static OSSL_OP_keymgmt_import_fn rsa_import; | |
44 | static OSSL_OP_keymgmt_import_types_fn rsa_import_types; | |
45 | static OSSL_OP_keymgmt_export_fn rsa_export; | |
46 | static OSSL_OP_keymgmt_export_types_fn rsa_export_types; | |
47 | static OSSL_OP_keymgmt_query_operation_name_fn rsapss_query_operation_name; | |
48 | ||
49 | #define RSA_DEFAULT_MD "SHA256" | |
50 | #define RSA_PSS_DEFAULT_MD OSSL_DIGEST_NAME_SHA1 | |
51 | #define RSA_POSSIBLE_SELECTIONS \ | |
52 | (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) | |
53 | ||
54 | DEFINE_STACK_OF(BIGNUM) | |
55 | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) | |
56 | ||
57 | static int pss_params_fromdata(RSA_PSS_PARAMS_30 *pss_params, | |
58 | const OSSL_PARAM params[], int rsa_type, | |
59 | OPENSSL_CTX *libctx) | |
60 | { | |
61 | if (!rsa_pss_params_30_fromdata(pss_params, params, libctx)) | |
62 | return 0; | |
63 | ||
64 | /* If not a PSS type RSA, sending us PSS parameters is wrong */ | |
65 | if (rsa_type != RSA_FLAG_TYPE_RSASSAPSS | |
66 | && !rsa_pss_params_30_is_unrestricted(pss_params)) | |
67 | return 0; | |
68 | ||
69 | return 1; | |
70 | } | |
71 | ||
72 | static void *rsa_newdata(void *provctx) | |
73 | { | |
74 | OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
75 | RSA *rsa = rsa_new_with_ctx(libctx); | |
76 | ||
77 | if (rsa != NULL) { | |
78 | RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK); | |
79 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA); | |
80 | } | |
81 | return rsa; | |
82 | } | |
83 | ||
84 | static void *rsapss_newdata(void *provctx) | |
85 | { | |
86 | OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
87 | RSA *rsa = rsa_new_with_ctx(libctx); | |
88 | ||
89 | if (rsa != NULL) { | |
90 | RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK); | |
91 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS); | |
92 | } | |
93 | return rsa; | |
94 | } | |
95 | ||
96 | static void rsa_freedata(void *keydata) | |
97 | { | |
98 | RSA_free(keydata); | |
99 | } | |
100 | ||
101 | static int rsa_has(void *keydata, int selection) | |
102 | { | |
103 | RSA *rsa = keydata; | |
104 | int ok = 0; | |
105 | ||
106 | if (rsa != NULL) { | |
107 | if ((selection & RSA_POSSIBLE_SELECTIONS) != 0) | |
108 | ok = 1; | |
109 | ||
110 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) | |
111 | /* This will change with OAEP */ | |
112 | ok = ok && (RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS) != 0); | |
113 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
114 | ok = ok && (RSA_get0_e(rsa) != NULL); | |
115 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
116 | ok = ok && (RSA_get0_n(rsa) != NULL); | |
117 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
118 | ok = ok && (RSA_get0_d(rsa) != NULL); | |
119 | } | |
120 | return ok; | |
121 | } | |
122 | ||
123 | static int rsa_match(const void *keydata1, const void *keydata2, int selection) | |
124 | { | |
125 | const RSA *rsa1 = keydata1; | |
126 | const RSA *rsa2 = keydata2; | |
127 | int ok = 1; | |
128 | ||
129 | /* There is always an |e| */ | |
130 | ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0; | |
131 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
132 | ok = ok && BN_cmp(RSA_get0_n(rsa1), RSA_get0_n(rsa2)) == 0; | |
133 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
134 | ok = ok && BN_cmp(RSA_get0_d(rsa1), RSA_get0_d(rsa2)) == 0; | |
135 | return ok; | |
136 | } | |
137 | ||
138 | static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[]) | |
139 | { | |
140 | RSA *rsa = keydata; | |
141 | int rsa_type; | |
142 | int ok = 1; | |
143 | ||
144 | if (rsa == NULL) | |
145 | return 0; | |
146 | ||
147 | if ((selection & RSA_POSSIBLE_SELECTIONS) == 0) | |
148 | return 0; | |
149 | ||
150 | rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK); | |
151 | ||
152 | /* TODO(3.0) OAEP should bring on parameters as well */ | |
153 | ||
154 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) | |
155 | ok = ok && pss_params_fromdata(rsa_get0_pss_params_30(rsa), params, | |
156 | rsa_type, rsa_get0_libctx(rsa)); | |
157 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
158 | ok = ok && rsa_fromdata(rsa, params); | |
159 | ||
160 | return ok; | |
161 | } | |
162 | ||
163 | static int rsa_export(void *keydata, int selection, | |
164 | OSSL_CALLBACK *param_callback, void *cbarg) | |
165 | { | |
166 | RSA *rsa = keydata; | |
167 | const RSA_PSS_PARAMS_30 *pss_params = rsa_get0_pss_params_30(rsa); | |
168 | OSSL_PARAM_BLD *tmpl; | |
169 | OSSL_PARAM *params = NULL; | |
170 | int ok = 1; | |
171 | ||
172 | if (rsa == NULL) | |
173 | return 0; | |
174 | ||
175 | /* TODO(3.0) OAEP should bring on parameters */ | |
176 | ||
177 | tmpl = OSSL_PARAM_BLD_new(); | |
178 | if (tmpl == NULL) | |
179 | return 0; | |
180 | ||
181 | if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) | |
182 | ok = ok && (rsa_pss_params_30_is_unrestricted(pss_params) | |
183 | || rsa_pss_params_30_todata(pss_params, NULL, tmpl, NULL)); | |
184 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
185 | ok = ok && rsa_todata(rsa, tmpl, NULL); | |
186 | ||
187 | if (!ok | |
188 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) | |
189 | goto err; | |
190 | ||
191 | ok = param_callback(params, cbarg); | |
192 | OSSL_PARAM_BLD_free_params(params); | |
193 | err: | |
194 | OSSL_PARAM_BLD_free(tmpl); | |
195 | return ok; | |
196 | } | |
197 | ||
198 | #ifdef FIPS_MODULE | |
199 | /* In fips mode there are no multi-primes. */ | |
200 | # define RSA_KEY_MP_TYPES() \ | |
201 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \ | |
202 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \ | |
203 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \ | |
204 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \ | |
205 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0), | |
206 | #else | |
207 | /* | |
208 | * We allow up to 10 prime factors (starting with p, q). | |
209 | * NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT | |
210 | */ | |
211 | # define RSA_KEY_MP_TYPES() \ | |
212 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \ | |
213 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \ | |
214 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR3, NULL, 0), \ | |
215 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR4, NULL, 0), \ | |
216 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR5, NULL, 0), \ | |
217 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR6, NULL, 0), \ | |
218 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR7, NULL, 0), \ | |
219 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR8, NULL, 0), \ | |
220 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR9, NULL, 0), \ | |
221 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR10, NULL, 0), \ | |
222 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \ | |
223 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \ | |
224 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT3, NULL, 0), \ | |
225 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT4, NULL, 0), \ | |
226 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT5, NULL, 0), \ | |
227 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT6, NULL, 0), \ | |
228 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT7, NULL, 0), \ | |
229 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT8, NULL, 0), \ | |
230 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT9, NULL, 0), \ | |
231 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT10, NULL, 0), \ | |
232 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0), \ | |
233 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT2, NULL, 0), \ | |
234 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT3, NULL, 0), \ | |
235 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT4, NULL, 0), \ | |
236 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT5, NULL, 0), \ | |
237 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT6, NULL, 0), \ | |
238 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, NULL, 0), \ | |
239 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, NULL, 0), \ | |
240 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, NULL, 0), | |
241 | #endif | |
242 | ||
243 | #define RSA_KEY_TYPES() \ | |
244 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), \ | |
245 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), \ | |
246 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0), \ | |
247 | RSA_KEY_MP_TYPES() | |
248 | ||
249 | /* | |
250 | * This provider can export everything in an RSA key, so we use the exact | |
251 | * same type description for export as for import. Other providers might | |
252 | * choose to import full keys, but only export the public parts, and will | |
253 | * therefore have the importkey_types and importkey_types functions return | |
254 | * different arrays. | |
255 | */ | |
256 | static const OSSL_PARAM rsa_key_types[] = { | |
257 | RSA_KEY_TYPES() | |
258 | OSSL_PARAM_END | |
259 | }; | |
260 | /* | |
261 | * We lied about the amount of factors, exponents and coefficients, the | |
262 | * export and import functions can really deal with an infinite amount | |
263 | * of these numbers. However, RSA keys with too many primes are futile, | |
264 | * so we at least pretend to have some limits. | |
265 | */ | |
266 | ||
267 | static const OSSL_PARAM *rsa_imexport_types(int selection) | |
268 | { | |
269 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
270 | return rsa_key_types; | |
271 | return NULL; | |
272 | } | |
273 | ||
274 | static const OSSL_PARAM *rsa_import_types(int selection) | |
275 | { | |
276 | return rsa_imexport_types(selection); | |
277 | } | |
278 | ||
279 | static const OSSL_PARAM *rsa_export_types(int selection) | |
280 | { | |
281 | return rsa_imexport_types(selection); | |
282 | } | |
283 | ||
284 | static int rsa_get_params(void *key, OSSL_PARAM params[]) | |
285 | { | |
286 | RSA *rsa = key; | |
287 | const RSA_PSS_PARAMS_30 *pss_params = rsa_get0_pss_params_30(rsa); | |
288 | int rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK); | |
289 | OSSL_PARAM *p; | |
290 | ||
291 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL | |
292 | && !OSSL_PARAM_set_int(p, RSA_bits(rsa))) | |
293 | return 0; | |
294 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL | |
295 | && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa))) | |
296 | return 0; | |
297 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL | |
298 | && !OSSL_PARAM_set_int(p, RSA_size(rsa))) | |
299 | return 0; | |
300 | ||
301 | /* | |
302 | * For RSA-PSS keys, we ignore the default digest request | |
303 | * TODO(3.0) with RSA-OAEP keys, this may need to be amended | |
304 | */ | |
305 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL | |
306 | && rsa_type != RSA_FLAG_TYPE_RSASSAPSS) { | |
307 | if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD)) | |
308 | return 0; | |
309 | } | |
310 | ||
311 | /* | |
312 | * For non-RSA-PSS keys, we ignore the mandatory digest request | |
313 | * TODO(3.0) with RSA-OAEP keys, this may need to be amended | |
314 | */ | |
315 | if ((p = OSSL_PARAM_locate(params, | |
316 | OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL | |
317 | && rsa_type == RSA_FLAG_TYPE_RSASSAPSS) { | |
318 | const char *mdname = RSA_PSS_DEFAULT_MD; | |
319 | ||
320 | if (!rsa_pss_params_30_is_unrestricted(pss_params)) { | |
321 | mdname = | |
322 | rsa_oaeppss_nid2name(rsa_pss_params_30_hashalg(pss_params)); | |
323 | ||
324 | if (mdname == NULL || !OSSL_PARAM_set_utf8_string(p, mdname)) | |
325 | return 0; | |
326 | } | |
327 | } | |
328 | return (rsa_type != RSA_FLAG_TYPE_RSASSAPSS | |
329 | || rsa_pss_params_30_todata(pss_params, NULL, NULL, params)) | |
330 | && rsa_todata(rsa, NULL, params); | |
331 | } | |
332 | ||
333 | static const OSSL_PARAM rsa_params[] = { | |
334 | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), | |
335 | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), | |
336 | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), | |
337 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0), | |
338 | RSA_KEY_TYPES() | |
339 | OSSL_PARAM_END | |
340 | }; | |
341 | ||
342 | static const OSSL_PARAM *rsa_gettable_params(void) | |
343 | { | |
344 | return rsa_params; | |
345 | } | |
346 | ||
347 | static int rsa_validate(void *keydata, int selection) | |
348 | { | |
349 | RSA *rsa = keydata; | |
350 | int ok = 0; | |
351 | ||
352 | if ((selection & RSA_POSSIBLE_SELECTIONS) != 0) | |
353 | ok = 1; | |
354 | ||
355 | /* If the whole key is selected, we do a pairwise validation */ | |
356 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) | |
357 | == OSSL_KEYMGMT_SELECT_KEYPAIR) { | |
358 | ok = ok && rsa_validate_pairwise(rsa); | |
359 | } else { | |
360 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
361 | ok = ok && rsa_validate_private(rsa); | |
362 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
363 | ok = ok && rsa_validate_public(rsa); | |
364 | } | |
365 | return ok; | |
366 | } | |
367 | ||
368 | struct rsa_gen_ctx { | |
369 | OPENSSL_CTX *libctx; | |
370 | ||
371 | int rsa_type; | |
372 | ||
373 | size_t nbits; | |
374 | BIGNUM *pub_exp; | |
375 | size_t primes; | |
376 | ||
377 | /* For PSS */ | |
378 | RSA_PSS_PARAMS_30 pss_params; | |
379 | ||
380 | /* For generation callback */ | |
381 | OSSL_CALLBACK *cb; | |
382 | void *cbarg; | |
383 | }; | |
384 | ||
385 | static int rsa_gencb(int p, int n, BN_GENCB *cb) | |
386 | { | |
387 | struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb); | |
388 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; | |
389 | ||
390 | params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p); | |
391 | params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n); | |
392 | ||
393 | return gctx->cb(params, gctx->cbarg); | |
394 | } | |
395 | ||
396 | static void *gen_init(void *provctx, int selection, int rsa_type) | |
397 | { | |
398 | OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
399 | struct rsa_gen_ctx *gctx = NULL; | |
400 | ||
401 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) | |
402 | return NULL; | |
403 | ||
404 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { | |
405 | gctx->libctx = libctx; | |
406 | if ((gctx->pub_exp = BN_new()) == NULL | |
407 | || !BN_set_word(gctx->pub_exp, RSA_F4)) { | |
408 | BN_free(gctx->pub_exp); | |
409 | OPENSSL_free(gctx); | |
410 | gctx = NULL; | |
411 | } else { | |
412 | gctx->nbits = 2048; | |
413 | gctx->primes = RSA_DEFAULT_PRIME_NUM; | |
414 | gctx->rsa_type = rsa_type; | |
415 | } | |
416 | } | |
417 | return gctx; | |
418 | } | |
419 | ||
420 | static void *rsa_gen_init(void *provctx, int selection) | |
421 | { | |
422 | return gen_init(provctx, selection, RSA_FLAG_TYPE_RSA); | |
423 | } | |
424 | ||
425 | static void *rsapss_gen_init(void *provctx, int selection) | |
426 | { | |
427 | return gen_init(provctx, selection, RSA_FLAG_TYPE_RSASSAPSS); | |
428 | } | |
429 | ||
430 | /* | |
431 | * This function is common for all RSA sub-types, to detect possible | |
432 | * misuse, such as PSS parameters being passed when a plain RSA key | |
433 | * is generated. | |
434 | */ | |
435 | static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[]) | |
436 | { | |
437 | struct rsa_gen_ctx *gctx = genctx; | |
438 | const OSSL_PARAM *p; | |
439 | ||
440 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL | |
441 | && !OSSL_PARAM_get_size_t(p, &gctx->nbits)) | |
442 | return 0; | |
443 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL | |
444 | && !OSSL_PARAM_get_size_t(p, &gctx->primes)) | |
445 | return 0; | |
446 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL | |
447 | && !OSSL_PARAM_get_BN(p, &gctx->pub_exp)) | |
448 | return 0; | |
449 | /* Only attempt to get PSS parameters when generating an RSA-PSS key */ | |
450 | if (gctx->rsa_type == RSA_FLAG_TYPE_RSASSAPSS | |
451 | && !pss_params_fromdata(&gctx->pss_params, params, gctx->rsa_type, | |
452 | gctx->libctx)) | |
453 | return 0; | |
454 | return 1; | |
455 | } | |
456 | ||
457 | #define rsa_gen_basic \ | |
458 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL), \ | |
459 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL), \ | |
460 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0) | |
461 | ||
462 | /* | |
463 | * The following must be kept in sync with rsa_pss_params_30_fromdata() | |
464 | * in crypto/rsa/rsa_backend.c | |
465 | */ | |
466 | #define rsa_gen_pss \ | |
467 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST, NULL, 0), \ | |
468 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MASKGENFUNC, NULL, 0), \ | |
469 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MGF1_DIGEST, NULL, 0), \ | |
470 | OSSL_PARAM_int(OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, NULL) | |
471 | ||
472 | static const OSSL_PARAM *rsa_gen_settable_params(void *provctx) | |
473 | { | |
474 | static OSSL_PARAM settable[] = { | |
475 | rsa_gen_basic, | |
476 | OSSL_PARAM_END | |
477 | }; | |
478 | ||
479 | return settable; | |
480 | } | |
481 | ||
482 | static const OSSL_PARAM *rsapss_gen_settable_params(void *provctx) | |
483 | { | |
484 | static OSSL_PARAM settable[] = { | |
485 | rsa_gen_basic, | |
486 | rsa_gen_pss, | |
487 | OSSL_PARAM_END | |
488 | }; | |
489 | ||
490 | return settable; | |
491 | } | |
492 | ||
493 | static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) | |
494 | { | |
495 | struct rsa_gen_ctx *gctx = genctx; | |
496 | RSA *rsa = NULL, *rsa_tmp = NULL; | |
497 | BN_GENCB *gencb = NULL; | |
498 | ||
499 | if (gctx == NULL) | |
500 | return NULL; | |
501 | ||
502 | switch (gctx->rsa_type) { | |
503 | case RSA_FLAG_TYPE_RSA: | |
504 | /* For plain RSA keys, PSS parameters must not be set */ | |
505 | if (!rsa_pss_params_30_is_unrestricted(&gctx->pss_params)) | |
506 | goto err; | |
507 | break; | |
508 | case RSA_FLAG_TYPE_RSASSAPSS: | |
509 | /* | |
510 | * For plain RSA-PSS keys, PSS parameters may be set but don't have | |
511 | * to, so not check. | |
512 | */ | |
513 | break; | |
514 | default: | |
515 | /* Unsupported RSA key sub-type... */ | |
516 | return NULL; | |
517 | } | |
518 | ||
519 | if ((rsa_tmp = rsa_new_with_ctx(gctx->libctx)) == NULL) | |
520 | return NULL; | |
521 | ||
522 | gctx->cb = osslcb; | |
523 | gctx->cbarg = cbarg; | |
524 | gencb = BN_GENCB_new(); | |
525 | if (gencb != NULL) | |
526 | BN_GENCB_set(gencb, rsa_gencb, genctx); | |
527 | ||
528 | if (!RSA_generate_multi_prime_key(rsa_tmp, | |
529 | (int)gctx->nbits, (int)gctx->primes, | |
530 | gctx->pub_exp, gencb)) | |
531 | goto err; | |
532 | ||
533 | if (!rsa_pss_params_30_copy(rsa_get0_pss_params_30(rsa_tmp), | |
534 | &gctx->pss_params)) | |
535 | goto err; | |
536 | ||
537 | RSA_clear_flags(rsa_tmp, RSA_FLAG_TYPE_MASK); | |
538 | RSA_set_flags(rsa_tmp, gctx->rsa_type); | |
539 | ||
540 | rsa = rsa_tmp; | |
541 | rsa_tmp = NULL; | |
542 | err: | |
543 | BN_GENCB_free(gencb); | |
544 | RSA_free(rsa_tmp); | |
545 | return rsa; | |
546 | } | |
547 | ||
548 | static void rsa_gen_cleanup(void *genctx) | |
549 | { | |
550 | struct rsa_gen_ctx *gctx = genctx; | |
551 | ||
552 | if (gctx == NULL) | |
553 | return; | |
554 | ||
555 | BN_clear_free(gctx->pub_exp); | |
556 | OPENSSL_free(gctx); | |
557 | } | |
558 | ||
559 | /* For any RSA key, we use the "RSA" algorithms regardless of sub-type. */ | |
560 | static const char *rsapss_query_operation_name(int operation_id) | |
561 | { | |
562 | return "RSA"; | |
563 | } | |
564 | ||
565 | const OSSL_DISPATCH rsa_keymgmt_functions[] = { | |
566 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata }, | |
567 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init }, | |
568 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, | |
569 | (void (*)(void))rsa_gen_set_params }, | |
570 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, | |
571 | (void (*)(void))rsa_gen_settable_params }, | |
572 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen }, | |
573 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup }, | |
574 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata }, | |
575 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params }, | |
576 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params }, | |
577 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has }, | |
578 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match }, | |
579 | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate }, | |
580 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import }, | |
581 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types }, | |
582 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export }, | |
583 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types }, | |
584 | { 0, NULL } | |
585 | }; | |
586 | ||
587 | const OSSL_DISPATCH rsapss_keymgmt_functions[] = { | |
588 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsapss_newdata }, | |
589 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsapss_gen_init }, | |
590 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))rsa_gen_set_params }, | |
591 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, | |
592 | (void (*)(void))rsapss_gen_settable_params }, | |
593 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen }, | |
594 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup }, | |
595 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata }, | |
596 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params }, | |
597 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params }, | |
598 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has }, | |
599 | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate }, | |
600 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import }, | |
601 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types }, | |
602 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export }, | |
603 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types }, | |
604 | { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, | |
605 | (void (*)(void))rsapss_query_operation_name }, | |
606 | { 0, NULL } | |
607 | }; |