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