Coverage Report

Created: 2025-06-13 06:36

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