SecureStreamSocketImpl.h 9.3 KB

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