Coverage Report

Created: 2025-06-13 06:55

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