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
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 "crypto/dsa.h"
16 #include "prov/implementations.h"
17 #include "prov/providercommon.h"
18 #include "prov/provider_ctx.h"
19 #include "crypto/dsa.h"
21 static OSSL_OP_keymgmt_new_fn dsa_newdata;
22 static OSSL_OP_keymgmt_free_fn dsa_freedata;
23 static OSSL_OP_keymgmt_has_fn dsa_has;
24 static OSSL_OP_keymgmt_import_fn dsa_import;
25 static OSSL_OP_keymgmt_export_fn dsa_export;
26 static OSSL_OP_keymgmt_get_params_fn dsa_get_params;
28 #define DSA_DEFAULT_MD "SHA256"
29 #define DSA_POSSIBLE_SELECTIONS \
30 (OSSL_KEYMGMT_SELECT_KEY | OSSL_KEYMGMT_FLAG_DOMAIN_PARAMETERS)
32 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
34 const OSSL_PARAM *param_p, *param_q, *param_g;
35 BIGNUM *p = NULL, *q = NULL, *g = NULL;
40 param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
41 param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
42 param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
44 if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
45 || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
46 || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
49 if (!DSA_set0_pqg(dsa, p, q, g))
61 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
63 const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
68 DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
70 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
73 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
76 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
82 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
84 const OSSL_PARAM *param_priv_key, *param_pub_key;
85 BIGNUM *priv_key = NULL, *pub_key = NULL;
90 if (!params_to_domparams(dsa, params))
94 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PRIV_KEY);
96 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PUB_KEY);
99 * DSA documentation says that a public key must be present if a private key
102 if (param_priv_key != NULL && param_pub_key == NULL)
105 if ((param_priv_key != NULL
106 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
107 || (param_pub_key != NULL
108 && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
111 if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
122 static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
124 const BIGNUM *priv_key = NULL, *pub_key = NULL;
128 if (!domparams_to_params(dsa, tmpl))
131 DSA_get0_key(dsa, &pub_key, &priv_key);
133 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY, priv_key))
136 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY, pub_key))
142 static void *dsa_newdata(void *provctx)
147 static void dsa_freedata(void *keydata)
152 static int dsa_has(void *keydata, int selection)
157 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
160 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
161 ok = ok && (DSA_get0_pub_key(dsa) != NULL);
162 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
163 ok = ok && (DSA_get0_priv_key(dsa) != NULL);
164 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
165 ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
169 static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
177 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
180 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
181 ok = ok && params_to_domparams(dsa, params);
182 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
183 ok = ok && params_to_key(dsa, params);
188 static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
193 OSSL_PARAM *params = NULL;
199 ossl_param_bld_init(&tmpl);
201 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
202 ok = ok && domparams_to_params(dsa, &tmpl);
203 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
204 ok = ok && key_to_params(dsa, &tmpl);
207 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
210 ok = param_cb(params, cbarg);
211 ossl_param_bld_free(params);
215 static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
220 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
221 && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
223 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
224 && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
226 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
227 && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
229 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
230 && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
235 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
236 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
237 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
238 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
239 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
240 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
241 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },