Coverage Report

Created: 2025-06-13 06:58

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