apps/x509.c: Fix mem leaks in processing of -next_serial in print loop
[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     char *mdname;
73     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         OPENSSL_free(gctx->mdname);
553         gctx->mdname = OPENSSL_strdup(p->data);
554         if (gctx->mdname == NULL)
555             return 0;
556     }
557     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
558     if (p != NULL) {
559         if (p->data_type != OSSL_PARAM_UTF8_STRING)
560             return 0;
561         OPENSSL_free(gctx->mdprops);
562         gctx->mdprops = OPENSSL_strdup(p->data);
563         if (gctx->mdprops == NULL)
564             return 0;
565     }
566     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
567     if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len))
568         return 0;
569     return 1;
570 }
571
572 static const OSSL_PARAM *dh_gen_settable_params(void *provctx)
573 {
574     static OSSL_PARAM settable[] = {
575         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
576         OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
577         OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL),
578         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
579         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
580         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
581         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
582         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
583         OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
584         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
585         OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
586         OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
587         OSSL_PARAM_END
588     };
589     return settable;
590 }
591
592 static int dh_gencb(int p, int n, BN_GENCB *cb)
593 {
594     struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb);
595     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
596
597     params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
598     params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
599
600     return gctx->cb(params, gctx->cbarg);
601 }
602
603 static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
604 {
605     int ret = 0;
606     struct dh_gen_ctx *gctx = genctx;
607     DH *dh = NULL;
608     BN_GENCB *gencb = NULL;
609     FFC_PARAMS *ffc;
610
611     if (!ossl_prov_is_running() || gctx == NULL)
612         return NULL;
613
614     /* For parameter generation - If there is a group name just create it */
615     if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP
616             && gctx->ffc_params == NULL) {
617         /* Select a named group if there is not one already */
618         if (gctx->group_nid == NID_undef)
619             gctx->group_nid = dh_get_named_group_uid_from_size(gctx->pbits);
620         if (gctx->group_nid == NID_undef)
621             return NULL;
622         dh = dh_new_by_nid_ex(gctx->libctx, gctx->group_nid);
623         if (dh == NULL)
624             return NULL;
625         ffc = dh_get0_params(dh);
626     } else {
627         dh = dh_new_ex(gctx->libctx);
628         if (dh == NULL)
629             return NULL;
630         ffc = dh_get0_params(dh);
631
632         /* Copy the template value if one was passed */
633         if (gctx->ffc_params != NULL
634             && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
635             goto end;
636
637         if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
638             goto end;
639         if (gctx->gindex != -1) {
640             ossl_ffc_params_set_gindex(ffc, gctx->gindex);
641             if (gctx->pcounter != -1)
642                 ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
643         } else if (gctx->hindex != 0) {
644             ossl_ffc_params_set_h(ffc, gctx->hindex);
645         }
646         if (gctx->mdname != NULL) {
647             if (!ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops))
648                 goto end;
649         }
650         gctx->cb = osslcb;
651         gctx->cbarg = cbarg;
652         gencb = BN_GENCB_new();
653         if (gencb != NULL)
654             BN_GENCB_set(gencb, dh_gencb, genctx);
655
656         if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
657             /*
658              * NOTE: The old safe prime generator code is not used in fips mode,
659              * (i.e internally it ignores the generator and chooses a named
660              * group based on pbits.
661              */
662             if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR)
663                 ret = DH_generate_parameters_ex(dh, gctx->pbits,
664                                                 gctx->generator, gencb);
665             else
666                 ret = dh_generate_ffc_parameters(dh, gctx->gen_type, gctx->pbits,
667                                                  gctx->qbits, gencb);
668             if (ret <= 0)
669                 goto end;
670         }
671     }
672
673     if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
674         if (ffc->p == NULL || ffc->g == NULL)
675             goto end;
676         if (gctx->priv_len > 0)
677             DH_set_length(dh, (long)gctx->priv_len);
678         ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
679                                      gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2);
680         if (DH_generate_key(dh) <= 0)
681             goto end;
682     }
683     DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
684     DH_set_flags(dh, gctx->dh_type);
685
686     ret = 1;
687 end:
688     if (ret <= 0) {
689         DH_free(dh);
690         dh = NULL;
691     }
692     BN_GENCB_free(gencb);
693     return dh;
694 }
695
696 static void dh_gen_cleanup(void *genctx)
697 {
698     struct dh_gen_ctx *gctx = genctx;
699
700     if (gctx == NULL)
701         return;
702
703     OPENSSL_free(gctx->mdname);
704     OPENSSL_free(gctx->mdprops);
705     OPENSSL_clear_free(gctx->seed, gctx->seedlen);
706     OPENSSL_free(gctx);
707 }
708
709 void *dh_load(const void *reference, size_t reference_sz)
710 {
711     DH *dh = NULL;
712
713     if (ossl_prov_is_running() && reference_sz == sizeof(dh)) {
714         /* The contents of the reference is the address to our object */
715         dh = *(DH **)reference;
716         /* We grabbed, so we detach it */
717         *(DH **)reference = NULL;
718         return dh;
719     }
720     return NULL;
721 }
722
723 const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = {
724     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
725     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_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     { 0, NULL }
746 };
747
748 /* For any DH key, we use the "DH" algorithms regardless of sub-type. */
749 static const char *dhx_query_operation_name(int operation_id)
750 {
751     return "DH";
752 }
753
754 const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = {
755     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata },
756     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init },
757     { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
758     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
759     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
760       (void (*)(void))dh_gen_settable_params },
761     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
762     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
763     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
764     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
765     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
766     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
767     { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
768     { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
769     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
770     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
771     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
772     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
773     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
774     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
775     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
776     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
777       (void (*)(void))dhx_query_operation_name },
778     { 0, NULL }
779 };