Coverage Report

Created: 2025-10-28 06:56

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