LibsslDHKeyExchange.h 5.3 KB

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