PROV: Add a OP_keymgmt_match() function to our DH, DSA, RSA and EC_KEY impl
[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 /*
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 <openssl/core_numbers.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/params.h>
20 #include "internal/param_build.h"
21 #include "crypto/dh.h"
22 #include "prov/implementations.h"
23 #include "prov/providercommon.h"
24 #include "prov/provider_ctx.h"
25 #include "crypto/dh.h"
26
27 static OSSL_OP_keymgmt_new_fn dh_newdata;
28 static OSSL_OP_keymgmt_free_fn dh_freedata;
29 static OSSL_OP_keymgmt_get_params_fn dh_get_params;
30 static OSSL_OP_keymgmt_gettable_params_fn dh_gettable_params;
31 static OSSL_OP_keymgmt_has_fn dh_has;
32 static OSSL_OP_keymgmt_match_fn dh_match;
33 static OSSL_OP_keymgmt_import_fn dh_import;
34 static OSSL_OP_keymgmt_import_types_fn dh_import_types;
35 static OSSL_OP_keymgmt_export_fn dh_export;
36 static OSSL_OP_keymgmt_export_types_fn dh_export_types;
37
38 #define DH_POSSIBLE_SELECTIONS                 \
39     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
40
41 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
42 {
43     const OSSL_PARAM *param_p, *param_g;
44     BIGNUM *p = NULL, *g = NULL;
45
46     if (dh == NULL)
47         return 0;
48
49     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
50     param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
51
52     if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
53         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
54         goto err;
55
56     if (!DH_set0_pqg(dh, p, NULL, g))
57         goto err;
58
59     return 1;
60
61  err:
62     BN_free(p);
63     BN_free(g);
64     return 0;
65 }
66
67 static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
68 {
69     const BIGNUM *dh_p = NULL, *dh_g = NULL;
70
71     if (dh == NULL)
72         return 0;
73
74     DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
75     if (dh_p != NULL
76         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
77         return 0;
78     if (dh_g != NULL
79         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
80         return 0;
81
82     return 1;
83 }
84
85 static int params_to_key(DH *dh, const OSSL_PARAM params[])
86 {
87     const OSSL_PARAM *param_priv_key, *param_pub_key;
88     BIGNUM *priv_key = NULL, *pub_key = NULL;
89
90     if (dh == NULL)
91         return 0;
92
93     if (!params_to_domparams(dh, params))
94         return 0;
95
96     param_priv_key =
97         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
98     param_pub_key =
99         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
100
101     /*
102      * DH documentation says that a public key must be present if a
103      * private key is present.
104      * We want to have at least a public key either way, so we end up
105      * requiring it unconditionally.
106      */
107     if (param_pub_key == NULL)
108         return 0;
109
110     if ((param_priv_key != NULL
111          && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
112         || !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
113         goto err;
114
115     if (!DH_set0_key(dh, pub_key, priv_key))
116         goto err;
117
118     return 1;
119
120  err:
121     BN_clear_free(priv_key);
122     BN_free(pub_key);
123     return 0;
124 }
125
126 static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
127 {
128     const BIGNUM *priv_key = NULL, *pub_key = NULL;
129
130     if (dh == NULL)
131         return 0;
132     if (!domparams_to_params(dh, tmpl))
133         return 0;
134
135     DH_get0_key(dh, &pub_key, &priv_key);
136     if (priv_key != NULL
137         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
138         return 0;
139     if (pub_key != NULL
140         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
141         return 0;
142
143     return 1;
144 }
145
146 static void *dh_newdata(void *provctx)
147 {
148     return dh_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx));
149 }
150
151 static void dh_freedata(void *keydata)
152 {
153     DH_free(keydata);
154 }
155
156 static int dh_has(void *keydata, int selection)
157 {
158     DH *dh = keydata;
159     int ok = 0;
160
161     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
162         ok = 1;
163
164     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
165         ok = ok && (DH_get0_pub_key(dh) != NULL);
166     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
167         ok = ok && (DH_get0_priv_key(dh) != NULL);
168     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
169         ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
170     return ok;
171 }
172
173 static int dh_match(const void *keydata1, const void *keydata2, int selection)
174 {
175     const DH *dh1 = keydata1;
176     const DH *dh2 = keydata2;
177     int ok = 1;
178
179     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
180         ok = ok && BN_cmp(DH_get0_pub_key(dh1), DH_get0_pub_key(dh2)) == 0;
181     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
182         ok = ok && BN_cmp(DH_get0_priv_key(dh1), DH_get0_priv_key(dh2)) == 0;
183     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
184         FFC_PARAMS *dhparams1 = dh_get0_params((DH *)dh1);
185         FFC_PARAMS *dhparams2 = dh_get0_params((DH *)dh2);
186
187         ok = ok && ffc_params_cmp(dhparams1, dhparams2, 1);
188     }
189     return ok;
190 }
191
192 static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
193 {
194     DH *dh = keydata;
195     int ok = 0;
196
197     if (dh == NULL)
198         return 0;
199
200     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
201         ok = 1;
202
203     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
204         ok = ok && params_to_domparams(dh, params);
205     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
206         ok = ok && params_to_key(dh, params);
207
208     return ok;
209 }
210
211 static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
212                      void *cbarg)
213 {
214     DH *dh = keydata;
215     OSSL_PARAM_BLD tmpl;
216     OSSL_PARAM *params = NULL;
217     int ok = 1;
218
219     if (dh == NULL)
220         return 0;
221
222     ossl_param_bld_init(&tmpl);
223
224     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
225         ok = ok && domparams_to_params(dh, &tmpl);
226     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
227         ok = ok && key_to_params(dh, &tmpl);
228
229     if (!ok
230         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
231         return 0;
232
233     ok = param_cb(params, cbarg);
234     ossl_param_bld_free(params);
235     return ok;
236 }
237
238 /* IMEXPORT = IMPORT + EXPORT */
239
240 # define DH_IMEXPORTABLE_PARAMETERS                     \
241     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0),      \
242     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
243 # define DH_IMEXPORTABLE_PUBLIC_KEY                     \
244     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
245 # define DH_IMEXPORTABLE_PRIVATE_KEY                    \
246     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
247 static const OSSL_PARAM dh_all_types[] = {
248     DH_IMEXPORTABLE_PARAMETERS,
249     DH_IMEXPORTABLE_PUBLIC_KEY,
250     DH_IMEXPORTABLE_PRIVATE_KEY,
251     OSSL_PARAM_END
252 };
253 static const OSSL_PARAM dh_parameter_types[] = {
254     DH_IMEXPORTABLE_PARAMETERS,
255     OSSL_PARAM_END
256 };
257 static const OSSL_PARAM dh_key_types[] = {
258     DH_IMEXPORTABLE_PUBLIC_KEY,
259     DH_IMEXPORTABLE_PRIVATE_KEY,
260     OSSL_PARAM_END
261 };
262 static const OSSL_PARAM *dh_types[] = {
263     NULL,                        /* Index 0 = none of them */
264     dh_parameter_types,          /* Index 1 = parameter types */
265     dh_key_types,                /* Index 2 = key types */
266     dh_all_types                 /* Index 3 = 1 + 2 */
267 };
268
269 static const OSSL_PARAM *dh_imexport_types(int selection)
270 {
271     int type_select = 0;
272
273     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
274         type_select += 1;
275     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
276         type_select += 2;
277     return dh_types[type_select];
278 }
279
280 static const OSSL_PARAM *dh_import_types(int selection)
281 {
282     return dh_imexport_types(selection);
283 }
284
285 static const OSSL_PARAM *dh_export_types(int selection)
286 {
287     return dh_imexport_types(selection);
288 }
289
290 static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
291 {
292     DH *dh = key;
293     OSSL_PARAM *p;
294
295     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
296         && !OSSL_PARAM_set_int(p, DH_bits(dh)))
297         return 0;
298     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
299         && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
300         return 0;
301     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
302         && !OSSL_PARAM_set_int(p, DH_size(dh)))
303         return 0;
304     return 1;
305 }
306
307 static const OSSL_PARAM dh_params[] = {
308     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
309     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
310     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
311     OSSL_PARAM_END
312 };
313
314 static const OSSL_PARAM *dh_gettable_params(void)
315 {
316     return dh_params;
317 }
318
319 const OSSL_DISPATCH dh_keymgmt_functions[] = {
320     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
321     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
322     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
323     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
324     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
325     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
326     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
327     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
328     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
329     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
330     { 0, NULL }
331 };