87 lines
2.0 KiB
C++
87 lines
2.0 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: RSA.hpp
|
|
Date: 2021-7-1
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
#include "../Crypto.hpp"
|
|
|
|
namespace Aurora::Crypto::RSA
|
|
{
|
|
static bool ExportRSAKey(const rsa_key &key, EKeyType side, ERSAKeyType type, Memory::ByteBuffer &out)
|
|
{
|
|
int flags = 0;
|
|
|
|
if (type == ERSAKeyType::eRsaKey)
|
|
{
|
|
flags |= kRsaFlagPKCS1;
|
|
}
|
|
|
|
if (side == EKeyType::eKeyPublic)
|
|
{
|
|
flags |= kRsaFlagPublic;
|
|
}
|
|
|
|
if (!AuTryResize(out, 4096))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
unsigned long actualSize = out.size();
|
|
auto ret = rsa_pkcs8_export(out.data(), &actualSize, &key, flags);
|
|
|
|
if (ret != CRYPT_OK)
|
|
{
|
|
SysPushErrorCrypt("{}", ret);
|
|
return false;
|
|
}
|
|
|
|
out.resize(actualSize);
|
|
return true;
|
|
}
|
|
|
|
static bool ImportRSAKey(rsa_key &in, const RSAKey &rsakey)
|
|
{
|
|
int flags{};
|
|
|
|
if (rsakey.meta.type == ERSAKeyType::eCert)
|
|
{
|
|
if (rsakey.meta.side == EKeyType::eKeyPrivate)
|
|
{
|
|
SysPushErrorArg("Attempted to import a certificate as a private key.");
|
|
return false;
|
|
}
|
|
|
|
auto ret = rsa_import_x509(rsakey.blob.data(), rsakey.blob.size(), &in);
|
|
if (ret != CRYPT_OK)
|
|
{
|
|
SysPushErrorCrypt("{}", ret);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
if (rsakey.meta.type == ERSAKeyType::eRsaKey)
|
|
{
|
|
flags |= kRsaFlagPKCS1;
|
|
}
|
|
|
|
if (rsakey.meta.side == EKeyType::eKeyPublic)
|
|
{
|
|
flags |= kRsaFlagPublic;
|
|
}
|
|
|
|
auto ret = rsa_import_ex(rsakey.blob.data(), rsakey.blob.size(), &in, flags);
|
|
if (ret != CRYPT_OK)
|
|
{
|
|
SysPushErrorCrypt("{}", ret);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|