Coverage Report

Created: 2025-10-10 07:01

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