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