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