Coverage Report

Created: 2023-06-08 06:41

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