mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 02:12:09 -05:00
LibCrypto: Add PEM encoder
This commit adds a new method to create a PEM encoded ASN1 from its DER variant.
This commit is contained in:
parent
7ce75ee3c5
commit
8b881eaf02
2 changed files with 42 additions and 0 deletions
|
@ -93,4 +93,40 @@ ErrorOr<Vector<ByteBuffer>> decode_pems(ReadonlyBytes data)
|
|||
return pems;
|
||||
}
|
||||
|
||||
ErrorOr<ByteBuffer> encode_pem(ReadonlyBytes data, PEMType type)
|
||||
{
|
||||
ByteBuffer encoded;
|
||||
StringView block_start;
|
||||
StringView block_end;
|
||||
|
||||
switch (type) {
|
||||
case Certificate:
|
||||
block_start = "-----BEGIN CERTIFICATE-----\n"sv;
|
||||
block_end = "-----END CERTIFICATE-----\n"sv;
|
||||
break;
|
||||
case PrivateKey:
|
||||
block_start = "-----BEGIN PRIVATE KEY-----\n"sv;
|
||||
block_end = "-----END PRIVATE KEY-----\n"sv;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
auto b64encoded = TRY(encode_base64(data));
|
||||
|
||||
TRY(encoded.try_append(block_start.bytes()));
|
||||
|
||||
size_t to_read = 64;
|
||||
for (size_t i = 0; i < b64encoded.bytes().size(); i += to_read) {
|
||||
if (i + to_read > b64encoded.bytes().size())
|
||||
to_read = b64encoded.bytes().size() - i;
|
||||
TRY(encoded.try_append(b64encoded.bytes().slice(i, to_read)));
|
||||
TRY(encoded.try_append("\n"sv.bytes()));
|
||||
}
|
||||
|
||||
TRY(encoded.try_append(block_end.bytes()));
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,7 +12,13 @@
|
|||
|
||||
namespace Crypto {
|
||||
|
||||
enum PEMType {
|
||||
Certificate,
|
||||
PrivateKey,
|
||||
};
|
||||
|
||||
ByteBuffer decode_pem(ReadonlyBytes);
|
||||
ErrorOr<Vector<ByteBuffer>> decode_pems(ReadonlyBytes);
|
||||
ErrorOr<ByteBuffer> encode_pem(ReadonlyBytes, PEMType = PEMType::Certificate);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue