LibsslDHKeyExchange.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "LibsslDHKeyExchange.h"
  36. #include <cstring>
  37. #include <openssl/rand.h>
  38. #include <openssl/err.h>
  39. #include "DlAbortEx.h"
  40. #include "fmt.h"
  41. namespace aria2 {
  42. namespace {
  43. void handleError(const std::string& funName)
  44. {
  45. throw DL_ABORT_EX(
  46. fmt("Exception in libssl routine %s(DHKeyExchange class): %s",
  47. funName.c_str(), ERR_error_string(ERR_get_error(), nullptr)));
  48. }
  49. } // namespace
  50. DHKeyExchange::DHKeyExchange()
  51. : bnCtx_(nullptr),
  52. keyLength_(0),
  53. prime_(nullptr),
  54. generator_(nullptr),
  55. privateKey_(nullptr),
  56. publicKey_(nullptr)
  57. {
  58. }
  59. DHKeyExchange::~DHKeyExchange()
  60. {
  61. BN_CTX_free(bnCtx_);
  62. BN_free(prime_);
  63. BN_free(generator_);
  64. BN_free(privateKey_);
  65. BN_free(publicKey_);
  66. }
  67. void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
  68. const unsigned char* generator, size_t privateKeyBits)
  69. {
  70. BN_CTX_free(bnCtx_);
  71. bnCtx_ = BN_CTX_new();
  72. if (!bnCtx_) {
  73. handleError("BN_CTX_new in init");
  74. }
  75. BN_free(prime_);
  76. prime_ = nullptr;
  77. BN_free(generator_);
  78. generator_ = nullptr;
  79. BN_free(privateKey_);
  80. privateKey_ = nullptr;
  81. if (BN_hex2bn(&prime_, reinterpret_cast<const char*>(prime)) == 0) {
  82. handleError("BN_hex2bn in init");
  83. }
  84. if (BN_hex2bn(&generator_, reinterpret_cast<const char*>(generator)) == 0) {
  85. handleError("BN_hex2bn in init");
  86. }
  87. privateKey_ = BN_new();
  88. if (BN_rand(privateKey_, privateKeyBits, -1, false) == 0) {
  89. handleError("BN_new in init");
  90. }
  91. keyLength_ = (primeBits + 7) / 8;
  92. }
  93. void DHKeyExchange::generatePublicKey()
  94. {
  95. BN_free(publicKey_);
  96. publicKey_ = BN_new();
  97. BN_mod_exp(publicKey_, generator_, privateKey_, prime_, bnCtx_);
  98. }
  99. size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
  100. {
  101. if (outLength < keyLength_) {
  102. throw DL_ABORT_EX(
  103. fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
  104. static_cast<unsigned long>(keyLength_),
  105. static_cast<unsigned long>(outLength)));
  106. }
  107. memset(out, 0, outLength);
  108. size_t publicKeyBytes = BN_num_bytes(publicKey_);
  109. size_t offset = keyLength_ - publicKeyBytes;
  110. size_t nwritten = BN_bn2bin(publicKey_, out + offset);
  111. if (nwritten != publicKeyBytes) {
  112. throw DL_ABORT_EX(
  113. fmt("BN_bn2bin in DHKeyExchange::getPublicKey, %lu bytes written,"
  114. " but %lu bytes expected.",
  115. static_cast<unsigned long>(nwritten),
  116. static_cast<unsigned long>(publicKeyBytes)));
  117. }
  118. return nwritten;
  119. }
  120. void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
  121. {
  122. if (RAND_bytes(out, outLength) != 1) {
  123. handleError("RAND_bytes in generateNonce");
  124. }
  125. }
  126. size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
  127. const unsigned char* peerPublicKeyData,
  128. size_t peerPublicKeyLength) const
  129. {
  130. if (outLength < keyLength_) {
  131. throw DL_ABORT_EX(
  132. fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
  133. static_cast<unsigned long>(keyLength_),
  134. static_cast<unsigned long>(outLength)));
  135. }
  136. BIGNUM* peerPublicKey =
  137. BN_bin2bn(peerPublicKeyData, peerPublicKeyLength, nullptr);
  138. if (!peerPublicKey) {
  139. handleError("BN_bin2bn in computeSecret");
  140. }
  141. BIGNUM* secret = BN_new();
  142. BN_mod_exp(secret, peerPublicKey, privateKey_, prime_, bnCtx_);
  143. BN_free(peerPublicKey);
  144. memset(out, 0, outLength);
  145. size_t secretBytes = BN_num_bytes(secret);
  146. size_t offset = keyLength_ - secretBytes;
  147. size_t nwritten = BN_bn2bin(secret, out + offset);
  148. BN_free(secret);
  149. if (nwritten != secretBytes) {
  150. throw DL_ABORT_EX(
  151. fmt("BN_bn2bin in DHKeyExchange::getPublicKey, %lu bytes written,"
  152. " but %lu bytes expected.",
  153. static_cast<unsigned long>(nwritten),
  154. static_cast<unsigned long>(secretBytes)));
  155. }
  156. return nwritten;
  157. }
  158. } // namespace aria2