mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
LibCrypto: Implement UTCTime and GeneralizedTime parsers
This commit is contained in:
parent
39997e2ab1
commit
13abbc5ea8
2 changed files with 118 additions and 0 deletions
|
@ -92,4 +92,118 @@ String type_name(Type type)
|
||||||
return "InvalidType";
|
return "InvalidType";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<Core::DateTime> parse_utc_time(const StringView& time)
|
||||||
|
{
|
||||||
|
// YYMMDDhhmm[ss]Z or YYMMDDhhmm[ss](+|-)hhmm
|
||||||
|
GenericLexer lexer(time);
|
||||||
|
auto year_in_century = lexer.consume(2).to_uint();
|
||||||
|
auto month = lexer.consume(2).to_uint();
|
||||||
|
auto day = lexer.consume(2).to_uint();
|
||||||
|
auto hour = lexer.consume(2).to_uint();
|
||||||
|
auto minute = lexer.consume(2).to_uint();
|
||||||
|
Optional<unsigned> seconds, offset_hours, offset_minutes;
|
||||||
|
[[maybe_unused]] bool negative_offset = false;
|
||||||
|
if (!lexer.next_is('Z')) {
|
||||||
|
if (!lexer.next_is(is_any_of("+-"))) {
|
||||||
|
seconds = lexer.consume(2).to_uint();
|
||||||
|
if (!seconds.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lexer.next_is(is_any_of("+-"))) {
|
||||||
|
negative_offset = lexer.consume() == '-';
|
||||||
|
offset_hours = lexer.consume(2).to_uint();
|
||||||
|
offset_minutes = lexer.consume(2).to_uint();
|
||||||
|
if (!offset_hours.has_value() || !offset_minutes.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lexer.consume();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lexer.consume();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!year_in_century.has_value() || !month.has_value() || !day.has_value() || !hour.has_value() || !minute.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto full_year = (Core::DateTime::now().year() / 100) * 100 + year_in_century.value();
|
||||||
|
auto full_seconds = seconds.value_or(0);
|
||||||
|
|
||||||
|
// FIXME: Handle offsets!
|
||||||
|
if (offset_hours.has_value() || offset_minutes.has_value())
|
||||||
|
dbgln("FIXME: Implement UTCTime with offset!");
|
||||||
|
|
||||||
|
return Core::DateTime::create(full_year, month.value(), day.value(), hour.value(), minute.value(), full_seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<Core::DateTime> parse_generalized_time(const StringView& time)
|
||||||
|
{
|
||||||
|
// YYYYMMDDhh[mm[ss[.fff]]] or YYYYMMDDhh[mm[ss[.fff]]]Z or YYYYMMDDhh[mm[ss[.fff]]](+|-)hhmm
|
||||||
|
GenericLexer lexer(time);
|
||||||
|
auto year = lexer.consume(4).to_uint();
|
||||||
|
auto month = lexer.consume(2).to_uint();
|
||||||
|
auto day = lexer.consume(2).to_uint();
|
||||||
|
auto hour = lexer.consume(2).to_uint();
|
||||||
|
Optional<unsigned> minute, seconds, miliseconds, offset_hours, offset_minutes;
|
||||||
|
[[maybe_unused]] bool negative_offset = false;
|
||||||
|
if (!lexer.is_eof()) {
|
||||||
|
if (lexer.consume_specific('Z'))
|
||||||
|
goto done_parsing;
|
||||||
|
|
||||||
|
if (!lexer.next_is(is_any_of("+-"))) {
|
||||||
|
minute = lexer.consume(2).to_uint();
|
||||||
|
if (!minute.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (lexer.consume_specific('Z'))
|
||||||
|
goto done_parsing;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!lexer.next_is(is_any_of("+-"))) {
|
||||||
|
seconds = lexer.consume(2).to_uint();
|
||||||
|
if (!seconds.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (lexer.consume_specific('Z'))
|
||||||
|
goto done_parsing;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lexer.consume_specific('.')) {
|
||||||
|
miliseconds = lexer.consume(3).to_uint();
|
||||||
|
if (!miliseconds.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (lexer.consume_specific('Z'))
|
||||||
|
goto done_parsing;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lexer.next_is(is_any_of("+-"))) {
|
||||||
|
negative_offset = lexer.consume() == '-';
|
||||||
|
offset_hours = lexer.consume(2).to_uint();
|
||||||
|
offset_minutes = lexer.consume(2).to_uint();
|
||||||
|
if (!offset_hours.has_value() || !offset_minutes.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lexer.consume();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done_parsing:;
|
||||||
|
|
||||||
|
if (!year.has_value() || !month.has_value() || !day.has_value() || !hour.has_value()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Handle offsets!
|
||||||
|
if (offset_hours.has_value() || offset_minutes.has_value())
|
||||||
|
dbgln("FIXME: Implement GeneralizedTime with offset!");
|
||||||
|
|
||||||
|
// Unceremonially drop the miliseconds on the floor.
|
||||||
|
return Core::DateTime::create(year.value(), month.value(), day.value(), hour.value(), minute.value_or(0), seconds.value_or(0));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
#include <LibCore/DateTime.h>
|
||||||
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
|
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
|
||||||
|
|
||||||
namespace Crypto::ASN1 {
|
namespace Crypto::ASN1 {
|
||||||
|
@ -71,4 +72,7 @@ String kind_name(Kind);
|
||||||
String class_name(Class);
|
String class_name(Class);
|
||||||
String type_name(Type);
|
String type_name(Type);
|
||||||
|
|
||||||
|
Optional<Core::DateTime> parse_utc_time(const StringView&);
|
||||||
|
Optional<Core::DateTime> parse_generalized_time(const StringView&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue