Coverage Report

Created: 2025-12-31 06:58

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