@@ -16,73 +16,73 @@ |
||
16 | 16 | */ |
17 | 17 | class UTCTime extends TimeType |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Regular expression to parse date. |
|
24 | - * |
|
25 | - * DER restricts format to UTC timezone (Z suffix). |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - const REGEX = /* @formatter:off */ '#^' . |
|
30 | - '(\d\d)' . /* YY */ |
|
31 | - '(\d\d)' . /* MM */ |
|
32 | - '(\d\d)' . /* DD */ |
|
33 | - '(\d\d)' . /* hh */ |
|
34 | - '(\d\d)' . /* mm */ |
|
35 | - '(\d\d)' . /* ss */ |
|
36 | - 'Z' . /* TZ */ |
|
37 | - '$#' /* @formatter:on */; |
|
22 | + /** |
|
23 | + * Regular expression to parse date. |
|
24 | + * |
|
25 | + * DER restricts format to UTC timezone (Z suffix). |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + const REGEX = /* @formatter:off */ '#^' . |
|
30 | + '(\d\d)' . /* YY */ |
|
31 | + '(\d\d)' . /* MM */ |
|
32 | + '(\d\d)' . /* DD */ |
|
33 | + '(\d\d)' . /* hh */ |
|
34 | + '(\d\d)' . /* mm */ |
|
35 | + '(\d\d)' . /* ss */ |
|
36 | + 'Z' . /* TZ */ |
|
37 | + '$#' /* @formatter:on */; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Constructor. |
|
41 | - * |
|
42 | - * @param \DateTimeImmutable $dt |
|
43 | - */ |
|
44 | - public function __construct(\DateTimeImmutable $dt) |
|
45 | - { |
|
46 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
47 | - parent::__construct($dt); |
|
48 | - } |
|
39 | + /** |
|
40 | + * Constructor. |
|
41 | + * |
|
42 | + * @param \DateTimeImmutable $dt |
|
43 | + */ |
|
44 | + public function __construct(\DateTimeImmutable $dt) |
|
45 | + { |
|
46 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
47 | + parent::__construct($dt); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
57 | - return $dt->format("ymdHis\Z"); |
|
58 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
57 | + return $dt->format("ymdHis\Z"); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * |
|
62 | - * {@inheritdoc} |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - protected static function _decodeFromDER(Identifier $identifier, |
|
66 | - string $data, int &$offset): ElementBase |
|
67 | - { |
|
68 | - $idx = $offset; |
|
69 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
70 | - $str = substr($data, $idx, $length); |
|
71 | - $idx += $length; |
|
72 | - /** @var $match string[] */ |
|
73 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
74 | - throw new DecodeException("Invalid UTCTime format."); |
|
75 | - } |
|
76 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
77 | - $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
78 | - $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
79 | - self::_createTimeZone(self::TZ_UTC)); |
|
80 | - if (!$dt) { |
|
81 | - throw new DecodeException( |
|
82 | - "Failed to decode UTCTime: " . |
|
83 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
84 | - } |
|
85 | - $offset = $idx; |
|
86 | - return new self($dt); |
|
87 | - } |
|
60 | + /** |
|
61 | + * |
|
62 | + * {@inheritdoc} |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + protected static function _decodeFromDER(Identifier $identifier, |
|
66 | + string $data, int &$offset): ElementBase |
|
67 | + { |
|
68 | + $idx = $offset; |
|
69 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
70 | + $str = substr($data, $idx, $length); |
|
71 | + $idx += $length; |
|
72 | + /** @var $match string[] */ |
|
73 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
74 | + throw new DecodeException("Invalid UTCTime format."); |
|
75 | + } |
|
76 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
77 | + $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
78 | + $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
79 | + self::_createTimeZone(self::TZ_UTC)); |
|
80 | + if (!$dt) { |
|
81 | + throw new DecodeException( |
|
82 | + "Failed to decode UTCTime: " . |
|
83 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
84 | + } |
|
85 | + $offset = $idx; |
|
86 | + return new self($dt); |
|
87 | + } |
|
88 | 88 | } |
@@ -16,64 +16,64 @@ |
||
16 | 16 | */ |
17 | 17 | class Boolean extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Value. |
|
24 | - * |
|
25 | - * @var bool |
|
26 | - */ |
|
27 | - private $_bool; |
|
22 | + /** |
|
23 | + * Value. |
|
24 | + * |
|
25 | + * @var bool |
|
26 | + */ |
|
27 | + private $_bool; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param bool $bool |
|
33 | - */ |
|
34 | - public function __construct(bool $bool) |
|
35 | - { |
|
36 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
37 | - $this->_bool = $bool; |
|
38 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param bool $bool |
|
33 | + */ |
|
34 | + public function __construct(bool $bool) |
|
35 | + { |
|
36 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
37 | + $this->_bool = $bool; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get the value. |
|
42 | - * |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function value(): bool |
|
46 | - { |
|
47 | - return $this->_bool; |
|
48 | - } |
|
40 | + /** |
|
41 | + * Get the value. |
|
42 | + * |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function value(): bool |
|
46 | + { |
|
47 | + return $this->_bool; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - return $this->_bool ? chr(0xff) : chr(0); |
|
57 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + return $this->_bool ? chr(0xff) : chr(0); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * |
|
61 | - * {@inheritdoc} |
|
62 | - * @return self |
|
63 | - */ |
|
64 | - protected static function _decodeFromDER(Identifier $identifier, |
|
65 | - string $data, int &$offset): ElementBase |
|
66 | - { |
|
67 | - $idx = $offset; |
|
68 | - Length::expectFromDER($data, $idx, 1); |
|
69 | - $byte = ord($data[$idx++]); |
|
70 | - if ($byte !== 0) { |
|
71 | - if ($byte != 0xff) { |
|
72 | - throw new DecodeException( |
|
73 | - "DER encoded boolean true must have all bits set to 1."); |
|
74 | - } |
|
75 | - } |
|
76 | - $offset = $idx; |
|
77 | - return new self($byte !== 0); |
|
78 | - } |
|
59 | + /** |
|
60 | + * |
|
61 | + * {@inheritdoc} |
|
62 | + * @return self |
|
63 | + */ |
|
64 | + protected static function _decodeFromDER(Identifier $identifier, |
|
65 | + string $data, int &$offset): ElementBase |
|
66 | + { |
|
67 | + $idx = $offset; |
|
68 | + Length::expectFromDER($data, $idx, 1); |
|
69 | + $byte = ord($data[$idx++]); |
|
70 | + if ($byte !== 0) { |
|
71 | + if ($byte != 0xff) { |
|
72 | + throw new DecodeException( |
|
73 | + "DER encoded boolean true must have all bits set to 1."); |
|
74 | + } |
|
75 | + } |
|
76 | + $offset = $idx; |
|
77 | + return new self($byte !== 0); |
|
78 | + } |
|
79 | 79 | } |
@@ -16,195 +16,195 @@ |
||
16 | 16 | */ |
17 | 17 | class BitString extends StringType |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Number of unused bits in the last octet. |
|
24 | - * |
|
25 | - * @var int $_unusedBits |
|
26 | - */ |
|
27 | - protected $_unusedBits; |
|
22 | + /** |
|
23 | + * Number of unused bits in the last octet. |
|
24 | + * |
|
25 | + * @var int $_unusedBits |
|
26 | + */ |
|
27 | + protected $_unusedBits; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param string $string Content octets |
|
33 | - * @param int $unused_bits Number of unused bits in the last octet |
|
34 | - */ |
|
35 | - public function __construct(string $string, int $unused_bits = 0) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
38 | - parent::__construct($string); |
|
39 | - $this->_unusedBits = $unused_bits; |
|
40 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param string $string Content octets |
|
33 | + * @param int $unused_bits Number of unused bits in the last octet |
|
34 | + */ |
|
35 | + public function __construct(string $string, int $unused_bits = 0) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
38 | + parent::__construct($string); |
|
39 | + $this->_unusedBits = $unused_bits; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Get the number of bits in the string. |
|
44 | - * |
|
45 | - * @return int |
|
46 | - */ |
|
47 | - public function numBits(): int |
|
48 | - { |
|
49 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
50 | - } |
|
42 | + /** |
|
43 | + * Get the number of bits in the string. |
|
44 | + * |
|
45 | + * @return int |
|
46 | + */ |
|
47 | + public function numBits(): int |
|
48 | + { |
|
49 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Get the number of unused bits in the last octet of the string. |
|
54 | - * |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function unusedBits(): int |
|
58 | - { |
|
59 | - return $this->_unusedBits; |
|
60 | - } |
|
52 | + /** |
|
53 | + * Get the number of unused bits in the last octet of the string. |
|
54 | + * |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function unusedBits(): int |
|
58 | + { |
|
59 | + return $this->_unusedBits; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Test whether bit is set. |
|
64 | - * |
|
65 | - * @param int $idx Bit index. |
|
66 | - * Most significant bit of the first octet is index 0. |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function testBit(int $idx): bool |
|
70 | - { |
|
71 | - // octet index |
|
72 | - $oi = (int) floor($idx / 8); |
|
73 | - // if octet is outside range |
|
74 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
75 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
76 | - } |
|
77 | - // bit index |
|
78 | - $bi = $idx % 8; |
|
79 | - // if tested bit is last octet's unused bit |
|
80 | - if ($oi == strlen($this->_string) - 1) { |
|
81 | - if ($bi >= 8 - $this->_unusedBits) { |
|
82 | - throw new \OutOfBoundsException( |
|
83 | - "Index refers to an unused bit."); |
|
84 | - } |
|
85 | - } |
|
86 | - $byte = $this->_string[$oi]; |
|
87 | - // index 0 is the most significant bit in byte |
|
88 | - $mask = 0x01 << (7 - $bi); |
|
89 | - return (ord($byte) & $mask) > 0; |
|
90 | - } |
|
62 | + /** |
|
63 | + * Test whether bit is set. |
|
64 | + * |
|
65 | + * @param int $idx Bit index. |
|
66 | + * Most significant bit of the first octet is index 0. |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function testBit(int $idx): bool |
|
70 | + { |
|
71 | + // octet index |
|
72 | + $oi = (int) floor($idx / 8); |
|
73 | + // if octet is outside range |
|
74 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
75 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
76 | + } |
|
77 | + // bit index |
|
78 | + $bi = $idx % 8; |
|
79 | + // if tested bit is last octet's unused bit |
|
80 | + if ($oi == strlen($this->_string) - 1) { |
|
81 | + if ($bi >= 8 - $this->_unusedBits) { |
|
82 | + throw new \OutOfBoundsException( |
|
83 | + "Index refers to an unused bit."); |
|
84 | + } |
|
85 | + } |
|
86 | + $byte = $this->_string[$oi]; |
|
87 | + // index 0 is the most significant bit in byte |
|
88 | + $mask = 0x01 << (7 - $bi); |
|
89 | + return (ord($byte) & $mask) > 0; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Get range of bits. |
|
94 | - * |
|
95 | - * @param int $start Index of first bit |
|
96 | - * @param int $length Number of bits in range |
|
97 | - * @throws \OutOfBoundsException |
|
98 | - * @return string Integer of $length bits |
|
99 | - */ |
|
100 | - public function range(int $start, int $length): string |
|
101 | - { |
|
102 | - if (!$length) { |
|
103 | - return "0"; |
|
104 | - } |
|
105 | - if ($start + $length > $this->numBits()) { |
|
106 | - throw new \OutOfBoundsException("Not enough bits."); |
|
107 | - } |
|
108 | - $bits = gmp_init(0); |
|
109 | - $idx = $start; |
|
110 | - $end = $start + $length; |
|
111 | - while (true) { |
|
112 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
113 | - $bits |= $bit; |
|
114 | - if (++$idx >= $end) { |
|
115 | - break; |
|
116 | - } |
|
117 | - $bits <<= 1; |
|
118 | - } |
|
119 | - return gmp_strval($bits, 10); |
|
120 | - } |
|
92 | + /** |
|
93 | + * Get range of bits. |
|
94 | + * |
|
95 | + * @param int $start Index of first bit |
|
96 | + * @param int $length Number of bits in range |
|
97 | + * @throws \OutOfBoundsException |
|
98 | + * @return string Integer of $length bits |
|
99 | + */ |
|
100 | + public function range(int $start, int $length): string |
|
101 | + { |
|
102 | + if (!$length) { |
|
103 | + return "0"; |
|
104 | + } |
|
105 | + if ($start + $length > $this->numBits()) { |
|
106 | + throw new \OutOfBoundsException("Not enough bits."); |
|
107 | + } |
|
108 | + $bits = gmp_init(0); |
|
109 | + $idx = $start; |
|
110 | + $end = $start + $length; |
|
111 | + while (true) { |
|
112 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
113 | + $bits |= $bit; |
|
114 | + if (++$idx >= $end) { |
|
115 | + break; |
|
116 | + } |
|
117 | + $bits <<= 1; |
|
118 | + } |
|
119 | + return gmp_strval($bits, 10); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Get a copy of the bit string with trailing zeroes removed. |
|
124 | - * |
|
125 | - * @return self |
|
126 | - */ |
|
127 | - public function withoutTrailingZeroes(): self |
|
128 | - { |
|
129 | - // if bit string was empty |
|
130 | - if (!strlen($this->_string)) { |
|
131 | - return new self(""); |
|
132 | - } |
|
133 | - $bits = $this->_string; |
|
134 | - // count number of empty trailing octets |
|
135 | - $unused_octets = 0; |
|
136 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
137 | - if ($bits[$idx] != "\x0") { |
|
138 | - break; |
|
139 | - } |
|
140 | - } |
|
141 | - // strip trailing octets |
|
142 | - if ($unused_octets) { |
|
143 | - $bits = substr($bits, 0, -$unused_octets); |
|
144 | - } |
|
145 | - // if bit string was full of zeroes |
|
146 | - if (!strlen($bits)) { |
|
147 | - return new self(""); |
|
148 | - } |
|
149 | - // count number of trailing zeroes in the last octet |
|
150 | - $unused_bits = 0; |
|
151 | - $byte = ord($bits[strlen($bits) - 1]); |
|
152 | - while (!($byte & 0x01)) { |
|
153 | - $unused_bits++; |
|
154 | - $byte >>= 1; |
|
155 | - } |
|
156 | - return new self($bits, $unused_bits); |
|
157 | - } |
|
122 | + /** |
|
123 | + * Get a copy of the bit string with trailing zeroes removed. |
|
124 | + * |
|
125 | + * @return self |
|
126 | + */ |
|
127 | + public function withoutTrailingZeroes(): self |
|
128 | + { |
|
129 | + // if bit string was empty |
|
130 | + if (!strlen($this->_string)) { |
|
131 | + return new self(""); |
|
132 | + } |
|
133 | + $bits = $this->_string; |
|
134 | + // count number of empty trailing octets |
|
135 | + $unused_octets = 0; |
|
136 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
137 | + if ($bits[$idx] != "\x0") { |
|
138 | + break; |
|
139 | + } |
|
140 | + } |
|
141 | + // strip trailing octets |
|
142 | + if ($unused_octets) { |
|
143 | + $bits = substr($bits, 0, -$unused_octets); |
|
144 | + } |
|
145 | + // if bit string was full of zeroes |
|
146 | + if (!strlen($bits)) { |
|
147 | + return new self(""); |
|
148 | + } |
|
149 | + // count number of trailing zeroes in the last octet |
|
150 | + $unused_bits = 0; |
|
151 | + $byte = ord($bits[strlen($bits) - 1]); |
|
152 | + while (!($byte & 0x01)) { |
|
153 | + $unused_bits++; |
|
154 | + $byte >>= 1; |
|
155 | + } |
|
156 | + return new self($bits, $unused_bits); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * |
|
161 | - * {@inheritdoc} |
|
162 | - */ |
|
163 | - protected function _encodedContentDER(): string |
|
164 | - { |
|
165 | - $der = chr($this->_unusedBits); |
|
166 | - $der .= $this->_string; |
|
167 | - if ($this->_unusedBits) { |
|
168 | - $octet = $der[strlen($der) - 1]; |
|
169 | - // set unused bits to zero |
|
170 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
171 | - $der[strlen($der) - 1] = $octet; |
|
172 | - } |
|
173 | - return $der; |
|
174 | - } |
|
159 | + /** |
|
160 | + * |
|
161 | + * {@inheritdoc} |
|
162 | + */ |
|
163 | + protected function _encodedContentDER(): string |
|
164 | + { |
|
165 | + $der = chr($this->_unusedBits); |
|
166 | + $der .= $this->_string; |
|
167 | + if ($this->_unusedBits) { |
|
168 | + $octet = $der[strlen($der) - 1]; |
|
169 | + // set unused bits to zero |
|
170 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
171 | + $der[strlen($der) - 1] = $octet; |
|
172 | + } |
|
173 | + return $der; |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * |
|
178 | - * {@inheritdoc} |
|
179 | - * @return self |
|
180 | - */ |
|
181 | - protected static function _decodeFromDER(Identifier $identifier, |
|
182 | - string $data, int &$offset): ElementBase |
|
183 | - { |
|
184 | - $idx = $offset; |
|
185 | - $length = Length::expectFromDER($data, $idx); |
|
186 | - if ($length->intLength() < 1) { |
|
187 | - throw new DecodeException("Bit string length must be at least 1."); |
|
188 | - } |
|
189 | - $unused_bits = ord($data[$idx++]); |
|
190 | - if ($unused_bits > 7) { |
|
191 | - throw new DecodeException( |
|
192 | - "Unused bits in a bit string must be less than 8."); |
|
193 | - } |
|
194 | - $str_len = $length->intLength() - 1; |
|
195 | - if ($str_len) { |
|
196 | - $str = substr($data, $idx, $str_len); |
|
197 | - if ($unused_bits) { |
|
198 | - $mask = (1 << $unused_bits) - 1; |
|
199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
200 | - throw new DecodeException( |
|
201 | - "DER encoded bit string must have zero padding."); |
|
202 | - } |
|
203 | - } |
|
204 | - } else { |
|
205 | - $str = ""; |
|
206 | - } |
|
207 | - $offset = $idx + $str_len; |
|
208 | - return new self($str, $unused_bits); |
|
209 | - } |
|
176 | + /** |
|
177 | + * |
|
178 | + * {@inheritdoc} |
|
179 | + * @return self |
|
180 | + */ |
|
181 | + protected static function _decodeFromDER(Identifier $identifier, |
|
182 | + string $data, int &$offset): ElementBase |
|
183 | + { |
|
184 | + $idx = $offset; |
|
185 | + $length = Length::expectFromDER($data, $idx); |
|
186 | + if ($length->intLength() < 1) { |
|
187 | + throw new DecodeException("Bit string length must be at least 1."); |
|
188 | + } |
|
189 | + $unused_bits = ord($data[$idx++]); |
|
190 | + if ($unused_bits > 7) { |
|
191 | + throw new DecodeException( |
|
192 | + "Unused bits in a bit string must be less than 8."); |
|
193 | + } |
|
194 | + $str_len = $length->intLength() - 1; |
|
195 | + if ($str_len) { |
|
196 | + $str = substr($data, $idx, $str_len); |
|
197 | + if ($unused_bits) { |
|
198 | + $mask = (1 << $unused_bits) - 1; |
|
199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
200 | + throw new DecodeException( |
|
201 | + "DER encoded bit string must have zero padding."); |
|
202 | + } |
|
203 | + } |
|
204 | + } else { |
|
205 | + $str = ""; |
|
206 | + } |
|
207 | + $offset = $idx + $str_len; |
|
208 | + return new self($str, $unused_bits); |
|
209 | + } |
|
210 | 210 | } |
@@ -16,41 +16,41 @@ |
||
16 | 16 | */ |
17 | 17 | class NullType extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Constructor. |
|
24 | - */ |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->_typeTag = self::TYPE_NULL; |
|
28 | - } |
|
22 | + /** |
|
23 | + * Constructor. |
|
24 | + */ |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->_typeTag = self::TYPE_NULL; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * |
|
32 | - * {@inheritdoc} |
|
33 | - */ |
|
34 | - protected function _encodedContentDER(): string |
|
35 | - { |
|
36 | - return ""; |
|
37 | - } |
|
30 | + /** |
|
31 | + * |
|
32 | + * {@inheritdoc} |
|
33 | + */ |
|
34 | + protected function _encodedContentDER(): string |
|
35 | + { |
|
36 | + return ""; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * |
|
41 | - * {@inheritdoc} |
|
42 | - * @return self |
|
43 | - */ |
|
44 | - protected static function _decodeFromDER(Identifier $identifier, |
|
45 | - string $data, int &$offset): ElementBase |
|
46 | - { |
|
47 | - $idx = $offset; |
|
48 | - if (!$identifier->isPrimitive()) { |
|
49 | - throw new DecodeException("Null value must be primitive."); |
|
50 | - } |
|
51 | - // null type has always zero length |
|
52 | - Length::expectFromDER($data, $idx, 0); |
|
53 | - $offset = $idx; |
|
54 | - return new self(); |
|
55 | - } |
|
39 | + /** |
|
40 | + * |
|
41 | + * {@inheritdoc} |
|
42 | + * @return self |
|
43 | + */ |
|
44 | + protected static function _decodeFromDER(Identifier $identifier, |
|
45 | + string $data, int &$offset): ElementBase |
|
46 | + { |
|
47 | + $idx = $offset; |
|
48 | + if (!$identifier->isPrimitive()) { |
|
49 | + throw new DecodeException("Null value must be primitive."); |
|
50 | + } |
|
51 | + // null type has always zero length |
|
52 | + Length::expectFromDER($data, $idx, 0); |
|
53 | + $offset = $idx; |
|
54 | + return new self(); |
|
55 | + } |
|
56 | 56 | } |
@@ -16,113 +16,113 @@ |
||
16 | 16 | */ |
17 | 17 | class GeneralizedTime extends TimeType |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Regular expression to parse date. |
|
24 | - * |
|
25 | - * DER restricts format to UTC timezone (Z suffix). |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - const REGEX = /* @formatter:off */ '#^' . |
|
30 | - '(\d\d\d\d)' . /* YYYY */ |
|
31 | - '(\d\d)' . /* MM */ |
|
32 | - '(\d\d)' . /* DD */ |
|
33 | - '(\d\d)' . /* hh */ |
|
34 | - '(\d\d)' . /* mm */ |
|
35 | - '(\d\d)' . /* ss */ |
|
36 | - '(?:\.(\d+))?' . /* frac */ |
|
37 | - 'Z' . /* TZ */ |
|
38 | - '$#' /* @formatter:on */; |
|
22 | + /** |
|
23 | + * Regular expression to parse date. |
|
24 | + * |
|
25 | + * DER restricts format to UTC timezone (Z suffix). |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + const REGEX = /* @formatter:off */ '#^' . |
|
30 | + '(\d\d\d\d)' . /* YYYY */ |
|
31 | + '(\d\d)' . /* MM */ |
|
32 | + '(\d\d)' . /* DD */ |
|
33 | + '(\d\d)' . /* hh */ |
|
34 | + '(\d\d)' . /* mm */ |
|
35 | + '(\d\d)' . /* ss */ |
|
36 | + '(?:\.(\d+))?' . /* frac */ |
|
37 | + 'Z' . /* TZ */ |
|
38 | + '$#' /* @formatter:on */; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Cached formatted date. |
|
42 | - * |
|
43 | - * @var string|null |
|
44 | - */ |
|
45 | - private $_formatted; |
|
40 | + /** |
|
41 | + * Cached formatted date. |
|
42 | + * |
|
43 | + * @var string|null |
|
44 | + */ |
|
45 | + private $_formatted; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Constructor. |
|
49 | - * |
|
50 | - * @param \DateTimeImmutable $dt |
|
51 | - */ |
|
52 | - public function __construct(\DateTimeImmutable $dt) |
|
53 | - { |
|
54 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
55 | - parent::__construct($dt); |
|
56 | - } |
|
47 | + /** |
|
48 | + * Constructor. |
|
49 | + * |
|
50 | + * @param \DateTimeImmutable $dt |
|
51 | + */ |
|
52 | + public function __construct(\DateTimeImmutable $dt) |
|
53 | + { |
|
54 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
55 | + parent::__construct($dt); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Clear cached variables on clone. |
|
60 | - */ |
|
61 | - public function __clone() |
|
62 | - { |
|
63 | - $this->_formatted = null; |
|
64 | - } |
|
58 | + /** |
|
59 | + * Clear cached variables on clone. |
|
60 | + */ |
|
61 | + public function __clone() |
|
62 | + { |
|
63 | + $this->_formatted = null; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * {@inheritdoc} |
|
69 | - */ |
|
70 | - protected function _encodedContentDER(): string |
|
71 | - { |
|
72 | - if (!isset($this->_formatted)) { |
|
73 | - $dt = $this->_dateTime->setTimezone( |
|
74 | - self::_createTimeZone(self::TZ_UTC)); |
|
75 | - $this->_formatted = $dt->format("YmdHis"); |
|
76 | - // if fractions were used |
|
77 | - $frac = $dt->format("u"); |
|
78 | - if ($frac != 0) { |
|
79 | - $frac = rtrim($frac, "0"); |
|
80 | - $this->_formatted .= ".$frac"; |
|
81 | - } |
|
82 | - // timezone |
|
83 | - $this->_formatted .= "Z"; |
|
84 | - } |
|
85 | - return $this->_formatted; |
|
86 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * {@inheritdoc} |
|
69 | + */ |
|
70 | + protected function _encodedContentDER(): string |
|
71 | + { |
|
72 | + if (!isset($this->_formatted)) { |
|
73 | + $dt = $this->_dateTime->setTimezone( |
|
74 | + self::_createTimeZone(self::TZ_UTC)); |
|
75 | + $this->_formatted = $dt->format("YmdHis"); |
|
76 | + // if fractions were used |
|
77 | + $frac = $dt->format("u"); |
|
78 | + if ($frac != 0) { |
|
79 | + $frac = rtrim($frac, "0"); |
|
80 | + $this->_formatted .= ".$frac"; |
|
81 | + } |
|
82 | + // timezone |
|
83 | + $this->_formatted .= "Z"; |
|
84 | + } |
|
85 | + return $this->_formatted; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * |
|
90 | - * {@inheritdoc} |
|
91 | - * @return self |
|
92 | - */ |
|
93 | - protected static function _decodeFromDER(Identifier $identifier, |
|
94 | - string $data, int &$offset): ElementBase |
|
95 | - { |
|
96 | - $idx = $offset; |
|
97 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
98 | - $str = substr($data, $idx, $length); |
|
99 | - $idx += $length; |
|
100 | - /** @var $match string[] */ |
|
101 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
102 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
103 | - } |
|
104 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
105 | - if (isset($match[7])) { |
|
106 | - $frac = $match[7]; |
|
107 | - // DER restricts trailing zeroes in fractional seconds component |
|
108 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
109 | - throw new DecodeException( |
|
110 | - "Fractional seconds must omit trailing zeroes."); |
|
111 | - } |
|
112 | - $frac = (int) $frac; |
|
113 | - } else { |
|
114 | - $frac = 0; |
|
115 | - } |
|
116 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
117 | - self::TZ_UTC; |
|
118 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
119 | - self::_createTimeZone(self::TZ_UTC)); |
|
120 | - if (!$dt) { |
|
121 | - throw new DecodeException( |
|
122 | - "Failed to decode GeneralizedTime: " . |
|
123 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
124 | - } |
|
125 | - $offset = $idx; |
|
126 | - return new self($dt); |
|
127 | - } |
|
88 | + /** |
|
89 | + * |
|
90 | + * {@inheritdoc} |
|
91 | + * @return self |
|
92 | + */ |
|
93 | + protected static function _decodeFromDER(Identifier $identifier, |
|
94 | + string $data, int &$offset): ElementBase |
|
95 | + { |
|
96 | + $idx = $offset; |
|
97 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
98 | + $str = substr($data, $idx, $length); |
|
99 | + $idx += $length; |
|
100 | + /** @var $match string[] */ |
|
101 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
102 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
103 | + } |
|
104 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
105 | + if (isset($match[7])) { |
|
106 | + $frac = $match[7]; |
|
107 | + // DER restricts trailing zeroes in fractional seconds component |
|
108 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
109 | + throw new DecodeException( |
|
110 | + "Fractional seconds must omit trailing zeroes."); |
|
111 | + } |
|
112 | + $frac = (int) $frac; |
|
113 | + } else { |
|
114 | + $frac = 0; |
|
115 | + } |
|
116 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
117 | + self::TZ_UTC; |
|
118 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
119 | + self::_createTimeZone(self::TZ_UTC)); |
|
120 | + if (!$dt) { |
|
121 | + throw new DecodeException( |
|
122 | + "Failed to decode GeneralizedTime: " . |
|
123 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
124 | + } |
|
125 | + $offset = $idx; |
|
126 | + return new self($dt); |
|
127 | + } |
|
128 | 128 | } |
@@ -16,161 +16,161 @@ |
||
16 | 16 | */ |
17 | 17 | class Integer extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * The number. |
|
24 | - * |
|
25 | - * @var BigInt |
|
26 | - */ |
|
27 | - private $_number; |
|
22 | + /** |
|
23 | + * The number. |
|
24 | + * |
|
25 | + * @var BigInt |
|
26 | + */ |
|
27 | + private $_number; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param int|string $number Base 10 integer |
|
33 | - */ |
|
34 | - public function __construct($number) |
|
35 | - { |
|
36 | - $this->_typeTag = self::TYPE_INTEGER; |
|
37 | - if (!self::_validateNumber($number)) { |
|
38 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
39 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
40 | - } |
|
41 | - $this->_number = new BigInt($number); |
|
42 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param int|string $number Base 10 integer |
|
33 | + */ |
|
34 | + public function __construct($number) |
|
35 | + { |
|
36 | + $this->_typeTag = self::TYPE_INTEGER; |
|
37 | + if (!self::_validateNumber($number)) { |
|
38 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
39 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
40 | + } |
|
41 | + $this->_number = new BigInt($number); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get the number as a base 10. |
|
46 | - * |
|
47 | - * @return string Integer as a string |
|
48 | - */ |
|
49 | - public function number(): string |
|
50 | - { |
|
51 | - return $this->_number->base10(); |
|
52 | - } |
|
44 | + /** |
|
45 | + * Get the number as a base 10. |
|
46 | + * |
|
47 | + * @return string Integer as a string |
|
48 | + */ |
|
49 | + public function number(): string |
|
50 | + { |
|
51 | + return $this->_number->base10(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get the number as an integer type. |
|
56 | - * |
|
57 | - * @return int |
|
58 | - */ |
|
59 | - public function intNumber(): int |
|
60 | - { |
|
61 | - return $this->_number->intVal(); |
|
62 | - } |
|
54 | + /** |
|
55 | + * Get the number as an integer type. |
|
56 | + * |
|
57 | + * @return int |
|
58 | + */ |
|
59 | + public function intNumber(): int |
|
60 | + { |
|
61 | + return $this->_number->intVal(); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * |
|
66 | - * {@inheritdoc} |
|
67 | - */ |
|
68 | - protected function _encodedContentDER(): string |
|
69 | - { |
|
70 | - $num = $this->_number->gmpObj(); |
|
71 | - switch (gmp_sign($num)) { |
|
72 | - // positive |
|
73 | - case 1: |
|
74 | - return self::_encodePositiveInteger($num); |
|
75 | - // negative |
|
76 | - case -1: |
|
77 | - return self::_encodeNegativeInteger($num); |
|
78 | - } |
|
79 | - // zero |
|
80 | - return "\0"; |
|
81 | - } |
|
64 | + /** |
|
65 | + * |
|
66 | + * {@inheritdoc} |
|
67 | + */ |
|
68 | + protected function _encodedContentDER(): string |
|
69 | + { |
|
70 | + $num = $this->_number->gmpObj(); |
|
71 | + switch (gmp_sign($num)) { |
|
72 | + // positive |
|
73 | + case 1: |
|
74 | + return self::_encodePositiveInteger($num); |
|
75 | + // negative |
|
76 | + case -1: |
|
77 | + return self::_encodeNegativeInteger($num); |
|
78 | + } |
|
79 | + // zero |
|
80 | + return "\0"; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Encode positive integer to DER content. |
|
85 | - * |
|
86 | - * @param \GMP $num |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - private static function _encodePositiveInteger(\GMP $num): string |
|
90 | - { |
|
91 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
92 | - // if first bit is 1, prepend full zero byte |
|
93 | - // to represent positive two's complement |
|
94 | - if (ord($bin[0]) & 0x80) { |
|
95 | - $bin = chr(0x00) . $bin; |
|
96 | - } |
|
97 | - return $bin; |
|
98 | - } |
|
83 | + /** |
|
84 | + * Encode positive integer to DER content. |
|
85 | + * |
|
86 | + * @param \GMP $num |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + private static function _encodePositiveInteger(\GMP $num): string |
|
90 | + { |
|
91 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
92 | + // if first bit is 1, prepend full zero byte |
|
93 | + // to represent positive two's complement |
|
94 | + if (ord($bin[0]) & 0x80) { |
|
95 | + $bin = chr(0x00) . $bin; |
|
96 | + } |
|
97 | + return $bin; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Encode negative integer to DER content. |
|
102 | - * |
|
103 | - * @param \GMP $num |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
107 | - { |
|
108 | - $num = gmp_abs($num); |
|
109 | - // compute number of bytes required |
|
110 | - $width = 1; |
|
111 | - if ($num > 128) { |
|
112 | - $tmp = $num; |
|
113 | - do { |
|
114 | - $width++; |
|
115 | - $tmp >>= 8; |
|
116 | - } while ($tmp > 128); |
|
117 | - } |
|
118 | - // compute two's complement 2^n - x |
|
119 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
120 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
121 | - // if first bit is 0, prepend full inverted byte |
|
122 | - // to represent negative two's complement |
|
123 | - if (!(ord($bin[0]) & 0x80)) { |
|
124 | - $bin = chr(0xff) . $bin; |
|
125 | - } |
|
126 | - return $bin; |
|
127 | - } |
|
100 | + /** |
|
101 | + * Encode negative integer to DER content. |
|
102 | + * |
|
103 | + * @param \GMP $num |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
107 | + { |
|
108 | + $num = gmp_abs($num); |
|
109 | + // compute number of bytes required |
|
110 | + $width = 1; |
|
111 | + if ($num > 128) { |
|
112 | + $tmp = $num; |
|
113 | + do { |
|
114 | + $width++; |
|
115 | + $tmp >>= 8; |
|
116 | + } while ($tmp > 128); |
|
117 | + } |
|
118 | + // compute two's complement 2^n - x |
|
119 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
120 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
121 | + // if first bit is 0, prepend full inverted byte |
|
122 | + // to represent negative two's complement |
|
123 | + if (!(ord($bin[0]) & 0x80)) { |
|
124 | + $bin = chr(0xff) . $bin; |
|
125 | + } |
|
126 | + return $bin; |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * |
|
131 | - * {@inheritdoc} |
|
132 | - * @return self |
|
133 | - */ |
|
134 | - protected static function _decodeFromDER(Identifier $identifier, |
|
135 | - string $data, int &$offset): ElementBase |
|
136 | - { |
|
137 | - $idx = $offset; |
|
138 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
139 | - $bytes = substr($data, $idx, $length); |
|
140 | - $idx += $length; |
|
141 | - $neg = ord($bytes[0]) & 0x80; |
|
142 | - // negative, apply inversion of two's complement |
|
143 | - if ($neg) { |
|
144 | - $len = strlen($bytes); |
|
145 | - for ($i = 0; $i < $len; $i++) { |
|
146 | - $bytes[$i] = ~$bytes[$i]; |
|
147 | - } |
|
148 | - } |
|
149 | - $num = gmp_init(bin2hex($bytes), 16); |
|
150 | - // negative, apply addition of two's complement |
|
151 | - // and produce negative result |
|
152 | - if ($neg) { |
|
153 | - $num = gmp_neg($num + 1); |
|
154 | - } |
|
155 | - $offset = $idx; |
|
156 | - // late static binding since enumerated extends integer type |
|
157 | - return new static(gmp_strval($num, 10)); |
|
158 | - } |
|
129 | + /** |
|
130 | + * |
|
131 | + * {@inheritdoc} |
|
132 | + * @return self |
|
133 | + */ |
|
134 | + protected static function _decodeFromDER(Identifier $identifier, |
|
135 | + string $data, int &$offset): ElementBase |
|
136 | + { |
|
137 | + $idx = $offset; |
|
138 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
139 | + $bytes = substr($data, $idx, $length); |
|
140 | + $idx += $length; |
|
141 | + $neg = ord($bytes[0]) & 0x80; |
|
142 | + // negative, apply inversion of two's complement |
|
143 | + if ($neg) { |
|
144 | + $len = strlen($bytes); |
|
145 | + for ($i = 0; $i < $len; $i++) { |
|
146 | + $bytes[$i] = ~$bytes[$i]; |
|
147 | + } |
|
148 | + } |
|
149 | + $num = gmp_init(bin2hex($bytes), 16); |
|
150 | + // negative, apply addition of two's complement |
|
151 | + // and produce negative result |
|
152 | + if ($neg) { |
|
153 | + $num = gmp_neg($num + 1); |
|
154 | + } |
|
155 | + $offset = $idx; |
|
156 | + // late static binding since enumerated extends integer type |
|
157 | + return new static(gmp_strval($num, 10)); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Test that number is valid for this context. |
|
162 | - * |
|
163 | - * @param mixed $num |
|
164 | - * @return bool |
|
165 | - */ |
|
166 | - private static function _validateNumber($num): bool |
|
167 | - { |
|
168 | - if (is_int($num)) { |
|
169 | - return true; |
|
170 | - } |
|
171 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
172 | - return true; |
|
173 | - } |
|
174 | - return false; |
|
175 | - } |
|
160 | + /** |
|
161 | + * Test that number is valid for this context. |
|
162 | + * |
|
163 | + * @param mixed $num |
|
164 | + * @return bool |
|
165 | + */ |
|
166 | + private static function _validateNumber($num): bool |
|
167 | + { |
|
168 | + if (is_int($num)) { |
|
169 | + return true; |
|
170 | + } |
|
171 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
172 | + return true; |
|
173 | + } |
|
174 | + return false; |
|
175 | + } |
|
176 | 176 | } |
@@ -12,295 +12,295 @@ |
||
12 | 12 | */ |
13 | 13 | class Identifier implements Encodable |
14 | 14 | { |
15 | - // Type class enumerations |
|
16 | - const CLASS_UNIVERSAL = 0b00; |
|
17 | - const CLASS_APPLICATION = 0b01; |
|
18 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | - const CLASS_PRIVATE = 0b11; |
|
15 | + // Type class enumerations |
|
16 | + const CLASS_UNIVERSAL = 0b00; |
|
17 | + const CLASS_APPLICATION = 0b01; |
|
18 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | + const CLASS_PRIVATE = 0b11; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Mapping from type class to human readable name. |
|
23 | - * |
|
24 | - * @internal |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | - self::CLASS_APPLICATION => "APPLICATION", |
|
31 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | - self::CLASS_PRIVATE => "PRIVATE", |
|
33 | - /* @formatter:on */ |
|
34 | - ]; |
|
21 | + /** |
|
22 | + * Mapping from type class to human readable name. |
|
23 | + * |
|
24 | + * @internal |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | + self::CLASS_APPLICATION => "APPLICATION", |
|
31 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | + self::CLASS_PRIVATE => "PRIVATE", |
|
33 | + /* @formatter:on */ |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var BigInt |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var BigInt |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = new BigInt($tag); |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = new BigInt($tag); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param int|null $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. Variable is updated to |
|
81 | - * the offset next to the parsed identifier. If null, start from |
|
82 | - * offset 0. |
|
83 | - * @throws DecodeException If decoding fails |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
87 | - { |
|
88 | - $idx = $offset ?? 0; |
|
89 | - $datalen = strlen($data); |
|
90 | - if ($idx >= $datalen) { |
|
91 | - throw new DecodeException("Invalid offset."); |
|
92 | - } |
|
93 | - $byte = ord($data[$idx++]); |
|
94 | - // bits 8 and 7 (class) |
|
95 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | - $class = (0b11000000 & $byte) >> 6; |
|
97 | - // bit 6 (0 = primitive / 1 = constructed) |
|
98 | - $pc = (0b00100000 & $byte) >> 5; |
|
99 | - // bits 5 to 1 (tag number) |
|
100 | - $tag = (0b00011111 & $byte); |
|
101 | - // long-form identifier |
|
102 | - if (0x1f == $tag) { |
|
103 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | - } |
|
105 | - if (isset($offset)) { |
|
106 | - $offset = $idx; |
|
107 | - } |
|
108 | - return new self($class, $pc, $tag); |
|
109 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param int|null $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. Variable is updated to |
|
81 | + * the offset next to the parsed identifier. If null, start from |
|
82 | + * offset 0. |
|
83 | + * @throws DecodeException If decoding fails |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
87 | + { |
|
88 | + $idx = $offset ?? 0; |
|
89 | + $datalen = strlen($data); |
|
90 | + if ($idx >= $datalen) { |
|
91 | + throw new DecodeException("Invalid offset."); |
|
92 | + } |
|
93 | + $byte = ord($data[$idx++]); |
|
94 | + // bits 8 and 7 (class) |
|
95 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | + $class = (0b11000000 & $byte) >> 6; |
|
97 | + // bit 6 (0 = primitive / 1 = constructed) |
|
98 | + $pc = (0b00100000 & $byte) >> 5; |
|
99 | + // bits 5 to 1 (tag number) |
|
100 | + $tag = (0b00011111 & $byte); |
|
101 | + // long-form identifier |
|
102 | + if (0x1f == $tag) { |
|
103 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | + } |
|
105 | + if (isset($offset)) { |
|
106 | + $offset = $idx; |
|
107 | + } |
|
108 | + return new self($class, $pc, $tag); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Parse long form tag. |
|
113 | - * |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the variable containing offset to data |
|
116 | - * @throws DecodeException If decoding fails |
|
117 | - * @return string Tag number |
|
118 | - */ |
|
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | - { |
|
121 | - $datalen = strlen($data); |
|
122 | - $tag = gmp_init(0, 10); |
|
123 | - while (true) { |
|
124 | - if ($offset >= $datalen) { |
|
125 | - throw new DecodeException( |
|
126 | - "Unexpected end of data while decoding" . |
|
127 | - " long form identifier."); |
|
128 | - } |
|
129 | - $byte = ord($data[$offset++]); |
|
130 | - $tag <<= 7; |
|
131 | - $tag |= 0x7f & $byte; |
|
132 | - // last byte has bit 8 set to zero |
|
133 | - if (!(0x80 & $byte)) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - return gmp_strval($tag, 10); |
|
138 | - } |
|
111 | + /** |
|
112 | + * Parse long form tag. |
|
113 | + * |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the variable containing offset to data |
|
116 | + * @throws DecodeException If decoding fails |
|
117 | + * @return string Tag number |
|
118 | + */ |
|
119 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | + { |
|
121 | + $datalen = strlen($data); |
|
122 | + $tag = gmp_init(0, 10); |
|
123 | + while (true) { |
|
124 | + if ($offset >= $datalen) { |
|
125 | + throw new DecodeException( |
|
126 | + "Unexpected end of data while decoding" . |
|
127 | + " long form identifier."); |
|
128 | + } |
|
129 | + $byte = ord($data[$offset++]); |
|
130 | + $tag <<= 7; |
|
131 | + $tag |= 0x7f & $byte; |
|
132 | + // last byte has bit 8 set to zero |
|
133 | + if (!(0x80 & $byte)) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + return gmp_strval($tag, 10); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * |
|
142 | - * @see Encodable::toDER() |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function toDER(): string |
|
146 | - { |
|
147 | - $bytes = []; |
|
148 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | - $tag = $this->_tag->gmpObj(); |
|
150 | - if ($tag < 0x1f) { |
|
151 | - $bytes[] = $byte | $tag; |
|
152 | - } else { // long-form identifier |
|
153 | - $bytes[] = $byte | 0x1f; |
|
154 | - $octets = []; |
|
155 | - for (; $tag > 0; $tag >>= 7) { |
|
156 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | - } |
|
158 | - // last octet has bit 8 set to zero |
|
159 | - $octets[0] &= 0x7f; |
|
160 | - foreach (array_reverse($octets) as $octet) { |
|
161 | - $bytes[] = $octet; |
|
162 | - } |
|
163 | - } |
|
164 | - return pack("C*", ...$bytes); |
|
165 | - } |
|
140 | + /** |
|
141 | + * |
|
142 | + * @see Encodable::toDER() |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function toDER(): string |
|
146 | + { |
|
147 | + $bytes = []; |
|
148 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | + $tag = $this->_tag->gmpObj(); |
|
150 | + if ($tag < 0x1f) { |
|
151 | + $bytes[] = $byte | $tag; |
|
152 | + } else { // long-form identifier |
|
153 | + $bytes[] = $byte | 0x1f; |
|
154 | + $octets = []; |
|
155 | + for (; $tag > 0; $tag >>= 7) { |
|
156 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | + } |
|
158 | + // last octet has bit 8 set to zero |
|
159 | + $octets[0] &= 0x7f; |
|
160 | + foreach (array_reverse($octets) as $octet) { |
|
161 | + $bytes[] = $octet; |
|
162 | + } |
|
163 | + } |
|
164 | + return pack("C*", ...$bytes); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get class of the type. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
172 | - public function typeClass(): int |
|
173 | - { |
|
174 | - return $this->_class; |
|
175 | - } |
|
167 | + /** |
|
168 | + * Get class of the type. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | + public function typeClass(): int |
|
173 | + { |
|
174 | + return $this->_class; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get P/C. |
|
179 | - * |
|
180 | - * @return int |
|
181 | - */ |
|
182 | - public function pc(): int |
|
183 | - { |
|
184 | - return $this->_pc; |
|
185 | - } |
|
177 | + /** |
|
178 | + * Get P/C. |
|
179 | + * |
|
180 | + * @return int |
|
181 | + */ |
|
182 | + public function pc(): int |
|
183 | + { |
|
184 | + return $this->_pc; |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the tag number. |
|
189 | - * |
|
190 | - * @return string Base 10 integer string |
|
191 | - */ |
|
192 | - public function tag(): string |
|
193 | - { |
|
194 | - return $this->_tag->base10(); |
|
195 | - } |
|
187 | + /** |
|
188 | + * Get the tag number. |
|
189 | + * |
|
190 | + * @return string Base 10 integer string |
|
191 | + */ |
|
192 | + public function tag(): string |
|
193 | + { |
|
194 | + return $this->_tag->base10(); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Get the tag as an integer. |
|
199 | - * |
|
200 | - * @return int |
|
201 | - */ |
|
202 | - public function intTag(): int |
|
203 | - { |
|
204 | - return $this->_tag->intVal(); |
|
205 | - } |
|
197 | + /** |
|
198 | + * Get the tag as an integer. |
|
199 | + * |
|
200 | + * @return int |
|
201 | + */ |
|
202 | + public function intTag(): int |
|
203 | + { |
|
204 | + return $this->_tag->intVal(); |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Check whether type is of an universal class. |
|
209 | - * |
|
210 | - * @return bool |
|
211 | - */ |
|
212 | - public function isUniversal(): bool |
|
213 | - { |
|
214 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
215 | - } |
|
207 | + /** |
|
208 | + * Check whether type is of an universal class. |
|
209 | + * |
|
210 | + * @return bool |
|
211 | + */ |
|
212 | + public function isUniversal(): bool |
|
213 | + { |
|
214 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Check whether type is of an application class. |
|
219 | - * |
|
220 | - * @return bool |
|
221 | - */ |
|
222 | - public function isApplication(): bool |
|
223 | - { |
|
224 | - return self::CLASS_APPLICATION == $this->_class; |
|
225 | - } |
|
217 | + /** |
|
218 | + * Check whether type is of an application class. |
|
219 | + * |
|
220 | + * @return bool |
|
221 | + */ |
|
222 | + public function isApplication(): bool |
|
223 | + { |
|
224 | + return self::CLASS_APPLICATION == $this->_class; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Check whether type is of a context specific class. |
|
229 | - * |
|
230 | - * @return bool |
|
231 | - */ |
|
232 | - public function isContextSpecific(): bool |
|
233 | - { |
|
234 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
235 | - } |
|
227 | + /** |
|
228 | + * Check whether type is of a context specific class. |
|
229 | + * |
|
230 | + * @return bool |
|
231 | + */ |
|
232 | + public function isContextSpecific(): bool |
|
233 | + { |
|
234 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether type is of a private class. |
|
239 | - * |
|
240 | - * @return bool |
|
241 | - */ |
|
242 | - public function isPrivate(): bool |
|
243 | - { |
|
244 | - return self::CLASS_PRIVATE == $this->_class; |
|
245 | - } |
|
237 | + /** |
|
238 | + * Check whether type is of a private class. |
|
239 | + * |
|
240 | + * @return bool |
|
241 | + */ |
|
242 | + public function isPrivate(): bool |
|
243 | + { |
|
244 | + return self::CLASS_PRIVATE == $this->_class; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * Check whether content is primitive type. |
|
249 | - * |
|
250 | - * @return bool |
|
251 | - */ |
|
252 | - public function isPrimitive(): bool |
|
253 | - { |
|
254 | - return self::PRIMITIVE == $this->_pc; |
|
255 | - } |
|
247 | + /** |
|
248 | + * Check whether content is primitive type. |
|
249 | + * |
|
250 | + * @return bool |
|
251 | + */ |
|
252 | + public function isPrimitive(): bool |
|
253 | + { |
|
254 | + return self::PRIMITIVE == $this->_pc; |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Check hether content is constructed type. |
|
259 | - * |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public function isConstructed(): bool |
|
263 | - { |
|
264 | - return self::CONSTRUCTED == $this->_pc; |
|
265 | - } |
|
257 | + /** |
|
258 | + * Check hether content is constructed type. |
|
259 | + * |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public function isConstructed(): bool |
|
263 | + { |
|
264 | + return self::CONSTRUCTED == $this->_pc; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Get self with given type class. |
|
269 | - * |
|
270 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
271 | - * @return self |
|
272 | - */ |
|
273 | - public function withClass(int $class): Identifier |
|
274 | - { |
|
275 | - $obj = clone $this; |
|
276 | - $obj->_class = 0b11 & $class; |
|
277 | - return $obj; |
|
278 | - } |
|
267 | + /** |
|
268 | + * Get self with given type class. |
|
269 | + * |
|
270 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
271 | + * @return self |
|
272 | + */ |
|
273 | + public function withClass(int $class): Identifier |
|
274 | + { |
|
275 | + $obj = clone $this; |
|
276 | + $obj->_class = 0b11 & $class; |
|
277 | + return $obj; |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Get self with given type tag. |
|
282 | - * |
|
283 | - * @param int|string $tag Tag number |
|
284 | - * @return self |
|
285 | - */ |
|
286 | - public function withTag($tag): Identifier |
|
287 | - { |
|
288 | - $obj = clone $this; |
|
289 | - $obj->_tag = new BigInt($tag); |
|
290 | - return $obj; |
|
291 | - } |
|
280 | + /** |
|
281 | + * Get self with given type tag. |
|
282 | + * |
|
283 | + * @param int|string $tag Tag number |
|
284 | + * @return self |
|
285 | + */ |
|
286 | + public function withTag($tag): Identifier |
|
287 | + { |
|
288 | + $obj = clone $this; |
|
289 | + $obj->_tag = new BigInt($tag); |
|
290 | + return $obj; |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * Get human readable name of the type class. |
|
295 | - * |
|
296 | - * @param int $class |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - public static function classToName(int $class): string |
|
300 | - { |
|
301 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
302 | - return "CLASS $class"; |
|
303 | - } |
|
304 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
305 | - } |
|
293 | + /** |
|
294 | + * Get human readable name of the type class. |
|
295 | + * |
|
296 | + * @param int $class |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + public static function classToName(int $class): string |
|
300 | + { |
|
301 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
302 | + return "CLASS $class"; |
|
303 | + } |
|
304 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
305 | + } |
|
306 | 306 | } |
@@ -21,438 +21,438 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class Element implements ElementBase |
23 | 23 | { |
24 | - // Universal type tags |
|
25 | - const TYPE_EOC = 0x00; |
|
26 | - const TYPE_BOOLEAN = 0x01; |
|
27 | - const TYPE_INTEGER = 0x02; |
|
28 | - const TYPE_BIT_STRING = 0x03; |
|
29 | - const TYPE_OCTET_STRING = 0x04; |
|
30 | - const TYPE_NULL = 0x05; |
|
31 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
32 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
33 | - const TYPE_EXTERNAL = 0x08; |
|
34 | - const TYPE_REAL = 0x09; |
|
35 | - const TYPE_ENUMERATED = 0x0a; |
|
36 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
37 | - const TYPE_UTF8_STRING = 0x0c; |
|
38 | - const TYPE_RELATIVE_OID = 0x0d; |
|
39 | - const TYPE_SEQUENCE = 0x10; |
|
40 | - const TYPE_SET = 0x11; |
|
41 | - const TYPE_NUMERIC_STRING = 0x12; |
|
42 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
43 | - const TYPE_T61_STRING = 0x14; |
|
44 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
45 | - const TYPE_IA5_STRING = 0x16; |
|
46 | - const TYPE_UTC_TIME = 0x17; |
|
47 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
48 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
49 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
50 | - const TYPE_GENERAL_STRING = 0x1b; |
|
51 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
52 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
53 | - const TYPE_BMP_STRING = 0x1e; |
|
24 | + // Universal type tags |
|
25 | + const TYPE_EOC = 0x00; |
|
26 | + const TYPE_BOOLEAN = 0x01; |
|
27 | + const TYPE_INTEGER = 0x02; |
|
28 | + const TYPE_BIT_STRING = 0x03; |
|
29 | + const TYPE_OCTET_STRING = 0x04; |
|
30 | + const TYPE_NULL = 0x05; |
|
31 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
32 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
33 | + const TYPE_EXTERNAL = 0x08; |
|
34 | + const TYPE_REAL = 0x09; |
|
35 | + const TYPE_ENUMERATED = 0x0a; |
|
36 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
37 | + const TYPE_UTF8_STRING = 0x0c; |
|
38 | + const TYPE_RELATIVE_OID = 0x0d; |
|
39 | + const TYPE_SEQUENCE = 0x10; |
|
40 | + const TYPE_SET = 0x11; |
|
41 | + const TYPE_NUMERIC_STRING = 0x12; |
|
42 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
43 | + const TYPE_T61_STRING = 0x14; |
|
44 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
45 | + const TYPE_IA5_STRING = 0x16; |
|
46 | + const TYPE_UTC_TIME = 0x17; |
|
47 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
48 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
49 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
50 | + const TYPE_GENERAL_STRING = 0x1b; |
|
51 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
52 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
53 | + const TYPE_BMP_STRING = 0x1e; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Mapping from universal type tag to implementation class name. |
|
57 | - * |
|
58 | - * @internal |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
63 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
64 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
65 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
66 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
67 | - self::TYPE_NULL => Primitive\NullType::class, |
|
68 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
69 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
70 | - self::TYPE_REAL => Primitive\Real::class, |
|
71 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
72 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
73 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
74 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
75 | - self::TYPE_SET => Constructed\Set::class, |
|
76 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
77 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
78 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
79 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
80 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
81 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
82 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
83 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
84 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
85 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
86 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
87 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
88 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
89 | - /* @formatter:on */ |
|
90 | - ]; |
|
55 | + /** |
|
56 | + * Mapping from universal type tag to implementation class name. |
|
57 | + * |
|
58 | + * @internal |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
63 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
64 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
65 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
66 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
67 | + self::TYPE_NULL => Primitive\NullType::class, |
|
68 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
69 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
70 | + self::TYPE_REAL => Primitive\Real::class, |
|
71 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
72 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
73 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
74 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
75 | + self::TYPE_SET => Constructed\Set::class, |
|
76 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
77 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
78 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
79 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
80 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
81 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
82 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
83 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
84 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
85 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
86 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
87 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
88 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
89 | + /* @formatter:on */ |
|
90 | + ]; |
|
91 | 91 | |
92 | - /** |
|
93 | - * Pseudotype for all string types. |
|
94 | - * |
|
95 | - * May be used as an expectation parameter. |
|
96 | - * |
|
97 | - * @var int |
|
98 | - */ |
|
99 | - const TYPE_STRING = -1; |
|
92 | + /** |
|
93 | + * Pseudotype for all string types. |
|
94 | + * |
|
95 | + * May be used as an expectation parameter. |
|
96 | + * |
|
97 | + * @var int |
|
98 | + */ |
|
99 | + const TYPE_STRING = -1; |
|
100 | 100 | |
101 | - /** |
|
102 | - * Pseudotype for all time types. |
|
103 | - * |
|
104 | - * May be used as an expectation parameter. |
|
105 | - * |
|
106 | - * @var int |
|
107 | - */ |
|
108 | - const TYPE_TIME = -2; |
|
101 | + /** |
|
102 | + * Pseudotype for all time types. |
|
103 | + * |
|
104 | + * May be used as an expectation parameter. |
|
105 | + * |
|
106 | + * @var int |
|
107 | + */ |
|
108 | + const TYPE_TIME = -2; |
|
109 | 109 | |
110 | - /** |
|
111 | - * Mapping from universal type tag to human readable name. |
|
112 | - * |
|
113 | - * @internal |
|
114 | - * |
|
115 | - * @var array |
|
116 | - */ |
|
117 | - const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
118 | - self::TYPE_EOC => "EOC", |
|
119 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
120 | - self::TYPE_INTEGER => "INTEGER", |
|
121 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
122 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
123 | - self::TYPE_NULL => "NULL", |
|
124 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
125 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
126 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
127 | - self::TYPE_REAL => "REAL", |
|
128 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
129 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
130 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
131 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
132 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
133 | - self::TYPE_SET => "SET", |
|
134 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
135 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
136 | - self::TYPE_T61_STRING => "T61String", |
|
137 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
138 | - self::TYPE_IA5_STRING => "IA5String", |
|
139 | - self::TYPE_UTC_TIME => "UTCTime", |
|
140 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
141 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
142 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
143 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
144 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
145 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
146 | - self::TYPE_BMP_STRING => "BMPString", |
|
147 | - self::TYPE_STRING => "Any String", |
|
148 | - self::TYPE_TIME => "Any Time" |
|
149 | - /* @formatter:on */ |
|
150 | - ]; |
|
110 | + /** |
|
111 | + * Mapping from universal type tag to human readable name. |
|
112 | + * |
|
113 | + * @internal |
|
114 | + * |
|
115 | + * @var array |
|
116 | + */ |
|
117 | + const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
118 | + self::TYPE_EOC => "EOC", |
|
119 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
120 | + self::TYPE_INTEGER => "INTEGER", |
|
121 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
122 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
123 | + self::TYPE_NULL => "NULL", |
|
124 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
125 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
126 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
127 | + self::TYPE_REAL => "REAL", |
|
128 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
129 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
130 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
131 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
132 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
133 | + self::TYPE_SET => "SET", |
|
134 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
135 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
136 | + self::TYPE_T61_STRING => "T61String", |
|
137 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
138 | + self::TYPE_IA5_STRING => "IA5String", |
|
139 | + self::TYPE_UTC_TIME => "UTCTime", |
|
140 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
141 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
142 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
143 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
144 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
145 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
146 | + self::TYPE_BMP_STRING => "BMPString", |
|
147 | + self::TYPE_STRING => "Any String", |
|
148 | + self::TYPE_TIME => "Any Time" |
|
149 | + /* @formatter:on */ |
|
150 | + ]; |
|
151 | 151 | |
152 | - /** |
|
153 | - * Element's type tag. |
|
154 | - * |
|
155 | - * @var int |
|
156 | - */ |
|
157 | - protected $_typeTag; |
|
152 | + /** |
|
153 | + * Element's type tag. |
|
154 | + * |
|
155 | + * @var int |
|
156 | + */ |
|
157 | + protected $_typeTag; |
|
158 | 158 | |
159 | - /** |
|
160 | - * |
|
161 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - abstract public function typeClass(): int; |
|
159 | + /** |
|
160 | + * |
|
161 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + abstract public function typeClass(): int; |
|
165 | 165 | |
166 | - /** |
|
167 | - * |
|
168 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - abstract public function isConstructed(): bool; |
|
166 | + /** |
|
167 | + * |
|
168 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + abstract public function isConstructed(): bool; |
|
172 | 172 | |
173 | - /** |
|
174 | - * Get the content encoded in DER. |
|
175 | - * |
|
176 | - * Returns the DER encoded content without identifier and length header |
|
177 | - * octets. |
|
178 | - * |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - abstract protected function _encodedContentDER(): string; |
|
173 | + /** |
|
174 | + * Get the content encoded in DER. |
|
175 | + * |
|
176 | + * Returns the DER encoded content without identifier and length header |
|
177 | + * octets. |
|
178 | + * |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + abstract protected function _encodedContentDER(): string; |
|
182 | 182 | |
183 | - /** |
|
184 | - * Decode type-specific element from DER. |
|
185 | - * |
|
186 | - * @param Identifier $identifier Pre-parsed identifier |
|
187 | - * @param string $data DER data |
|
188 | - * @param int $offset Offset in data to the next byte after identifier |
|
189 | - * @throws DecodeException If decoding fails |
|
190 | - * @return ElementBase |
|
191 | - */ |
|
192 | - protected static function _decodeFromDER(Identifier $identifier, |
|
193 | - string $data, int &$offset): ElementBase |
|
194 | - { |
|
195 | - throw new \BadMethodCallException( |
|
196 | - __METHOD__ . " must be implemented in derived class."); |
|
197 | - } |
|
183 | + /** |
|
184 | + * Decode type-specific element from DER. |
|
185 | + * |
|
186 | + * @param Identifier $identifier Pre-parsed identifier |
|
187 | + * @param string $data DER data |
|
188 | + * @param int $offset Offset in data to the next byte after identifier |
|
189 | + * @throws DecodeException If decoding fails |
|
190 | + * @return ElementBase |
|
191 | + */ |
|
192 | + protected static function _decodeFromDER(Identifier $identifier, |
|
193 | + string $data, int &$offset): ElementBase |
|
194 | + { |
|
195 | + throw new \BadMethodCallException( |
|
196 | + __METHOD__ . " must be implemented in derived class."); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Decode element from DER data. |
|
201 | - * |
|
202 | - * @param string $data DER encoded data |
|
203 | - * @param int|null $offset Reference to the variable that contains offset |
|
204 | - * into the data where to start parsing. Variable is updated to |
|
205 | - * the offset next to the parsed element. If null, start from offset |
|
206 | - * 0. |
|
207 | - * @throws DecodeException If decoding fails |
|
208 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
209 | - * type, but decoding yields another type |
|
210 | - * @return ElementBase |
|
211 | - */ |
|
212 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
213 | - { |
|
214 | - // decode identifier |
|
215 | - $idx = $offset ?? 0; |
|
216 | - $identifier = Identifier::fromDER($data, $idx); |
|
217 | - // determine class that implements type specific decoding |
|
218 | - $cls = self::_determineImplClass($identifier); |
|
219 | - try { |
|
220 | - // decode remaining element |
|
221 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
222 | - } catch (\LogicException $e) { |
|
223 | - // rethrow as a RuntimeException for unified exception handling |
|
224 | - throw new DecodeException( |
|
225 | - sprintf("Error while decoding %s.", |
|
226 | - self::tagToName($identifier->intTag())), 0, $e); |
|
227 | - } |
|
228 | - // if called in the context of a concrete class, check |
|
229 | - // that decoded type matches the type of a calling class |
|
230 | - $called_class = get_called_class(); |
|
231 | - if (self::class != $called_class) { |
|
232 | - if (!$element instanceof $called_class) { |
|
233 | - throw new \UnexpectedValueException( |
|
234 | - sprintf("%s expected, got %s.", $called_class, |
|
235 | - get_class($element))); |
|
236 | - } |
|
237 | - } |
|
238 | - // update offset for the caller |
|
239 | - if (isset($offset)) { |
|
240 | - $offset = $idx; |
|
241 | - } |
|
242 | - return $element; |
|
243 | - } |
|
199 | + /** |
|
200 | + * Decode element from DER data. |
|
201 | + * |
|
202 | + * @param string $data DER encoded data |
|
203 | + * @param int|null $offset Reference to the variable that contains offset |
|
204 | + * into the data where to start parsing. Variable is updated to |
|
205 | + * the offset next to the parsed element. If null, start from offset |
|
206 | + * 0. |
|
207 | + * @throws DecodeException If decoding fails |
|
208 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
209 | + * type, but decoding yields another type |
|
210 | + * @return ElementBase |
|
211 | + */ |
|
212 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
213 | + { |
|
214 | + // decode identifier |
|
215 | + $idx = $offset ?? 0; |
|
216 | + $identifier = Identifier::fromDER($data, $idx); |
|
217 | + // determine class that implements type specific decoding |
|
218 | + $cls = self::_determineImplClass($identifier); |
|
219 | + try { |
|
220 | + // decode remaining element |
|
221 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
222 | + } catch (\LogicException $e) { |
|
223 | + // rethrow as a RuntimeException for unified exception handling |
|
224 | + throw new DecodeException( |
|
225 | + sprintf("Error while decoding %s.", |
|
226 | + self::tagToName($identifier->intTag())), 0, $e); |
|
227 | + } |
|
228 | + // if called in the context of a concrete class, check |
|
229 | + // that decoded type matches the type of a calling class |
|
230 | + $called_class = get_called_class(); |
|
231 | + if (self::class != $called_class) { |
|
232 | + if (!$element instanceof $called_class) { |
|
233 | + throw new \UnexpectedValueException( |
|
234 | + sprintf("%s expected, got %s.", $called_class, |
|
235 | + get_class($element))); |
|
236 | + } |
|
237 | + } |
|
238 | + // update offset for the caller |
|
239 | + if (isset($offset)) { |
|
240 | + $offset = $idx; |
|
241 | + } |
|
242 | + return $element; |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * |
|
247 | - * @see \ASN1\Feature\Encodable::toDER() |
|
248 | - * @return string |
|
249 | - */ |
|
250 | - public function toDER(): string |
|
251 | - { |
|
252 | - $identifier = new Identifier($this->typeClass(), |
|
253 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
254 | - $this->_typeTag); |
|
255 | - $content = $this->_encodedContentDER(); |
|
256 | - $length = new Length(strlen($content)); |
|
257 | - return $identifier->toDER() . $length->toDER() . $content; |
|
258 | - } |
|
245 | + /** |
|
246 | + * |
|
247 | + * @see \ASN1\Feature\Encodable::toDER() |
|
248 | + * @return string |
|
249 | + */ |
|
250 | + public function toDER(): string |
|
251 | + { |
|
252 | + $identifier = new Identifier($this->typeClass(), |
|
253 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
254 | + $this->_typeTag); |
|
255 | + $content = $this->_encodedContentDER(); |
|
256 | + $length = new Length(strlen($content)); |
|
257 | + return $identifier->toDER() . $length->toDER() . $content; |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * |
|
262 | - * @see \ASN1\Feature\ElementBase::tag() |
|
263 | - * @return int |
|
264 | - */ |
|
265 | - public function tag(): int |
|
266 | - { |
|
267 | - return $this->_typeTag; |
|
268 | - } |
|
260 | + /** |
|
261 | + * |
|
262 | + * @see \ASN1\Feature\ElementBase::tag() |
|
263 | + * @return int |
|
264 | + */ |
|
265 | + public function tag(): int |
|
266 | + { |
|
267 | + return $this->_typeTag; |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * |
|
272 | - * @see \ASN1\Feature\ElementBase::isType() |
|
273 | - * @return bool |
|
274 | - */ |
|
275 | - public function isType(int $tag): bool |
|
276 | - { |
|
277 | - // if element is context specific |
|
278 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
279 | - return false; |
|
280 | - } |
|
281 | - // negative tags identify an abstract pseudotype |
|
282 | - if ($tag < 0) { |
|
283 | - return $this->_isPseudoType($tag); |
|
284 | - } |
|
285 | - return $this->_isConcreteType($tag); |
|
286 | - } |
|
270 | + /** |
|
271 | + * |
|
272 | + * @see \ASN1\Feature\ElementBase::isType() |
|
273 | + * @return bool |
|
274 | + */ |
|
275 | + public function isType(int $tag): bool |
|
276 | + { |
|
277 | + // if element is context specific |
|
278 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
279 | + return false; |
|
280 | + } |
|
281 | + // negative tags identify an abstract pseudotype |
|
282 | + if ($tag < 0) { |
|
283 | + return $this->_isPseudoType($tag); |
|
284 | + } |
|
285 | + return $this->_isConcreteType($tag); |
|
286 | + } |
|
287 | 287 | |
288 | - /** |
|
289 | - * |
|
290 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
291 | - * @return ElementBase |
|
292 | - */ |
|
293 | - public function expectType(int $tag): ElementBase |
|
294 | - { |
|
295 | - if (!$this->isType($tag)) { |
|
296 | - throw new \UnexpectedValueException( |
|
297 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
298 | - $this->_typeDescriptorString())); |
|
299 | - } |
|
300 | - return $this; |
|
301 | - } |
|
288 | + /** |
|
289 | + * |
|
290 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
291 | + * @return ElementBase |
|
292 | + */ |
|
293 | + public function expectType(int $tag): ElementBase |
|
294 | + { |
|
295 | + if (!$this->isType($tag)) { |
|
296 | + throw new \UnexpectedValueException( |
|
297 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
298 | + $this->_typeDescriptorString())); |
|
299 | + } |
|
300 | + return $this; |
|
301 | + } |
|
302 | 302 | |
303 | - /** |
|
304 | - * Check whether the element is a concrete type of a given tag. |
|
305 | - * |
|
306 | - * @param int $tag |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - private function _isConcreteType(int $tag): bool |
|
310 | - { |
|
311 | - // if tag doesn't match |
|
312 | - if ($this->tag() != $tag) { |
|
313 | - return false; |
|
314 | - } |
|
315 | - // if type is universal check that instance is of a correct class |
|
316 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
317 | - $cls = self::_determineUniversalImplClass($tag); |
|
318 | - if (!$this instanceof $cls) { |
|
319 | - return false; |
|
320 | - } |
|
321 | - } |
|
322 | - return true; |
|
323 | - } |
|
303 | + /** |
|
304 | + * Check whether the element is a concrete type of a given tag. |
|
305 | + * |
|
306 | + * @param int $tag |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + private function _isConcreteType(int $tag): bool |
|
310 | + { |
|
311 | + // if tag doesn't match |
|
312 | + if ($this->tag() != $tag) { |
|
313 | + return false; |
|
314 | + } |
|
315 | + // if type is universal check that instance is of a correct class |
|
316 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
317 | + $cls = self::_determineUniversalImplClass($tag); |
|
318 | + if (!$this instanceof $cls) { |
|
319 | + return false; |
|
320 | + } |
|
321 | + } |
|
322 | + return true; |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Check whether the element is a pseudotype. |
|
327 | - * |
|
328 | - * @param int $tag |
|
329 | - * @return bool |
|
330 | - */ |
|
331 | - private function _isPseudoType(int $tag): bool |
|
332 | - { |
|
333 | - switch ($tag) { |
|
334 | - case self::TYPE_STRING: |
|
335 | - return $this instanceof StringType; |
|
336 | - case self::TYPE_TIME: |
|
337 | - return $this instanceof TimeType; |
|
338 | - } |
|
339 | - return false; |
|
340 | - } |
|
325 | + /** |
|
326 | + * Check whether the element is a pseudotype. |
|
327 | + * |
|
328 | + * @param int $tag |
|
329 | + * @return bool |
|
330 | + */ |
|
331 | + private function _isPseudoType(int $tag): bool |
|
332 | + { |
|
333 | + switch ($tag) { |
|
334 | + case self::TYPE_STRING: |
|
335 | + return $this instanceof StringType; |
|
336 | + case self::TYPE_TIME: |
|
337 | + return $this instanceof TimeType; |
|
338 | + } |
|
339 | + return false; |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * |
|
344 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
345 | - * @return bool |
|
346 | - */ |
|
347 | - public function isTagged(): bool |
|
348 | - { |
|
349 | - return $this instanceof TaggedType; |
|
350 | - } |
|
342 | + /** |
|
343 | + * |
|
344 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
345 | + * @return bool |
|
346 | + */ |
|
347 | + public function isTagged(): bool |
|
348 | + { |
|
349 | + return $this instanceof TaggedType; |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * |
|
354 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
355 | - * @return TaggedType |
|
356 | - */ |
|
357 | - public function expectTagged($tag = null): TaggedType |
|
358 | - { |
|
359 | - if (!$this->isTagged()) { |
|
360 | - throw new \UnexpectedValueException( |
|
361 | - sprintf("Context specific element expected, got %s.", |
|
362 | - Identifier::classToName($this->typeClass()))); |
|
363 | - } |
|
364 | - if (isset($tag) && $this->tag() != $tag) { |
|
365 | - throw new \UnexpectedValueException( |
|
366 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
367 | - } |
|
368 | - return $this; |
|
369 | - } |
|
352 | + /** |
|
353 | + * |
|
354 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
355 | + * @return TaggedType |
|
356 | + */ |
|
357 | + public function expectTagged($tag = null): TaggedType |
|
358 | + { |
|
359 | + if (!$this->isTagged()) { |
|
360 | + throw new \UnexpectedValueException( |
|
361 | + sprintf("Context specific element expected, got %s.", |
|
362 | + Identifier::classToName($this->typeClass()))); |
|
363 | + } |
|
364 | + if (isset($tag) && $this->tag() != $tag) { |
|
365 | + throw new \UnexpectedValueException( |
|
366 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
367 | + } |
|
368 | + return $this; |
|
369 | + } |
|
370 | 370 | |
371 | - /** |
|
372 | - * |
|
373 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
374 | - * @return Element |
|
375 | - */ |
|
376 | - final public function asElement(): Element |
|
377 | - { |
|
378 | - return $this; |
|
379 | - } |
|
371 | + /** |
|
372 | + * |
|
373 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
374 | + * @return Element |
|
375 | + */ |
|
376 | + final public function asElement(): Element |
|
377 | + { |
|
378 | + return $this; |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * Get element decorated with UnspecifiedType object. |
|
383 | - * |
|
384 | - * @return UnspecifiedType |
|
385 | - */ |
|
386 | - public function asUnspecified(): UnspecifiedType |
|
387 | - { |
|
388 | - return new UnspecifiedType($this); |
|
389 | - } |
|
381 | + /** |
|
382 | + * Get element decorated with UnspecifiedType object. |
|
383 | + * |
|
384 | + * @return UnspecifiedType |
|
385 | + */ |
|
386 | + public function asUnspecified(): UnspecifiedType |
|
387 | + { |
|
388 | + return new UnspecifiedType($this); |
|
389 | + } |
|
390 | 390 | |
391 | - /** |
|
392 | - * Determine the class that implements the type. |
|
393 | - * |
|
394 | - * @param Identifier $identifier |
|
395 | - * @return string Class name |
|
396 | - */ |
|
397 | - protected static function _determineImplClass(Identifier $identifier): string |
|
398 | - { |
|
399 | - switch ($identifier->typeClass()) { |
|
400 | - case Identifier::CLASS_UNIVERSAL: |
|
401 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
402 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
403 | - return TaggedType::class; |
|
404 | - case Identifier::CLASS_APPLICATION: |
|
405 | - return ApplicationType::class; |
|
406 | - case Identifier::CLASS_PRIVATE: |
|
407 | - return PrivateType::class; |
|
408 | - } |
|
409 | - throw new \UnexpectedValueException( |
|
410 | - sprintf("%s %d not implemented.", |
|
411 | - Identifier::classToName($identifier->typeClass()), |
|
412 | - $identifier->tag())); |
|
413 | - } |
|
391 | + /** |
|
392 | + * Determine the class that implements the type. |
|
393 | + * |
|
394 | + * @param Identifier $identifier |
|
395 | + * @return string Class name |
|
396 | + */ |
|
397 | + protected static function _determineImplClass(Identifier $identifier): string |
|
398 | + { |
|
399 | + switch ($identifier->typeClass()) { |
|
400 | + case Identifier::CLASS_UNIVERSAL: |
|
401 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
402 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
403 | + return TaggedType::class; |
|
404 | + case Identifier::CLASS_APPLICATION: |
|
405 | + return ApplicationType::class; |
|
406 | + case Identifier::CLASS_PRIVATE: |
|
407 | + return PrivateType::class; |
|
408 | + } |
|
409 | + throw new \UnexpectedValueException( |
|
410 | + sprintf("%s %d not implemented.", |
|
411 | + Identifier::classToName($identifier->typeClass()), |
|
412 | + $identifier->tag())); |
|
413 | + } |
|
414 | 414 | |
415 | - /** |
|
416 | - * Determine the class that implements an universal type of the given tag. |
|
417 | - * |
|
418 | - * @param int $tag |
|
419 | - * @throws \UnexpectedValueException |
|
420 | - * @return string Class name |
|
421 | - */ |
|
422 | - protected static function _determineUniversalImplClass(int $tag): string |
|
423 | - { |
|
424 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
425 | - throw new \UnexpectedValueException( |
|
426 | - "Universal tag $tag not implemented."); |
|
427 | - } |
|
428 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
429 | - } |
|
415 | + /** |
|
416 | + * Determine the class that implements an universal type of the given tag. |
|
417 | + * |
|
418 | + * @param int $tag |
|
419 | + * @throws \UnexpectedValueException |
|
420 | + * @return string Class name |
|
421 | + */ |
|
422 | + protected static function _determineUniversalImplClass(int $tag): string |
|
423 | + { |
|
424 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
425 | + throw new \UnexpectedValueException( |
|
426 | + "Universal tag $tag not implemented."); |
|
427 | + } |
|
428 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
429 | + } |
|
430 | 430 | |
431 | - /** |
|
432 | - * Get textual description of the type for debugging purposes. |
|
433 | - * |
|
434 | - * @return string |
|
435 | - */ |
|
436 | - protected function _typeDescriptorString(): string |
|
437 | - { |
|
438 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
439 | - return self::tagToName($this->_typeTag); |
|
440 | - } |
|
441 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
442 | - $this->_typeTag); |
|
443 | - } |
|
431 | + /** |
|
432 | + * Get textual description of the type for debugging purposes. |
|
433 | + * |
|
434 | + * @return string |
|
435 | + */ |
|
436 | + protected function _typeDescriptorString(): string |
|
437 | + { |
|
438 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
439 | + return self::tagToName($this->_typeTag); |
|
440 | + } |
|
441 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
442 | + $this->_typeTag); |
|
443 | + } |
|
444 | 444 | |
445 | - /** |
|
446 | - * Get human readable name for an universal tag. |
|
447 | - * |
|
448 | - * @param int $tag |
|
449 | - * @return string |
|
450 | - */ |
|
451 | - public static function tagToName(int $tag): string |
|
452 | - { |
|
453 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
454 | - return "TAG $tag"; |
|
455 | - } |
|
456 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
457 | - } |
|
445 | + /** |
|
446 | + * Get human readable name for an universal tag. |
|
447 | + * |
|
448 | + * @param int $tag |
|
449 | + * @return string |
|
450 | + */ |
|
451 | + public static function tagToName(int $tag): string |
|
452 | + { |
|
453 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
454 | + return "TAG $tag"; |
|
455 | + } |
|
456 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
457 | + } |
|
458 | 458 | } |