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