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