2022-08-30 21:18:15 +00:00
|
|
|
/***
|
|
|
|
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(),
|
2022-08-30 23:06:50 +00:00
|
|
|
mbedtls_ctr_drbg_random,
|
2022-08-30 21:18:15 +00:00
|
|
|
&gCtrDrbg);
|
|
|
|
if (iRet != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorCrypto("Invalid Private Key: {} ({})", TLSErrorToString(iRet), iRet);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return pPrivateKey;
|
|
|
|
}
|
|
|
|
}
|