Coverage Report

Created: 2025-12-10 06:24

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