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 "e_os.h" /* strcasecmp */
17 #include <openssl/core_numbers.h>
18 #include <openssl/core_names.h>
19 #include <openssl/bn.h>
20 #include <openssl/err.h>
21 #include "prov/providercommon.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_ctx.h"
24 #include "crypto/dsa.h"
25 #include "internal/sizes.h"
26 #include "internal/nelem.h"
27 #include "internal/param_build_set.h"
29 static OSSL_OP_keymgmt_new_fn dsa_newdata;
30 static OSSL_OP_keymgmt_free_fn dsa_freedata;
31 static OSSL_OP_keymgmt_gen_init_fn dsa_gen_init;
32 static OSSL_OP_keymgmt_gen_set_template_fn dsa_gen_set_template;
33 static OSSL_OP_keymgmt_gen_set_params_fn dsa_gen_set_params;
34 static OSSL_OP_keymgmt_gen_settable_params_fn dsa_gen_settable_params;
35 static OSSL_OP_keymgmt_gen_fn dsa_gen;
36 static OSSL_OP_keymgmt_gen_cleanup_fn dsa_gen_cleanup;
37 static OSSL_OP_keymgmt_get_params_fn dsa_get_params;
38 static OSSL_OP_keymgmt_gettable_params_fn dsa_gettable_params;
39 static OSSL_OP_keymgmt_has_fn dsa_has;
40 static OSSL_OP_keymgmt_match_fn dsa_match;
41 static OSSL_OP_keymgmt_validate_fn dsa_validate;
42 static OSSL_OP_keymgmt_import_fn dsa_import;
43 static OSSL_OP_keymgmt_import_types_fn dsa_import_types;
44 static OSSL_OP_keymgmt_export_fn dsa_export;
45 static OSSL_OP_keymgmt_export_types_fn dsa_export_types;
47 #define DSA_DEFAULT_MD "SHA256"
48 #define DSA_POSSIBLE_SELECTIONS \
49 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
54 FFC_PARAMS *ffc_params;
56 /* All these parameters are used for parameter generation only */
60 unsigned char *seed; /* optional FIPS186-4 param for testing */
62 int gindex; /* optional FIPS186-4 generator index (ignored if -1) */
63 int gen_type; /* DSA_PARAMGEN_TYPE_FIPS_186_2 or DSA_PARAMGEN_TYPE_FIPS_186_4 */
69 typedef struct dh_name2id_st{
72 } DSA_GENTYPE_NAME2ID;
74 static const DSA_GENTYPE_NAME2ID dsatype2id[]=
76 { "default", DSA_PARAMGEN_TYPE_FIPS_186_4 },
77 { "fips186_4", DSA_PARAMGEN_TYPE_FIPS_186_4 },
78 { "fips186_2", DSA_PARAMGEN_TYPE_FIPS_186_2 },
81 static int dsa_gen_type_name2id(const char *name)
85 for (i = 0; i < OSSL_NELEM(dsatype2id); ++i) {
86 if (strcasecmp(dsatype2id[i].name, name) == 0)
87 return dsatype2id[i].id;
92 static int dsa_key_todata(DSA *dsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
94 const BIGNUM *priv = NULL, *pub = NULL;
99 DSA_get0_key(dsa, &pub, &priv);
101 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
104 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
110 static void *dsa_newdata(void *provctx)
112 return dsa_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx));
115 static void dsa_freedata(void *keydata)
120 static int dsa_has(void *keydata, int selection)
126 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
129 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
130 ok = ok && (DSA_get0_pub_key(dsa) != NULL);
131 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
132 ok = ok && (DSA_get0_priv_key(dsa) != NULL);
133 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
134 ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
139 static int dsa_match(const void *keydata1, const void *keydata2, int selection)
141 const DSA *dsa1 = keydata1;
142 const DSA *dsa2 = keydata2;
145 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
147 && BN_cmp(DSA_get0_pub_key(dsa1), DSA_get0_pub_key(dsa2)) == 0;
148 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
150 && BN_cmp(DSA_get0_priv_key(dsa1), DSA_get0_priv_key(dsa2)) == 0;
151 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
152 FFC_PARAMS *dsaparams1 = dsa_get0_params((DSA *)dsa1);
153 FFC_PARAMS *dsaparams2 = dsa_get0_params((DSA *)dsa2);
155 ok = ok && ffc_params_cmp(dsaparams1, dsaparams2, 1);
160 static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
168 if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
171 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
172 ok = ok && dsa_ffc_params_fromdata(dsa, params);
173 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
174 ok = ok && dsa_key_fromdata(dsa, params);
179 static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
183 OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
184 OSSL_PARAM *params = NULL;
190 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
191 ok = ok && ffc_params_todata(dsa_get0_params(dsa), tmpl, NULL);
192 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
193 ok = ok && dsa_key_todata(dsa, tmpl, NULL);
196 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
199 ok = param_cb(params, cbarg);
200 OSSL_PARAM_BLD_free_params(params);
202 OSSL_PARAM_BLD_free(tmpl);
206 /* IMEXPORT = IMPORT + EXPORT */
208 # define DSA_IMEXPORTABLE_PARAMETERS \
209 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
210 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
211 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
212 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
213 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
214 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
215 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
216 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0)
217 # define DSA_IMEXPORTABLE_PUBLIC_KEY \
218 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
219 # define DSA_IMEXPORTABLE_PRIVATE_KEY \
220 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
221 static const OSSL_PARAM dsa_all_types[] = {
222 DSA_IMEXPORTABLE_PARAMETERS,
223 DSA_IMEXPORTABLE_PUBLIC_KEY,
224 DSA_IMEXPORTABLE_PRIVATE_KEY,
227 static const OSSL_PARAM dsa_parameter_types[] = {
228 DSA_IMEXPORTABLE_PARAMETERS,
231 static const OSSL_PARAM dsa_key_types[] = {
232 DSA_IMEXPORTABLE_PUBLIC_KEY,
233 DSA_IMEXPORTABLE_PRIVATE_KEY,
236 static const OSSL_PARAM *dsa_types[] = {
237 NULL, /* Index 0 = none of them */
238 dsa_parameter_types, /* Index 1 = parameter types */
239 dsa_key_types, /* Index 2 = key types */
240 dsa_all_types /* Index 3 = 1 + 2 */
243 static const OSSL_PARAM *dsa_imexport_types(int selection)
247 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
249 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
251 return dsa_types[type_select];
254 static const OSSL_PARAM *dsa_import_types(int selection)
256 return dsa_imexport_types(selection);
259 static const OSSL_PARAM *dsa_export_types(int selection)
261 return dsa_imexport_types(selection);
264 static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
269 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
270 && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
272 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
273 && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
275 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
276 && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
278 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
279 && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
281 return ffc_params_todata(dsa_get0_params(dsa), NULL, params)
282 && dsa_key_todata(dsa, NULL, params);
285 static const OSSL_PARAM dsa_params[] = {
286 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
287 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
288 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
289 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
290 DSA_IMEXPORTABLE_PARAMETERS,
291 DSA_IMEXPORTABLE_PUBLIC_KEY,
292 DSA_IMEXPORTABLE_PRIVATE_KEY,
296 static const OSSL_PARAM *dsa_gettable_params(void)
301 static int dsa_validate_domparams(DSA *dsa)
305 return dsa_check_params(dsa, &status);
308 static int dsa_validate_public(DSA *dsa)
311 const BIGNUM *pub_key = NULL;
313 DSA_get0_key(dsa, &pub_key, NULL);
316 return dsa_check_pub_key(dsa, pub_key, &status);
319 static int dsa_validate_private(DSA *dsa)
322 const BIGNUM *priv_key = NULL;
324 DSA_get0_key(dsa, NULL, &priv_key);
325 if (priv_key == NULL)
327 return dsa_check_priv_key(dsa, priv_key, &status);
330 static int dsa_validate(void *keydata, int selection)
335 if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
338 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
339 ok = ok && dsa_validate_domparams(dsa);
341 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
342 ok = ok && dsa_validate_public(dsa);
344 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
345 ok = ok && dsa_validate_private(dsa);
347 /* If the whole key is selected, we do a pairwise validation */
348 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
349 == OSSL_KEYMGMT_SELECT_KEYPAIR)
350 ok = ok && dsa_check_pairwise(dsa);
354 static void *dsa_gen_init(void *provctx, int selection)
356 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
357 struct dsa_gen_ctx *gctx = NULL;
359 if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
362 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
363 gctx->selection = selection;
364 gctx->libctx = libctx;
368 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4;
376 static int dsa_gen_set_template(void *genctx, void *templ)
378 struct dsa_gen_ctx *gctx = genctx;
381 if (gctx == NULL || dsa == NULL)
383 gctx->ffc_params = dsa_get0_params(dsa);
387 static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed,
390 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
393 if (seed != NULL && seedlen > 0) {
394 gctx->seed = OPENSSL_memdup(seed, seedlen);
395 if (gctx->seed == NULL)
397 gctx->seedlen = seedlen;
402 static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
404 struct dsa_gen_ctx *gctx = genctx;
410 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
412 if (p->data_type != OSSL_PARAM_UTF8_STRING
413 || ((gctx->gen_type = dsa_gen_type_name2id(p->data)) == -1)) {
414 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
418 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
420 && !OSSL_PARAM_get_int(p, &gctx->gindex))
422 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
424 && !OSSL_PARAM_get_int(p, &gctx->pcounter))
426 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
428 && !OSSL_PARAM_get_int(p, &gctx->hindex))
430 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
432 && (p->data_type != OSSL_PARAM_OCTET_STRING
433 || !dsa_set_gen_seed(gctx, p->data, p->data_size)))
435 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
436 && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
438 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
439 && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
441 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
443 const OSSL_PARAM *p1;
444 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
447 if (p->data_type != OSSL_PARAM_UTF8_STRING)
449 p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
451 if (!OSSL_PARAM_get_utf8_string(p1, &str, sizeof(mdprops)))
454 EVP_MD_free(gctx->md);
455 gctx->md = EVP_MD_fetch(gctx->libctx, p->data, mdprops);
456 if (gctx->md == NULL)
462 static const OSSL_PARAM *dsa_gen_settable_params(void *provctx)
464 static OSSL_PARAM settable[] = {
465 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
466 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
467 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
468 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
469 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
470 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
471 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
472 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
473 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
479 static int dsa_gencb(int p, int n, BN_GENCB *cb)
481 struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
482 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
484 params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
485 params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
487 return gctx->cb(params, gctx->cbarg);
490 static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
492 struct dsa_gen_ctx *gctx = genctx;
494 BN_GENCB *gencb = NULL;
500 dsa = dsa_new_with_ctx(gctx->libctx);
506 gencb = BN_GENCB_new();
508 BN_GENCB_set(gencb, dsa_gencb, genctx);
510 ffc = dsa_get0_params(dsa);
511 /* Copy the template value if one was passed */
512 if (gctx->ffc_params != NULL
513 && !ffc_params_copy(ffc, gctx->ffc_params))
516 if (gctx->seed != NULL
517 && !ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
519 if (gctx->gindex != -1) {
520 ffc_params_set_gindex(ffc, gctx->gindex);
521 if (gctx->pcounter != -1)
522 ffc_params_set_pcounter(ffc, gctx->pcounter);
523 } else if (gctx->hindex != 0) {
524 ffc_params_set_h(ffc, gctx->hindex);
526 if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
528 if (dsa_generate_ffc_parameters(dsa, gctx->gen_type,
529 gctx->pbits, gctx->qbits, gctx->md,
533 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
538 if (DSA_generate_key(dsa) <= 0)
547 BN_GENCB_free(gencb);
551 static void dsa_gen_cleanup(void *genctx)
553 struct dsa_gen_ctx *gctx = genctx;
558 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
559 EVP_MD_free(gctx->md);
563 const OSSL_DISPATCH dsa_keymgmt_functions[] = {
564 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
565 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init },
566 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template },
567 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params },
568 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
569 (void (*)(void))dsa_gen_settable_params },
570 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen },
571 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup },
572 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
573 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
574 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
575 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
576 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match },
577 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate },
578 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
579 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
580 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
581 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },