WinTLSSession.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 TLSBuffer : public ::SecBuffer {
  88. public:
  89. TLSBuffer() : ::SecBuffer{} {}
  90. explicit TLSBuffer(ULONG type, ULONG size, void* data)
  91. {
  92. cbBuffer = size;
  93. BufferType = type;
  94. pvBuffer = data;
  95. }
  96. };
  97. class WinTLSSession : public TLSSession {
  98. enum state_t {
  99. st_constructed,
  100. st_initialized,
  101. st_handshake_write,
  102. st_handshake_write_last,
  103. st_handshake_read,
  104. st_handshake_done,
  105. st_connected,
  106. st_closing,
  107. st_closed,
  108. st_error
  109. };
  110. public:
  111. WinTLSSession(WinTLSContext* ctx);
  112. // MUST deallocate all resources
  113. virtual ~WinTLSSession();
  114. // Initializes SSL/TLS session. The |sockfd| is the underlying
  115. // transport socket. This function returns TLS_ERR_OK if it
  116. // succeeds, or TLS_ERR_ERROR.
  117. virtual int init(sock_t sockfd) CXX11_OVERRIDE;
  118. // Sets |hostname| for TLS SNI extension. This is only meaningful for
  119. // client side session. This function returns TLS_ERR_OK if it
  120. // succeeds, or TLS_ERR_ERROR.
  121. virtual int setSNIHostname(const std::string& hostname) CXX11_OVERRIDE;
  122. // Closes the SSL/TLS session. Don't close underlying transport
  123. // socket. This function returns TLS_ERR_OK if it succeeds, or
  124. // TLS_ERR_ERROR.
  125. virtual int closeConnection() CXX11_OVERRIDE;
  126. // Returns TLS_WANT_READ if SSL/TLS session needs more data from
  127. // remote endpoint to proceed, or TLS_WANT_WRITE if SSL/TLS session
  128. // needs to write more data to proceed. If SSL/TLS session needs
  129. // neither read nor write data at the moment, return value is
  130. // undefined.
  131. virtual int checkDirection() CXX11_OVERRIDE;
  132. // Sends |data| with length |len|. This function returns the number
  133. // of bytes sent if it succeeds, or TLS_ERR_WOULDBLOCK if the
  134. // underlying transport blocks, or TLS_ERR_ERROR.
  135. virtual ssize_t writeData(const void* data, size_t len) CXX11_OVERRIDE;
  136. // Receives data into |data| with length |len|. This function returns
  137. // the number of bytes received if it succeeds, or TLS_ERR_WOULDBLOCK
  138. // if the underlying transport blocks, or TLS_ERR_ERROR.
  139. virtual ssize_t readData(void* data, size_t len) CXX11_OVERRIDE;
  140. // Performs client side handshake. The |hostname| is the hostname of
  141. // the remote endpoint and is used to verify its certificate. This
  142. // function returns TLS_ERR_OK if it succeeds, or TLS_ERR_WOULDBLOCK
  143. // if the underlying transport blocks, or TLS_ERR_ERROR.
  144. // When returning TLS_ERR_ERROR, provide certificate validation error
  145. // in |handshakeErr|.
  146. virtual int tlsConnect(const std::string& hostname, TLSVersion& version,
  147. std::string& handshakeErr) CXX11_OVERRIDE;
  148. // Performs server side handshake. This function returns TLS_ERR_OK
  149. // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
  150. // blocks, or TLS_ERR_ERROR.
  151. virtual int tlsAccept(TLSVersion& version) CXX11_OVERRIDE;
  152. // Returns last error string
  153. virtual std::string getLastErrorString() CXX11_OVERRIDE;
  154. virtual size_t getRecvBufferedLength() CXX11_OVERRIDE;
  155. private:
  156. // Obtains TLS record size limits. This function returns 0 if it
  157. // succeeds, or -1. status_ and state_ are updated according to the
  158. // result.
  159. int obtainTLSRecordSizes();
  160. // Ensures the buffer size so that maximum TLS record can be sent.
  161. void ensureSendBuffer();
  162. // Sends TLS record specified in sendRecordBuffers_. It uses
  163. // recordBytesSent_ to track down how many bytes have been sent.
  164. // This function returns 0 if it succeeds, or negative error codes.
  165. int sendTLSRecord();
  166. // Returns the number of bytes in the remaining TLS record size.
  167. size_t getLeftTLSRecordSize() const;
  168. std::string hostname_;
  169. sock_t sockfd_;
  170. TLSSessionSide side_;
  171. CredHandle* cred_;
  172. CtxtHandle handle_;
  173. // Buffer for already encrypted writes. This is only used in
  174. // handshake.
  175. wintls::Buffer writeBuf_;
  176. // While the sendRecordBuffers_ holds encrypted messages,
  177. // writeBuffered_ has the corresponding size of unencrypted data
  178. // used to produce the messages.
  179. size_t writeBuffered_;
  180. // Buffer for still encrypted reads
  181. wintls::Buffer readBuf_;
  182. // Buffer for already decrypted reads
  183. wintls::Buffer decBuf_;
  184. state_t state_;
  185. SECURITY_STATUS status_;
  186. // The number of maximum size for TLS record header, body, and
  187. // trailer.
  188. SecPkgContext_StreamSizes streamSizes_;
  189. // Underlying buffer for outgoing TLS record.
  190. std::vector<unsigned char> sendBuffer_;
  191. // How many bytes has been sent for current TLS record held in
  192. // sendRecordBuffers_.
  193. size_t recordBytesSent_;
  194. // This holds current outgoing TLS record.
  195. std::array<TLSBuffer, 4> sendRecordBuffers_;
  196. };
  197. } // namespace aria2
  198. #endif // TLS_SESSION_H