Skip to content

Truly independent web browser

LibCrypto: Remove the ASN.1 UTCTime and GeneralizedTime parsers

Shannon Bootha613963e5538
diff --git a/Libraries/LibCrypto/ASN1/ASN1.cpp b/Libraries/LibCrypto/ASN1/ASN1.cpp
index 6271a9d8f8b..876aae66ad2 100644
--- a/Libraries/LibCrypto/ASN1/ASN1.cpp
+++ b/Libraries/LibCrypto/ASN1/ASN1.cpp
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/GenericLexer.h>
#include <LibCrypto/ASN1/ASN1.h>
namespace Crypto::ASN1 {
@@ -91,122 +90,4 @@ StringView kind_name(Kind kind)
return "InvalidKind"sv;
}
-Optional<UnixDateTime> parse_utc_time(StringView time)
-{
- // YYMMDDhhmm[ss]Z or YYMMDDhhmm[ss](+|-)hhmm
- GenericLexer lexer(time);
- auto year_in_century = lexer.consume(2).to_number<unsigned>();
- auto month = lexer.consume(2).to_number<unsigned>();
- auto day = lexer.consume(2).to_number<unsigned>();
- auto hour = lexer.consume(2).to_number<unsigned>();
- auto minute = lexer.consume(2).to_number<unsigned>();
- Optional<unsigned> seconds, offset_hours, offset_minutes;
- [[maybe_unused]] bool negative_offset = false;
-
- if (lexer.next_is(is_any_of("0123456789"sv))) {
- seconds = lexer.consume(2).to_number<unsigned>();
- if (!seconds.has_value()) {
- return {};
- }
- }
-
- if (lexer.next_is('Z')) {
- lexer.consume();
- } else if (lexer.next_is(is_any_of("+-"sv))) {
- negative_offset = lexer.consume() == '-';
- offset_hours = lexer.consume(2).to_number<unsigned>();
- offset_minutes = lexer.consume(2).to_number<unsigned>();
- if (!offset_hours.has_value() || !offset_minutes.has_value()) {
- return {};
- }
- } else {
- return {};
- }
-
- if (!year_in_century.has_value() || !month.has_value() || !day.has_value() || !hour.has_value() || !minute.has_value()) {
- return {};
- }
-
- // RFC5280 section 4.1.2.5.1.
- auto full_year = year_in_century.value();
- full_year += (full_year < 50) ? 2000 : 1900;
- 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 UnixDateTime::from_unix_time_parts(full_year, month.value(), day.value(), hour.value(), minute.value(), full_seconds, 0);
-}
-
-Optional<UnixDateTime> parse_generalized_time(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_number<unsigned>();
- auto month = lexer.consume(2).to_number<unsigned>();
- auto day = lexer.consume(2).to_number<unsigned>();
- auto hour = lexer.consume(2).to_number<unsigned>();
- Optional<unsigned> minute, seconds, milliseconds, 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("+-"sv))) {
- minute = lexer.consume(2).to_number<unsigned>();
- if (!minute.has_value()) {
- return {};
- }
- if (lexer.is_eof() || lexer.consume_specific('Z'))
- goto done_parsing;
- }
-
- if (!lexer.next_is(is_any_of("+-"sv))) {
- seconds = lexer.consume(2).to_number<unsigned>();
- if (!seconds.has_value()) {
- return {};
- }
- if (lexer.is_eof() || lexer.consume_specific('Z'))
- goto done_parsing;
- }
-
- if (lexer.consume_specific('.')) {
- milliseconds = lexer.consume(3).to_number<unsigned>();
- if (!milliseconds.has_value()) {
- return {};
- }
- if (lexer.is_eof() || lexer.consume_specific('Z'))
- goto done_parsing;
- }
-
- if (lexer.next_is(is_any_of("+-"sv))) {
- negative_offset = lexer.consume() == '-';
- offset_hours = lexer.consume(2).to_number<unsigned>();
- offset_minutes = lexer.consume(2).to_number<unsigned>();
- if (!offset_hours.has_value() || !offset_minutes.has_value()) {
- return {};
- }
- }
-
- // Any character would be garbage.
- if (!lexer.is_eof()) {
- return {};
- }
- }
-
-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!");
-
- return UnixDateTime::from_unix_time_parts(year.value(), month.value(), day.value(), hour.value(), minute.value_or(0), seconds.value_or(0), milliseconds.value_or(0));
-}
-
}
diff --git a/Libraries/LibCrypto/ASN1/ASN1.h b/Libraries/LibCrypto/ASN1/ASN1.h
index 8743254dc1a..9a9a8fa7f16 100644
--- a/Libraries/LibCrypto/ASN1/ASN1.h
+++ b/Libraries/LibCrypto/ASN1/ASN1.h
@@ -7,7 +7,6 @@
#pragma once
#include <AK/StringView.h>
-#include <AK/Time.h>
#include <AK/Types.h>
namespace Crypto::ASN1 {
@@ -73,7 +72,4 @@ struct Tag {
StringView kind_name(Kind);
-Optional<UnixDateTime> parse_utc_time(StringView);
-Optional<UnixDateTime> parse_generalized_time(StringView);
-
}
diff --git a/Tests/LibCrypto/TestASN1.cpp b/Tests/LibCrypto/TestASN1.cpp
index 87c904a84d6..52c83b61098 100644
--- a/Tests/LibCrypto/TestASN1.cpp
+++ b/Tests/LibCrypto/TestASN1.cpp
@@ -5,166 +5,10 @@
*/
#include <AK/StringView.h>
-#include <AK/Time.h>
#include <LibCrypto/ASN1/ASN1.h>
#include <LibCrypto/ASN1/DER.h>
#include <LibTest/TestCase.h>
-#define EXPECT_DATETIME(sv, y, mo, d, h, mi, s) \
- EXPECT_EQ(Crypto::ASN1::parse_utc_time(sv).value(), UnixDateTime::from_unix_time_parts(y, mo, d, h, mi, s, 0))
-
-TEST_CASE(test_utc_boring)
-{
- // YYMMDDhhmm[ss]Z
- EXPECT_DATETIME("010101010101Z"sv, 2001, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("010203040506Z"sv, 2001, 2, 3, 4, 5, 6);
- EXPECT_DATETIME("020406081012Z"sv, 2002, 4, 6, 8, 10, 12);
- EXPECT_DATETIME("0204060810Z"sv, 2002, 4, 6, 8, 10, 0);
- EXPECT_DATETIME("220911220000Z"sv, 2022, 9, 11, 22, 0, 0);
-}
-
-TEST_CASE(test_utc_year_rollover)
-{
- // YYMMDDhhmm[ss]Z
- EXPECT_DATETIME("000101010101Z"sv, 2000, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("010101010101Z"sv, 2001, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("020101010101Z"sv, 2002, 1, 1, 1, 1, 1);
- // ...
- EXPECT_DATETIME("480101010101Z"sv, 2048, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("490101010101Z"sv, 2049, 1, 1, 1, 1, 1);
- // This Y2050-problem is hardcoded in the spec. Oh no.
- EXPECT_DATETIME("500101010101Z"sv, 1950, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("510101010101Z"sv, 1951, 1, 1, 1, 1, 1);
- // ...
- EXPECT_DATETIME("970101010101Z"sv, 1997, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("980101010101Z"sv, 1998, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("990101010101Z"sv, 1999, 1, 1, 1, 1, 1);
-}
-
-TEST_CASE(test_utc_offset)
-{
- // YYMMDDhhmm[ss](+|-)hhmm
- // We don't yet support storing the offset anywhere and instead just assume that the offset is just +0000.
- EXPECT_DATETIME("010101010101+0000"sv, 2001, 1, 1, 1, 1, 1);
- EXPECT_DATETIME("010203040506+0000"sv, 2001, 2, 3, 4, 5, 6);
- EXPECT_DATETIME("020406081012+0000"sv, 2002, 4, 6, 8, 10, 12);
- EXPECT_DATETIME("0204060810+0000"sv, 2002, 4, 6, 8, 10, 0);
- EXPECT_DATETIME("220911220000+0000"sv, 2022, 9, 11, 22, 0, 0);
- // Designed to fail once we support offsets:
- EXPECT_DATETIME("220911220000+0600"sv, 2022, 9, 11, 22, 0, 0);
-}
-
-TEST_CASE(test_utc_missing_z)
-{
- // YYMMDDhhmm[ss]
- // We don't actually need to parse this correctly; rejecting these inputs is fine.
- // This test just makes sure that we don't crash.
- (void)Crypto::ASN1::parse_utc_time("010101010101"sv);
- (void)Crypto::ASN1::parse_utc_time("010203040506"sv);
- (void)Crypto::ASN1::parse_utc_time("020406081012"sv);
- (void)Crypto::ASN1::parse_utc_time("0204060810"sv);
- (void)Crypto::ASN1::parse_utc_time("220911220000"sv);
-}
-
-#undef EXPECT_DATETIME
-#define EXPECT_DATETIME(sv, y, mo, d, h, mi, s, ms) \
- EXPECT_EQ(Crypto::ASN1::parse_generalized_time(sv).value(), UnixDateTime::from_unix_time_parts(y, mo, d, h, mi, s, ms))
-
-TEST_CASE(test_generalized_boring)
-{
- // YYYYMMDDhh[mm[ss[.fff]]]
- EXPECT_DATETIME("20010101010101Z"sv, 2001, 1, 1, 1, 1, 1, 0);
- EXPECT_DATETIME("20010203040506Z"sv, 2001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("20020406081012Z"sv, 2002, 4, 6, 8, 10, 12, 0);
- EXPECT_DATETIME("200204060810Z"sv, 2002, 4, 6, 8, 10, 0, 0);
- EXPECT_DATETIME("2002040608Z"sv, 2002, 4, 6, 8, 0, 0, 0);
- EXPECT_DATETIME("20020406081012.567Z"sv, 2002, 4, 6, 8, 10, 12, 567);
- EXPECT_DATETIME("20220911220000Z"sv, 2022, 9, 11, 22, 0, 0, 0);
-}
-
-TEST_CASE(test_generalized_offset)
-{
- // YYYYMMDDhh[mm[ss[.fff]]](+|-)hhmm
- // We don't yet support storing the offset anywhere and instead just assume that the offset is just +0000.
- EXPECT_DATETIME("20010101010101+0000"sv, 2001, 1, 1, 1, 1, 1, 0);
- EXPECT_DATETIME("20010203040506+0000"sv, 2001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("20020406081012+0000"sv, 2002, 4, 6, 8, 10, 12, 0);
- EXPECT_DATETIME("200204060810+0000"sv, 2002, 4, 6, 8, 10, 0, 0);
- EXPECT_DATETIME("2002040608+0000"sv, 2002, 4, 6, 8, 0, 0, 0);
- EXPECT_DATETIME("20020406081012.567+0000"sv, 2002, 4, 6, 8, 10, 12, 567);
- EXPECT_DATETIME("20220911220000+0000"sv, 2022, 9, 11, 22, 0, 0, 0);
- // Designed to fail once we support offsets:
- EXPECT_DATETIME("20220911220000+0600"sv, 2022, 9, 11, 22, 0, 0, 0);
-}
-
-TEST_CASE(test_generalized_missing_z)
-{
- // YYYYMMDDhh[mm[ss[.fff]]]
- EXPECT_DATETIME("20010101010101"sv, 2001, 1, 1, 1, 1, 1, 0);
- EXPECT_DATETIME("20010203040506"sv, 2001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("20020406081012"sv, 2002, 4, 6, 8, 10, 12, 0);
- EXPECT_DATETIME("200204060810"sv, 2002, 4, 6, 8, 10, 0, 0);
- EXPECT_DATETIME("2002040608"sv, 2002, 4, 6, 8, 0, 0, 0);
- EXPECT_DATETIME("20020406081012.567"sv, 2002, 4, 6, 8, 10, 12, 567);
- EXPECT_DATETIME("20220911220000"sv, 2022, 9, 11, 22, 0, 0, 0);
-}
-
-TEST_CASE(test_generalized_unusual_year)
-{
- // Towards the positive
- EXPECT_DATETIME("20010203040506Z"sv, 2001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("20110203040506Z"sv, 2011, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("21010203040506Z"sv, 2101, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("30010203040506Z"sv, 3001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("40010203040506Z"sv, 4001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("90010203040506Z"sv, 9001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("99990203040506Z"sv, 9999, 2, 3, 4, 5, 6, 0);
-
- // Towards zero
- EXPECT_DATETIME("20010203040506Z"sv, 2001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("19990203040506Z"sv, 1999, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("19500203040506Z"sv, 1950, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("19010203040506Z"sv, 1901, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("18010203040506Z"sv, 1801, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("15010203040506Z"sv, 1501, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("10010203040506Z"sv, 1001, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("01010203040506Z"sv, 101, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("00110203040506Z"sv, 11, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("00010203040506Z"sv, 1, 2, 3, 4, 5, 6, 0);
- EXPECT_DATETIME("00000203040506Z"sv, 0, 2, 3, 4, 5, 6, 0);
-
- // Problematic dates
- EXPECT_DATETIME("20200229040506Z"sv, 2020, 2, 29, 4, 5, 6, 0);
- EXPECT_DATETIME("20000229040506Z"sv, 2000, 2, 29, 4, 5, 6, 0);
- EXPECT_DATETIME("24000229040506Z"sv, 2400, 2, 29, 4, 5, 6, 0);
-}
-
-TEST_CASE(test_generalized_nonexistent_dates)
-{
- // The following dates don't exist. I'm not sure what the "correct" result is,
- // but we need to make sure that we don't crash.
- (void)Crypto::ASN1::parse_generalized_time("20210229040506Z"sv); // Not a leap year (not divisible by 4)
- (void)Crypto::ASN1::parse_generalized_time("21000229040506Z"sv); // Not a leap year (divisible by 100)
- (void)Crypto::ASN1::parse_generalized_time("20220230040506Z"sv); // Never exists
- (void)Crypto::ASN1::parse_generalized_time("20220631040506Z"sv); // Never exists
- (void)Crypto::ASN1::parse_generalized_time("20220732040506Z"sv); // Never exists
-
- // https://www.timeanddate.com/calendar/julian-gregorian-switch.html
- (void)Crypto::ASN1::parse_generalized_time("15821214040506Z"sv); // Gregorian switch; France
- (void)Crypto::ASN1::parse_generalized_time("15821011040506Z"sv); // Gregorian switch; Italy, Poland, Portugal, Spain
- (void)Crypto::ASN1::parse_generalized_time("15830105040506Z"sv); // Gregorian switch; Germany (Catholic)
- (void)Crypto::ASN1::parse_generalized_time("15831011040506Z"sv); // Gregorian switch; Austria
- (void)Crypto::ASN1::parse_generalized_time("15871026040506Z"sv); // Gregorian switch; Hungary
- (void)Crypto::ASN1::parse_generalized_time("16100826040506Z"sv); // Gregorian switch; Germany (old Prussia)
- (void)Crypto::ASN1::parse_generalized_time("17000223040506Z"sv); // Gregorian switch; Germany (Protestant)
- (void)Crypto::ASN1::parse_generalized_time("17520908040506Z"sv); // Gregorian switch; US, Canada, UK
- (void)Crypto::ASN1::parse_generalized_time("18711225040506Z"sv); // Gregorian switch; Japan
- (void)Crypto::ASN1::parse_generalized_time("19160407040506Z"sv); // Gregorian switch; Bulgaria
- (void)Crypto::ASN1::parse_generalized_time("19180207040506Z"sv); // Gregorian switch; Estonia, Russia
- (void)Crypto::ASN1::parse_generalized_time("19230222040506Z"sv); // Gregorian switch; Greece
- (void)Crypto::ASN1::parse_generalized_time("19261224040506Z"sv); // Gregorian switch; Turkey
-}
-
TEST_CASE(test_encoder_primitives)
{
auto roundtrip_value = [](auto value) {