]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/keymgmt/dsa_kmgmt.c
Add dsa signature alg to fips provider
[thirdparty/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_importkey_fn dsa_importkey;
23 static OSSL_OP_keymgmt_exportkey_fn dsa_exportkey;
24
25 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
26 {
27 const OSSL_PARAM *param_p, *param_q, *param_g;
28 BIGNUM *p = NULL, *q = NULL, *g = NULL;
29
30 if (dsa == NULL)
31 return 0;
32
33 param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
34 param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
35 param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
36
37 if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
38 || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
39 || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
40 goto err;
41
42 if (!DSA_set0_pqg(dsa, p, q, g))
43 goto err;
44
45 return 1;
46
47 err:
48 BN_free(p);
49 BN_free(q);
50 BN_free(g);
51 return 0;
52 }
53
54 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
55 {
56 const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
57
58 if (dsa == NULL)
59 return 0;
60
61 DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
62 if (dsa_p != NULL
63 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
64 return 0;
65 if (dsa_q != NULL
66 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
67 return 0;
68 if (dsa_g != NULL
69 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
70 return 0;
71
72 return 1;
73 }
74
75 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
76 {
77 const OSSL_PARAM *param_priv_key, *param_pub_key;
78 BIGNUM *priv_key = NULL, *pub_key = NULL;
79
80 if (dsa == NULL)
81 return 0;
82
83 if (!params_to_domparams(dsa, params))
84 return 0;
85
86 param_priv_key =
87 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PRIV_KEY);
88 param_pub_key =
89 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PUB_KEY);
90
91 /*
92 * DSA documentation says that a public key must be present if a private key
93 * is.
94 */
95 if (param_priv_key != NULL && param_pub_key == NULL)
96 return 0;
97
98 if ((param_priv_key != NULL
99 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
100 || (param_pub_key != NULL
101 && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
102 goto err;
103
104 if (pub_key != NULL && !DSA_set0_key(dsa, 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(DSA *dsa, OSSL_PARAM_BLD *tmpl)
116 {
117 const BIGNUM *priv_key = NULL, *pub_key = NULL;
118
119 if (dsa == NULL)
120 return 0;
121 if (!domparams_to_params(dsa, tmpl))
122 return 0;
123
124 DSA_get0_key(dsa, &pub_key, &priv_key);
125 if (priv_key != NULL
126 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY, priv_key))
127 return 0;
128 if (pub_key != NULL
129 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY, pub_key))
130 return 0;
131
132 return 1;
133 }
134
135 static void *dsa_importdomparams(void *provctx, const OSSL_PARAM params[])
136 {
137 DSA *dsa;
138 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
139
140 if ((dsa = dsa_new(libctx)) == NULL
141 || !params_to_domparams(dsa, params)) {
142 DSA_free(dsa);
143 dsa = NULL;
144 }
145 return dsa;
146 }
147
148 static int dsa_exportdomparams(void *domparams,
149 OSSL_CALLBACK *param_cb, void *cbarg)
150 {
151 DSA *dsa = domparams;
152 OSSL_PARAM_BLD tmpl;
153 OSSL_PARAM *params = NULL;
154 int ret;
155
156 ossl_param_bld_init(&tmpl);
157 if (dsa == NULL
158 || !domparams_to_params(dsa, &tmpl)
159 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
160 return 0;
161 ret = param_cb(params, cbarg);
162 ossl_param_bld_free(params);
163 return ret;
164 }
165
166 static void *dsa_importkey(void *provctx, const OSSL_PARAM params[])
167 {
168 DSA *dsa;
169 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
170
171 if ((dsa = dsa_new(libctx)) == 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 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
197 /*
198 * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
199 * implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
200 */
201 { OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dsa_importdomparams },
202 { OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS, (void (*)(void))dsa_exportdomparams },
203 { OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DSA_free },
204 { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dsa_importkey },
205 { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))dsa_exportkey },
206 { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DSA_free },
207 { 0, NULL }
208 };