Coverage Report

Created: 2026-02-22 06:11

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