|
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 *GeneralizedTime* type. |
|
17
|
|
|
*/ |
|
18
|
|
|
class GeneralizedTime 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\d\d)' . // YYYY |
|
32
|
|
|
'(\d\d)' . // MM |
|
33
|
|
|
'(\d\d)' . // DD |
|
34
|
|
|
'(\d\d)' . // hh |
|
35
|
|
|
'(\d\d)' . // mm |
|
36
|
|
|
'(\d\d)' . // ss |
|
37
|
|
|
'(?:\.(\d+))?' . // frac |
|
38
|
|
|
'Z' . // TZ |
|
39
|
|
|
'$#'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Cached formatted date. |
|
43
|
|
|
* |
|
44
|
|
|
* @var null|string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $_formatted; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor. |
|
50
|
|
|
*/ |
|
51
|
16 |
|
public function __construct(\DateTimeImmutable $dt) |
|
52
|
|
|
{ |
|
53
|
16 |
|
$this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
54
|
16 |
|
parent::__construct($dt); |
|
55
|
16 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Clear cached variables on clone. |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function __clone() |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->_formatted = null; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
10 |
|
protected function _encodedContentDER(): string |
|
69
|
|
|
{ |
|
70
|
10 |
|
if (!isset($this->_formatted)) { |
|
71
|
9 |
|
$dt = $this->_dateTime->setTimezone( |
|
72
|
9 |
|
self::_createTimeZone(self::TZ_UTC)); |
|
73
|
9 |
|
$this->_formatted = $dt->format('YmdHis'); |
|
74
|
|
|
// if fractions were used |
|
75
|
9 |
|
$frac = $dt->format('u'); |
|
76
|
9 |
|
if (0 !== intval($frac)) { |
|
77
|
6 |
|
$frac = rtrim($frac, '0'); |
|
78
|
6 |
|
$this->_formatted .= ".{$frac}"; |
|
79
|
|
|
} |
|
80
|
|
|
// timezone |
|
81
|
9 |
|
$this->_formatted .= 'Z'; |
|
82
|
|
|
} |
|
83
|
10 |
|
return $this->_formatted; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
12 |
|
protected static function _decodeFromDER(Identifier $identifier, |
|
90
|
|
|
string $data, int &$offset): ElementBase |
|
91
|
|
|
{ |
|
92
|
12 |
|
$idx = $offset; |
|
93
|
12 |
|
$length = Length::expectFromDER($data, $idx)->intLength(); |
|
94
|
12 |
|
$str = substr($data, $idx, $length); |
|
95
|
12 |
|
$idx += $length; |
|
96
|
|
|
/** @var string[] $match */ |
|
97
|
12 |
|
if (!preg_match(self::REGEX, $str, $match)) { |
|
98
|
2 |
|
throw new DecodeException('Invalid GeneralizedTime format.'); |
|
99
|
|
|
} |
|
100
|
10 |
|
[, $year, $month, $day, $hour, $minute, $second] = $match; |
|
101
|
|
|
// if fractions match, there's at least one digit |
|
102
|
10 |
|
if (isset($match[7])) { |
|
103
|
7 |
|
$frac = $match[7]; |
|
104
|
|
|
// DER restricts trailing zeroes in fractional seconds component |
|
105
|
7 |
|
if ('0' === $frac[strlen($frac) - 1]) { |
|
106
|
2 |
|
throw new DecodeException( |
|
107
|
2 |
|
'Fractional seconds must omit trailing zeroes.'); |
|
108
|
|
|
} |
|
109
|
5 |
|
$frac = $frac; |
|
110
|
|
|
} else { |
|
111
|
3 |
|
$frac = '0'; |
|
112
|
|
|
} |
|
113
|
8 |
|
$time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
114
|
8 |
|
self::TZ_UTC; |
|
115
|
8 |
|
$dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
116
|
8 |
|
self::_createTimeZone(self::TZ_UTC)); |
|
117
|
8 |
|
if (!$dt) { |
|
|
|
|
|
|
118
|
1 |
|
throw new DecodeException( |
|
119
|
|
|
'Failed to decode GeneralizedTime: ' . |
|
120
|
1 |
|
self::_getLastDateTimeImmutableErrorsStr()); |
|
121
|
|
|
} |
|
122
|
7 |
|
$offset = $idx; |
|
123
|
7 |
|
return new self($dt); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|