Deprecate the low level DSA functions.
[openssl.git] / providers / implementations / keymgmt / dsa_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  * DSA 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/dsa.h"
22 #include "prov/implementations.h"
23 #include "prov/providercommon.h"
24 #include "prov/provider_ctx.h"
25 #include "crypto/dsa.h"
26
27 static OSSL_OP_keymgmt_new_fn dsa_newdata;
28 static OSSL_OP_keymgmt_free_fn dsa_freedata;
29 static OSSL_OP_keymgmt_get_params_fn dsa_get_params;
30 static OSSL_OP_keymgmt_gettable_params_fn dsa_gettable_params;
31 static OSSL_OP_keymgmt_has_fn dsa_has;
32 static OSSL_OP_keymgmt_import_fn dsa_import;
33 static OSSL_OP_keymgmt_import_types_fn dsa_import_types;
34 static OSSL_OP_keymgmt_export_fn dsa_export;
35 static OSSL_OP_keymgmt_export_types_fn dsa_export_types;
36
37 #define DSA_DEFAULT_MD "SHA256"
38 #define DSA_POSSIBLE_SELECTIONS                 \
39     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
40
41 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
42 {
43     const OSSL_PARAM *param_p, *param_q, *param_g;
44     BIGNUM *p = NULL, *q = NULL, *g = NULL;
45
46     if (dsa == NULL)
47         return 0;
48
49     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
50     param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
51     param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
52
53     if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
54         || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
55         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
56         goto err;
57
58     if (!DSA_set0_pqg(dsa, p, q, g))
59         goto err;
60
61     return 1;
62
63  err:
64     BN_free(p);
65     BN_free(q);
66     BN_free(g);
67     return 0;
68 }
69
70 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
71 {
72     const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
73
74     if (dsa == NULL)
75         return 0;
76
77     DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
78     if (dsa_p != NULL
79         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
80         return 0;
81     if (dsa_q != NULL
82         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
83         return 0;
84     if (dsa_g != NULL
85         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
86         return 0;
87
88     return 1;
89 }
90
91 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
92 {
93     const OSSL_PARAM *param_priv_key, *param_pub_key;
94     BIGNUM *priv_key = NULL, *pub_key = NULL;
95
96     if (dsa == NULL)
97         return 0;
98
99     if (!params_to_domparams(dsa, params))
100         return 0;
101
102     param_priv_key =
103         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
104     param_pub_key =
105         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
106
107     /*
108      * DSA documentation says that a public key must be present if a private key
109      * is.
110      */
111     if (param_priv_key != NULL && param_pub_key == NULL)
112         return 0;
113
114     if ((param_priv_key != NULL
115          && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
116         || (param_pub_key != NULL
117             && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
118         goto err;
119
120     if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
121         goto err;
122
123     return 1;
124
125  err:
126     BN_free(priv_key);
127     BN_free(pub_key);
128     return 0;
129 }
130
131 static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
132 {
133     const BIGNUM *priv_key = NULL, *pub_key = NULL;
134
135     if (dsa == NULL)
136         return 0;
137     if (!domparams_to_params(dsa, tmpl))
138         return 0;
139
140     DSA_get0_key(dsa, &pub_key, &priv_key);
141     if (priv_key != NULL
142         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
143         return 0;
144     if (pub_key != NULL
145         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
146         return 0;
147
148     return 1;
149 }
150
151 static void *dsa_newdata(void *provctx)
152 {
153     return DSA_new();
154 }
155
156 static void dsa_freedata(void *keydata)
157 {
158     DSA_free(keydata);
159 }
160
161 static int dsa_has(void *keydata, int selection)
162 {
163     DSA *dsa = keydata;
164     int ok = 0;
165
166     if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
167         ok = 1;
168
169     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
170         ok = ok && (DSA_get0_pub_key(dsa) != NULL);
171     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
172         ok = ok && (DSA_get0_priv_key(dsa) != NULL);
173     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
174         ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
175     return ok;
176 }
177
178 static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
179 {
180     DSA *dsa = keydata;
181     int ok = 0;
182
183     if (dsa == NULL)
184         return 0;
185
186     if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
187         ok = 1;
188
189     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
190         ok = ok && params_to_domparams(dsa, params);
191     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
192         ok = ok && params_to_key(dsa, params);
193
194     return ok;
195 }
196
197 static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
198                       void *cbarg)
199 {
200     DSA *dsa = keydata;
201     OSSL_PARAM_BLD tmpl;
202     OSSL_PARAM *params = NULL;
203     int ok = 1;
204
205     if (dsa == NULL)
206         return 0;
207
208     ossl_param_bld_init(&tmpl);
209
210     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
211         ok = ok && domparams_to_params(dsa, &tmpl);
212     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
213         ok = ok && key_to_params(dsa, &tmpl);
214
215     if (!ok
216         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
217         return 0;
218
219     ok = param_cb(params, cbarg);
220     ossl_param_bld_free(params);
221     return ok;
222 }
223
224 /* IMEXPORT = IMPORT + EXPORT */
225
226 # define DSA_IMEXPORTABLE_PARAMETERS                    \
227     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0),      \
228     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0),      \
229     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
230 # define DSA_IMEXPORTABLE_PUBLIC_KEY                    \
231     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
232 # define DSA_IMEXPORTABLE_PRIVATE_KEY                   \
233     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
234 static const OSSL_PARAM dsa_all_types[] = {
235     DSA_IMEXPORTABLE_PARAMETERS,
236     DSA_IMEXPORTABLE_PUBLIC_KEY,
237     DSA_IMEXPORTABLE_PRIVATE_KEY,
238     OSSL_PARAM_END
239 };
240 static const OSSL_PARAM dsa_parameter_types[] = {
241     DSA_IMEXPORTABLE_PARAMETERS,
242     OSSL_PARAM_END
243 };
244 static const OSSL_PARAM dsa_key_types[] = {
245     DSA_IMEXPORTABLE_PUBLIC_KEY,
246     DSA_IMEXPORTABLE_PRIVATE_KEY,
247     OSSL_PARAM_END
248 };
249 static const OSSL_PARAM *dsa_types[] = {
250     NULL,                        /* Index 0 = none of them */
251     dsa_parameter_types,          /* Index 1 = parameter types */
252     dsa_key_types,                /* Index 2 = key types */
253     dsa_all_types                 /* Index 3 = 1 + 2 */
254 };
255
256 static const OSSL_PARAM *dsa_imexport_types(int selection)
257 {
258     int type_select = 0;
259
260     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
261         type_select += 1;
262     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
263         type_select += 2;
264     return dsa_types[type_select];
265 }
266
267 static const OSSL_PARAM *dsa_import_types(int selection)
268 {
269     return dsa_imexport_types(selection);
270 }
271
272 static const OSSL_PARAM *dsa_export_types(int selection)
273 {
274     return dsa_imexport_types(selection);
275 }
276
277 static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
278 {
279     DSA *dsa = key;
280     OSSL_PARAM *p;
281
282     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
283         && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
284         return 0;
285     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
286         && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
287         return 0;
288     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
289         && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
290         return 0;
291     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
292         && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
293         return 0;
294     return 1;
295 }
296
297 static const OSSL_PARAM dsa_params[] = {
298     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
299     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
300     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
301     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
302     OSSL_PARAM_END
303 };
304
305 static const OSSL_PARAM *dsa_gettable_params(void)
306 {
307     return dsa_params;
308 }
309
310 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
311     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
312     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
313     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
314     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
315     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
316     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
317     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
318     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
319     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },
320     { 0, NULL }
321 };