SecureStreamSocket.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // SecureStreamSocket.h
  3. //
  4. // $Id: //poco/Main/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h#7 $
  5. //
  6. // Library: NetSSL_OpenSSL
  7. // Package: SSLSockets
  8. // Module: SecureStreamSocket
  9. //
  10. // Definition of the SecureStreamSocket class.
  11. //
  12. // Copyright (c) 2006-2009, 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_SecureStreamSocket_INCLUDED
  38. #define NetSSL_SecureStreamSocket_INCLUDED
  39. #include "Poco/Net/NetSSL.h"
  40. #include "Poco/Net/StreamSocket.h"
  41. #include "Poco/Net/Context.h"
  42. #include "Poco/Net/X509Certificate.h"
  43. namespace Poco {
  44. namespace Net {
  45. class NetSSL_API SecureStreamSocket: public StreamSocket
  46. /// A subclass of StreamSocket for secure SSL sockets.
  47. ///
  48. /// A few notes about nonblocking IO:
  49. /// sendBytes() and receiveBytes() can return a
  50. /// negative value when using a nonblocking socket, which means
  51. /// a SSL handshake is currently in progress and more data
  52. /// needs to be read or written for the handshake to continue.
  53. /// If sendBytes() or receiveBytes() return ERR_SSL_WANT_WRITE,
  54. /// sendBytes() must be called as soon as possible (usually, after
  55. /// select() indicates that data can be written). Likewise, if
  56. /// ERR_SSL_WANT_READ is returned, receiveBytes() must be called
  57. /// as soon as data is available for reading (indicated by select()).
  58. ///
  59. /// The SSL handshake is delayed until the first sendBytes() or
  60. /// receiveBytes() operation is performed on the socket. No automatic
  61. /// post connection check (checking the peer certificate for a valid
  62. /// hostname) is performed when using nonblocking I/O.
  63. {
  64. public:
  65. enum
  66. {
  67. ERR_SSL_WANT_READ = -1,
  68. ERR_SSL_WANT_WRITE = -2
  69. };
  70. SecureStreamSocket();
  71. /// Creates an unconnected secure stream socket
  72. /// using the default client SSL context.
  73. ///
  74. /// Before sending or receiving data, the socket
  75. /// must be connected with a call to connect().
  76. explicit SecureStreamSocket(Context::Ptr pContext);
  77. /// Creates an unconnected secure stream socket
  78. /// using the given SSL context.
  79. ///
  80. /// Before sending or receiving data, the socket
  81. /// must be connected with a call to connect().
  82. explicit SecureStreamSocket(const SocketAddress& address);
  83. /// Creates a secure stream socket using the default
  84. /// client SSL context and connects it to
  85. /// the socket specified by address.
  86. SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext);
  87. /// Creates a secure stream socket using the given
  88. /// client SSL context and connects it to
  89. /// the socket specified by address.
  90. SecureStreamSocket(const SocketAddress& address, const std::string& hostName);
  91. /// Creates a secure stream socket using the default
  92. /// client SSL context and connects it to
  93. /// the socket specified by address.
  94. ///
  95. /// The given host name is used for certificate verification.
  96. SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext);
  97. /// Creates a secure stream socket using the given
  98. /// client SSL context and connects it to
  99. /// the socket specified by address.
  100. ///
  101. /// The given host name is used for certificate verification.
  102. SecureStreamSocket(const Socket& socket);
  103. /// Creates the SecureStreamSocket with the SocketImpl
  104. /// from another socket. The SocketImpl must be
  105. /// a SecureStreamSocketImpl, otherwise an InvalidArgumentException
  106. /// will be thrown.
  107. virtual ~SecureStreamSocket();
  108. /// Destroys the StreamSocket.
  109. SecureStreamSocket& operator = (const Socket& socket);
  110. /// Assignment operator.
  111. ///
  112. /// Releases the socket's SocketImpl and
  113. /// attaches the SocketImpl from the other socket and
  114. /// increments the reference count of the SocketImpl.
  115. X509Certificate peerCertificate() const;
  116. /// Returns the peer's X509 certificate.
  117. void setPeerHostName(const std::string& hostName);
  118. /// Sets the peer's host name used for certificate validation.
  119. const std::string& getPeerHostName() const;
  120. /// Returns the peer's host name used for certificate validation.
  121. static SecureStreamSocket attach(const StreamSocket& streamSocket);
  122. /// Creates a SecureStreamSocket over an existing socket
  123. /// connection. The given StreamSocket must be connected.
  124. /// A SSL handshake will be performed.
  125. static SecureStreamSocket attach(const StreamSocket& streamSocket, Context::Ptr pContext);
  126. /// Creates a SecureStreamSocket over an existing socket
  127. /// connection. The given StreamSocket must be connected.
  128. /// A SSL handshake will be performed.
  129. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName);
  130. /// Creates a SecureStreamSocket over an existing socket
  131. /// connection. The given StreamSocket must be connected.
  132. /// A SSL handshake will be performed.
  133. static SecureStreamSocket attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext);
  134. /// Creates a SecureStreamSocket over an existing socket
  135. /// connection. The given StreamSocket must be connected.
  136. /// A SSL handshake will be performed.
  137. Context::Ptr context() const;
  138. /// Returns the SSL context used by this socket.
  139. protected:
  140. SecureStreamSocket(SocketImpl* pImpl);
  141. friend class SecureServerSocket;
  142. };
  143. } } // namespace Poco::Net
  144. #endif // NetSSL_SecureStreamSocket_INCLUDED