Coverage Report

Created: 2025-12-10 06:24

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