Coverage Report

Created: 2025-10-28 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/exchange/dh_exch.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * DH low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <openssl/crypto.h>
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/dh.h>
21
#include <openssl/err.h>
22
#include <openssl/proverr.h>
23
#include <openssl/params.h>
24
#include "internal/cryptlib.h"
25
#include "prov/providercommon.h"
26
#include "prov/implementations.h"
27
#include "prov/provider_ctx.h"
28
#include "prov/securitycheck.h"
29
#include "crypto/dh.h"
30
#include "providers/implementations/exchange/dh_exch.inc"
31
32
static OSSL_FUNC_keyexch_newctx_fn dh_newctx;
33
static OSSL_FUNC_keyexch_init_fn dh_init;
34
static OSSL_FUNC_keyexch_set_peer_fn dh_set_peer;
35
static OSSL_FUNC_keyexch_derive_fn dh_derive;
36
static OSSL_FUNC_keyexch_freectx_fn dh_freectx;
37
static OSSL_FUNC_keyexch_dupctx_fn dh_dupctx;
38
static OSSL_FUNC_keyexch_set_ctx_params_fn dh_set_ctx_params;
39
static OSSL_FUNC_keyexch_settable_ctx_params_fn dh_settable_ctx_params;
40
static OSSL_FUNC_keyexch_get_ctx_params_fn dh_get_ctx_params;
41
static OSSL_FUNC_keyexch_gettable_ctx_params_fn dh_gettable_ctx_params;
42
43
/*
44
 * This type is only really used to handle some legacy related functionality.
45
 * If you need to use other KDF's (such as SSKDF) just use PROV_DH_KDF_NONE
46
 * here and then create and run a KDF after the key is derived.
47
 * Note that X942 has 2 variants of key derivation:
48
 *   (1) DH_KDF_X9_42_ASN1 - which contains an ANS1 encoded object that has
49
 *   the counter embedded in it.
50
 *   (2) DH_KDF_X941_CONCAT - which is the same as ECDH_X963_KDF (which can be
51
 *       done by creating a "X963KDF".
52
 */
53
enum kdf_type {
54
    PROV_DH_KDF_NONE = 0,
55
    PROV_DH_KDF_X9_42_ASN1
56
};
57
58
/*
59
 * What's passed as an actual key is defined by the KEYMGMT interface.
60
 * We happen to know that our KEYMGMT simply passes DH structures, so
61
 * we use that here too.
62
 */
63
64
typedef struct {
65
    OSSL_LIB_CTX *libctx;
66
    DH *dh;
67
    DH *dhpeer;
68
    unsigned int pad : 1;
69
70
    /* DH KDF */
71
    /* KDF (if any) to use for DH */
72
    enum kdf_type kdf_type;
73
    /* Message digest to use for key derivation */
74
    EVP_MD *kdf_md;
75
    /* User key material */
76
    unsigned char *kdf_ukm;
77
    size_t kdf_ukmlen;
78
    /* KDF output length */
79
    size_t kdf_outlen;
80
    char *kdf_cekalg;
81
    OSSL_FIPS_IND_DECLARE
82
} PROV_DH_CTX;
83
84
static void *dh_newctx(void *provctx)
85
0
{
86
0
    PROV_DH_CTX *pdhctx;
87
88
0
    if (!ossl_prov_is_running())
89
0
        return NULL;
90
91
0
    pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX));
92
0
    if (pdhctx == NULL)
93
0
        return NULL;
94
0
    OSSL_FIPS_IND_INIT(pdhctx)
95
0
    pdhctx->libctx = PROV_LIBCTX_OF(provctx);
96
0
    pdhctx->kdf_type = PROV_DH_KDF_NONE;
97
0
    return pdhctx;
98
0
}
99
100
#ifdef FIPS_MODULE
101
static int dh_check_key(PROV_DH_CTX *ctx)
102
{
103
    int key_approved = ossl_dh_check_key(ctx->dh);
104
105
    if (!key_approved) {
106
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
107
                                         ctx->libctx, "DH Init", "DH Key",
108
                                         ossl_fips_config_securitycheck_enabled)) {
109
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
110
            return 0;
111
        }
112
    }
113
    return 1;
114
}
115
116
static int digest_check(PROV_DH_CTX *ctx, const EVP_MD *md)
117
{
118
    return ossl_fips_ind_digest_exch_check(OSSL_FIPS_IND_GET(ctx),
119
                                           OSSL_FIPS_IND_SETTABLE1, ctx->libctx,
120
                                           md, "DH Set Ctx");
121
}
122
#endif
123
124
static int dh_init(void *vpdhctx, void *vdh, const OSSL_PARAM params[])
125
0
{
126
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
127
128
0
    if (!ossl_prov_is_running()
129
0
            || pdhctx == NULL
130
0
            || vdh == NULL
131
0
            || !DH_up_ref(vdh))
132
0
        return 0;
133
0
    DH_free(pdhctx->dh);
134
0
    pdhctx->dh = vdh;
135
0
    pdhctx->kdf_type = PROV_DH_KDF_NONE;
136
137
0
    OSSL_FIPS_IND_SET_APPROVED(pdhctx)
138
0
    if (!dh_set_ctx_params(pdhctx, params))
139
0
        return 0;
140
#ifdef FIPS_MODULE
141
    if (!dh_check_key(pdhctx))
142
        return 0;
143
#endif
144
0
    return 1;
145
0
}
146
147
/* The 2 parties must share the same domain parameters */
148
static int dh_match_params(DH *priv, DH *peer)
149
0
{
150
0
    int ret;
151
0
    FFC_PARAMS *dhparams_priv = ossl_dh_get0_params(priv);
152
0
    FFC_PARAMS *dhparams_peer = ossl_dh_get0_params(peer);
153
154
0
    ret = dhparams_priv != NULL
155
0
          && dhparams_peer != NULL
156
0
          && ossl_ffc_params_cmp(dhparams_priv, dhparams_peer, 1);
157
0
    if (!ret)
158
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISMATCHING_DOMAIN_PARAMETERS);
159
0
    return ret;
160
0
}
161
162
static int dh_set_peer(void *vpdhctx, void *vdh)
163
0
{
164
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
165
166
0
    if (!ossl_prov_is_running()
167
0
            || pdhctx == NULL
168
0
            || vdh == NULL
169
0
            || !dh_match_params(vdh, pdhctx->dh)
170
0
            || !DH_up_ref(vdh))
171
0
        return 0;
172
0
    DH_free(pdhctx->dhpeer);
173
0
    pdhctx->dhpeer = vdh;
174
0
    return 1;
175
0
}
176
177
static int dh_plain_derive(void *vpdhctx,
178
                           unsigned char *secret, size_t *secretlen,
179
                           size_t outlen, unsigned int pad)
180
0
{
181
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
182
0
    int ret;
183
0
    size_t dhsize;
184
0
    const BIGNUM *pub_key = NULL;
185
186
0
    if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL) {
187
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
188
0
        return 0;
189
0
    }
190
191
0
    dhsize = (size_t)DH_size(pdhctx->dh);
192
0
    if (secret == NULL) {
193
0
        *secretlen = dhsize;
194
0
        return 1;
195
0
    }
196
0
    if (outlen < dhsize) {
197
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
198
0
        return 0;
199
0
    }
200
201
0
    DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
202
0
    if (pad)
203
0
        ret = DH_compute_key_padded(secret, pub_key, pdhctx->dh);
204
0
    else
205
0
        ret = DH_compute_key(secret, pub_key, pdhctx->dh);
206
0
    if (ret <= 0)
207
0
        return 0;
208
209
0
    *secretlen = ret;
210
0
    return 1;
211
0
}
212
213
static int dh_X9_42_kdf_derive(void *vpdhctx, unsigned char *secret,
214
                               size_t *secretlen, size_t outlen)
215
0
{
216
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
217
0
    unsigned char *stmp = NULL;
218
0
    size_t stmplen;
219
0
    int ret = 0;
220
221
0
    if (secret == NULL) {
222
0
        *secretlen = pdhctx->kdf_outlen;
223
0
        return 1;
224
0
    }
225
226
0
    if (pdhctx->kdf_outlen > outlen) {
227
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
228
0
        return 0;
229
0
    }
230
0
    if (!dh_plain_derive(pdhctx, NULL, &stmplen, 0, 1))
231
0
        return 0;
232
0
    if ((stmp = OPENSSL_secure_malloc(stmplen)) == NULL)
233
0
        return 0;
234
0
    if (!dh_plain_derive(pdhctx, stmp, &stmplen, stmplen, 1))
235
0
        goto err;
236
237
    /* Do KDF stuff */
238
0
    if (pdhctx->kdf_type == PROV_DH_KDF_X9_42_ASN1) {
239
0
        if (!ossl_dh_kdf_X9_42_asn1(secret, pdhctx->kdf_outlen,
240
0
                                    stmp, stmplen,
241
0
                                    pdhctx->kdf_cekalg,
242
0
                                    pdhctx->kdf_ukm,
243
0
                                    pdhctx->kdf_ukmlen,
244
0
                                    pdhctx->kdf_md,
245
0
                                    pdhctx->libctx, NULL))
246
0
            goto err;
247
0
    }
248
0
    *secretlen = pdhctx->kdf_outlen;
249
0
    ret = 1;
250
0
err:
251
0
    OPENSSL_secure_clear_free(stmp, stmplen);
252
0
    return ret;
253
0
}
254
255
static int dh_derive(void *vpdhctx, unsigned char *secret,
256
                     size_t *psecretlen, size_t outlen)
257
0
{
258
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
259
260
0
    if (!ossl_prov_is_running())
261
0
        return 0;
262
263
0
    switch (pdhctx->kdf_type) {
264
0
        case PROV_DH_KDF_NONE:
265
0
            return dh_plain_derive(pdhctx, secret, psecretlen, outlen,
266
0
                                   pdhctx->pad);
267
0
        case PROV_DH_KDF_X9_42_ASN1:
268
0
            return dh_X9_42_kdf_derive(pdhctx, secret, psecretlen, outlen);
269
0
        default:
270
0
            break;
271
0
    }
272
0
    return 0;
273
0
}
274
275
static void dh_freectx(void *vpdhctx)
276
0
{
277
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
278
279
0
    OPENSSL_free(pdhctx->kdf_cekalg);
280
0
    DH_free(pdhctx->dh);
281
0
    DH_free(pdhctx->dhpeer);
282
0
    EVP_MD_free(pdhctx->kdf_md);
283
0
    OPENSSL_clear_free(pdhctx->kdf_ukm, pdhctx->kdf_ukmlen);
284
285
0
    OPENSSL_free(pdhctx);
286
0
}
287
288
static void *dh_dupctx(void *vpdhctx)
289
0
{
290
0
    PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
291
0
    PROV_DH_CTX *dstctx;
292
293
0
    if (!ossl_prov_is_running())
294
0
        return NULL;
295
296
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
297
0
    if (dstctx == NULL)
298
0
        return NULL;
299
300
0
    *dstctx = *srcctx;
301
0
    dstctx->dh = NULL;
302
0
    dstctx->dhpeer = NULL;
303
0
    dstctx->kdf_md = NULL;
304
0
    dstctx->kdf_ukm = NULL;
305
0
    dstctx->kdf_cekalg = NULL;
306
307
0
    if (srcctx->dh != NULL && !DH_up_ref(srcctx->dh))
308
0
        goto err;
309
0
    else
310
0
        dstctx->dh = srcctx->dh;
311
312
0
    if (srcctx->dhpeer != NULL && !DH_up_ref(srcctx->dhpeer))
313
0
        goto err;
314
0
    else
315
0
        dstctx->dhpeer = srcctx->dhpeer;
316
317
0
    if (srcctx->kdf_md != NULL && !EVP_MD_up_ref(srcctx->kdf_md))
318
0
        goto err;
319
0
    else
320
0
        dstctx->kdf_md = srcctx->kdf_md;
321
322
    /* Duplicate UKM data if present */
323
0
    if (srcctx->kdf_ukm != NULL && srcctx->kdf_ukmlen > 0) {
324
0
        dstctx->kdf_ukm = OPENSSL_memdup(srcctx->kdf_ukm,
325
0
                                         srcctx->kdf_ukmlen);
326
0
        if (dstctx->kdf_ukm == NULL)
327
0
            goto err;
328
0
    }
329
330
0
    if (srcctx->kdf_cekalg != NULL) {
331
0
        dstctx->kdf_cekalg = OPENSSL_strdup(srcctx->kdf_cekalg);
332
0
        if (dstctx->kdf_cekalg == NULL)
333
0
            goto err;
334
0
    }
335
336
0
    return dstctx;
337
0
err:
338
0
    dh_freectx(dstctx);
339
0
    return NULL;
340
0
}
341
342
static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[])
343
0
{
344
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
345
0
    struct dh_set_ctx_params_st p;
346
0
    unsigned int pad;
347
0
    char name[80] = { '\0' }; /* should be big enough */
348
0
    char *str = NULL;
349
350
0
    if (pdhctx == NULL || !dh_set_ctx_params_decoder(params, &p))
351
0
        return 0;
352
353
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pdhctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
354
0
        return 0;
355
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pdhctx, OSSL_FIPS_IND_SETTABLE1, p.ind_d))
356
0
        return 0;
357
358
0
    if (p.kdf != NULL) {
359
0
        str = name;
360
0
        if (!OSSL_PARAM_get_utf8_string(p.kdf, &str, sizeof(name)))
361
0
            return 0;
362
363
0
        if (name[0] == '\0')
364
0
            pdhctx->kdf_type = PROV_DH_KDF_NONE;
365
0
        else if (strcmp(name, OSSL_KDF_NAME_X942KDF_ASN1) == 0)
366
0
            pdhctx->kdf_type = PROV_DH_KDF_X9_42_ASN1;
367
0
        else
368
0
            return 0;
369
0
    }
370
371
0
    if (p.digest != NULL) {
372
0
        char mdprops[80] = { '\0' }; /* should be big enough */
373
374
0
        str = name;
375
0
        if (!OSSL_PARAM_get_utf8_string(p.digest, &str, sizeof(name)))
376
0
            return 0;
377
378
0
        str = mdprops;
379
0
        if (p.propq != NULL) {
380
0
            if (!OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(mdprops)))
381
0
                return 0;
382
0
        }
383
384
0
        EVP_MD_free(pdhctx->kdf_md);
385
0
        pdhctx->kdf_md = EVP_MD_fetch(pdhctx->libctx, name, mdprops);
386
0
        if (pdhctx->kdf_md == NULL)
387
0
            return 0;
388
        /* XOF digests are not allowed */
389
0
        if (EVP_MD_xof(pdhctx->kdf_md)) {
390
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
391
0
            return 0;
392
0
        }
393
#ifdef FIPS_MODULE
394
        if (!digest_check(pdhctx, pdhctx->kdf_md)) {
395
            EVP_MD_free(pdhctx->kdf_md);
396
            pdhctx->kdf_md = NULL;
397
            return 0;
398
        }
399
#endif
400
0
    }
401
402
0
    if (p.len != NULL) {
403
0
        size_t outlen;
404
405
0
        if (!OSSL_PARAM_get_size_t(p.len, &outlen))
406
0
            return 0;
407
0
        pdhctx->kdf_outlen = outlen;
408
0
    }
409
410
0
    if (p.ukm != NULL) {
411
0
        void *tmp_ukm = NULL;
412
0
        size_t tmp_ukmlen;
413
414
0
        OPENSSL_free(pdhctx->kdf_ukm);
415
0
        pdhctx->kdf_ukm = NULL;
416
0
        pdhctx->kdf_ukmlen = 0;
417
        /* ukm is an optional field so it can be NULL */
418
0
        if (p.ukm->data != NULL && p.ukm->data_size != 0) {
419
0
            if (!OSSL_PARAM_get_octet_string(p.ukm, &tmp_ukm, 0, &tmp_ukmlen))
420
0
                return 0;
421
0
            pdhctx->kdf_ukm = tmp_ukm;
422
0
            pdhctx->kdf_ukmlen = tmp_ukmlen;
423
0
        }
424
0
    }
425
426
0
    if (p.pad != NULL) {
427
0
        if (!OSSL_PARAM_get_uint(p.pad, &pad))
428
0
            return 0;
429
0
        pdhctx->pad = pad ? 1 : 0;
430
0
    }
431
432
0
    if (p.cekalg != NULL) {
433
0
        str = name;
434
435
0
        OPENSSL_free(pdhctx->kdf_cekalg);
436
0
        pdhctx->kdf_cekalg = NULL;
437
0
        if (p.cekalg->data != NULL && p.cekalg->data_size != 0) {
438
0
            if (!OSSL_PARAM_get_utf8_string(p.cekalg, &str, sizeof(name)))
439
0
                return 0;
440
0
            pdhctx->kdf_cekalg = OPENSSL_strdup(name);
441
0
            if (pdhctx->kdf_cekalg == NULL)
442
0
                return 0;
443
0
        }
444
0
    }
445
0
    return 1;
446
0
}
447
448
static const OSSL_PARAM *dh_settable_ctx_params(ossl_unused void *vpdhctx,
449
                                                ossl_unused void *provctx)
450
0
{
451
0
    return dh_set_ctx_params_list;
452
0
}
453
454
static const OSSL_PARAM *dh_gettable_ctx_params(ossl_unused void *vpdhctx,
455
                                                ossl_unused void *provctx)
456
0
{
457
0
    return dh_get_ctx_params_list;
458
0
}
459
460
static int dh_get_ctx_params(void *vpdhctx, OSSL_PARAM params[])
461
0
{
462
0
    PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
463
0
    struct dh_get_ctx_params_st p;
464
465
0
    if (pdhctx == NULL || !dh_get_ctx_params_decoder(params, &p))
466
0
        return 0;
467
468
0
    if (p.kdf != NULL) {
469
0
        const char *kdf_type = NULL;
470
471
0
        switch (pdhctx->kdf_type) {
472
0
        case PROV_DH_KDF_NONE:
473
0
            kdf_type = "";
474
0
            break;
475
0
        case PROV_DH_KDF_X9_42_ASN1:
476
0
            kdf_type = OSSL_KDF_NAME_X942KDF_ASN1;
477
0
            break;
478
0
        default:
479
0
            return 0;
480
0
        }
481
482
0
        if (!OSSL_PARAM_set_utf8_string(p.kdf, kdf_type))
483
0
            return 0;
484
0
    }
485
486
0
    if (p.digest != NULL
487
0
            && !OSSL_PARAM_set_utf8_string(p.digest, pdhctx->kdf_md == NULL
488
0
                                           ? ""
489
0
                                           : EVP_MD_get0_name(pdhctx->kdf_md))) {
490
0
        return 0;
491
0
    }
492
493
0
    if (p.len != NULL && !OSSL_PARAM_set_size_t(p.len, pdhctx->kdf_outlen))
494
0
        return 0;
495
496
0
    if (p.ukm != NULL
497
0
        && !OSSL_PARAM_set_octet_ptr(p.ukm, pdhctx->kdf_ukm, pdhctx->kdf_ukmlen))
498
0
        return 0;
499
500
0
    if (p.cekalg != NULL
501
0
            && !OSSL_PARAM_set_utf8_string(p.cekalg, pdhctx->kdf_cekalg == NULL
502
0
                                           ? "" :  pdhctx->kdf_cekalg))
503
0
        return 0;
504
505
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(pdhctx, p.ind))
506
0
        return 0;
507
0
    return 1;
508
0
}
509
510
const OSSL_DISPATCH ossl_dh_keyexch_functions[] = {
511
    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
512
    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
513
    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
514
    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
515
    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
516
    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
517
    { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params },
518
    { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
519
      (void (*)(void))dh_settable_ctx_params },
520
    { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))dh_get_ctx_params },
521
    { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
522
      (void (*)(void))dh_gettable_ctx_params },
523
    OSSL_DISPATCH_END
524
};