Coverage Report

Created: 2025-12-31 06:58

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