SecureStreamSocketImpl.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // SecureStreamSocketImpl.h
  3. //
  4. // Library: NetSSL_OpenSSL
  5. // Package: SSLSockets
  6. // Module: SecureStreamSocketImpl
  7. //
  8. // Definition of the SecureStreamSocketImpl 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_SecureStreamSocketImpl_INCLUDED
  16. #define NetSSL_SecureStreamSocketImpl_INCLUDED
  17. #include "Poco/Net/NetSSL.h"
  18. #include "Poco/Net/SecureSocketImpl.h"
  19. #include "Poco/Net/StreamSocketImpl.h"
  20. #include "Poco/Net/Context.h"
  21. #include "Poco/Net/X509Certificate.h"
  22. namespace Poco {
  23. namespace Net {
  24. class NetSSL_API SecureStreamSocketImpl: public StreamSocketImpl
  25. /// This class implements a SSL stream socket.
  26. {
  27. public:
  28. SecureStreamSocketImpl(Context::Ptr pContext);
  29. /// Creates the SecureStreamSocketImpl.
  30. SecureStreamSocketImpl(StreamSocketImpl* pStreamSocket, Context::Ptr pContext);
  31. /// Creates the SecureStreamSocketImpl.
  32. void setSendTimeout(const Poco::Timespan& timeout);
  33. void setReceiveTimeout(const Poco::Timespan& timeout);
  34. SocketImpl* acceptConnection(SocketAddress& clientAddr);
  35. /// Not supported by a SecureStreamSocket.
  36. ///
  37. /// Throws a Poco::InvalidAccessException.
  38. void connect(const SocketAddress& address);
  39. /// Initializes the socket and establishes a connection to
  40. /// the TCP server at the given address.
  41. ///
  42. /// Can also be used for UDP sockets. In this case, no
  43. /// connection is established. Instead, incoming and outgoing
  44. /// packets are restricted to the specified address.
  45. void connect(const SocketAddress& address, const Poco::Timespan& timeout);
  46. /// Initializes the socket, sets the socket timeout and
  47. /// establishes a connection to the TCP server at the given address.
  48. void connectNB(const SocketAddress& address);
  49. /// Initializes the socket and establishes a connection to
  50. /// the TCP server at the given address. Prior to opening the
  51. /// connection the socket is set to nonblocking mode.
  52. void bind(const SocketAddress& address, bool reuseAddress = false, bool reusePort = false);
  53. /// Not supported by a SecureStreamSocket.
  54. ///
  55. /// Throws a Poco::InvalidAccessException.
  56. void listen(int backlog = 64);
  57. /// Not supported by a SecureStreamSocket.
  58. ///
  59. /// Throws a Poco::InvalidAccessException.
  60. void close();
  61. /// Close the socket.
  62. int sendBytes(const void* buffer, int length, int flags = 0);
  63. /// Sends the contents of the given buffer through
  64. /// the socket. Any specified flags are ignored.
  65. ///
  66. /// Returns the number of bytes sent, which may be
  67. /// less than the number of bytes specified.
  68. int receiveBytes(void* buffer, int length, int flags = 0);
  69. /// Receives data from the socket and stores it
  70. /// in buffer. Up to length bytes are received.
  71. ///
  72. /// Returns the number of bytes received.
  73. int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
  74. /// Not supported by a SecureStreamSocket.
  75. ///
  76. /// Throws a Poco::InvalidAccessException.
  77. int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
  78. /// Not supported by a SecureStreamSocket.
  79. ///
  80. /// Throws a Poco::InvalidAccessException.
  81. void sendUrgent(unsigned char data);
  82. /// Not supported by a SecureStreamSocket.
  83. ///
  84. /// Throws a Poco::InvalidAccessException.
  85. int available();
  86. /// Returns the number of bytes available that can be read
  87. /// without causing the socket to block.
  88. ///
  89. /// For an SSL connection, returns the number of bytes that
  90. /// can be read from the currently buffered SSL record,
  91. /// before a new record is read from the underlying socket.
  92. void shutdownReceive();
  93. /// Shuts down the receiving part of the socket connection.
  94. ///
  95. /// Since SSL does not support a half shutdown, this does
  96. /// nothing.
  97. void shutdownSend();
  98. /// Shuts down the receiving part of the socket connection.
  99. ///
  100. /// Since SSL does not support a half shutdown, this does
  101. /// nothing.
  102. void shutdown();
  103. /// Shuts down the SSL connection.
  104. void abort();
  105. /// Aborts the connection by closing the underlying
  106. /// TCP connection. No orderly SSL shutdown is performed.
  107. bool secure() const;
  108. /// Returns true iff the socket's connection is secure
  109. /// (using SSL or TLS).
  110. void setPeerHostName(const std::string& hostName);
  111. /// Sets the peer host name for certificate validation purposes.
  112. const std::string& getPeerHostName() const;
  113. /// Returns the peer host name.
  114. bool havePeerCertificate() const;
  115. /// Returns true iff the peer has presented a
  116. /// certificate.
  117. X509Certificate peerCertificate() const;
  118. /// Returns the peer's X509 certificate.
  119. ///
  120. /// Throws a SSLException if the peer did not
  121. /// present a certificate.
  122. Context::Ptr context() const;
  123. /// Returns the SSL context used by this socket.
  124. void setLazyHandshake(bool flag = true);
  125. /// Enable lazy SSL handshake. If enabled, the SSL handshake
  126. /// will be performed the first time date is sent or
  127. /// received over the connection.
  128. bool getLazyHandshake() const;
  129. /// Returns true if setLazyHandshake(true) has been called.
  130. void verifyPeerCertificate();
  131. /// Performs post-connect (or post-accept) peer certificate validation,
  132. /// using the peer's IP address as host name.
  133. void verifyPeerCertificate(const std::string& hostName);
  134. /// Performs post-connect (or post-accept) peer certificate validation
  135. /// using the given host name.
  136. int completeHandshake();
  137. /// Completes the SSL handshake.
  138. ///
  139. /// If the SSL connection was the result of an accept(),
  140. /// the server-side handshake is completed, otherwise
  141. /// a client-side handshake is performed.
  142. Session::Ptr currentSession();
  143. /// Returns the SSL session of the current connection,
  144. /// for reuse in a future connection (if session caching
  145. /// is enabled).
  146. ///
  147. /// If no connection is established, returns null.
  148. void useSession(Session::Ptr pSession);
  149. /// Sets the SSL session to use for the next
  150. /// connection. Setting a previously saved Session
  151. /// object is necessary to enable session caching.
  152. ///
  153. /// To remove the currently set session, a null pointer
  154. /// can be given.
  155. ///
  156. /// Must be called before connect() to be effective.
  157. bool sessionWasReused();
  158. /// Returns true iff a reused session was negotiated during
  159. /// the handshake.
  160. protected:
  161. void acceptSSL();
  162. /// Performs a SSL server-side handshake.
  163. void connectSSL();
  164. /// Performs a SSL client-side handshake on an already connected TCP socket.
  165. ~SecureStreamSocketImpl();
  166. /// Destroys the SecureStreamSocketImpl.
  167. static int lastError();
  168. static void error();
  169. static void error(const std::string& arg);
  170. static void error(int code);
  171. static void error(int code, const std::string& arg);
  172. private:
  173. SecureStreamSocketImpl(const SecureStreamSocketImpl&);
  174. SecureStreamSocketImpl& operator = (const SecureStreamSocketImpl&);
  175. StreamSocketImpl * underlying_socket;
  176. SecureSocketImpl _impl;
  177. bool _lazyHandshake;
  178. friend class SecureSocketImpl;
  179. friend class SecureStreamSocket;
  180. };
  181. //
  182. // inlines
  183. //
  184. inline const std::string& SecureStreamSocketImpl::getPeerHostName() const
  185. {
  186. return _impl.getPeerHostName();
  187. }
  188. inline void SecureStreamSocketImpl::setPeerHostName(const std::string& peerHostName)
  189. {
  190. _impl.setPeerHostName(peerHostName);
  191. }
  192. inline Context::Ptr SecureStreamSocketImpl::context() const
  193. {
  194. return _impl.context();
  195. }
  196. inline Session::Ptr SecureStreamSocketImpl::currentSession()
  197. {
  198. return _impl.currentSession();
  199. }
  200. inline void SecureStreamSocketImpl::useSession(Session::Ptr pSession)
  201. {
  202. _impl.useSession(pSession);
  203. }
  204. inline bool SecureStreamSocketImpl::sessionWasReused()
  205. {
  206. return _impl.sessionWasReused();
  207. }
  208. inline int SecureStreamSocketImpl::lastError()
  209. {
  210. return SocketImpl::lastError();
  211. }
  212. inline void SecureStreamSocketImpl::error()
  213. {
  214. return SocketImpl::error();
  215. }
  216. inline void SecureStreamSocketImpl::error(const std::string& arg)
  217. {
  218. return SocketImpl::error(arg);
  219. }
  220. inline void SecureStreamSocketImpl::error(int code)
  221. {
  222. return SocketImpl::error(code);
  223. }
  224. inline void SecureStreamSocketImpl::error(int code, const std::string& arg)
  225. {
  226. return SocketImpl::error(code, arg);
  227. }
  228. } } // namespace Poco::Net
  229. #endif // NetSSL_SecureStreamSocketImpl_INCLUDED