SecureSocketImpl.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // SecureSocketImpl.h
  3. //
  4. // $Id: //poco/1.4/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h#2 $
  5. //
  6. // Library: NetSSL_OpenSSL
  7. // Package: SSLSockets
  8. // Module: SecureSocketImpl
  9. //
  10. // Definition of the SecureSocketImpl 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_SecureSocketImpl_INCLUDED
  38. #define NetSSL_SecureSocketImpl_INCLUDED
  39. #include "Poco/Net/NetSSL.h"
  40. #include "Poco/Net/SocketImpl.h"
  41. #include "Poco/Net/Context.h"
  42. #include "Poco/Net/X509Certificate.h"
  43. #include "Poco/Net/Session.h"
  44. #include <openssl/bio.h>
  45. #include <openssl/ssl.h>
  46. namespace Poco {
  47. namespace Net {
  48. class HostEntry;
  49. class NetSSL_API SecureSocketImpl
  50. /// The SocketImpl for SecureStreamSocket.
  51. {
  52. public:
  53. SecureSocketImpl(Poco::AutoPtr<SocketImpl> pSocketImpl, Context::Ptr pContext);
  54. /// Creates the SecureSocketImpl using an already
  55. /// connected stream socket.
  56. virtual ~SecureSocketImpl();
  57. /// Destroys the SecureSocketImpl.
  58. SocketImpl* acceptConnection(SocketAddress& clientAddr);
  59. /// Get the next completed connection from the
  60. /// socket's completed connection queue.
  61. ///
  62. /// If the queue is empty, waits until a connection
  63. /// request completes.
  64. ///
  65. /// Returns a new SSL socket for the connection
  66. /// with the client.
  67. ///
  68. /// The client socket's address is returned in clientAddr.
  69. void connect(const SocketAddress& address, bool performHandshake);
  70. /// Initializes the socket and establishes a secure connection to
  71. /// the TCP server at the given address.
  72. ///
  73. /// If performHandshake is true, the SSL handshake is performed immediately
  74. /// after establishing the connection. Otherwise, the handshake is performed
  75. /// the first time sendBytes(), receiveBytes() or completeHandshake() is called.
  76. void connect(const SocketAddress& address, const Poco::Timespan& timeout, bool performHandshake);
  77. /// Initializes the socket, sets the socket timeout and
  78. /// establishes a secure connection to the TCP server at the given address.
  79. ///
  80. /// If performHandshake is true, the SSL handshake is performed immediately
  81. /// after establishing the connection. Otherwise, the handshake is performed
  82. /// the first time sendBytes(), receiveBytes() or completeHandshake() is called.
  83. void connectNB(const SocketAddress& address);
  84. /// Initializes the socket and establishes a secure connection to
  85. /// the TCP server at the given address. Prior to opening the
  86. /// connection the socket is set to nonblocking mode.
  87. void bind(const SocketAddress& address, bool reuseAddress = false);
  88. /// Bind a local address to the socket.
  89. ///
  90. /// This is usually only done when establishing a server
  91. /// socket. SSL clients should not bind a socket to a
  92. /// specific address.
  93. ///
  94. /// If reuseAddress is true, sets the SO_REUSEADDR
  95. /// socket option.
  96. void listen(int backlog = 64);
  97. /// Puts the socket into listening state.
  98. ///
  99. /// The socket becomes a passive socket that
  100. /// can accept incoming connection requests.
  101. ///
  102. /// The backlog argument specifies the maximum
  103. /// number of connections that can be queued
  104. /// for this socket.
  105. void shutdown();
  106. /// Shuts down the connection by attempting
  107. /// an orderly SSL shutdown, then actually
  108. /// shutting down the TCP connection.
  109. void close();
  110. /// Close the socket.
  111. void abort();
  112. /// Aborts the connection by closing the
  113. /// underlying TCP connection. No orderly SSL shutdown
  114. /// is performed.
  115. int sendBytes(const void* buffer, int length, int flags = 0);
  116. /// Sends the contents of the given buffer through
  117. /// the socket. Any specified flags are ignored.
  118. ///
  119. /// Returns the number of bytes sent, which may be
  120. /// less than the number of bytes specified.
  121. int receiveBytes(void* buffer, int length, int flags = 0);
  122. /// Receives data from the socket and stores it
  123. /// in buffer. Up to length bytes are received.
  124. ///
  125. /// Returns the number of bytes received.
  126. int available() const;
  127. /// Returns the number of bytes available from the
  128. /// SSL buffer for immediate reading.
  129. int completeHandshake();
  130. /// Completes the SSL handshake.
  131. ///
  132. /// If the SSL connection was the result of an accept(),
  133. /// the server-side handshake is completed, otherwise
  134. /// a client-side handshake is performed.
  135. poco_socket_t sockfd();
  136. /// Returns the underlying socket descriptor.
  137. X509* peerCertificate() const;
  138. /// Returns the peer's certificate.
  139. Context::Ptr context() const;
  140. /// Returns the SSL context used for this socket.
  141. void verifyPeerCertificate();
  142. /// Performs post-connect (or post-accept) peer certificate validation,
  143. /// using the peer host name set with setPeerHostName(), or the peer's
  144. /// IP address string if no peer host name has been set.
  145. void verifyPeerCertificate(const std::string& hostName);
  146. /// Performs post-connect (or post-accept) peer certificate validation
  147. /// using the given peer host name.
  148. void setPeerHostName(const std::string& hostName);
  149. /// Sets the peer host name for certificate validation purposes.
  150. const std::string& getPeerHostName() const;
  151. /// Returns the peer host name.
  152. Session::Ptr currentSession();
  153. /// Returns the SSL session of the current connection,
  154. /// for reuse in a future connection (if session caching
  155. /// is enabled).
  156. ///
  157. /// If no connection is established, returns null.
  158. void useSession(Session::Ptr pSession);
  159. /// Sets the SSL session to use for the next
  160. /// connection. Setting a previously saved Session
  161. /// object is necessary to enable session caching.
  162. ///
  163. /// To remove the currently set session, a null pointer
  164. /// can be given.
  165. ///
  166. /// Must be called before connect() to be effective.
  167. bool sessionWasReused();
  168. /// Returns true iff a reused session was negotiated during
  169. /// the handshake.
  170. protected:
  171. void acceptSSL();
  172. /// Performs a server-side SSL handshake and certificate verification.
  173. void connectSSL(bool performHandshake);
  174. /// Performs a client-side SSL handshake and establishes a secure
  175. /// connection over an already existing TCP connection.
  176. long verifyPeerCertificateImpl(const std::string& hostName);
  177. /// Performs post-connect (or post-accept) peer certificate validation.
  178. static bool isLocalHost(const std::string& hostName);
  179. /// Returns true iff the given host name is the local host
  180. /// (either "localhost" or "127.0.0.1").
  181. int handleError(int rc);
  182. /// Handles an SSL error by throwing an appropriate exception.
  183. void reset();
  184. /// Prepares the socket for re-use.
  185. ///
  186. /// After closing and resetting a socket, the socket can
  187. /// be used for a new connection.
  188. ///
  189. /// Note that simply closing a socket is not sufficient
  190. /// to be able to re-use it again.
  191. private:
  192. SecureSocketImpl(const SecureSocketImpl&);
  193. SecureSocketImpl& operator = (const SecureSocketImpl&);
  194. SSL* _pSSL;
  195. Poco::AutoPtr<SocketImpl> _pSocket;
  196. Context::Ptr _pContext;
  197. bool _needHandshake;
  198. std::string _peerHostName;
  199. Session::Ptr _pSession;
  200. friend class SecureStreamSocketImpl;
  201. };
  202. //
  203. // inlines
  204. //
  205. inline poco_socket_t SecureSocketImpl::sockfd()
  206. {
  207. return _pSocket->sockfd();
  208. }
  209. inline Context::Ptr SecureSocketImpl::context() const
  210. {
  211. return _pContext;
  212. }
  213. inline const std::string& SecureSocketImpl::getPeerHostName() const
  214. {
  215. return _peerHostName;
  216. }
  217. } } // namespace Poco::Net
  218. #endif // NetSSL_SecureSocketImpl_INCLUDED