]>
Commit | Line | Data |
---|---|---|
8b84b075 | 1 | /* |
7165593c | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
8b84b075 RL |
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 | ||
7165593c | 16 | #include <string.h> /* strcmp */ |
23c48d94 | 17 | #include <openssl/core_dispatch.h> |
8b84b075 RL |
18 | #include <openssl/core_names.h> |
19 | #include <openssl/bn.h> | |
7165593c | 20 | #include <openssl/err.h> |
af3e7e1b | 21 | #include "prov/implementations.h" |
1640d48c | 22 | #include "prov/providercommon.h" |
8083fd3a SL |
23 | #include "prov/provider_ctx.h" |
24 | #include "crypto/dh.h" | |
7165593c SL |
25 | #include "internal/sizes.h" |
26 | #include "internal/nelem.h" | |
27 | #include "internal/param_build_set.h" | |
8b84b075 | 28 | |
363b1e5d DMSP |
29 | static OSSL_FUNC_keymgmt_new_fn dh_newdata; |
30 | static OSSL_FUNC_keymgmt_free_fn dh_freedata; | |
31 | static OSSL_FUNC_keymgmt_gen_init_fn dh_gen_init; | |
32 | static OSSL_FUNC_keymgmt_gen_set_template_fn dh_gen_set_template; | |
33 | static OSSL_FUNC_keymgmt_gen_set_params_fn dh_gen_set_params; | |
34 | static OSSL_FUNC_keymgmt_gen_settable_params_fn dh_gen_settable_params; | |
35 | static OSSL_FUNC_keymgmt_gen_fn dh_gen; | |
36 | static OSSL_FUNC_keymgmt_gen_cleanup_fn dh_gen_cleanup; | |
7c664b1f | 37 | static OSSL_FUNC_keymgmt_load_fn dh_load; |
363b1e5d DMSP |
38 | static OSSL_FUNC_keymgmt_get_params_fn dh_get_params; |
39 | static OSSL_FUNC_keymgmt_gettable_params_fn dh_gettable_params; | |
40 | static OSSL_FUNC_keymgmt_set_params_fn dh_set_params; | |
41 | static OSSL_FUNC_keymgmt_settable_params_fn dh_settable_params; | |
42 | static OSSL_FUNC_keymgmt_has_fn dh_has; | |
43 | static OSSL_FUNC_keymgmt_match_fn dh_match; | |
44 | static OSSL_FUNC_keymgmt_validate_fn dh_validate; | |
45 | static OSSL_FUNC_keymgmt_import_fn dh_import; | |
46 | static OSSL_FUNC_keymgmt_import_types_fn dh_import_types; | |
47 | static OSSL_FUNC_keymgmt_export_fn dh_export; | |
48 | static OSSL_FUNC_keymgmt_export_types_fn dh_export_types; | |
8dd5c603 | 49 | |
7165593c | 50 | #define DH_POSSIBLE_SELECTIONS \ |
273a67e3 | 51 | (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) |
8b84b075 | 52 | |
7165593c SL |
53 | struct dh_gen_ctx { |
54 | OPENSSL_CTX *libctx; | |
55 | ||
56 | FFC_PARAMS *ffc_params; | |
57 | int selection; | |
58 | /* All these parameters are used for parameter generation only */ | |
59 | /* If there is a group name then the remaining parameters are not needed */ | |
60 | int group_nid; | |
61 | size_t pbits; | |
62 | size_t qbits; | |
7165593c SL |
63 | unsigned char *seed; /* optional FIPS186-4 param for testing */ |
64 | size_t seedlen; | |
65 | int gindex; /* optional FIPS186-4 generator index (ignored if -1) */ | |
66 | int gen_type; /* see dhtype2id */ | |
67 | int generator; /* Used by DH_PARAMGEN_TYPE_GENERATOR in non fips mode only */ | |
68 | int pcounter; | |
69 | int hindex; | |
738ee181 | 70 | int priv_len; |
7165593c | 71 | |
4f2271d5 SL |
72 | const char *mdname; |
73 | const char *mdprops; | |
7165593c SL |
74 | OSSL_CALLBACK *cb; |
75 | void *cbarg; | |
76 | }; | |
77 | ||
78 | typedef struct dh_name2id_st{ | |
79 | const char *name; | |
80 | int id; | |
81 | } DH_GENTYPE_NAME2ID; | |
82 | ||
83 | static const DH_GENTYPE_NAME2ID dhtype2id[]= | |
c8f23016 | 84 | { |
7165593c SL |
85 | { "default", DH_PARAMGEN_TYPE_FIPS_186_4 }, |
86 | { "fips186_4", DH_PARAMGEN_TYPE_FIPS_186_4 }, | |
87 | { "fips186_2", DH_PARAMGEN_TYPE_FIPS_186_2 }, | |
88 | { "group", DH_PARAMGEN_TYPE_GROUP }, | |
89 | { "generator", DH_PARAMGEN_TYPE_GENERATOR } | |
90 | }; | |
c8f23016 | 91 | |
7165593c SL |
92 | const char *dh_gen_type_id2name(int id) |
93 | { | |
94 | size_t i; | |
c8f23016 | 95 | |
7165593c SL |
96 | for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) { |
97 | if (dhtype2id[i].id == id) | |
98 | return dhtype2id[i].name; | |
99 | } | |
100 | return NULL; | |
101 | } | |
c8f23016 | 102 | |
7165593c SL |
103 | static int dh_gen_type_name2id(const char *name) |
104 | { | |
105 | size_t i; | |
106 | ||
107 | for (i = 0; i < OSSL_NELEM(dhtype2id); ++i) { | |
108 | if (strcmp(dhtype2id[i].name, name) == 0) | |
109 | return dhtype2id[i].id; | |
110 | } | |
111 | return -1; | |
c8f23016 RL |
112 | } |
113 | ||
7165593c | 114 | static int dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[]) |
c8f23016 | 115 | { |
7165593c | 116 | const BIGNUM *priv = NULL, *pub = NULL; |
c8f23016 RL |
117 | |
118 | if (dh == NULL) | |
119 | return 0; | |
c8f23016 | 120 | |
7165593c SL |
121 | DH_get0_key(dh, &pub, &priv); |
122 | if (priv != NULL | |
123 | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv)) | |
c8f23016 | 124 | return 0; |
7165593c SL |
125 | if (pub != NULL |
126 | && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub)) | |
c8f23016 RL |
127 | return 0; |
128 | ||
129 | return 1; | |
130 | } | |
131 | ||
8dd5c603 | 132 | static void *dh_newdata(void *provctx) |
14e3e00f | 133 | { |
7165593c | 134 | return dh_new_with_libctx(PROV_LIBRARY_CONTEXT_OF(provctx)); |
14e3e00f RL |
135 | } |
136 | ||
8dd5c603 | 137 | static void dh_freedata(void *keydata) |
c8f23016 | 138 | { |
8dd5c603 RL |
139 | DH_free(keydata); |
140 | } | |
1640d48c | 141 | |
8dd5c603 RL |
142 | static int dh_has(void *keydata, int selection) |
143 | { | |
144 | DH *dh = keydata; | |
145 | int ok = 0; | |
146 | ||
adc9f731 RL |
147 | if (dh != NULL) { |
148 | if ((selection & DH_POSSIBLE_SELECTIONS) != 0) | |
149 | ok = 1; | |
150 | ||
151 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
152 | ok = ok && (DH_get0_pub_key(dh) != NULL); | |
153 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
154 | ok = ok && (DH_get0_priv_key(dh) != NULL); | |
155 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) | |
156 | ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL); | |
157 | } | |
8dd5c603 | 158 | return ok; |
c8f23016 RL |
159 | } |
160 | ||
2888fc15 RL |
161 | static int dh_match(const void *keydata1, const void *keydata2, int selection) |
162 | { | |
163 | const DH *dh1 = keydata1; | |
164 | const DH *dh2 = keydata2; | |
165 | int ok = 1; | |
166 | ||
167 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
168 | ok = ok && BN_cmp(DH_get0_pub_key(dh1), DH_get0_pub_key(dh2)) == 0; | |
169 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
170 | ok = ok && BN_cmp(DH_get0_priv_key(dh1), DH_get0_priv_key(dh2)) == 0; | |
171 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { | |
172 | FFC_PARAMS *dhparams1 = dh_get0_params((DH *)dh1); | |
173 | FFC_PARAMS *dhparams2 = dh_get0_params((DH *)dh2); | |
174 | ||
175 | ok = ok && ffc_params_cmp(dhparams1, dhparams2, 1); | |
176 | } | |
177 | return ok; | |
178 | } | |
179 | ||
8dd5c603 | 180 | static int dh_import(void *keydata, int selection, const OSSL_PARAM params[]) |
8b84b075 | 181 | { |
8dd5c603 | 182 | DH *dh = keydata; |
7165593c | 183 | int ok = 1; |
8dd5c603 RL |
184 | |
185 | if (dh == NULL) | |
186 | return 0; | |
187 | ||
7165593c SL |
188 | if ((selection & DH_POSSIBLE_SELECTIONS) == 0) |
189 | return 0; | |
8dd5c603 RL |
190 | |
191 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) | |
7165593c SL |
192 | ok = ok && dh_ffc_params_fromdata(dh, params); |
193 | ||
8dd5c603 | 194 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
0abae163 | 195 | ok = ok && dh_key_fromdata(dh, params); |
8dd5c603 RL |
196 | |
197 | return ok; | |
8b84b075 RL |
198 | } |
199 | ||
8dd5c603 RL |
200 | static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
201 | void *cbarg) | |
c8f23016 | 202 | { |
8dd5c603 | 203 | DH *dh = keydata; |
b03ec3b5 | 204 | OSSL_PARAM_BLD *tmpl = NULL; |
1640d48c | 205 | OSSL_PARAM *params = NULL; |
8dd5c603 RL |
206 | int ok = 1; |
207 | ||
208 | if (dh == NULL) | |
209 | return 0; | |
1640d48c | 210 | |
6d4e6009 P |
211 | tmpl = OSSL_PARAM_BLD_new(); |
212 | if (tmpl == NULL) | |
213 | return 0; | |
8dd5c603 RL |
214 | |
215 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) | |
7165593c | 216 | ok = ok && ffc_params_todata(dh_get0_params(dh), tmpl, NULL); |
8dd5c603 | 217 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
7165593c | 218 | ok = ok && dh_key_todata(dh, tmpl, NULL); |
8dd5c603 RL |
219 | |
220 | if (!ok | |
6d4e6009 | 221 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
b03ec3b5 SL |
222 | ok = 0; |
223 | goto err; | |
6d4e6009 | 224 | } |
8dd5c603 | 225 | ok = param_cb(params, cbarg); |
6d4e6009 | 226 | OSSL_PARAM_BLD_free_params(params); |
b03ec3b5 SL |
227 | err: |
228 | OSSL_PARAM_BLD_free(tmpl); | |
8dd5c603 | 229 | return ok; |
c8f23016 RL |
230 | } |
231 | ||
273a67e3 RL |
232 | /* IMEXPORT = IMPORT + EXPORT */ |
233 | ||
7165593c SL |
234 | # define DH_IMEXPORTABLE_PARAMETERS \ |
235 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \ | |
236 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \ | |
237 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \ | |
238 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \ | |
239 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \ | |
240 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \ | |
241 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \ | |
b8086652 | 242 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), \ |
023b188c | 243 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0) |
7165593c | 244 | # define DH_IMEXPORTABLE_PUBLIC_KEY \ |
90d3cb57 | 245 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0) |
7165593c | 246 | # define DH_IMEXPORTABLE_PRIVATE_KEY \ |
90d3cb57 | 247 | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0) |
273a67e3 RL |
248 | static const OSSL_PARAM dh_all_types[] = { |
249 | DH_IMEXPORTABLE_PARAMETERS, | |
250 | DH_IMEXPORTABLE_PUBLIC_KEY, | |
251 | DH_IMEXPORTABLE_PRIVATE_KEY, | |
252 | OSSL_PARAM_END | |
253 | }; | |
254 | static const OSSL_PARAM dh_parameter_types[] = { | |
255 | DH_IMEXPORTABLE_PARAMETERS, | |
256 | OSSL_PARAM_END | |
257 | }; | |
258 | static const OSSL_PARAM dh_key_types[] = { | |
259 | DH_IMEXPORTABLE_PUBLIC_KEY, | |
260 | DH_IMEXPORTABLE_PRIVATE_KEY, | |
261 | OSSL_PARAM_END | |
262 | }; | |
263 | static const OSSL_PARAM *dh_types[] = { | |
264 | NULL, /* Index 0 = none of them */ | |
265 | dh_parameter_types, /* Index 1 = parameter types */ | |
266 | dh_key_types, /* Index 2 = key types */ | |
267 | dh_all_types /* Index 3 = 1 + 2 */ | |
268 | }; | |
269 | ||
270 | static const OSSL_PARAM *dh_imexport_types(int selection) | |
271 | { | |
272 | int type_select = 0; | |
273 | ||
274 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) | |
275 | type_select += 1; | |
276 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) | |
277 | type_select += 2; | |
278 | return dh_types[type_select]; | |
279 | } | |
280 | ||
281 | static const OSSL_PARAM *dh_import_types(int selection) | |
282 | { | |
283 | return dh_imexport_types(selection); | |
284 | } | |
285 | ||
286 | static const OSSL_PARAM *dh_export_types(int selection) | |
287 | { | |
288 | return dh_imexport_types(selection); | |
289 | } | |
290 | ||
8dd5c603 | 291 | static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[]) |
9e5aaf78 RL |
292 | { |
293 | DH *dh = key; | |
294 | OSSL_PARAM *p; | |
295 | ||
296 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL | |
297 | && !OSSL_PARAM_set_int(p, DH_bits(dh))) | |
298 | return 0; | |
299 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL | |
300 | && !OSSL_PARAM_set_int(p, DH_security_bits(dh))) | |
301 | return 0; | |
302 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL | |
303 | && !OSSL_PARAM_set_int(p, DH_size(dh))) | |
304 | return 0; | |
6a9bd929 MC |
305 | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT)) != NULL) { |
306 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
307 | return 0; | |
308 | p->return_size = dh_key2buf(dh, (unsigned char **)&p->data, | |
309 | p->data_size, 0); | |
310 | if (p->return_size == 0) | |
311 | return 0; | |
312 | } | |
313 | ||
7165593c SL |
314 | return ffc_params_todata(dh_get0_params(dh), NULL, params) |
315 | && dh_key_todata(dh, NULL, params); | |
9e5aaf78 RL |
316 | } |
317 | ||
273a67e3 RL |
318 | static const OSSL_PARAM dh_params[] = { |
319 | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), | |
320 | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), | |
321 | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), | |
6a9bd929 | 322 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0), |
7165593c SL |
323 | DH_IMEXPORTABLE_PARAMETERS, |
324 | DH_IMEXPORTABLE_PUBLIC_KEY, | |
325 | DH_IMEXPORTABLE_PRIVATE_KEY, | |
273a67e3 RL |
326 | OSSL_PARAM_END |
327 | }; | |
328 | ||
329 | static const OSSL_PARAM *dh_gettable_params(void) | |
330 | { | |
331 | return dh_params; | |
332 | } | |
333 | ||
6a9bd929 MC |
334 | static const OSSL_PARAM dh_known_settable_params[] = { |
335 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_TLS_ENCODED_PT, NULL, 0), | |
336 | OSSL_PARAM_END | |
337 | }; | |
338 | ||
339 | static const OSSL_PARAM *dh_settable_params(void) | |
340 | { | |
341 | return dh_known_settable_params; | |
342 | } | |
343 | ||
344 | static int dh_set_params(void *key, const OSSL_PARAM params[]) | |
345 | { | |
346 | DH *dh = key; | |
347 | const OSSL_PARAM *p; | |
348 | ||
349 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_TLS_ENCODED_PT); | |
350 | if (p != NULL | |
351 | && (p->data_type != OSSL_PARAM_OCTET_STRING | |
352 | || !dh_buf2key(dh, p->data, p->data_size))) | |
353 | return 0; | |
354 | ||
355 | return 1; | |
356 | } | |
357 | ||
a54ff473 SL |
358 | static int dh_validate_public(DH *dh) |
359 | { | |
360 | const BIGNUM *pub_key = NULL; | |
361 | ||
362 | DH_get0_key(dh, &pub_key, NULL); | |
2fc2e37b MB |
363 | if (pub_key == NULL) |
364 | return 0; | |
a54ff473 SL |
365 | return DH_check_pub_key_ex(dh, pub_key); |
366 | } | |
367 | ||
368 | static int dh_validate_private(DH *dh) | |
369 | { | |
370 | int status = 0; | |
371 | const BIGNUM *priv_key = NULL; | |
372 | ||
373 | DH_get0_key(dh, NULL, &priv_key); | |
2fc2e37b MB |
374 | if (priv_key == NULL) |
375 | return 0; | |
a54ff473 SL |
376 | return dh_check_priv_key(dh, priv_key, &status);; |
377 | } | |
378 | ||
379 | static int dh_validate(void *keydata, int selection) | |
380 | { | |
381 | DH *dh = keydata; | |
382 | int ok = 0; | |
383 | ||
384 | if ((selection & DH_POSSIBLE_SELECTIONS) != 0) | |
385 | ok = 1; | |
386 | ||
387 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) | |
388 | ok = ok && DH_check_params_ex(dh); | |
389 | ||
390 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) | |
391 | ok = ok && dh_validate_public(dh); | |
392 | ||
393 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) | |
394 | ok = ok && dh_validate_private(dh); | |
395 | ||
396 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) | |
397 | == OSSL_KEYMGMT_SELECT_KEYPAIR) | |
398 | ok = ok && dh_check_pairwise(dh); | |
399 | return ok; | |
400 | } | |
401 | ||
7165593c SL |
402 | static void *dh_gen_init(void *provctx, int selection) |
403 | { | |
404 | OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
405 | struct dh_gen_ctx *gctx = NULL; | |
406 | ||
407 | if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR | |
408 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0) | |
409 | return NULL; | |
410 | ||
411 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { | |
412 | gctx->selection = selection; | |
413 | gctx->libctx = libctx; | |
414 | gctx->pbits = 2048; | |
415 | gctx->qbits = 224; | |
4f2271d5 | 416 | gctx->mdname = NULL; |
7165593c SL |
417 | gctx->gen_type = DH_PARAMGEN_TYPE_FIPS_186_4; |
418 | gctx->gindex = -1; | |
419 | gctx->hindex = 0; | |
420 | gctx->pcounter = -1; | |
421 | gctx->generator = DH_GENERATOR_2; | |
422 | } | |
423 | return gctx; | |
424 | } | |
425 | ||
426 | static int dh_gen_set_template(void *genctx, void *templ) | |
427 | { | |
428 | struct dh_gen_ctx *gctx = genctx; | |
429 | DH *dh = templ; | |
430 | ||
431 | if (gctx == NULL || dh == NULL) | |
432 | return 0; | |
433 | gctx->ffc_params = dh_get0_params(dh); | |
434 | return 1; | |
435 | } | |
436 | ||
437 | static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed, | |
438 | size_t seedlen) | |
439 | { | |
440 | OPENSSL_clear_free(gctx->seed, gctx->seedlen); | |
441 | gctx->seed = NULL; | |
442 | gctx->seedlen = 0; | |
443 | if (seed != NULL && seedlen > 0) { | |
444 | gctx->seed = OPENSSL_memdup(seed, seedlen); | |
445 | if (gctx->seed == NULL) | |
446 | return 0; | |
447 | gctx->seedlen = seedlen; | |
448 | } | |
449 | return 1; | |
450 | } | |
451 | ||
452 | static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[]) | |
453 | { | |
454 | struct dh_gen_ctx *gctx = genctx; | |
455 | const OSSL_PARAM *p; | |
456 | ||
457 | if (gctx == NULL) | |
458 | return 0; | |
459 | ||
460 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE); | |
461 | if (p != NULL) { | |
462 | if (p->data_type != OSSL_PARAM_UTF8_STRING | |
463 | || ((gctx->gen_type = dh_gen_type_name2id(p->data)) == -1)) { | |
464 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
465 | return 0; | |
466 | } | |
467 | } | |
023b188c | 468 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); |
7165593c SL |
469 | if (p != NULL) { |
470 | if (p->data_type != OSSL_PARAM_UTF8_STRING | |
471 | || ((gctx->group_nid = ffc_named_group_to_uid(p->data)) == NID_undef)) { | |
472 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
473 | return 0; | |
474 | } | |
475 | gctx->gen_type = DH_PARAMGEN_TYPE_GROUP; | |
476 | } | |
b8086652 | 477 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR); |
738ee181 | 478 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator)) |
7165593c SL |
479 | return 0; |
480 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX); | |
738ee181 | 481 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex)) |
7165593c SL |
482 | return 0; |
483 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER); | |
738ee181 | 484 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter)) |
7165593c SL |
485 | return 0; |
486 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H); | |
738ee181 | 487 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex)) |
7165593c SL |
488 | return 0; |
489 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED); | |
490 | if (p != NULL | |
491 | && (p->data_type != OSSL_PARAM_OCTET_STRING | |
492 | || !dh_set_gen_seed(gctx, p->data, p->data_size))) | |
493 | return 0; | |
494 | ||
495 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL | |
496 | && !OSSL_PARAM_get_size_t(p, &gctx->pbits)) | |
497 | return 0; | |
498 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL | |
499 | && !OSSL_PARAM_get_size_t(p, &gctx->qbits)) | |
500 | return 0; | |
501 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST); | |
502 | if (p != NULL) { | |
7165593c SL |
503 | if (p->data_type != OSSL_PARAM_UTF8_STRING) |
504 | return 0; | |
4f2271d5 SL |
505 | gctx->mdname = p->data; |
506 | } | |
507 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS); | |
508 | if (p != NULL) { | |
509 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
7165593c | 510 | return 0; |
4f2271d5 | 511 | gctx->mdprops = p->data; |
7165593c | 512 | } |
738ee181 SL |
513 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN); |
514 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len)) | |
515 | return 0; | |
7165593c SL |
516 | return 1; |
517 | } | |
518 | ||
519 | static const OSSL_PARAM *dh_gen_settable_params(void *provctx) | |
520 | { | |
521 | static OSSL_PARAM settable[] = { | |
023b188c | 522 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), |
b8086652 SL |
523 | OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), |
524 | OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL), | |
7165593c SL |
525 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0), |
526 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL), | |
527 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL), | |
528 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0), | |
529 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0), | |
530 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), | |
531 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), | |
7165593c SL |
532 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), |
533 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), | |
534 | OSSL_PARAM_END | |
535 | }; | |
536 | return settable; | |
537 | } | |
538 | ||
539 | static int dh_gencb(int p, int n, BN_GENCB *cb) | |
540 | { | |
541 | struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb); | |
542 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; | |
543 | ||
544 | params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p); | |
545 | params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n); | |
546 | ||
547 | return gctx->cb(params, gctx->cbarg); | |
548 | } | |
549 | ||
550 | static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) | |
551 | { | |
552 | int ret = 0; | |
553 | struct dh_gen_ctx *gctx = genctx; | |
554 | DH *dh = NULL; | |
555 | BN_GENCB *gencb = NULL; | |
556 | FFC_PARAMS *ffc; | |
557 | ||
558 | if (gctx == NULL) | |
559 | return NULL; | |
560 | ||
561 | /* For parameter generation - If there is a group name just create it */ | |
562 | if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP) { | |
563 | /* Select a named group if there is not one already */ | |
564 | if (gctx->group_nid == NID_undef) | |
565 | gctx->group_nid = dh_get_named_group_uid_from_size(gctx->pbits); | |
566 | if (gctx->group_nid == NID_undef) | |
567 | return NULL; | |
568 | dh = dh_new_by_nid_with_libctx(gctx->libctx, gctx->group_nid); | |
569 | if (dh == NULL) | |
570 | return NULL; | |
571 | ffc = dh_get0_params(dh); | |
572 | } else { | |
573 | dh = dh_new_with_libctx(gctx->libctx); | |
574 | if (dh == NULL) | |
575 | return NULL; | |
576 | ffc = dh_get0_params(dh); | |
577 | ||
578 | /* Copy the template value if one was passed */ | |
579 | if (gctx->ffc_params != NULL | |
580 | && !ffc_params_copy(ffc, gctx->ffc_params)) | |
581 | goto end; | |
582 | ||
583 | if (!ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen)) | |
584 | goto end; | |
585 | if (gctx->gindex != -1) { | |
586 | ffc_params_set_gindex(ffc, gctx->gindex); | |
587 | if (gctx->pcounter != -1) | |
588 | ffc_params_set_pcounter(ffc, gctx->pcounter); | |
589 | } else if (gctx->hindex != 0) { | |
590 | ffc_params_set_h(ffc, gctx->hindex); | |
591 | } | |
4f2271d5 SL |
592 | if (gctx->mdname != NULL) { |
593 | if (!ffc_set_digest(ffc, gctx->mdname, gctx->mdprops)) | |
594 | goto end; | |
595 | } | |
7165593c SL |
596 | gctx->cb = osslcb; |
597 | gctx->cbarg = cbarg; | |
598 | gencb = BN_GENCB_new(); | |
599 | if (gencb != NULL) | |
600 | BN_GENCB_set(gencb, dh_gencb, genctx); | |
601 | ||
602 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { | |
603 | /* | |
604 | * NOTE: The old safe prime generator code is not used in fips mode, | |
605 | * (i.e internally it ignores the generator and chooses a named | |
606 | * group based on pbits. | |
607 | */ | |
608 | if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR) | |
609 | ret = DH_generate_parameters_ex(dh, gctx->pbits, | |
610 | gctx->generator, gencb); | |
611 | else | |
612 | ret = dh_generate_ffc_parameters(dh, gctx->gen_type, gctx->pbits, | |
4f2271d5 | 613 | gctx->qbits, gencb); |
7165593c SL |
614 | if (ret <= 0) |
615 | goto end; | |
616 | } | |
617 | } | |
618 | ||
619 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { | |
620 | if (ffc->p == NULL || ffc->g == NULL) | |
621 | goto end; | |
738ee181 SL |
622 | if (gctx->priv_len > 0) |
623 | DH_set_length(dh, (long)gctx->priv_len); | |
7165593c SL |
624 | if (DH_generate_key(dh) <= 0) |
625 | goto end; | |
626 | } | |
627 | ret = 1; | |
628 | end: | |
629 | if (ret <= 0) { | |
630 | DH_free(dh); | |
631 | dh = NULL; | |
632 | } | |
633 | BN_GENCB_free(gencb); | |
634 | return dh; | |
635 | } | |
636 | ||
637 | static void dh_gen_cleanup(void *genctx) | |
638 | { | |
639 | struct dh_gen_ctx *gctx = genctx; | |
640 | ||
641 | if (gctx == NULL) | |
642 | return; | |
643 | ||
644 | OPENSSL_clear_free(gctx->seed, gctx->seedlen); | |
7165593c SL |
645 | OPENSSL_free(gctx); |
646 | } | |
647 | ||
7c664b1f RL |
648 | void *dh_load(const void *reference, size_t reference_sz) |
649 | { | |
650 | DH *dh = NULL; | |
651 | ||
652 | if (reference_sz == sizeof(dh)) { | |
653 | /* The contents of the reference is the address to our object */ | |
654 | dh = *(DH **)reference; | |
655 | /* We grabbed, so we detach it */ | |
656 | *(DH **)reference = NULL; | |
657 | return dh; | |
658 | } | |
659 | return NULL; | |
660 | } | |
661 | ||
8b84b075 | 662 | const OSSL_DISPATCH dh_keymgmt_functions[] = { |
8dd5c603 | 663 | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata }, |
7165593c SL |
664 | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init }, |
665 | { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template }, | |
666 | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params }, | |
667 | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, | |
668 | (void (*)(void))dh_gen_settable_params }, | |
669 | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen }, | |
670 | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup }, | |
7c664b1f | 671 | { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load }, |
8dd5c603 | 672 | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata }, |
273a67e3 RL |
673 | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params }, |
674 | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params }, | |
6a9bd929 MC |
675 | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params }, |
676 | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params }, | |
8dd5c603 | 677 | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has }, |
2888fc15 | 678 | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match }, |
a54ff473 | 679 | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate }, |
8dd5c603 | 680 | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import }, |
273a67e3 | 681 | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types }, |
8dd5c603 | 682 | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export }, |
273a67e3 | 683 | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types }, |
8b84b075 RL |
684 | { 0, NULL } |
685 | }; |