2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/bn.h>
13 #include <openssl/params.h>
14 #include "internal/param_build.h"
15 #include "crypto/dh.h"
16 #include "prov/implementations.h"
17 #include "prov/providercommon.h"
18 #include "prov/provider_ctx.h"
19 #include "crypto/dh.h"
21 static OSSL_OP_keymgmt_new_fn dh_newdata;
22 static OSSL_OP_keymgmt_free_fn dh_freedata;
23 static OSSL_OP_keymgmt_get_params_fn dh_get_params;
24 static OSSL_OP_keymgmt_gettable_params_fn dh_gettable_params;
25 static OSSL_OP_keymgmt_has_fn dh_has;
26 static OSSL_OP_keymgmt_import_fn dh_import;
27 static OSSL_OP_keymgmt_import_types_fn dh_import_types;
28 static OSSL_OP_keymgmt_export_fn dh_export;
29 static OSSL_OP_keymgmt_export_types_fn dh_export_types;
31 #define DH_POSSIBLE_SELECTIONS \
32 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
34 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
36 const OSSL_PARAM *param_p, *param_g;
37 BIGNUM *p = NULL, *g = NULL;
42 param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
43 param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
45 if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
46 || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
49 if (!DH_set0_pqg(dh, p, NULL, g))
60 static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
62 const BIGNUM *dh_p = NULL, *dh_g = NULL;
67 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
69 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
72 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
78 static int params_to_key(DH *dh, const OSSL_PARAM params[])
80 const OSSL_PARAM *param_priv_key, *param_pub_key;
81 BIGNUM *priv_key = NULL, *pub_key = NULL;
86 if (!params_to_domparams(dh, params))
90 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
92 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
95 * DH documentation says that a public key must be present if a
96 * private key is present.
97 * We want to have at least a public key either way, so we end up
98 * requiring it unconditionally.
100 if (param_pub_key == NULL)
103 if ((param_priv_key != NULL
104 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
105 || !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
108 if (!DH_set0_key(dh, pub_key, priv_key))
114 BN_clear_free(priv_key);
119 static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
121 const BIGNUM *priv_key = NULL, *pub_key = NULL;
125 if (!domparams_to_params(dh, tmpl))
128 DH_get0_key(dh, &pub_key, &priv_key);
130 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
133 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
139 static void *dh_newdata(void *provctx)
141 return dh_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx));
144 static void dh_freedata(void *keydata)
149 static int dh_has(void *keydata, int selection)
154 if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
157 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
158 ok = ok && (DH_get0_pub_key(dh) != NULL);
159 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
160 ok = ok && (DH_get0_priv_key(dh) != NULL);
161 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
162 ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
166 static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
174 if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
177 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
178 ok = ok && params_to_domparams(dh, params);
179 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
180 ok = ok && params_to_key(dh, params);
185 static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
190 OSSL_PARAM *params = NULL;
196 ossl_param_bld_init(&tmpl);
198 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
199 ok = ok && domparams_to_params(dh, &tmpl);
200 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
201 ok = ok && key_to_params(dh, &tmpl);
204 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
207 ok = param_cb(params, cbarg);
208 ossl_param_bld_free(params);
212 /* IMEXPORT = IMPORT + EXPORT */
214 # define DH_IMEXPORTABLE_PARAMETERS \
215 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
216 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
217 # define DH_IMEXPORTABLE_PUBLIC_KEY \
218 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
219 # define DH_IMEXPORTABLE_PRIVATE_KEY \
220 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
221 static const OSSL_PARAM dh_all_types[] = {
222 DH_IMEXPORTABLE_PARAMETERS,
223 DH_IMEXPORTABLE_PUBLIC_KEY,
224 DH_IMEXPORTABLE_PRIVATE_KEY,
227 static const OSSL_PARAM dh_parameter_types[] = {
228 DH_IMEXPORTABLE_PARAMETERS,
231 static const OSSL_PARAM dh_key_types[] = {
232 DH_IMEXPORTABLE_PUBLIC_KEY,
233 DH_IMEXPORTABLE_PRIVATE_KEY,
236 static const OSSL_PARAM *dh_types[] = {
237 NULL, /* Index 0 = none of them */
238 dh_parameter_types, /* Index 1 = parameter types */
239 dh_key_types, /* Index 2 = key types */
240 dh_all_types /* Index 3 = 1 + 2 */
243 static const OSSL_PARAM *dh_imexport_types(int selection)
247 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
249 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
251 return dh_types[type_select];
254 static const OSSL_PARAM *dh_import_types(int selection)
256 return dh_imexport_types(selection);
259 static const OSSL_PARAM *dh_export_types(int selection)
261 return dh_imexport_types(selection);
264 static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
269 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
270 && !OSSL_PARAM_set_int(p, DH_bits(dh)))
272 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
273 && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
275 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
276 && !OSSL_PARAM_set_int(p, DH_size(dh)))
281 static const OSSL_PARAM dh_params[] = {
282 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
283 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
284 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
288 static const OSSL_PARAM *dh_gettable_params(void)
293 const OSSL_DISPATCH dh_keymgmt_functions[] = {
294 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
295 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
296 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
297 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
298 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
299 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
300 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
301 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
302 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },