Coverage Report

Created: 2025-12-31 06:58

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