Coverage Report

Created: 2025-12-31 06:58

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