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 "prov/implementations.h"
16 #include "prov/providercommon.h"
17 #include "prov/provider_ctx.h"
18 #include "crypto/dsa.h"
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;
25 static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
27 const OSSL_PARAM *param_p, *param_q, *param_g;
28 BIGNUM *p = NULL, *q = NULL, *g = NULL;
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);
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)))
42 if (!DSA_set0_pqg(dsa, p, q, g))
54 static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
56 const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
61 DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
63 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
66 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
69 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
75 static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
77 const OSSL_PARAM *param_priv_key, *param_pub_key;
78 BIGNUM *priv_key = NULL, *pub_key = NULL;
83 if (!params_to_domparams(dsa, params))
87 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PRIV_KEY);
89 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PUB_KEY);
92 * DSA documentation says that a public key must be present if a private key
95 if (param_priv_key != NULL && param_pub_key == NULL)
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)))
104 if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
115 static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
117 const BIGNUM *priv_key = NULL, *pub_key = NULL;
121 if (!domparams_to_params(dsa, tmpl))
124 DSA_get0_key(dsa, &pub_key, &priv_key);
126 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY, priv_key))
129 && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY, pub_key))
135 static void *dsa_importdomparams(void *provctx, const OSSL_PARAM params[])
138 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
140 if ((dsa = dsa_new(libctx)) == NULL
141 || !params_to_domparams(dsa, params)) {
148 static int dsa_exportdomparams(void *domparams,
149 OSSL_CALLBACK *param_cb, void *cbarg)
151 DSA *dsa = domparams;
153 OSSL_PARAM *params = NULL;
156 ossl_param_bld_init(&tmpl);
158 || !domparams_to_params(dsa, &tmpl)
159 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
161 ret = param_cb(params, cbarg);
162 ossl_param_bld_free(params);
166 static void *dsa_importkey(void *provctx, const OSSL_PARAM params[])
169 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
171 if ((dsa = dsa_new(libctx)) == NULL
172 || !params_to_key(dsa, params)) {
179 static int dsa_exportkey(void *key, OSSL_CALLBACK *param_cb, void *cbarg)
183 OSSL_PARAM *params = NULL;
186 ossl_param_bld_init(&tmpl);
188 || !key_to_params(dsa, &tmpl)
189 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
191 ret = param_cb(params, cbarg);
192 ossl_param_bld_free(params);
196 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
198 * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
199 * implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
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 },