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_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
;
27 static int params_to_domparams(DSA
*dsa
, const OSSL_PARAM params
[])
29 const OSSL_PARAM
*param_p
, *param_q
, *param_g
;
30 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
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
);
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
)))
44 if (!DSA_set0_pqg(dsa
, p
, q
, g
))
56 static int domparams_to_params(DSA
*dsa
, OSSL_PARAM_BLD
*tmpl
)
58 const BIGNUM
*dsa_p
= NULL
, *dsa_q
= NULL
, *dsa_g
= NULL
;
63 DSA_get0_pqg(dsa
, &dsa_p
, &dsa_q
, &dsa_g
);
65 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_P
, dsa_p
))
68 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_Q
, dsa_q
))
71 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_G
, dsa_g
))
77 static int params_to_key(DSA
*dsa
, const OSSL_PARAM params
[])
79 const OSSL_PARAM
*param_priv_key
, *param_pub_key
;
80 BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
85 if (!params_to_domparams(dsa
, params
))
89 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DSA_PRIV_KEY
);
91 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DSA_PUB_KEY
);
94 * DSA documentation says that a public key must be present if a private key
97 if (param_priv_key
!= NULL
&& param_pub_key
== NULL
)
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
)))
106 if (pub_key
!= NULL
&& !DSA_set0_key(dsa
, pub_key
, priv_key
))
117 static int key_to_params(DSA
*dsa
, OSSL_PARAM_BLD
*tmpl
)
119 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
123 if (!domparams_to_params(dsa
, tmpl
))
126 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
128 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_DSA_PRIV_KEY
, priv_key
))
131 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_DSA_PUB_KEY
, pub_key
))
137 static void *dsa_importdomparams(void *provctx
, const OSSL_PARAM params
[])
141 if ((dsa
= DSA_new()) == NULL
142 || !params_to_domparams(dsa
, params
)) {
149 static int dsa_exportdomparams(void *domparams
,
150 OSSL_CALLBACK
*param_cb
, void *cbarg
)
152 DSA
*dsa
= domparams
;
154 OSSL_PARAM
*params
= NULL
;
157 ossl_param_bld_init(&tmpl
);
159 || !domparams_to_params(dsa
, &tmpl
)
160 || (params
= ossl_param_bld_to_param(&tmpl
)) == NULL
)
162 ret
= param_cb(params
, cbarg
);
163 ossl_param_bld_free(params
);
167 static void *dsa_importkey(void *provctx
, const OSSL_PARAM params
[])
171 if ((dsa
= DSA_new()) == 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
);
197 * Same function for domain parameters and for keys.
198 * "dpk" = "domain parameters & keys"
200 static ossl_inline
int dsa_get_dpk_params(void *key
, OSSL_PARAM params
[])
205 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
206 && !OSSL_PARAM_set_int(p
, DSA_bits(dsa
)))
208 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
209 && !OSSL_PARAM_set_int(p
, DSA_security_bits(dsa
)))
211 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
212 && !OSSL_PARAM_set_int(p
, DSA_size(dsa
)))
218 * We have wrapper functions to make sure we get signatures right, see
219 * the forward declarations at the beginning of this file.
221 static int dsa_get_domparam_params(void *domparams
, OSSL_PARAM params
[])
223 return dsa_get_dpk_params(domparams
, params
);
226 static int dsa_get_key_params(void *key
, OSSL_PARAM params
[])
228 return dsa_get_dpk_params(key
, params
);
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
},