Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/dh/dh_backend.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2026 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 <openssl/err.h>
17
#include <openssl/core_names.h>
18
#ifndef FIPS_MODULE
19
#include <openssl/x509.h>
20
#endif
21
#include "internal/param_build_set.h"
22
#include "crypto/dh.h"
23
#include "dh_local.h"
24
25
#include <crypto/asn1.h>
26
27
/*
28
 * The intention with the "backend" source file is to offer backend functions
29
 * for legacy backends (EVP_PKEY_ASN1_METHOD) and provider implementations
30
 * alike.
31
 */
32
33
static int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
34
10.2k
{
35
10.2k
    int ret;
36
10.2k
    FFC_PARAMS *ffc = ossl_dh_get0_params(dh);
37
38
10.2k
    ret = ossl_ffc_params_fromdata(ffc, params);
39
10.2k
    if (ret)
40
10.2k
        ossl_dh_cache_named_group(dh); /* This increments dh->dirty_cnt */
41
10.2k
    return ret;
42
10.2k
}
43
44
int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[])
45
10.2k
{
46
10.2k
    const OSSL_PARAM *param_priv_len;
47
10.2k
    long priv_len;
48
49
10.2k
    if (!dh_ffc_params_fromdata(dh, params))
50
0
        return 0;
51
52
10.2k
    param_priv_len = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
53
10.2k
    if (param_priv_len != NULL
54
0
        && (!OSSL_PARAM_get_long(param_priv_len, &priv_len)
55
0
            || !DH_set_length(dh, priv_len)))
56
0
        return 0;
57
58
10.2k
    return 1;
59
10.2k
}
60
61
int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[], int include_private)
62
10.2k
{
63
10.2k
    const OSSL_PARAM *param_priv_key, *param_pub_key;
64
10.2k
    BIGNUM *priv_key = NULL, *pub_key = NULL;
65
66
10.2k
    if (dh == NULL)
67
0
        return 0;
68
69
10.2k
    param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
70
10.2k
    param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
71
72
10.2k
    if (include_private
73
10.2k
        && param_priv_key != NULL
74
0
        && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
75
0
        goto err;
76
77
10.2k
    if (param_pub_key != NULL
78
10.2k
        && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
79
0
        goto err;
80
81
10.2k
    if (!DH_set0_key(dh, pub_key, priv_key))
82
0
        goto err;
83
84
10.2k
    return 1;
85
86
0
err:
87
0
    BN_clear_free(priv_key);
88
0
    BN_free(pub_key);
89
0
    return 0;
90
10.2k
}
91
92
int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM *privlen,
93
    const FFC_OSSL_PARAMS *pp)
94
93.7k
{
95
93.7k
    const long l = DH_get_length(dh);
96
97
93.7k
    if (!ossl_ffc_params_todata(ossl_dh_get0_params(dh), bld, pp))
98
0
        return 0;
99
93.7k
    if (l > 0
100
623
        && !ossl_param_build_set_long(bld, privlen, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
101
0
        return 0;
102
93.7k
    return 1;
103
93.7k
}
104
105
int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM *pubkey,
106
    OSSL_PARAM *privkey, int include_private)
107
93.7k
{
108
93.7k
    const BIGNUM *priv = NULL, *pub = NULL;
109
110
93.7k
    if (dh == NULL)
111
0
        return 0;
112
113
93.7k
    DH_get0_key(dh, &pub, &priv);
114
93.7k
    if (priv != NULL
115
21.2k
        && include_private
116
21.2k
        && !ossl_param_build_set_bn(bld, privkey, OSSL_PKEY_PARAM_PRIV_KEY, priv))
117
0
        return 0;
118
93.7k
    if (pub != NULL
119
79.4k
        && !ossl_param_build_set_bn(bld, pubkey, OSSL_PKEY_PARAM_PUB_KEY, pub))
120
0
        return 0;
121
122
93.7k
    return 1;
123
93.7k
}
124
125
int ossl_dh_is_foreign(const DH *dh)
126
2.33k
{
127
2.33k
#ifndef FIPS_MODULE
128
2.33k
    if (ossl_dh_get_method(dh) != DH_OpenSSL())
129
0
        return 1;
130
2.33k
#endif
131
2.33k
    return 0;
132
2.33k
}
133
134
static ossl_inline int dh_bn_dup_check(BIGNUM **out, const BIGNUM *f)
135
13.2k
{
136
13.2k
    if (f != NULL && (*out = BN_dup(f)) == NULL)
137
0
        return 0;
138
13.2k
    return 1;
139
13.2k
}
140
141
DH *ossl_dh_dup(const DH *dh, int selection)
142
7.51k
{
143
7.51k
    DH *dupkey = NULL;
144
145
    /* Do not try to duplicate foreign DH keys */
146
7.51k
    if (ossl_dh_is_foreign(dh))
147
0
        return NULL;
148
149
7.51k
    if ((dupkey = ossl_dh_new_ex(dh->libctx)) == NULL)
150
0
        return NULL;
151
152
7.51k
    dupkey->length = DH_get_length(dh);
153
7.51k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0
154
7.51k
        && !ossl_ffc_params_copy(&dupkey->params, &dh->params))
155
0
        goto err;
156
157
7.51k
    dupkey->flags = dh->flags;
158
159
7.51k
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
160
6.60k
        && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
161
6.60k
            || !dh_bn_dup_check(&dupkey->pub_key, dh->pub_key)))
162
0
        goto err;
163
164
7.51k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
165
6.60k
        && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
166
6.60k
            || !dh_bn_dup_check(&dupkey->priv_key, dh->priv_key)))
167
0
        goto err;
168
169
7.51k
#ifndef FIPS_MODULE
170
7.51k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH,
171
7.51k
            &dupkey->ex_data, &dh->ex_data))
172
0
        goto err;
173
7.51k
#endif
174
175
7.51k
    return dupkey;
176
177
0
err:
178
0
    DH_free(dupkey);
179
0
    return NULL;
180
7.51k
}
181
182
#ifndef FIPS_MODULE
183
DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
184
    OSSL_LIB_CTX *libctx, const char *propq)
185
324
{
186
324
    const unsigned char *p, *pm;
187
324
    int pklen, pmlen;
188
324
    int ptype;
189
324
    const void *pval;
190
324
    const ASN1_STRING *pstr;
191
324
    const X509_ALGOR *palg;
192
324
    BIGNUM *privkey_bn = NULL;
193
324
    ASN1_INTEGER *privkey = NULL;
194
324
    DH *dh = NULL;
195
196
324
    if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
197
0
        return 0;
198
199
324
    X509_ALGOR_get0(NULL, &ptype, &pval, palg);
200
201
324
    if (ptype != V_ASN1_SEQUENCE)
202
58
        goto decerr;
203
266
    if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
204
27
        goto decerr;
205
206
239
    pstr = pval;
207
239
    pm = pstr->data;
208
239
    pmlen = pstr->length;
209
239
    switch (OBJ_obj2nid(palg->algorithm)) {
210
16
    case NID_dhKeyAgreement:
211
16
        dh = d2i_DHparams(NULL, &pm, pmlen);
212
16
        break;
213
223
    case NID_dhpublicnumber:
214
223
        dh = d2i_DHxparams(NULL, &pm, pmlen);
215
223
        break;
216
0
    default:
217
0
        goto decerr;
218
239
    }
219
239
    if (dh == NULL)
220
43
        goto decerr;
221
222
    /* We have parameters now set private key */
223
196
    if ((privkey_bn = BN_secure_new()) == NULL
224
196
        || !ASN1_INTEGER_to_BN(privkey, privkey_bn)) {
225
0
        ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
226
0
        BN_clear_free(privkey_bn);
227
0
        goto dherr;
228
0
    }
229
196
    if (!DH_set0_key(dh, NULL, privkey_bn))
230
0
        goto dherr;
231
    /* Calculate public key, increments dirty_cnt */
232
196
    if (!DH_generate_key(dh))
233
70
        goto dherr;
234
235
126
    goto done;
236
237
128
decerr:
238
128
    ERR_raise(ERR_LIB_DH, DH_R_DECODE_ERROR);
239
198
dherr:
240
198
    DH_free(dh);
241
198
    dh = NULL;
242
324
done:
243
324
    ASN1_STRING_clear_free(privkey);
244
324
    return dh;
245
198
}
246
#endif