AuroraRuntime/Source/Crypto/RSA/RSA.hpp
Reece Wilson 033f7e2453 [+] Aurora::Crypto::X509::CertRequest
[+] Aurora::Crypto::X509::GenerateCertificate
[*] Fix lazily copied gen1 RSA code
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP256R1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP256K1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP384R1
[+] Aurora::Crypto::ECC::EECCCurve::eCurveSECP521R1
[*] Unfuck ECC interop
[*] Tls pinning: use mbedtls_ssl_conf_verify for tls1.3 (when mbedtls is in a better state)
2022-11-18 21:03:11 +00:00

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.writePtr, &actualSize, &key, flags);
if (ret != CRYPT_OK)
{
SysPushErrorCrypt("{}", ret);
return false;
}
out.writePtr += actualSize;
return true;
}
static bool ImportRSAKey(rsa_key &in, const RSAKey &rsakey)
{
int flags{};
if (rsakey.meta.encoding == ERSAKeyType::eCert)
{
if (rsakey.meta.type == 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.encoding == ERSAKeyType::eRsaKey)
{
flags |= kRsaFlagPKCS1;
}
if (rsakey.meta.type == 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;
}
}