SecureStreamSocket.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // SecureStreamSocket.h
  3. //
  4. // Library: NetSSL_OpenSSL
  5. // Package: SSLSockets
  6. // Module: SecureStreamSocket
  7. //
  8. // Definition of the SecureStreamSocket class.
  9. //
  10. // Copyright (c) 2006-2010, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef NetSSL_SecureStreamSocket_INCLUDED
  16. #define NetSSL_SecureStreamSocket_INCLUDED
  17. #include "Poco/Net/NetSSL.h"
  18. #include "Poco/Net/StreamSocket.h"
  19. #include "Poco/Net/Context.h"
  20. #include "Poco/Net/Session.h"
  21. #include "Poco/Net/X509Certificate.h"
  22. namespace Poco {
  23. namespace Net {
  24. class NetSSL_API SecureStreamSocket: public StreamSocket
  25. /// A subclass of StreamSocket for secure SSL sockets.
  26. ///
  27. /// A few notes about nonblocking IO:
  28. /// sendBytes() and receiveBytes() can return a
  29. /// negative value when using a nonblocking socket, which means
  30. /// a SSL handshake is currently in progress and more data
  31. /// needs to be read or written for the handshake to continue.
  32. /// If sendBytes() or receiveBytes() return ERR_SSL_WANT_WRITE,
  33. /// sendBytes() must be called as soon as possible (usually, after
  34. /// select() indicates that data can be written). Likewise, if
  35. /// ERR_SSL_WANT_READ is returned, receiveBytes() must be called
  36. /// as soon as data is available for reading (indicated by select()).
  37. ///
  38. /// The SSL handshake is delayed until the first sendBytes() or
  39. /// receiveBytes() operation is performed on the socket. No automatic
  40. /// post connection check (checking the peer certificate for a valid
  41. /// hostname) is performed when using nonblocking I/O. To manually
  42. /// perform peer certificate validation, call verifyPeerCertificate()
  43. /// after the SSL handshake has been completed.
  44. {
  45. public:
  46. enum
  47. {
  48. ERR_SSL_WANT_READ = -1,
  49. ERR_SSL_WANT_WRITE = -2
  50. };
  51. SecureStreamSocket();
  52. /// Creates an unconnected secure stream socket
  53. /// using the default client SSL context.
  54. ///
  55. /// Before sending or receiving data, the socket
  56. /// must be connected with a call to connect().
  57. explicit SecureStreamSocket(Context::Ptr pContext);
  58. /// Creates an unconnected secure stream socket
  59. /// using the given SSL context.
  60. ///
  61. /// Before sending or receiving data, the socket
  62. /// must be connected with a call to connect().
  63. SecureStreamSocket(Context::Ptr pContext, Session::Ptr pSession);
  64. /// Creates an unconnected secure stream socket
  65. /// using the given SSL context.
  66. ///
  67. /// Before sending or receiving data, the socket
  68. /// must be connected with a call to connect().
  69. ///
  70. /// The given Session is reused, if possible (client session
  71. /// caching is enabled for the given Context, and the server
  72. /// agrees to reuse the session).
  73. explicit SecureStreamSocket(const SocketAddress& address);
  74. /// Creates a secure stream socket using the default
  75. /// client SSL context and connects it to
  76. /// the socket specified by address.
  77. SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext);
  78. /// Creates a secure stream socket using the given
  79. /// client SSL context and connects it to
  80. /// the socket specified by address.
  81. SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext, Session::Ptr pSession);
  82. /// Creates a secure stream socket using the given
  83. /// client SSL context and connects it to
  84. /// the socket specified by address.
  85. ///
  86. /// The given Session is reused, if possible (client session
  87. /// caching is enabled for the given Context, and the server
  88. /// agrees to reuse the session).
  89. SecureStreamSocket(const SocketAddress& address, const std::string& hostName);
  90. /// Creates a secure stream socket using the default
  91. /// client SSL context and connects it to
  92. /// the socket specified by address.
  93. ///
  94. /// The given host name is used for certificate verification.
  95. SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext);
  96. /// Creates a secure stream socket using the given
  97. /// client SSL context and connects it to
  98. /// the socket specified by address.
  99. ///
  100. /// The given host name is used for certificate verification.
  101. SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext, Session::Ptr pSession);
  102. /// Creates a secure stream socket using the given
  103. /// client SSL context and connects it to
  104. /// the socket specified by address.
  105. ///
  106. /// The given host name is used for certificate verification.
  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 Socket& socket);
  112. /// Creates the SecureStreamSocket with the SocketImpl
  113. /// from another socket. The SocketImpl must be
  114. /// a SecureStreamSocketImpl, otherwise an InvalidArgumentException
  115. /// will be thrown.
  116. virtual ~SecureStreamSocket();
  117. /// Destroys the StreamSocket.
  118. SecureStreamSocket& operator = (const Socket& socket);
  119. /// Assignment operator.
  120. ///
  121. /// Releases the socket's SocketImpl and
  122. /// attaches the SocketImpl from the other socket and
  123. /// increments the reference count of the SocketImpl.
  124. bool havePeerCertificate() const;
  125. /// Returns true iff the peer has presented a
  126. /// certificate.
  127. X509Certificate peerCertificate() const;
  128. /// Returns the peer's X509 certificate.
  129. ///
  130. /// Throws a SSLException if the peer did not
  131. /// present a certificate.
  132. void setPeerHostName(const std::string& hostName);
  133. /// Sets the peer's host name used for certificate validation.
  134. const std::string& getPeerHostName() const;
  135. /// Returns the peer's host name used for certificate validation.
  136. static SecureStreamSocket attach(const StreamSocket& streamSocket);
  137. /// Creates a SecureStreamSocket over an existing socket
  138. /// connection. The given StreamSocket must be connected.
  139. /// A SSL handshake will be performed.
  140. static SecureStreamSocket attach(const StreamSocket& streamSocket, Context::Ptr pContext);
  141. /// Creates a SecureStreamSocket over an existing socket
  142. /// connection. The given StreamSocket must be connected.
  143. /// A SSL handshake will be performed.
  144. static SecureStreamSocket attach(const StreamSocket& streamSocket, Context::Ptr pContext, Session::Ptr pSession);
  145. /// Creates a SecureStreamSocket over an existing socket
  146. /// connection. The given StreamSocket must be connected.
  147. /// A SSL handshake will be performed.
  148. ///
  149. /// The given Session is reused, if possible (client session
  150. /// caching is enabled for the given Context, and the server
  151. /// agrees to reuse the session).
  152. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName);
  153. /// Creates a SecureStreamSocket over an existing socket
  154. /// connection. The given StreamSocket must be connected.
  155. /// A SSL handshake will be performed.
  156. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext);
  157. /// Creates a SecureStreamSocket over an existing socket
  158. /// connection. The given StreamSocket must be connected.
  159. /// A SSL handshake will be performed.
  160. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext, Session::Ptr pSession);
  161. /// Creates a SecureStreamSocket over an existing socket
  162. /// connection. The given StreamSocket must be connected.
  163. /// A SSL handshake will be performed.
  164. ///
  165. /// The given Session is reused, if possible (client session
  166. /// caching is enabled for the given Context, and the server
  167. /// agrees to reuse the session).
  168. Context::Ptr context() const;
  169. /// Returns the SSL context used by this socket.
  170. void setLazyHandshake(bool flag = true);
  171. /// Enable lazy SSL handshake. If enabled, the SSL handshake
  172. /// will be performed the first time date is sent or
  173. /// received over the connection.
  174. bool getLazyHandshake() const;
  175. /// Returns true if setLazyHandshake(true) has been called.
  176. void verifyPeerCertificate();
  177. /// Performs post-connect (or post-accept) peer certificate validation,
  178. /// using the peer host name set with setPeerHostName(), or the peer's
  179. /// IP address string if no peer host name has been set.
  180. ///
  181. /// Should only be used for non-blocking connections, after the
  182. /// initial SSL handshake has been performed (see completeHandshake()).
  183. void verifyPeerCertificate(const std::string& hostName);
  184. /// Performs post-connect (or post-accept) peer certificate validation
  185. /// using the given host name.
  186. ///
  187. /// Should only be used for non-blocking connections, after the
  188. /// initial SSL handshake has been performed (see completeHandshake()).
  189. int completeHandshake();
  190. /// Completes the SSL handshake.
  191. ///
  192. /// If the SSL connection was the result of an accept(),
  193. /// the server-side handshake is completed, otherwise
  194. /// a client-side handshake is performed.
  195. ///
  196. /// Returns 1 if the handshake was successful, ERR_SSL_WANT_READ or
  197. /// ERR_SSL_WANT_WRITE if more data is required to complete the
  198. /// handshake. In this case, completeHandshake() should be called
  199. /// again, after the necessary condition has been met.
  200. Session::Ptr currentSession();
  201. /// Returns the SSL session of the current connection,
  202. /// for reuse in a future connection (if session caching
  203. /// is enabled).
  204. ///
  205. /// If no connection is established, returns null.
  206. void useSession(Session::Ptr pSession);
  207. /// Sets the SSL session to use for the next
  208. /// connection. Setting a previously saved Session
  209. /// object is necessary to enable session caching.
  210. ///
  211. /// To remove the currently set session, a null pointer
  212. /// can be given.
  213. ///
  214. /// Must be called before connect() to be effective.
  215. bool sessionWasReused();
  216. /// Returns true iff a reused session was negotiated during
  217. /// the handshake.
  218. void abort();
  219. /// Aborts the SSL connection by closing the underlying
  220. /// TCP connection. No orderly SSL shutdown is performed.
  221. protected:
  222. SecureStreamSocket(SocketImpl* pImpl);
  223. friend class SecureServerSocket;
  224. };
  225. } } // namespace Poco::Net
  226. #endif // NetSSL_SecureStreamSocket_INCLUDED