Coverage Report

Created: 2025-08-25 06:30

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