]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/keymgmt/dsa_kmgmt.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / keymgmt / dsa_kmgmt.c
1 /*
2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * 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 "e_os.h" /* strcasecmp */
17 #include <openssl/core_dispatch.h>
18 #include <openssl/core_names.h>
19 #include <openssl/bn.h>
20 #include <openssl/err.h>
21 #include "prov/providercommon.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_ctx.h"
24 #include "crypto/dsa.h"
25 #include "internal/sizes.h"
26 #include "internal/nelem.h"
27 #include "internal/param_build_set.h"
28
29 static OSSL_FUNC_keymgmt_new_fn dsa_newdata;
30 static OSSL_FUNC_keymgmt_free_fn dsa_freedata;
31 static OSSL_FUNC_keymgmt_gen_init_fn dsa_gen_init;
32 static OSSL_FUNC_keymgmt_gen_set_template_fn dsa_gen_set_template;
33 static OSSL_FUNC_keymgmt_gen_set_params_fn dsa_gen_set_params;
34 static OSSL_FUNC_keymgmt_gen_settable_params_fn dsa_gen_settable_params;
35 static OSSL_FUNC_keymgmt_gen_fn dsa_gen;
36 static OSSL_FUNC_keymgmt_gen_cleanup_fn dsa_gen_cleanup;
37 static OSSL_FUNC_keymgmt_load_fn dsa_load;
38 static OSSL_FUNC_keymgmt_get_params_fn dsa_get_params;
39 static OSSL_FUNC_keymgmt_gettable_params_fn dsa_gettable_params;
40 static OSSL_FUNC_keymgmt_has_fn dsa_has;
41 static OSSL_FUNC_keymgmt_match_fn dsa_match;
42 static OSSL_FUNC_keymgmt_validate_fn dsa_validate;
43 static OSSL_FUNC_keymgmt_import_fn dsa_import;
44 static OSSL_FUNC_keymgmt_import_types_fn dsa_import_types;
45 static OSSL_FUNC_keymgmt_export_fn dsa_export;
46 static OSSL_FUNC_keymgmt_export_types_fn dsa_export_types;
47
48 #define DSA_DEFAULT_MD "SHA256"
49 #define DSA_POSSIBLE_SELECTIONS \
50 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
51
52 struct dsa_gen_ctx {
53 OSSL_LIB_CTX *libctx;
54
55 FFC_PARAMS *ffc_params;
56 int selection;
57 /* All these parameters are used for parameter generation only */
58 size_t pbits;
59 size_t qbits;
60 unsigned char *seed; /* optional FIPS186-4 param for testing */
61 size_t seedlen;
62 int gindex; /* optional FIPS186-4 generator index (ignored if -1) */
63 int gen_type; /* DSA_PARAMGEN_TYPE_FIPS_186_2 or DSA_PARAMGEN_TYPE_FIPS_186_4 */
64 int pcounter;
65 int hindex;
66 char *mdname;
67 char *mdprops;
68 OSSL_CALLBACK *cb;
69 void *cbarg;
70 };
71 typedef struct dh_name2id_st{
72 const char *name;
73 int id;
74 } DSA_GENTYPE_NAME2ID;
75
76 static const DSA_GENTYPE_NAME2ID dsatype2id[]=
77 {
78 #ifdef FIPS_MODULE
79 { "default", DSA_PARAMGEN_TYPE_FIPS_186_4 },
80 #else
81 { "default", DSA_PARAMGEN_TYPE_FIPS_186_2 },
82 #endif
83 { "fips186_4", DSA_PARAMGEN_TYPE_FIPS_186_4 },
84 { "fips186_2", DSA_PARAMGEN_TYPE_FIPS_186_2 },
85 };
86
87 static int dsa_gen_type_name2id(const char *name)
88 {
89 size_t i;
90
91 for (i = 0; i < OSSL_NELEM(dsatype2id); ++i) {
92 if (strcasecmp(dsatype2id[i].name, name) == 0)
93 return dsatype2id[i].id;
94 }
95 return -1;
96 }
97
98 static int dsa_key_todata(DSA *dsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
99 {
100 const BIGNUM *priv = NULL, *pub = NULL;
101
102 if (dsa == NULL)
103 return 0;
104
105 DSA_get0_key(dsa, &pub, &priv);
106 if (priv != NULL
107 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
108 return 0;
109 if (pub != NULL
110 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
111 return 0;
112
113 return 1;
114 }
115
116 static void *dsa_newdata(void *provctx)
117 {
118 if (!ossl_prov_is_running())
119 return NULL;
120 return dsa_new_with_ctx(PROV_LIBCTX_OF(provctx));
121 }
122
123 static void dsa_freedata(void *keydata)
124 {
125 DSA_free(keydata);
126 }
127
128 static int dsa_has(const void *keydata, int selection)
129 {
130 const DSA *dsa = keydata;
131 int ok = 0;
132
133 if (ossl_prov_is_running() && dsa != NULL) {
134 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
135 ok = 1;
136
137 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
138 ok = ok && (DSA_get0_pub_key(dsa) != NULL);
139 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
140 ok = ok && (DSA_get0_priv_key(dsa) != NULL);
141 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
142 ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
143 }
144 return ok;
145 }
146
147 static int dsa_match(const void *keydata1, const void *keydata2, int selection)
148 {
149 const DSA *dsa1 = keydata1;
150 const DSA *dsa2 = keydata2;
151 int ok = 1;
152
153 if (!ossl_prov_is_running())
154 return 0;
155
156 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
157 ok = ok
158 && BN_cmp(DSA_get0_pub_key(dsa1), DSA_get0_pub_key(dsa2)) == 0;
159 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
160 ok = ok
161 && BN_cmp(DSA_get0_priv_key(dsa1), DSA_get0_priv_key(dsa2)) == 0;
162 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
163 FFC_PARAMS *dsaparams1 = dsa_get0_params((DSA *)dsa1);
164 FFC_PARAMS *dsaparams2 = dsa_get0_params((DSA *)dsa2);
165
166 ok = ok && ossl_ffc_params_cmp(dsaparams1, dsaparams2, 1);
167 }
168 return ok;
169 }
170
171 static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
172 {
173 DSA *dsa = keydata;
174 int ok = 1;
175
176 if (!ossl_prov_is_running() || dsa == NULL)
177 return 0;
178
179 if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
180 return 0;
181
182 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
183 ok = ok && dsa_ffc_params_fromdata(dsa, params);
184 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
185 ok = ok && dsa_key_fromdata(dsa, params);
186
187 return ok;
188 }
189
190 static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
191 void *cbarg)
192 {
193 DSA *dsa = keydata;
194 OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
195 OSSL_PARAM *params = NULL;
196 int ok = 1;
197
198 if (!ossl_prov_is_running() || dsa == NULL)
199 goto err;
200
201 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
202 ok = ok && ossl_ffc_params_todata(dsa_get0_params(dsa), tmpl, NULL);
203 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
204 ok = ok && dsa_key_todata(dsa, tmpl, NULL);
205
206 if (!ok
207 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
208 goto err;;
209
210 ok = param_cb(params, cbarg);
211 OSSL_PARAM_BLD_free_params(params);
212 err:
213 OSSL_PARAM_BLD_free(tmpl);
214 return ok;
215 }
216
217 /* IMEXPORT = IMPORT + EXPORT */
218
219 # define DSA_IMEXPORTABLE_PARAMETERS \
220 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
221 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
222 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
223 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
224 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
225 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
226 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
227 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0)
228 # define DSA_IMEXPORTABLE_PUBLIC_KEY \
229 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
230 # define DSA_IMEXPORTABLE_PRIVATE_KEY \
231 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
232 static const OSSL_PARAM dsa_all_types[] = {
233 DSA_IMEXPORTABLE_PARAMETERS,
234 DSA_IMEXPORTABLE_PUBLIC_KEY,
235 DSA_IMEXPORTABLE_PRIVATE_KEY,
236 OSSL_PARAM_END
237 };
238 static const OSSL_PARAM dsa_parameter_types[] = {
239 DSA_IMEXPORTABLE_PARAMETERS,
240 OSSL_PARAM_END
241 };
242 static const OSSL_PARAM dsa_key_types[] = {
243 DSA_IMEXPORTABLE_PUBLIC_KEY,
244 DSA_IMEXPORTABLE_PRIVATE_KEY,
245 OSSL_PARAM_END
246 };
247 static const OSSL_PARAM *dsa_types[] = {
248 NULL, /* Index 0 = none of them */
249 dsa_parameter_types, /* Index 1 = parameter types */
250 dsa_key_types, /* Index 2 = key types */
251 dsa_all_types /* Index 3 = 1 + 2 */
252 };
253
254 static const OSSL_PARAM *dsa_imexport_types(int selection)
255 {
256 int type_select = 0;
257
258 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
259 type_select += 1;
260 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
261 type_select += 2;
262 return dsa_types[type_select];
263 }
264
265 static const OSSL_PARAM *dsa_import_types(int selection)
266 {
267 return dsa_imexport_types(selection);
268 }
269
270 static const OSSL_PARAM *dsa_export_types(int selection)
271 {
272 return dsa_imexport_types(selection);
273 }
274
275 static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
276 {
277 DSA *dsa = key;
278 OSSL_PARAM *p;
279
280 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
281 && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
282 return 0;
283 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
284 && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
285 return 0;
286 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
287 && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
288 return 0;
289 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
290 && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
291 return 0;
292 return ossl_ffc_params_todata(dsa_get0_params(dsa), NULL, params)
293 && dsa_key_todata(dsa, NULL, params);
294 }
295
296 static const OSSL_PARAM dsa_params[] = {
297 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
298 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
299 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
300 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
301 DSA_IMEXPORTABLE_PARAMETERS,
302 DSA_IMEXPORTABLE_PUBLIC_KEY,
303 DSA_IMEXPORTABLE_PRIVATE_KEY,
304 OSSL_PARAM_END
305 };
306
307 static const OSSL_PARAM *dsa_gettable_params(void *provctx)
308 {
309 return dsa_params;
310 }
311
312 static int dsa_validate_domparams(const DSA *dsa, int checktype)
313 {
314 int status = 0;
315
316 return dsa_check_params(dsa, checktype, &status);
317 }
318
319 static int dsa_validate_public(const DSA *dsa)
320 {
321 int status = 0;
322 const BIGNUM *pub_key = NULL;
323
324 DSA_get0_key(dsa, &pub_key, NULL);
325 if (pub_key == NULL)
326 return 0;
327 return dsa_check_pub_key(dsa, pub_key, &status);
328 }
329
330 static int dsa_validate_private(const DSA *dsa)
331 {
332 int status = 0;
333 const BIGNUM *priv_key = NULL;
334
335 DSA_get0_key(dsa, NULL, &priv_key);
336 if (priv_key == NULL)
337 return 0;
338 return dsa_check_priv_key(dsa, priv_key, &status);
339 }
340
341 static int dsa_validate(const void *keydata, int selection, int checktype)
342 {
343 const DSA *dsa = keydata;
344 int ok = 0;
345
346 if (!ossl_prov_is_running())
347 return 0;
348
349 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
350 ok = 1;
351
352 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
353 ok = ok && dsa_validate_domparams(dsa, checktype);
354
355 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
356 ok = ok && dsa_validate_public(dsa);
357
358 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
359 ok = ok && dsa_validate_private(dsa);
360
361 /* If the whole key is selected, we do a pairwise validation */
362 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
363 == OSSL_KEYMGMT_SELECT_KEYPAIR)
364 ok = ok && dsa_check_pairwise(dsa);
365 return ok;
366 }
367
368 static void *dsa_gen_init(void *provctx, int selection)
369 {
370 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
371 struct dsa_gen_ctx *gctx = NULL;
372
373 if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0)
374 return NULL;
375
376 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
377 gctx->selection = selection;
378 gctx->libctx = libctx;
379 gctx->pbits = 2048;
380 gctx->qbits = 224;
381 #ifdef FIPS_MODULE
382 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4;
383 #else
384 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_2;
385 #endif
386 gctx->gindex = -1;
387 gctx->pcounter = -1;
388 gctx->hindex = 0;
389 }
390 return gctx;
391 }
392
393 static int dsa_gen_set_template(void *genctx, void *templ)
394 {
395 struct dsa_gen_ctx *gctx = genctx;
396 DSA *dsa = templ;
397
398 if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL)
399 return 0;
400 gctx->ffc_params = dsa_get0_params(dsa);
401 return 1;
402 }
403
404 static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed,
405 size_t seedlen)
406 {
407 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
408 gctx->seed = NULL;
409 gctx->seedlen = 0;
410 if (seed != NULL && seedlen > 0) {
411 gctx->seed = OPENSSL_memdup(seed, seedlen);
412 if (gctx->seed == NULL)
413 return 0;
414 gctx->seedlen = seedlen;
415 }
416 return 1;
417 }
418
419 static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
420 {
421 struct dsa_gen_ctx *gctx = genctx;
422 const OSSL_PARAM *p;
423
424 if (gctx == NULL)
425 return 0;
426
427 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
428 if (p != NULL) {
429 if (p->data_type != OSSL_PARAM_UTF8_STRING
430 || ((gctx->gen_type = dsa_gen_type_name2id(p->data)) == -1)) {
431 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
432 return 0;
433 }
434 }
435 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
436 if (p != NULL
437 && !OSSL_PARAM_get_int(p, &gctx->gindex))
438 return 0;
439 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
440 if (p != NULL
441 && !OSSL_PARAM_get_int(p, &gctx->pcounter))
442 return 0;
443 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
444 if (p != NULL
445 && !OSSL_PARAM_get_int(p, &gctx->hindex))
446 return 0;
447 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
448 if (p != NULL
449 && (p->data_type != OSSL_PARAM_OCTET_STRING
450 || !dsa_set_gen_seed(gctx, p->data, p->data_size)))
451 return 0;
452 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
453 && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
454 return 0;
455 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
456 && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
457 return 0;
458 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
459 if (p != NULL) {
460 if (p->data_type != OSSL_PARAM_UTF8_STRING)
461 return 0;
462 OPENSSL_free(gctx->mdname);
463 gctx->mdname = OPENSSL_strdup(p->data);
464 if (gctx->mdname == NULL)
465 return 0;
466 }
467 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
468 if (p != NULL) {
469 if (p->data_type != OSSL_PARAM_UTF8_STRING)
470 return 0;
471 OPENSSL_free(gctx->mdprops);
472 gctx->mdprops = OPENSSL_strdup(p->data);
473 if (gctx->mdprops == NULL)
474 return 0;
475 }
476 return 1;
477 }
478
479 static const OSSL_PARAM *dsa_gen_settable_params(void *provctx)
480 {
481 static OSSL_PARAM settable[] = {
482 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
483 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
484 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
485 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
486 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
487 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
488 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
489 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
490 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
491 OSSL_PARAM_END
492 };
493 return settable;
494 }
495
496 static int dsa_gencb(int p, int n, BN_GENCB *cb)
497 {
498 struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
499 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
500
501 params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
502 params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
503
504 return gctx->cb(params, gctx->cbarg);
505 }
506
507 static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
508 {
509 struct dsa_gen_ctx *gctx = genctx;
510 DSA *dsa = NULL;
511 BN_GENCB *gencb = NULL;
512 int ret = 0;
513 FFC_PARAMS *ffc;
514
515 if (!ossl_prov_is_running() || gctx == NULL)
516 return NULL;
517 dsa = dsa_new_with_ctx(gctx->libctx);
518 if (dsa == NULL)
519 return NULL;
520
521 gctx->cb = osslcb;
522 gctx->cbarg = cbarg;
523 gencb = BN_GENCB_new();
524 if (gencb != NULL)
525 BN_GENCB_set(gencb, dsa_gencb, genctx);
526
527 ffc = dsa_get0_params(dsa);
528 /* Copy the template value if one was passed */
529 if (gctx->ffc_params != NULL
530 && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
531 goto end;
532
533 if (gctx->seed != NULL
534 && !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
535 goto end;
536 if (gctx->gindex != -1) {
537 ossl_ffc_params_set_gindex(ffc, gctx->gindex);
538 if (gctx->pcounter != -1)
539 ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
540 } else if (gctx->hindex != 0) {
541 ossl_ffc_params_set_h(ffc, gctx->hindex);
542 }
543 if (gctx->mdname != NULL) {
544 if (!ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops))
545 goto end;
546 }
547 if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
548
549 if (dsa_generate_ffc_parameters(dsa, gctx->gen_type,
550 gctx->pbits, gctx->qbits,
551 gencb) <= 0)
552 goto end;
553 }
554 ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
555 gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_186_2);
556 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
557 if (ffc->p == NULL
558 || ffc->q == NULL
559 || ffc->g == NULL)
560 goto end;
561 if (DSA_generate_key(dsa) <= 0)
562 goto end;
563 }
564 ret = 1;
565 end:
566 if (ret <= 0) {
567 DSA_free(dsa);
568 dsa = NULL;
569 }
570 BN_GENCB_free(gencb);
571 return dsa;
572 }
573
574 static void dsa_gen_cleanup(void *genctx)
575 {
576 struct dsa_gen_ctx *gctx = genctx;
577
578 if (gctx == NULL)
579 return;
580
581 OPENSSL_free(gctx->mdname);
582 OPENSSL_free(gctx->mdprops);
583 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
584 OPENSSL_free(gctx);
585 }
586
587 void *dsa_load(const void *reference, size_t reference_sz)
588 {
589 DSA *dsa = NULL;
590
591 if (ossl_prov_is_running() && reference_sz == sizeof(dsa)) {
592 /* The contents of the reference is the address to our object */
593 dsa = *(DSA **)reference;
594 /* We grabbed, so we detach it */
595 *(DSA **)reference = NULL;
596 return dsa;
597 }
598 return NULL;
599 }
600
601 const OSSL_DISPATCH ossl_dsa_keymgmt_functions[] = {
602 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
603 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init },
604 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template },
605 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params },
606 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
607 (void (*)(void))dsa_gen_settable_params },
608 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen },
609 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup },
610 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dsa_load },
611 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
612 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
613 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
614 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
615 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match },
616 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate },
617 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
618 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
619 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
620 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },
621 { 0, NULL }
622 };