2525#include " Poco/Net/Context.h"
2626#include " Poco/Net/AutoSecBufferDesc.h"
2727#include " Poco/Net/X509Certificate.h"
28- #include " Poco/SharedPtr.h"
2928#include " Poco/Buffer.h"
3029#include < winsock2.h>
3130#include < windows.h>
@@ -46,32 +45,11 @@ class NetSSL_Win_API SecureSocketImpl
4645 // / The SocketImpl for SecureStreamSocket.
4746{
4847public:
49- enum
50- {
51- IO_BUFFER_SIZE = 65536 ,
52- TIMEOUT_MILLISECS = 200
53- };
54-
5548 enum Mode
5649 {
5750 MODE_CLIENT,
5851 MODE_SERVER
5952 };
60-
61- enum State
62- {
63- ST_INITIAL = 0 ,
64- ST_CONNECTING,
65- ST_CLIENTHANDSHAKESTART,
66- ST_CLIENTHANDSHAKECONDREAD,
67- ST_CLIENTHANDSHAKEINCOMPLETE,
68- ST_CLIENTHANDSHAKEOK,
69- ST_CLIENTHANDSHAKEEXTERROR,
70- ST_CLIENTHANDSHAKECONTINUE,
71- ST_VERIFY,
72- ST_DONE,
73- ST_ERROR
74- };
7553
7654 SecureSocketImpl (Poco::AutoPtr<SocketImpl> pSocketImpl, Context::Ptr pContext);
7755 // / Creates the SecureSocketImpl.
@@ -160,8 +138,14 @@ class NetSSL_Win_API SecureSocketImpl
160138 const std::string& getPeerHostName () const ;
161139 // / Returns the peer host name.
162140
163- State getState () const ;
164- // / Returns the state of the 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+
146+ void verifyPeerCertificate (const std::string& hostName);
147+ // / Performs post-connect (or post-accept) peer certificate validation
148+ // / using the given peer host name.
165149
166150 Context::Ptr context () const ;
167151 // / Returns the Context.
@@ -172,23 +156,40 @@ class NetSSL_Win_API SecureSocketImpl
172156 poco_socket_t sockfd ();
173157 // / Returns the underlying socket descriptor.
174158
159+ int available () const ;
160+ // / Returns the number of bytes available in the buffer.
161+
175162protected:
176- int sendRawBytes (const void * buffer, int length, int flags = 0 );
177- // / Sends the data in clearText
163+ enum
164+ {
165+ IO_BUFFER_SIZE = 32768 ,
166+ TIMEOUT_MILLISECS = 200
167+ };
178168
179- int receiveRawBytes (void * buffer, int length, int flags = 0 );
180- // / Receives raw data from the socket and stores it
181- // / in buffer. Up to length bytes are received.
182- // /
183- // / Returns the number of bytes received.
169+ enum State
170+ {
171+ ST_INITIAL = 0 ,
172+ ST_CONNECTING,
173+ ST_CLIENTHANDSHAKESTART,
174+ ST_CLIENTHANDSHAKECONDREAD,
175+ ST_CLIENTHANDSHAKEINCOMPLETE,
176+ ST_CLIENTHANDSHAKEOK,
177+ ST_CLIENTHANDSHAKEEXTERROR,
178+ ST_CLIENTHANDSHAKECONTINUE,
179+ ST_VERIFY,
180+ ST_DONE,
181+ ST_ERROR
182+ };
184183
184+ int sendRawBytes (const void * buffer, int length, int flags = 0 );
185+ int receiveRawBytes (void * buffer, int length, int flags = 0 );
185186 void clientConnectVerify ();
186187 void sendInitialTokenOutBuffer ();
187188 void performServerHandshake ();
188189 bool serverHandshakeLoop (PCtxtHandle phContext, PCredHandle phCred, bool requireClientAuth, bool doInitialRead, bool newContext);
189- void clientVerifyCertificate (PCCERT_CONTEXT pServerCert, const std::string& serverName, DWORD dwCertFlags );
190+ void clientVerifyCertificate (const std::string& hostName );
190191 void verifyCertificateChainClient (PCCERT_CONTEXT pServerCert, PCCERT_CHAIN_CONTEXT pChainContext);
191- void serverVerifyCertificate (PCCERT_CONTEXT pPeerCert, DWORD dwCertFlags );
192+ void serverVerifyCertificate ();
192193 LONG serverDisconnect (PCredHandle phCreds, CtxtHandle* phContext);
193194 LONG clientDisconnect (PCredHandle phCreds, CtxtHandle* phContext);
194195 bool loadSecurityLibrary ();
@@ -201,8 +202,8 @@ class NetSSL_Win_API SecureSocketImpl
201202 void performInitialClientHandshake ();
202203 SECURITY_STATUS performClientHandshakeLoop ();
203204 void performClientHandshakeLoopIncompleteMessage ();
204- void performClientHandshakeLoopCondRead ();
205- void performClientHandshakeLoopRead ();
205+ void performClientHandshakeLoopCondReceive ();
206+ void performClientHandshakeLoopReceive ();
206207 void performClientHandshakeLoopOK ();
207208 void performClientHandshakeLoopInit ();
208209 void performClientHandshakeExtraBuffer ();
@@ -219,7 +220,9 @@ class NetSSL_Win_API SecureSocketImpl
219220 void completeHandshake ();
220221 static int lastError ();
221222 void stateMachine ();
223+ State getState () const ;
222224 void setState (State st);
225+ static bool isLocalHost (const std::string& hostName);
223226
224227private:
225228 SecureSocketImpl (const SecureSocketImpl&);
@@ -231,34 +234,30 @@ class NetSSL_Win_API SecureSocketImpl
231234 std::string _peerHostName;
232235 bool _useMachineStore;
233236 bool _clientAuthRequired;
234- PCCERT_CONTEXT _pServerCertificate;
237+
238+ SecurityFunctionTableW& _securityFunctions;
239+
240+ PCCERT_CONTEXT _pOwnCertificate;
235241 PCCERT_CONTEXT _pPeerCertificate;
236242
237243 CredHandle _hCreds;
238244 CtxtHandle _hContext;
239- DWORD _clientFlags;
240- DWORD _serverFlags;
241-
242- SecurityFunctionTableW& _securityFunctions;
243-
244- BYTE* _pReceiveBuffer;
245- DWORD _receiveBufferSize;
245+ DWORD _contextFlags;
246246
247- BYTE* _pIOBuffer;
248- DWORD _ioBufferOffset;
247+ Poco::Buffer<BYTE> _overflowBuffer;
248+ Poco::Buffer<BYTE> _sendBuffer;
249+ Poco::Buffer<BYTE> _recvBuffer;
250+ DWORD _recvBufferOffset;
249251 DWORD _ioBufferSize;
250252
251253 SecPkgContext_StreamSizes _streamSizes;
252254 AutoSecBufferDesc<1 > _outSecBuffer;
253255 AutoSecBufferDesc<2 > _inSecBuffer;
254- Poco::SharedPtr<Poco::Buffer<BYTE> > _pSendBuffer;
255256 SecBuffer _extraSecBuffer;
256- bool _doReadFirst;
257- DWORD _bytesRead;
258257 SECURITY_STATUS _securityStatus;
259258 State _state;
260259 DWORD _outFlags;
261- std::string _hostName ;
260+ bool _needData ;
262261 bool _needHandshake;
263262
264263 friend class SecureStreamSocketImpl ;
0 commit comments