2017-09-19 04:34:08 -04:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
// BlockingSslClientSocket.h
|
|
|
|
|
|
|
|
// Declares the cBlockingSslClientSocket class representing a blocking TCP socket with client SSL encryption over it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../OSSupport/Network.h"
|
2014-04-27 16:27:53 -04:00
|
|
|
#include "CallbackSslContext.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cBlockingSslClientSocket :
|
|
|
|
protected cCallbackSslContext::cDataCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cBlockingSslClientSocket(void);
|
2015-05-22 18:54:32 -04:00
|
|
|
|
2017-05-20 02:16:28 -04:00
|
|
|
virtual ~cBlockingSslClientSocket(void) override
|
2015-05-22 18:54:32 -04:00
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Connects to the specified server and performs SSL handshake.
|
|
|
|
Returns true if successful, false on failure. Sets internal error text on failure. */
|
|
|
|
bool Connect(const AString & a_ServerName, UInt16 a_Port);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Sends the specified data over the connection.
|
|
|
|
Returns true if successful, false on failure. Sets the internal error text on failure. */
|
|
|
|
bool Send(const void * a_Data, size_t a_NumBytes);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Receives data from the connection.
|
|
|
|
Blocks until there is any data available, then returns as much as possible.
|
|
|
|
Returns the number of bytes actually received, negative number on failure.
|
|
|
|
Sets the internal error text on failure. */
|
|
|
|
int Receive(void * a_Data, size_t a_MaxBytes);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Disconnects the connection gracefully, if possible.
|
|
|
|
Note that this also frees the internal SSL context, so all the certificates etc. are lost. */
|
|
|
|
void Disconnect(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-08-30 10:00:06 -04:00
|
|
|
/** Sets the Expected peer name.
|
|
|
|
Needs to be used before calling Connect().
|
|
|
|
\param a_ExpectedPeerName Name that we expect to receive in the SSL peer's cert; verification will fail if
|
|
|
|
the presented name is different (possible MITM). */
|
|
|
|
void SetExpectedPeerName(AString a_ExpectedPeerName);
|
|
|
|
|
|
|
|
/** Set the config to be used by the SSL context.
|
|
|
|
Config must not be modified after calling connect. */
|
|
|
|
void SetSslConfig(std::shared_ptr<const cSslConfig> a_Config);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Returns the text of the last error that has occurred in this instance. */
|
|
|
|
const AString & GetLastErrorText(void) const { return m_LastErrorText; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
protected:
|
2015-01-24 17:17:13 -05:00
|
|
|
friend class cBlockingSslClientSocketConnectCallbacks;
|
|
|
|
friend class cBlockingSslClientSocketLinkCallbacks;
|
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** The SSL context used for the socket */
|
|
|
|
cCallbackSslContext m_Ssl;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** The underlying socket to the SSL server */
|
2015-01-24 17:17:13 -05:00
|
|
|
cTCPLinkPtr m_Socket;
|
|
|
|
|
|
|
|
/** The object used to signal state changes in the socket (the cause of the blocking). */
|
|
|
|
cEvent m_Event;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-08-30 10:00:06 -04:00
|
|
|
/** The configuration to be used by the SSL context. Set by SetSslConfig(). */
|
|
|
|
std::shared_ptr<const cSslConfig> m_Config;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-08-30 10:00:06 -04:00
|
|
|
/** The expected SSL peer's name, if we are to verify the cert strictly. Set by SetExpectedPeerName(). */
|
2014-04-27 16:27:53 -04:00
|
|
|
AString m_ExpectedPeerName;
|
2015-01-24 17:17:13 -05:00
|
|
|
|
|
|
|
/** The hostname to which the socket is connecting (stored for error reporting). */
|
|
|
|
AString m_ServerName;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Text of the last error that has occurred. */
|
|
|
|
AString m_LastErrorText;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
/** Set to true if the connection established successfully. */
|
2015-12-30 07:00:02 -05:00
|
|
|
std::atomic<bool> m_IsConnected;
|
2015-01-24 17:17:13 -05:00
|
|
|
|
|
|
|
/** Protects m_IncomingData against multithreaded access. */
|
|
|
|
cCriticalSection m_CSIncomingData;
|
|
|
|
|
|
|
|
/** Buffer for the data incoming on the network socket.
|
|
|
|
Protected by m_CSIncomingData. */
|
|
|
|
AString m_IncomingData;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2015-01-24 17:17:13 -05:00
|
|
|
/** Called when the connection is established successfully. */
|
|
|
|
void OnConnected(void);
|
|
|
|
|
|
|
|
/** Called when an error occurs while connecting the socket. */
|
|
|
|
void OnConnectError(const AString & a_ErrorMsg);
|
|
|
|
|
|
|
|
/** Called when there's incoming data from the socket. */
|
|
|
|
void OnReceivedData(const char * a_Data, size_t a_Size);
|
|
|
|
|
|
|
|
/** Called when the link for the connection is created. */
|
|
|
|
void SetLink(cTCPLinkPtr a_Link);
|
|
|
|
|
|
|
|
/** Called when the link is disconnected, either gracefully or by an error. */
|
|
|
|
void OnDisconnected(void);
|
|
|
|
|
2014-04-27 16:27:53 -04:00
|
|
|
// cCallbackSslContext::cDataCallbacks overrides:
|
|
|
|
virtual int ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) override;
|
|
|
|
virtual int SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes) override;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|