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_get_params_fn dsa_get_params
;
24 static OSSL_OP_keymgmt_gettable_params_fn dsa_gettable_params
;
25 static OSSL_OP_keymgmt_has_fn dsa_has
;
26 static OSSL_OP_keymgmt_import_fn dsa_import
;
27 static OSSL_OP_keymgmt_import_types_fn dsa_import_types
;
28 static OSSL_OP_keymgmt_export_fn dsa_export
;
29 static OSSL_OP_keymgmt_export_types_fn dsa_export_types
;
31 #define DSA_DEFAULT_MD "SHA256"
32 #define DSA_POSSIBLE_SELECTIONS \
33 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
35 static int params_to_domparams(DSA
*dsa
, const OSSL_PARAM params
[])
37 const OSSL_PARAM
*param_p
, *param_q
, *param_g
;
38 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
43 param_p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_P
);
44 param_q
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_Q
);
45 param_g
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_G
);
47 if ((param_p
!= NULL
&& !OSSL_PARAM_get_BN(param_p
, &p
))
48 || (param_q
!= NULL
&& !OSSL_PARAM_get_BN(param_q
, &q
))
49 || (param_g
!= NULL
&& !OSSL_PARAM_get_BN(param_g
, &g
)))
52 if (!DSA_set0_pqg(dsa
, p
, q
, g
))
64 static int domparams_to_params(DSA
*dsa
, OSSL_PARAM_BLD
*tmpl
)
66 const BIGNUM
*dsa_p
= NULL
, *dsa_q
= NULL
, *dsa_g
= NULL
;
71 DSA_get0_pqg(dsa
, &dsa_p
, &dsa_q
, &dsa_g
);
73 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_P
, dsa_p
))
76 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_Q
, dsa_q
))
79 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_G
, dsa_g
))
85 static int params_to_key(DSA
*dsa
, const OSSL_PARAM params
[])
87 const OSSL_PARAM
*param_priv_key
, *param_pub_key
;
88 BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
93 if (!params_to_domparams(dsa
, params
))
97 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DSA_PRIV_KEY
);
99 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DSA_PUB_KEY
);
102 * DSA documentation says that a public key must be present if a private key
105 if (param_priv_key
!= NULL
&& param_pub_key
== NULL
)
108 if ((param_priv_key
!= NULL
109 && !OSSL_PARAM_get_BN(param_priv_key
, &priv_key
))
110 || (param_pub_key
!= NULL
111 && !OSSL_PARAM_get_BN(param_pub_key
, &pub_key
)))
114 if (pub_key
!= NULL
&& !DSA_set0_key(dsa
, pub_key
, priv_key
))
125 static int key_to_params(DSA
*dsa
, OSSL_PARAM_BLD
*tmpl
)
127 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
131 if (!domparams_to_params(dsa
, tmpl
))
134 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
136 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_DSA_PRIV_KEY
, priv_key
))
139 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_DSA_PUB_KEY
, pub_key
))
145 static void *dsa_newdata(void *provctx
)
150 static void dsa_freedata(void *keydata
)
155 static int dsa_has(void *keydata
, int selection
)
160 if ((selection
& DSA_POSSIBLE_SELECTIONS
) != 0)
163 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
164 ok
= ok
&& (DSA_get0_pub_key(dsa
) != NULL
);
165 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
166 ok
= ok
&& (DSA_get0_priv_key(dsa
) != NULL
);
167 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
168 ok
= ok
&& (DSA_get0_p(dsa
) != NULL
&& DSA_get0_g(dsa
) != NULL
);
172 static int dsa_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
180 if ((selection
& DSA_POSSIBLE_SELECTIONS
) != 0)
183 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
184 ok
= ok
&& params_to_domparams(dsa
, params
);
185 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
186 ok
= ok
&& params_to_key(dsa
, params
);
191 static int dsa_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
196 OSSL_PARAM
*params
= NULL
;
202 ossl_param_bld_init(&tmpl
);
204 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
205 ok
= ok
&& domparams_to_params(dsa
, &tmpl
);
206 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
207 ok
= ok
&& key_to_params(dsa
, &tmpl
);
210 || (params
= ossl_param_bld_to_param(&tmpl
)) == NULL
)
213 ok
= param_cb(params
, cbarg
);
214 ossl_param_bld_free(params
);
218 /* IMEXPORT = IMPORT + EXPORT */
220 # define DSA_IMEXPORTABLE_PARAMETERS \
221 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
222 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
223 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
224 # define DSA_IMEXPORTABLE_PUBLIC_KEY \
225 OSSL_PARAM_BN(OSSL_PKEY_PARAM_DSA_PUB_KEY, NULL, 0)
226 # define DSA_IMEXPORTABLE_PRIVATE_KEY \
227 OSSL_PARAM_BN(OSSL_PKEY_PARAM_DSA_PRIV_KEY, NULL, 0)
228 static const OSSL_PARAM dsa_all_types
[] = {
229 DSA_IMEXPORTABLE_PARAMETERS
,
230 DSA_IMEXPORTABLE_PUBLIC_KEY
,
231 DSA_IMEXPORTABLE_PRIVATE_KEY
,
234 static const OSSL_PARAM dsa_parameter_types
[] = {
235 DSA_IMEXPORTABLE_PARAMETERS
,
238 static const OSSL_PARAM dsa_key_types
[] = {
239 DSA_IMEXPORTABLE_PUBLIC_KEY
,
240 DSA_IMEXPORTABLE_PRIVATE_KEY
,
243 static const OSSL_PARAM
*dsa_types
[] = {
244 NULL
, /* Index 0 = none of them */
245 dsa_parameter_types
, /* Index 1 = parameter types */
246 dsa_key_types
, /* Index 2 = key types */
247 dsa_all_types
/* Index 3 = 1 + 2 */
250 static const OSSL_PARAM
*dsa_imexport_types(int selection
)
254 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
256 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
258 return dsa_types
[type_select
];
261 static const OSSL_PARAM
*dsa_import_types(int selection
)
263 return dsa_imexport_types(selection
);
266 static const OSSL_PARAM
*dsa_export_types(int selection
)
268 return dsa_imexport_types(selection
);
271 static ossl_inline
int dsa_get_params(void *key
, OSSL_PARAM params
[])
276 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
277 && !OSSL_PARAM_set_int(p
, DSA_bits(dsa
)))
279 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
280 && !OSSL_PARAM_set_int(p
, DSA_security_bits(dsa
)))
282 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
283 && !OSSL_PARAM_set_int(p
, DSA_size(dsa
)))
285 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
286 && !OSSL_PARAM_set_utf8_string(p
, DSA_DEFAULT_MD
))
291 static const OSSL_PARAM dsa_params
[] = {
292 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
293 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
294 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
295 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
299 static const OSSL_PARAM
*dsa_gettable_params(void)
304 const OSSL_DISPATCH dsa_keymgmt_functions
[] = {
305 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))dsa_newdata
},
306 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))dsa_freedata
},
307 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))dsa_get_params
},
308 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))dsa_gettable_params
},
309 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))dsa_has
},
310 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))dsa_import
},
311 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))dsa_import_types
},
312 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))dsa_export
},
313 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))dsa_export_types
},