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