dh_backend.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /*
  10. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_names.h>
  15. #include "internal/param_build_set.h"
  16. #include "crypto/dh.h"
  17. /*
  18. * The intention with the "backend" source file is to offer backend functions
  19. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  20. * implementations alike.
  21. */
  22. static int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
  23. {
  24. int ret;
  25. FFC_PARAMS *ffc;
  26. if (dh == NULL)
  27. return 0;
  28. ffc = dh_get0_params(dh);
  29. if (ffc == NULL)
  30. return 0;
  31. ret = ossl_ffc_params_fromdata(ffc, params);
  32. if (ret)
  33. dh_cache_named_group(dh); /* This increments dh->dirty_cnt */
  34. return ret;
  35. }
  36. int dh_params_fromdata(DH *dh, const OSSL_PARAM params[])
  37. {
  38. const OSSL_PARAM *param_priv_len;
  39. long priv_len;
  40. if (!dh_ffc_params_fromdata(dh, params))
  41. return 0;
  42. param_priv_len =
  43. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
  44. if (param_priv_len != NULL
  45. && (!OSSL_PARAM_get_long(param_priv_len, &priv_len)
  46. || !DH_set_length(dh, priv_len)))
  47. return 0;
  48. return 1;
  49. }
  50. int dh_key_fromdata(DH *dh, const OSSL_PARAM params[])
  51. {
  52. const OSSL_PARAM *param_priv_key, *param_pub_key;
  53. BIGNUM *priv_key = NULL, *pub_key = NULL;
  54. if (dh == NULL)
  55. return 0;
  56. param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  57. param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  58. /*
  59. * DH documentation says that a public key must be present if a
  60. * private key is present.
  61. * We want to have at least a public key either way, so we end up
  62. * requiring it unconditionally.
  63. */
  64. if (param_priv_key != NULL && param_pub_key == NULL)
  65. return 0;
  66. if ((param_priv_key != NULL
  67. && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  68. || (param_pub_key != NULL
  69. && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
  70. goto err;
  71. if (!DH_set0_key(dh, pub_key, priv_key))
  72. goto err;
  73. return 1;
  74. err:
  75. BN_clear_free(priv_key);
  76. BN_free(pub_key);
  77. return 0;
  78. }
  79. int dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  80. {
  81. long l = DH_get_length(dh);
  82. if (!ossl_ffc_params_todata(dh_get0_params(dh), bld, params))
  83. return 0;
  84. if (l > 0
  85. && !ossl_param_build_set_long(bld, params, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
  86. return 0;
  87. return 1;
  88. }
  89. int dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  90. {
  91. const BIGNUM *priv = NULL, *pub = NULL;
  92. if (dh == NULL)
  93. return 0;
  94. DH_get0_key(dh, &pub, &priv);
  95. if (priv != NULL
  96. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  97. return 0;
  98. if (pub != NULL
  99. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
  100. return 0;
  101. return 1;
  102. }