LibsslDHKeyExchange.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(), 0)));
  48. }
  49. } // namespace
  50. DHKeyExchange::DHKeyExchange()
  51. : bnCtx_(0),
  52. keyLength_(0),
  53. prime_(0),
  54. generator_(0),
  55. privateKey_(0),
  56. publicKey_(0)
  57. {}
  58. DHKeyExchange::~DHKeyExchange()
  59. {
  60. BN_CTX_free(bnCtx_);
  61. BN_free(prime_);
  62. BN_free(generator_);
  63. BN_free(privateKey_);
  64. BN_free(publicKey_);
  65. }
  66. void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
  67. const unsigned char* generator,
  68. 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_ = 0;
  77. BN_free(generator_);
  78. generator_ = 0;
  79. BN_free(privateKey_);
  80. privateKey_ = 0;
  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
  100. (unsigned char* out, size_t outLength) const
  101. {
  102. if(outLength < keyLength_) {
  103. throw DL_ABORT_EX
  104. (fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
  105. static_cast<unsigned long>(keyLength_),
  106. static_cast<unsigned long>(outLength)));
  107. }
  108. memset(out, 0, outLength);
  109. size_t publicKeyBytes = BN_num_bytes(publicKey_);
  110. size_t offset = keyLength_-publicKeyBytes;
  111. size_t nwritten = BN_bn2bin(publicKey_, out+offset);
  112. if(nwritten != publicKeyBytes) {
  113. throw DL_ABORT_EX
  114. (fmt("BN_bn2bin in DHKeyExchange::getPublicKey, %lu bytes written,"
  115. " but %lu bytes expected.",
  116. static_cast<unsigned long>(nwritten),
  117. static_cast<unsigned long>(publicKeyBytes)));
  118. }
  119. return nwritten;
  120. }
  121. void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
  122. {
  123. if(RAND_bytes(out, outLength) != 1) {
  124. handleError("RAND_bytes in generateNonce");
  125. }
  126. }
  127. size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
  128. const unsigned char* peerPublicKeyData,
  129. size_t peerPublicKeyLength) const
  130. {
  131. if(outLength < keyLength_) {
  132. throw DL_ABORT_EX
  133. (fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
  134. static_cast<unsigned long>(keyLength_),
  135. static_cast<unsigned long>(outLength)));
  136. }
  137. BIGNUM* peerPublicKey = BN_bin2bn(peerPublicKeyData, peerPublicKeyLength, 0);
  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