dh_backend.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2020-2021 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. if ((param_priv_key != NULL
  59. && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  60. || (param_pub_key != NULL
  61. && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
  62. goto err;
  63. if (!DH_set0_key(dh, pub_key, priv_key))
  64. goto err;
  65. return 1;
  66. err:
  67. BN_clear_free(priv_key);
  68. BN_free(pub_key);
  69. return 0;
  70. }
  71. int dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  72. {
  73. long l = DH_get_length(dh);
  74. if (!ossl_ffc_params_todata(dh_get0_params(dh), bld, params))
  75. return 0;
  76. if (l > 0
  77. && !ossl_param_build_set_long(bld, params, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
  78. return 0;
  79. return 1;
  80. }
  81. int dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  82. {
  83. const BIGNUM *priv = NULL, *pub = NULL;
  84. if (dh == NULL)
  85. return 0;
  86. DH_get0_key(dh, &pub, &priv);
  87. if (priv != NULL
  88. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  89. return 0;
  90. if (pub != NULL
  91. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
  92. return 0;
  93. return 1;
  94. }