Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Asn1Decode.sol
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ library Asn1Decode {
require(year >= 1970);
require(1 <= month && month <= 12);
require(1 <= day && day <= 31);
if (month == 2) {
require(day <= 28 || (day == 29 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))));
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
require(day <= 30);
}
require(hour <= 23);
require(minute <= 59);
require(second <= 59);
Expand Down
20 changes: 20 additions & 0 deletions test/Asn1Decode.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ contract Asn1DecodeTest is Test {
assertEq(h.timestampAtRoot(_generalizedTime("20240101000000Z")), 1704067200);
}

function test_timestamp_february30_reverts() public {
vm.expectRevert();
h.timestampAtRoot(_generalizedTime("20240230000000Z"));
}

function test_timestamp_april31_reverts() public {
vm.expectRevert();
h.timestampAtRoot(_generalizedTime("20240431000000Z"));
}

function test_timestamp_nonLeapYearFebruary29_reverts() public {
vm.expectRevert();
h.timestampAtRoot(_generalizedTime("20230229000000Z"));
}

function test_timestamp_leapYearFebruary29() public view {
// GeneralizedTime "20240229000000Z" -> 2024-02-29T00:00:00Z
assertEq(h.timestampAtRoot(_generalizedTime("20240229000000Z")), 1709164800);
}

function test_timestamp_wrongType_reverts() public {
bytes memory der = abi.encodePacked(hex"160d", bytes("700101000000Z")); // type 0x16
vm.expectRevert(Asn1Decode.InvalidAsn1Value.selector);
Expand Down
Loading