|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\ASN1\Type\Primitive; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Component\Identifier; |
|
8
|
|
|
use Sop\ASN1\Component\Length; |
|
9
|
|
|
use Sop\ASN1\Exception\DecodeException; |
|
10
|
|
|
use Sop\ASN1\Feature\ElementBase; |
|
11
|
|
|
use Sop\ASN1\Type\BaseTime; |
|
12
|
|
|
use Sop\ASN1\Type\PrimitiveType; |
|
13
|
|
|
use Sop\ASN1\Type\UniversalClass; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Implements *UTCTime* type. |
|
17
|
|
|
*/ |
|
18
|
|
|
class UTCTime extends BaseTime |
|
19
|
|
|
{ |
|
20
|
|
|
use UniversalClass; |
|
21
|
|
|
use PrimitiveType; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Regular expression to parse date. |
|
25
|
|
|
* |
|
26
|
|
|
* DER restricts format to UTC timezone (Z suffix). |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
const REGEX = '#^' . |
|
31
|
|
|
'(\d\d)' . // YY |
|
32
|
|
|
'(\d\d)' . // MM |
|
33
|
|
|
'(\d\d)' . // DD |
|
34
|
|
|
'(\d\d)' . // hh |
|
35
|
|
|
'(\d\d)' . // mm |
|
36
|
|
|
'(\d\d)' . // ss |
|
37
|
|
|
'Z' . // TZ |
|
38
|
|
|
'$#'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
*/ |
|
43
|
9 |
|
public function __construct(\DateTimeImmutable $dt) |
|
44
|
|
|
{ |
|
45
|
9 |
|
$this->_typeTag = self::TYPE_UTC_TIME; |
|
46
|
9 |
|
parent::__construct($dt); |
|
47
|
9 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
4 |
|
protected function _encodedContentDER(): string |
|
53
|
|
|
{ |
|
54
|
4 |
|
$dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
55
|
4 |
|
return $dt->format('ymdHis\\Z'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
12 |
|
protected static function _decodeFromDER(Identifier $identifier, |
|
62
|
|
|
string $data, int &$offset): ElementBase |
|
63
|
|
|
{ |
|
64
|
12 |
|
$idx = $offset; |
|
65
|
12 |
|
$length = Length::expectFromDER($data, $idx)->intLength(); |
|
66
|
12 |
|
$str = substr($data, $idx, $length); |
|
67
|
12 |
|
$idx += $length; |
|
68
|
|
|
/** @var string[] $match */ |
|
69
|
12 |
|
if (!preg_match(self::REGEX, $str, $match)) { |
|
70
|
5 |
|
throw new DecodeException('Invalid UTCTime format.'); |
|
71
|
|
|
} |
|
72
|
7 |
|
[, $year, $month, $day, $hour, $minute, $second] = $match; |
|
73
|
7 |
|
$time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
74
|
7 |
|
$dt = \DateTimeImmutable::createFromFormat('!ymdHisT', $time, |
|
75
|
7 |
|
self::_createTimeZone(self::TZ_UTC)); |
|
76
|
7 |
|
if (!$dt) { |
|
|
|
|
|
|
77
|
|
|
throw new DecodeException('Failed to decode UTCTime: ' . |
|
78
|
|
|
self::_getLastDateTimeImmutableErrorsStr()); |
|
79
|
|
|
} |
|
80
|
7 |
|
$offset = $idx; |
|
81
|
7 |
|
return new self($dt); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|