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