[PROV][KEYMGMT][DH][DSA] use BN_clear_free for secrets
[oweals/openssl.git] / providers / implementations / keymgmt / dh_kmgmt.c
1 /*
2  * Copyright 2019 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 #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"
20
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;
30
31 #define DH_POSSIBLE_SELECTIONS                 \
32     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
33
34 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
35 {
36     const OSSL_PARAM *param_p, *param_g;
37     BIGNUM *p = NULL, *g = NULL;
38
39     if (dh == NULL)
40         return 0;
41
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);
44
45     if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
46         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
47         goto err;
48
49     if (!DH_set0_pqg(dh, p, NULL, g))
50         goto err;
51
52     return 1;
53
54  err:
55     BN_free(p);
56     BN_free(g);
57     return 0;
58 }
59
60 static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
61 {
62     const BIGNUM *dh_p = NULL, *dh_g = NULL;
63
64     if (dh == NULL)
65         return 0;
66
67     DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
68     if (dh_p != NULL
69         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
70         return 0;
71     if (dh_g != NULL
72         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
73         return 0;
74
75     return 1;
76 }
77
78 static int params_to_key(DH *dh, const OSSL_PARAM params[])
79 {
80     const OSSL_PARAM *param_priv_key, *param_pub_key;
81     BIGNUM *priv_key = NULL, *pub_key = NULL;
82
83     if (dh == NULL)
84         return 0;
85
86     if (!params_to_domparams(dh, params))
87         return 0;
88
89     param_priv_key =
90         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
91     param_pub_key =
92         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
93
94     /*
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.
99      */
100     if (param_pub_key == NULL)
101         return 0;
102
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))
106         goto err;
107
108     if (!DH_set0_key(dh, pub_key, priv_key))
109         goto err;
110
111     return 1;
112
113  err:
114     BN_clear_free(priv_key);
115     BN_free(pub_key);
116     return 0;
117 }
118
119 static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
120 {
121     const BIGNUM *priv_key = NULL, *pub_key = NULL;
122
123     if (dh == NULL)
124         return 0;
125     if (!domparams_to_params(dh, tmpl))
126         return 0;
127
128     DH_get0_key(dh, &pub_key, &priv_key);
129     if (priv_key != NULL
130         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
131         return 0;
132     if (pub_key != NULL
133         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
134         return 0;
135
136     return 1;
137 }
138
139 static void *dh_newdata(void *provctx)
140 {
141     return dh_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx));
142 }
143
144 static void dh_freedata(void *keydata)
145 {
146     DH_free(keydata);
147 }
148
149 static int dh_has(void *keydata, int selection)
150 {
151     DH *dh = keydata;
152     int ok = 0;
153
154     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
155         ok = 1;
156
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);
163     return ok;
164 }
165
166 static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
167 {
168     DH *dh = keydata;
169     int ok = 0;
170
171     if (dh == NULL)
172         return 0;
173
174     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
175         ok = 1;
176
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);
181
182     return ok;
183 }
184
185 static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
186                      void *cbarg)
187 {
188     DH *dh = keydata;
189     OSSL_PARAM_BLD tmpl;
190     OSSL_PARAM *params = NULL;
191     int ok = 1;
192
193     if (dh == NULL)
194         return 0;
195
196     ossl_param_bld_init(&tmpl);
197
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);
202
203     if (!ok
204         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
205         return 0;
206
207     ok = param_cb(params, cbarg);
208     ossl_param_bld_free(params);
209     return ok;
210 }
211
212 /* IMEXPORT = IMPORT + EXPORT */
213
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,
225     OSSL_PARAM_END
226 };
227 static const OSSL_PARAM dh_parameter_types[] = {
228     DH_IMEXPORTABLE_PARAMETERS,
229     OSSL_PARAM_END
230 };
231 static const OSSL_PARAM dh_key_types[] = {
232     DH_IMEXPORTABLE_PUBLIC_KEY,
233     DH_IMEXPORTABLE_PRIVATE_KEY,
234     OSSL_PARAM_END
235 };
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 */
241 };
242
243 static const OSSL_PARAM *dh_imexport_types(int selection)
244 {
245     int type_select = 0;
246
247     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
248         type_select += 1;
249     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
250         type_select += 2;
251     return dh_types[type_select];
252 }
253
254 static const OSSL_PARAM *dh_import_types(int selection)
255 {
256     return dh_imexport_types(selection);
257 }
258
259 static const OSSL_PARAM *dh_export_types(int selection)
260 {
261     return dh_imexport_types(selection);
262 }
263
264 static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
265 {
266     DH *dh = key;
267     OSSL_PARAM *p;
268
269     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
270         && !OSSL_PARAM_set_int(p, DH_bits(dh)))
271         return 0;
272     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
273         && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
274         return 0;
275     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
276         && !OSSL_PARAM_set_int(p, DH_size(dh)))
277         return 0;
278     return 1;
279 }
280
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),
285     OSSL_PARAM_END
286 };
287
288 static const OSSL_PARAM *dh_gettable_params(void)
289 {
290     return dh_params;
291 }
292
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 },
303     { 0, NULL }
304 };