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