EVP: Clarify the states of an EVP_PKEY
[oweals/openssl.git] / providers / implementations / keymgmt / dsa_kmgmt.c
1 /*
2  * Copyright 2019-2020 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 "prov/implementations.h"
21 #include "prov/providercommon.h"
22 #include "prov/provider_ctx.h"
23 #include "crypto/dsa.h"
24 #include "internal/param_build.h"
25
26 static OSSL_OP_keymgmt_new_fn dsa_newdata;
27 static OSSL_OP_keymgmt_free_fn dsa_freedata;
28 static OSSL_OP_keymgmt_get_params_fn dsa_get_params;
29 static OSSL_OP_keymgmt_gettable_params_fn dsa_gettable_params;
30 static OSSL_OP_keymgmt_has_fn dsa_has;
31 static OSSL_OP_keymgmt_match_fn dsa_match;
32 static OSSL_OP_keymgmt_validate_fn dsa_validate;
33 static OSSL_OP_keymgmt_import_fn dsa_import;
34 static OSSL_OP_keymgmt_import_types_fn dsa_import_types;
35 static OSSL_OP_keymgmt_export_fn dsa_export;
36 static OSSL_OP_keymgmt_export_types_fn dsa_export_types;
37
38 #define DSA_DEFAULT_MD "SHA256"
39 #define DSA_POSSIBLE_SELECTIONS                 \
40     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
41
42 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
43 {
44     const OSSL_PARAM *param_p, *param_q, *param_g;
45     BIGNUM *p = NULL, *q = NULL, *g = NULL;
46
47     if (dsa == NULL)
48         return 0;
49
50     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
51     param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
52     param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
53
54     if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
55         || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
56         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
57         goto err;
58
59     if (!DSA_set0_pqg(dsa, p, q, g))
60         goto err;
61
62     return 1;
63
64  err:
65     BN_free(p);
66     BN_free(q);
67     BN_free(g);
68     return 0;
69 }
70
71 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
72 {
73     const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
74
75     if (dsa == NULL)
76         return 0;
77
78     DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
79     if (dsa_p != NULL
80         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
81         return 0;
82     if (dsa_q != NULL
83         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
84         return 0;
85     if (dsa_g != NULL
86         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
87         return 0;
88
89     return 1;
90 }
91
92 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
93 {
94     const OSSL_PARAM *param_priv_key, *param_pub_key;
95     BIGNUM *priv_key = NULL, *pub_key = NULL;
96
97     if (dsa == NULL)
98         return 0;
99
100     if (!params_to_domparams(dsa, params))
101         return 0;
102
103     param_priv_key =
104         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
105     param_pub_key =
106         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
107
108     /*
109      * DSA documentation says that a public key must be present if a private key
110      * is.
111      */
112     if (param_priv_key != NULL && param_pub_key == NULL)
113         return 0;
114
115     if ((param_priv_key != NULL
116          && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
117         || (param_pub_key != NULL
118             && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
119         goto err;
120
121     if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
122         goto err;
123
124     return 1;
125
126  err:
127     BN_clear_free(priv_key);
128     BN_free(pub_key);
129     return 0;
130 }
131
132 static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
133 {
134     const BIGNUM *priv_key = NULL, *pub_key = NULL;
135
136     if (dsa == NULL)
137         return 0;
138     if (!domparams_to_params(dsa, tmpl))
139         return 0;
140
141     DSA_get0_key(dsa, &pub_key, &priv_key);
142     if (priv_key != NULL
143         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
144         return 0;
145     if (pub_key != NULL
146         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
147         return 0;
148
149     return 1;
150 }
151
152 static void *dsa_newdata(void *provctx)
153 {
154     return dsa_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx));
155 }
156
157 static void dsa_freedata(void *keydata)
158 {
159     DSA_free(keydata);
160 }
161
162 static int dsa_has(void *keydata, int selection)
163 {
164     DSA *dsa = keydata;
165     int ok = 0;
166
167     if (dsa != NULL) {
168         if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
169             ok = 1;
170
171         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
172             ok = ok && (DSA_get0_pub_key(dsa) != NULL);
173         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
174             ok = ok && (DSA_get0_priv_key(dsa) != NULL);
175         if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
176             ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
177     }
178     return ok;
179 }
180
181 static int dsa_match(const void *keydata1, const void *keydata2, int selection)
182 {
183     const DSA *dsa1 = keydata1;
184     const DSA *dsa2 = keydata2;
185     int ok = 1;
186
187     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
188         ok = ok
189             && BN_cmp(DSA_get0_pub_key(dsa1), DSA_get0_pub_key(dsa2)) == 0;
190     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
191         ok = ok
192             && BN_cmp(DSA_get0_priv_key(dsa1), DSA_get0_priv_key(dsa2)) == 0;
193     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
194         FFC_PARAMS *dsaparams1 = dsa_get0_params((DSA *)dsa1);
195         FFC_PARAMS *dsaparams2 = dsa_get0_params((DSA *)dsa2);
196
197         ok = ok && ffc_params_cmp(dsaparams1, dsaparams2, 1);
198     }
199     return ok;
200 }
201
202 static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
203 {
204     DSA *dsa = keydata;
205     int ok = 0;
206
207     if (dsa == NULL)
208         return 0;
209
210     if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
211         ok = 1;
212
213     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
214         ok = ok && params_to_domparams(dsa, params);
215     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
216         ok = ok && params_to_key(dsa, params);
217
218     return ok;
219 }
220
221 static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
222                       void *cbarg)
223 {
224     DSA *dsa = keydata;
225     OSSL_PARAM_BLD tmpl;
226     OSSL_PARAM *params = NULL;
227     int ok = 1;
228
229     if (dsa == NULL)
230         return 0;
231
232     ossl_param_bld_init(&tmpl);
233
234     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
235         ok = ok && domparams_to_params(dsa, &tmpl);
236     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
237         ok = ok && key_to_params(dsa, &tmpl);
238
239     if (!ok
240         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
241         return 0;
242
243     ok = param_cb(params, cbarg);
244     ossl_param_bld_free(params);
245     return ok;
246 }
247
248 /* IMEXPORT = IMPORT + EXPORT */
249
250 # define DSA_IMEXPORTABLE_PARAMETERS                    \
251     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0),      \
252     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0),      \
253     OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
254 # define DSA_IMEXPORTABLE_PUBLIC_KEY                    \
255     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
256 # define DSA_IMEXPORTABLE_PRIVATE_KEY                   \
257     OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
258 static const OSSL_PARAM dsa_all_types[] = {
259     DSA_IMEXPORTABLE_PARAMETERS,
260     DSA_IMEXPORTABLE_PUBLIC_KEY,
261     DSA_IMEXPORTABLE_PRIVATE_KEY,
262     OSSL_PARAM_END
263 };
264 static const OSSL_PARAM dsa_parameter_types[] = {
265     DSA_IMEXPORTABLE_PARAMETERS,
266     OSSL_PARAM_END
267 };
268 static const OSSL_PARAM dsa_key_types[] = {
269     DSA_IMEXPORTABLE_PUBLIC_KEY,
270     DSA_IMEXPORTABLE_PRIVATE_KEY,
271     OSSL_PARAM_END
272 };
273 static const OSSL_PARAM *dsa_types[] = {
274     NULL,                        /* Index 0 = none of them */
275     dsa_parameter_types,          /* Index 1 = parameter types */
276     dsa_key_types,                /* Index 2 = key types */
277     dsa_all_types                 /* Index 3 = 1 + 2 */
278 };
279
280 static const OSSL_PARAM *dsa_imexport_types(int selection)
281 {
282     int type_select = 0;
283
284     if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
285         type_select += 1;
286     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
287         type_select += 2;
288     return dsa_types[type_select];
289 }
290
291 static const OSSL_PARAM *dsa_import_types(int selection)
292 {
293     return dsa_imexport_types(selection);
294 }
295
296 static const OSSL_PARAM *dsa_export_types(int selection)
297 {
298     return dsa_imexport_types(selection);
299 }
300
301 static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
302 {
303     DSA *dsa = key;
304     OSSL_PARAM *p;
305
306     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
307         && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
308         return 0;
309     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
310         && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
311         return 0;
312     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
313         && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
314         return 0;
315     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
316         && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
317         return 0;
318     return 1;
319 }
320
321 static const OSSL_PARAM dsa_params[] = {
322     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
323     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
324     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
325     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
326     OSSL_PARAM_END
327 };
328
329 static const OSSL_PARAM *dsa_gettable_params(void)
330 {
331     return dsa_params;
332 }
333
334 static int dsa_validate_domparams(DSA *dsa)
335 {
336     int status = 0;
337
338     return dsa_check_params(dsa, &status);
339 }
340
341 static int dsa_validate_public(DSA *dsa)
342 {
343     int status = 0;
344     const BIGNUM *pub_key = NULL;
345
346     DSA_get0_key(dsa, &pub_key, NULL);
347     return dsa_check_pub_key(dsa, pub_key, &status);
348 }
349
350 static int dsa_validate_private(DSA *dsa)
351 {
352     int status = 0;
353     const BIGNUM *priv_key = NULL;
354
355     DSA_get0_key(dsa, NULL, &priv_key);
356     return dsa_check_priv_key(dsa, priv_key, &status);
357 }
358
359 static int dsa_validate(void *keydata, int selection)
360 {
361     DSA *dsa = keydata;
362     int ok = 0;
363
364     if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
365         ok = 1;
366
367     if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
368         ok = ok && dsa_validate_domparams(dsa);
369
370     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
371         ok = ok && dsa_validate_public(dsa);
372
373     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
374         ok = ok && dsa_validate_private(dsa);
375
376     /* If the whole key is selected, we do a pairwise validation */
377     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
378         == OSSL_KEYMGMT_SELECT_KEYPAIR)
379         ok = ok && dsa_check_pairwise(dsa);
380     return ok;
381 }
382
383 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
384     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
385     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
386     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
387     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
388     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
389     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match },
390     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate },
391     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
392     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
393     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
394     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },
395     { 0, NULL }
396 };