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