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