LibsslDHKeyExchange.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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. #ifndef _D_LIBSSL_DH_KEY_EXCHANGE_H_
  36. #define _D_LIBSSL_DH_KEY_EXCHANGE_H_
  37. #include "common.h"
  38. #include "DlAbortEx.h"
  39. #include "StringFormat.h"
  40. #include <cstring>
  41. #include <openssl/dh.h>
  42. #include <openssl/rand.h>
  43. #include <openssl/err.h>
  44. namespace aria2 {
  45. class DHKeyExchange {
  46. private:
  47. BN_CTX* bnCtx_;
  48. size_t keyLength_;
  49. BIGNUM* prime_;
  50. BIGNUM* generator_;
  51. BIGNUM* privateKey_;
  52. BIGNUM* publicKey_;
  53. void handleError(const std::string& funName) const
  54. {
  55. throw DL_ABORT_EX
  56. (StringFormat("Exception in libssl routine %s(DHKeyExchange class): %s",
  57. funName.c_str(), ERR_error_string(ERR_get_error(), 0)).str());
  58. }
  59. public:
  60. DHKeyExchange():bnCtx_(0),
  61. keyLength_(0),
  62. prime_(0),
  63. generator_(0),
  64. privateKey_(0),
  65. publicKey_(0) {}
  66. ~DHKeyExchange()
  67. {
  68. BN_CTX_free(bnCtx_);
  69. BN_free(prime_);
  70. BN_free(generator_);
  71. BN_free(privateKey_);
  72. BN_free(publicKey_);
  73. }
  74. void init(const unsigned char* prime, size_t primeBits,
  75. const unsigned char* generator,
  76. size_t privateKeyBits)
  77. {
  78. BN_CTX_free(bnCtx_);
  79. bnCtx_ = BN_CTX_new();
  80. if(!bnCtx_) {
  81. handleError("BN_CTX_new in init");
  82. }
  83. BN_free(prime_);
  84. prime_ = 0;
  85. BN_free(generator_);
  86. generator_ = 0;
  87. BN_free(privateKey_);
  88. privateKey_ = 0;
  89. if(BN_hex2bn(&prime_, reinterpret_cast<const char*>(prime)) == 0) {
  90. handleError("BN_hex2bn in init");
  91. }
  92. if(BN_hex2bn(&generator_, reinterpret_cast<const char*>(generator)) == 0) {
  93. handleError("BN_hex2bn in init");
  94. }
  95. privateKey_ = BN_new();
  96. if(BN_rand(privateKey_, privateKeyBits, -1, false) == 0) {
  97. handleError("BN_new in init");
  98. }
  99. keyLength_ = (primeBits+7)/8;
  100. }
  101. void generatePublicKey()
  102. {
  103. BN_free(publicKey_);
  104. publicKey_ = BN_new();
  105. BN_mod_exp(publicKey_, generator_, privateKey_, prime_, bnCtx_);
  106. }
  107. size_t getPublicKey(unsigned char* out, size_t outLength) const
  108. {
  109. if(outLength < keyLength_) {
  110. throw DL_ABORT_EX
  111. (StringFormat("Insufficient buffer for public key. expect:%u, actual:%u",
  112. keyLength_, outLength).str());
  113. }
  114. memset(out, 0, outLength);
  115. size_t publicKeyBytes = BN_num_bytes(publicKey_);
  116. size_t offset = keyLength_-publicKeyBytes;
  117. size_t nwritten = BN_bn2bin(publicKey_, out+offset);
  118. if(nwritten != publicKeyBytes) {
  119. throw DL_ABORT_EX
  120. (StringFormat("BN_bn2bin in DHKeyExchange::getPublicKey, %u bytes written, but %u bytes expected.", nwritten, publicKeyBytes).str());
  121. }
  122. return nwritten;
  123. }
  124. void generateNonce(unsigned char* out, size_t outLength) const
  125. {
  126. if(RAND_bytes(out, outLength) != 1) {
  127. handleError("RAND_bytes in generateNonce");
  128. }
  129. }
  130. size_t computeSecret(unsigned char* out, size_t outLength,
  131. const unsigned char* peerPublicKeyData,
  132. size_t peerPublicKeyLength) const
  133. {
  134. if(outLength < keyLength_) {
  135. throw DL_ABORT_EX
  136. (StringFormat("Insufficient buffer for secret. expect:%u, actual:%u",
  137. keyLength_, outLength).str());
  138. }
  139. BIGNUM* peerPublicKey = BN_bin2bn(peerPublicKeyData, peerPublicKeyLength, 0);
  140. if(!peerPublicKey) {
  141. handleError("BN_bin2bn in computeSecret");
  142. }
  143. BIGNUM* secret = BN_new();
  144. BN_mod_exp(secret, peerPublicKey, privateKey_, prime_, bnCtx_);
  145. BN_free(peerPublicKey);
  146. memset(out, 0, outLength);
  147. size_t secretBytes = BN_num_bytes(secret);
  148. size_t offset = keyLength_-secretBytes;
  149. size_t nwritten = BN_bn2bin(secret, out+offset);
  150. BN_free(secret);
  151. if(nwritten != secretBytes) {
  152. throw DL_ABORT_EX
  153. (StringFormat("BN_bn2bin in DHKeyExchange::getPublicKey, %u bytes written, but %u bytes expected.", nwritten, secretBytes).str());
  154. }
  155. return nwritten;
  156. }
  157. };
  158. } // namespace aria2
  159. #endif // _D_LIBSSL_DH_KEY_EXCHANGE_H_