make update
[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
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)
349 {
350     const BIGNUM *pub_key = NULL;
351
352     DH_get0_key(dh, &pub_key, NULL);
353     if (pub_key == NULL)
354         return 0;
355     return DH_check_pub_key_ex(dh, pub_key);
356 }
357
358 static int dh_validate_private(const DH *dh)
359 {
360     int status = 0;
361     const BIGNUM *priv_key = NULL;
362
363     DH_get0_key(dh, NULL, &priv_key);
364     if (priv_key == NULL)
365         return 0;
366     return dh_check_priv_key(dh, priv_key, &status);;
367 }
368
369 static int dh_validate(const void *keydata, int selection)
370 {
371     const DH *dh = keydata;
372     int ok = 0;
373
374     if (!ossl_prov_is_running())
375         return 0;
376
377     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
378         ok = 1;
379
380     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
381         ok = ok && DH_check_params_ex(dh);
382
383     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
384         ok = ok && dh_validate_public(dh);
385
386     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
387         ok = ok && dh_validate_private(dh);
388
389     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
390             == OSSL_KEYMGMT_SELECT_KEYPAIR)
391         ok = ok && dh_check_pairwise(dh);
392     return ok;
393 }
394
395 static void *dh_gen_init_base(void *provctx, int selection, int type)
396 {
397     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
398     struct dh_gen_ctx *gctx = NULL;
399
400     if (!ossl_prov_is_running())
401         return NULL;
402
403     if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
404                       | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
405         return NULL;
406
407     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
408         gctx->selection = selection;
409         gctx->libctx = libctx;
410         gctx->pbits = 2048;
411         gctx->qbits = 224;
412         gctx->mdname = NULL;
413 #ifdef FIPS_MODULE
414         gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
415                          ? DH_PARAMGEN_TYPE_FIPS_186_4
416                          : DH_PARAMGEN_TYPE_GROUP;
417 #else
418         gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
419                          ? DH_PARAMGEN_TYPE_FIPS_186_2
420                          : DH_PARAMGEN_TYPE_GENERATOR;
421 #endif
422         gctx->gindex = -1;
423         gctx->hindex = 0;
424         gctx->pcounter = -1;
425         gctx->generator = DH_GENERATOR_2;
426         gctx->dh_type = type;
427     }
428     return gctx;
429 }
430
431 static void *dh_gen_init(void *provctx, int selection)
432 {
433     return dh_gen_init_base(provctx, selection, DH_FLAG_TYPE_DH);
434 }
435
436 static void *dhx_gen_init(void *provctx, int selection)
437 {
438    return dh_gen_init_base(provctx, selection, DH_FLAG_TYPE_DHX);
439 }
440
441 static int dh_gen_set_template(void *genctx, void *templ)
442 {
443     struct dh_gen_ctx *gctx = genctx;
444     DH *dh = templ;
445
446     if (!ossl_prov_is_running() || gctx == NULL || dh == NULL)
447         return 0;
448     gctx->ffc_params = dh_get0_params(dh);
449     return 1;
450 }
451
452 static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed,
453                            size_t seedlen)
454 {
455     OPENSSL_clear_free(gctx->seed, gctx->seedlen);
456     gctx->seed = NULL;
457     gctx->seedlen = 0;
458     if (seed != NULL && seedlen > 0) {
459         gctx->seed = OPENSSL_memdup(seed, seedlen);
460         if (gctx->seed == NULL)
461             return 0;
462         gctx->seedlen = seedlen;
463     }
464     return 1;
465 }
466
467 static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[])
468 {
469     struct dh_gen_ctx *gctx = genctx;
470     const OSSL_PARAM *p;
471
472     if (gctx == NULL)
473         return 0;
474
475     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
476     if (p != NULL) {
477         if (p->data_type != OSSL_PARAM_UTF8_STRING
478             || ((gctx->gen_type =
479                  dh_gen_type_name2id_w_default(p->data,
480                                                gctx->dh_type)) == -1)) {
481             ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
482             return 0;
483         }
484     }
485     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
486     if (p != NULL) {
487         const DH_NAMED_GROUP *group = NULL;
488
489         if (p->data_type != OSSL_PARAM_UTF8_STRING
490             || (group = ossl_ffc_name_to_dh_named_group(p->data)) == NULL
491             || ((gctx->group_nid =
492                  ossl_ffc_named_group_get_uid(group)) == NID_undef)) {
493             ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
494             return 0;
495         }
496         gctx->gen_type = DH_PARAMGEN_TYPE_GROUP;
497     }
498     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
499     if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator))
500         return 0;
501     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
502     if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex))
503         return 0;
504     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
505     if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter))
506         return 0;
507     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
508     if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex))
509         return 0;
510     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
511     if (p != NULL
512         && (p->data_type != OSSL_PARAM_OCTET_STRING
513             || !dh_set_gen_seed(gctx, p->data, p->data_size)))
514             return 0;
515
516     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
517         && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
518         return 0;
519     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
520         && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
521         return 0;
522     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
523     if (p != NULL) {
524         if (p->data_type != OSSL_PARAM_UTF8_STRING)
525             return 0;
526         OPENSSL_free(gctx->mdname);
527         gctx->mdname = OPENSSL_strdup(p->data);
528         if (gctx->mdname == NULL)
529             return 0;
530     }
531     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
532     if (p != NULL) {
533         if (p->data_type != OSSL_PARAM_UTF8_STRING)
534             return 0;
535         OPENSSL_free(gctx->mdprops);
536         gctx->mdprops = OPENSSL_strdup(p->data);
537         if (gctx->mdprops == NULL)
538             return 0;
539     }
540     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
541     if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len))
542         return 0;
543     return 1;
544 }
545
546 static const OSSL_PARAM *dh_gen_settable_params(void *provctx)
547 {
548     static OSSL_PARAM settable[] = {
549         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
550         OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
551         OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL),
552         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
553         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
554         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
555         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
556         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
557         OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
558         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
559         OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
560         OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
561         OSSL_PARAM_END
562     };
563     return settable;
564 }
565
566 static int dh_gencb(int p, int n, BN_GENCB *cb)
567 {
568     struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb);
569     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
570
571     params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
572     params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
573
574     return gctx->cb(params, gctx->cbarg);
575 }
576
577 static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
578 {
579     int ret = 0;
580     struct dh_gen_ctx *gctx = genctx;
581     DH *dh = NULL;
582     BN_GENCB *gencb = NULL;
583     FFC_PARAMS *ffc;
584
585     if (!ossl_prov_is_running() || gctx == NULL)
586         return NULL;
587
588     /* For parameter generation - If there is a group name just create it */
589     if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP
590             && gctx->ffc_params == NULL) {
591         /* Select a named group if there is not one already */
592         if (gctx->group_nid == NID_undef)
593             gctx->group_nid = dh_get_named_group_uid_from_size(gctx->pbits);
594         if (gctx->group_nid == NID_undef)
595             return NULL;
596         dh = dh_new_by_nid_ex(gctx->libctx, gctx->group_nid);
597         if (dh == NULL)
598             return NULL;
599         ffc = dh_get0_params(dh);
600     } else {
601         dh = dh_new_ex(gctx->libctx);
602         if (dh == NULL)
603             return NULL;
604         ffc = dh_get0_params(dh);
605
606         /* Copy the template value if one was passed */
607         if (gctx->ffc_params != NULL
608             && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
609             goto end;
610
611         if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
612             goto end;
613         if (gctx->gindex != -1) {
614             ossl_ffc_params_set_gindex(ffc, gctx->gindex);
615             if (gctx->pcounter != -1)
616                 ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
617         } else if (gctx->hindex != 0) {
618             ossl_ffc_params_set_h(ffc, gctx->hindex);
619         }
620         if (gctx->mdname != NULL) {
621             if (!ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops))
622                 goto end;
623         }
624         gctx->cb = osslcb;
625         gctx->cbarg = cbarg;
626         gencb = BN_GENCB_new();
627         if (gencb != NULL)
628             BN_GENCB_set(gencb, dh_gencb, genctx);
629
630         if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
631             /*
632              * NOTE: The old safe prime generator code is not used in fips mode,
633              * (i.e internally it ignores the generator and chooses a named
634              * group based on pbits.
635              */
636             if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR)
637                 ret = DH_generate_parameters_ex(dh, gctx->pbits,
638                                                 gctx->generator, gencb);
639             else
640                 ret = dh_generate_ffc_parameters(dh, gctx->gen_type, gctx->pbits,
641                                                  gctx->qbits, gencb);
642             if (ret <= 0)
643                 goto end;
644         }
645     }
646
647     if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
648         if (ffc->p == NULL || ffc->g == NULL)
649             goto end;
650         if (gctx->priv_len > 0)
651             DH_set_length(dh, (long)gctx->priv_len);
652         ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
653                                      gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2);
654         if (DH_generate_key(dh) <= 0)
655             goto end;
656     }
657     DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
658     DH_set_flags(dh, gctx->dh_type);
659
660     ret = 1;
661 end:
662     if (ret <= 0) {
663         DH_free(dh);
664         dh = NULL;
665     }
666     BN_GENCB_free(gencb);
667     return dh;
668 }
669
670 static void dh_gen_cleanup(void *genctx)
671 {
672     struct dh_gen_ctx *gctx = genctx;
673
674     if (gctx == NULL)
675         return;
676
677     OPENSSL_free(gctx->mdname);
678     OPENSSL_free(gctx->mdprops);
679     OPENSSL_clear_free(gctx->seed, gctx->seedlen);
680     OPENSSL_free(gctx);
681 }
682
683 void *dh_load(const void *reference, size_t reference_sz)
684 {
685     DH *dh = NULL;
686
687     if (ossl_prov_is_running() && reference_sz == sizeof(dh)) {
688         /* The contents of the reference is the address to our object */
689         dh = *(DH **)reference;
690         /* We grabbed, so we detach it */
691         *(DH **)reference = NULL;
692         return dh;
693     }
694     return NULL;
695 }
696
697 const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = {
698     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
699     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init },
700     { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
701     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
702     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
703       (void (*)(void))dh_gen_settable_params },
704     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
705     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
706     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
707     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
708     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
709     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
710     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
711     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
712     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
713     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
714     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
715     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
716     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
717     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
718     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
719     { 0, NULL }
720 };
721
722 /* For any DH key, we use the "DH" algorithms regardless of sub-type. */
723 static const char *dhx_query_operation_name(int operation_id)
724 {
725     return "DH";
726 }
727
728 const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = {
729     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata },
730     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init },
731     { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
732     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
733     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
734       (void (*)(void))dh_gen_settable_params },
735     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
736     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
737     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
738     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
739     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
740     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
741     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
742     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
743     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
744     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
745     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
746     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
747     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
748     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
749     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
750     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
751       (void (*)(void))dhx_query_operation_name },
752     { 0, NULL }
753 };