Adapt existing KEYMGMT implementations to the redesigned interface
[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_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;
26
27 #define DH_POSSIBLE_SELECTIONS                 \
28     (OSSL_KEYMGMT_SELECT_KEY | OSSL_KEYMGMT_FLAG_DOMAIN_PARAMETERS)
29
30 static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
31 {
32     const OSSL_PARAM *param_p, *param_g;
33     BIGNUM *p = NULL, *g = NULL;
34
35     if (dh == NULL)
36         return 0;
37
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);
40
41     if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
42         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
43         goto err;
44
45     if (!DH_set0_pqg(dh, p, NULL, g))
46         goto err;
47
48     return 1;
49
50  err:
51     BN_free(p);
52     BN_free(g);
53     return 0;
54 }
55
56 static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
57 {
58     const BIGNUM *dh_p = NULL, *dh_g = NULL;
59
60     if (dh == NULL)
61         return 0;
62
63     DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
64     if (dh_p != NULL
65         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
66         return 0;
67     if (dh_g != NULL
68         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
69         return 0;
70
71     return 1;
72 }
73
74 static int params_to_key(DH *dh, const OSSL_PARAM params[])
75 {
76     const OSSL_PARAM *param_priv_key, *param_pub_key;
77     BIGNUM *priv_key = NULL, *pub_key = NULL;
78
79     if (dh == NULL)
80         return 0;
81
82     if (!params_to_domparams(dh, params))
83         return 0;
84
85     param_priv_key =
86         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_KEY);
87     param_pub_key =
88         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PUB_KEY);
89
90     /*
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.
95      */
96     if (param_pub_key == NULL)
97         return 0;
98
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))
102         goto err;
103
104     if (!DH_set0_key(dh, pub_key, priv_key))
105         goto err;
106
107     return 1;
108
109  err:
110     BN_free(priv_key);
111     BN_free(pub_key);
112     return 0;
113 }
114
115 static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
116 {
117     const BIGNUM *priv_key = NULL, *pub_key = NULL;
118
119     if (dh == NULL)
120         return 0;
121     if (!domparams_to_params(dh, tmpl))
122         return 0;
123
124     DH_get0_key(dh, &pub_key, &priv_key);
125     if (priv_key != NULL
126         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv_key))
127         return 0;
128     if (pub_key != NULL
129         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_key))
130         return 0;
131
132     return 1;
133 }
134
135 static void *dh_newdata(void *provctx)
136 {
137     return DH_new();
138 }
139
140 static void dh_freedata(void *keydata)
141 {
142     DH_free(keydata);
143 }
144
145 static int dh_has(void *keydata, int selection)
146 {
147     DH *dh = keydata;
148     int ok = 0;
149
150     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
151         ok = 1;
152
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);
159     return ok;
160 }
161
162 static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
163 {
164     DH *dh = keydata;
165     int ok = 0;
166
167     if (dh == NULL)
168         return 0;
169
170     if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
171         ok = 1;
172
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);
177
178     return ok;
179 }
180
181 static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
182                      void *cbarg)
183 {
184     DH *dh = keydata;
185     OSSL_PARAM_BLD tmpl;
186     OSSL_PARAM *params = NULL;
187     int ok = 1;
188
189     if (dh == NULL)
190         return 0;
191
192     ossl_param_bld_init(&tmpl);
193
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);
198
199     if (!ok
200         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
201         return 0;
202
203     ok = param_cb(params, cbarg);
204     ossl_param_bld_free(params);
205     return ok;
206 }
207
208 static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
209 {
210     DH *dh = key;
211     OSSL_PARAM *p;
212
213     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
214         && !OSSL_PARAM_set_int(p, DH_bits(dh)))
215         return 0;
216     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
217         && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
218         return 0;
219     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
220         && !OSSL_PARAM_set_int(p, DH_size(dh)))
221         return 0;
222     return 1;
223 }
224
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 },
232     { 0, NULL }
233 };