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