2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: TLSProtocolRecv.cpp
|
|
|
|
Date: 2022-8-24
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "TLS.hpp"
|
|
|
|
#include "TLSContext.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::IO::TLS
|
|
|
|
{
|
|
|
|
TLSProtocolRecv::TLSProtocolRecv(TLSContext *pParent) :
|
|
|
|
pParent_(pParent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSProtocolRecv::OnDataAvailable(const AuSPtr<Memory::ByteBuffer> &pReadInByteBuffer,
|
|
|
|
const AuSPtr<Memory::ByteBuffer> &pWriteOutByteBuffer)
|
|
|
|
{
|
|
|
|
this->bHasRead = false;
|
|
|
|
this->pReadInByteBuffer = pReadInByteBuffer;
|
|
|
|
this->uBytesReadAvail = pReadInByteBuffer->RemainingBytes();
|
|
|
|
this->uBytesRead = 0;
|
|
|
|
|
|
|
|
if (this->pParent_->bIsDead)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryHandshake())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
do
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
this->bHasRead = false;
|
|
|
|
|
|
|
|
if (!this->DoOneTick(pWriteOutByteBuffer))
|
|
|
|
{
|
|
|
|
this->pReadInByteBuffer.reset();
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
2023-05-11 15:05:00 +00:00
|
|
|
while (this->bHasRead);
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
this->pReadInByteBuffer.reset();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSProtocolRecv::TryHandshake()
|
|
|
|
{
|
|
|
|
if (this->bHasCompletedHandshake_)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bComplete {};
|
|
|
|
if (!this->DoHandshake(bComplete))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bComplete)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bHasCompletedHandshake_ = true;
|
|
|
|
this->pParent_->bIsAlive = true;
|
2022-11-18 04:15:05 +00:00
|
|
|
this->pParent_->bPinLock_ = false;
|
2022-08-28 19:02:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSProtocolRecv::DoHandshake(bool &bComplete)
|
|
|
|
{
|
|
|
|
bComplete = false;
|
|
|
|
|
|
|
|
if (this->bHasFailedOnce)
|
|
|
|
{
|
|
|
|
auto pBuffer = this->pReadInByteBuffer.lock();
|
|
|
|
|
|
|
|
if (!pBuffer)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pBuffer->RemainingBytes())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int iRet {};
|
|
|
|
switch ((iRet = ::mbedtls_ssl_handshake(&this->pParent_->ssl)))
|
|
|
|
{
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_READ:
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_WRITE:
|
|
|
|
case MBEDTLS_ERR_SSL_CONN_EOF:
|
|
|
|
{
|
|
|
|
this->bHasFailedOnce = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED:
|
|
|
|
{
|
|
|
|
this->bHasFailedOnce = true;
|
|
|
|
this->pParent_->OnClose();
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-18 04:15:05 +00:00
|
|
|
case MBEDTLS_ERR_X509_FATAL_ERROR:
|
|
|
|
{
|
|
|
|
this->bHasFailedOnce = true;
|
|
|
|
this->pParent_->OnClose();
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
bComplete = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
this->pParent_->iFatalError = iRet;
|
|
|
|
SysPushErrorNet("Error during handshake: {:x}", iRet);
|
|
|
|
this->pParent_->OnFatal();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TLSProtocolRecv::DoOneTick(const AuSPtr<Memory::ByteBuffer> &pWriteOutByteBuffer)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2022-08-30 23:06:50 +00:00
|
|
|
auto pDest = pWriteOutByteBuffer->GetNextLinearWrite();
|
|
|
|
AuUInt8 *pBase { pDest.ToPointer() };
|
|
|
|
AuUInt uCount { pDest.length };
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
// ...
|
|
|
|
if (!uCount)
|
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
SysPushErrorCrypto("A TLS tick failed because there was not enough space in the output buffer");
|
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mbedtls tick
|
|
|
|
int iRet = ::mbedtls_ssl_read(&this->pParent_->ssl,
|
|
|
|
pBase,
|
|
|
|
uCount);
|
|
|
|
|
|
|
|
if ((iRet == MBEDTLS_ERR_SSL_WANT_READ) ||
|
|
|
|
(iRet == MBEDTLS_ERR_SSL_WANT_WRITE) ||
|
|
|
|
(iRet == MBEDTLS_ERR_SSL_CONN_EOF))
|
|
|
|
{
|
|
|
|
// mbedtls doesn't know about peeking. their os doesn't support it. wont be added for linux+nt.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iRet == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
|
|
|
|
{
|
|
|
|
this->pParent_->OnClose();
|
|
|
|
// mbedtls doesn't know about peeking. their os doesn't support it. wont be added for linux+nt.
|
2023-05-11 15:05:00 +00:00
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iRet < 0)
|
|
|
|
{
|
|
|
|
this->pParent_->iFatalError = iRet;
|
|
|
|
SysPushErrorNet("TLS Error: {}", iRet);
|
|
|
|
this->pParent_->OnFatal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pWriteOutByteBuffer->writePtr += iRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->bHasRead;
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
bool &TLSProtocolRecv::HasCompletedHandshake()
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
return this->bHasCompletedHandshake_;
|
|
|
|
}
|
|
|
|
}
|