@@ -12,20 +12,20 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class ContextSpecificTaggedType extends TaggedType |
14 | 14 | { |
15 | - /** |
|
16 | - * Wrapped element. |
|
17 | - * |
|
18 | - * @var \ASN1\Element $_element |
|
19 | - */ |
|
20 | - protected $_element; |
|
15 | + /** |
|
16 | + * Wrapped element. |
|
17 | + * |
|
18 | + * @var \ASN1\Element $_element |
|
19 | + */ |
|
20 | + protected $_element; |
|
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * @see \ASN1\Element::typeClass() |
|
25 | - * @return int |
|
26 | - */ |
|
27 | - public function typeClass(): int |
|
28 | - { |
|
29 | - return Identifier::CLASS_CONTEXT_SPECIFIC; |
|
30 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * @see \ASN1\Element::typeClass() |
|
25 | + * @return int |
|
26 | + */ |
|
27 | + public function typeClass(): int |
|
28 | + { |
|
29 | + return Identifier::CLASS_CONTEXT_SPECIFIC; |
|
30 | + } |
|
31 | 31 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | interface ExplicitTagging extends ElementBase |
14 | 14 | { |
15 | - /** |
|
16 | - * Get explicitly tagged wrapped element. |
|
17 | - * |
|
18 | - * NOTE! Expectation checking is deprecated and shall be done |
|
19 | - * with UnspecifiedType. |
|
20 | - * |
|
21 | - * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * @return \ASN1\Type\UnspecifiedType |
|
24 | - */ |
|
25 | - public function explicit($expectedTag = null): UnspecifiedType; |
|
15 | + /** |
|
16 | + * Get explicitly tagged wrapped element. |
|
17 | + * |
|
18 | + * NOTE! Expectation checking is deprecated and shall be done |
|
19 | + * with UnspecifiedType. |
|
20 | + * |
|
21 | + * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * @return \ASN1\Type\UnspecifiedType |
|
24 | + */ |
|
25 | + public function explicit($expectedTag = null): UnspecifiedType; |
|
26 | 26 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | trait PrimitiveType |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * @see \ASN1\Element::isConstructed() |
|
15 | - * @return boolean |
|
16 | - */ |
|
17 | - public function isConstructed(): bool |
|
18 | - { |
|
19 | - return false; |
|
20 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * @see \ASN1\Element::isConstructed() |
|
15 | + * @return boolean |
|
16 | + */ |
|
17 | + public function isConstructed(): bool |
|
18 | + { |
|
19 | + return false; |
|
20 | + } |
|
21 | 21 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
28 | + const MAP_CLASS_TO_NAME = [/* @formatter:off */ |
|
29 | 29 | self::CLASS_UNIVERSAL => "UNIVERSAL", |
30 | 30 | self::CLASS_APPLICATION => "APPLICATION", |
31 | 31 | self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @throws DecodeException If decoding fails |
84 | 84 | * @return self |
85 | 85 | */ |
86 | - public static function fromDER(string $data, int &$offset = null): self |
|
86 | + public static function fromDER(string $data, int & $offset = null): self |
|
87 | 87 | { |
88 | 88 | $idx = $offset ? $offset : 0; |
89 | 89 | $datalen = strlen($data); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * @throws DecodeException If decoding fails |
117 | 117 | * @return string Tag number |
118 | 118 | */ |
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
119 | + private static function _decodeLongFormTag(string $data, int & $offset): string |
|
120 | 120 | { |
121 | 121 | $datalen = strlen($data); |
122 | 122 | $tag = gmp_init(0, 10); |
@@ -13,295 +13,295 @@ |
||
13 | 13 | */ |
14 | 14 | class Identifier implements Encodable |
15 | 15 | { |
16 | - // Type class enumerations |
|
17 | - const CLASS_UNIVERSAL = 0b00; |
|
18 | - const CLASS_APPLICATION = 0b01; |
|
19 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
20 | - const CLASS_PRIVATE = 0b11; |
|
16 | + // Type class enumerations |
|
17 | + const CLASS_UNIVERSAL = 0b00; |
|
18 | + const CLASS_APPLICATION = 0b01; |
|
19 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
20 | + const CLASS_PRIVATE = 0b11; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Mapping from type class to human readable name. |
|
24 | - * |
|
25 | - * @internal |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
30 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
31 | - self::CLASS_APPLICATION => "APPLICATION", |
|
32 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
33 | - self::CLASS_PRIVATE => "PRIVATE", |
|
34 | - /* @formatter:on */ |
|
35 | - ]; |
|
22 | + /** |
|
23 | + * Mapping from type class to human readable name. |
|
24 | + * |
|
25 | + * @internal |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
30 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
31 | + self::CLASS_APPLICATION => "APPLICATION", |
|
32 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
33 | + self::CLASS_PRIVATE => "PRIVATE", |
|
34 | + /* @formatter:on */ |
|
35 | + ]; |
|
36 | 36 | |
37 | - // P/C enumerations |
|
38 | - const PRIMITIVE = 0b0; |
|
39 | - const CONSTRUCTED = 0b1; |
|
37 | + // P/C enumerations |
|
38 | + const PRIMITIVE = 0b0; |
|
39 | + const CONSTRUCTED = 0b1; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Type class. |
|
43 | - * |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - private $_class; |
|
41 | + /** |
|
42 | + * Type class. |
|
43 | + * |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + private $_class; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Primitive or Constructed. |
|
50 | - * |
|
51 | - * @var int |
|
52 | - */ |
|
53 | - private $_pc; |
|
48 | + /** |
|
49 | + * Primitive or Constructed. |
|
50 | + * |
|
51 | + * @var int |
|
52 | + */ |
|
53 | + private $_pc; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Content type tag. |
|
57 | - * |
|
58 | - * @var BigInt |
|
59 | - */ |
|
60 | - private $_tag; |
|
55 | + /** |
|
56 | + * Content type tag. |
|
57 | + * |
|
58 | + * @var BigInt |
|
59 | + */ |
|
60 | + private $_tag; |
|
61 | 61 | |
62 | - /** |
|
63 | - * Constructor. |
|
64 | - * |
|
65 | - * @param int $class Type class |
|
66 | - * @param int $pc Primitive / Constructed |
|
67 | - * @param int|string $tag Type tag number |
|
68 | - */ |
|
69 | - public function __construct(int $class, int $pc, $tag) |
|
70 | - { |
|
71 | - $this->_class = 0b11 & $class; |
|
72 | - $this->_pc = 0b1 & $pc; |
|
73 | - $this->_tag = new BigInt($tag); |
|
74 | - } |
|
62 | + /** |
|
63 | + * Constructor. |
|
64 | + * |
|
65 | + * @param int $class Type class |
|
66 | + * @param int $pc Primitive / Constructed |
|
67 | + * @param int|string $tag Type tag number |
|
68 | + */ |
|
69 | + public function __construct(int $class, int $pc, $tag) |
|
70 | + { |
|
71 | + $this->_class = 0b11 & $class; |
|
72 | + $this->_pc = 0b1 & $pc; |
|
73 | + $this->_tag = new BigInt($tag); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Decode identifier component from DER data. |
|
78 | - * |
|
79 | - * @param string $data DER encoded data |
|
80 | - * @param int|null $offset Reference to the variable that contains offset |
|
81 | - * into the data where to start parsing. Variable is updated to |
|
82 | - * the offset next to the parsed identifier. If null, start from |
|
83 | - * offset 0. |
|
84 | - * @throws DecodeException If decoding fails |
|
85 | - * @return self |
|
86 | - */ |
|
87 | - public static function fromDER(string $data, int &$offset = null): self |
|
88 | - { |
|
89 | - $idx = $offset ? $offset : 0; |
|
90 | - $datalen = strlen($data); |
|
91 | - if ($idx >= $datalen) { |
|
92 | - throw new DecodeException("Invalid offset."); |
|
93 | - } |
|
94 | - $byte = ord($data[$idx++]); |
|
95 | - // bits 8 and 7 (class) |
|
96 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
97 | - $class = (0b11000000 & $byte) >> 6; |
|
98 | - // bit 6 (0 = primitive / 1 = constructed) |
|
99 | - $pc = (0b00100000 & $byte) >> 5; |
|
100 | - // bits 5 to 1 (tag number) |
|
101 | - $tag = (0b00011111 & $byte); |
|
102 | - // long-form identifier |
|
103 | - if (0x1f == $tag) { |
|
104 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
105 | - } |
|
106 | - if (isset($offset)) { |
|
107 | - $offset = $idx; |
|
108 | - } |
|
109 | - return new self($class, $pc, $tag); |
|
110 | - } |
|
76 | + /** |
|
77 | + * Decode identifier component from DER data. |
|
78 | + * |
|
79 | + * @param string $data DER encoded data |
|
80 | + * @param int|null $offset Reference to the variable that contains offset |
|
81 | + * into the data where to start parsing. Variable is updated to |
|
82 | + * the offset next to the parsed identifier. If null, start from |
|
83 | + * offset 0. |
|
84 | + * @throws DecodeException If decoding fails |
|
85 | + * @return self |
|
86 | + */ |
|
87 | + public static function fromDER(string $data, int &$offset = null): self |
|
88 | + { |
|
89 | + $idx = $offset ? $offset : 0; |
|
90 | + $datalen = strlen($data); |
|
91 | + if ($idx >= $datalen) { |
|
92 | + throw new DecodeException("Invalid offset."); |
|
93 | + } |
|
94 | + $byte = ord($data[$idx++]); |
|
95 | + // bits 8 and 7 (class) |
|
96 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
97 | + $class = (0b11000000 & $byte) >> 6; |
|
98 | + // bit 6 (0 = primitive / 1 = constructed) |
|
99 | + $pc = (0b00100000 & $byte) >> 5; |
|
100 | + // bits 5 to 1 (tag number) |
|
101 | + $tag = (0b00011111 & $byte); |
|
102 | + // long-form identifier |
|
103 | + if (0x1f == $tag) { |
|
104 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
105 | + } |
|
106 | + if (isset($offset)) { |
|
107 | + $offset = $idx; |
|
108 | + } |
|
109 | + return new self($class, $pc, $tag); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Parse long form tag. |
|
114 | - * |
|
115 | - * @param string $data DER data |
|
116 | - * @param int $offset Reference to the variable containing offset to data |
|
117 | - * @throws DecodeException If decoding fails |
|
118 | - * @return string Tag number |
|
119 | - */ |
|
120 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
121 | - { |
|
122 | - $datalen = strlen($data); |
|
123 | - $tag = gmp_init(0, 10); |
|
124 | - while (true) { |
|
125 | - if ($offset >= $datalen) { |
|
126 | - throw new DecodeException( |
|
127 | - "Unexpected end of data while decoding" . |
|
128 | - " long form identifier."); |
|
129 | - } |
|
130 | - $byte = ord($data[$offset++]); |
|
131 | - $tag <<= 7; |
|
132 | - $tag |= 0x7f & $byte; |
|
133 | - // last byte has bit 8 set to zero |
|
134 | - if (!(0x80 & $byte)) { |
|
135 | - break; |
|
136 | - } |
|
137 | - } |
|
138 | - return gmp_strval($tag, 10); |
|
139 | - } |
|
112 | + /** |
|
113 | + * Parse long form tag. |
|
114 | + * |
|
115 | + * @param string $data DER data |
|
116 | + * @param int $offset Reference to the variable containing offset to data |
|
117 | + * @throws DecodeException If decoding fails |
|
118 | + * @return string Tag number |
|
119 | + */ |
|
120 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
121 | + { |
|
122 | + $datalen = strlen($data); |
|
123 | + $tag = gmp_init(0, 10); |
|
124 | + while (true) { |
|
125 | + if ($offset >= $datalen) { |
|
126 | + throw new DecodeException( |
|
127 | + "Unexpected end of data while decoding" . |
|
128 | + " long form identifier."); |
|
129 | + } |
|
130 | + $byte = ord($data[$offset++]); |
|
131 | + $tag <<= 7; |
|
132 | + $tag |= 0x7f & $byte; |
|
133 | + // last byte has bit 8 set to zero |
|
134 | + if (!(0x80 & $byte)) { |
|
135 | + break; |
|
136 | + } |
|
137 | + } |
|
138 | + return gmp_strval($tag, 10); |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * |
|
143 | - * @see Encodable::toDER() |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - public function toDER(): string |
|
147 | - { |
|
148 | - $bytes = []; |
|
149 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
150 | - $tag = $this->_tag->gmpObj(); |
|
151 | - if ($tag < 0x1f) { |
|
152 | - $bytes[] = $byte | $tag; |
|
153 | - } else { // long-form identifier |
|
154 | - $bytes[] = $byte | 0x1f; |
|
155 | - $octets = []; |
|
156 | - for (; $tag > 0; $tag >>= 7) { |
|
157 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
158 | - } |
|
159 | - // last octet has bit 8 set to zero |
|
160 | - $octets[0] &= 0x7f; |
|
161 | - foreach (array_reverse($octets) as $octet) { |
|
162 | - $bytes[] = $octet; |
|
163 | - } |
|
164 | - } |
|
165 | - return pack("C*", ...$bytes); |
|
166 | - } |
|
141 | + /** |
|
142 | + * |
|
143 | + * @see Encodable::toDER() |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + public function toDER(): string |
|
147 | + { |
|
148 | + $bytes = []; |
|
149 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
150 | + $tag = $this->_tag->gmpObj(); |
|
151 | + if ($tag < 0x1f) { |
|
152 | + $bytes[] = $byte | $tag; |
|
153 | + } else { // long-form identifier |
|
154 | + $bytes[] = $byte | 0x1f; |
|
155 | + $octets = []; |
|
156 | + for (; $tag > 0; $tag >>= 7) { |
|
157 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
158 | + } |
|
159 | + // last octet has bit 8 set to zero |
|
160 | + $octets[0] &= 0x7f; |
|
161 | + foreach (array_reverse($octets) as $octet) { |
|
162 | + $bytes[] = $octet; |
|
163 | + } |
|
164 | + } |
|
165 | + return pack("C*", ...$bytes); |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Get class of the type. |
|
170 | - * |
|
171 | - * @return int |
|
172 | - */ |
|
173 | - public function typeClass(): int |
|
174 | - { |
|
175 | - return $this->_class; |
|
176 | - } |
|
168 | + /** |
|
169 | + * Get class of the type. |
|
170 | + * |
|
171 | + * @return int |
|
172 | + */ |
|
173 | + public function typeClass(): int |
|
174 | + { |
|
175 | + return $this->_class; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * Get P/C. |
|
180 | - * |
|
181 | - * @return int |
|
182 | - */ |
|
183 | - public function pc(): int |
|
184 | - { |
|
185 | - return $this->_pc; |
|
186 | - } |
|
178 | + /** |
|
179 | + * Get P/C. |
|
180 | + * |
|
181 | + * @return int |
|
182 | + */ |
|
183 | + public function pc(): int |
|
184 | + { |
|
185 | + return $this->_pc; |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Get the tag number. |
|
190 | - * |
|
191 | - * @return string Base 10 integer string |
|
192 | - */ |
|
193 | - public function tag(): string |
|
194 | - { |
|
195 | - return $this->_tag->base10(); |
|
196 | - } |
|
188 | + /** |
|
189 | + * Get the tag number. |
|
190 | + * |
|
191 | + * @return string Base 10 integer string |
|
192 | + */ |
|
193 | + public function tag(): string |
|
194 | + { |
|
195 | + return $this->_tag->base10(); |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * Get the tag as an integer. |
|
200 | - * |
|
201 | - * @return int |
|
202 | - */ |
|
203 | - public function intTag(): int |
|
204 | - { |
|
205 | - return $this->_tag->intVal(); |
|
206 | - } |
|
198 | + /** |
|
199 | + * Get the tag as an integer. |
|
200 | + * |
|
201 | + * @return int |
|
202 | + */ |
|
203 | + public function intTag(): int |
|
204 | + { |
|
205 | + return $this->_tag->intVal(); |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * Check whether type is of an universal class. |
|
210 | - * |
|
211 | - * @return boolean |
|
212 | - */ |
|
213 | - public function isUniversal(): bool |
|
214 | - { |
|
215 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
216 | - } |
|
208 | + /** |
|
209 | + * Check whether type is of an universal class. |
|
210 | + * |
|
211 | + * @return boolean |
|
212 | + */ |
|
213 | + public function isUniversal(): bool |
|
214 | + { |
|
215 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Check whether type is of an application class. |
|
220 | - * |
|
221 | - * @return boolean |
|
222 | - */ |
|
223 | - public function isApplication(): bool |
|
224 | - { |
|
225 | - return self::CLASS_APPLICATION == $this->_class; |
|
226 | - } |
|
218 | + /** |
|
219 | + * Check whether type is of an application class. |
|
220 | + * |
|
221 | + * @return boolean |
|
222 | + */ |
|
223 | + public function isApplication(): bool |
|
224 | + { |
|
225 | + return self::CLASS_APPLICATION == $this->_class; |
|
226 | + } |
|
227 | 227 | |
228 | - /** |
|
229 | - * Check whether type is of a context specific class. |
|
230 | - * |
|
231 | - * @return boolean |
|
232 | - */ |
|
233 | - public function isContextSpecific(): bool |
|
234 | - { |
|
235 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
236 | - } |
|
228 | + /** |
|
229 | + * Check whether type is of a context specific class. |
|
230 | + * |
|
231 | + * @return boolean |
|
232 | + */ |
|
233 | + public function isContextSpecific(): bool |
|
234 | + { |
|
235 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
236 | + } |
|
237 | 237 | |
238 | - /** |
|
239 | - * Check whether type is of a private class. |
|
240 | - * |
|
241 | - * @return boolean |
|
242 | - */ |
|
243 | - public function isPrivate(): bool |
|
244 | - { |
|
245 | - return self::CLASS_PRIVATE == $this->_class; |
|
246 | - } |
|
238 | + /** |
|
239 | + * Check whether type is of a private class. |
|
240 | + * |
|
241 | + * @return boolean |
|
242 | + */ |
|
243 | + public function isPrivate(): bool |
|
244 | + { |
|
245 | + return self::CLASS_PRIVATE == $this->_class; |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Check whether content is primitive type. |
|
250 | - * |
|
251 | - * @return boolean |
|
252 | - */ |
|
253 | - public function isPrimitive(): bool |
|
254 | - { |
|
255 | - return self::PRIMITIVE == $this->_pc; |
|
256 | - } |
|
248 | + /** |
|
249 | + * Check whether content is primitive type. |
|
250 | + * |
|
251 | + * @return boolean |
|
252 | + */ |
|
253 | + public function isPrimitive(): bool |
|
254 | + { |
|
255 | + return self::PRIMITIVE == $this->_pc; |
|
256 | + } |
|
257 | 257 | |
258 | - /** |
|
259 | - * Check hether content is constructed type. |
|
260 | - * |
|
261 | - * @return boolean |
|
262 | - */ |
|
263 | - public function isConstructed(): bool |
|
264 | - { |
|
265 | - return self::CONSTRUCTED == $this->_pc; |
|
266 | - } |
|
258 | + /** |
|
259 | + * Check hether content is constructed type. |
|
260 | + * |
|
261 | + * @return boolean |
|
262 | + */ |
|
263 | + public function isConstructed(): bool |
|
264 | + { |
|
265 | + return self::CONSTRUCTED == $this->_pc; |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Get self with given type class. |
|
270 | - * |
|
271 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
272 | - * @return self |
|
273 | - */ |
|
274 | - public function withClass(int $class): self |
|
275 | - { |
|
276 | - $obj = clone $this; |
|
277 | - $obj->_class = $class; |
|
278 | - return $obj; |
|
279 | - } |
|
268 | + /** |
|
269 | + * Get self with given type class. |
|
270 | + * |
|
271 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
272 | + * @return self |
|
273 | + */ |
|
274 | + public function withClass(int $class): self |
|
275 | + { |
|
276 | + $obj = clone $this; |
|
277 | + $obj->_class = $class; |
|
278 | + return $obj; |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * Get self with given type tag. |
|
283 | - * |
|
284 | - * @param int|string $tag Tag number |
|
285 | - * @return self |
|
286 | - */ |
|
287 | - public function withTag($tag): self |
|
288 | - { |
|
289 | - $obj = clone $this; |
|
290 | - $obj->_tag = new BigInt($tag); |
|
291 | - return $obj; |
|
292 | - } |
|
281 | + /** |
|
282 | + * Get self with given type tag. |
|
283 | + * |
|
284 | + * @param int|string $tag Tag number |
|
285 | + * @return self |
|
286 | + */ |
|
287 | + public function withTag($tag): self |
|
288 | + { |
|
289 | + $obj = clone $this; |
|
290 | + $obj->_tag = new BigInt($tag); |
|
291 | + return $obj; |
|
292 | + } |
|
293 | 293 | |
294 | - /** |
|
295 | - * Get human readable name of the type class. |
|
296 | - * |
|
297 | - * @param int $class |
|
298 | - * @return string |
|
299 | - */ |
|
300 | - public static function classToName(int $class): string |
|
301 | - { |
|
302 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
303 | - return "CLASS $class"; |
|
304 | - } |
|
305 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
306 | - } |
|
294 | + /** |
|
295 | + * Get human readable name of the type class. |
|
296 | + * |
|
297 | + * @param int $class |
|
298 | + * @return string |
|
299 | + */ |
|
300 | + public static function classToName(int $class): string |
|
301 | + { |
|
302 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
303 | + return "CLASS $class"; |
|
304 | + } |
|
305 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
306 | + } |
|
307 | 307 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @throws DecodeException If decoding fails |
50 | 50 | * @return self |
51 | 51 | */ |
52 | - public static function fromDER(string $data, int &$offset = null): self |
|
52 | + public static function fromDER(string $data, int & $offset = null): self |
|
53 | 53 | { |
54 | 54 | $idx = $offset ? $offset : 0; |
55 | 55 | $datalen = strlen($data); |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * @return string Integer as a string |
89 | 89 | */ |
90 | 90 | private static function _decodeLongFormLength(int $length, string $data, |
91 | - int &$offset): string |
|
91 | + int & $offset): string |
|
92 | 92 | { |
93 | 93 | // first octet must not be 0xff (spec 8.1.3.5c) |
94 | 94 | if ($length == 127) { |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * @throws DecodeException If decoding or expectation fails |
118 | 118 | * @return self |
119 | 119 | */ |
120 | - public static function expectFromDER(string $data, int &$offset, |
|
120 | + public static function expectFromDER(string $data, int & $offset, |
|
121 | 121 | int $expected = null): self |
122 | 122 | { |
123 | 123 | $idx = $offset; |
@@ -13,206 +13,206 @@ |
||
13 | 13 | */ |
14 | 14 | class Length implements Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Length. |
|
18 | - * |
|
19 | - * @var BigInt |
|
20 | - */ |
|
21 | - private $_length; |
|
16 | + /** |
|
17 | + * Length. |
|
18 | + * |
|
19 | + * @var BigInt |
|
20 | + */ |
|
21 | + private $_length; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Whether length is indefinite. |
|
25 | - * |
|
26 | - * @var boolean |
|
27 | - */ |
|
28 | - private $_indefinite; |
|
23 | + /** |
|
24 | + * Whether length is indefinite. |
|
25 | + * |
|
26 | + * @var boolean |
|
27 | + */ |
|
28 | + private $_indefinite; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $length Length |
|
34 | - * @param boolean $indefinite Whether length is indefinite |
|
35 | - */ |
|
36 | - public function __construct($length, bool $indefinite = false) |
|
37 | - { |
|
38 | - $this->_length = new BigInt($length); |
|
39 | - $this->_indefinite = $indefinite; |
|
40 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $length Length |
|
34 | + * @param boolean $indefinite Whether length is indefinite |
|
35 | + */ |
|
36 | + public function __construct($length, bool $indefinite = false) |
|
37 | + { |
|
38 | + $this->_length = new BigInt($length); |
|
39 | + $this->_indefinite = $indefinite; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Decode length component from DER data. |
|
44 | - * |
|
45 | - * @param string $data DER encoded data |
|
46 | - * @param int|null $offset Reference to the variable that contains offset |
|
47 | - * into the data where to start parsing. Variable is updated to |
|
48 | - * the offset next to the parsed length component. If null, start |
|
49 | - * from offset 0. |
|
50 | - * @throws DecodeException If decoding fails |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromDER(string $data, int &$offset = null): self |
|
54 | - { |
|
55 | - $idx = $offset ? $offset : 0; |
|
56 | - $datalen = strlen($data); |
|
57 | - if ($idx >= $datalen) { |
|
58 | - throw new DecodeException("Invalid offset."); |
|
59 | - } |
|
60 | - $indefinite = false; |
|
61 | - $byte = ord($data[$idx++]); |
|
62 | - // bits 7 to 1 |
|
63 | - $length = (0x7f & $byte); |
|
64 | - // long form |
|
65 | - if (0x80 & $byte) { |
|
66 | - if (!$length) { |
|
67 | - $indefinite = true; |
|
68 | - } else { |
|
69 | - if ($idx + $length > $datalen) { |
|
70 | - throw new DecodeException("Too many length octets."); |
|
71 | - } |
|
72 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
73 | - } |
|
74 | - } |
|
75 | - if (isset($offset)) { |
|
76 | - $offset = $idx; |
|
77 | - } |
|
78 | - return new self($length, $indefinite); |
|
79 | - } |
|
42 | + /** |
|
43 | + * Decode length component from DER data. |
|
44 | + * |
|
45 | + * @param string $data DER encoded data |
|
46 | + * @param int|null $offset Reference to the variable that contains offset |
|
47 | + * into the data where to start parsing. Variable is updated to |
|
48 | + * the offset next to the parsed length component. If null, start |
|
49 | + * from offset 0. |
|
50 | + * @throws DecodeException If decoding fails |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromDER(string $data, int &$offset = null): self |
|
54 | + { |
|
55 | + $idx = $offset ? $offset : 0; |
|
56 | + $datalen = strlen($data); |
|
57 | + if ($idx >= $datalen) { |
|
58 | + throw new DecodeException("Invalid offset."); |
|
59 | + } |
|
60 | + $indefinite = false; |
|
61 | + $byte = ord($data[$idx++]); |
|
62 | + // bits 7 to 1 |
|
63 | + $length = (0x7f & $byte); |
|
64 | + // long form |
|
65 | + if (0x80 & $byte) { |
|
66 | + if (!$length) { |
|
67 | + $indefinite = true; |
|
68 | + } else { |
|
69 | + if ($idx + $length > $datalen) { |
|
70 | + throw new DecodeException("Too many length octets."); |
|
71 | + } |
|
72 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
73 | + } |
|
74 | + } |
|
75 | + if (isset($offset)) { |
|
76 | + $offset = $idx; |
|
77 | + } |
|
78 | + return new self($length, $indefinite); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Decode long form length. |
|
83 | - * |
|
84 | - * @param int $length Number of octets |
|
85 | - * @param string $data Data |
|
86 | - * @param int $offset Reference to the variable containing offset to the |
|
87 | - * data. |
|
88 | - * @throws DecodeException If decoding fails |
|
89 | - * @return string Integer as a string |
|
90 | - */ |
|
91 | - private static function _decodeLongFormLength(int $length, string $data, |
|
92 | - int &$offset): string |
|
93 | - { |
|
94 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
95 | - if ($length == 127) { |
|
96 | - throw new DecodeException("Invalid number of length octets."); |
|
97 | - } |
|
98 | - $num = gmp_init(0, 10); |
|
99 | - while (--$length >= 0) { |
|
100 | - $byte = ord($data[$offset++]); |
|
101 | - $num <<= 8; |
|
102 | - $num |= $byte; |
|
103 | - } |
|
104 | - return gmp_strval($num); |
|
105 | - } |
|
81 | + /** |
|
82 | + * Decode long form length. |
|
83 | + * |
|
84 | + * @param int $length Number of octets |
|
85 | + * @param string $data Data |
|
86 | + * @param int $offset Reference to the variable containing offset to the |
|
87 | + * data. |
|
88 | + * @throws DecodeException If decoding fails |
|
89 | + * @return string Integer as a string |
|
90 | + */ |
|
91 | + private static function _decodeLongFormLength(int $length, string $data, |
|
92 | + int &$offset): string |
|
93 | + { |
|
94 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
95 | + if ($length == 127) { |
|
96 | + throw new DecodeException("Invalid number of length octets."); |
|
97 | + } |
|
98 | + $num = gmp_init(0, 10); |
|
99 | + while (--$length >= 0) { |
|
100 | + $byte = ord($data[$offset++]); |
|
101 | + $num <<= 8; |
|
102 | + $num |= $byte; |
|
103 | + } |
|
104 | + return gmp_strval($num); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Decode length from DER. |
|
109 | - * |
|
110 | - * Throws an exception if length doesn't match with expected or if data |
|
111 | - * doesn't contain enough bytes. |
|
112 | - * |
|
113 | - * @see self::fromDER |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the offset variable |
|
116 | - * @param int|null $expected Expected length, null to bypass checking |
|
117 | - * @throws DecodeException If decoding or expectation fails |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public static function expectFromDER(string $data, int &$offset, |
|
121 | - int $expected = null): self |
|
122 | - { |
|
123 | - $idx = $offset; |
|
124 | - $length = self::fromDER($data, $idx); |
|
125 | - // DER encoding must have definite length (spec 10.1) |
|
126 | - if ($length->isIndefinite()) { |
|
127 | - throw new DecodeException("DER encoding must have definite length."); |
|
128 | - } |
|
129 | - // if certain length was expected |
|
130 | - if (isset($expected) && $expected != $length->intLength()) { |
|
131 | - throw new DecodeException( |
|
132 | - sprintf("Expected length %d, got %d.", $expected, |
|
133 | - $length->intLength())); |
|
134 | - } |
|
135 | - // check that enough data is available |
|
136 | - if (strlen($data) < $idx + $length->intLength()) { |
|
137 | - throw new DecodeException( |
|
138 | - sprintf("Length %d overflows data, %d bytes left.", |
|
139 | - $length->intLength(), strlen($data) - $idx)); |
|
140 | - } |
|
141 | - $offset = $idx; |
|
142 | - return $length; |
|
143 | - } |
|
107 | + /** |
|
108 | + * Decode length from DER. |
|
109 | + * |
|
110 | + * Throws an exception if length doesn't match with expected or if data |
|
111 | + * doesn't contain enough bytes. |
|
112 | + * |
|
113 | + * @see self::fromDER |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the offset variable |
|
116 | + * @param int|null $expected Expected length, null to bypass checking |
|
117 | + * @throws DecodeException If decoding or expectation fails |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public static function expectFromDER(string $data, int &$offset, |
|
121 | + int $expected = null): self |
|
122 | + { |
|
123 | + $idx = $offset; |
|
124 | + $length = self::fromDER($data, $idx); |
|
125 | + // DER encoding must have definite length (spec 10.1) |
|
126 | + if ($length->isIndefinite()) { |
|
127 | + throw new DecodeException("DER encoding must have definite length."); |
|
128 | + } |
|
129 | + // if certain length was expected |
|
130 | + if (isset($expected) && $expected != $length->intLength()) { |
|
131 | + throw new DecodeException( |
|
132 | + sprintf("Expected length %d, got %d.", $expected, |
|
133 | + $length->intLength())); |
|
134 | + } |
|
135 | + // check that enough data is available |
|
136 | + if (strlen($data) < $idx + $length->intLength()) { |
|
137 | + throw new DecodeException( |
|
138 | + sprintf("Length %d overflows data, %d bytes left.", |
|
139 | + $length->intLength(), strlen($data) - $idx)); |
|
140 | + } |
|
141 | + $offset = $idx; |
|
142 | + return $length; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * |
|
147 | - * @see Encodable::toDER() |
|
148 | - * @throws \DomainException If length is too large to encode |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function toDER(): string |
|
152 | - { |
|
153 | - $bytes = []; |
|
154 | - if ($this->_indefinite) { |
|
155 | - $bytes[] = 0x80; |
|
156 | - } else { |
|
157 | - $num = $this->_length->gmpObj(); |
|
158 | - // long form |
|
159 | - if ($num > 127) { |
|
160 | - $octets = []; |
|
161 | - for (; $num > 0; $num >>= 8) { |
|
162 | - $octets[] = gmp_intval(0xff & $num); |
|
163 | - } |
|
164 | - $count = count($octets); |
|
165 | - // first octet must not be 0xff |
|
166 | - if ($count >= 127) { |
|
167 | - throw new \DomainException("Too many length octets."); |
|
168 | - } |
|
169 | - $bytes[] = 0x80 | $count; |
|
170 | - foreach (array_reverse($octets) as $octet) { |
|
171 | - $bytes[] = $octet; |
|
172 | - } |
|
173 | - } else { // short form |
|
174 | - $bytes[] = gmp_intval($num); |
|
175 | - } |
|
176 | - } |
|
177 | - return pack("C*", ...$bytes); |
|
178 | - } |
|
145 | + /** |
|
146 | + * |
|
147 | + * @see Encodable::toDER() |
|
148 | + * @throws \DomainException If length is too large to encode |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function toDER(): string |
|
152 | + { |
|
153 | + $bytes = []; |
|
154 | + if ($this->_indefinite) { |
|
155 | + $bytes[] = 0x80; |
|
156 | + } else { |
|
157 | + $num = $this->_length->gmpObj(); |
|
158 | + // long form |
|
159 | + if ($num > 127) { |
|
160 | + $octets = []; |
|
161 | + for (; $num > 0; $num >>= 8) { |
|
162 | + $octets[] = gmp_intval(0xff & $num); |
|
163 | + } |
|
164 | + $count = count($octets); |
|
165 | + // first octet must not be 0xff |
|
166 | + if ($count >= 127) { |
|
167 | + throw new \DomainException("Too many length octets."); |
|
168 | + } |
|
169 | + $bytes[] = 0x80 | $count; |
|
170 | + foreach (array_reverse($octets) as $octet) { |
|
171 | + $bytes[] = $octet; |
|
172 | + } |
|
173 | + } else { // short form |
|
174 | + $bytes[] = gmp_intval($num); |
|
175 | + } |
|
176 | + } |
|
177 | + return pack("C*", ...$bytes); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Get the length. |
|
182 | - * |
|
183 | - * @throws \LogicException If length is indefinite |
|
184 | - * @return string Length as an integer string |
|
185 | - */ |
|
186 | - public function length(): string |
|
187 | - { |
|
188 | - if ($this->_indefinite) { |
|
189 | - throw new \LogicException("Length is indefinite."); |
|
190 | - } |
|
191 | - return $this->_length->base10(); |
|
192 | - } |
|
180 | + /** |
|
181 | + * Get the length. |
|
182 | + * |
|
183 | + * @throws \LogicException If length is indefinite |
|
184 | + * @return string Length as an integer string |
|
185 | + */ |
|
186 | + public function length(): string |
|
187 | + { |
|
188 | + if ($this->_indefinite) { |
|
189 | + throw new \LogicException("Length is indefinite."); |
|
190 | + } |
|
191 | + return $this->_length->base10(); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Get the length as an integer. |
|
196 | - * |
|
197 | - * @throws \LogicException If length is indefinite |
|
198 | - * @throws \RuntimeException If length overflows integer size |
|
199 | - * @return int |
|
200 | - */ |
|
201 | - public function intLength(): int |
|
202 | - { |
|
203 | - if ($this->_indefinite) { |
|
204 | - throw new \LogicException("Length is indefinite."); |
|
205 | - } |
|
206 | - return $this->_length->intVal(); |
|
207 | - } |
|
194 | + /** |
|
195 | + * Get the length as an integer. |
|
196 | + * |
|
197 | + * @throws \LogicException If length is indefinite |
|
198 | + * @throws \RuntimeException If length overflows integer size |
|
199 | + * @return int |
|
200 | + */ |
|
201 | + public function intLength(): int |
|
202 | + { |
|
203 | + if ($this->_indefinite) { |
|
204 | + throw new \LogicException("Length is indefinite."); |
|
205 | + } |
|
206 | + return $this->_length->intVal(); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Whether length is indefinite. |
|
211 | - * |
|
212 | - * @return boolean |
|
213 | - */ |
|
214 | - public function isIndefinite(): bool |
|
215 | - { |
|
216 | - return $this->_indefinite; |
|
217 | - } |
|
209 | + /** |
|
210 | + * Whether length is indefinite. |
|
211 | + * |
|
212 | + * @return boolean |
|
213 | + */ |
|
214 | + public function isIndefinite(): bool |
|
215 | + { |
|
216 | + return $this->_indefinite; |
|
217 | + } |
|
218 | 218 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * {@inheritdoc} |
23 | 23 | */ |
24 | 24 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
25 | - int &$offset): ElementBase |
|
25 | + int & $offset): ElementBase |
|
26 | 26 | { |
27 | 27 | $idx = $offset; |
28 | 28 | $type = new DERTaggedType($identifier, $data, $idx); |
@@ -17,85 +17,85 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class TaggedType extends Element |
19 | 19 | { |
20 | - /** |
|
21 | - * |
|
22 | - * {@inheritdoc} |
|
23 | - */ |
|
24 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | - int &$offset): ElementBase |
|
26 | - { |
|
27 | - $idx = $offset; |
|
28 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
30 | - $offset = $idx + $length; |
|
31 | - return $type; |
|
32 | - } |
|
20 | + /** |
|
21 | + * |
|
22 | + * {@inheritdoc} |
|
23 | + */ |
|
24 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | + int &$offset): ElementBase |
|
26 | + { |
|
27 | + $idx = $offset; |
|
28 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
30 | + $offset = $idx + $length; |
|
31 | + return $type; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Check whether element supports explicit tagging. |
|
36 | - * |
|
37 | - * @param int|null $expectedTag Optional outer tag expectation |
|
38 | - * @throws \UnexpectedValueException If expectation fails |
|
39 | - * @return ExplicitTagging |
|
40 | - */ |
|
41 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
42 | - { |
|
43 | - $el = $this; |
|
44 | - if (!$el instanceof ExplicitTagging) { |
|
45 | - throw new \UnexpectedValueException( |
|
46 | - "Element doesn't implement explicit tagging."); |
|
47 | - } |
|
48 | - if (isset($expectedTag)) { |
|
49 | - $el->expectTagged($expectedTag); |
|
50 | - } |
|
51 | - return $el; |
|
52 | - } |
|
34 | + /** |
|
35 | + * Check whether element supports explicit tagging. |
|
36 | + * |
|
37 | + * @param int|null $expectedTag Optional outer tag expectation |
|
38 | + * @throws \UnexpectedValueException If expectation fails |
|
39 | + * @return ExplicitTagging |
|
40 | + */ |
|
41 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
42 | + { |
|
43 | + $el = $this; |
|
44 | + if (!$el instanceof ExplicitTagging) { |
|
45 | + throw new \UnexpectedValueException( |
|
46 | + "Element doesn't implement explicit tagging."); |
|
47 | + } |
|
48 | + if (isset($expectedTag)) { |
|
49 | + $el->expectTagged($expectedTag); |
|
50 | + } |
|
51 | + return $el; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get the wrapped inner element employing explicit tagging. |
|
56 | - * |
|
57 | - * @param int|null $expectedTag Optional outer tag expectation |
|
58 | - * @throws \UnexpectedValueException If expectation fails |
|
59 | - * @return UnspecifiedType |
|
60 | - */ |
|
61 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | - { |
|
63 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | - } |
|
54 | + /** |
|
55 | + * Get the wrapped inner element employing explicit tagging. |
|
56 | + * |
|
57 | + * @param int|null $expectedTag Optional outer tag expectation |
|
58 | + * @throws \UnexpectedValueException If expectation fails |
|
59 | + * @return UnspecifiedType |
|
60 | + */ |
|
61 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | + { |
|
63 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Check whether element supports implicit tagging. |
|
68 | - * |
|
69 | - * @param int|null $expectedTag Optional outer tag expectation |
|
70 | - * @throws \UnexpectedValueException If expectation fails |
|
71 | - * @return ImplicitTagging |
|
72 | - */ |
|
73 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | - { |
|
75 | - $el = $this; |
|
76 | - if (!$el instanceof ImplicitTagging) { |
|
77 | - throw new \UnexpectedValueException( |
|
78 | - "Element doesn't implement implicit tagging."); |
|
79 | - } |
|
80 | - if (isset($expectedTag)) { |
|
81 | - $el->expectTagged($expectedTag); |
|
82 | - } |
|
83 | - return $el; |
|
84 | - } |
|
66 | + /** |
|
67 | + * Check whether element supports implicit tagging. |
|
68 | + * |
|
69 | + * @param int|null $expectedTag Optional outer tag expectation |
|
70 | + * @throws \UnexpectedValueException If expectation fails |
|
71 | + * @return ImplicitTagging |
|
72 | + */ |
|
73 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | + { |
|
75 | + $el = $this; |
|
76 | + if (!$el instanceof ImplicitTagging) { |
|
77 | + throw new \UnexpectedValueException( |
|
78 | + "Element doesn't implement implicit tagging."); |
|
79 | + } |
|
80 | + if (isset($expectedTag)) { |
|
81 | + $el->expectTagged($expectedTag); |
|
82 | + } |
|
83 | + return $el; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get the wrapped inner element employing implicit tagging. |
|
88 | - * |
|
89 | - * @param int $tag Type tag of the inner element |
|
90 | - * @param int|null $expectedTag Optional outer tag expectation |
|
91 | - * @param int $expectedClass Optional inner type class expectation |
|
92 | - * @throws \UnexpectedValueException If expectation fails |
|
93 | - * @return UnspecifiedType |
|
94 | - */ |
|
95 | - public function asImplicit(int $tag, $expectedTag = null, |
|
96 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | - { |
|
98 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | - $expectedClass); |
|
100 | - } |
|
86 | + /** |
|
87 | + * Get the wrapped inner element employing implicit tagging. |
|
88 | + * |
|
89 | + * @param int $tag Type tag of the inner element |
|
90 | + * @param int|null $expectedTag Optional outer tag expectation |
|
91 | + * @param int $expectedClass Optional inner type class expectation |
|
92 | + * @throws \UnexpectedValueException If expectation fails |
|
93 | + * @return UnspecifiedType |
|
94 | + */ |
|
95 | + public function asImplicit(int $tag, $expectedTag = null, |
|
96 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | + { |
|
98 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | + $expectedClass); |
|
100 | + } |
|
101 | 101 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @return self |
92 | 92 | */ |
93 | 93 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
94 | - int &$offset): ElementBase |
|
94 | + int & $offset): ElementBase |
|
95 | 95 | { |
96 | 96 | $idx = $offset; |
97 | 97 | if (!$identifier->isConstructed()) { |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | { |
237 | 237 | if (!isset($this->_unspecifiedTypes)) { |
238 | 238 | $this->_unspecifiedTypes = array_map( |
239 | - function (Element $el) { |
|
239 | + function(Element $el) { |
|
240 | 240 | return new UnspecifiedType($el); |
241 | 241 | }, $this->_elements); |
242 | 242 | } |
@@ -14,336 +14,336 @@ |
||
14 | 14 | * Base class for the constructed types. |
15 | 15 | */ |
16 | 16 | abstract class Structure extends Element implements |
17 | - \Countable, |
|
18 | - \IteratorAggregate |
|
17 | + \Countable, |
|
18 | + \IteratorAggregate |
|
19 | 19 | { |
20 | - use UniversalClass; |
|
20 | + use UniversalClass; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Array of elements in the structure. |
|
24 | - * |
|
25 | - * @var Element[] $_elements |
|
26 | - */ |
|
27 | - protected $_elements; |
|
22 | + /** |
|
23 | + * Array of elements in the structure. |
|
24 | + * |
|
25 | + * @var Element[] $_elements |
|
26 | + */ |
|
27 | + protected $_elements; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Lookup table for the tagged elements. |
|
31 | - * |
|
32 | - * @var TaggedType[]|null $_taggedMap |
|
33 | - */ |
|
34 | - private $_taggedMap; |
|
29 | + /** |
|
30 | + * Lookup table for the tagged elements. |
|
31 | + * |
|
32 | + * @var TaggedType[]|null $_taggedMap |
|
33 | + */ |
|
34 | + private $_taggedMap; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
38 | - * |
|
39 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
40 | - */ |
|
41 | - private $_unspecifiedTypes; |
|
36 | + /** |
|
37 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
38 | + * |
|
39 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
40 | + */ |
|
41 | + private $_unspecifiedTypes; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param Element ...$elements Any number of elements |
|
47 | - */ |
|
48 | - public function __construct(Element ...$elements) |
|
49 | - { |
|
50 | - $this->_elements = $elements; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param Element ...$elements Any number of elements |
|
47 | + */ |
|
48 | + public function __construct(Element ...$elements) |
|
49 | + { |
|
50 | + $this->_elements = $elements; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Clone magic method. |
|
55 | - */ |
|
56 | - public function __clone() |
|
57 | - { |
|
58 | - // clear cache-variables |
|
59 | - $this->_taggedMap = null; |
|
60 | - $this->_unspecifiedTypes = null; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Clone magic method. |
|
55 | + */ |
|
56 | + public function __clone() |
|
57 | + { |
|
58 | + // clear cache-variables |
|
59 | + $this->_taggedMap = null; |
|
60 | + $this->_unspecifiedTypes = null; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * |
|
65 | - * @see \ASN1\Element::isConstructed() |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isConstructed(): bool |
|
69 | - { |
|
70 | - return true; |
|
71 | - } |
|
63 | + /** |
|
64 | + * |
|
65 | + * @see \ASN1\Element::isConstructed() |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isConstructed(): bool |
|
69 | + { |
|
70 | + return true; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * |
|
75 | - * @see \ASN1\Element::_encodedContentDER() |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - protected function _encodedContentDER(): string |
|
79 | - { |
|
80 | - $data = ""; |
|
81 | - foreach ($this->_elements as $element) { |
|
82 | - $data .= $element->toDER(); |
|
83 | - } |
|
84 | - return $data; |
|
85 | - } |
|
73 | + /** |
|
74 | + * |
|
75 | + * @see \ASN1\Element::_encodedContentDER() |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + protected function _encodedContentDER(): string |
|
79 | + { |
|
80 | + $data = ""; |
|
81 | + foreach ($this->_elements as $element) { |
|
82 | + $data .= $element->toDER(); |
|
83 | + } |
|
84 | + return $data; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * |
|
89 | - * {@inheritdoc} |
|
90 | - * @see \ASN1\Element::_decodeFromDER() |
|
91 | - * @return self |
|
92 | - */ |
|
93 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
94 | - int &$offset): ElementBase |
|
95 | - { |
|
96 | - $idx = $offset; |
|
97 | - if (!$identifier->isConstructed()) { |
|
98 | - throw new DecodeException( |
|
99 | - "Structured element must have constructed bit set."); |
|
100 | - } |
|
101 | - $length = Length::expectFromDER($data, $idx); |
|
102 | - $end = $idx + $length->intLength(); |
|
103 | - $elements = []; |
|
104 | - while ($idx < $end) { |
|
105 | - $elements[] = Element::fromDER($data, $idx); |
|
106 | - // check that element didn't overflow length |
|
107 | - if ($idx > $end) { |
|
108 | - throw new DecodeException( |
|
109 | - "Structure's content overflows length."); |
|
110 | - } |
|
111 | - } |
|
112 | - $offset = $idx; |
|
113 | - // return instance by static late binding |
|
114 | - return new static(...$elements); |
|
115 | - } |
|
87 | + /** |
|
88 | + * |
|
89 | + * {@inheritdoc} |
|
90 | + * @see \ASN1\Element::_decodeFromDER() |
|
91 | + * @return self |
|
92 | + */ |
|
93 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
94 | + int &$offset): ElementBase |
|
95 | + { |
|
96 | + $idx = $offset; |
|
97 | + if (!$identifier->isConstructed()) { |
|
98 | + throw new DecodeException( |
|
99 | + "Structured element must have constructed bit set."); |
|
100 | + } |
|
101 | + $length = Length::expectFromDER($data, $idx); |
|
102 | + $end = $idx + $length->intLength(); |
|
103 | + $elements = []; |
|
104 | + while ($idx < $end) { |
|
105 | + $elements[] = Element::fromDER($data, $idx); |
|
106 | + // check that element didn't overflow length |
|
107 | + if ($idx > $end) { |
|
108 | + throw new DecodeException( |
|
109 | + "Structure's content overflows length."); |
|
110 | + } |
|
111 | + } |
|
112 | + $offset = $idx; |
|
113 | + // return instance by static late binding |
|
114 | + return new static(...$elements); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Explode DER structure to DER encoded components that it contains. |
|
119 | - * |
|
120 | - * @param string $data |
|
121 | - * @throws DecodeException |
|
122 | - * @return string[] |
|
123 | - */ |
|
124 | - public static function explodeDER(string $data): array |
|
125 | - { |
|
126 | - $offset = 0; |
|
127 | - $identifier = Identifier::fromDER($data, $offset); |
|
128 | - if (!$identifier->isConstructed()) { |
|
129 | - throw new DecodeException("Element is not constructed."); |
|
130 | - } |
|
131 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
132 | - $end = $offset + $length; |
|
133 | - $parts = []; |
|
134 | - while ($offset < $end) { |
|
135 | - // start of the element |
|
136 | - $idx = $offset; |
|
137 | - // skip identifier |
|
138 | - Identifier::fromDER($data, $offset); |
|
139 | - // decode element length |
|
140 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
141 | - // extract der encoding of the element |
|
142 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
143 | - // update offset over content |
|
144 | - $offset += $length; |
|
145 | - } |
|
146 | - return $parts; |
|
147 | - } |
|
117 | + /** |
|
118 | + * Explode DER structure to DER encoded components that it contains. |
|
119 | + * |
|
120 | + * @param string $data |
|
121 | + * @throws DecodeException |
|
122 | + * @return string[] |
|
123 | + */ |
|
124 | + public static function explodeDER(string $data): array |
|
125 | + { |
|
126 | + $offset = 0; |
|
127 | + $identifier = Identifier::fromDER($data, $offset); |
|
128 | + if (!$identifier->isConstructed()) { |
|
129 | + throw new DecodeException("Element is not constructed."); |
|
130 | + } |
|
131 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
132 | + $end = $offset + $length; |
|
133 | + $parts = []; |
|
134 | + while ($offset < $end) { |
|
135 | + // start of the element |
|
136 | + $idx = $offset; |
|
137 | + // skip identifier |
|
138 | + Identifier::fromDER($data, $offset); |
|
139 | + // decode element length |
|
140 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
141 | + // extract der encoding of the element |
|
142 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
143 | + // update offset over content |
|
144 | + $offset += $length; |
|
145 | + } |
|
146 | + return $parts; |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Get self with an element at the given index replaced by another. |
|
151 | - * |
|
152 | - * @param int $idx Element index |
|
153 | - * @param Element $el New element to insert into the structure |
|
154 | - * @throws \OutOfBoundsException |
|
155 | - * @return self |
|
156 | - */ |
|
157 | - public function withReplaced(int $idx, Element $el): self |
|
158 | - { |
|
159 | - if (!isset($this->_elements[$idx])) { |
|
160 | - throw new \OutOfBoundsException( |
|
161 | - "Structure doesn't have element at index $idx."); |
|
162 | - } |
|
163 | - $obj = clone $this; |
|
164 | - $obj->_elements[$idx] = $el; |
|
165 | - return $obj; |
|
166 | - } |
|
149 | + /** |
|
150 | + * Get self with an element at the given index replaced by another. |
|
151 | + * |
|
152 | + * @param int $idx Element index |
|
153 | + * @param Element $el New element to insert into the structure |
|
154 | + * @throws \OutOfBoundsException |
|
155 | + * @return self |
|
156 | + */ |
|
157 | + public function withReplaced(int $idx, Element $el): self |
|
158 | + { |
|
159 | + if (!isset($this->_elements[$idx])) { |
|
160 | + throw new \OutOfBoundsException( |
|
161 | + "Structure doesn't have element at index $idx."); |
|
162 | + } |
|
163 | + $obj = clone $this; |
|
164 | + $obj->_elements[$idx] = $el; |
|
165 | + return $obj; |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Get self with an element inserted before the given index. |
|
170 | - * |
|
171 | - * @param int $idx Element index |
|
172 | - * @param Element $el New element to insert into the structure |
|
173 | - * @throws \OutOfBoundsException |
|
174 | - * @return self |
|
175 | - */ |
|
176 | - public function withInserted(int $idx, Element $el): self |
|
177 | - { |
|
178 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
179 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
180 | - } |
|
181 | - $obj = clone $this; |
|
182 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
183 | - return $obj; |
|
184 | - } |
|
168 | + /** |
|
169 | + * Get self with an element inserted before the given index. |
|
170 | + * |
|
171 | + * @param int $idx Element index |
|
172 | + * @param Element $el New element to insert into the structure |
|
173 | + * @throws \OutOfBoundsException |
|
174 | + * @return self |
|
175 | + */ |
|
176 | + public function withInserted(int $idx, Element $el): self |
|
177 | + { |
|
178 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
179 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
180 | + } |
|
181 | + $obj = clone $this; |
|
182 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
183 | + return $obj; |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Get self with an element appended to the end. |
|
188 | - * |
|
189 | - * @param Element $el Element to insert into the structure |
|
190 | - * @return self |
|
191 | - */ |
|
192 | - public function withAppended(Element $el): self |
|
193 | - { |
|
194 | - $obj = clone $this; |
|
195 | - array_push($obj->_elements, $el); |
|
196 | - return $obj; |
|
197 | - } |
|
186 | + /** |
|
187 | + * Get self with an element appended to the end. |
|
188 | + * |
|
189 | + * @param Element $el Element to insert into the structure |
|
190 | + * @return self |
|
191 | + */ |
|
192 | + public function withAppended(Element $el): self |
|
193 | + { |
|
194 | + $obj = clone $this; |
|
195 | + array_push($obj->_elements, $el); |
|
196 | + return $obj; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get self with an element prepended in the beginning. |
|
201 | - * |
|
202 | - * @param Element $el Element to insert into the structure |
|
203 | - * @return self |
|
204 | - */ |
|
205 | - public function withPrepended(Element $el): self |
|
206 | - { |
|
207 | - $obj = clone $this; |
|
208 | - array_unshift($obj->_elements, $el); |
|
209 | - return $obj; |
|
210 | - } |
|
199 | + /** |
|
200 | + * Get self with an element prepended in the beginning. |
|
201 | + * |
|
202 | + * @param Element $el Element to insert into the structure |
|
203 | + * @return self |
|
204 | + */ |
|
205 | + public function withPrepended(Element $el): self |
|
206 | + { |
|
207 | + $obj = clone $this; |
|
208 | + array_unshift($obj->_elements, $el); |
|
209 | + return $obj; |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * Get self with an element at the given index removed. |
|
214 | - * |
|
215 | - * @param int $idx Element index |
|
216 | - * @throws \OutOfBoundsException |
|
217 | - * @return self |
|
218 | - */ |
|
219 | - public function withoutElement(int $idx): self |
|
220 | - { |
|
221 | - if (!isset($this->_elements[$idx])) { |
|
222 | - throw new \OutOfBoundsException( |
|
223 | - "Structure doesn't have element at index $idx."); |
|
224 | - } |
|
225 | - $obj = clone $this; |
|
226 | - array_splice($obj->_elements, $idx, 1); |
|
227 | - return $obj; |
|
228 | - } |
|
212 | + /** |
|
213 | + * Get self with an element at the given index removed. |
|
214 | + * |
|
215 | + * @param int $idx Element index |
|
216 | + * @throws \OutOfBoundsException |
|
217 | + * @return self |
|
218 | + */ |
|
219 | + public function withoutElement(int $idx): self |
|
220 | + { |
|
221 | + if (!isset($this->_elements[$idx])) { |
|
222 | + throw new \OutOfBoundsException( |
|
223 | + "Structure doesn't have element at index $idx."); |
|
224 | + } |
|
225 | + $obj = clone $this; |
|
226 | + array_splice($obj->_elements, $idx, 1); |
|
227 | + return $obj; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Get elements in the structure. |
|
232 | - * |
|
233 | - * @return UnspecifiedType[] |
|
234 | - */ |
|
235 | - public function elements(): array |
|
236 | - { |
|
237 | - if (!isset($this->_unspecifiedTypes)) { |
|
238 | - $this->_unspecifiedTypes = array_map( |
|
239 | - function (Element $el) { |
|
240 | - return new UnspecifiedType($el); |
|
241 | - }, $this->_elements); |
|
242 | - } |
|
243 | - return $this->_unspecifiedTypes; |
|
244 | - } |
|
230 | + /** |
|
231 | + * Get elements in the structure. |
|
232 | + * |
|
233 | + * @return UnspecifiedType[] |
|
234 | + */ |
|
235 | + public function elements(): array |
|
236 | + { |
|
237 | + if (!isset($this->_unspecifiedTypes)) { |
|
238 | + $this->_unspecifiedTypes = array_map( |
|
239 | + function (Element $el) { |
|
240 | + return new UnspecifiedType($el); |
|
241 | + }, $this->_elements); |
|
242 | + } |
|
243 | + return $this->_unspecifiedTypes; |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * Check whether the structure has an element at the given index, optionally |
|
248 | - * satisfying given tag expectation. |
|
249 | - * |
|
250 | - * @param int $idx Index 0..n |
|
251 | - * @param int|null $expectedTag Optional type tag expectation |
|
252 | - * @return bool |
|
253 | - */ |
|
254 | - public function has(int $idx, $expectedTag = null): bool |
|
255 | - { |
|
256 | - if (!isset($this->_elements[$idx])) { |
|
257 | - return false; |
|
258 | - } |
|
259 | - if (isset($expectedTag)) { |
|
260 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
261 | - return false; |
|
262 | - } |
|
263 | - } |
|
264 | - return true; |
|
265 | - } |
|
246 | + /** |
|
247 | + * Check whether the structure has an element at the given index, optionally |
|
248 | + * satisfying given tag expectation. |
|
249 | + * |
|
250 | + * @param int $idx Index 0..n |
|
251 | + * @param int|null $expectedTag Optional type tag expectation |
|
252 | + * @return bool |
|
253 | + */ |
|
254 | + public function has(int $idx, $expectedTag = null): bool |
|
255 | + { |
|
256 | + if (!isset($this->_elements[$idx])) { |
|
257 | + return false; |
|
258 | + } |
|
259 | + if (isset($expectedTag)) { |
|
260 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
261 | + return false; |
|
262 | + } |
|
263 | + } |
|
264 | + return true; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Get the element at the given index, optionally checking that the element |
|
269 | - * has a given tag. |
|
270 | - * |
|
271 | - * NOTE! Expectation checking is deprecated and should be done |
|
272 | - * with UnspecifiedType. |
|
273 | - * |
|
274 | - * @param int $idx Index 0..n |
|
275 | - * @param int|null $expectedTag Optional type tag expectation |
|
276 | - * @throws \OutOfBoundsException If element doesn't exists |
|
277 | - * @throws \UnexpectedValueException If expectation fails |
|
278 | - * @return UnspecifiedType |
|
279 | - */ |
|
280 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
281 | - { |
|
282 | - if (!isset($this->_elements[$idx])) { |
|
283 | - throw new \OutOfBoundsException( |
|
284 | - "Structure doesn't have an element at index $idx."); |
|
285 | - } |
|
286 | - $element = $this->_elements[$idx]; |
|
287 | - if (isset($expectedTag)) { |
|
288 | - $element->expectType($expectedTag); |
|
289 | - } |
|
290 | - return new UnspecifiedType($element); |
|
291 | - } |
|
267 | + /** |
|
268 | + * Get the element at the given index, optionally checking that the element |
|
269 | + * has a given tag. |
|
270 | + * |
|
271 | + * NOTE! Expectation checking is deprecated and should be done |
|
272 | + * with UnspecifiedType. |
|
273 | + * |
|
274 | + * @param int $idx Index 0..n |
|
275 | + * @param int|null $expectedTag Optional type tag expectation |
|
276 | + * @throws \OutOfBoundsException If element doesn't exists |
|
277 | + * @throws \UnexpectedValueException If expectation fails |
|
278 | + * @return UnspecifiedType |
|
279 | + */ |
|
280 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
281 | + { |
|
282 | + if (!isset($this->_elements[$idx])) { |
|
283 | + throw new \OutOfBoundsException( |
|
284 | + "Structure doesn't have an element at index $idx."); |
|
285 | + } |
|
286 | + $element = $this->_elements[$idx]; |
|
287 | + if (isset($expectedTag)) { |
|
288 | + $element->expectType($expectedTag); |
|
289 | + } |
|
290 | + return new UnspecifiedType($element); |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * Check whether the structure contains a context specific element with a |
|
295 | - * given tag. |
|
296 | - * |
|
297 | - * @param int $tag Tag number |
|
298 | - * @return boolean |
|
299 | - */ |
|
300 | - public function hasTagged(int $tag): bool |
|
301 | - { |
|
302 | - // lazily build lookup map |
|
303 | - if (!isset($this->_taggedMap)) { |
|
304 | - $this->_taggedMap = []; |
|
305 | - foreach ($this->_elements as $element) { |
|
306 | - if ($element->isTagged()) { |
|
307 | - $this->_taggedMap[$element->tag()] = $element; |
|
308 | - } |
|
309 | - } |
|
310 | - } |
|
311 | - return isset($this->_taggedMap[$tag]); |
|
312 | - } |
|
293 | + /** |
|
294 | + * Check whether the structure contains a context specific element with a |
|
295 | + * given tag. |
|
296 | + * |
|
297 | + * @param int $tag Tag number |
|
298 | + * @return boolean |
|
299 | + */ |
|
300 | + public function hasTagged(int $tag): bool |
|
301 | + { |
|
302 | + // lazily build lookup map |
|
303 | + if (!isset($this->_taggedMap)) { |
|
304 | + $this->_taggedMap = []; |
|
305 | + foreach ($this->_elements as $element) { |
|
306 | + if ($element->isTagged()) { |
|
307 | + $this->_taggedMap[$element->tag()] = $element; |
|
308 | + } |
|
309 | + } |
|
310 | + } |
|
311 | + return isset($this->_taggedMap[$tag]); |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Get a context specific element tagged with a given tag. |
|
316 | - * |
|
317 | - * @param int $tag |
|
318 | - * @throws \LogicException If tag doesn't exists |
|
319 | - * @return TaggedType |
|
320 | - */ |
|
321 | - public function getTagged(int $tag): TaggedType |
|
322 | - { |
|
323 | - if (!$this->hasTagged($tag)) { |
|
324 | - throw new \LogicException("No tagged element for tag $tag."); |
|
325 | - } |
|
326 | - return $this->_taggedMap[$tag]; |
|
327 | - } |
|
314 | + /** |
|
315 | + * Get a context specific element tagged with a given tag. |
|
316 | + * |
|
317 | + * @param int $tag |
|
318 | + * @throws \LogicException If tag doesn't exists |
|
319 | + * @return TaggedType |
|
320 | + */ |
|
321 | + public function getTagged(int $tag): TaggedType |
|
322 | + { |
|
323 | + if (!$this->hasTagged($tag)) { |
|
324 | + throw new \LogicException("No tagged element for tag $tag."); |
|
325 | + } |
|
326 | + return $this->_taggedMap[$tag]; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * |
|
331 | - * @see \Countable::count() |
|
332 | - * @return int |
|
333 | - */ |
|
334 | - public function count(): int |
|
335 | - { |
|
336 | - return count($this->_elements); |
|
337 | - } |
|
329 | + /** |
|
330 | + * |
|
331 | + * @see \Countable::count() |
|
332 | + * @return int |
|
333 | + */ |
|
334 | + public function count(): int |
|
335 | + { |
|
336 | + return count($this->_elements); |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Get an iterator for the UnspecifiedElement objects. |
|
341 | - * |
|
342 | - * @see \IteratorAggregate::getIterator() |
|
343 | - * @return \ArrayIterator |
|
344 | - */ |
|
345 | - public function getIterator(): \ArrayIterator |
|
346 | - { |
|
347 | - return new \ArrayIterator($this->elements()); |
|
348 | - } |
|
339 | + /** |
|
340 | + * Get an iterator for the UnspecifiedElement objects. |
|
341 | + * |
|
342 | + * @see \IteratorAggregate::getIterator() |
|
343 | + * @return \ArrayIterator |
|
344 | + */ |
|
345 | + public function getIterator(): \ArrayIterator |
|
346 | + { |
|
347 | + return new \ArrayIterator($this->elements()); |
|
348 | + } |
|
349 | 349 | } |
@@ -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()) { |
@@ -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)->intLength(); |
|
43 | - $str = $length ? substr($data, $idx, $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; |
|
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)->intLength(); |
|
43 | + $str = $length ? substr($data, $idx, $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; |
|
48 | + try { |
|
49 | + return new static($str); |
|
50 | + } catch (\InvalidArgumentException $e) { |
|
51 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | } |
@@ -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,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(int $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(int $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->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, string $data, |
|
182 | + 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 | } |