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