VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.2/providers/implementations/keymgmt/dsa_kmgmt.c@ 94404

Last change on this file since 94404 was 94404, checked in by vboxsync, 3 years ago

libs/openssl: Update to 3.0.2 and switch to it, bugref:10128

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette