mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-07 11:39:44 +03:00
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.
This commit is contained in:
parent
700ad6bf35
commit
06340ca674
Notes:
sideshowbarker
2024-07-18 22:57:59 +09:00
Author: https://github.com/fdellwing Commit: https://github.com/SerenityOS/serenity/commit/06340ca674 Pull-request: https://github.com/SerenityOS/serenity/pull/17835 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/linusg Reviewed-by: https://github.com/sin-ack Reviewed-by: https://github.com/timschumi
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,5 +13,6 @@
|
||||
namespace Crypto {
|
||||
|
||||
ByteBuffer decode_pem(ReadonlyBytes);
|
||||
ErrorOr<Vector<ByteBuffer>> decode_pems(ReadonlyBytes);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user