/src/openssl/providers/implementations/keymgmt/dsa_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 | | * DSA 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/proverr.h> |
21 | | #include "prov/securitycheck.h" |
22 | | #include "prov/providercommon.h" |
23 | | #include "prov/implementations.h" |
24 | | #include "prov/provider_ctx.h" |
25 | | #include "crypto/dsa.h" |
26 | | #include "internal/sizes.h" |
27 | | #include "internal/nelem.h" |
28 | | #include "internal/param_build_set.h" |
29 | | |
30 | | static OSSL_FUNC_keymgmt_new_fn dsa_newdata; |
31 | | static OSSL_FUNC_keymgmt_new_ex_fn dsa_newdata_ex; |
32 | | static OSSL_FUNC_keymgmt_free_fn dsa_freedata; |
33 | | static OSSL_FUNC_keymgmt_gen_init_fn dsa_gen_init; |
34 | | static OSSL_FUNC_keymgmt_gen_set_template_fn dsa_gen_set_template; |
35 | | static OSSL_FUNC_keymgmt_gen_set_params_fn dsa_gen_set_params; |
36 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn dsa_gen_settable_params; |
37 | | static OSSL_FUNC_keymgmt_gen_get_params_fn dsa_gen_get_params; |
38 | | static OSSL_FUNC_keymgmt_gen_gettable_params_fn dsa_gen_gettable_params; |
39 | | static OSSL_FUNC_keymgmt_gen_fn dsa_gen; |
40 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn dsa_gen_cleanup; |
41 | | static OSSL_FUNC_keymgmt_load_fn dsa_load; |
42 | | static OSSL_FUNC_keymgmt_get_params_fn dsa_get_params; |
43 | | static OSSL_FUNC_keymgmt_gettable_params_fn dsa_gettable_params; |
44 | | static OSSL_FUNC_keymgmt_has_fn dsa_has; |
45 | | static OSSL_FUNC_keymgmt_match_fn dsa_match; |
46 | | static OSSL_FUNC_keymgmt_validate_fn dsa_validate; |
47 | | static OSSL_FUNC_keymgmt_import_fn dsa_import; |
48 | | static OSSL_FUNC_keymgmt_import_types_fn dsa_import_types; |
49 | | static OSSL_FUNC_keymgmt_export_fn dsa_export; |
50 | | static OSSL_FUNC_keymgmt_export_types_fn dsa_export_types; |
51 | | static OSSL_FUNC_keymgmt_dup_fn dsa_dup; |
52 | | |
53 | 0 | #define DSA_DEFAULT_MD "SHA256" |
54 | | #define DSA_POSSIBLE_SELECTIONS \ |
55 | 0 | (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) |
56 | | |
57 | | struct dsa_gen_ctx { |
58 | | OSSL_LIB_CTX *libctx; |
59 | | |
60 | | FFC_PARAMS *ffc_params; |
61 | | int selection; |
62 | | /* All these parameters are used for parameter generation only */ |
63 | | size_t pbits; |
64 | | size_t qbits; |
65 | | unsigned char *seed; /* optional FIPS186-4 param for testing */ |
66 | | size_t seedlen; |
67 | | int gindex; /* optional FIPS186-4 generator index (ignored if -1) */ |
68 | | int gen_type; /* DSA_PARAMGEN_TYPE_FIPS_186_2 or DSA_PARAMGEN_TYPE_FIPS_186_4 */ |
69 | | int pcounter; |
70 | | int hindex; |
71 | | char *mdname; |
72 | | char *mdprops; |
73 | | OSSL_CALLBACK *cb; |
74 | | void *cbarg; |
75 | | OSSL_FIPS_IND_DECLARE |
76 | | }; |
77 | | typedef struct dh_name2id_st { |
78 | | const char *name; |
79 | | int id; |
80 | | } DSA_GENTYPE_NAME2ID; |
81 | | |
82 | | static const DSA_GENTYPE_NAME2ID dsatype2id[] = { |
83 | | #ifdef FIPS_MODULE |
84 | | { "default", DSA_PARAMGEN_TYPE_FIPS_186_4 }, |
85 | | #else |
86 | | { "default", DSA_PARAMGEN_TYPE_FIPS_DEFAULT }, |
87 | | #endif |
88 | | { "fips186_4", DSA_PARAMGEN_TYPE_FIPS_186_4 }, |
89 | | { "fips186_2", DSA_PARAMGEN_TYPE_FIPS_186_2 }, |
90 | | }; |
91 | | |
92 | | static int dsa_gen_type_name2id(const char *name) |
93 | 0 | { |
94 | 0 | size_t i; |
95 | |
|
96 | 0 | for (i = 0; i < OSSL_NELEM(dsatype2id); ++i) { |
97 | 0 | if (OPENSSL_strcasecmp(dsatype2id[i].name, name) == 0) |
98 | 0 | return dsatype2id[i].id; |
99 | 0 | } |
100 | 0 | return -1; |
101 | 0 | } |
102 | | |
103 | | static int dsa_key_todata(DSA *dsa, OSSL_PARAM_BLD *bld, OSSL_PARAM *pubkey, |
104 | | OSSL_PARAM *privkey, int include_private) |
105 | 0 | { |
106 | 0 | const BIGNUM *priv = NULL, *pub = NULL; |
107 | |
|
108 | 0 | if (dsa == NULL) |
109 | 0 | return 0; |
110 | | |
111 | 0 | DSA_get0_key(dsa, &pub, &priv); |
112 | 0 | if (include_private |
113 | 0 | && priv != NULL |
114 | 0 | && !ossl_param_build_set_bn(bld, privkey, OSSL_PKEY_PARAM_PRIV_KEY, priv)) |
115 | 0 | return 0; |
116 | 0 | if (pub != NULL |
117 | 0 | && !ossl_param_build_set_bn(bld, pubkey, OSSL_PKEY_PARAM_PUB_KEY, pub)) |
118 | 0 | return 0; |
119 | | |
120 | 0 | return 1; |
121 | 0 | } |
122 | | |
123 | | static void *dsa_newdata_ex(void *provctx, const OSSL_PARAM params[]) |
124 | 0 | { |
125 | 0 | DSA *dsa = NULL; |
126 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
127 | |
|
128 | 0 | if (!ossl_prov_is_running()) |
129 | 0 | return NULL; |
130 | | |
131 | 0 | #ifndef FIPS_MODULE |
132 | 0 | const OSSL_PARAM *p = NULL; |
133 | |
|
134 | 0 | if (params != NULL) |
135 | 0 | p = OSSL_PARAM_locate_const(params, "legacy-object"); |
136 | | |
137 | | /* |
138 | | * This only works because we are in the default provider. We are not |
139 | | * normally allowed to pass complex objects across the provider boundary |
140 | | * like this. |
141 | | */ |
142 | 0 | if (p != NULL && OSSL_PARAM_get_octet_ptr(p, (const void **)&dsa, NULL) && dsa != NULL) { |
143 | 0 | if (ossl_lib_ctx_get_concrete(ossl_dsa_get0_libctx(dsa)) != ossl_lib_ctx_get_concrete(libctx)) |
144 | 0 | dsa = NULL; |
145 | 0 | else if (!DSA_up_ref(dsa)) |
146 | 0 | return NULL; |
147 | 0 | } |
148 | 0 | #endif |
149 | | |
150 | 0 | if (dsa == NULL) |
151 | 0 | dsa = ossl_dsa_new(libctx); |
152 | |
|
153 | 0 | return dsa; |
154 | 0 | } |
155 | | |
156 | | static void *dsa_newdata(void *provctx) |
157 | 0 | { |
158 | 0 | return dsa_newdata_ex(provctx, NULL); |
159 | 0 | } |
160 | | |
161 | | static void dsa_freedata(void *keydata) |
162 | 0 | { |
163 | 0 | DSA_free(keydata); |
164 | 0 | } |
165 | | |
166 | | static int dsa_has(const void *keydata, int selection) |
167 | 0 | { |
168 | 0 | const DSA *dsa = keydata; |
169 | 0 | int ok = 1; |
170 | |
|
171 | 0 | if (!ossl_prov_is_running() || dsa == NULL) |
172 | 0 | return 0; |
173 | 0 | if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) |
174 | 0 | return 1; /* the selection is not missing */ |
175 | | |
176 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
177 | 0 | ok = ok && (DSA_get0_pub_key(dsa) != NULL); |
178 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
179 | 0 | ok = ok && (DSA_get0_priv_key(dsa) != NULL); |
180 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
181 | 0 | ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL); |
182 | 0 | return ok; |
183 | 0 | } |
184 | | |
185 | | static int dsa_match(const void *keydata1, const void *keydata2, int selection) |
186 | 0 | { |
187 | 0 | const DSA *dsa1 = keydata1; |
188 | 0 | const DSA *dsa2 = keydata2; |
189 | 0 | int ok = 1; |
190 | |
|
191 | 0 | if (!ossl_prov_is_running()) |
192 | 0 | return 0; |
193 | | |
194 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
195 | 0 | int key_checked = 0; |
196 | |
|
197 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
198 | 0 | const BIGNUM *pa = DSA_get0_pub_key(dsa1); |
199 | 0 | const BIGNUM *pb = DSA_get0_pub_key(dsa2); |
200 | |
|
201 | 0 | if (pa != NULL && pb != NULL) { |
202 | 0 | ok = ok && BN_cmp(pa, pb) == 0; |
203 | 0 | key_checked = 1; |
204 | 0 | } |
205 | 0 | } |
206 | 0 | if (!key_checked |
207 | 0 | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
208 | 0 | const BIGNUM *pa = DSA_get0_priv_key(dsa1); |
209 | 0 | const BIGNUM *pb = DSA_get0_priv_key(dsa2); |
210 | |
|
211 | 0 | if (pa != NULL && pb != NULL) { |
212 | 0 | ok = ok && BN_cmp(pa, pb) == 0; |
213 | 0 | key_checked = 1; |
214 | 0 | } |
215 | 0 | } |
216 | 0 | ok = ok && key_checked; |
217 | 0 | } |
218 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
219 | 0 | FFC_PARAMS *dsaparams1 = ossl_dsa_get0_params((DSA *)dsa1); |
220 | 0 | FFC_PARAMS *dsaparams2 = ossl_dsa_get0_params((DSA *)dsa2); |
221 | |
|
222 | 0 | ok = ok && ossl_ffc_params_cmp(dsaparams1, dsaparams2, 1); |
223 | 0 | } |
224 | 0 | return ok; |
225 | 0 | } |
226 | | |
227 | | static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[]) |
228 | 0 | { |
229 | 0 | DSA *dsa = keydata; |
230 | 0 | int ok = 1; |
231 | |
|
232 | 0 | if (!ossl_prov_is_running() || dsa == NULL) |
233 | 0 | return 0; |
234 | | |
235 | 0 | if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) |
236 | 0 | return 0; |
237 | | |
238 | | /* a key without parameters is meaningless */ |
239 | 0 | ok = ok && ossl_dsa_ffc_params_fromdata(dsa, params); |
240 | |
|
241 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
242 | 0 | int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
243 | |
|
244 | 0 | ok = ok && ossl_dsa_key_fromdata(dsa, params, include_private); |
245 | 0 | } |
246 | |
|
247 | 0 | return ok; |
248 | 0 | } |
249 | | |
250 | | static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
251 | | void *cbarg) |
252 | 0 | { |
253 | 0 | DSA *dsa = keydata; |
254 | 0 | OSSL_PARAM_BLD *tmpl; |
255 | 0 | OSSL_PARAM *params = NULL; |
256 | 0 | int ok = 1; |
257 | |
|
258 | 0 | if (!ossl_prov_is_running() || dsa == NULL) |
259 | 0 | return 0; |
260 | | |
261 | 0 | if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) |
262 | 0 | return 0; |
263 | | |
264 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
265 | 0 | if (tmpl == NULL) |
266 | 0 | return 0; |
267 | | |
268 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) |
269 | 0 | ok = ok && ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), tmpl, NULL); |
270 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
271 | 0 | int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
272 | |
|
273 | 0 | ok = ok && dsa_key_todata(dsa, tmpl, NULL, NULL, include_private); |
274 | 0 | } |
275 | |
|
276 | 0 | if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
277 | 0 | ok = 0; |
278 | 0 | goto err; |
279 | 0 | } |
280 | | |
281 | 0 | ok = param_cb(params, cbarg); |
282 | 0 | OSSL_PARAM_clear_free(params); |
283 | 0 | err: |
284 | 0 | OSSL_PARAM_BLD_free(tmpl); |
285 | 0 | return ok; |
286 | 0 | } |
287 | | |
288 | | /* IMEXPORT = IMPORT + EXPORT */ |
289 | | |
290 | | #define DSA_IMEXPORTABLE_PARAMETERS \ |
291 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \ |
292 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \ |
293 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \ |
294 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \ |
295 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \ |
296 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \ |
297 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \ |
298 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0) |
299 | | #define DSA_IMEXPORTABLE_PUBLIC_KEY \ |
300 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0) |
301 | | #define DSA_IMEXPORTABLE_PRIVATE_KEY \ |
302 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0) |
303 | | static const OSSL_PARAM dsa_all_types[] = { |
304 | | DSA_IMEXPORTABLE_PARAMETERS, |
305 | | DSA_IMEXPORTABLE_PUBLIC_KEY, |
306 | | DSA_IMEXPORTABLE_PRIVATE_KEY, |
307 | | OSSL_PARAM_END |
308 | | }; |
309 | | static const OSSL_PARAM dsa_parameter_types[] = { |
310 | | DSA_IMEXPORTABLE_PARAMETERS, |
311 | | OSSL_PARAM_END |
312 | | }; |
313 | | static const OSSL_PARAM dsa_key_types[] = { |
314 | | DSA_IMEXPORTABLE_PUBLIC_KEY, |
315 | | DSA_IMEXPORTABLE_PRIVATE_KEY, |
316 | | OSSL_PARAM_END |
317 | | }; |
318 | | static const OSSL_PARAM *dsa_types[] = { |
319 | | NULL, /* Index 0 = none of them */ |
320 | | dsa_parameter_types, /* Index 1 = parameter types */ |
321 | | dsa_key_types, /* Index 2 = key types */ |
322 | | dsa_all_types /* Index 3 = 1 + 2 */ |
323 | | }; |
324 | | |
325 | | static const OSSL_PARAM *dsa_imexport_types(int selection) |
326 | 0 | { |
327 | 0 | int type_select = 0; |
328 | |
|
329 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) |
330 | 0 | type_select += 1; |
331 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
332 | 0 | type_select += 2; |
333 | 0 | return dsa_types[type_select]; |
334 | 0 | } |
335 | | |
336 | | static const OSSL_PARAM *dsa_import_types(int selection) |
337 | 0 | { |
338 | 0 | return dsa_imexport_types(selection); |
339 | 0 | } |
340 | | |
341 | | static const OSSL_PARAM *dsa_export_types(int selection) |
342 | 0 | { |
343 | 0 | return dsa_imexport_types(selection); |
344 | 0 | } |
345 | | |
346 | | struct dsa_params_st { |
347 | | FFC_OSSL_PARAMS ffp; |
348 | | OSSL_PARAM *bits; |
349 | | OSSL_PARAM *secbits; |
350 | | OSSL_PARAM *maxsize; |
351 | | OSSL_PARAM *seccat; |
352 | | OSSL_PARAM *digest; |
353 | | OSSL_PARAM *privkey; |
354 | | OSSL_PARAM *pubkey; |
355 | | }; |
356 | | |
357 | | #define dsa_get_params_st dsa_params_st |
358 | | |
359 | | #include "providers/implementations/keymgmt/dsa_kmgmt.inc" |
360 | | |
361 | | static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[]) |
362 | 0 | { |
363 | 0 | DSA *dsa = key; |
364 | 0 | struct dsa_params_st p; |
365 | |
|
366 | 0 | if (key == NULL || !dsa_get_params_decoder(params, &p)) |
367 | 0 | return 0; |
368 | | |
369 | 0 | if (p.bits != NULL && !OSSL_PARAM_set_int(p.bits, DSA_bits(dsa))) |
370 | 0 | return 0; |
371 | | |
372 | 0 | if (p.secbits != NULL && !OSSL_PARAM_set_int(p.secbits, DSA_security_bits(dsa))) |
373 | 0 | return 0; |
374 | | |
375 | 0 | if (p.maxsize != NULL && !OSSL_PARAM_set_int(p.maxsize, DSA_size(dsa))) |
376 | 0 | return 0; |
377 | | |
378 | 0 | if (p.digest != NULL && !OSSL_PARAM_set_utf8_string(p.digest, DSA_DEFAULT_MD)) |
379 | 0 | return 0; |
380 | | |
381 | 0 | if (p.seccat != NULL && !OSSL_PARAM_set_int(p.seccat, 0)) |
382 | 0 | return 0; |
383 | | |
384 | 0 | return ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), NULL, &p.ffp) |
385 | 0 | && dsa_key_todata(dsa, NULL, p.pubkey, p.privkey, 1); |
386 | 0 | } |
387 | | |
388 | | static const OSSL_PARAM *dsa_gettable_params(void *provctx) |
389 | 0 | { |
390 | 0 | return dsa_get_params_list; |
391 | 0 | } |
392 | | |
393 | | static int dsa_validate_domparams(const DSA *dsa, int checktype) |
394 | 0 | { |
395 | 0 | int status = 0; |
396 | |
|
397 | 0 | return ossl_dsa_check_params(dsa, checktype, &status); |
398 | 0 | } |
399 | | |
400 | | static int dsa_validate_public(const DSA *dsa) |
401 | 0 | { |
402 | 0 | int status = 0; |
403 | 0 | const BIGNUM *pub_key = NULL; |
404 | |
|
405 | 0 | DSA_get0_key(dsa, &pub_key, NULL); |
406 | 0 | if (pub_key == NULL) |
407 | 0 | return 0; |
408 | 0 | return ossl_dsa_check_pub_key(dsa, pub_key, &status); |
409 | 0 | } |
410 | | |
411 | | static int dsa_validate_private(const DSA *dsa) |
412 | 0 | { |
413 | 0 | int status = 0; |
414 | 0 | const BIGNUM *priv_key = NULL; |
415 | |
|
416 | 0 | DSA_get0_key(dsa, NULL, &priv_key); |
417 | 0 | if (priv_key == NULL) |
418 | 0 | return 0; |
419 | 0 | return ossl_dsa_check_priv_key(dsa, priv_key, &status); |
420 | 0 | } |
421 | | |
422 | | static int dsa_validate(const void *keydata, int selection, int checktype) |
423 | 0 | { |
424 | 0 | const DSA *dsa = keydata; |
425 | 0 | int ok = 1; |
426 | |
|
427 | 0 | if (!ossl_prov_is_running()) |
428 | 0 | return 0; |
429 | | |
430 | 0 | if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) |
431 | 0 | return 1; /* nothing to validate */ |
432 | | |
433 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
434 | 0 | ok = ok && dsa_validate_domparams(dsa, checktype); |
435 | |
|
436 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
437 | 0 | ok = ok && dsa_validate_public(dsa); |
438 | |
|
439 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
440 | 0 | ok = ok && dsa_validate_private(dsa); |
441 | | |
442 | | /* If the whole key is selected, we do a pairwise validation */ |
443 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) |
444 | 0 | == OSSL_KEYMGMT_SELECT_KEYPAIR) |
445 | 0 | ok = ok && ossl_dsa_check_pairwise(dsa); |
446 | 0 | return ok; |
447 | 0 | } |
448 | | |
449 | | static void *dsa_gen_init(void *provctx, int selection, |
450 | | const OSSL_PARAM params[]) |
451 | 0 | { |
452 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
453 | 0 | struct dsa_gen_ctx *gctx = NULL; |
454 | |
|
455 | 0 | if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0) |
456 | 0 | return NULL; |
457 | | |
458 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
459 | 0 | gctx->selection = selection; |
460 | 0 | gctx->libctx = libctx; |
461 | 0 | gctx->pbits = 2048; |
462 | 0 | gctx->qbits = 224; |
463 | | #ifdef FIPS_MODULE |
464 | | gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4; |
465 | | #else |
466 | 0 | gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_DEFAULT; |
467 | 0 | #endif |
468 | 0 | gctx->gindex = -1; |
469 | 0 | gctx->pcounter = -1; |
470 | 0 | gctx->hindex = 0; |
471 | 0 | OSSL_FIPS_IND_INIT(gctx) |
472 | 0 | } |
473 | 0 | if (!dsa_gen_set_params(gctx, params)) { |
474 | 0 | dsa_gen_cleanup(gctx); |
475 | 0 | gctx = NULL; |
476 | 0 | } |
477 | 0 | return gctx; |
478 | 0 | } |
479 | | |
480 | | static int dsa_gen_set_template(void *genctx, void *templ) |
481 | 0 | { |
482 | 0 | struct dsa_gen_ctx *gctx = genctx; |
483 | 0 | DSA *dsa = templ; |
484 | |
|
485 | 0 | if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL) |
486 | 0 | return 0; |
487 | 0 | gctx->ffc_params = ossl_dsa_get0_params(dsa); |
488 | 0 | return 1; |
489 | 0 | } |
490 | | |
491 | | static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed, |
492 | | size_t seedlen) |
493 | 0 | { |
494 | 0 | OPENSSL_clear_free(gctx->seed, gctx->seedlen); |
495 | 0 | gctx->seed = NULL; |
496 | 0 | gctx->seedlen = 0; |
497 | 0 | if (seed != NULL && seedlen > 0) { |
498 | 0 | gctx->seed = OPENSSL_memdup(seed, seedlen); |
499 | 0 | if (gctx->seed == NULL) |
500 | 0 | return 0; |
501 | 0 | gctx->seedlen = seedlen; |
502 | 0 | } |
503 | 0 | return 1; |
504 | 0 | } |
505 | | |
506 | | static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
507 | 0 | { |
508 | 0 | struct dsa_gen_ctx *gctx = genctx; |
509 | 0 | struct dsa_gen_set_params_st p; |
510 | 0 | int gen_type = -1; |
511 | |
|
512 | 0 | if (gctx == NULL || !dsa_gen_set_params_decoder(params, &p)) |
513 | 0 | return 0; |
514 | | |
515 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, |
516 | 0 | p.ind_sign)) |
517 | 0 | return 0; |
518 | | |
519 | 0 | if (p.type != NULL) { |
520 | 0 | if (p.type->data_type != OSSL_PARAM_UTF8_STRING |
521 | 0 | || ((gen_type = dsa_gen_type_name2id(p.type->data)) == -1)) { |
522 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
523 | 0 | return 0; |
524 | 0 | } |
525 | | |
526 | | /* |
527 | | * Only assign context gen_type if it was set by dsa_gen_type_name2id |
528 | | * must be in range: |
529 | | * DSA_PARAMGEN_TYPE_FIPS_186_4 <= gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT |
530 | | */ |
531 | 0 | if (gen_type != -1) |
532 | 0 | gctx->gen_type = gen_type; |
533 | 0 | } |
534 | | |
535 | 0 | if (p.g_index != NULL && !OSSL_PARAM_get_int(p.g_index, &gctx->gindex)) |
536 | 0 | return 0; |
537 | | |
538 | 0 | if (p.p_counter != NULL && !OSSL_PARAM_get_int(p.p_counter, &gctx->pcounter)) |
539 | 0 | return 0; |
540 | | |
541 | 0 | if (p.h != NULL && !OSSL_PARAM_get_int(p.h, &gctx->hindex)) |
542 | 0 | return 0; |
543 | | |
544 | 0 | if (p.seed != NULL |
545 | 0 | && (p.seed->data_type != OSSL_PARAM_OCTET_STRING |
546 | 0 | || !dsa_set_gen_seed(gctx, p.seed->data, p.seed->data_size))) |
547 | 0 | return 0; |
548 | | |
549 | 0 | if (p.pbits != NULL && !OSSL_PARAM_get_size_t(p.pbits, &gctx->pbits)) |
550 | 0 | return 0; |
551 | | |
552 | 0 | if (p.qbits != NULL && !OSSL_PARAM_get_size_t(p.qbits, &gctx->qbits)) |
553 | 0 | return 0; |
554 | | |
555 | 0 | if (p.digest != NULL) { |
556 | 0 | if (p.digest->data_type != OSSL_PARAM_UTF8_STRING) |
557 | 0 | return 0; |
558 | 0 | OPENSSL_free(gctx->mdname); |
559 | 0 | gctx->mdname = OPENSSL_strdup(p.digest->data); |
560 | 0 | if (gctx->mdname == NULL) |
561 | 0 | return 0; |
562 | 0 | } |
563 | | |
564 | 0 | if (p.propq != NULL) { |
565 | 0 | if (p.propq->data_type != OSSL_PARAM_UTF8_STRING) |
566 | 0 | return 0; |
567 | 0 | OPENSSL_free(gctx->mdprops); |
568 | 0 | gctx->mdprops = OPENSSL_strdup(p.propq->data); |
569 | 0 | if (gctx->mdprops == NULL) |
570 | 0 | return 0; |
571 | 0 | } |
572 | 0 | return 1; |
573 | 0 | } |
574 | | |
575 | | static const OSSL_PARAM *dsa_gen_settable_params(ossl_unused void *genctx, |
576 | | ossl_unused void *provctx) |
577 | 0 | { |
578 | 0 | return dsa_gen_set_params_list; |
579 | 0 | } |
580 | | |
581 | | static int dsa_gen_get_params(void *genctx, OSSL_PARAM *params) |
582 | 0 | { |
583 | 0 | struct dsa_gen_ctx *gctx = genctx; |
584 | 0 | struct dsa_gen_get_params_st p; |
585 | |
|
586 | 0 | if (gctx == NULL || !dsa_gen_get_params_decoder(params, &p)) |
587 | 0 | return 0; |
588 | | |
589 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(gctx, p.ind)) |
590 | 0 | return 0; |
591 | 0 | return 1; |
592 | 0 | } |
593 | | |
594 | | static const OSSL_PARAM *dsa_gen_gettable_params(ossl_unused void *ctx, |
595 | | ossl_unused void *provctx) |
596 | 0 | { |
597 | 0 | return dsa_gen_get_params_list; |
598 | 0 | } |
599 | | |
600 | | static int dsa_gencb(int p, int n, BN_GENCB *cb) |
601 | 0 | { |
602 | 0 | struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb); |
603 | 0 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; |
604 | |
|
605 | 0 | params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p); |
606 | 0 | params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n); |
607 | |
|
608 | 0 | return gctx->cb(params, gctx->cbarg); |
609 | 0 | } |
610 | | |
611 | | static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) |
612 | 0 | { |
613 | 0 | struct dsa_gen_ctx *gctx = genctx; |
614 | 0 | DSA *dsa = NULL; |
615 | 0 | BN_GENCB *gencb = NULL; |
616 | 0 | int ret = 0; |
617 | 0 | FFC_PARAMS *ffc; |
618 | |
|
619 | 0 | if (!ossl_prov_is_running() || gctx == NULL) |
620 | 0 | return NULL; |
621 | | |
622 | | #ifdef FIPS_MODULE |
623 | | /* |
624 | | * DSA signing is not approved in FIPS 140-3, so there is no |
625 | | * need for DSA keygen either. |
626 | | */ |
627 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(gctx, OSSL_FIPS_IND_SETTABLE0, |
628 | | gctx->libctx, "DSA", "Keygen", |
629 | | ossl_fips_config_dsa_sign_disallowed)) |
630 | | return 0; |
631 | | #endif |
632 | | |
633 | 0 | dsa = ossl_dsa_new(gctx->libctx); |
634 | 0 | if (dsa == NULL) |
635 | 0 | return NULL; |
636 | | |
637 | 0 | if (gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_DEFAULT) |
638 | 0 | gctx->gen_type = (gctx->pbits >= 2048 ? DSA_PARAMGEN_TYPE_FIPS_186_4 : DSA_PARAMGEN_TYPE_FIPS_186_2); |
639 | | |
640 | | /* |
641 | | * Do a bounds check on context gen_type. Must be in range: |
642 | | * DSA_PARAMGEN_TYPE_FIPS_186_4 <= gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT |
643 | | * Noted here as this needs to be adjusted if a new type is |
644 | | * added. |
645 | | */ |
646 | 0 | if (!ossl_assert((gctx->gen_type >= DSA_PARAMGEN_TYPE_FIPS_186_4) |
647 | 0 | && (gctx->gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT))) { |
648 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
649 | 0 | "gen_type set to unsupported value %d", gctx->gen_type); |
650 | 0 | goto end; |
651 | 0 | } |
652 | | |
653 | 0 | gctx->cb = osslcb; |
654 | 0 | gctx->cbarg = cbarg; |
655 | 0 | gencb = BN_GENCB_new(); |
656 | 0 | if (gencb != NULL) |
657 | 0 | BN_GENCB_set(gencb, dsa_gencb, genctx); |
658 | |
|
659 | 0 | ffc = ossl_dsa_get0_params(dsa); |
660 | | /* Copy the template value if one was passed */ |
661 | 0 | if (gctx->ffc_params != NULL |
662 | 0 | && !ossl_ffc_params_copy(ffc, gctx->ffc_params)) |
663 | 0 | goto end; |
664 | | |
665 | 0 | if (gctx->seed != NULL |
666 | 0 | && !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen)) |
667 | 0 | goto end; |
668 | 0 | if (gctx->gindex != -1) { |
669 | 0 | ossl_ffc_params_set_gindex(ffc, gctx->gindex); |
670 | 0 | if (gctx->pcounter != -1) |
671 | 0 | ossl_ffc_params_set_pcounter(ffc, gctx->pcounter); |
672 | 0 | } else if (gctx->hindex != 0) { |
673 | 0 | ossl_ffc_params_set_h(ffc, gctx->hindex); |
674 | 0 | } |
675 | 0 | if (gctx->mdname != NULL) |
676 | 0 | ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops); |
677 | |
|
678 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
679 | |
|
680 | 0 | if (ossl_dsa_generate_ffc_parameters(dsa, gctx->gen_type, |
681 | 0 | (int)gctx->pbits, |
682 | 0 | (int)gctx->qbits, gencb) |
683 | 0 | <= 0) |
684 | 0 | goto end; |
685 | 0 | } |
686 | 0 | ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, |
687 | 0 | gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_186_2); |
688 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
689 | 0 | if (ffc->p == NULL |
690 | 0 | || ffc->q == NULL |
691 | 0 | || ffc->g == NULL) |
692 | 0 | goto end; |
693 | 0 | if (DSA_generate_key(dsa) <= 0) |
694 | 0 | goto end; |
695 | 0 | } |
696 | 0 | ret = 1; |
697 | 0 | end: |
698 | 0 | if (ret <= 0) { |
699 | 0 | DSA_free(dsa); |
700 | 0 | dsa = NULL; |
701 | 0 | } |
702 | 0 | BN_GENCB_free(gencb); |
703 | 0 | return dsa; |
704 | 0 | } |
705 | | |
706 | | static void dsa_gen_cleanup(void *genctx) |
707 | 0 | { |
708 | 0 | struct dsa_gen_ctx *gctx = genctx; |
709 | |
|
710 | 0 | if (gctx == NULL) |
711 | 0 | return; |
712 | | |
713 | 0 | OPENSSL_free(gctx->mdname); |
714 | 0 | OPENSSL_free(gctx->mdprops); |
715 | 0 | OPENSSL_clear_free(gctx->seed, gctx->seedlen); |
716 | 0 | OPENSSL_free(gctx); |
717 | 0 | } |
718 | | |
719 | | static void *dsa_load(const void *reference, size_t reference_sz) |
720 | 0 | { |
721 | 0 | DSA *dsa = NULL; |
722 | |
|
723 | 0 | if (ossl_prov_is_running() && reference_sz == sizeof(dsa)) { |
724 | | /* The contents of the reference is the address to our object */ |
725 | 0 | dsa = *(DSA **)reference; |
726 | | /* We grabbed, so we detach it */ |
727 | 0 | *(DSA **)reference = NULL; |
728 | 0 | return dsa; |
729 | 0 | } |
730 | 0 | return NULL; |
731 | 0 | } |
732 | | |
733 | | static void *dsa_dup(const void *keydata_from, int selection) |
734 | 0 | { |
735 | 0 | if (ossl_prov_is_running()) |
736 | 0 | return ossl_dsa_dup(keydata_from, selection); |
737 | 0 | return NULL; |
738 | 0 | } |
739 | | |
740 | | const OSSL_DISPATCH ossl_dsa_keymgmt_functions[] = { |
741 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata }, |
742 | | { OSSL_FUNC_KEYMGMT_NEW_EX, (void (*)(void))dsa_newdata_ex }, |
743 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init }, |
744 | | { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template }, |
745 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params }, |
746 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
747 | | (void (*)(void))dsa_gen_settable_params }, |
748 | | { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))dsa_gen_get_params }, |
749 | | { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS, |
750 | | (void (*)(void))dsa_gen_gettable_params }, |
751 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen }, |
752 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup }, |
753 | | { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dsa_load }, |
754 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata }, |
755 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))dsa_get_params }, |
756 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))dsa_gettable_params }, |
757 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has }, |
758 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match }, |
759 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate }, |
760 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import }, |
761 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types }, |
762 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export }, |
763 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types }, |
764 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dsa_dup }, |
765 | | OSSL_DISPATCH_END |
766 | | }; |