Coverage Report

Created: 2026-10-16 07:07

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