2 * Copyright 2019-2020 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 "prov/implementations.h"
21 #include "prov/providercommon.h"
22 #include "prov/provider_ctx.h"
23 #include "crypto/dsa.h"
24 #include "internal/param_build.h"
26 static OSSL_OP_keymgmt_new_fn dsa_newdata
;
27 static OSSL_OP_keymgmt_free_fn dsa_freedata
;
28 static OSSL_OP_keymgmt_get_params_fn dsa_get_params
;
29 static OSSL_OP_keymgmt_gettable_params_fn dsa_gettable_params
;
30 static OSSL_OP_keymgmt_has_fn dsa_has
;
31 static OSSL_OP_keymgmt_match_fn dsa_match
;
32 static OSSL_OP_keymgmt_validate_fn dsa_validate
;
33 static OSSL_OP_keymgmt_import_fn dsa_import
;
34 static OSSL_OP_keymgmt_import_types_fn dsa_import_types
;
35 static OSSL_OP_keymgmt_export_fn dsa_export
;
36 static OSSL_OP_keymgmt_export_types_fn dsa_export_types
;
38 #define DSA_DEFAULT_MD "SHA256"
39 #define DSA_POSSIBLE_SELECTIONS \
40 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
42 static int params_to_domparams(DSA
*dsa
, const OSSL_PARAM params
[])
44 const OSSL_PARAM
*param_p
, *param_q
, *param_g
;
45 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
50 param_p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_P
);
51 param_q
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_Q
);
52 param_g
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_G
);
54 if ((param_p
!= NULL
&& !OSSL_PARAM_get_BN(param_p
, &p
))
55 || (param_q
!= NULL
&& !OSSL_PARAM_get_BN(param_q
, &q
))
56 || (param_g
!= NULL
&& !OSSL_PARAM_get_BN(param_g
, &g
)))
59 if (!DSA_set0_pqg(dsa
, p
, q
, g
))
71 static int domparams_to_params(DSA
*dsa
, OSSL_PARAM_BLD
*tmpl
)
73 const BIGNUM
*dsa_p
= NULL
, *dsa_q
= NULL
, *dsa_g
= NULL
;
78 DSA_get0_pqg(dsa
, &dsa_p
, &dsa_q
, &dsa_g
);
80 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_P
, dsa_p
))
83 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_Q
, dsa_q
))
86 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_G
, dsa_g
))
92 static int params_to_key(DSA
*dsa
, const OSSL_PARAM params
[])
94 const OSSL_PARAM
*param_priv_key
, *param_pub_key
;
95 BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
100 if (!params_to_domparams(dsa
, params
))
104 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
);
106 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
);
109 * DSA documentation says that a public key must be present if a private key
112 if (param_priv_key
!= NULL
&& param_pub_key
== NULL
)
115 if ((param_priv_key
!= NULL
116 && !OSSL_PARAM_get_BN(param_priv_key
, &priv_key
))
117 || (param_pub_key
!= NULL
118 && !OSSL_PARAM_get_BN(param_pub_key
, &pub_key
)))
121 if (pub_key
!= NULL
&& !DSA_set0_key(dsa
, pub_key
, priv_key
))
127 BN_clear_free(priv_key
);
132 static int key_to_params(DSA
*dsa
, OSSL_PARAM_BLD
*tmpl
)
134 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
138 if (!domparams_to_params(dsa
, tmpl
))
141 DSA_get0_key(dsa
, &pub_key
, &priv_key
);
143 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_PRIV_KEY
, priv_key
))
146 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_PUB_KEY
, pub_key
))
152 static void *dsa_newdata(void *provctx
)
154 return dsa_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx
));
157 static void dsa_freedata(void *keydata
)
162 static int dsa_has(void *keydata
, int selection
)
167 if ((selection
& DSA_POSSIBLE_SELECTIONS
) != 0)
170 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
171 ok
= ok
&& (DSA_get0_pub_key(dsa
) != NULL
);
172 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
173 ok
= ok
&& (DSA_get0_priv_key(dsa
) != NULL
);
174 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
175 ok
= ok
&& (DSA_get0_p(dsa
) != NULL
&& DSA_get0_g(dsa
) != NULL
);
179 static int dsa_match(const void *keydata1
, const void *keydata2
, int selection
)
181 const DSA
*dsa1
= keydata1
;
182 const DSA
*dsa2
= keydata2
;
185 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
187 && BN_cmp(DSA_get0_pub_key(dsa1
), DSA_get0_pub_key(dsa2
)) == 0;
188 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
190 && BN_cmp(DSA_get0_priv_key(dsa1
), DSA_get0_priv_key(dsa2
)) == 0;
191 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
192 FFC_PARAMS
*dsaparams1
= dsa_get0_params((DSA
*)dsa1
);
193 FFC_PARAMS
*dsaparams2
= dsa_get0_params((DSA
*)dsa2
);
195 ok
= ok
&& ffc_params_cmp(dsaparams1
, dsaparams2
, 1);
200 static int dsa_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
208 if ((selection
& DSA_POSSIBLE_SELECTIONS
) != 0)
211 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
212 ok
= ok
&& params_to_domparams(dsa
, params
);
213 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
214 ok
= ok
&& params_to_key(dsa
, params
);
219 static int dsa_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
224 OSSL_PARAM
*params
= NULL
;
230 ossl_param_bld_init(&tmpl
);
232 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
233 ok
= ok
&& domparams_to_params(dsa
, &tmpl
);
234 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
235 ok
= ok
&& key_to_params(dsa
, &tmpl
);
238 || (params
= ossl_param_bld_to_param(&tmpl
)) == NULL
)
241 ok
= param_cb(params
, cbarg
);
242 ossl_param_bld_free(params
);
246 /* IMEXPORT = IMPORT + EXPORT */
248 # define DSA_IMEXPORTABLE_PARAMETERS \
249 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
250 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
251 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
252 # define DSA_IMEXPORTABLE_PUBLIC_KEY \
253 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
254 # define DSA_IMEXPORTABLE_PRIVATE_KEY \
255 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
256 static const OSSL_PARAM dsa_all_types
[] = {
257 DSA_IMEXPORTABLE_PARAMETERS
,
258 DSA_IMEXPORTABLE_PUBLIC_KEY
,
259 DSA_IMEXPORTABLE_PRIVATE_KEY
,
262 static const OSSL_PARAM dsa_parameter_types
[] = {
263 DSA_IMEXPORTABLE_PARAMETERS
,
266 static const OSSL_PARAM dsa_key_types
[] = {
267 DSA_IMEXPORTABLE_PUBLIC_KEY
,
268 DSA_IMEXPORTABLE_PRIVATE_KEY
,
271 static const OSSL_PARAM
*dsa_types
[] = {
272 NULL
, /* Index 0 = none of them */
273 dsa_parameter_types
, /* Index 1 = parameter types */
274 dsa_key_types
, /* Index 2 = key types */
275 dsa_all_types
/* Index 3 = 1 + 2 */
278 static const OSSL_PARAM
*dsa_imexport_types(int selection
)
282 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
284 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
286 return dsa_types
[type_select
];
289 static const OSSL_PARAM
*dsa_import_types(int selection
)
291 return dsa_imexport_types(selection
);
294 static const OSSL_PARAM
*dsa_export_types(int selection
)
296 return dsa_imexport_types(selection
);
299 static ossl_inline
int dsa_get_params(void *key
, OSSL_PARAM params
[])
304 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
305 && !OSSL_PARAM_set_int(p
, DSA_bits(dsa
)))
307 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
308 && !OSSL_PARAM_set_int(p
, DSA_security_bits(dsa
)))
310 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
311 && !OSSL_PARAM_set_int(p
, DSA_size(dsa
)))
313 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
314 && !OSSL_PARAM_set_utf8_string(p
, DSA_DEFAULT_MD
))
319 static const OSSL_PARAM dsa_params
[] = {
320 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
321 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
322 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
323 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
327 static const OSSL_PARAM
*dsa_gettable_params(void)
332 static int dsa_validate_domparams(DSA
*dsa
)
336 return dsa_check_params(dsa
, &status
);
339 static int dsa_validate_public(DSA
*dsa
)
342 const BIGNUM
*pub_key
= NULL
;
344 DSA_get0_key(dsa
, &pub_key
, NULL
);
345 return dsa_check_pub_key(dsa
, pub_key
, &status
);
348 static int dsa_validate_private(DSA
*dsa
)
351 const BIGNUM
*priv_key
= NULL
;
353 DSA_get0_key(dsa
, NULL
, &priv_key
);
354 return dsa_check_priv_key(dsa
, priv_key
, &status
);
357 static int dsa_validate(void *keydata
, int selection
)
362 if ((selection
& DSA_POSSIBLE_SELECTIONS
) != 0)
365 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
366 ok
= ok
&& dsa_validate_domparams(dsa
);
368 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
369 ok
= ok
&& dsa_validate_public(dsa
);
371 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
372 ok
= ok
&& dsa_validate_private(dsa
);
374 /* If the whole key is selected, we do a pairwise validation */
375 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
)
376 == OSSL_KEYMGMT_SELECT_KEYPAIR
)
377 ok
= ok
&& dsa_check_pairwise(dsa
);
381 const OSSL_DISPATCH dsa_keymgmt_functions
[] = {
382 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))dsa_newdata
},
383 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))dsa_freedata
},
384 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))dsa_get_params
},
385 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))dsa_gettable_params
},
386 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))dsa_has
},
387 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))dsa_match
},
388 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))dsa_validate
},
389 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))dsa_import
},
390 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))dsa_import_types
},
391 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))dsa_export
},
392 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))dsa_export_types
},