SecureStreamSocketImpl.h 8.0 KB

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