diff options
author | Fabian Dellwing <fabian.dellwing@gmail.com> | 2023-03-13 12:18:14 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2023-03-16 18:54:20 +0330 |
commit | 06340ca6749224edac56415ffe0d3db4765c6635 (patch) | |
tree | 0558a675210b15a822dd5c3dfcc8ff7723e8b9f3 /Userland/Libraries | |
parent | 700ad6bf350f9e74044c015157f8ad7072c7f04c (diff) | |
download | serenity-06340ca6749224edac56415ffe0d3db4765c6635.zip |
LibCrypto: Add multiple PEM parser
This adds a function to parse multiple PEMs out of a single input.
This allows us to load certificates from a cacert.pem file without
need for preprocessing.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCrypto/ASN1/PEM.cpp | 37 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/ASN1/PEM.h | 1 |
2 files changed, 38 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCrypto/ASN1/PEM.cpp b/Userland/Libraries/LibCrypto/ASN1/PEM.cpp index a23bed08bf..f682493f56 100644 --- a/Userland/Libraries/LibCrypto/ASN1/PEM.cpp +++ b/Userland/Libraries/LibCrypto/ASN1/PEM.cpp @@ -56,4 +56,41 @@ ByteBuffer decode_pem(ReadonlyBytes data) return decoded; } +ErrorOr<Vector<ByteBuffer>> decode_pems(ReadonlyBytes data) +{ + GenericLexer lexer { data }; + ByteBuffer decoded; + Vector<ByteBuffer> pems; + + enum { + Junk, + Parsing, + } state { Junk }; + while (!lexer.is_eof()) { + switch (state) { + case Junk: + if (lexer.consume_specific("-----BEGIN")) + state = Parsing; + lexer.consume_line(); + break; + case Parsing: { + if (lexer.consume_specific("-----END")) { + state = Junk; + lexer.consume_line(); + TRY(pems.try_append(decoded)); + decoded.clear(); + break; + } + auto b64decoded = TRY(decode_base64(lexer.consume_line().trim_whitespace(TrimMode::Right))); + TRY(decoded.try_append(b64decoded.data(), b64decoded.size())); + break; + } + default: + VERIFY_NOT_REACHED(); + } + } + + return pems; +} + } diff --git a/Userland/Libraries/LibCrypto/ASN1/PEM.h b/Userland/Libraries/LibCrypto/ASN1/PEM.h index c95617e507..396a41fd05 100644 --- a/Userland/Libraries/LibCrypto/ASN1/PEM.h +++ b/Userland/Libraries/LibCrypto/ASN1/PEM.h @@ -13,5 +13,6 @@ namespace Crypto { ByteBuffer decode_pem(ReadonlyBytes); +ErrorOr<Vector<ByteBuffer>> decode_pems(ReadonlyBytes); } |