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/dh.h>
14 #include <openssl/params.h>
15 #include "internal/param_build.h"
16 #include "crypto/dh.h"
17 #include "prov/implementations.h"
18 #include "prov/providercommon.h"
20 static OSSL_OP_keymgmt_new_fn dh_newdata;
21 static OSSL_OP_keymgmt_free_fn dh_freedata;
22 static OSSL_OP_keymgmt_has_fn dh_has;
23 static OSSL_OP_keymgmt_import_fn dh_import;
24 static OSSL_OP_keymgmt_export_fn dh_export;
25 static OSSL_OP_keymgmt_get_params_fn dh_get_params;
27 #define DH_POSSIBLE_SELECTIONS \
28 (OSSL_KEYMGMT_SELECT_KEY | OSSL_KEYMGMT_FLAG_DOMAIN_PARAMETERS)
30 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
32 const OSSL_PARAM *param_p, *param_g;
33 BIGNUM *p = NULL, *g = NULL;
38 param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
39 param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
41 if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
42 || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
45 if (!DH_set0_pqg(dh, p, NULL, g))
56 static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
58 const BIGNUM *dh_p = NULL, *dh_g = NULL;
63 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
65 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
68 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
74 static int params_to_key(DH *dh, const OSSL_PARAM params[])
76 const OSSL_PARAM *param_priv_key, *param_pub_key;
77 BIGNUM *priv_key = NULL, *pub_key = NULL;
82 if (!params_to_domparams(dh, params))
86 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_KEY);
88 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PUB_KEY);
91 * DH documentation says that a public key must be present if a
92 * private key is present.
93 * We want to have at least a public key either way, so we end up
94 * requiring it unconditionally.
96 if (param_pub_key == NULL)
99 if ((param_priv_key != NULL
100 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
101 || !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
104 if (!DH_set0_key(dh, pub_key, priv_key))
115 static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
117 const BIGNUM *priv_key = NULL, *pub_key = NULL;
121 if (!domparams_to_params(dh, tmpl))
124 DH_get0_key(dh, &pub_key, &priv_key);
126 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv_key))
129 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_key))
135 static void *dh_newdata(void *provctx)
140 static void dh_freedata(void *keydata)
145 static int dh_has(void *keydata, int selection)
150 if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
153 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
154 ok = ok && (DH_get0_pub_key(dh) != NULL);
155 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
156 ok = ok && (DH_get0_priv_key(dh) != NULL);
157 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
158 ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
162 static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
170 if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
173 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
174 ok = ok && params_to_domparams(dh, params);
175 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
176 ok = ok && params_to_key(dh, params);
181 static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
186 OSSL_PARAM *params = NULL;
192 ossl_param_bld_init(&tmpl);
194 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
195 ok = ok && domparams_to_params(dh, &tmpl);
196 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
197 ok = ok && key_to_params(dh, &tmpl);
200 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
203 ok = param_cb(params, cbarg);
204 ossl_param_bld_free(params);
208 static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
213 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
214 && !OSSL_PARAM_set_int(p, DH_bits(dh)))
216 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
217 && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
219 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
220 && !OSSL_PARAM_set_int(p, DH_size(dh)))
225 const OSSL_DISPATCH dh_keymgmt_functions[] = {
226 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
227 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
228 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
229 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
230 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
231 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },