WinTLSSession.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  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 WIN_TLS_SESSION_H
  36. #define WIN_TLS_SESSION_H
  37. #include <vector>
  38. #include "common.h"
  39. #include "TLSSession.h"
  40. #include "WinTLSContext.h"
  41. namespace aria2 {
  42. namespace wintls {
  43. struct Buffer
  44. {
  45. private:
  46. size_t off_, free_, cap_;
  47. std::vector<char> buf_;
  48. public:
  49. inline Buffer() : off_(0), free_(0), cap_(0) {}
  50. inline size_t size() const
  51. {
  52. return off_;
  53. }
  54. inline size_t free() const
  55. {
  56. return free_;
  57. }
  58. inline void resize(size_t len)
  59. {
  60. if (cap_ >= len) {
  61. return;
  62. }
  63. buf_.resize(len);
  64. cap_ = buf_.size();
  65. free_ = cap_ - off_;
  66. }
  67. inline char* data()
  68. {
  69. return buf_.data();
  70. }
  71. inline char* end()
  72. {
  73. return buf_.data() + off_;
  74. }
  75. inline void eat(size_t len)
  76. {
  77. off_ -= len;
  78. if (off_) {
  79. memmove(buf_.data(), buf_.data() + len, off_);
  80. }
  81. free_ = cap_ - off_;
  82. }
  83. inline void clear()
  84. {
  85. eat(off_);
  86. }
  87. inline void advance(size_t len)
  88. {
  89. off_ += len;
  90. free_ = cap_ - off_;
  91. }
  92. inline void write(const void* data, size_t len)
  93. {
  94. if (!len) {
  95. return;
  96. }
  97. resize(off_ + len);
  98. memcpy(end(), data, len);
  99. advance(len);
  100. }
  101. };
  102. } // namespace wintls
  103. class WinTLSSession : public TLSSession
  104. {
  105. enum state_t {
  106. st_constructed,
  107. st_initialized,
  108. st_handshake_write,
  109. st_handshake_write_last,
  110. st_handshake_read,
  111. st_handshake_done,
  112. st_connected,
  113. st_closing,
  114. st_closed,
  115. st_error
  116. };
  117. public:
  118. WinTLSSession(WinTLSContext* ctx);
  119. // MUST deallocate all resources
  120. virtual ~WinTLSSession();
  121. // Initializes SSL/TLS session. The |sockfd| is the underlying
  122. // tranport socket. This function returns TLS_ERR_OK if it
  123. // succeeds, or TLS_ERR_ERROR.
  124. virtual int init(sock_t sockfd) CXX11_OVERRIDE;
  125. // Sets |hostname| for TLS SNI extension. This is only meaningful for
  126. // client side session. This function returns TLS_ERR_OK if it
  127. // succeeds, or TLS_ERR_ERROR.
  128. virtual int setSNIHostname(const std::string& hostname) CXX11_OVERRIDE;
  129. // Closes the SSL/TLS session. Don't close underlying transport
  130. // socket. This function returns TLS_ERR_OK if it succeeds, or
  131. // TLS_ERR_ERROR.
  132. virtual int closeConnection() CXX11_OVERRIDE;
  133. // Returns TLS_WANT_READ if SSL/TLS session needs more data from
  134. // remote endpoint to proceed, or TLS_WANT_WRITE if SSL/TLS session
  135. // needs to write more data to proceed. If SSL/TLS session needs
  136. // neither read nor write data at the moment, return value is
  137. // undefined.
  138. virtual int checkDirection() CXX11_OVERRIDE;
  139. // Sends |data| with length |len|. This function returns the number
  140. // of bytes sent if it succeeds, or TLS_ERR_WOULDBLOCK if the
  141. // underlying tranport blocks, or TLS_ERR_ERROR.
  142. virtual ssize_t writeData(const void* data, size_t len) CXX11_OVERRIDE;
  143. // Receives data into |data| with length |len|. This function returns
  144. // the number of bytes received if it succeeds, or TLS_ERR_WOULDBLOCK
  145. // if the underlying tranport blocks, or TLS_ERR_ERROR.
  146. virtual ssize_t readData(void* data, size_t len) CXX11_OVERRIDE;
  147. // Performs client side handshake. The |hostname| is the hostname of
  148. // the remote endpoint and is used to verify its certificate. This
  149. // function returns TLS_ERR_OK if it succeeds, or TLS_ERR_WOULDBLOCK
  150. // if the underlying transport blocks, or TLS_ERR_ERROR.
  151. // When returning TLS_ERR_ERROR, provide certificate validation error
  152. // in |handshakeErr|.
  153. virtual int tlsConnect(const std::string& hostname,
  154. TLSVersion& version,
  155. std::string& handshakeErr) CXX11_OVERRIDE;
  156. // Performs server side handshake. This function returns TLS_ERR_OK
  157. // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
  158. // blocks, or TLS_ERR_ERROR.
  159. virtual int tlsAccept(TLSVersion& version) CXX11_OVERRIDE;
  160. // Returns last error string
  161. virtual std::string getLastErrorString() CXX11_OVERRIDE;
  162. private:
  163. std::string hostname_;
  164. sock_t sockfd_;
  165. TLSSessionSide side_;
  166. CredHandle* cred_;
  167. CtxtHandle handle_;
  168. // Buffer for already encrypted writes
  169. wintls::Buffer writeBuf_;
  170. // While the writeBuf_ holds encrypted messages, writeBuffered_ has the
  171. // corresponding size of unencrpted data used to procude the messages.
  172. size_t writeBuffered_;
  173. // Buffer for still encrypted reads
  174. wintls::Buffer readBuf_;
  175. // Buffer for already decrypted reads
  176. wintls::Buffer decBuf_;
  177. state_t state_;
  178. SECURITY_STATUS status_;
  179. std::unique_ptr<SecPkgContext_StreamSizes> streamSizes_;
  180. };
  181. } // namespace aria2
  182. #endif // TLS_SESSION_H