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