AuroraRuntime/Source/IO/TLS/TLSPrivateKeyPair.cpp

69 lines
2.0 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: TLSPrivateKeyPair.cpp
Date: 2022-8-27
Author: Reece
***/
#include "TLS.hpp"
#include "TLSPrivateKeyPair.hpp"
namespace Aurora::IO::TLS
{
TLSPrivateKeyPairImpl::TLSPrivateKeyPairImpl()
{
::mbedtls_pk_init(&this->privateKey_);
}
TLSPrivateKeyPairImpl::~TLSPrivateKeyPairImpl()
{
::mbedtls_pk_free(&this->privateKey_);
}
AuSPtr<ICertificateChain> TLSPrivateKeyPairImpl::GetChain()
{
return AuSPtr<ICertificateChain>(AuSharedFromThis(), &this->chain_);
}
CertificateChain *TLSPrivateKeyPairImpl::ToChain()
{
return &this->chain_;
}
mbedtls_pk_context &TLSPrivateKeyPairImpl::GetInternal()
{
return this->privateKey_;
}
AUKN_SYM AuSPtr<ITLSPrivateKeyPair> ImportPrivateKeyPair(const TLSPrivateKeyPair &keyPair)
{
int iRet {};
auto pPrivateKey = AuMakeShared<TLSPrivateKeyPairImpl>();
if (!pPrivateKey)
{
SysPushErrorMemory();
return {};
}
if (!pPrivateKey->ToChain()->Init(keyPair.certificateChain))
{
// Not going to bother with a nested push
return {};
}
iRet = ::mbedtls_pk_parse_key(&pPrivateKey->GetInternal(),
(const unsigned char *)keyPair.privateKey.readPtr,
keyPair.privateKey.RemainingBytes(),
keyPair.sPassword.size() ? (const unsigned char *)keyPair.sPassword.c_str() : nullptr,
keyPair.sPassword.size(),
mbedtls_ctr_drbg_random,
&gCtrDrbg);
if (iRet != 0)
{
SysPushErrorCrypto("Invalid Private Key: {} ({})", TLSErrorToString(iRet), iRet);
return {};
}
return pPrivateKey;
}
}