WinTLSSession.h 6.0 KB

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