Coverage Report

Created: 2024-07-27 06:38

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