9 #include "../mbedTLS++/X509Cert.h"
10 #include "../mbedTLS++/CryptoKey.h"
17 m_Callbacks(
std::move(a_Callbacks))
26 m_Callbacks(
std::move(a_Callbacks)),
27 m_Server(
std::move(a_ServerHandle))
61 return link->Send(a_Data);
78 return link->GetLocalIP();
95 return link->GetLocalPort();
112 return link->GetRemoteIP();
129 return link->GetRemotePort();
168 const AString & a_OwnPrivKeyData,
169 const AString & a_OwnPrivKeyPassword,
170 const AString & a_TrustedRootCAs
177 if (!a_OwnCertData.empty())
179 ownCert = std::make_shared<cX509Cert>();
180 auto res = ownCert->Parse(a_OwnCertData.data(), a_OwnCertData.size());
183 return fmt::format(FMT_STRING(
"Cannot parse client certificate: -0x{:x}"), -res);
187 if (!a_OwnPrivKeyData.empty())
189 ownPrivKey = std::make_shared<cCryptoKey>();
190 auto res = ownPrivKey->ParsePrivate(a_OwnPrivKeyData.data(), a_OwnPrivKeyData.size(), a_OwnPrivKeyPassword);
193 return fmt::format(FMT_STRING(
"Cannot parse client private key: -0x{:x}"), -res);
198 if (!a_TrustedRootCAs.empty())
200 trustedRootCAs = std::make_shared<cX509Cert>();
201 auto res = trustedRootCAs->Parse(a_TrustedRootCAs.data(), a_TrustedRootCAs.size());
204 return fmt::format(
"Cannot parse trusted root CAs: {}", res);
207 return link->StartTLSClient(ownCert, ownPrivKey, trustedRootCAs);
218 const AString & a_OwnPrivKeyData,
219 const AString & a_OwnPrivKeyPassword,
227 auto OwnCert = std::make_shared<cX509Cert>();
228 int res = OwnCert->Parse(a_OwnCertData.data(), a_OwnCertData.size());
231 return fmt::format(FMT_STRING(
"Cannot parse server certificate: -0x{:x}"), -res);
233 auto OwnPrivKey = std::make_shared<cCryptoKey>();
234 res = OwnPrivKey->ParsePrivate(a_OwnPrivKeyData.data(), a_OwnPrivKeyData.size(), a_OwnPrivKeyPassword);
237 return fmt::format(FMT_STRING(
"Cannot parse server private key: -0x{:x}"), -res);
240 return link->StartTLSServer(OwnCert, OwnPrivKey, a_StartTLSData);
259 if (Server !=
nullptr)
261 Server->RemoveLink(
this);
302 m_Callbacks->CallTableFn(
"OnError",
this, a_ErrorCode, a_ErrorMsg);
std::weak_ptr< cLuaServerHandle > cLuaServerHandleWPtr
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
std::shared_ptr< cX509Cert > cX509CertPtr
std::shared_ptr< cTCPLink > cTCPLinkPtr
std::unique_ptr< cTableRef > cTableRefPtr
UInt16 GetRemotePort(void) const
Returns the port used by the remote endpoint of the connection.
cLuaServerHandleWPtr m_Server
The server that is responsible for this link, if any.
virtual void OnReceivedData(const char *a_Data, size_t a_Length) override
Called when there's data incoming from the remote peer.
AString GetLocalIP(void) const
Returns the IP address of the local endpoint of the connection.
cLuaTCPLink(cLuaState::cTableRefPtr &&a_Callbacks)
Creates a new instance of the link, wrapping the callbacks that are in the specified table.
bool Send(const AString &a_Data)
Sends the data contained in the string to the remote peer.
AString StartTLSClient(const AString &a_OwnCertData, const AString &a_OwnPrivKeyData, const AString &a_OwnPrivKeyPassword, const AString &a_TrustedRootCAs)
Starts a TLS handshake as a client connection.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when an error is detected on the connection.
virtual void OnRemoteClosed(void) override
Called when the remote end closes the connection.
void Close(void)
Drops the connection without any more processing.
void ReceivedCleartextData(const char *a_Data, size_t a_NumBytes)
Called by the SSL context when there's incoming data available in the cleartext.
AString StartTLSServer(const AString &a_OwnCertData, const AString &a_OwnPrivKeyData, const AString &a_OwnPrivKeyPassword, const AString &a_StartTLSData)
Starts a TLS handshake as a server connection.
void Terminated(void)
Common code called when the link is considered as terminated.
cLuaState::cTableRefPtr m_Callbacks
The Lua table that holds the callbacks to be invoked.
virtual void OnConnected(cTCPLink &a_Link) override
Called when the Connect call succeeds.
AString GetRemoteIP(void) const
Returns the IP address of the remote endpoint of the connection.
void Shutdown(void)
Closes the link gracefully.
virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
Called when the cTCPLink for the connection is created.
cTCPLinkPtr m_Link
The underlying link representing the connection.
UInt16 GetLocalPort(void) const
Returns the port used by the local endpoint of the connection.
virtual ~cLuaTCPLink() override
Interface that provides the methods available on a single TCP connection.