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