@@ -14,41 +14,41 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class PrimitiveString extends StringType |
16 | 16 | { |
17 | - use PrimitiveType; |
|
17 | + use PrimitiveType; |
|
18 | 18 | |
19 | - /** |
|
20 | - * |
|
21 | - * @see \ASN1\Element::_encodedContentDER() |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - protected function _encodedContentDER(): string |
|
25 | - { |
|
26 | - return $this->_string; |
|
27 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * @see \ASN1\Element::_encodedContentDER() |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + protected function _encodedContentDER(): string |
|
25 | + { |
|
26 | + return $this->_string; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * |
|
31 | - * {@inheritdoc} |
|
32 | - * @see \ASN1\Element::_decodeFromDER() |
|
33 | - * @return self |
|
34 | - */ |
|
35 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
36 | - int &$offset): ElementBase |
|
37 | - { |
|
38 | - $idx = $offset; |
|
39 | - if (!$identifier->isPrimitive()) { |
|
40 | - throw new DecodeException("DER encoded string must be primitive."); |
|
41 | - } |
|
42 | - $length = Length::expectFromDER($data, $idx); |
|
43 | - $str = $length->length() ? substr($data, $idx, $length->length()) : ""; |
|
44 | - // substr should never return false, since length is |
|
45 | - // checked by Length::expectFromDER. |
|
46 | - assert(is_string($str), "substr"); |
|
47 | - $offset = $idx + $length->length(); |
|
48 | - try { |
|
49 | - return new static($str); |
|
50 | - } catch (\InvalidArgumentException $e) { |
|
51 | - throw new DecodeException($e->getMessage(), 0, $e); |
|
52 | - } |
|
53 | - } |
|
29 | + /** |
|
30 | + * |
|
31 | + * {@inheritdoc} |
|
32 | + * @see \ASN1\Element::_decodeFromDER() |
|
33 | + * @return self |
|
34 | + */ |
|
35 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
36 | + int &$offset): ElementBase |
|
37 | + { |
|
38 | + $idx = $offset; |
|
39 | + if (!$identifier->isPrimitive()) { |
|
40 | + throw new DecodeException("DER encoded string must be primitive."); |
|
41 | + } |
|
42 | + $length = Length::expectFromDER($data, $idx); |
|
43 | + $str = $length->length() ? substr($data, $idx, $length->length()) : ""; |
|
44 | + // substr should never return false, since length is |
|
45 | + // checked by Length::expectFromDER. |
|
46 | + assert(is_string($str), "substr"); |
|
47 | + $offset = $idx + $length->length(); |
|
48 | + try { |
|
49 | + return new static($str); |
|
50 | + } catch (\InvalidArgumentException $e) { |
|
51 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | } |
@@ -33,7 +33,7 @@ |
||
33 | 33 | * @return self |
34 | 34 | */ |
35 | 35 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
36 | - int &$offset): ElementBase |
|
36 | + int & $offset): ElementBase |
|
37 | 37 | { |
38 | 38 | $idx = $offset; |
39 | 39 | if (!$identifier->isPrimitive()) { |
@@ -17,194 +17,194 @@ |
||
17 | 17 | */ |
18 | 18 | class BitString extends StringType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Number of unused bits in the last octet. |
|
25 | - * |
|
26 | - * @var int $_unusedBits |
|
27 | - */ |
|
28 | - protected $_unusedBits; |
|
23 | + /** |
|
24 | + * Number of unused bits in the last octet. |
|
25 | + * |
|
26 | + * @var int $_unusedBits |
|
27 | + */ |
|
28 | + protected $_unusedBits; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $string Content octets |
|
34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
35 | - */ |
|
36 | - public function __construct(string $string, int $unused_bits = 0) |
|
37 | - { |
|
38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | - parent::__construct($string); |
|
40 | - $this->_unusedBits = $unused_bits; |
|
41 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $string Content octets |
|
34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
35 | + */ |
|
36 | + public function __construct(string $string, int $unused_bits = 0) |
|
37 | + { |
|
38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | + parent::__construct($string); |
|
40 | + $this->_unusedBits = $unused_bits; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the number of bits in the string. |
|
45 | - * |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - public function numBits(): int |
|
49 | - { |
|
50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Get the number of bits in the string. |
|
45 | + * |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + public function numBits(): int |
|
49 | + { |
|
50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the number of unused bits in the last octet of the string. |
|
55 | - * |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public function unusedBits(): int |
|
59 | - { |
|
60 | - return $this->_unusedBits; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Get the number of unused bits in the last octet of the string. |
|
55 | + * |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public function unusedBits(): int |
|
59 | + { |
|
60 | + return $this->_unusedBits; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Test whether bit is set. |
|
65 | - * |
|
66 | - * @param int $idx Bit index. |
|
67 | - * Most significant bit of the first octet is index 0. |
|
68 | - * @return boolean |
|
69 | - */ |
|
70 | - public function testBit($idx): bool |
|
71 | - { |
|
72 | - // octet index |
|
73 | - $oi = (int) floor($idx / 8); |
|
74 | - // if octet is outside range |
|
75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
76 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
77 | - } |
|
78 | - // bit index |
|
79 | - $bi = $idx % 8; |
|
80 | - // if tested bit is last octet's unused bit |
|
81 | - if ($oi == strlen($this->_string) - 1) { |
|
82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
83 | - throw new \OutOfBoundsException("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 | - } |
|
63 | + /** |
|
64 | + * Test whether bit is set. |
|
65 | + * |
|
66 | + * @param int $idx Bit index. |
|
67 | + * Most significant bit of the first octet is index 0. |
|
68 | + * @return boolean |
|
69 | + */ |
|
70 | + public function testBit($idx): bool |
|
71 | + { |
|
72 | + // octet index |
|
73 | + $oi = (int) floor($idx / 8); |
|
74 | + // if octet is outside range |
|
75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
76 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
77 | + } |
|
78 | + // bit index |
|
79 | + $bi = $idx % 8; |
|
80 | + // if tested bit is last octet's unused bit |
|
81 | + if ($oi == strlen($this->_string) - 1) { |
|
82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
83 | + throw new \OutOfBoundsException("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, string $data, |
|
182 | - int &$offset): ElementBase |
|
183 | - { |
|
184 | - $idx = $offset; |
|
185 | - $length = Length::expectFromDER($data, $idx); |
|
186 | - if ($length->length() < 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->length() - 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, string $data, |
|
182 | + int &$offset): ElementBase |
|
183 | + { |
|
184 | + $idx = $offset; |
|
185 | + $length = Length::expectFromDER($data, $idx); |
|
186 | + if ($length->length() < 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->length() - 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 | } |
@@ -179,7 +179,7 @@ |
||
179 | 179 | * @return self |
180 | 180 | */ |
181 | 181 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
182 | - int &$offset): ElementBase |
|
182 | + int & $offset): ElementBase |
|
183 | 183 | { |
184 | 184 | $idx = $offset; |
185 | 185 | $length = Length::expectFromDER($data, $idx); |
@@ -17,41 +17,41 @@ |
||
17 | 17 | */ |
18 | 18 | class NullType extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor. |
|
25 | - */ |
|
26 | - public function __construct() |
|
27 | - { |
|
28 | - $this->_typeTag = self::TYPE_NULL; |
|
29 | - } |
|
23 | + /** |
|
24 | + * Constructor. |
|
25 | + */ |
|
26 | + public function __construct() |
|
27 | + { |
|
28 | + $this->_typeTag = self::TYPE_NULL; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * |
|
33 | - * {@inheritdoc} |
|
34 | - */ |
|
35 | - protected function _encodedContentDER(): string |
|
36 | - { |
|
37 | - return ""; |
|
38 | - } |
|
31 | + /** |
|
32 | + * |
|
33 | + * {@inheritdoc} |
|
34 | + */ |
|
35 | + protected function _encodedContentDER(): string |
|
36 | + { |
|
37 | + return ""; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * |
|
42 | - * {@inheritdoc} |
|
43 | - * @return self |
|
44 | - */ |
|
45 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
46 | - int &$offset): ElementBase |
|
47 | - { |
|
48 | - $idx = $offset; |
|
49 | - if (!$identifier->isPrimitive()) { |
|
50 | - throw new DecodeException("Null value must be primitive."); |
|
51 | - } |
|
52 | - // null type has always zero length |
|
53 | - Length::expectFromDER($data, $idx, 0); |
|
54 | - $offset = $idx; |
|
55 | - return new self(); |
|
56 | - } |
|
40 | + /** |
|
41 | + * |
|
42 | + * {@inheritdoc} |
|
43 | + * @return self |
|
44 | + */ |
|
45 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
46 | + int &$offset): ElementBase |
|
47 | + { |
|
48 | + $idx = $offset; |
|
49 | + if (!$identifier->isPrimitive()) { |
|
50 | + throw new DecodeException("Null value must be primitive."); |
|
51 | + } |
|
52 | + // null type has always zero length |
|
53 | + Length::expectFromDER($data, $idx, 0); |
|
54 | + $offset = $idx; |
|
55 | + return new self(); |
|
56 | + } |
|
57 | 57 | } |
@@ -43,7 +43,7 @@ |
||
43 | 43 | * @return self |
44 | 44 | */ |
45 | 45 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
46 | - int &$offset): ElementBase |
|
46 | + int & $offset): ElementBase |
|
47 | 47 | { |
48 | 48 | $idx = $offset; |
49 | 49 | if (!$identifier->isPrimitive()) { |
@@ -17,64 +17,64 @@ |
||
17 | 17 | */ |
18 | 18 | class Boolean extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Value. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_bool; |
|
23 | + /** |
|
24 | + * Value. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_bool; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param bool $bool |
|
34 | - */ |
|
35 | - public function __construct(bool $bool) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
38 | - $this->_bool = $bool; |
|
39 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param bool $bool |
|
34 | + */ |
|
35 | + public function __construct(bool $bool) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
38 | + $this->_bool = $bool; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get the value. |
|
43 | - * |
|
44 | - * @return bool |
|
45 | - */ |
|
46 | - public function value(): bool |
|
47 | - { |
|
48 | - return $this->_bool; |
|
49 | - } |
|
41 | + /** |
|
42 | + * Get the value. |
|
43 | + * |
|
44 | + * @return bool |
|
45 | + */ |
|
46 | + public function value(): bool |
|
47 | + { |
|
48 | + return $this->_bool; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - return $this->_bool ? chr(0xff) : chr(0); |
|
58 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + return $this->_bool ? chr(0xff) : chr(0); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * |
|
62 | - * {@inheritdoc} |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
66 | - int &$offset): ElementBase |
|
67 | - { |
|
68 | - $idx = $offset; |
|
69 | - Length::expectFromDER($data, $idx, 1); |
|
70 | - $byte = ord($data[$idx++]); |
|
71 | - if ($byte !== 0) { |
|
72 | - if ($byte != 0xff) { |
|
73 | - throw new DecodeException( |
|
74 | - "DER encoded boolean true must have all bits set to 1."); |
|
75 | - } |
|
76 | - } |
|
77 | - $offset = $idx; |
|
78 | - return new self($byte !== 0); |
|
79 | - } |
|
60 | + /** |
|
61 | + * |
|
62 | + * {@inheritdoc} |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
66 | + int &$offset): ElementBase |
|
67 | + { |
|
68 | + $idx = $offset; |
|
69 | + Length::expectFromDER($data, $idx, 1); |
|
70 | + $byte = ord($data[$idx++]); |
|
71 | + if ($byte !== 0) { |
|
72 | + if ($byte != 0xff) { |
|
73 | + throw new DecodeException( |
|
74 | + "DER encoded boolean true must have all bits set to 1."); |
|
75 | + } |
|
76 | + } |
|
77 | + $offset = $idx; |
|
78 | + return new self($byte !== 0); |
|
79 | + } |
|
80 | 80 | } |
@@ -63,7 +63,7 @@ |
||
63 | 63 | * @return self |
64 | 64 | */ |
65 | 65 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
66 | - int &$offset): ElementBase |
|
66 | + int & $offset): ElementBase |
|
67 | 67 | { |
68 | 68 | $idx = $offset; |
69 | 69 | Length::expectFromDER($data, $idx, 1); |
@@ -13,38 +13,38 @@ |
||
13 | 13 | */ |
14 | 14 | class RelativeOID extends ObjectIdentifier |
15 | 15 | { |
16 | - /** |
|
17 | - * Constructor. |
|
18 | - * |
|
19 | - * @param string $oid OID in dotted format |
|
20 | - */ |
|
21 | - public function __construct(string $oid) |
|
22 | - { |
|
23 | - $this->_oid = $oid; |
|
24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
25 | - } |
|
16 | + /** |
|
17 | + * Constructor. |
|
18 | + * |
|
19 | + * @param string $oid OID in dotted format |
|
20 | + */ |
|
21 | + public function __construct(string $oid) |
|
22 | + { |
|
23 | + $this->_oid = $oid; |
|
24 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _encodedContentDER(): string |
|
32 | - { |
|
33 | - return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _encodedContentDER(): string |
|
32 | + { |
|
33 | + return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
42 | - int &$offset): ElementBase |
|
43 | - { |
|
44 | - $idx = $offset; |
|
45 | - $len = Length::expectFromDER($data, $idx)->length(); |
|
46 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
47 | - $offset = $idx + $len; |
|
48 | - return new self(self::_implodeSubIDs(...$subids)); |
|
49 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
42 | + int &$offset): ElementBase |
|
43 | + { |
|
44 | + $idx = $offset; |
|
45 | + $len = Length::expectFromDER($data, $idx)->length(); |
|
46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
47 | + $offset = $idx + $len; |
|
48 | + return new self(self::_implodeSubIDs(...$subids)); |
|
49 | + } |
|
50 | 50 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | * @return self |
40 | 40 | */ |
41 | 41 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
42 | - int &$offset): ElementBase |
|
42 | + int & $offset): ElementBase |
|
43 | 43 | { |
44 | 44 | $idx = $offset; |
45 | 45 | $len = Length::expectFromDER($data, $idx)->length(); |
@@ -17,160 +17,160 @@ |
||
17 | 17 | */ |
18 | 18 | class ObjectIdentifier extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Object identifier in dotted format. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $_oid; |
|
23 | + /** |
|
24 | + * Object identifier in dotted format. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $_oid; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $oid OID in dotted format |
|
34 | - */ |
|
35 | - public function __construct(string $oid) |
|
36 | - { |
|
37 | - $this->_oid = $oid; |
|
38 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
39 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $oid OID in dotted format |
|
34 | + */ |
|
35 | + public function __construct(string $oid) |
|
36 | + { |
|
37 | + $this->_oid = $oid; |
|
38 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get OID in dotted format. |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function oid(): string |
|
47 | - { |
|
48 | - return $this->_oid; |
|
49 | - } |
|
41 | + /** |
|
42 | + * Get OID in dotted format. |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function oid(): string |
|
47 | + { |
|
48 | + return $this->_oid; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - $subids = self::_explodeDottedOID($this->_oid); |
|
58 | - // encode first two subids to one according to spec section 8.19.4 |
|
59 | - if (count($subids) >= 2) { |
|
60 | - $num = ($subids[0] * 40) + $subids[1]; |
|
61 | - array_splice($subids, 0, 2, array($num)); |
|
62 | - } |
|
63 | - return self::_encodeSubIDs(...$subids); |
|
64 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + $subids = self::_explodeDottedOID($this->_oid); |
|
58 | + // encode first two subids to one according to spec section 8.19.4 |
|
59 | + if (count($subids) >= 2) { |
|
60 | + $num = ($subids[0] * 40) + $subids[1]; |
|
61 | + array_splice($subids, 0, 2, array($num)); |
|
62 | + } |
|
63 | + return self::_encodeSubIDs(...$subids); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * {@inheritdoc} |
|
69 | - * @return self |
|
70 | - */ |
|
71 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
72 | - int &$offset): ElementBase |
|
73 | - { |
|
74 | - $idx = $offset; |
|
75 | - $len = Length::expectFromDER($data, $idx)->length(); |
|
76 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
77 | - $idx += $len; |
|
78 | - // decode first subidentifier according to spec section 8.19.4 |
|
79 | - if (isset($subids[0])) { |
|
80 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
81 | - array_splice($subids, 0, 1, array($x, $y)); |
|
82 | - } |
|
83 | - $offset = $idx; |
|
84 | - return new self(self::_implodeSubIDs(...$subids)); |
|
85 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * {@inheritdoc} |
|
69 | + * @return self |
|
70 | + */ |
|
71 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
72 | + int &$offset): ElementBase |
|
73 | + { |
|
74 | + $idx = $offset; |
|
75 | + $len = Length::expectFromDER($data, $idx)->length(); |
|
76 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
77 | + $idx += $len; |
|
78 | + // decode first subidentifier according to spec section 8.19.4 |
|
79 | + if (isset($subids[0])) { |
|
80 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
81 | + array_splice($subids, 0, 1, array($x, $y)); |
|
82 | + } |
|
83 | + $offset = $idx; |
|
84 | + return new self(self::_implodeSubIDs(...$subids)); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Explode dotted OID to an array of sub ID's. |
|
89 | - * |
|
90 | - * @param string $oid OID in dotted format |
|
91 | - * @return \GMP[] Array of GMP numbers |
|
92 | - */ |
|
93 | - protected static function _explodeDottedOID($oid): array |
|
94 | - { |
|
95 | - $subids = []; |
|
96 | - foreach (explode(".", $oid) as $subid) { |
|
97 | - $subids[] = gmp_init($subid, 10); |
|
98 | - } |
|
99 | - return $subids; |
|
100 | - } |
|
87 | + /** |
|
88 | + * Explode dotted OID to an array of sub ID's. |
|
89 | + * |
|
90 | + * @param string $oid OID in dotted format |
|
91 | + * @return \GMP[] Array of GMP numbers |
|
92 | + */ |
|
93 | + protected static function _explodeDottedOID($oid): array |
|
94 | + { |
|
95 | + $subids = []; |
|
96 | + foreach (explode(".", $oid) as $subid) { |
|
97 | + $subids[] = gmp_init($subid, 10); |
|
98 | + } |
|
99 | + return $subids; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Implode an array of sub IDs to dotted OID format. |
|
104 | - * |
|
105 | - * @param \GMP[] $subids |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
109 | - { |
|
110 | - return implode(".", |
|
111 | - array_map( |
|
112 | - function ($num) { |
|
113 | - return gmp_strval($num, 10); |
|
114 | - }, $subids)); |
|
115 | - } |
|
102 | + /** |
|
103 | + * Implode an array of sub IDs to dotted OID format. |
|
104 | + * |
|
105 | + * @param \GMP[] $subids |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
109 | + { |
|
110 | + return implode(".", |
|
111 | + array_map( |
|
112 | + function ($num) { |
|
113 | + return gmp_strval($num, 10); |
|
114 | + }, $subids)); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Encode sub ID's to DER. |
|
119 | - * |
|
120 | - * @param \GMP[] $subids |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
124 | - { |
|
125 | - $data = ""; |
|
126 | - foreach ($subids as $subid) { |
|
127 | - // if number fits to one base 128 byte |
|
128 | - if ($subid < 128) { |
|
129 | - $data .= chr(intval($subid)); |
|
130 | - } else { // encode to multiple bytes |
|
131 | - $bytes = []; |
|
132 | - do { |
|
133 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
134 | - $subid >>= 7; |
|
135 | - } while ($subid > 0); |
|
136 | - // all bytes except last must have bit 8 set to one |
|
137 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
138 | - $data .= chr(0x80 | $byte); |
|
139 | - } |
|
140 | - $data .= chr(reset($bytes)); |
|
141 | - } |
|
142 | - } |
|
143 | - return $data; |
|
144 | - } |
|
117 | + /** |
|
118 | + * Encode sub ID's to DER. |
|
119 | + * |
|
120 | + * @param \GMP[] $subids |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
124 | + { |
|
125 | + $data = ""; |
|
126 | + foreach ($subids as $subid) { |
|
127 | + // if number fits to one base 128 byte |
|
128 | + if ($subid < 128) { |
|
129 | + $data .= chr(intval($subid)); |
|
130 | + } else { // encode to multiple bytes |
|
131 | + $bytes = []; |
|
132 | + do { |
|
133 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
134 | + $subid >>= 7; |
|
135 | + } while ($subid > 0); |
|
136 | + // all bytes except last must have bit 8 set to one |
|
137 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
138 | + $data .= chr(0x80 | $byte); |
|
139 | + } |
|
140 | + $data .= chr(reset($bytes)); |
|
141 | + } |
|
142 | + } |
|
143 | + return $data; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Decode sub ID's from DER data. |
|
148 | - * |
|
149 | - * @param string $data |
|
150 | - * @throws DecodeException |
|
151 | - * @return \GMP[] Array of GMP numbers |
|
152 | - */ |
|
153 | - protected static function _decodeSubIDs($data): array |
|
154 | - { |
|
155 | - $subids = []; |
|
156 | - $idx = 0; |
|
157 | - $end = strlen($data); |
|
158 | - while ($idx < $end) { |
|
159 | - $num = gmp_init("0", 10); |
|
160 | - while (true) { |
|
161 | - if ($idx >= $end) { |
|
162 | - throw new DecodeException("Unexpected end of data."); |
|
163 | - } |
|
164 | - $byte = ord($data[$idx++]); |
|
165 | - $num |= $byte & 0x7f; |
|
166 | - // bit 8 of the last octet is zero |
|
167 | - if (!($byte & 0x80)) { |
|
168 | - break; |
|
169 | - } |
|
170 | - $num <<= 7; |
|
171 | - } |
|
172 | - $subids[] = $num; |
|
173 | - } |
|
174 | - return $subids; |
|
175 | - } |
|
146 | + /** |
|
147 | + * Decode sub ID's from DER data. |
|
148 | + * |
|
149 | + * @param string $data |
|
150 | + * @throws DecodeException |
|
151 | + * @return \GMP[] Array of GMP numbers |
|
152 | + */ |
|
153 | + protected static function _decodeSubIDs($data): array |
|
154 | + { |
|
155 | + $subids = []; |
|
156 | + $idx = 0; |
|
157 | + $end = strlen($data); |
|
158 | + while ($idx < $end) { |
|
159 | + $num = gmp_init("0", 10); |
|
160 | + while (true) { |
|
161 | + if ($idx >= $end) { |
|
162 | + throw new DecodeException("Unexpected end of data."); |
|
163 | + } |
|
164 | + $byte = ord($data[$idx++]); |
|
165 | + $num |= $byte & 0x7f; |
|
166 | + // bit 8 of the last octet is zero |
|
167 | + if (!($byte & 0x80)) { |
|
168 | + break; |
|
169 | + } |
|
170 | + $num <<= 7; |
|
171 | + } |
|
172 | + $subids[] = $num; |
|
173 | + } |
|
174 | + return $subids; |
|
175 | + } |
|
176 | 176 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @return self |
70 | 70 | */ |
71 | 71 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
72 | - int &$offset): ElementBase |
|
72 | + int & $offset): ElementBase |
|
73 | 73 | { |
74 | 74 | $idx = $offset; |
75 | 75 | $len = Length::expectFromDER($data, $idx)->length(); |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | { |
110 | 110 | return implode(".", |
111 | 111 | array_map( |
112 | - function ($num) { |
|
112 | + function($num) { |
|
113 | 113 | return gmp_strval($num, 10); |
114 | 114 | }, $subids)); |
115 | 115 | } |
@@ -17,112 +17,112 @@ |
||
17 | 17 | */ |
18 | 18 | class GeneralizedTime extends TimeType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 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 = /* @formatter:off */ '#^' . |
|
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 | - '$#' /* @formatter:on */; |
|
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 = /* @formatter:off */ '#^' . |
|
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 | + '$#' /* @formatter:on */; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Cached formatted date. |
|
43 | - * |
|
44 | - * @var string|null |
|
45 | - */ |
|
46 | - private $_formatted; |
|
41 | + /** |
|
42 | + * Cached formatted date. |
|
43 | + * |
|
44 | + * @var string|null |
|
45 | + */ |
|
46 | + private $_formatted; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param \DateTimeImmutable $dt |
|
52 | - */ |
|
53 | - public function __construct(\DateTimeImmutable $dt) |
|
54 | - { |
|
55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | - parent::__construct($dt); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param \DateTimeImmutable $dt |
|
52 | + */ |
|
53 | + public function __construct(\DateTimeImmutable $dt) |
|
54 | + { |
|
55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | + parent::__construct($dt); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Clear cached variables on clone. |
|
61 | - */ |
|
62 | - public function __clone() |
|
63 | - { |
|
64 | - $this->_formatted = null; |
|
65 | - } |
|
59 | + /** |
|
60 | + * Clear cached variables on clone. |
|
61 | + */ |
|
62 | + public function __clone() |
|
63 | + { |
|
64 | + $this->_formatted = null; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - protected function _encodedContentDER(): string |
|
72 | - { |
|
73 | - if (!isset($this->_formatted)) { |
|
74 | - $dt = $this->_dateTime->setTimezone( |
|
75 | - self::_createTimeZone(self::TZ_UTC)); |
|
76 | - $this->_formatted = $dt->format("YmdHis"); |
|
77 | - // if fractions were used |
|
78 | - $frac = $dt->format("u"); |
|
79 | - if ($frac != 0) { |
|
80 | - $frac = rtrim($frac, "0"); |
|
81 | - $this->_formatted .= ".$frac"; |
|
82 | - } |
|
83 | - // timezone |
|
84 | - $this->_formatted .= "Z"; |
|
85 | - } |
|
86 | - return $this->_formatted; |
|
87 | - } |
|
67 | + /** |
|
68 | + * |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + protected function _encodedContentDER(): string |
|
72 | + { |
|
73 | + if (!isset($this->_formatted)) { |
|
74 | + $dt = $this->_dateTime->setTimezone( |
|
75 | + self::_createTimeZone(self::TZ_UTC)); |
|
76 | + $this->_formatted = $dt->format("YmdHis"); |
|
77 | + // if fractions were used |
|
78 | + $frac = $dt->format("u"); |
|
79 | + if ($frac != 0) { |
|
80 | + $frac = rtrim($frac, "0"); |
|
81 | + $this->_formatted .= ".$frac"; |
|
82 | + } |
|
83 | + // timezone |
|
84 | + $this->_formatted .= "Z"; |
|
85 | + } |
|
86 | + return $this->_formatted; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * |
|
91 | - * {@inheritdoc} |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
95 | - int &$offset): ElementBase |
|
96 | - { |
|
97 | - $idx = $offset; |
|
98 | - $length = Length::expectFromDER($data, $idx); |
|
99 | - $str = substr($data, $idx, $length->length()); |
|
100 | - $idx += $length->length(); |
|
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 | - } |
|
89 | + /** |
|
90 | + * |
|
91 | + * {@inheritdoc} |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
95 | + int &$offset): ElementBase |
|
96 | + { |
|
97 | + $idx = $offset; |
|
98 | + $length = Length::expectFromDER($data, $idx); |
|
99 | + $str = substr($data, $idx, $length->length()); |
|
100 | + $idx += $length->length(); |
|
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 | } |
@@ -92,7 +92,7 @@ |
||
92 | 92 | * @return self |
93 | 93 | */ |
94 | 94 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
95 | - int &$offset): ElementBase |
|
95 | + int & $offset): ElementBase |
|
96 | 96 | { |
97 | 97 | $idx = $offset; |
98 | 98 | $length = Length::expectFromDER($data, $idx); |
@@ -17,72 +17,72 @@ |
||
17 | 17 | */ |
18 | 18 | class UTCTime extends TimeType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 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 = /* @formatter:off */ '#^' . |
|
31 | - '(\d\d)' . /* YY */ |
|
32 | - '(\d\d)' . /* MM */ |
|
33 | - '(\d\d)' . /* DD */ |
|
34 | - '(\d\d)' . /* hh */ |
|
35 | - '(\d\d)' . /* mm */ |
|
36 | - '(\d\d)' . /* ss */ |
|
37 | - 'Z' . /* TZ */ |
|
38 | - '$#' /* @formatter:on */; |
|
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 = /* @formatter:off */ '#^' . |
|
31 | + '(\d\d)' . /* YY */ |
|
32 | + '(\d\d)' . /* MM */ |
|
33 | + '(\d\d)' . /* DD */ |
|
34 | + '(\d\d)' . /* hh */ |
|
35 | + '(\d\d)' . /* mm */ |
|
36 | + '(\d\d)' . /* ss */ |
|
37 | + 'Z' . /* TZ */ |
|
38 | + '$#' /* @formatter:on */; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor. |
|
42 | - * |
|
43 | - * @param \DateTimeImmutable $dt |
|
44 | - */ |
|
45 | - public function __construct(\DateTimeImmutable $dt) |
|
46 | - { |
|
47 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | - parent::__construct($dt); |
|
49 | - } |
|
40 | + /** |
|
41 | + * Constructor. |
|
42 | + * |
|
43 | + * @param \DateTimeImmutable $dt |
|
44 | + */ |
|
45 | + public function __construct(\DateTimeImmutable $dt) |
|
46 | + { |
|
47 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | + parent::__construct($dt); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
58 | - return $dt->format("ymdHis\Z"); |
|
59 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
58 | + return $dt->format("ymdHis\Z"); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * |
|
63 | - * {@inheritdoc} |
|
64 | - * @return self |
|
65 | - */ |
|
66 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
67 | - int &$offset): ElementBase |
|
68 | - { |
|
69 | - $idx = $offset; |
|
70 | - $length = Length::expectFromDER($data, $idx); |
|
71 | - $str = substr($data, $idx, $length->length()); |
|
72 | - $idx += $length->length(); |
|
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 | - } |
|
61 | + /** |
|
62 | + * |
|
63 | + * {@inheritdoc} |
|
64 | + * @return self |
|
65 | + */ |
|
66 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
67 | + int &$offset): ElementBase |
|
68 | + { |
|
69 | + $idx = $offset; |
|
70 | + $length = Length::expectFromDER($data, $idx); |
|
71 | + $str = substr($data, $idx, $length->length()); |
|
72 | + $idx += $length->length(); |
|
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 | } |
@@ -64,7 +64,7 @@ |
||
64 | 64 | * @return self |
65 | 65 | */ |
66 | 66 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
67 | - int &$offset): ElementBase |
|
67 | + int & $offset): ElementBase |
|
68 | 68 | { |
69 | 69 | $idx = $offset; |
70 | 70 | $length = Length::expectFromDER($data, $idx); |
@@ -16,156 +16,156 @@ |
||
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 | - * Number as a base 10. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - private $_number; |
|
22 | + /** |
|
23 | + * Number as a base 10. |
|
24 | + * |
|
25 | + * @var string |
|
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 = strval($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 = strval($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; |
|
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; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - protected function _encodedContentDER(): string |
|
59 | - { |
|
60 | - $num = gmp_init($this->_number, 10); |
|
61 | - switch (gmp_sign($num)) { |
|
62 | - // positive |
|
63 | - case 1: |
|
64 | - return self::_encodePositiveInteger($num); |
|
65 | - // negative |
|
66 | - case -1: |
|
67 | - return self::_encodeNegativeInteger($num); |
|
68 | - } |
|
69 | - // zero |
|
70 | - return "\0"; |
|
71 | - } |
|
54 | + /** |
|
55 | + * |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + protected function _encodedContentDER(): string |
|
59 | + { |
|
60 | + $num = gmp_init($this->_number, 10); |
|
61 | + switch (gmp_sign($num)) { |
|
62 | + // positive |
|
63 | + case 1: |
|
64 | + return self::_encodePositiveInteger($num); |
|
65 | + // negative |
|
66 | + case -1: |
|
67 | + return self::_encodeNegativeInteger($num); |
|
68 | + } |
|
69 | + // zero |
|
70 | + return "\0"; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Encode positive integer to DER content. |
|
75 | - * |
|
76 | - * @param \GMP|resource $num |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - private static function _encodePositiveInteger(\GMP $num): string |
|
80 | - { |
|
81 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
82 | - // if first bit is 1, prepend full zero byte |
|
83 | - // to represent positive two's complement |
|
84 | - if (ord($bin[0]) & 0x80) { |
|
85 | - $bin = chr(0x00) . $bin; |
|
86 | - } |
|
87 | - return $bin; |
|
88 | - } |
|
73 | + /** |
|
74 | + * Encode positive integer to DER content. |
|
75 | + * |
|
76 | + * @param \GMP|resource $num |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + private static function _encodePositiveInteger(\GMP $num): string |
|
80 | + { |
|
81 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
82 | + // if first bit is 1, prepend full zero byte |
|
83 | + // to represent positive two's complement |
|
84 | + if (ord($bin[0]) & 0x80) { |
|
85 | + $bin = chr(0x00) . $bin; |
|
86 | + } |
|
87 | + return $bin; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Encode negative integer to DER content. |
|
92 | - * |
|
93 | - * @param \GMP|resource $num |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
97 | - { |
|
98 | - $num = gmp_abs($num); |
|
99 | - // compute number of bytes required |
|
100 | - $width = 1; |
|
101 | - if ($num > 128) { |
|
102 | - $tmp = $num; |
|
103 | - do { |
|
104 | - $width++; |
|
105 | - $tmp >>= 8; |
|
106 | - } while ($tmp > 128); |
|
107 | - } |
|
108 | - // compute two's complement 2^n - x |
|
109 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
110 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
111 | - // if first bit is 0, prepend full inverted byte |
|
112 | - // to represent negative two's complement |
|
113 | - if (!(ord($bin[0]) & 0x80)) { |
|
114 | - $bin = chr(0xff) . $bin; |
|
115 | - } |
|
116 | - return $bin; |
|
117 | - } |
|
90 | + /** |
|
91 | + * Encode negative integer to DER content. |
|
92 | + * |
|
93 | + * @param \GMP|resource $num |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
97 | + { |
|
98 | + $num = gmp_abs($num); |
|
99 | + // compute number of bytes required |
|
100 | + $width = 1; |
|
101 | + if ($num > 128) { |
|
102 | + $tmp = $num; |
|
103 | + do { |
|
104 | + $width++; |
|
105 | + $tmp >>= 8; |
|
106 | + } while ($tmp > 128); |
|
107 | + } |
|
108 | + // compute two's complement 2^n - x |
|
109 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
110 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
111 | + // if first bit is 0, prepend full inverted byte |
|
112 | + // to represent negative two's complement |
|
113 | + if (!(ord($bin[0]) & 0x80)) { |
|
114 | + $bin = chr(0xff) . $bin; |
|
115 | + } |
|
116 | + return $bin; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * |
|
121 | - * {@inheritdoc} |
|
122 | - * @return self |
|
123 | - */ |
|
124 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
125 | - int &$offset): ElementBase |
|
126 | - { |
|
127 | - $idx = $offset; |
|
128 | - $length = Length::expectFromDER($data, $idx); |
|
129 | - if (gmp_cmp(gmp_init($length->length(), 10), gmp_init(PHP_INT_MAX, 10)) >= |
|
130 | - 0) { |
|
131 | - throw new \RuntimeException("Integer length too large"); |
|
132 | - } |
|
119 | + /** |
|
120 | + * |
|
121 | + * {@inheritdoc} |
|
122 | + * @return self |
|
123 | + */ |
|
124 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
125 | + int &$offset): ElementBase |
|
126 | + { |
|
127 | + $idx = $offset; |
|
128 | + $length = Length::expectFromDER($data, $idx); |
|
129 | + if (gmp_cmp(gmp_init($length->length(), 10), gmp_init(PHP_INT_MAX, 10)) >= |
|
130 | + 0) { |
|
131 | + throw new \RuntimeException("Integer length too large"); |
|
132 | + } |
|
133 | 133 | |
134 | - $bytes = substr($data, $idx, (int) $length->length()); |
|
135 | - $idx += $length->length(); |
|
136 | - $neg = ord($bytes[0]) & 0x80; |
|
137 | - // negative, apply inversion of two's complement |
|
138 | - if ($neg) { |
|
139 | - $len = strlen($bytes); |
|
140 | - for ($i = 0; $i < $len; $i++) { |
|
141 | - $bytes[$i] = ~$bytes[$i]; |
|
142 | - } |
|
143 | - } |
|
144 | - $num = gmp_init(bin2hex($bytes), 16); |
|
145 | - // negative, apply addition of two's complement |
|
146 | - // and produce negative result |
|
147 | - if ($neg) { |
|
148 | - $num = gmp_neg($num + 1); |
|
149 | - } |
|
150 | - $offset = $idx; |
|
151 | - // late static binding since enumerated extends integer type |
|
152 | - return new static(gmp_strval($num, 10)); |
|
153 | - } |
|
134 | + $bytes = substr($data, $idx, (int) $length->length()); |
|
135 | + $idx += $length->length(); |
|
136 | + $neg = ord($bytes[0]) & 0x80; |
|
137 | + // negative, apply inversion of two's complement |
|
138 | + if ($neg) { |
|
139 | + $len = strlen($bytes); |
|
140 | + for ($i = 0; $i < $len; $i++) { |
|
141 | + $bytes[$i] = ~$bytes[$i]; |
|
142 | + } |
|
143 | + } |
|
144 | + $num = gmp_init(bin2hex($bytes), 16); |
|
145 | + // negative, apply addition of two's complement |
|
146 | + // and produce negative result |
|
147 | + if ($neg) { |
|
148 | + $num = gmp_neg($num + 1); |
|
149 | + } |
|
150 | + $offset = $idx; |
|
151 | + // late static binding since enumerated extends integer type |
|
152 | + return new static(gmp_strval($num, 10)); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * Test that number is valid for this context. |
|
157 | - * |
|
158 | - * @param mixed $num |
|
159 | - * @return boolean |
|
160 | - */ |
|
161 | - private static function _validateNumber($num): bool |
|
162 | - { |
|
163 | - if (is_int($num)) { |
|
164 | - return true; |
|
165 | - } |
|
166 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
167 | - return true; |
|
168 | - } |
|
169 | - return false; |
|
170 | - } |
|
155 | + /** |
|
156 | + * Test that number is valid for this context. |
|
157 | + * |
|
158 | + * @param mixed $num |
|
159 | + * @return boolean |
|
160 | + */ |
|
161 | + private static function _validateNumber($num): bool |
|
162 | + { |
|
163 | + if (is_int($num)) { |
|
164 | + return true; |
|
165 | + } |
|
166 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
167 | + return true; |
|
168 | + } |
|
169 | + return false; |
|
170 | + } |
|
171 | 171 | } |
@@ -122,7 +122,7 @@ |
||
122 | 122 | * @return self |
123 | 123 | */ |
124 | 124 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
125 | - int &$offset): ElementBase |
|
125 | + int & $offset): ElementBase |
|
126 | 126 | { |
127 | 127 | $idx = $offset; |
128 | 128 | $length = Length::expectFromDER($data, $idx); |