]>
Commit | Line | Data |
---|---|---|
8b84b075 RL |
1 | /* |
2 | * Copyright 2019 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 | ||
ada66e78 P |
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 | ||
8b84b075 RL |
16 | #include <openssl/core_numbers.h> |
17 | #include <openssl/core_names.h> | |
18 | #include <openssl/bn.h> | |
8b84b075 | 19 | #include <openssl/params.h> |
1640d48c | 20 | #include "internal/param_build.h" |
8dd5c603 | 21 | #include "crypto/dh.h" |
af3e7e1b | 22 | #include "prov/implementations.h" |
1640d48c | 23 | #include "prov/providercommon.h" |
8083fd3a SL |
24 | #include "prov/provider_ctx.h" |
25 | #include "crypto/dh.h" | |
8b84b075 | 26 | |
8dd5c603 RL |
27 | static OSSL_OP_keymgmt_new_fn dh_newdata; |
28 | static OSSL_OP_keymgmt_free_fn dh_freedata; | |
273a67e3 RL |
29 | static OSSL_OP_keymgmt_get_params_fn dh_get_params; |
30 | static OSSL_OP_keymgmt_gettable_params_fn dh_gettable_params; | |
8dd5c603 RL |
31 | static OSSL_OP_keymgmt_has_fn dh_has; |
32 | static OSSL_OP_keymgmt_import_fn dh_import; | |
273a67e3 | 33 | static OSSL_OP_keymgmt_import_types_fn dh_import_types; |
8dd5c603 | 34 | static OSSL_OP_keymgmt_export_fn dh_export; |
273a67e3 | 35 | static OSSL_OP_keymgmt_export_types_fn dh_export_types; |
8dd5c603 RL |
36 | |
37 | #define DH_POSSIBLE_SELECTIONS \ | |
273a67e3 | 38 | (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) |
8b84b075 | 39 | |
14e3e00f | 40 | static int params_to_domparams(DH *dh, const OSSL_PARAM params[]) |
8b84b075 | 41 | { |
14e3e00f RL |
42 | const OSSL_PARAM *param_p, *param_g; |
43 | BIGNUM *p = NULL, *g = NULL; | |
8b84b075 RL |
44 | |
45 | if (dh == NULL) | |
46 | return 0; | |
47 | ||
4889dadc MC |
48 | param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P); |
49 | param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G); | |
14e3e00f RL |
50 | |
51 | if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p)) | |
52 | || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g))) | |
53 | goto err; | |
54 | ||
55 | if (!DH_set0_pqg(dh, p, NULL, g)) | |
56 | goto err; | |
57 | ||
58 | return 1; | |
59 | ||
60 | err: | |
61 | BN_free(p); | |
62 | BN_free(g); | |
63 | return 0; | |
64 | } | |
65 | ||
1640d48c | 66 | static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl) |
c8f23016 | 67 | { |
c8f23016 RL |
68 | const BIGNUM *dh_p = NULL, *dh_g = NULL; |
69 | ||
70 | if (dh == NULL) | |
71 | return 0; | |
72 | ||
73 | DH_get0_pqg(dh, &dh_p, NULL, &dh_g); | |
1640d48c RL |
74 | if (dh_p != NULL |
75 | && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p)) | |
c8f23016 | 76 | return 0; |
1640d48c RL |
77 | if (dh_g != NULL |
78 | && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g)) | |
c8f23016 RL |
79 | return 0; |
80 | ||
81 | return 1; | |
82 | } | |
83 | ||
14e3e00f RL |
84 | static int params_to_key(DH *dh, const OSSL_PARAM params[]) |
85 | { | |
86 | const OSSL_PARAM *param_priv_key, *param_pub_key; | |
87 | BIGNUM *priv_key = NULL, *pub_key = NULL; | |
88 | ||
89 | if (dh == NULL) | |
90 | return 0; | |
91 | ||
92 | if (!params_to_domparams(dh, params)) | |
93 | return 0; | |
94 | ||
8b84b075 | 95 | param_priv_key = |
90d3cb57 | 96 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); |
8b84b075 | 97 | param_pub_key = |
90d3cb57 | 98 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY); |
8b84b075 RL |
99 | |
100 | /* | |
101 | * DH documentation says that a public key must be present if a | |
102 | * private key is present. | |
103 | * We want to have at least a public key either way, so we end up | |
104 | * requiring it unconditionally. | |
105 | */ | |
106 | if (param_pub_key == NULL) | |
107 | return 0; | |
108 | ||
14e3e00f RL |
109 | if ((param_priv_key != NULL |
110 | && !OSSL_PARAM_get_BN(param_priv_key, &priv_key)) | |
8b84b075 RL |
111 | || !OSSL_PARAM_get_BN(param_pub_key, &pub_key)) |
112 | goto err; | |
113 | ||
8b84b075 RL |
114 | if (!DH_set0_key(dh, pub_key, priv_key)) |
115 | goto err; | |
8b84b075 RL |
116 | |
117 | return 1; | |
118 | ||
119 | err: | |
afa2b389 | 120 | BN_clear_free(priv_key); |
8b84b075 RL |
121 | BN_free(pub_key); |
122 | return 0; | |
123 | } | |
124 | ||
1640d48c | 125 | static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl) |
c8f23016 | 126 | { |
c8f23016 RL |
127 | const BIGNUM *priv_key = NULL, *pub_key = NULL; |
128 | ||
129 | if (dh == NULL) | |
130 | return 0; | |
1640d48c | 131 | if (!domparams_to_params(dh, tmpl)) |
c8f23016 RL |
132 | return 0; |
133 | ||
134 | DH_get0_key(dh, &pub_key, &priv_key); | |
1640d48c | 135 | if (priv_key != NULL |
90d3cb57 | 136 | && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key)) |
c8f23016 | 137 | return 0; |
1640d48c | 138 | if (pub_key != NULL |
90d3cb57 | 139 | && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key)) |
c8f23016 RL |
140 | return 0; |
141 | ||
142 | return 1; | |
143 | } | |
144 | ||
8dd5c603 | 145 | static void *dh_newdata(void *provctx) |
14e3e00f | 146 | { |
8083fd3a | 147 | return dh_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx)); |
14e3e00f RL |
148 | } |
149 | ||
8dd5c603 | 150 | static void dh_freedata(void *keydata) |
c8f23016 | 151 | { |
8dd5c603 RL |
152 | DH_free(keydata); |
153 | } | |
1640d48c | 154 | |
8dd5c603 RL |
155 | static int dh_has(void *keydata, int selection) |
156 | { | |
157 | DH *dh = keydata; | |
158 | int ok = 0; | |
159 | ||
160 | if ((selection & DH_POSSIBLE_SELECTIONS) != 0) | |
161 | ok = 1; | |
162 | ||
163 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
164 | ok = ok && (DH_get0_pub_key(dh) != NULL); | |
165 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
166 | ok = ok && (DH_get0_priv_key(dh) != NULL); | |
167 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) | |
168 | ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL); | |
169 | return ok; | |
c8f23016 RL |
170 | } |
171 | ||
8dd5c603 | 172 | static int dh_import(void *keydata, int selection, const OSSL_PARAM params[]) |
8b84b075 | 173 | { |
8dd5c603 RL |
174 | DH *dh = keydata; |
175 | int ok = 0; | |
176 | ||
177 | if (dh == NULL) | |
178 | return 0; | |
179 | ||
180 | if ((selection & DH_POSSIBLE_SELECTIONS) != 0) | |
181 | ok = 1; | |
182 | ||
183 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) | |
184 | ok = ok && params_to_domparams(dh, params); | |
185 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
186 | ok = ok && params_to_key(dh, params); | |
187 | ||
188 | return ok; | |
8b84b075 RL |
189 | } |
190 | ||
8dd5c603 RL |
191 | static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
192 | void *cbarg) | |
c8f23016 | 193 | { |
8dd5c603 | 194 | DH *dh = keydata; |
1640d48c RL |
195 | OSSL_PARAM_BLD tmpl; |
196 | OSSL_PARAM *params = NULL; | |
8dd5c603 RL |
197 | int ok = 1; |
198 | ||
199 | if (dh == NULL) | |
200 | return 0; | |
1640d48c RL |
201 | |
202 | ossl_param_bld_init(&tmpl); | |
8dd5c603 RL |
203 | |
204 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) | |
205 | ok = ok && domparams_to_params(dh, &tmpl); | |
206 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
207 | ok = ok && key_to_params(dh, &tmpl); | |
208 | ||
209 | if (!ok | |
1640d48c RL |
210 | || (params = ossl_param_bld_to_param(&tmpl)) == NULL) |
211 | return 0; | |
8dd5c603 RL |
212 | |
213 | ok = param_cb(params, cbarg); | |
1640d48c | 214 | ossl_param_bld_free(params); |
8dd5c603 | 215 | return ok; |
c8f23016 RL |
216 | } |
217 | ||
273a67e3 RL |
218 | /* IMEXPORT = IMPORT + EXPORT */ |
219 | ||
220 | # define DH_IMEXPORTABLE_PARAMETERS \ | |
221 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \ | |
222 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0) | |
223 | # define DH_IMEXPORTABLE_PUBLIC_KEY \ | |
90d3cb57 | 224 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0) |
273a67e3 | 225 | # define DH_IMEXPORTABLE_PRIVATE_KEY \ |
90d3cb57 | 226 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0) |
273a67e3 RL |
227 | static const OSSL_PARAM dh_all_types[] = { |
228 | DH_IMEXPORTABLE_PARAMETERS, | |
229 | DH_IMEXPORTABLE_PUBLIC_KEY, | |
230 | DH_IMEXPORTABLE_PRIVATE_KEY, | |
231 | OSSL_PARAM_END | |
232 | }; | |
233 | static const OSSL_PARAM dh_parameter_types[] = { | |
234 | DH_IMEXPORTABLE_PARAMETERS, | |
235 | OSSL_PARAM_END | |
236 | }; | |
237 | static const OSSL_PARAM dh_key_types[] = { | |
238 | DH_IMEXPORTABLE_PUBLIC_KEY, | |
239 | DH_IMEXPORTABLE_PRIVATE_KEY, | |
240 | OSSL_PARAM_END | |
241 | }; | |
242 | static const OSSL_PARAM *dh_types[] = { | |
243 | NULL, /* Index 0 = none of them */ | |
244 | dh_parameter_types, /* Index 1 = parameter types */ | |
245 | dh_key_types, /* Index 2 = key types */ | |
246 | dh_all_types /* Index 3 = 1 + 2 */ | |
247 | }; | |
248 | ||
249 | static const OSSL_PARAM *dh_imexport_types(int selection) | |
250 | { | |
251 | int type_select = 0; | |
252 | ||
253 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) | |
254 | type_select += 1; | |
255 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
256 | type_select += 2; | |
257 | return dh_types[type_select]; | |
258 | } | |
259 | ||
260 | static const OSSL_PARAM *dh_import_types(int selection) | |
261 | { | |
262 | return dh_imexport_types(selection); | |
263 | } | |
264 | ||
265 | static const OSSL_PARAM *dh_export_types(int selection) | |
266 | { | |
267 | return dh_imexport_types(selection); | |
268 | } | |
269 | ||
8dd5c603 | 270 | static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[]) |
9e5aaf78 RL |
271 | { |
272 | DH *dh = key; | |
273 | OSSL_PARAM *p; | |
274 | ||
275 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL | |
276 | && !OSSL_PARAM_set_int(p, DH_bits(dh))) | |
277 | return 0; | |
278 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL | |
279 | && !OSSL_PARAM_set_int(p, DH_security_bits(dh))) | |
280 | return 0; | |
281 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL | |
282 | && !OSSL_PARAM_set_int(p, DH_size(dh))) | |
283 | return 0; | |
284 | return 1; | |
285 | } | |
286 | ||
273a67e3 RL |
287 | static const OSSL_PARAM dh_params[] = { |
288 | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), | |
289 | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), | |
290 | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), | |
291 | OSSL_PARAM_END | |
292 | }; | |
293 | ||
294 | static const OSSL_PARAM *dh_gettable_params(void) | |
295 | { | |
296 | return dh_params; | |
297 | } | |
298 | ||
8b84b075 | 299 | const OSSL_DISPATCH dh_keymgmt_functions[] = { |
8dd5c603 RL |
300 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata }, |
301 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata }, | |
273a67e3 RL |
302 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params }, |
303 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params }, | |
8dd5c603 RL |
304 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has }, |
305 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import }, | |
273a67e3 | 306 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types }, |
8dd5c603 | 307 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export }, |
273a67e3 | 308 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types }, |
8b84b075 RL |
309 | { 0, NULL } |
310 | }; |