2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: TLSContext.cpp
|
|
|
|
Date: 2022-8-24
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "TLS.hpp"
|
|
|
|
#include "TLSContext.hpp"
|
2022-12-12 23:50:05 +00:00
|
|
|
#include <Source/IO/Protocol/AuProtocolStack.hpp>
|
2022-08-28 19:02:06 +00:00
|
|
|
#include <Aurora/IO/Net/NetExperimental.hpp>
|
|
|
|
#include <Source/IO/Net/AuNetSocket.hpp>
|
2022-11-18 04:15:05 +00:00
|
|
|
#include <Source/Crypto/X509/x509.hpp>
|
|
|
|
#include "TLSCertificateChain.hpp"
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO::TLS
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
mbedtls_entropy_context gEntropy;
|
|
|
|
mbedtls_ctr_drbg_context gCtrDrbg;
|
2022-11-18 21:03:11 +00:00
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
static bool gTlsReady {};
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
AuString TLSErrorToString(int iError)
|
|
|
|
{
|
2022-08-31 15:09:45 +00:00
|
|
|
char description[1024];
|
|
|
|
::mbedtls_strerror(iError, description, AuArraySize(description));
|
|
|
|
return description;
|
2022-08-30 21:18:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
void TLSInit()
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
int iRet;
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
::mbedtls_ctr_drbg_init(&gCtrDrbg);
|
|
|
|
::mbedtls_entropy_init(&gEntropy);
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if ((iRet = ::mbedtls_ctr_drbg_seed(&gCtrDrbg,
|
2022-08-28 19:02:06 +00:00
|
|
|
::mbedtls_entropy_func,
|
|
|
|
&gEntropy,
|
|
|
|
(const unsigned char *)"ReeceWasHere",
|
|
|
|
12)) != 0)
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
SysPushErrorNet("{} ({})", TLSErrorToString(iRet), iRet);
|
2022-08-28 19:02:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gTlsReady = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int TLSContextRecv(void *ctx,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return ((TLSContext *)ctx)->Read(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int TLSContextSend(void *ctx,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return ((TLSContext *)ctx)->Write(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
TLSContext::TLSContext(const TLSMeta &meta) :
|
|
|
|
channelRecv_(this),
|
|
|
|
channelSend_(this),
|
|
|
|
meta_(meta)
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
this->pRecvStack_ = AuMakeShared<Protocol::ProtocolStack>();
|
|
|
|
this->pSendStack_ = AuMakeShared<Protocol::ProtocolStack>();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TLSContext::TLSContext(const AuSPtr<Protocol::IProtocolStack> &pSendStack,
|
|
|
|
const AuSPtr<Protocol::IProtocolStack> &pRecvStack,
|
|
|
|
const TLSMeta &meta) :
|
|
|
|
channelRecv_(this),
|
|
|
|
channelSend_(this),
|
2022-08-30 21:18:15 +00:00
|
|
|
pRecvStack_(AuStaticCast<Protocol::ProtocolStack>(pRecvStack)),
|
|
|
|
pSendStack_(AuStaticCast<Protocol::ProtocolStack>(pSendStack)),
|
2022-08-28 19:02:06 +00:00
|
|
|
meta_(meta)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TLSContext::~TLSContext()
|
|
|
|
{
|
|
|
|
this->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// mbedtls nonblocking interface
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
int TLSContext::Write(const void *pIn, AuUInt length)
|
|
|
|
{
|
2022-10-01 00:07:26 +00:00
|
|
|
if (auto pPiece = this->pPiece_.lock())
|
|
|
|
{
|
|
|
|
AuUInt count {};
|
|
|
|
if (Aurora::IO::EStreamError::eErrorNone !=
|
|
|
|
pPiece->ToNextWriter()->Write(AuMemoryViewStreamRead { AuMemoryViewRead { pIn, length }, count }))
|
|
|
|
{
|
|
|
|
SysPushErrorIO("TLS couldn't flush write into next protocol layer or drain");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2022-08-30 21:18:15 +00:00
|
|
|
return this->pSendStack_->pDrainBuffer->Write(pIn, length);
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TLSContext::Read(void *pOut, AuUInt length)
|
|
|
|
{
|
|
|
|
auto tempReadBuffer = this->channelRecv_.pReadInByteBuffer.lock();
|
|
|
|
if (!tempReadBuffer)
|
|
|
|
{
|
|
|
|
//SysPushErrorNet();
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto toRead = length;// AuMin<AuUInt>(length, this->channelRecv_.uBytesReadAvail - this->channelRecv_.uBytesRead);
|
|
|
|
auto uBytesRead = tempReadBuffer->Read(pOut, toRead);
|
|
|
|
if (!uBytesRead)
|
|
|
|
{
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->channelRecv_.bHasRead = true;
|
|
|
|
auto old = this->channelRecv_.uBytesRead;
|
|
|
|
this->channelRecv_.uBytesRead += uBytesRead;
|
|
|
|
return uBytesRead;
|
|
|
|
}
|
|
|
|
|
2022-11-18 04:15:05 +00:00
|
|
|
bool TLSContext::CheckCertificate(mbedtls_x509_crt const *child, const AuMemoryViewRead &read)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
if (!this->meta_.pCertPin)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-18 04:15:05 +00:00
|
|
|
|
|
|
|
auto pCertChain = AuMakeShared<CertificateChain>();
|
|
|
|
if (!pCertChain)
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCertChain->Init(child);
|
|
|
|
|
|
|
|
auto bRet = this->meta_.pCertPin->CheckCertificate(pCertChain, read);
|
|
|
|
pCertChain->pCertificate = nullptr;
|
|
|
|
return bRet;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// tls context
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
bool TLSContext::Init()
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
int iRet;
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if (!this->pSendStack_)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if (!this->pRecvStack_)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 00:07:26 +00:00
|
|
|
auto pPiece = this->pSendStack_->AppendInterceptorEx(this->GetSendInterceptor(), this->meta_.uOutPageSize);
|
|
|
|
if (!pPiece)
|
2022-08-30 21:18:15 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNet("Couldn't add TLS interceptor");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:07:26 +00:00
|
|
|
this->pPiece_ = pPiece;
|
|
|
|
|
2022-09-04 00:16:49 +00:00
|
|
|
if (!this->pRecvStack_->AppendInterceptorEx(this->GetRecvInterceptor(), this->meta_.uOutPageSize))
|
2022-08-30 21:18:15 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNet("Couldn't add TLS interceptor");
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
::mbedtls_ssl_init(&this->ssl);
|
|
|
|
::mbedtls_ssl_config_init(&this->conf);
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if ((iRet = ::mbedtls_ssl_config_defaults(&this->conf,
|
2022-08-28 19:02:06 +00:00
|
|
|
this->meta_.bIsClient ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
|
|
|
|
this->meta_.transportProtocol == AuNet::ETransportProtocol::eProtocolUDP ? MBEDTLS_SSL_TRANSPORT_DATAGRAM : MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
SysPushErrorNet("{} ({})", TLSErrorToString(iRet), iRet);
|
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 17:46:50 +00:00
|
|
|
if (this->meta_.bIsClient)
|
|
|
|
{
|
2022-11-18 21:03:11 +00:00
|
|
|
::mbedtls_ssl_conf_authmode(&this->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
2022-08-31 17:46:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this->meta_.server.bPinServerPeers)
|
|
|
|
{
|
|
|
|
::mbedtls_ssl_conf_authmode(&this->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
::mbedtls_ssl_conf_authmode(&this->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 21:18:15 +00:00
|
|
|
|
2022-11-18 21:03:11 +00:00
|
|
|
::mbedtls_ssl_conf_verify(&this->conf, [](void *p_ctx, mbedtls_x509_crt *crt,
|
|
|
|
int depth, uint32_t *flags)
|
|
|
|
{
|
|
|
|
*flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
|
|
|
|
if (depth != 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
((TLSContext *)p_ctx)->CheckCertificate(crt, { crt->raw.p, crt->raw.len }) ? 0 : -1;
|
|
|
|
return 0;
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
//
|
2022-08-30 21:18:15 +00:00
|
|
|
::mbedtls_ssl_conf_ca_cb(&this->conf, [](void *p_ctx,
|
2022-08-28 19:02:06 +00:00
|
|
|
mbedtls_x509_crt const *child,
|
|
|
|
mbedtls_x509_crt **candidate_cas) -> int
|
|
|
|
{
|
2022-11-18 21:03:11 +00:00
|
|
|
return 0;// ((TLSContext *)p_ctx)->CheckCertificate(child, { child->raw.p, child->raw.len }) ? 0 : -1;
|
2022-08-28 19:02:06 +00:00
|
|
|
}, this);
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
::mbedtls_ssl_conf_rng(&this->conf, mbedtls_ctr_drbg_random, &gCtrDrbg);
|
|
|
|
|
|
|
|
::mbedtls_ssl_conf_dbg(&this->conf, [](void *, int, const char *as, int, const char *ad)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
//AuLogDbg("{} <--> {}", as, ad);
|
|
|
|
}, nullptr);
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if ((iRet = ::mbedtls_ssl_setup(&this->ssl, &this->conf)) != 0)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
SysPushErrorNet("{} ({})", TLSErrorToString(iRet), iRet);
|
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 17:46:50 +00:00
|
|
|
if (this->meta_.bIsClient)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-31 17:46:50 +00:00
|
|
|
if (this->meta_.client.sSNIServerName.size())
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-31 17:46:50 +00:00
|
|
|
if ((iRet = ::mbedtls_ssl_set_hostname(&this->ssl, this->meta_.client.sSNIServerName.c_str())) != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("{} ({})", TLSErrorToString(iRet), iRet);
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 17:46:50 +00:00
|
|
|
if (!this->meta_.bIsClient)
|
|
|
|
{
|
|
|
|
if (this->meta_.server.bSessionCache)
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
::mbedtls_ssl_cache_init(&this->cache_);
|
|
|
|
|
|
|
|
if (this->meta_.server.iCacheMax != -1)
|
|
|
|
{
|
|
|
|
::mbedtls_ssl_cache_set_max_entries(&this->cache_, this->meta_.server.iCacheMax);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->meta_.server.iCacheTimeout)
|
|
|
|
{
|
|
|
|
::mbedtls_ssl_cache_set_timeout(&this->cache_, this->meta_.server.iCacheTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
::mbedtls_ssl_conf_session_cache(&this->conf,
|
|
|
|
&this->cache_,
|
|
|
|
mbedtls_ssl_cache_get,
|
|
|
|
mbedtls_ssl_cache_set);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
::mbedtls_ssl_ticket_init(&this->ticketCtx_);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (this->meta_.transportProtocol == Net::ETransportProtocol::eProtocolUDP)
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_COOKIE_C)
|
|
|
|
::mbedtls_ssl_cookie_init(&this->cookieCtx_);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_COOKIE_C)
|
|
|
|
if (this->meta_.dtls.iServerCookies > 0)
|
|
|
|
{
|
|
|
|
if ((iRet = ::mbedtls_ssl_cookie_setup(&this->cookieCtx_,
|
|
|
|
mbedtls_ctr_drbg_random,
|
|
|
|
&gCtrDrbg)) != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("{} ({})", TLSErrorToString(iRet), iRet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
::mbedtls_ssl_conf_dtls_cookies(&conf,
|
|
|
|
mbedtls_ssl_cookie_write,
|
|
|
|
mbedtls_ssl_cookie_check,
|
|
|
|
&this->cookieCtx_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
|
|
|
|
if (this->meta_.dtls.iServerCookies == 0)
|
|
|
|
{
|
|
|
|
::mbedtls_ssl_conf_dtls_cookies(&conf, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
::mbedtls_ssl_conf_dtls_anti_replay(&this->conf,
|
|
|
|
this->meta_.dtls.iServerCookies ? MBEDTLS_SSL_ANTI_REPLAY_ENABLED : MBEDTLS_SSL_ANTI_REPLAY_DISABLED);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
::mbedtls_ssl_conf_dtls_badmac_limit(&this->conf,
|
2022-09-02 19:53:25 +00:00
|
|
|
this->meta_.dtls.iServerBadMacLimit);
|
2022-08-31 17:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
if (this->meta_.server.bEnableTickets)
|
|
|
|
{
|
|
|
|
if ((iRet = ::mbedtls_ssl_ticket_setup(&this->ticketCtx_,
|
|
|
|
mbedtls_ctr_drbg_random,
|
|
|
|
&gCtrDrbg,
|
|
|
|
MBEDTLS_CIPHER_AES_256_GCM,
|
|
|
|
this->meta_.server.iTicketTimeout)) != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("{} ({})", TLSErrorToString(iRet), iRet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
::mbedtls_ssl_conf_session_tickets_cb(&this->conf,
|
|
|
|
mbedtls_ssl_ticket_write,
|
|
|
|
mbedtls_ssl_ticket_parse,
|
|
|
|
&this->ticketCtx_);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->meta_.transportProtocol == Net::ETransportProtocol::eProtocolUDP)
|
|
|
|
{
|
|
|
|
if (this->meta_.dtls.iMTUSize)
|
|
|
|
{
|
|
|
|
::mbedtls_ssl_set_mtu(&this->ssl,
|
|
|
|
this->meta_.dtls.iMTUSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
::mbedtls_ssl_set_timer_cb(&this->ssl,
|
|
|
|
&this->timer_,
|
|
|
|
mbedtls_timing_set_delay,
|
|
|
|
mbedtls_timing_get_delay);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
::mbedtls_ssl_set_bio(&this->ssl,
|
|
|
|
this,
|
|
|
|
TLSContextSend,
|
|
|
|
TLSContextRecv,
|
|
|
|
nullptr);
|
2022-08-29 18:57:24 +00:00
|
|
|
|
|
|
|
if (this->meta_.cipherSuites.size())
|
|
|
|
{
|
|
|
|
this->cipherSuites_.reserve(this->meta_.cipherSuites.size());
|
|
|
|
for (const auto &cipher : this->meta_.cipherSuites)
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
if (!AuTryInsert(this->cipherSuites_, cipher))
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-29 18:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto &defaultCiphers = GetDefaultCipherSuites();
|
|
|
|
this->cipherSuites_.reserve(defaultCiphers.size());
|
|
|
|
for (const auto &cipher : defaultCiphers)
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
if (!AuTryInsert(this->cipherSuites_, cipher))
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-29 18:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if (!AuTryInsert(this->cipherSuites_, 0))
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
((mbedtls_ssl_config *)this->ssl.private_conf/*fuck yourself*/)->private_ciphersuite_list = this->cipherSuites_.data();
|
|
|
|
return true;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TLSContext::Destroy()
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
::mbedtls_ssl_free(&this->ssl);
|
|
|
|
::mbedtls_ssl_config_free(&this->conf);
|
2022-08-31 17:46:50 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
::mbedtls_ssl_ticket_free(&this->ticketCtx_);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
::mbedtls_ssl_cache_free(&this->cache_);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_COOKIE_C)
|
|
|
|
::mbedtls_ssl_cookie_free(&this->cookieCtx_);
|
|
|
|
#endif
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
this->Attach({});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TLSContext::OnClose()
|
|
|
|
{
|
|
|
|
this->bIsDead = true;
|
|
|
|
|
|
|
|
if (auto pSocket = this->wpSocket_.lock())
|
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
pSocket->Shutdown(false);
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuResetMember(this->meta_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TLSContext::OnFatal()
|
|
|
|
{
|
|
|
|
this->bIsDead = true;
|
|
|
|
this->bIsFatal = true;
|
|
|
|
|
|
|
|
if (auto pSocket = this->wpSocket_.lock())
|
|
|
|
{
|
|
|
|
AuDynamicCast<AuNet::Socket, AuNet::ISocket>(pSocket)->SendErrorBeginShutdown(AuNet::ENetworkError::eTLSError);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuResetMember(this->meta_);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// public api
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
AuSPtr<Protocol::IProtocolStack> TLSContext::ToReadStack()
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
return this->pRecvStack_;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Protocol::IProtocolStack> TLSContext::ToWriteStack()
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
return this->pSendStack_;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Protocol::IProtocolInterceptorEx> TLSContext::GetRecvInterceptor()
|
|
|
|
{
|
|
|
|
return AuSPtr<Protocol::IProtocolInterceptorEx>(AuSharedFromThis(), &this->channelRecv_);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Protocol::IProtocolInterceptorEx> TLSContext::GetSendInterceptor()
|
|
|
|
{
|
|
|
|
return AuSPtr<Protocol::IProtocolInterceptorEx>(AuSharedFromThis(), &this->channelSend_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TLSContext::Attach(const AuSPtr<Net::ISocket> &pSocket)
|
|
|
|
{
|
|
|
|
if (!pSocket)
|
|
|
|
{
|
|
|
|
if (auto pOldSocket = this->wpSocket_.lock())
|
|
|
|
{
|
|
|
|
this->wpSocket_.reset();
|
|
|
|
pOldSocket->ToChannel()->SpecifyRecvProtocol({});
|
|
|
|
pOldSocket->ToChannel()->SpecifySendProtocol({});
|
|
|
|
// TODO (Reece): Shutdown hook
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->wpSocket_ = pSocket;
|
|
|
|
pSocket->ToChannel()->SpecifyRecvProtocol(ToReadStack());
|
|
|
|
pSocket->ToChannel()->SpecifySendProtocol(ToWriteStack());
|
|
|
|
// TODO (Reece): Shutdown hook
|
|
|
|
}
|
|
|
|
|
|
|
|
void TLSContext::StartHandshake()
|
|
|
|
{
|
2022-08-30 21:18:15 +00:00
|
|
|
this->bIsAlive = false;
|
|
|
|
this->bIsDead = false;
|
|
|
|
this->bIsFatal = false;
|
|
|
|
this->iFatalError = 0;
|
|
|
|
|
2022-11-18 04:15:05 +00:00
|
|
|
this->bPinLock_ = false;
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
this->channelRecv_.HasCompletedHandshake() = false;
|
|
|
|
|
|
|
|
if (::mbedtls_ssl_session_reset(&this->ssl) != 0)
|
|
|
|
{
|
|
|
|
this->OnFatal();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->channelRecv_.TryHandshake();
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 17:46:50 +00:00
|
|
|
AuUInt16 TLSContext::GetCurrentCipherSuite()
|
|
|
|
{
|
|
|
|
return ::mbedtls_ssl_get_ciphersuite_id_from_ssl(&this->ssl);
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
void TLSContext::StartClose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSContext::HasCompletedHandshake()
|
|
|
|
{
|
|
|
|
return this->channelRecv_.HasCompletedHandshake();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSContext::HasEnded()
|
|
|
|
{
|
|
|
|
return this->bIsDead;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSContext::HasFailed()
|
|
|
|
{
|
|
|
|
return this->bIsFatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TLSContext::GetFatalErrorCode()
|
|
|
|
{
|
|
|
|
return this->iFatalError;
|
|
|
|
}
|
2022-08-30 21:18:15 +00:00
|
|
|
|
|
|
|
AuString TLSContext::GetFatalErrorCodeAsString()
|
|
|
|
{
|
|
|
|
return TLSErrorToString(this->GetFatalErrorCode());
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<ITLSContext> NewTLSContext(const TLSMeta &meta)
|
|
|
|
{
|
|
|
|
auto pTlsContext = AuMakeShared<TLSContext>(meta);
|
|
|
|
if (!pTlsContext)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pTlsContext->Init();
|
|
|
|
|
|
|
|
return pTlsContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<ITLSContext> NewTLSContextEx(const AuSPtr<Protocol::IProtocolStack> &pSendStack,
|
|
|
|
const AuSPtr<Protocol::IProtocolStack> &pRecvStack,
|
|
|
|
const TLSMeta &meta)
|
|
|
|
{
|
|
|
|
auto pTlsContext = AuMakeShared<TLSContext>(pSendStack, pRecvStack, meta);
|
|
|
|
if (!pTlsContext)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
if (!pTlsContext->Init())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
return pTlsContext;
|
|
|
|
}
|
|
|
|
}
|