]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/keymgmt/dsa_kmgmt.c
provider: add params argument to key manager's gen_init call
[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 ossl_dsa_new(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 = ossl_dsa_get0_params((DSA *)dsa1);
164 FFC_PARAMS *dsaparams2 = ossl_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 && ossl_dsa_ffc_params_fromdata(dsa, params);
184 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
185 ok = ok && ossl_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(ossl_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(ossl_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 ossl_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 ossl_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 ossl_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 && ossl_dsa_check_pairwise(dsa);
365 return ok;
366 }
367
368 static void *dsa_gen_init(void *provctx, int selection,
369 const OSSL_PARAM params[])
370 {
371 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
372 struct dsa_gen_ctx *gctx = NULL;
373
374 if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0)
375 return NULL;
376
377 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
378 gctx->selection = selection;
379 gctx->libctx = libctx;
380 gctx->pbits = 2048;
381 gctx->qbits = 224;
382 #ifdef FIPS_MODULE
383 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4;
384 #else
385 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_2;
386 #endif
387 gctx->gindex = -1;
388 gctx->pcounter = -1;
389 gctx->hindex = 0;
390 }
391 if (!dsa_gen_set_params(gctx, params)) {
392 OPENSSL_free(gctx);
393 gctx = NULL;
394 }
395 return gctx;
396 }
397
398 static int dsa_gen_set_template(void *genctx, void *templ)
399 {
400 struct dsa_gen_ctx *gctx = genctx;
401 DSA *dsa = templ;
402
403 if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL)
404 return 0;
405 gctx->ffc_params = ossl_dsa_get0_params(dsa);
406 return 1;
407 }
408
409 static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed,
410 size_t seedlen)
411 {
412 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
413 gctx->seed = NULL;
414 gctx->seedlen = 0;
415 if (seed != NULL && seedlen > 0) {
416 gctx->seed = OPENSSL_memdup(seed, seedlen);
417 if (gctx->seed == NULL)
418 return 0;
419 gctx->seedlen = seedlen;
420 }
421 return 1;
422 }
423
424 static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
425 {
426 struct dsa_gen_ctx *gctx = genctx;
427 const OSSL_PARAM *p;
428
429 if (gctx == NULL)
430 return 0;
431 if (params == NULL)
432 return 1;
433
434
435 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
436 if (p != NULL) {
437 if (p->data_type != OSSL_PARAM_UTF8_STRING
438 || ((gctx->gen_type = dsa_gen_type_name2id(p->data)) == -1)) {
439 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
440 return 0;
441 }
442 }
443 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
444 if (p != NULL
445 && !OSSL_PARAM_get_int(p, &gctx->gindex))
446 return 0;
447 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
448 if (p != NULL
449 && !OSSL_PARAM_get_int(p, &gctx->pcounter))
450 return 0;
451 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
452 if (p != NULL
453 && !OSSL_PARAM_get_int(p, &gctx->hindex))
454 return 0;
455 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
456 if (p != NULL
457 && (p->data_type != OSSL_PARAM_OCTET_STRING
458 || !dsa_set_gen_seed(gctx, p->data, p->data_size)))
459 return 0;
460 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
461 && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
462 return 0;
463 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
464 && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
465 return 0;
466 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
467 if (p != NULL) {
468 if (p->data_type != OSSL_PARAM_UTF8_STRING)
469 return 0;
470 OPENSSL_free(gctx->mdname);
471 gctx->mdname = OPENSSL_strdup(p->data);
472 if (gctx->mdname == NULL)
473 return 0;
474 }
475 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
476 if (p != NULL) {
477 if (p->data_type != OSSL_PARAM_UTF8_STRING)
478 return 0;
479 OPENSSL_free(gctx->mdprops);
480 gctx->mdprops = OPENSSL_strdup(p->data);
481 if (gctx->mdprops == NULL)
482 return 0;
483 }
484 return 1;
485 }
486
487 static const OSSL_PARAM *dsa_gen_settable_params(ossl_unused void *genctx,
488 ossl_unused void *provctx)
489 {
490 static OSSL_PARAM settable[] = {
491 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
492 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
493 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
494 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
495 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
496 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
497 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
498 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
499 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
500 OSSL_PARAM_END
501 };
502 return settable;
503 }
504
505 static int dsa_gencb(int p, int n, BN_GENCB *cb)
506 {
507 struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
508 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
509
510 params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
511 params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
512
513 return gctx->cb(params, gctx->cbarg);
514 }
515
516 static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
517 {
518 struct dsa_gen_ctx *gctx = genctx;
519 DSA *dsa = NULL;
520 BN_GENCB *gencb = NULL;
521 int ret = 0;
522 FFC_PARAMS *ffc;
523
524 if (!ossl_prov_is_running() || gctx == NULL)
525 return NULL;
526 dsa = ossl_dsa_new(gctx->libctx);
527 if (dsa == NULL)
528 return NULL;
529
530 gctx->cb = osslcb;
531 gctx->cbarg = cbarg;
532 gencb = BN_GENCB_new();
533 if (gencb != NULL)
534 BN_GENCB_set(gencb, dsa_gencb, genctx);
535
536 ffc = ossl_dsa_get0_params(dsa);
537 /* Copy the template value if one was passed */
538 if (gctx->ffc_params != NULL
539 && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
540 goto end;
541
542 if (gctx->seed != NULL
543 && !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
544 goto end;
545 if (gctx->gindex != -1) {
546 ossl_ffc_params_set_gindex(ffc, gctx->gindex);
547 if (gctx->pcounter != -1)
548 ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
549 } else if (gctx->hindex != 0) {
550 ossl_ffc_params_set_h(ffc, gctx->hindex);
551 }
552 if (gctx->mdname != NULL) {
553 if (!ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops))
554 goto end;
555 }
556 if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
557
558 if (ossl_dsa_generate_ffc_parameters(dsa, gctx->gen_type,
559 gctx->pbits, gctx->qbits,
560 gencb) <= 0)
561 goto end;
562 }
563 ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
564 gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_186_2);
565 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
566 if (ffc->p == NULL
567 || ffc->q == NULL
568 || ffc->g == NULL)
569 goto end;
570 if (DSA_generate_key(dsa) <= 0)
571 goto end;
572 }
573 ret = 1;
574 end:
575 if (ret <= 0) {
576 DSA_free(dsa);
577 dsa = NULL;
578 }
579 BN_GENCB_free(gencb);
580 return dsa;
581 }
582
583 static void dsa_gen_cleanup(void *genctx)
584 {
585 struct dsa_gen_ctx *gctx = genctx;
586
587 if (gctx == NULL)
588 return;
589
590 OPENSSL_free(gctx->mdname);
591 OPENSSL_free(gctx->mdprops);
592 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
593 OPENSSL_free(gctx);
594 }
595
596 void *dsa_load(const void *reference, size_t reference_sz)
597 {
598 DSA *dsa = NULL;
599
600 if (ossl_prov_is_running() && reference_sz == sizeof(dsa)) {
601 /* The contents of the reference is the address to our object */
602 dsa = *(DSA **)reference;
603 /* We grabbed, so we detach it */
604 *(DSA **)reference = NULL;
605 return dsa;
606 }
607 return NULL;
608 }
609
610 const OSSL_DISPATCH ossl_dsa_keymgmt_functions[] = {
611 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
612 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init },
613 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template },
614 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params },
615 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
616 (void (*)(void))dsa_gen_settable_params },
617 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen },
618 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup },
619 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dsa_load },
620 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
621 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
622 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
623 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
624 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match },
625 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate },
626 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
627 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
628 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
629 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },
630 { 0, NULL }
631 };