2022-08-30 21:18:15 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: TLSCertificateChain.cpp
|
|
|
|
Date: 2022-8-27
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "TLS.hpp"
|
|
|
|
#include "TLSCertificateChain.hpp"
|
2022-11-18 04:15:05 +00:00
|
|
|
#include <Source/Crypto/X509/x509.hpp>
|
2022-08-30 21:18:15 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO::TLS
|
|
|
|
{
|
|
|
|
CertificateChain::CertificateChain()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CertificateChain::~CertificateChain()
|
|
|
|
{
|
|
|
|
::mbedtls_x509_crt_free(&this->ownCertificate);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt32 CertificateChain::GetCertificateCount()
|
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
AuUInt32 ret {};
|
|
|
|
|
|
|
|
auto pCert = this->pCertificate;
|
|
|
|
if (!pCert)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto index = ret++;
|
|
|
|
}
|
2023-08-27 11:41:51 +00:00
|
|
|
while ((pCert = pCert->next));
|
2022-11-18 04:15:05 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<AuMemoryViewRead> CertificateChain::GetCertificate(AuUInt32 idx)
|
|
|
|
{
|
|
|
|
AuUInt32 ret {};
|
|
|
|
|
|
|
|
auto pCert = this->pCertificate;
|
|
|
|
if (!pCert)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto index = ret++;
|
|
|
|
|
|
|
|
if (index == idx)
|
|
|
|
{
|
|
|
|
struct View : AuMemoryViewRead
|
|
|
|
{
|
|
|
|
View(const AuMemoryViewRead &in, AuSPtr<void> pin) :
|
|
|
|
AuMemoryViewRead(in),
|
|
|
|
pin(pin)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
AuSPtr<void> pin;
|
|
|
|
};
|
|
|
|
|
|
|
|
return AuMakeSharedThrow<View>(AuMemoryViewRead { pCert->raw.p, pCert->raw.len}, AuSharedFromThis());
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 11:41:51 +00:00
|
|
|
while ((pCert = pCert->next));
|
2022-11-18 04:15:05 +00:00
|
|
|
|
|
|
|
return {};
|
2022-08-30 21:18:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 04:15:05 +00:00
|
|
|
Crypto::X509::DecodedCertificate CertificateChain::GetCertificateDetails(AuUInt32 idx)
|
2022-08-30 21:18:15 +00:00
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
AuUInt32 ret {};
|
|
|
|
|
|
|
|
auto pCert = this->pCertificate;
|
|
|
|
if (!pCert)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto index = ret++;
|
|
|
|
|
|
|
|
if (index == idx)
|
|
|
|
{
|
|
|
|
Crypto::X509::DecodedCertificate cert;
|
|
|
|
AuCrypto::X509::DecodeInternal(*pCert, cert);
|
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 11:41:51 +00:00
|
|
|
while ((pCert = pCert->next));
|
2022-11-18 04:15:05 +00:00
|
|
|
|
2022-08-30 21:18:15 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateChain::Init(const AuList<AuByteBuffer> &certs)
|
|
|
|
{
|
|
|
|
int iRet {};
|
|
|
|
|
|
|
|
this->pCertificate = &this->ownCertificate;
|
|
|
|
::mbedtls_x509_crt_init(&this->ownCertificate);
|
|
|
|
|
|
|
|
for (const auto &cert : certs)
|
|
|
|
{
|
|
|
|
iRet = ::mbedtls_x509_crt_parse(&this->ownCertificate,
|
|
|
|
(const unsigned char *)cert.base,
|
|
|
|
cert.length);
|
|
|
|
if (iRet != 0)
|
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
this->pCertificate = nullptr;
|
2022-08-30 21:18:15 +00:00
|
|
|
SysPushErrorCrypto("Failed to parse certificate chain: {}", iRet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->Precache();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateChain::Init(const AuList<AuMemoryViewRead> &certs)
|
|
|
|
{
|
|
|
|
int iRet {};
|
|
|
|
|
|
|
|
this->pCertificate = &this->ownCertificate;
|
|
|
|
::mbedtls_x509_crt_init(&this->ownCertificate);
|
|
|
|
|
|
|
|
for (const auto &cert : certs)
|
|
|
|
{
|
|
|
|
iRet = ::mbedtls_x509_crt_parse(&this->ownCertificate,
|
|
|
|
(const unsigned char *)cert.ToPointer(),
|
|
|
|
cert.length);
|
|
|
|
if (iRet != 0)
|
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
this->pCertificate = nullptr;
|
2022-08-30 21:18:15 +00:00
|
|
|
SysPushErrorCrypto("Failed to parse certificate chain: {}", iRet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->Precache();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateChain::Init(const AuMemoryViewRead &cert)
|
|
|
|
{
|
|
|
|
int iRet {};
|
|
|
|
|
|
|
|
this->pCertificate = &this->ownCertificate;
|
|
|
|
::mbedtls_x509_crt_init(&this->ownCertificate);
|
|
|
|
|
|
|
|
iRet = ::mbedtls_x509_crt_parse(&this->ownCertificate,
|
|
|
|
(const unsigned char *)cert.ToPointer(),
|
|
|
|
cert.length);
|
|
|
|
if (iRet != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorCrypto("Failed to parse certificate chain: {}", iRet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->Precache();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateChain::Init(const mbedtls_x509_crt *pCert)
|
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
this->pCertificate = (mbedtls_x509_crt *)pCert;
|
2022-08-30 21:18:15 +00:00
|
|
|
return this->Precache();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CertificateChain::Precache()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<ICertificateChain> ChainFromOne(const AuMemoryViewRead &read)
|
|
|
|
{
|
|
|
|
auto pCertificateChain = AuMakeShared<CertificateChain>();
|
|
|
|
if (!pCertificateChain)
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pCertificateChain->Init(read))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return pCertificateChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<ICertificateChain> ChainFromMany(const AuList<AuMemoryViewRead> &read)
|
|
|
|
{
|
|
|
|
auto pCertificateChain = AuMakeShared<CertificateChain>();
|
|
|
|
if (!pCertificateChain)
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pCertificateChain->Init(read))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return pCertificateChain;
|
|
|
|
}
|
|
|
|
}
|