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