2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
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"
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;
37 #define DSA_DEFAULT_MD "SHA256"
38 #define DSA_POSSIBLE_SELECTIONS \
39 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
41 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
43 const OSSL_PARAM *param_p, *param_q, *param_g;
44 BIGNUM *p = NULL, *q = NULL, *g = NULL;
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);
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)))
58 if (!DSA_set0_pqg(dsa, p, q, g))
70 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
72 const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
77 DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
79 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
82 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
85 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
91 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
93 const OSSL_PARAM *param_priv_key, *param_pub_key;
94 BIGNUM *priv_key = NULL, *pub_key = NULL;
99 if (!params_to_domparams(dsa, params))
103 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
105 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
108 * DSA documentation says that a public key must be present if a private key
111 if (param_priv_key != NULL && param_pub_key == NULL)
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)))
120 if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
126 BN_clear_free(priv_key);
131 static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
133 const BIGNUM *priv_key = NULL, *pub_key = NULL;
137 if (!domparams_to_params(dsa, tmpl))
140 DSA_get0_key(dsa, &pub_key, &priv_key);
142 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
145 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
151 static void *dsa_newdata(void *provctx)
153 return dsa_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx));
156 static void dsa_freedata(void *keydata)
161 static int dsa_has(void *keydata, int selection)
166 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
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);
178 static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
186 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
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);
197 static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
202 OSSL_PARAM *params = NULL;
208 ossl_param_bld_init(&tmpl);
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);
216 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
219 ok = param_cb(params, cbarg);
220 ossl_param_bld_free(params);
224 /* IMEXPORT = IMPORT + EXPORT */
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,
240 static const OSSL_PARAM dsa_parameter_types[] = {
241 DSA_IMEXPORTABLE_PARAMETERS,
244 static const OSSL_PARAM dsa_key_types[] = {
245 DSA_IMEXPORTABLE_PUBLIC_KEY,
246 DSA_IMEXPORTABLE_PRIVATE_KEY,
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 */
256 static const OSSL_PARAM *dsa_imexport_types(int selection)
260 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
262 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
264 return dsa_types[type_select];
267 static const OSSL_PARAM *dsa_import_types(int selection)
269 return dsa_imexport_types(selection);
272 static const OSSL_PARAM *dsa_export_types(int selection)
274 return dsa_imexport_types(selection);
277 static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
282 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
283 && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
285 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
286 && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
288 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
289 && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
291 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
292 && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
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),
305 static const OSSL_PARAM *dsa_gettable_params(void)
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 },