dsa_backend.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <openssl/core_names.h>
  10. #include "crypto/dsa.h"
  11. /*
  12. * The intention with the "backend" source file is to offer backend support
  13. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  14. * implementations alike.
  15. */
  16. int dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[])
  17. {
  18. const OSSL_PARAM *param_priv_key, *param_pub_key;
  19. BIGNUM *priv_key = NULL, *pub_key = NULL;
  20. if (dsa == NULL)
  21. return 0;
  22. param_priv_key =
  23. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  24. param_pub_key =
  25. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  26. /* It's ok if neither half is present */
  27. if (param_priv_key == NULL && param_pub_key == NULL)
  28. return 1;
  29. /*
  30. * DSA documentation says that a public key must be present if a
  31. * private key is present.
  32. */
  33. if (param_priv_key != NULL && param_pub_key == NULL)
  34. return 0;
  35. if (param_pub_key != NULL && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
  36. goto err;
  37. if (param_priv_key != NULL && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  38. goto err;
  39. if (!DSA_set0_key(dsa, pub_key, priv_key))
  40. goto err;
  41. return 1;
  42. err:
  43. BN_clear_free(priv_key);
  44. BN_free(pub_key);
  45. return 0;
  46. }