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