AuroraRuntime/Source/Crypto/PEM/PEM.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

131 lines
3.6 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: PEM.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "../Crypto.hpp"
#include "PEM.hpp"
namespace Aurora::Crypto::PEM
{
static bool ParsePEM(const AuROString &begin, const AuROString &end, const AuROString &src, AuByteBuffer &buf)
{
AuUInt lines = 0;
AuString str;
int fail = 0;
bool finished = 0;
try
{
str.reserve(src.size());
Parse::SplitNewlines(src,
[&](const AuROString &src)
{
if (lines == 0)
{
fail |= (int)(src != begin);
lines++;
return;
}
if (src == end)
{
fail |= (int)!Parse::Base64Decode(str, buf);
finished = true;
return;
}
str += src;
});
}
catch (...)
{
return false;
}
return fail ? false : finished;
}
static AuString SerializePEM(const AuROString &begin, const AuROString &end, const AuByteBuffer &buf)
{
auto &delm = AuLocale::NewLine();
AuString ret;
ret += begin;
ret += delm;
AuString b64;
try
{
if (!Parse::Base64Encode(buf, b64))
{
return "";
}
else
{
b64 = Parse::SplitStringDelm(b64, delm, 16);
}
ret += b64;
ret += delm;
ret += end;
return ret;
}
catch (...)
{
return "";
}
}
AUKN_SYM AuString ToString(const Aurora::Crypto::X509::Certificate &in)
{
return SerializePEM("-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----", in);
}
AUKN_SYM AuString PrivateToString(const DerBuffer &in)
{
return SerializePEM("-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----", in);
}
AUKN_SYM AuString PublicToString(const DerBuffer &in)
{
return SerializePEM("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----", in);
}
AUKN_SYM AuString PublicRSAToString(const DerBuffer &in)
{
return SerializePEM("-----BEGIN RSA PUBLIC KEY-----", "-----END RSA PUBLIC KEY-----", in);
}
AUKN_SYM AuString PrivateRSAToString(const DerBuffer &in)
{
return SerializePEM("-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----", in);
}
AUKN_SYM bool FromString(const AuROString &in, Aurora::Crypto::X509::Certificate &out)
{
return ParsePEM("-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----", in, out);
}
AUKN_SYM bool PrivateFromString(const AuROString &in, DerBuffer &out)
{
return ParsePEM("-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----", in, out);
}
AUKN_SYM bool PublicRSAFromString(const AuROString &in, DerBuffer &out)
{
return ParsePEM("-----BEGIN RSA PUBLIC KEY-----", "-----END RSA PUBLIC KEY-----", in, out);
}
AUKN_SYM bool PrivateRSAFromString(const AuROString &in, DerBuffer &out)
{
return ParsePEM("-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----", in, out);
}
AUKN_SYM bool PublicFromString(const AuROString &in, DerBuffer &out)
{
return ParsePEM("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----", in, out);
}
}