Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/keymgmt/dh_kmgmt.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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
 * DH low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
#include "internal/common.h"
16
17
#include <string.h> /* strcmp */
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/bn.h>
21
#include <openssl/err.h>
22
#include <openssl/self_test.h>
23
#include "prov/implementations.h"
24
#include "prov/providercommon.h"
25
#include "prov/provider_ctx.h"
26
#include "crypto/dh.h"
27
#include "internal/fips.h"
28
#include "internal/sizes.h"
29
#include "fips/fipsindicator.h"
30
31
static OSSL_FUNC_keymgmt_new_fn dh_newdata;
32
static OSSL_FUNC_keymgmt_free_fn dh_freedata;
33
static OSSL_FUNC_keymgmt_gen_init_fn dh_gen_init;
34
static OSSL_FUNC_keymgmt_gen_init_fn dhx_gen_init;
35
static OSSL_FUNC_keymgmt_gen_set_template_fn dh_gen_set_template;
36
static OSSL_FUNC_keymgmt_gen_set_params_fn dh_gen_set_params;
37
static OSSL_FUNC_keymgmt_gen_settable_params_fn dh_gen_settable_params;
38
static OSSL_FUNC_keymgmt_gen_fn dh_gen;
39
static OSSL_FUNC_keymgmt_gen_cleanup_fn dh_gen_cleanup;
40
static OSSL_FUNC_keymgmt_load_fn dh_load;
41
static OSSL_FUNC_keymgmt_get_params_fn dh_get_params;
42
static OSSL_FUNC_keymgmt_gettable_params_fn dh_gettable_params;
43
static OSSL_FUNC_keymgmt_set_params_fn dh_set_params;
44
static OSSL_FUNC_keymgmt_settable_params_fn dh_settable_params;
45
static OSSL_FUNC_keymgmt_has_fn dh_has;
46
static OSSL_FUNC_keymgmt_match_fn dh_match;
47
static OSSL_FUNC_keymgmt_validate_fn dh_validate;
48
static OSSL_FUNC_keymgmt_import_fn dh_import;
49
static OSSL_FUNC_keymgmt_import_types_fn dh_import_types;
50
static OSSL_FUNC_keymgmt_export_fn dh_export;
51
static OSSL_FUNC_keymgmt_export_types_fn dh_export_types;
52
static OSSL_FUNC_keymgmt_dup_fn dh_dup;
53
54
#define DH_POSSIBLE_SELECTIONS \
55
67.7k
    (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
56
57
struct dh_gen_ctx {
58
    OSSL_LIB_CTX *libctx;
59
60
    FFC_PARAMS *ffc_params;
61
    int selection;
62
    /* All these parameters are used for parameter generation only */
63
    /* If there is a group name then the remaining parameters are not needed */
64
    int group_nid;
65
    size_t pbits;
66
    size_t qbits;
67
    unsigned char *seed; /* optional FIPS186-4 param for testing */
68
    size_t seedlen;
69
    int gindex; /* optional  FIPS186-4 generator index (ignored if -1) */
70
    int gen_type; /* see dhtype2id */
71
    int generator; /* Used by DH_PARAMGEN_TYPE_GENERATOR in non fips mode only */
72
    int pcounter;
73
    int hindex;
74
    int priv_len;
75
76
    char *mdname;
77
    char *mdprops;
78
    OSSL_CALLBACK *cb;
79
    void *cbarg;
80
    int dh_type;
81
};
82
83
static int dh_gen_type_name2id_w_default(const char *name, int type)
84
0
{
85
0
    if (strcmp(name, "default") == 0) {
86
#ifdef FIPS_MODULE
87
        if (type == DH_FLAG_TYPE_DHX)
88
            return DH_PARAMGEN_TYPE_FIPS_186_4;
89
90
        return DH_PARAMGEN_TYPE_GROUP;
91
#else
92
0
        if (type == DH_FLAG_TYPE_DHX)
93
0
            return DH_PARAMGEN_TYPE_FIPS_186_2;
94
95
0
        return DH_PARAMGEN_TYPE_GENERATOR;
96
0
#endif
97
0
    }
98
99
0
    return ossl_dh_gen_type_name2id(name, type);
100
0
}
101
102
static void *dh_newdata(void *provctx)
103
5.46k
{
104
5.46k
    DH *dh = NULL;
105
106
5.46k
    if (ossl_prov_is_running()) {
107
5.46k
        dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
108
5.46k
        if (dh != NULL) {
109
5.46k
            DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
110
5.46k
            DH_set_flags(dh, DH_FLAG_TYPE_DH);
111
5.46k
        }
112
5.46k
    }
113
5.46k
    return dh;
114
5.46k
}
115
116
static void *dhx_newdata(void *provctx)
117
0
{
118
0
    DH *dh = NULL;
119
120
0
    dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
121
0
    if (dh != NULL) {
122
0
        DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
123
0
        DH_set_flags(dh, DH_FLAG_TYPE_DHX);
124
0
    }
125
0
    return dh;
126
0
}
127
128
static void dh_freedata(void *keydata)
129
79.0k
{
130
79.0k
    DH_free(keydata);
131
79.0k
}
132
133
static int dh_has(const void *keydata, int selection)
134
9.04k
{
135
9.04k
    const DH *dh = keydata;
136
9.04k
    int ok = 1;
137
138
9.04k
    if (!ossl_prov_is_running() || dh == NULL)
139
918
        return 0;
140
8.12k
    if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
141
0
        return 1; /* the selection is not missing */
142
143
8.12k
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
144
7.00k
        ok = ok && (DH_get0_pub_key(dh) != NULL);
145
8.12k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
146
82
        ok = ok && (DH_get0_priv_key(dh) != NULL);
147
8.12k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
148
1.03k
        ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
149
8.12k
    return ok;
150
8.12k
}
151
152
static int dh_match(const void *keydata1, const void *keydata2, int selection)
153
6.60k
{
154
6.60k
    const DH *dh1 = keydata1;
155
6.60k
    const DH *dh2 = keydata2;
156
6.60k
    int ok = 1;
157
158
6.60k
    if (!ossl_prov_is_running())
159
0
        return 0;
160
161
6.60k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
162
6.60k
        int key_checked = 0;
163
164
6.60k
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
165
6.60k
            const BIGNUM *pa = DH_get0_pub_key(dh1);
166
6.60k
            const BIGNUM *pb = DH_get0_pub_key(dh2);
167
168
6.60k
            if (pa != NULL && pb != NULL) {
169
405
                ok = ok && BN_cmp(pa, pb) == 0;
170
405
                key_checked = 1;
171
405
            }
172
6.60k
        }
173
6.60k
        if (!key_checked
174
6.19k
            && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
175
6.19k
            const BIGNUM *pa = DH_get0_priv_key(dh1);
176
6.19k
            const BIGNUM *pb = DH_get0_priv_key(dh2);
177
178
6.19k
            if (pa != NULL && pb != NULL) {
179
0
                ok = ok && BN_cmp(pa, pb) == 0;
180
0
                key_checked = 1;
181
0
            }
182
6.19k
        }
183
6.60k
        ok = ok && key_checked;
184
6.60k
    }
185
6.60k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
186
6.60k
        FFC_PARAMS *dhparams1 = ossl_dh_get0_params((DH *)dh1);
187
6.60k
        FFC_PARAMS *dhparams2 = ossl_dh_get0_params((DH *)dh2);
188
189
6.60k
        ok = ok && ossl_ffc_params_cmp(dhparams1, dhparams2, 1);
190
6.60k
    }
191
6.60k
    return ok;
192
6.60k
}
193
194
static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
195
10.2k
{
196
10.2k
    DH *dh = keydata;
197
10.2k
    int ok = 1;
198
199
10.2k
    if (!ossl_prov_is_running() || dh == NULL)
200
0
        return 0;
201
202
10.2k
    if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
203
0
        return 0;
204
205
    /* a key without parameters is meaningless */
206
10.2k
    ok = ok && ossl_dh_params_fromdata(dh, params);
207
208
10.2k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
209
10.2k
        int include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
210
211
10.2k
        ok = ok && ossl_dh_key_fromdata(dh, params, include_private);
212
10.2k
    }
213
214
10.2k
    return ok;
215
10.2k
}
216
217
static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
218
    void *cbarg)
219
0
{
220
0
    DH *dh = keydata;
221
0
    OSSL_PARAM_BLD *tmpl = NULL;
222
0
    OSSL_PARAM *params = NULL;
223
0
    int ok = 1;
224
225
0
    if (!ossl_prov_is_running() || dh == NULL)
226
0
        return 0;
227
228
0
    if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
229
0
        return 0;
230
231
0
    tmpl = OSSL_PARAM_BLD_new();
232
0
    if (tmpl == NULL)
233
0
        return 0;
234
235
0
    if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
236
0
        ok = ok && ossl_dh_params_todata(dh, tmpl, NULL);
237
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 && ossl_dh_key_todata(dh, tmpl, 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_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 DH_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_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),          \
267
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), \
268
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0)
269
#define DH_IMEXPORTABLE_PUBLIC_KEY \
270
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
271
#define DH_IMEXPORTABLE_PRIVATE_KEY \
272
    OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
273
static const OSSL_PARAM dh_all_types[] = {
274
    DH_IMEXPORTABLE_PARAMETERS,
275
    DH_IMEXPORTABLE_PUBLIC_KEY,
276
    DH_IMEXPORTABLE_PRIVATE_KEY,
277
    OSSL_PARAM_END
278
};
279
static const OSSL_PARAM dh_parameter_types[] = {
280
    DH_IMEXPORTABLE_PARAMETERS,
281
    OSSL_PARAM_END
282
};
283
static const OSSL_PARAM dh_key_types[] = {
284
    DH_IMEXPORTABLE_PUBLIC_KEY,
285
    DH_IMEXPORTABLE_PRIVATE_KEY,
286
    OSSL_PARAM_END
287
};
288
static const OSSL_PARAM *dh_types[] = {
289
    NULL, /* Index 0 = none of them */
290
    dh_parameter_types, /* Index 1 = parameter types */
291
    dh_key_types, /* Index 2 = key types */
292
    dh_all_types /* Index 3 = 1 + 2 */
293
};
294
295
static const OSSL_PARAM *dh_imexport_types(int selection)
296
0
{
297
0
    int type_select = 0;
298
299
0
    if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
300
0
        type_select += 1;
301
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
302
0
        type_select += 2;
303
0
    return dh_types[type_select];
304
0
}
305
306
static const OSSL_PARAM *dh_import_types(int selection)
307
0
{
308
0
    return dh_imexport_types(selection);
309
0
}
310
311
static const OSSL_PARAM *dh_export_types(int selection)
312
0
{
313
0
    return dh_imexport_types(selection);
314
0
}
315
316
static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
317
46.6k
{
318
46.6k
    DH *dh = key;
319
46.6k
    OSSL_PARAM *p;
320
321
46.6k
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
322
43.3k
        && !OSSL_PARAM_set_int(p, DH_bits(dh)))
323
0
        return 0;
324
46.6k
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
325
43.3k
        && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
326
0
        return 0;
327
46.6k
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
328
43.3k
        && !OSSL_PARAM_set_int(p, DH_size(dh)))
329
0
        return 0;
330
46.6k
    if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
331
3.26k
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
332
0
            return 0;
333
3.26k
        p->return_size = ossl_dh_key2buf(dh, (unsigned char **)&p->data,
334
3.26k
            p->data_size, 0);
335
3.26k
        if (p->return_size == 0)
336
0
            return 0;
337
3.26k
    }
338
339
46.6k
    return ossl_dh_params_todata(dh, NULL, params)
340
46.6k
        && ossl_dh_key_todata(dh, NULL, params, 1);
341
46.6k
}
342
343
static const OSSL_PARAM dh_params[] = {
344
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
345
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
346
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
347
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
348
    DH_IMEXPORTABLE_PARAMETERS,
349
    DH_IMEXPORTABLE_PUBLIC_KEY,
350
    DH_IMEXPORTABLE_PRIVATE_KEY,
351
    OSSL_PARAM_END
352
};
353
354
static const OSSL_PARAM *dh_gettable_params(void *provctx)
355
0
{
356
0
    return dh_params;
357
0
}
358
359
static const OSSL_PARAM dh_known_settable_params[] = {
360
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
361
    OSSL_PARAM_END
362
};
363
364
static const OSSL_PARAM *dh_settable_params(void *provctx)
365
0
{
366
0
    return dh_known_settable_params;
367
0
}
368
369
static int dh_set_params(void *key, const OSSL_PARAM params[])
370
549
{
371
549
    DH *dh = key;
372
549
    const OSSL_PARAM *p;
373
374
549
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
375
549
    if (p != NULL
376
549
        && (p->data_type != OSSL_PARAM_OCTET_STRING
377
549
            || !ossl_dh_buf2key(dh, p->data, p->data_size)))
378
18
        return 0;
379
380
531
    return 1;
381
549
}
382
383
static int dh_validate_public(const DH *dh, int checktype)
384
26.0k
{
385
26.0k
    const BIGNUM *pub_key = NULL;
386
26.0k
    int res = 0;
387
388
26.0k
    DH_get0_key(dh, &pub_key, NULL);
389
26.0k
    if (pub_key == NULL)
390
12.3k
        return 0;
391
392
    /*
393
     * The partial test is only valid for named group's with q = (p - 1) / 2
394
     * but for that case it is also fully sufficient to check the key validity.
395
     */
396
13.7k
    if (ossl_dh_is_named_safe_prime_group(dh))
397
1.31k
        return ossl_dh_check_pub_key_partial(dh, pub_key, &res);
398
399
12.4k
    return DH_check_pub_key_ex(dh, pub_key);
400
13.7k
}
401
402
static int dh_validate_private(const DH *dh)
403
6.58k
{
404
6.58k
    int status = 0;
405
6.58k
    const BIGNUM *priv_key = NULL;
406
407
6.58k
    DH_get0_key(dh, NULL, &priv_key);
408
6.58k
    if (priv_key == NULL)
409
6.54k
        return 0;
410
46
    return ossl_dh_check_priv_key(dh, priv_key, &status);
411
6.58k
}
412
413
static int dh_validate(const void *keydata, int selection, int checktype)
414
49.4k
{
415
49.4k
    const DH *dh = keydata;
416
49.4k
    int ok = 1;
417
418
49.4k
    if (!ossl_prov_is_running())
419
0
        return 0;
420
421
49.4k
    if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
422
0
        return 1; /* nothing to validate */
423
424
49.4k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
425
        /*
426
         * Both of these functions check parameters. DH_check_params_ex()
427
         * performs a lightweight check (e.g. it does not check that p is a
428
         * safe prime)
429
         */
430
16.7k
        if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
431
10.2k
            ok = ok && DH_check_params_ex(dh);
432
6.53k
        else
433
6.53k
            ok = ok && DH_check_ex(dh);
434
16.7k
    }
435
436
49.4k
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
437
26.0k
        ok = ok && dh_validate_public(dh, checktype);
438
439
49.4k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
440
13.1k
        ok = ok && dh_validate_private(dh);
441
442
49.4k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
443
49.4k
        == OSSL_KEYMGMT_SELECT_KEYPAIR)
444
6.55k
        ok = ok && ossl_dh_check_pairwise(dh, 0);
445
49.4k
    return ok;
446
49.4k
}
447
448
static void *dh_gen_init_base(void *provctx, int selection,
449
    const OSSL_PARAM params[], int type)
450
7.34k
{
451
7.34k
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
452
7.34k
    struct dh_gen_ctx *gctx = NULL;
453
454
7.34k
    if (!ossl_prov_is_running())
455
0
        return NULL;
456
457
7.34k
    if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
458
0
        return NULL;
459
460
7.34k
    if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
461
7.34k
        gctx->selection = selection;
462
7.34k
        gctx->libctx = libctx;
463
7.34k
        gctx->pbits = 2048;
464
7.34k
        gctx->qbits = 224;
465
7.34k
        gctx->mdname = NULL;
466
#ifdef FIPS_MODULE
467
        gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
468
            ? DH_PARAMGEN_TYPE_FIPS_186_4
469
            : DH_PARAMGEN_TYPE_GROUP;
470
#else
471
7.34k
        gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
472
7.34k
            ? DH_PARAMGEN_TYPE_FIPS_186_2
473
7.34k
            : DH_PARAMGEN_TYPE_GENERATOR;
474
7.34k
#endif
475
7.34k
        gctx->gindex = -1;
476
7.34k
        gctx->hindex = 0;
477
7.34k
        gctx->pcounter = -1;
478
7.34k
        gctx->generator = DH_GENERATOR_2;
479
7.34k
        gctx->dh_type = type;
480
7.34k
    }
481
7.34k
    if (!dh_gen_set_params(gctx, params)) {
482
0
        OPENSSL_free(gctx);
483
0
        gctx = NULL;
484
0
    }
485
7.34k
    return gctx;
486
7.34k
}
487
488
static void *dh_gen_init(void *provctx, int selection,
489
    const OSSL_PARAM params[])
490
7.34k
{
491
7.34k
    return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DH);
492
7.34k
}
493
494
static void *dhx_gen_init(void *provctx, int selection,
495
    const OSSL_PARAM params[])
496
0
{
497
0
    return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DHX);
498
0
}
499
500
static int dh_gen_set_template(void *genctx, void *templ)
501
4.48k
{
502
4.48k
    struct dh_gen_ctx *gctx = genctx;
503
4.48k
    DH *dh = templ;
504
505
4.48k
    if (!ossl_prov_is_running() || gctx == NULL || dh == NULL)
506
0
        return 0;
507
4.48k
    gctx->ffc_params = ossl_dh_get0_params(dh);
508
4.48k
    return 1;
509
4.48k
}
510
511
static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed,
512
    size_t seedlen)
513
0
{
514
0
    OPENSSL_clear_free(gctx->seed, gctx->seedlen);
515
0
    gctx->seed = NULL;
516
0
    gctx->seedlen = 0;
517
0
    if (seed != NULL && seedlen > 0) {
518
0
        gctx->seed = OPENSSL_memdup(seed, seedlen);
519
0
        if (gctx->seed == NULL)
520
0
            return 0;
521
0
        gctx->seedlen = seedlen;
522
0
    }
523
0
    return 1;
524
0
}
525
526
static int dh_gen_common_set_params(void *genctx, const OSSL_PARAM params[])
527
2.46k
{
528
2.46k
    struct dh_gen_ctx *gctx = genctx;
529
2.46k
    const OSSL_PARAM *p;
530
2.46k
    int gen_type = -1;
531
532
2.46k
    if (gctx == NULL)
533
0
        return 0;
534
2.46k
    if (ossl_param_is_empty(params))
535
2.07k
        return 1;
536
537
389
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
538
389
    if (p != NULL) {
539
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING
540
0
            || ((gen_type = dh_gen_type_name2id_w_default(p->data, gctx->dh_type)) == -1)) {
541
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
542
0
            return 0;
543
0
        }
544
0
        if (gen_type != -1)
545
0
            gctx->gen_type = gen_type;
546
0
    }
547
389
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
548
389
    if (p != NULL) {
549
389
        const DH_NAMED_GROUP *group = NULL;
550
551
389
        if (p->data_type != OSSL_PARAM_UTF8_STRING
552
389
            || p->data == NULL
553
389
            || (group = ossl_ffc_name_to_dh_named_group(p->data)) == NULL
554
389
            || ((gctx->group_nid = ossl_ffc_named_group_get_uid(group)) == NID_undef)) {
555
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
556
0
            return 0;
557
0
        }
558
389
    }
559
389
    if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
560
0
        && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
561
0
        return 0;
562
389
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
563
389
    if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len))
564
0
        return 0;
565
389
    return 1;
566
389
}
567
568
static const OSSL_PARAM *dh_gen_settable_params(ossl_unused void *genctx,
569
    ossl_unused void *provctx)
570
0
{
571
0
    static const OSSL_PARAM dh_gen_settable[] = {
572
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
573
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
574
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
575
0
        OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
576
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL),
577
0
        OSSL_PARAM_END
578
0
    };
579
0
    return dh_gen_settable;
580
0
}
581
582
static const OSSL_PARAM *dhx_gen_settable_params(ossl_unused void *genctx,
583
    ossl_unused void *provctx)
584
0
{
585
0
    static const OSSL_PARAM dhx_gen_settable[] = {
586
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
587
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
588
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
589
0
        OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
590
0
        OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
591
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
592
0
        OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
593
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
594
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
595
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
596
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
597
0
        OSSL_PARAM_END
598
0
    };
599
0
    return dhx_gen_settable;
600
0
}
601
602
static int dhx_gen_set_params(void *genctx, const OSSL_PARAM params[])
603
0
{
604
0
    struct dh_gen_ctx *gctx = genctx;
605
0
    const OSSL_PARAM *p;
606
607
0
    if (!dh_gen_common_set_params(genctx, params))
608
0
        return 0;
609
610
    /* Parameters related to fips186-4 and fips186-2 */
611
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
612
0
    if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex))
613
0
        return 0;
614
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
615
0
    if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter))
616
0
        return 0;
617
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
618
0
    if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex))
619
0
        return 0;
620
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
621
0
    if (p != NULL
622
0
        && (p->data_type != OSSL_PARAM_OCTET_STRING
623
0
            || !dh_set_gen_seed(gctx, p->data, p->data_size)))
624
0
        return 0;
625
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
626
0
        && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
627
0
        return 0;
628
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
629
0
    if (p != NULL) {
630
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
631
0
            return 0;
632
0
        OPENSSL_free(gctx->mdname);
633
0
        gctx->mdname = OPENSSL_strdup(p->data);
634
0
        if (gctx->mdname == NULL)
635
0
            return 0;
636
0
    }
637
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
638
0
    if (p != NULL) {
639
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
640
0
            return 0;
641
0
        OPENSSL_free(gctx->mdprops);
642
0
        gctx->mdprops = OPENSSL_strdup(p->data);
643
0
        if (gctx->mdprops == NULL)
644
0
            return 0;
645
0
    }
646
647
    /* Parameters that are not allowed for DHX */
648
0
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
649
0
    if (p != NULL) {
650
0
        ERR_raise(ERR_LIB_PROV, ERR_R_UNSUPPORTED);
651
0
        return 0;
652
0
    }
653
0
    return 1;
654
0
}
655
656
static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[])
657
3.51k
{
658
3.51k
    struct dh_gen_ctx *gctx = genctx;
659
3.51k
    const OSSL_PARAM *p;
660
661
3.51k
    if (!dh_gen_common_set_params(genctx, params))
662
0
        return 0;
663
664
3.51k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
665
3.51k
    if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator))
666
0
        return 0;
667
668
    /* Parameters that are not allowed for DH */
669
3.51k
    if (OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX) != NULL
670
3.51k
        || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER) != NULL
671
3.51k
        || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H) != NULL
672
3.51k
        || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED) != NULL
673
3.51k
        || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS) != NULL
674
3.51k
        || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST) != NULL
675
3.51k
        || OSSL_PARAM_locate_const(params,
676
3.51k
               OSSL_PKEY_PARAM_FFC_DIGEST_PROPS)
677
3.51k
            != NULL) {
678
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
679
0
        return 0;
680
0
    }
681
3.51k
    return 1;
682
3.51k
}
683
684
static int dh_gencb(int p, int n, BN_GENCB *cb)
685
0
{
686
0
    struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb);
687
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
688
689
0
    params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
690
0
    params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
691
692
0
    return gctx->cb(params, gctx->cbarg);
693
0
}
694
695
static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
696
7.34k
{
697
7.34k
    int ret = 0;
698
7.34k
    struct dh_gen_ctx *gctx = genctx;
699
7.34k
    DH *dh = NULL;
700
7.34k
    BN_GENCB *gencb = NULL;
701
7.34k
    FFC_PARAMS *ffc;
702
703
7.34k
    if (!ossl_prov_is_running() || gctx == NULL)
704
0
        return NULL;
705
706
    /*
707
     * If a group name is selected then the type is group regardless of what
708
     * the user selected. This overrides rather than errors for backwards
709
     * compatibility.
710
     */
711
7.34k
    if (gctx->group_nid != NID_undef)
712
2.86k
        gctx->gen_type = DH_PARAMGEN_TYPE_GROUP;
713
714
    /*
715
     * Do a bounds check on context gen_type. Must be in range:
716
     * DH_PARAMGEN_TYPE_GENERATOR <= gen_type <= DH_PARAMGEN_TYPE_GROUP
717
     * Noted here as this needs to be adjusted if a new group type is
718
     * added.
719
     */
720
7.34k
    if (!ossl_assert((gctx->gen_type >= DH_PARAMGEN_TYPE_GENERATOR)
721
7.34k
            && (gctx->gen_type <= DH_PARAMGEN_TYPE_GROUP))) {
722
0
        ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
723
0
            "gen_type set to unsupported value %d", gctx->gen_type);
724
0
        return NULL;
725
0
    }
726
727
    /* For parameter generation - If there is a group name just create it */
728
7.34k
    if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP
729
2.86k
        && gctx->ffc_params == NULL) {
730
        /* Select a named group if there is not one already */
731
2.86k
        if (gctx->group_nid == NID_undef)
732
0
            gctx->group_nid = ossl_dh_get_named_group_uid_from_size(gctx->pbits);
733
2.86k
        if (gctx->group_nid == NID_undef)
734
0
            return NULL;
735
2.86k
        dh = ossl_dh_new_by_nid_ex(gctx->libctx, gctx->group_nid);
736
2.86k
        if (dh == NULL)
737
0
            return NULL;
738
2.86k
        ffc = ossl_dh_get0_params(dh);
739
4.48k
    } else {
740
4.48k
        dh = ossl_dh_new_ex(gctx->libctx);
741
4.48k
        if (dh == NULL)
742
0
            return NULL;
743
4.48k
        ffc = ossl_dh_get0_params(dh);
744
745
        /* Copy the template value if one was passed */
746
4.48k
        if (gctx->ffc_params != NULL
747
4.48k
            && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
748
0
            goto end;
749
750
4.48k
        if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
751
0
            goto end;
752
4.48k
        if (gctx->gindex != -1) {
753
0
            ossl_ffc_params_set_gindex(ffc, gctx->gindex);
754
0
            if (gctx->pcounter != -1)
755
0
                ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
756
4.48k
        } else if (gctx->hindex != 0) {
757
0
            ossl_ffc_params_set_h(ffc, gctx->hindex);
758
0
        }
759
4.48k
        if (gctx->mdname != NULL)
760
0
            ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops);
761
4.48k
        gctx->cb = osslcb;
762
4.48k
        gctx->cbarg = cbarg;
763
4.48k
        gencb = BN_GENCB_new();
764
4.48k
        if (gencb != NULL)
765
4.48k
            BN_GENCB_set(gencb, dh_gencb, genctx);
766
767
4.48k
        if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
768
            /*
769
             * NOTE: The old safe prime generator code is not used in fips mode,
770
             * (i.e internally it ignores the generator and chooses a named
771
             * group based on pbits.
772
             */
773
0
            if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR)
774
0
                ret = DH_generate_parameters_ex(dh, gctx->pbits,
775
0
                    gctx->generator, gencb);
776
0
            else
777
0
                ret = ossl_dh_generate_ffc_parameters(dh, gctx->gen_type,
778
0
                    gctx->pbits, gctx->qbits,
779
0
                    gencb);
780
0
            if (ret <= 0)
781
0
                goto end;
782
0
        }
783
4.48k
    }
784
785
7.34k
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
786
6.39k
        if (ffc->p == NULL || ffc->g == NULL)
787
0
            goto end;
788
6.39k
        if (gctx->priv_len > 0)
789
0
            DH_set_length(dh, (long)gctx->priv_len);
790
6.39k
        ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
791
6.39k
            gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2);
792
6.39k
        if (DH_generate_key(dh) <= 0)
793
0
            goto end;
794
#ifdef FIPS_MODULE
795
        if (!ossl_fips_self_testing()) {
796
            ret = ossl_dh_check_pairwise(dh, 0);
797
            if (ret <= 0)
798
                goto end;
799
        }
800
#endif /* FIPS_MODULE */
801
6.39k
    }
802
7.34k
    DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
803
7.34k
    DH_set_flags(dh, gctx->dh_type);
804
805
7.34k
    ret = 1;
806
7.34k
end:
807
7.34k
    if (ret <= 0) {
808
0
        DH_free(dh);
809
0
        dh = NULL;
810
0
    }
811
7.34k
    BN_GENCB_free(gencb);
812
7.34k
    return dh;
813
7.34k
}
814
815
static void dh_gen_cleanup(void *genctx)
816
7.34k
{
817
7.34k
    struct dh_gen_ctx *gctx = genctx;
818
819
7.34k
    if (gctx == NULL)
820
0
        return;
821
822
7.34k
    OPENSSL_free(gctx->mdname);
823
7.34k
    OPENSSL_free(gctx->mdprops);
824
7.34k
    OPENSSL_clear_free(gctx->seed, gctx->seedlen);
825
7.34k
    OPENSSL_free(gctx);
826
7.34k
}
827
828
static void *dh_load(const void *reference, size_t reference_sz)
829
53.9k
{
830
53.9k
    DH *dh = NULL;
831
832
53.9k
    if (ossl_prov_is_running() && reference_sz == sizeof(dh)) {
833
        /* The contents of the reference is the address to our object */
834
53.9k
        dh = *(DH **)reference;
835
        /* We grabbed, so we detach it */
836
53.9k
        *(DH **)reference = NULL;
837
53.9k
        return dh;
838
53.9k
    }
839
0
    return NULL;
840
53.9k
}
841
842
static void *dh_dup(const void *keydata_from, int selection)
843
7.51k
{
844
7.51k
    if (ossl_prov_is_running())
845
7.51k
        return ossl_dh_dup(keydata_from, selection);
846
0
    return NULL;
847
7.51k
}
848
849
const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = {
850
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
851
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init },
852
    { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
853
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
854
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
855
        (void (*)(void))dh_gen_settable_params },
856
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
857
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
858
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
859
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
860
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))dh_get_params },
861
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))dh_gettable_params },
862
    { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))dh_set_params },
863
    { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))dh_settable_params },
864
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
865
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
866
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
867
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
868
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
869
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
870
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
871
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
872
    OSSL_FIPS_IND_APPROVED_DISPATCH(OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS,
873
        OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS)
874
        OSSL_DISPATCH_END
875
};
876
877
/* For any DH key, we use the "DH" algorithms regardless of sub-type. */
878
static const char *dhx_query_operation_name(int operation_id)
879
31
{
880
31
    return "DH";
881
31
}
882
883
const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = {
884
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata },
885
    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init },
886
    { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
887
    { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dhx_gen_set_params },
888
    { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
889
        (void (*)(void))dhx_gen_settable_params },
890
    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
891
    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
892
    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
893
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
894
    { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))dh_get_params },
895
    { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))dh_gettable_params },
896
    { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))dh_set_params },
897
    { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))dh_settable_params },
898
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
899
    { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
900
    { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
901
    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
902
    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
903
    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
904
    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
905
    { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
906
        (void (*)(void))dhx_query_operation_name },
907
    { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
908
    OSSL_FIPS_IND_APPROVED_DISPATCH(OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS,
909
        OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS)
910
        OSSL_DISPATCH_END
911
};