1193141Sdougb/* 2193141Sdougb * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. 3245163Serwin * 4193141Sdougb * Licensed under the Apache License 2.0 (the "License"). You may not use 5193141Sdougb * this file except in compliance with the License. You can obtain a copy 6193141Sdougb * in the file LICENSE in the source distribution or at 7193141Sdougb * https://www.openssl.org/source/license.html 8193141Sdougb */ 9193141Sdougb 10193141Sdougb/* 11193141Sdougb * DSA low level APIs are deprecated for public use, but still ok for 12193141Sdougb * internal use. 13193141Sdougb */ 14193141Sdougb#include "internal/deprecated.h" 15193141Sdougb 16193141Sdougb#include <openssl/core_dispatch.h> 17234010Sdougb#include <openssl/core_names.h> 18193141Sdougb#include <openssl/bn.h> 19193141Sdougb#include <openssl/err.h> 20193141Sdougb#include "prov/providercommon.h" 21193141Sdougb#include "prov/implementations.h" 22193141Sdougb#include "prov/provider_ctx.h" 23193141Sdougb#include "crypto/dsa.h" 24193141Sdougb#include "internal/sizes.h" 25193141Sdougb#include "internal/nelem.h" 26193141Sdougb#include "internal/param_build_set.h" 27193141Sdougb 28193141Sdougbstatic OSSL_FUNC_keymgmt_new_fn dsa_newdata; 29193141Sdougbstatic OSSL_FUNC_keymgmt_free_fn dsa_freedata; 30193141Sdougbstatic OSSL_FUNC_keymgmt_gen_init_fn dsa_gen_init; 31193141Sdougbstatic OSSL_FUNC_keymgmt_gen_set_template_fn dsa_gen_set_template; 32193141Sdougbstatic OSSL_FUNC_keymgmt_gen_set_params_fn dsa_gen_set_params; 33193141Sdougbstatic OSSL_FUNC_keymgmt_gen_settable_params_fn dsa_gen_settable_params; 34193141Sdougbstatic OSSL_FUNC_keymgmt_gen_fn dsa_gen; 35193141Sdougbstatic OSSL_FUNC_keymgmt_gen_cleanup_fn dsa_gen_cleanup; 36193141Sdougbstatic OSSL_FUNC_keymgmt_load_fn dsa_load; 37193141Sdougbstatic OSSL_FUNC_keymgmt_get_params_fn dsa_get_params; 38193141Sdougbstatic OSSL_FUNC_keymgmt_gettable_params_fn dsa_gettable_params; 39193141Sdougbstatic OSSL_FUNC_keymgmt_has_fn dsa_has; 40193141Sdougbstatic OSSL_FUNC_keymgmt_match_fn dsa_match; 41193141Sdougbstatic OSSL_FUNC_keymgmt_validate_fn dsa_validate; 42193141Sdougbstatic OSSL_FUNC_keymgmt_import_fn dsa_import; 43193141Sdougbstatic OSSL_FUNC_keymgmt_import_types_fn dsa_import_types; 44193141Sdougbstatic OSSL_FUNC_keymgmt_export_fn dsa_export; 45193141Sdougbstatic OSSL_FUNC_keymgmt_export_types_fn dsa_export_types; 46193141Sdougbstatic OSSL_FUNC_keymgmt_dup_fn dsa_dup; 47193141Sdougb 48193141Sdougb#define DSA_DEFAULT_MD "SHA256" 49193141Sdougb#define DSA_POSSIBLE_SELECTIONS \ 50193141Sdougb (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) 51193141Sdougb 52193141Sdougbstruct dsa_gen_ctx { 53193141Sdougb OSSL_LIB_CTX *libctx; 54193141Sdougb 55193141Sdougb FFC_PARAMS *ffc_params; 56193141Sdougb int selection; 57193141Sdougb /* All these parameters are used for parameter generation only */ 58193141Sdougb size_t pbits; 59193141Sdougb size_t qbits; 60193141Sdougb unsigned char *seed; /* optional FIPS186-4 param for testing */ 61193141Sdougb size_t seedlen; 62193141Sdougb int gindex; /* optional FIPS186-4 generator index (ignored if -1) */ 63193141Sdougb int gen_type; /* DSA_PARAMGEN_TYPE_FIPS_186_2 or DSA_PARAMGEN_TYPE_FIPS_186_4 */ 64193141Sdougb int pcounter; 65193141Sdougb int hindex; 66193141Sdougb char *mdname; 67193141Sdougb char *mdprops; 68193141Sdougb OSSL_CALLBACK *cb; 69193141Sdougb void *cbarg; 70193141Sdougb}; 71193141Sdougbtypedef struct dh_name2id_st{ 72193141Sdougb const char *name; 73193141Sdougb int id; 74193141Sdougb} DSA_GENTYPE_NAME2ID; 75193141Sdougb 76193141Sdougbstatic const DSA_GENTYPE_NAME2ID dsatype2id[]= 77193141Sdougb{ 78193141Sdougb#ifdef FIPS_MODULE 79193141Sdougb { "default", DSA_PARAMGEN_TYPE_FIPS_186_4 }, 80193141Sdougb#else 81193141Sdougb { "default", DSA_PARAMGEN_TYPE_FIPS_DEFAULT }, 82193141Sdougb#endif 83193141Sdougb { "fips186_4", DSA_PARAMGEN_TYPE_FIPS_186_4 }, 84193141Sdougb { "fips186_2", DSA_PARAMGEN_TYPE_FIPS_186_2 }, 85193141Sdougb}; 86193141Sdougb 87193141Sdougbstatic int dsa_gen_type_name2id(const char *name) 88193141Sdougb{ 89193141Sdougb size_t i; 90193141Sdougb 91193141Sdougb for (i = 0; i < OSSL_NELEM(dsatype2id); ++i) { 92193141Sdougb if (OPENSSL_strcasecmp(dsatype2id[i].name, name) == 0) 93193141Sdougb return dsatype2id[i].id; 94193141Sdougb } 95193141Sdougb return -1; 96193141Sdougb} 97193141Sdougb 98193141Sdougbstatic int dsa_key_todata(DSA *dsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[], 99193141Sdougb int include_private) 100193141Sdougb{ 101193141Sdougb const BIGNUM *priv = NULL, *pub = NULL; 102234010Sdougb 103193141Sdougb if (dsa == NULL) 104193141Sdougb return 0; 105193141Sdougb 106193141Sdougb DSA_get0_key(dsa, &pub, &priv); 107193141Sdougb if (include_private 108193141Sdougb && priv != NULL 109193141Sdougb && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv)) 110193141Sdougb return 0; 111193141Sdougb if (pub != NULL 112193141Sdougb && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub)) 113193141Sdougb return 0; 114193141Sdougb 115193141Sdougb return 1; 116193141Sdougb} 117193141Sdougb 118193141Sdougbstatic void *dsa_newdata(void *provctx) 119193141Sdougb{ 120193141Sdougb if (!ossl_prov_is_running()) 121193141Sdougb return NULL; 122193141Sdougb return ossl_dsa_new(PROV_LIBCTX_OF(provctx)); 123193141Sdougb} 124193141Sdougb 125193141Sdougbstatic void dsa_freedata(void *keydata) 126193141Sdougb{ 127193141Sdougb DSA_free(keydata); 128193141Sdougb} 129193141Sdougb 130193141Sdougbstatic int dsa_has(const void *keydata, int selection) 131193141Sdougb{ 132193141Sdougb const DSA *dsa = keydata; 133193141Sdougb int ok = 1; 134193141Sdougb 135193141Sdougb if (!ossl_prov_is_running() || dsa == NULL) 136193141Sdougb return 0; 137193141Sdougb if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) 138193141Sdougb return 1; /* the selection is not missing */ 139193141Sdougb 140193141Sdougb if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) 141193141Sdougb ok = ok && (DSA_get0_pub_key(dsa) != NULL); 142193141Sdougb if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) 143193141Sdougb ok = ok && (DSA_get0_priv_key(dsa) != NULL); 144193141Sdougb if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) 145193141Sdougb ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL); 146193141Sdougb return ok; 147193141Sdougb} 148193141Sdougb 149193141Sdougbstatic int dsa_match(const void *keydata1, const void *keydata2, int selection) 150193141Sdougb{ 151193141Sdougb const DSA *dsa1 = keydata1; 152193141Sdougb const DSA *dsa2 = keydata2; 153193141Sdougb int ok = 1; 154193141Sdougb 155193141Sdougb if (!ossl_prov_is_running()) 156193141Sdougb return 0; 157193141Sdougb 158193141Sdougb if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 159193141Sdougb int key_checked = 0; 160193141Sdougb 161193141Sdougb if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { 162193141Sdougb const BIGNUM *pa = DSA_get0_pub_key(dsa1); 163193141Sdougb const BIGNUM *pb = DSA_get0_pub_key(dsa2); 164193141Sdougb 165193141Sdougb if (pa != NULL && pb != NULL) { 166193141Sdougb ok = ok && BN_cmp(pa, pb) == 0; 167193141Sdougb key_checked = 1; 168193141Sdougb } 169193141Sdougb } 170193141Sdougb if (!key_checked 171193141Sdougb && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { 172193141Sdougb const BIGNUM *pa = DSA_get0_priv_key(dsa1); 173193141Sdougb const BIGNUM *pb = DSA_get0_priv_key(dsa2); 174193141Sdougb 175193141Sdougb if (pa != NULL && pb != NULL) { 176193141Sdougb ok = ok && BN_cmp(pa, pb) == 0; 177193141Sdougb key_checked = 1; 178193141Sdougb } 179193141Sdougb } 180193141Sdougb ok = ok && key_checked; 181193141Sdougb } 182193141Sdougb if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { 183193141Sdougb FFC_PARAMS *dsaparams1 = ossl_dsa_get0_params((DSA *)dsa1); 184193141Sdougb FFC_PARAMS *dsaparams2 = ossl_dsa_get0_params((DSA *)dsa2); 185193141Sdougb 186193141Sdougb ok = ok && ossl_ffc_params_cmp(dsaparams1, dsaparams2, 1); 187193141Sdougb } 188193141Sdougb return ok; 189193141Sdougb} 190193141Sdougb 191193141Sdougbstatic int dsa_import(void *keydata, int selection, const OSSL_PARAM params[]) 192193141Sdougb{ 193193141Sdougb DSA *dsa = keydata; 194193141Sdougb int ok = 1; 195193141Sdougb 196193141Sdougb if (!ossl_prov_is_running() || dsa == NULL) 197193141Sdougb return 0; 198193141Sdougb 199193141Sdougb if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) 200193141Sdougb return 0; 201 202 /* a key without parameters is meaningless */ 203 ok = ok && ossl_dsa_ffc_params_fromdata(dsa, params); 204 205 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 206 int include_private = 207 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; 208 209 ok = ok && ossl_dsa_key_fromdata(dsa, params, include_private); 210 } 211 212 return ok; 213} 214 215static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, 216 void *cbarg) 217{ 218 DSA *dsa = keydata; 219 OSSL_PARAM_BLD *tmpl; 220 OSSL_PARAM *params = NULL; 221 int ok = 1; 222 223 if (!ossl_prov_is_running() || dsa == NULL) 224 return 0; 225 226 if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) 227 return 0; 228 229 tmpl = OSSL_PARAM_BLD_new(); 230 if (tmpl == NULL) 231 return 0; 232 233 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) 234 ok = ok && ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), tmpl, NULL); 235 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 236 int include_private = 237 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; 238 239 ok = ok && dsa_key_todata(dsa, tmpl, NULL, include_private); 240 } 241 242 if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { 243 ok = 0; 244 goto err; 245 } 246 247 ok = param_cb(params, cbarg); 248 OSSL_PARAM_free(params); 249err: 250 OSSL_PARAM_BLD_free(tmpl); 251 return ok; 252} 253 254/* IMEXPORT = IMPORT + EXPORT */ 255 256# define DSA_IMEXPORTABLE_PARAMETERS \ 257 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \ 258 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \ 259 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \ 260 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \ 261 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \ 262 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \ 263 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \ 264 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0) 265# define DSA_IMEXPORTABLE_PUBLIC_KEY \ 266 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0) 267# define DSA_IMEXPORTABLE_PRIVATE_KEY \ 268 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0) 269static const OSSL_PARAM dsa_all_types[] = { 270 DSA_IMEXPORTABLE_PARAMETERS, 271 DSA_IMEXPORTABLE_PUBLIC_KEY, 272 DSA_IMEXPORTABLE_PRIVATE_KEY, 273 OSSL_PARAM_END 274}; 275static const OSSL_PARAM dsa_parameter_types[] = { 276 DSA_IMEXPORTABLE_PARAMETERS, 277 OSSL_PARAM_END 278}; 279static const OSSL_PARAM dsa_key_types[] = { 280 DSA_IMEXPORTABLE_PUBLIC_KEY, 281 DSA_IMEXPORTABLE_PRIVATE_KEY, 282 OSSL_PARAM_END 283}; 284static const OSSL_PARAM *dsa_types[] = { 285 NULL, /* Index 0 = none of them */ 286 dsa_parameter_types, /* Index 1 = parameter types */ 287 dsa_key_types, /* Index 2 = key types */ 288 dsa_all_types /* Index 3 = 1 + 2 */ 289}; 290 291static const OSSL_PARAM *dsa_imexport_types(int selection) 292{ 293 int type_select = 0; 294 295 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) 296 type_select += 1; 297 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) 298 type_select += 2; 299 return dsa_types[type_select]; 300} 301 302static const OSSL_PARAM *dsa_import_types(int selection) 303{ 304 return dsa_imexport_types(selection); 305} 306 307static const OSSL_PARAM *dsa_export_types(int selection) 308{ 309 return dsa_imexport_types(selection); 310} 311 312static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[]) 313{ 314 DSA *dsa = key; 315 OSSL_PARAM *p; 316 317 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL 318 && !OSSL_PARAM_set_int(p, DSA_bits(dsa))) 319 return 0; 320 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL 321 && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa))) 322 return 0; 323 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL 324 && !OSSL_PARAM_set_int(p, DSA_size(dsa))) 325 return 0; 326 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL 327 && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD)) 328 return 0; 329 return ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), NULL, params) 330 && dsa_key_todata(dsa, NULL, params, 1); 331} 332 333static const OSSL_PARAM dsa_params[] = { 334 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), 335 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), 336 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), 337 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0), 338 DSA_IMEXPORTABLE_PARAMETERS, 339 DSA_IMEXPORTABLE_PUBLIC_KEY, 340 DSA_IMEXPORTABLE_PRIVATE_KEY, 341 OSSL_PARAM_END 342}; 343 344static const OSSL_PARAM *dsa_gettable_params(void *provctx) 345{ 346 return dsa_params; 347} 348 349static int dsa_validate_domparams(const DSA *dsa, int checktype) 350{ 351 int status = 0; 352 353 return ossl_dsa_check_params(dsa, checktype, &status); 354} 355 356static int dsa_validate_public(const DSA *dsa) 357{ 358 int status = 0; 359 const BIGNUM *pub_key = NULL; 360 361 DSA_get0_key(dsa, &pub_key, NULL); 362 if (pub_key == NULL) 363 return 0; 364 return ossl_dsa_check_pub_key(dsa, pub_key, &status); 365} 366 367static int dsa_validate_private(const DSA *dsa) 368{ 369 int status = 0; 370 const BIGNUM *priv_key = NULL; 371 372 DSA_get0_key(dsa, NULL, &priv_key); 373 if (priv_key == NULL) 374 return 0; 375 return ossl_dsa_check_priv_key(dsa, priv_key, &status); 376} 377 378static int dsa_validate(const void *keydata, int selection, int checktype) 379{ 380 const DSA *dsa = keydata; 381 int ok = 1; 382 383 if (!ossl_prov_is_running()) 384 return 0; 385 386 if ((selection & DSA_POSSIBLE_SELECTIONS) == 0) 387 return 1; /* nothing to validate */ 388 389 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) 390 ok = ok && dsa_validate_domparams(dsa, checktype); 391 392 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) 393 ok = ok && dsa_validate_public(dsa); 394 395 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) 396 ok = ok && dsa_validate_private(dsa); 397 398 /* If the whole key is selected, we do a pairwise validation */ 399 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) 400 == OSSL_KEYMGMT_SELECT_KEYPAIR) 401 ok = ok && ossl_dsa_check_pairwise(dsa); 402 return ok; 403} 404 405static void *dsa_gen_init(void *provctx, int selection, 406 const OSSL_PARAM params[]) 407{ 408 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); 409 struct dsa_gen_ctx *gctx = NULL; 410 411 if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0) 412 return NULL; 413 414 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { 415 gctx->selection = selection; 416 gctx->libctx = libctx; 417 gctx->pbits = 2048; 418 gctx->qbits = 224; 419#ifdef FIPS_MODULE 420 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4; 421#else 422 gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_DEFAULT; 423#endif 424 gctx->gindex = -1; 425 gctx->pcounter = -1; 426 gctx->hindex = 0; 427 } 428 if (!dsa_gen_set_params(gctx, params)) { 429 OPENSSL_free(gctx); 430 gctx = NULL; 431 } 432 return gctx; 433} 434 435static int dsa_gen_set_template(void *genctx, void *templ) 436{ 437 struct dsa_gen_ctx *gctx = genctx; 438 DSA *dsa = templ; 439 440 if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL) 441 return 0; 442 gctx->ffc_params = ossl_dsa_get0_params(dsa); 443 return 1; 444} 445 446static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed, 447 size_t seedlen) 448{ 449 OPENSSL_clear_free(gctx->seed, gctx->seedlen); 450 gctx->seed = NULL; 451 gctx->seedlen = 0; 452 if (seed != NULL && seedlen > 0) { 453 gctx->seed = OPENSSL_memdup(seed, seedlen); 454 if (gctx->seed == NULL) 455 return 0; 456 gctx->seedlen = seedlen; 457 } 458 return 1; 459} 460 461static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[]) 462{ 463 struct dsa_gen_ctx *gctx = genctx; 464 const OSSL_PARAM *p; 465 466 if (gctx == NULL) 467 return 0; 468 if (params == NULL) 469 return 1; 470 471 472 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE); 473 if (p != NULL) { 474 if (p->data_type != OSSL_PARAM_UTF8_STRING 475 || ((gctx->gen_type = dsa_gen_type_name2id(p->data)) == -1)) { 476 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); 477 return 0; 478 } 479 } 480 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX); 481 if (p != NULL 482 && !OSSL_PARAM_get_int(p, &gctx->gindex)) 483 return 0; 484 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER); 485 if (p != NULL 486 && !OSSL_PARAM_get_int(p, &gctx->pcounter)) 487 return 0; 488 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H); 489 if (p != NULL 490 && !OSSL_PARAM_get_int(p, &gctx->hindex)) 491 return 0; 492 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED); 493 if (p != NULL 494 && (p->data_type != OSSL_PARAM_OCTET_STRING 495 || !dsa_set_gen_seed(gctx, p->data, p->data_size))) 496 return 0; 497 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL 498 && !OSSL_PARAM_get_size_t(p, &gctx->pbits)) 499 return 0; 500 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL 501 && !OSSL_PARAM_get_size_t(p, &gctx->qbits)) 502 return 0; 503 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST); 504 if (p != NULL) { 505 if (p->data_type != OSSL_PARAM_UTF8_STRING) 506 return 0; 507 OPENSSL_free(gctx->mdname); 508 gctx->mdname = OPENSSL_strdup(p->data); 509 if (gctx->mdname == NULL) 510 return 0; 511 } 512 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS); 513 if (p != NULL) { 514 if (p->data_type != OSSL_PARAM_UTF8_STRING) 515 return 0; 516 OPENSSL_free(gctx->mdprops); 517 gctx->mdprops = OPENSSL_strdup(p->data); 518 if (gctx->mdprops == NULL) 519 return 0; 520 } 521 return 1; 522} 523 524static const OSSL_PARAM *dsa_gen_settable_params(ossl_unused void *genctx, 525 ossl_unused void *provctx) 526{ 527 static OSSL_PARAM settable[] = { 528 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0), 529 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL), 530 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL), 531 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0), 532 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0), 533 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), 534 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), 535 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), 536 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), 537 OSSL_PARAM_END 538 }; 539 return settable; 540} 541 542static int dsa_gencb(int p, int n, BN_GENCB *cb) 543{ 544 struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb); 545 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; 546 547 params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p); 548 params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n); 549 550 return gctx->cb(params, gctx->cbarg); 551} 552 553static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) 554{ 555 struct dsa_gen_ctx *gctx = genctx; 556 DSA *dsa = NULL; 557 BN_GENCB *gencb = NULL; 558 int ret = 0; 559 FFC_PARAMS *ffc; 560 561 if (!ossl_prov_is_running() || gctx == NULL) 562 return NULL; 563 dsa = ossl_dsa_new(gctx->libctx); 564 if (dsa == NULL) 565 return NULL; 566 567 if (gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_DEFAULT) 568 gctx->gen_type = (gctx->pbits >= 2048 ? DSA_PARAMGEN_TYPE_FIPS_186_4 : 569 DSA_PARAMGEN_TYPE_FIPS_186_2); 570 571 gctx->cb = osslcb; 572 gctx->cbarg = cbarg; 573 gencb = BN_GENCB_new(); 574 if (gencb != NULL) 575 BN_GENCB_set(gencb, dsa_gencb, genctx); 576 577 ffc = ossl_dsa_get0_params(dsa); 578 /* Copy the template value if one was passed */ 579 if (gctx->ffc_params != NULL 580 && !ossl_ffc_params_copy(ffc, gctx->ffc_params)) 581 goto end; 582 583 if (gctx->seed != NULL 584 && !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen)) 585 goto end; 586 if (gctx->gindex != -1) { 587 ossl_ffc_params_set_gindex(ffc, gctx->gindex); 588 if (gctx->pcounter != -1) 589 ossl_ffc_params_set_pcounter(ffc, gctx->pcounter); 590 } else if (gctx->hindex != 0) { 591 ossl_ffc_params_set_h(ffc, gctx->hindex); 592 } 593 if (gctx->mdname != NULL) { 594 if (!ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops)) 595 goto end; 596 } 597 if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { 598 599 if (ossl_dsa_generate_ffc_parameters(dsa, gctx->gen_type, 600 gctx->pbits, gctx->qbits, 601 gencb) <= 0) 602 goto end; 603 } 604 ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, 605 gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_186_2); 606 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { 607 if (ffc->p == NULL 608 || ffc->q == NULL 609 || ffc->g == NULL) 610 goto end; 611 if (DSA_generate_key(dsa) <= 0) 612 goto end; 613 } 614 ret = 1; 615end: 616 if (ret <= 0) { 617 DSA_free(dsa); 618 dsa = NULL; 619 } 620 BN_GENCB_free(gencb); 621 return dsa; 622} 623 624static void dsa_gen_cleanup(void *genctx) 625{ 626 struct dsa_gen_ctx *gctx = genctx; 627 628 if (gctx == NULL) 629 return; 630 631 OPENSSL_free(gctx->mdname); 632 OPENSSL_free(gctx->mdprops); 633 OPENSSL_clear_free(gctx->seed, gctx->seedlen); 634 OPENSSL_free(gctx); 635} 636 637static void *dsa_load(const void *reference, size_t reference_sz) 638{ 639 DSA *dsa = NULL; 640 641 if (ossl_prov_is_running() && reference_sz == sizeof(dsa)) { 642 /* The contents of the reference is the address to our object */ 643 dsa = *(DSA **)reference; 644 /* We grabbed, so we detach it */ 645 *(DSA **)reference = NULL; 646 return dsa; 647 } 648 return NULL; 649} 650 651static void *dsa_dup(const void *keydata_from, int selection) 652{ 653 if (ossl_prov_is_running()) 654 return ossl_dsa_dup(keydata_from, selection); 655 return NULL; 656} 657 658const OSSL_DISPATCH ossl_dsa_keymgmt_functions[] = { 659 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata }, 660 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init }, 661 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template }, 662 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params }, 663 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, 664 (void (*)(void))dsa_gen_settable_params }, 665 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen }, 666 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup }, 667 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dsa_load }, 668 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata }, 669 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params }, 670 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params }, 671 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has }, 672 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match }, 673 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate }, 674 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import }, 675 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types }, 676 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export }, 677 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types }, 678 { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dsa_dup }, 679 { 0, NULL } 680}; 681