From bf86e8e273729fb5a5c78a808a227c91a9d54115 Mon Sep 17 00:00:00 2001 From: Joni Eskelinen Date: Mon, 20 May 2019 10:28:17 +0300 Subject: [PATCH] Fix bug with leading fractional zeroes --- lib/ASN1/Type/Primitive/GeneralizedTime.php | 4 ++-- .../generalized-time/GeneralizedTimeTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/ASN1/Type/Primitive/GeneralizedTime.php b/lib/ASN1/Type/Primitive/GeneralizedTime.php index d77c7a2..cef3570 100644 --- a/lib/ASN1/Type/Primitive/GeneralizedTime.php +++ b/lib/ASN1/Type/Primitive/GeneralizedTime.php @@ -109,9 +109,9 @@ protected static function _decodeFromDER(Identifier $identifier, throw new DecodeException( "Fractional seconds must omit trailing zeroes."); } - $frac = (int) $frac; + $frac = $frac; } else { - $frac = 0; + $frac = '0'; } $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . self::TZ_UTC; diff --git a/test/tests/type/primitive/generalized-time/GeneralizedTimeTest.php b/test/tests/type/primitive/generalized-time/GeneralizedTimeTest.php index ea09580..e20b3da 100644 --- a/test/tests/type/primitive/generalized-time/GeneralizedTimeTest.php +++ b/test/tests/type/primitive/generalized-time/GeneralizedTimeTest.php @@ -102,4 +102,19 @@ public function testClone(Element $el) $clone = clone $el; $this->assertInstanceOf(GeneralizedTime::class, $clone); } + + /** + * Test bug where leading zeroes in fraction gets stripped, + * such that `.05` becomes `.5`. + */ + public function testLeadingFractionZeroes() + { + $ts = strtotime('Mon Jan 2 15:04:05 MST 2006'); + $dt = \DateTimeImmutable::createFromFormat('U.u', "{$ts}.05", + new \DateTimeZone('UTC')); + $el = new GeneralizedTime($dt); + $der = $el->toDER(); + $el = GeneralizedTime::fromDER($der); + $this->assertEquals($der, $el->toDER()); + } }