SecureStreamSocket.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // SecureStreamSocket.h
  3. //
  4. // $Id: //poco/1.4/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h#2 $
  5. //
  6. // Library: NetSSL_OpenSSL
  7. // Package: SSLSockets
  8. // Module: SecureStreamSocket
  9. //
  10. // Definition of the SecureStreamSocket class.
  11. //
  12. // Copyright (c) 2006-2010, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef NetSSL_SecureStreamSocket_INCLUDED
  38. #define NetSSL_SecureStreamSocket_INCLUDED
  39. #include "Poco/Net/NetSSL.h"
  40. #include "Poco/Net/StreamSocket.h"
  41. #include "Poco/Net/Context.h"
  42. #include "Poco/Net/Session.h"
  43. #include "Poco/Net/X509Certificate.h"
  44. namespace Poco {
  45. namespace Net {
  46. class NetSSL_API SecureStreamSocket: public StreamSocket
  47. /// A subclass of StreamSocket for secure SSL sockets.
  48. ///
  49. /// A few notes about nonblocking IO:
  50. /// sendBytes() and receiveBytes() can return a
  51. /// negative value when using a nonblocking socket, which means
  52. /// a SSL handshake is currently in progress and more data
  53. /// needs to be read or written for the handshake to continue.
  54. /// If sendBytes() or receiveBytes() return ERR_SSL_WANT_WRITE,
  55. /// sendBytes() must be called as soon as possible (usually, after
  56. /// select() indicates that data can be written). Likewise, if
  57. /// ERR_SSL_WANT_READ is returned, receiveBytes() must be called
  58. /// as soon as data is available for reading (indicated by select()).
  59. ///
  60. /// The SSL handshake is delayed until the first sendBytes() or
  61. /// receiveBytes() operation is performed on the socket. No automatic
  62. /// post connection check (checking the peer certificate for a valid
  63. /// hostname) is performed when using nonblocking I/O. To manually
  64. /// perform peer certificate validation, call verifyPeerCertificate()
  65. /// after the SSL handshake has been completed.
  66. {
  67. public:
  68. enum
  69. {
  70. ERR_SSL_WANT_READ = -1,
  71. ERR_SSL_WANT_WRITE = -2
  72. };
  73. SecureStreamSocket();
  74. /// Creates an unconnected secure stream socket
  75. /// using the default client SSL context.
  76. ///
  77. /// Before sending or receiving data, the socket
  78. /// must be connected with a call to connect().
  79. explicit SecureStreamSocket(Context::Ptr pContext);
  80. /// Creates an unconnected secure stream socket
  81. /// using the given SSL context.
  82. ///
  83. /// Before sending or receiving data, the socket
  84. /// must be connected with a call to connect().
  85. SecureStreamSocket(Context::Ptr pContext, Session::Ptr pSession);
  86. /// Creates an unconnected secure stream socket
  87. /// using the given SSL context.
  88. ///
  89. /// Before sending or receiving data, the socket
  90. /// must be connected with a call to connect().
  91. ///
  92. /// The given Session is reused, if possible (client session
  93. /// caching is enabled for the given Context, and the server
  94. /// agrees to reuse the session).
  95. explicit SecureStreamSocket(const SocketAddress& address);
  96. /// Creates a secure stream socket using the default
  97. /// client SSL context and connects it to
  98. /// the socket specified by address.
  99. SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext);
  100. /// Creates a secure stream socket using the given
  101. /// client SSL context and connects it to
  102. /// the socket specified by address.
  103. SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext, Session::Ptr pSession);
  104. /// Creates a secure stream socket using the given
  105. /// client SSL context and connects it to
  106. /// the socket specified by address.
  107. ///
  108. /// The given Session is reused, if possible (client session
  109. /// caching is enabled for the given Context, and the server
  110. /// agrees to reuse the session).
  111. SecureStreamSocket(const SocketAddress& address, const std::string& hostName);
  112. /// Creates a secure stream socket using the default
  113. /// client SSL context and connects it to
  114. /// the socket specified by address.
  115. ///
  116. /// The given host name is used for certificate verification.
  117. SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext);
  118. /// Creates a secure stream socket using the given
  119. /// client SSL context and connects it to
  120. /// the socket specified by address.
  121. ///
  122. /// The given host name is used for certificate verification.
  123. SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext, Session::Ptr pSession);
  124. /// Creates a secure stream socket using the given
  125. /// client SSL context and connects it to
  126. /// the socket specified by address.
  127. ///
  128. /// The given host name is used for certificate verification.
  129. ///
  130. /// The given Session is reused, if possible (client session
  131. /// caching is enabled for the given Context, and the server
  132. /// agrees to reuse the session).
  133. SecureStreamSocket(const Socket& socket);
  134. /// Creates the SecureStreamSocket with the SocketImpl
  135. /// from another socket. The SocketImpl must be
  136. /// a SecureStreamSocketImpl, otherwise an InvalidArgumentException
  137. /// will be thrown.
  138. virtual ~SecureStreamSocket();
  139. /// Destroys the StreamSocket.
  140. SecureStreamSocket& operator = (const Socket& socket);
  141. /// Assignment operator.
  142. ///
  143. /// Releases the socket's SocketImpl and
  144. /// attaches the SocketImpl from the other socket and
  145. /// increments the reference count of the SocketImpl.
  146. bool havePeerCertificate() const;
  147. /// Returns true iff the peer has presented a
  148. /// certificate.
  149. X509Certificate peerCertificate() const;
  150. /// Returns the peer's X509 certificate.
  151. ///
  152. /// Throws a SSLException if the peer did not
  153. /// present a certificate.
  154. void setPeerHostName(const std::string& hostName);
  155. /// Sets the peer's host name used for certificate validation.
  156. const std::string& getPeerHostName() const;
  157. /// Returns the peer's host name used for certificate validation.
  158. static SecureStreamSocket attach(const StreamSocket& streamSocket);
  159. /// Creates a SecureStreamSocket over an existing socket
  160. /// connection. The given StreamSocket must be connected.
  161. /// A SSL handshake will be performed.
  162. static SecureStreamSocket attach(const StreamSocket& streamSocket, Context::Ptr pContext);
  163. /// Creates a SecureStreamSocket over an existing socket
  164. /// connection. The given StreamSocket must be connected.
  165. /// A SSL handshake will be performed.
  166. static SecureStreamSocket attach(const StreamSocket& streamSocket, Context::Ptr pContext, Session::Ptr pSession);
  167. /// Creates a SecureStreamSocket over an existing socket
  168. /// connection. The given StreamSocket must be connected.
  169. /// A SSL handshake will be performed.
  170. ///
  171. /// The given Session is reused, if possible (client session
  172. /// caching is enabled for the given Context, and the server
  173. /// agrees to reuse the session).
  174. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName);
  175. /// Creates a SecureStreamSocket over an existing socket
  176. /// connection. The given StreamSocket must be connected.
  177. /// A SSL handshake will be performed.
  178. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext);
  179. /// Creates a SecureStreamSocket over an existing socket
  180. /// connection. The given StreamSocket must be connected.
  181. /// A SSL handshake will be performed.
  182. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext, Session::Ptr pSession);
  183. /// Creates a SecureStreamSocket over an existing socket
  184. /// connection. The given StreamSocket must be connected.
  185. /// A SSL handshake will be performed.
  186. ///
  187. /// The given Session is reused, if possible (client session
  188. /// caching is enabled for the given Context, and the server
  189. /// agrees to reuse the session).
  190. Context::Ptr context() const;
  191. /// Returns the SSL context used by this socket.
  192. void setLazyHandshake(bool flag = true);
  193. /// Enable lazy SSL handshake. If enabled, the SSL handshake
  194. /// will be performed the first time date is sent or
  195. /// received over the connection.
  196. bool getLazyHandshake() const;
  197. /// Returns true if setLazyHandshake(true) has been called.
  198. void verifyPeerCertificate();
  199. /// Performs post-connect (or post-accept) peer certificate validation,
  200. /// using the peer host name set with setPeerHostName(), or the peer's
  201. /// IP address string if no peer host name has been set.
  202. ///
  203. /// Should only be used for non-blocking connections, after the
  204. /// initial SSL handshake has been performed (see completeHandshake()).
  205. void verifyPeerCertificate(const std::string& hostName);
  206. /// Performs post-connect (or post-accept) peer certificate validation
  207. /// using the given host name.
  208. ///
  209. /// Should only be used for non-blocking connections, after the
  210. /// initial SSL handshake has been performed (see completeHandshake()).
  211. int completeHandshake();
  212. /// Completes the SSL handshake.
  213. ///
  214. /// If the SSL connection was the result of an accept(),
  215. /// the server-side handshake is completed, otherwise
  216. /// a client-side handshake is performed.
  217. ///
  218. /// Returns 1 if the handshake was successful, ERR_SSL_WANT_READ or
  219. /// ERR_SSL_WANT_WRITE if more data is required to complete the
  220. /// handshake. In this case, completeHandshake() should be called
  221. /// again, after the necessary condition has been met.
  222. Session::Ptr currentSession();
  223. /// Returns the SSL session of the current connection,
  224. /// for reuse in a future connection (if session caching
  225. /// is enabled).
  226. ///
  227. /// If no connection is established, returns null.
  228. void useSession(Session::Ptr pSession);
  229. /// Sets the SSL session to use for the next
  230. /// connection. Setting a previously saved Session
  231. /// object is necessary to enable session caching.
  232. ///
  233. /// To remove the currently set session, a null pointer
  234. /// can be given.
  235. ///
  236. /// Must be called before connect() to be effective.
  237. bool sessionWasReused();
  238. /// Returns true iff a reused session was negotiated during
  239. /// the handshake.
  240. void abort();
  241. /// Aborts the SSL connection by closing the underlying
  242. /// TCP connection. No orderly SSL shutdown is performed.
  243. protected:
  244. SecureStreamSocket(SocketImpl* pImpl);
  245. friend class SecureServerSocket;
  246. };
  247. } } // namespace Poco::Net
  248. #endif // NetSSL_SecureStreamSocket_INCLUDED