Coverage Report

Created: 2025-12-31 06:58

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