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