5a53a439d916d884bb79eea03bdc3afb08354d25
[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 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/bn.h>
13 #include <openssl/params.h>
14 #include "internal/param_build.h"
15 #include "prov/implementations.h"
16 #include "prov/providercommon.h"
17 #include "prov/provider_ctx.h"
18 #include "crypto/dsa.h"
19
20 static OSSL_OP_keymgmt_importdomparams_fn dsa_importdomparams;
21 static OSSL_OP_keymgmt_exportdomparams_fn dsa_exportdomparams;
22 static OSSL_OP_keymgmt_get_domparam_params_fn dsa_get_domparam_params;
23 static OSSL_OP_keymgmt_importkey_fn dsa_importkey;
24 static OSSL_OP_keymgmt_exportkey_fn dsa_exportkey;
25 static OSSL_OP_keymgmt_get_key_params_fn dsa_get_key_params;
26
27 #define DSA_DEFAULT_MD "SHA256"
28
29 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
30 {
31     const OSSL_PARAM *param_p, *param_q, *param_g;
32     BIGNUM *p = NULL, *q = NULL, *g = NULL;
33
34     if (dsa == NULL)
35         return 0;
36
37     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
38     param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
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_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
43         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
44         goto err;
45
46     if (!DSA_set0_pqg(dsa, p, q, g))
47         goto err;
48
49     return 1;
50
51  err:
52     BN_free(p);
53     BN_free(q);
54     BN_free(g);
55     return 0;
56 }
57
58 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
59 {
60     const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
61
62     if (dsa == NULL)
63         return 0;
64
65     DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
66     if (dsa_p != NULL
67         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
68         return 0;
69     if (dsa_q != NULL
70         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
71         return 0;
72     if (dsa_g != NULL
73         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
74         return 0;
75
76     return 1;
77 }
78
79 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
80 {
81     const OSSL_PARAM *param_priv_key, *param_pub_key;
82     BIGNUM *priv_key = NULL, *pub_key = NULL;
83
84     if (dsa == NULL)
85         return 0;
86
87     if (!params_to_domparams(dsa, params))
88         return 0;
89
90     param_priv_key =
91         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PRIV_KEY);
92     param_pub_key =
93         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PUB_KEY);
94
95     /*
96      * DSA documentation says that a public key must be present if a private key
97      * is.
98      */
99     if (param_priv_key != NULL && 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         || (param_pub_key != NULL
105             && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
106         goto err;
107
108     if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
109         goto err;
110
111     return 1;
112
113  err:
114     BN_free(priv_key);
115     BN_free(pub_key);
116     return 0;
117 }
118
119 static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
120 {
121     const BIGNUM *priv_key = NULL, *pub_key = NULL;
122
123     if (dsa == NULL)
124         return 0;
125     if (!domparams_to_params(dsa, tmpl))
126         return 0;
127
128     DSA_get0_key(dsa, &pub_key, &priv_key);
129     if (priv_key != NULL
130         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY, priv_key))
131         return 0;
132     if (pub_key != NULL
133         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY, pub_key))
134         return 0;
135
136     return 1;
137 }
138
139 static void *dsa_importdomparams(void *provctx, const OSSL_PARAM params[])
140 {
141     DSA *dsa;
142
143     if ((dsa = DSA_new()) == NULL
144         || !params_to_domparams(dsa, params)) {
145         DSA_free(dsa);
146         dsa = NULL;
147     }
148     return dsa;
149 }
150
151 static int dsa_exportdomparams(void *domparams,
152                                OSSL_CALLBACK *param_cb, void *cbarg)
153 {
154     DSA *dsa = domparams;
155     OSSL_PARAM_BLD tmpl;
156     OSSL_PARAM *params = NULL;
157     int ret;
158
159     ossl_param_bld_init(&tmpl);
160     if (dsa == NULL
161         || !domparams_to_params(dsa, &tmpl)
162         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
163         return 0;
164     ret = param_cb(params, cbarg);
165     ossl_param_bld_free(params);
166     return ret;
167 }
168
169 static void *dsa_importkey(void *provctx, const OSSL_PARAM params[])
170 {
171     DSA *dsa;
172
173     if ((dsa = DSA_new()) == NULL
174         || !params_to_key(dsa, params)) {
175         DSA_free(dsa);
176         dsa = NULL;
177     }
178     return dsa;
179 }
180
181 static int dsa_exportkey(void *key, OSSL_CALLBACK *param_cb, void *cbarg)
182 {
183     DSA *dsa = key;
184     OSSL_PARAM_BLD tmpl;
185     OSSL_PARAM *params = NULL;
186     int ret;
187
188     ossl_param_bld_init(&tmpl);
189     if (dsa == NULL
190         || !key_to_params(dsa, &tmpl)
191         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
192         return 0;
193     ret = param_cb(params, cbarg);
194     ossl_param_bld_free(params);
195     return ret;
196 }
197
198 /*
199  * Same function for domain parameters and for keys.
200  * "dpk" = "domain parameters & keys"
201  */
202 static ossl_inline int dsa_get_dpk_params(void *key, OSSL_PARAM params[])
203 {
204     DSA *dsa = key;
205     OSSL_PARAM *p;
206
207     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
208         && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
209         return 0;
210     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
211         && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
212         return 0;
213     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
214         && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
215         return 0;
216     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
217         && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
218         return 0;
219     return 1;
220 }
221
222 /*
223  * We have wrapper functions to make sure we get signatures right, see
224  * the forward declarations at the beginning of this file.
225  */
226 static int dsa_get_domparam_params(void *domparams, OSSL_PARAM params[])
227 {
228     return dsa_get_dpk_params(domparams, params);
229 }
230
231 static int dsa_get_key_params(void *key, OSSL_PARAM params[])
232 {
233     return dsa_get_dpk_params(key, params);
234 }
235
236 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
237     { OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dsa_importdomparams },
238     { OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS, (void (*)(void))dsa_exportdomparams },
239     { OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DSA_free },
240     { OSSL_FUNC_KEYMGMT_GET_DOMPARAM_PARAMS,
241       (void (*) (void))dsa_get_domparam_params },
242     { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dsa_importkey },
243     { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))dsa_exportkey },
244     { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DSA_free },
245     { OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS,  (void (*) (void))dsa_get_key_params },
246     { 0, NULL }
247 };