@@ -14,28 +14,28 @@ |
||
14 | 14 | */ |
15 | 15 | class UniversalString extends PrimitiveString |
16 | 16 | { |
17 | - use UniversalClass; |
|
17 | + use UniversalClass; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param string $string |
|
23 | - */ |
|
24 | - public function __construct(string $string) |
|
25 | - { |
|
26 | - $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
27 | - parent::__construct($string); |
|
28 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param string $string |
|
23 | + */ |
|
24 | + public function __construct(string $string) |
|
25 | + { |
|
26 | + $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
27 | + parent::__construct($string); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected function _validateString(string $string): bool |
|
34 | - { |
|
35 | - // UCS-4 has fixed with of 4 octets (32 bits) |
|
36 | - if (0 !== strlen($string) % 4) { |
|
37 | - return false; |
|
38 | - } |
|
39 | - return true; |
|
40 | - } |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected function _validateString(string $string): bool |
|
34 | + { |
|
35 | + // UCS-4 has fixed with of 4 octets (32 bits) |
|
36 | + if (0 !== strlen($string) % 4) { |
|
37 | + return false; |
|
38 | + } |
|
39 | + return true; |
|
40 | + } |
|
41 | 41 | } |
@@ -14,76 +14,76 @@ |
||
14 | 14 | */ |
15 | 15 | class DERData extends Element |
16 | 16 | { |
17 | - /** |
|
18 | - * DER encoded data. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_der; |
|
17 | + /** |
|
18 | + * DER encoded data. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_der; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Identifier of the underlying type. |
|
26 | - * |
|
27 | - * @var Identifier |
|
28 | - */ |
|
29 | - protected $_identifier; |
|
24 | + /** |
|
25 | + * Identifier of the underlying type. |
|
26 | + * |
|
27 | + * @var Identifier |
|
28 | + */ |
|
29 | + protected $_identifier; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Offset to the content in DER data. |
|
33 | - * |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - protected $_contentOffset = 0; |
|
31 | + /** |
|
32 | + * Offset to the content in DER data. |
|
33 | + * |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + protected $_contentOffset = 0; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param string $data DER encoded data |
|
42 | - * |
|
43 | - * @throws \Sop\ASN1\Exception\DecodeException If data does not adhere to DER |
|
44 | - */ |
|
45 | - public function __construct(string $data) |
|
46 | - { |
|
47 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
48 | - // check that length encoding is valid |
|
49 | - Length::expectFromDER($data, $this->_contentOffset); |
|
50 | - $this->_der = $data; |
|
51 | - $this->_typeTag = $this->_identifier->intTag(); |
|
52 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param string $data DER encoded data |
|
42 | + * |
|
43 | + * @throws \Sop\ASN1\Exception\DecodeException If data does not adhere to DER |
|
44 | + */ |
|
45 | + public function __construct(string $data) |
|
46 | + { |
|
47 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
48 | + // check that length encoding is valid |
|
49 | + Length::expectFromDER($data, $this->_contentOffset); |
|
50 | + $this->_der = $data; |
|
51 | + $this->_typeTag = $this->_identifier->intTag(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * {@inheritdoc} |
|
56 | - */ |
|
57 | - public function typeClass(): int |
|
58 | - { |
|
59 | - return $this->_identifier->typeClass(); |
|
60 | - } |
|
54 | + /** |
|
55 | + * {@inheritdoc} |
|
56 | + */ |
|
57 | + public function typeClass(): int |
|
58 | + { |
|
59 | + return $this->_identifier->typeClass(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * {@inheritdoc} |
|
64 | - */ |
|
65 | - public function isConstructed(): bool |
|
66 | - { |
|
67 | - return $this->_identifier->isConstructed(); |
|
68 | - } |
|
62 | + /** |
|
63 | + * {@inheritdoc} |
|
64 | + */ |
|
65 | + public function isConstructed(): bool |
|
66 | + { |
|
67 | + return $this->_identifier->isConstructed(); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritdoc} |
|
72 | - */ |
|
73 | - public function toDER(): string |
|
74 | - { |
|
75 | - return $this->_der; |
|
76 | - } |
|
70 | + /** |
|
71 | + * {@inheritdoc} |
|
72 | + */ |
|
73 | + public function toDER(): string |
|
74 | + { |
|
75 | + return $this->_der; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * {@inheritdoc} |
|
80 | - */ |
|
81 | - protected function _encodedContentDER(): string |
|
82 | - { |
|
83 | - // if there's no content payload |
|
84 | - if (strlen($this->_der) === $this->_contentOffset) { |
|
85 | - return ''; |
|
86 | - } |
|
87 | - return substr($this->_der, $this->_contentOffset); |
|
88 | - } |
|
78 | + /** |
|
79 | + * {@inheritdoc} |
|
80 | + */ |
|
81 | + protected function _encodedContentDER(): string |
|
82 | + { |
|
83 | + // if there's no content payload |
|
84 | + if (strlen($this->_der) === $this->_contentOffset) { |
|
85 | + return ''; |
|
86 | + } |
|
87 | + return substr($this->_der, $this->_contentOffset); |
|
88 | + } |
|
89 | 89 | } |
@@ -9,17 +9,17 @@ |
||
9 | 9 | */ |
10 | 10 | interface Stringable |
11 | 11 | { |
12 | - /** |
|
13 | - * Convert object to string. |
|
14 | - * |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function __toString(): string; |
|
12 | + /** |
|
13 | + * Convert object to string. |
|
14 | + * |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function __toString(): string; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Get the string representation of the type. |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function string(): string; |
|
19 | + /** |
|
20 | + * Get the string representation of the type. |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function string(): string; |
|
25 | 25 | } |
@@ -13,301 +13,301 @@ |
||
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 = [ |
|
30 | - self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
31 | - self::CLASS_APPLICATION => 'APPLICATION', |
|
32 | - self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
33 | - self::CLASS_PRIVATE => 'PRIVATE', |
|
34 | - ]; |
|
22 | + /** |
|
23 | + * Mapping from type class to human readable name. |
|
24 | + * |
|
25 | + * @internal |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + const MAP_CLASS_TO_NAME = [ |
|
30 | + self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
31 | + self::CLASS_APPLICATION => 'APPLICATION', |
|
32 | + self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
33 | + self::CLASS_PRIVATE => 'PRIVATE', |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var BigInt |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var BigInt |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = new BigInt($tag); |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = new BigInt($tag); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param null|int $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. |
|
81 | - * Variable is updated to the offset next to the |
|
82 | - * parsed identifier. If null, start from offset 0. |
|
83 | - * |
|
84 | - * @throws DecodeException If decoding fails |
|
85 | - * |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
89 | - { |
|
90 | - $idx = $offset ?? 0; |
|
91 | - $datalen = strlen($data); |
|
92 | - if ($idx >= $datalen) { |
|
93 | - throw new DecodeException('Invalid offset.'); |
|
94 | - } |
|
95 | - $byte = ord($data[$idx++]); |
|
96 | - // bits 8 and 7 (class) |
|
97 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
98 | - $class = (0b11000000 & $byte) >> 6; |
|
99 | - // bit 6 (0 = primitive / 1 = constructed) |
|
100 | - $pc = (0b00100000 & $byte) >> 5; |
|
101 | - // bits 5 to 1 (tag number) |
|
102 | - $tag = (0b00011111 & $byte); |
|
103 | - // long-form identifier |
|
104 | - if (0x1f === $tag) { |
|
105 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
106 | - } |
|
107 | - if (isset($offset)) { |
|
108 | - $offset = $idx; |
|
109 | - } |
|
110 | - return new self($class, $pc, $tag); |
|
111 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param null|int $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. |
|
81 | + * Variable is updated to the offset next to the |
|
82 | + * parsed identifier. If null, start from offset 0. |
|
83 | + * |
|
84 | + * @throws DecodeException If decoding fails |
|
85 | + * |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
89 | + { |
|
90 | + $idx = $offset ?? 0; |
|
91 | + $datalen = strlen($data); |
|
92 | + if ($idx >= $datalen) { |
|
93 | + throw new DecodeException('Invalid offset.'); |
|
94 | + } |
|
95 | + $byte = ord($data[$idx++]); |
|
96 | + // bits 8 and 7 (class) |
|
97 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
98 | + $class = (0b11000000 & $byte) >> 6; |
|
99 | + // bit 6 (0 = primitive / 1 = constructed) |
|
100 | + $pc = (0b00100000 & $byte) >> 5; |
|
101 | + // bits 5 to 1 (tag number) |
|
102 | + $tag = (0b00011111 & $byte); |
|
103 | + // long-form identifier |
|
104 | + if (0x1f === $tag) { |
|
105 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
106 | + } |
|
107 | + if (isset($offset)) { |
|
108 | + $offset = $idx; |
|
109 | + } |
|
110 | + return new self($class, $pc, $tag); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - public function toDER(): string |
|
117 | - { |
|
118 | - $bytes = []; |
|
119 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
120 | - $tag = $this->_tag->gmpObj(); |
|
121 | - if ($tag < 0x1f) { |
|
122 | - $bytes[] = $byte | $tag; |
|
123 | - } |
|
124 | - // long-form identifier |
|
125 | - else { |
|
126 | - $bytes[] = $byte | 0x1f; |
|
127 | - $octets = []; |
|
128 | - for (; $tag > 0; $tag >>= 7) { |
|
129 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
130 | - } |
|
131 | - // last octet has bit 8 set to zero |
|
132 | - $octets[0] &= 0x7f; |
|
133 | - foreach (array_reverse($octets) as $octet) { |
|
134 | - $bytes[] = $octet; |
|
135 | - } |
|
136 | - } |
|
137 | - return pack('C*', ...$bytes); |
|
138 | - } |
|
113 | + /** |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + public function toDER(): string |
|
117 | + { |
|
118 | + $bytes = []; |
|
119 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
120 | + $tag = $this->_tag->gmpObj(); |
|
121 | + if ($tag < 0x1f) { |
|
122 | + $bytes[] = $byte | $tag; |
|
123 | + } |
|
124 | + // long-form identifier |
|
125 | + else { |
|
126 | + $bytes[] = $byte | 0x1f; |
|
127 | + $octets = []; |
|
128 | + for (; $tag > 0; $tag >>= 7) { |
|
129 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
130 | + } |
|
131 | + // last octet has bit 8 set to zero |
|
132 | + $octets[0] &= 0x7f; |
|
133 | + foreach (array_reverse($octets) as $octet) { |
|
134 | + $bytes[] = $octet; |
|
135 | + } |
|
136 | + } |
|
137 | + return pack('C*', ...$bytes); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Get class of the type. |
|
142 | - * |
|
143 | - * @return int |
|
144 | - */ |
|
145 | - public function typeClass(): int |
|
146 | - { |
|
147 | - return $this->_class; |
|
148 | - } |
|
140 | + /** |
|
141 | + * Get class of the type. |
|
142 | + * |
|
143 | + * @return int |
|
144 | + */ |
|
145 | + public function typeClass(): int |
|
146 | + { |
|
147 | + return $this->_class; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Get P/C. |
|
152 | - * |
|
153 | - * @return int |
|
154 | - */ |
|
155 | - public function pc(): int |
|
156 | - { |
|
157 | - return $this->_pc; |
|
158 | - } |
|
150 | + /** |
|
151 | + * Get P/C. |
|
152 | + * |
|
153 | + * @return int |
|
154 | + */ |
|
155 | + public function pc(): int |
|
156 | + { |
|
157 | + return $this->_pc; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Get the tag number. |
|
162 | - * |
|
163 | - * @return string Base 10 integer string |
|
164 | - */ |
|
165 | - public function tag(): string |
|
166 | - { |
|
167 | - return $this->_tag->base10(); |
|
168 | - } |
|
160 | + /** |
|
161 | + * Get the tag number. |
|
162 | + * |
|
163 | + * @return string Base 10 integer string |
|
164 | + */ |
|
165 | + public function tag(): string |
|
166 | + { |
|
167 | + return $this->_tag->base10(); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Get the tag as an integer. |
|
172 | - * |
|
173 | - * @return int |
|
174 | - */ |
|
175 | - public function intTag(): int |
|
176 | - { |
|
177 | - return $this->_tag->intVal(); |
|
178 | - } |
|
170 | + /** |
|
171 | + * Get the tag as an integer. |
|
172 | + * |
|
173 | + * @return int |
|
174 | + */ |
|
175 | + public function intTag(): int |
|
176 | + { |
|
177 | + return $this->_tag->intVal(); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Check whether type is of an universal class. |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function isUniversal(): bool |
|
186 | - { |
|
187 | - return self::CLASS_UNIVERSAL === $this->_class; |
|
188 | - } |
|
180 | + /** |
|
181 | + * Check whether type is of an universal class. |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function isUniversal(): bool |
|
186 | + { |
|
187 | + return self::CLASS_UNIVERSAL === $this->_class; |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Check whether type is of an application class. |
|
192 | - * |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - public function isApplication(): bool |
|
196 | - { |
|
197 | - return self::CLASS_APPLICATION === $this->_class; |
|
198 | - } |
|
190 | + /** |
|
191 | + * Check whether type is of an application class. |
|
192 | + * |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + public function isApplication(): bool |
|
196 | + { |
|
197 | + return self::CLASS_APPLICATION === $this->_class; |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Check whether type is of a context specific class. |
|
202 | - * |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function isContextSpecific(): bool |
|
206 | - { |
|
207 | - return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
208 | - } |
|
200 | + /** |
|
201 | + * Check whether type is of a context specific class. |
|
202 | + * |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function isContextSpecific(): bool |
|
206 | + { |
|
207 | + return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * Check whether type is of a private class. |
|
212 | - * |
|
213 | - * @return bool |
|
214 | - */ |
|
215 | - public function isPrivate(): bool |
|
216 | - { |
|
217 | - return self::CLASS_PRIVATE === $this->_class; |
|
218 | - } |
|
210 | + /** |
|
211 | + * Check whether type is of a private class. |
|
212 | + * |
|
213 | + * @return bool |
|
214 | + */ |
|
215 | + public function isPrivate(): bool |
|
216 | + { |
|
217 | + return self::CLASS_PRIVATE === $this->_class; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Check whether content is primitive type. |
|
222 | - * |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function isPrimitive(): bool |
|
226 | - { |
|
227 | - return self::PRIMITIVE === $this->_pc; |
|
228 | - } |
|
220 | + /** |
|
221 | + * Check whether content is primitive type. |
|
222 | + * |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function isPrimitive(): bool |
|
226 | + { |
|
227 | + return self::PRIMITIVE === $this->_pc; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Check hether content is constructed type. |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - public function isConstructed(): bool |
|
236 | - { |
|
237 | - return self::CONSTRUCTED === $this->_pc; |
|
238 | - } |
|
230 | + /** |
|
231 | + * Check hether content is constructed type. |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + public function isConstructed(): bool |
|
236 | + { |
|
237 | + return self::CONSTRUCTED === $this->_pc; |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Get self with given type class. |
|
242 | - * |
|
243 | - * @param int $class One of `CLASS_*` enumerations |
|
244 | - * |
|
245 | - * @return self |
|
246 | - */ |
|
247 | - public function withClass(int $class): Identifier |
|
248 | - { |
|
249 | - $obj = clone $this; |
|
250 | - $obj->_class = 0b11 & $class; |
|
251 | - return $obj; |
|
252 | - } |
|
240 | + /** |
|
241 | + * Get self with given type class. |
|
242 | + * |
|
243 | + * @param int $class One of `CLASS_*` enumerations |
|
244 | + * |
|
245 | + * @return self |
|
246 | + */ |
|
247 | + public function withClass(int $class): Identifier |
|
248 | + { |
|
249 | + $obj = clone $this; |
|
250 | + $obj->_class = 0b11 & $class; |
|
251 | + return $obj; |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Get self with given type tag. |
|
256 | - * |
|
257 | - * @param int|string $tag Tag number |
|
258 | - * |
|
259 | - * @return self |
|
260 | - */ |
|
261 | - public function withTag($tag): Identifier |
|
262 | - { |
|
263 | - $obj = clone $this; |
|
264 | - $obj->_tag = new BigInt($tag); |
|
265 | - return $obj; |
|
266 | - } |
|
254 | + /** |
|
255 | + * Get self with given type tag. |
|
256 | + * |
|
257 | + * @param int|string $tag Tag number |
|
258 | + * |
|
259 | + * @return self |
|
260 | + */ |
|
261 | + public function withTag($tag): Identifier |
|
262 | + { |
|
263 | + $obj = clone $this; |
|
264 | + $obj->_tag = new BigInt($tag); |
|
265 | + return $obj; |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Get human readable name of the type class. |
|
270 | - * |
|
271 | - * @param int $class |
|
272 | - * |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public static function classToName(int $class): string |
|
276 | - { |
|
277 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
278 | - return "CLASS {$class}"; |
|
279 | - } |
|
280 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
281 | - } |
|
268 | + /** |
|
269 | + * Get human readable name of the type class. |
|
270 | + * |
|
271 | + * @param int $class |
|
272 | + * |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public static function classToName(int $class): string |
|
276 | + { |
|
277 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
278 | + return "CLASS {$class}"; |
|
279 | + } |
|
280 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Parse long form tag. |
|
285 | - * |
|
286 | - * @param string $data DER data |
|
287 | - * @param int $offset Reference to the variable containing offset to data |
|
288 | - * |
|
289 | - * @throws DecodeException If decoding fails |
|
290 | - * |
|
291 | - * @return string Tag number |
|
292 | - */ |
|
293 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
294 | - { |
|
295 | - $datalen = strlen($data); |
|
296 | - $tag = gmp_init(0, 10); |
|
297 | - while (true) { |
|
298 | - if ($offset >= $datalen) { |
|
299 | - throw new DecodeException( |
|
300 | - 'Unexpected end of data while decoding' . |
|
301 | - ' long form identifier.'); |
|
302 | - } |
|
303 | - $byte = ord($data[$offset++]); |
|
304 | - $tag <<= 7; |
|
305 | - $tag |= 0x7f & $byte; |
|
306 | - // last byte has bit 8 set to zero |
|
307 | - if (!(0x80 & $byte)) { |
|
308 | - break; |
|
309 | - } |
|
310 | - } |
|
311 | - return gmp_strval($tag, 10); |
|
312 | - } |
|
283 | + /** |
|
284 | + * Parse long form tag. |
|
285 | + * |
|
286 | + * @param string $data DER data |
|
287 | + * @param int $offset Reference to the variable containing offset to data |
|
288 | + * |
|
289 | + * @throws DecodeException If decoding fails |
|
290 | + * |
|
291 | + * @return string Tag number |
|
292 | + */ |
|
293 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
294 | + { |
|
295 | + $datalen = strlen($data); |
|
296 | + $tag = gmp_init(0, 10); |
|
297 | + while (true) { |
|
298 | + if ($offset >= $datalen) { |
|
299 | + throw new DecodeException( |
|
300 | + 'Unexpected end of data while decoding' . |
|
301 | + ' long form identifier.'); |
|
302 | + } |
|
303 | + $byte = ord($data[$offset++]); |
|
304 | + $tag <<= 7; |
|
305 | + $tag |= 0x7f & $byte; |
|
306 | + // last byte has bit 8 set to zero |
|
307 | + if (!(0x80 & $byte)) { |
|
308 | + break; |
|
309 | + } |
|
310 | + } |
|
311 | + return gmp_strval($tag, 10); |
|
312 | + } |
|
313 | 313 | } |
@@ -12,56 +12,56 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor. |
|
17 | - * |
|
18 | - * @param Element ...$elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor. |
|
17 | + * |
|
18 | + * @param Element ...$elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * |
|
29 | - * Used for DER encoding of *SET* type. |
|
30 | - * |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function sortedSet(): self |
|
34 | - { |
|
35 | - $obj = clone $this; |
|
36 | - usort($obj->_elements, |
|
37 | - function (Element $a, Element $b) { |
|
38 | - if ($a->typeClass() !== $b->typeClass()) { |
|
39 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | - } |
|
41 | - if ($a->tag() === $b->tag()) { |
|
42 | - return 0; |
|
43 | - } |
|
44 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | - }); |
|
46 | - return $obj; |
|
47 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * |
|
29 | + * Used for DER encoding of *SET* type. |
|
30 | + * |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function sortedSet(): self |
|
34 | + { |
|
35 | + $obj = clone $this; |
|
36 | + usort($obj->_elements, |
|
37 | + function (Element $a, Element $b) { |
|
38 | + if ($a->typeClass() !== $b->typeClass()) { |
|
39 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | + } |
|
41 | + if ($a->tag() === $b->tag()) { |
|
42 | + return 0; |
|
43 | + } |
|
44 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | + }); |
|
46 | + return $obj; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Sort by encoding ascending order. |
|
51 | - * |
|
52 | - * Used for DER encoding of *SET OF* type. |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public function sortedSetOf(): self |
|
57 | - { |
|
58 | - $obj = clone $this; |
|
59 | - usort($obj->_elements, |
|
60 | - function (Element $a, Element $b) { |
|
61 | - $a_der = $a->toDER(); |
|
62 | - $b_der = $b->toDER(); |
|
63 | - return strcmp($a_der, $b_der); |
|
64 | - }); |
|
65 | - return $obj; |
|
66 | - } |
|
49 | + /** |
|
50 | + * Sort by encoding ascending order. |
|
51 | + * |
|
52 | + * Used for DER encoding of *SET OF* type. |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public function sortedSetOf(): self |
|
57 | + { |
|
58 | + $obj = clone $this; |
|
59 | + usort($obj->_elements, |
|
60 | + function (Element $a, Element $b) { |
|
61 | + $a_der = $a->toDER(); |
|
62 | + $b_der = $b->toDER(); |
|
63 | + return strcmp($a_der, $b_der); |
|
64 | + }); |
|
65 | + return $obj; |
|
66 | + } |
|
67 | 67 | } |
@@ -31,106 +31,106 @@ |
||
31 | 31 | */ |
32 | 32 | class ConstructedString extends Structure implements StringType |
33 | 33 | { |
34 | - /** |
|
35 | - * Constructor. |
|
36 | - * |
|
37 | - * @internal Use `create()` or `createWithTag()` method instead |
|
38 | - * |
|
39 | - * @param Element ...$elements Any number of elements |
|
40 | - */ |
|
41 | - protected function __construct(Element ...$elements) |
|
42 | - { |
|
43 | - parent::__construct(...$elements); |
|
44 | - } |
|
34 | + /** |
|
35 | + * Constructor. |
|
36 | + * |
|
37 | + * @internal Use `create()` or `createWithTag()` method instead |
|
38 | + * |
|
39 | + * @param Element ...$elements Any number of elements |
|
40 | + */ |
|
41 | + protected function __construct(Element ...$elements) |
|
42 | + { |
|
43 | + parent::__construct(...$elements); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - public function __toString(): string |
|
50 | - { |
|
51 | - return $this->string(); |
|
52 | - } |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + public function __toString(): string |
|
50 | + { |
|
51 | + return $this->string(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Create from a list of string type elements. |
|
56 | - * |
|
57 | - * All strings must have the same type. |
|
58 | - * |
|
59 | - * @param StringType ...$elements |
|
60 | - * |
|
61 | - * @throws \LogicException |
|
62 | - * |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - public static function create(StringType ...$elements): self |
|
66 | - { |
|
67 | - if (!count($elements)) { |
|
68 | - throw new \LogicException( |
|
69 | - 'No elements, unable to determine type tag.'); |
|
70 | - } |
|
71 | - $tag = $elements[0]->tag(); |
|
72 | - foreach ($elements as $el) { |
|
73 | - if ($el->tag() !== $tag) { |
|
74 | - throw new \LogicException( |
|
75 | - 'All elements in constructed string must have the same type.'); |
|
76 | - } |
|
77 | - } |
|
78 | - return self::createWithTag($tag, ...$elements); |
|
79 | - } |
|
54 | + /** |
|
55 | + * Create from a list of string type elements. |
|
56 | + * |
|
57 | + * All strings must have the same type. |
|
58 | + * |
|
59 | + * @param StringType ...$elements |
|
60 | + * |
|
61 | + * @throws \LogicException |
|
62 | + * |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + public static function create(StringType ...$elements): self |
|
66 | + { |
|
67 | + if (!count($elements)) { |
|
68 | + throw new \LogicException( |
|
69 | + 'No elements, unable to determine type tag.'); |
|
70 | + } |
|
71 | + $tag = $elements[0]->tag(); |
|
72 | + foreach ($elements as $el) { |
|
73 | + if ($el->tag() !== $tag) { |
|
74 | + throw new \LogicException( |
|
75 | + 'All elements in constructed string must have the same type.'); |
|
76 | + } |
|
77 | + } |
|
78 | + return self::createWithTag($tag, ...$elements); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Create from strings with a given type tag. |
|
83 | - * |
|
84 | - * Does not perform any validation on types. |
|
85 | - * |
|
86 | - * @param int $tag Type tag for the constructed string element |
|
87 | - * @param StringType ...$elements Any number of elements |
|
88 | - * |
|
89 | - * @return self |
|
90 | - */ |
|
91 | - public static function createWithTag(int $tag, StringType ...$elements) |
|
92 | - { |
|
93 | - $el = new self(...$elements); |
|
94 | - $el->_typeTag = $tag; |
|
95 | - return $el; |
|
96 | - } |
|
81 | + /** |
|
82 | + * Create from strings with a given type tag. |
|
83 | + * |
|
84 | + * Does not perform any validation on types. |
|
85 | + * |
|
86 | + * @param int $tag Type tag for the constructed string element |
|
87 | + * @param StringType ...$elements Any number of elements |
|
88 | + * |
|
89 | + * @return self |
|
90 | + */ |
|
91 | + public static function createWithTag(int $tag, StringType ...$elements) |
|
92 | + { |
|
93 | + $el = new self(...$elements); |
|
94 | + $el->_typeTag = $tag; |
|
95 | + return $el; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Get a list of strings in this structure. |
|
100 | - * |
|
101 | - * @return string[] |
|
102 | - */ |
|
103 | - public function strings(): array |
|
104 | - { |
|
105 | - return array_map(function (StringType $el) { |
|
106 | - return $el->string(); |
|
107 | - }, $this->_elements); |
|
108 | - } |
|
98 | + /** |
|
99 | + * Get a list of strings in this structure. |
|
100 | + * |
|
101 | + * @return string[] |
|
102 | + */ |
|
103 | + public function strings(): array |
|
104 | + { |
|
105 | + return array_map(function (StringType $el) { |
|
106 | + return $el->string(); |
|
107 | + }, $this->_elements); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Get the contained strings concatenated together. |
|
112 | - * |
|
113 | - * NOTE: It's unclear how bit strings with unused bits should be concatenated. |
|
114 | - * |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function string(): string |
|
118 | - { |
|
119 | - return implode('', $this->strings()); |
|
120 | - } |
|
110 | + /** |
|
111 | + * Get the contained strings concatenated together. |
|
112 | + * |
|
113 | + * NOTE: It's unclear how bit strings with unused bits should be concatenated. |
|
114 | + * |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function string(): string |
|
118 | + { |
|
119 | + return implode('', $this->strings()); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * {@inheritdoc} |
|
124 | - * |
|
125 | - * @return self |
|
126 | - */ |
|
127 | - protected static function _decodeFromDER(Identifier $identifier, |
|
128 | - string $data, int &$offset): ElementBase |
|
129 | - { |
|
130 | - /** @var ConstructedString $type */ |
|
131 | - $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
132 | - [$identifier, $data, &$offset]); |
|
133 | - $type->_typeTag = $identifier->intTag(); |
|
134 | - return $type; |
|
135 | - } |
|
122 | + /** |
|
123 | + * {@inheritdoc} |
|
124 | + * |
|
125 | + * @return self |
|
126 | + */ |
|
127 | + protected static function _decodeFromDER(Identifier $identifier, |
|
128 | + string $data, int &$offset): ElementBase |
|
129 | + { |
|
130 | + /** @var ConstructedString $type */ |
|
131 | + $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
132 | + [$identifier, $data, &$offset]); |
|
133 | + $type->_typeTag = $identifier->intTag(); |
|
134 | + return $type; |
|
135 | + } |
|
136 | 136 | } |
@@ -13,15 +13,15 @@ |
||
13 | 13 | */ |
14 | 14 | interface ImplicitTagging extends ElementBase |
15 | 15 | { |
16 | - /** |
|
17 | - * Get implicitly tagged wrapped element. |
|
18 | - * |
|
19 | - * @param int $tag Tag of the element |
|
20 | - * @param int $class Expected type class of the element |
|
21 | - * |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * |
|
24 | - * @return UnspecifiedType |
|
25 | - */ |
|
26 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
16 | + /** |
|
17 | + * Get implicitly tagged wrapped element. |
|
18 | + * |
|
19 | + * @param int $tag Tag of the element |
|
20 | + * @param int $class Expected type class of the element |
|
21 | + * |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * |
|
24 | + * @return UnspecifiedType |
|
25 | + */ |
|
26 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
27 | 27 | } |
@@ -9,10 +9,10 @@ |
||
9 | 9 | */ |
10 | 10 | interface TimeType extends StringType |
11 | 11 | { |
12 | - /** |
|
13 | - * Get the date and time. |
|
14 | - * |
|
15 | - * @return \DateTimeImmutable |
|
16 | - */ |
|
17 | - public function dateTime(): \DateTimeImmutable; |
|
12 | + /** |
|
13 | + * Get the date and time. |
|
14 | + * |
|
15 | + * @return \DateTimeImmutable |
|
16 | + */ |
|
17 | + public function dateTime(): \DateTimeImmutable; |
|
18 | 18 | } |
@@ -11,58 +11,58 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class BaseString extends Element implements StringType |
13 | 13 | { |
14 | - /** |
|
15 | - * String value. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $_string; |
|
14 | + /** |
|
15 | + * String value. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $_string; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Constructor. |
|
23 | - * |
|
24 | - * @param string $string |
|
25 | - * |
|
26 | - * @throws \InvalidArgumentException |
|
27 | - */ |
|
28 | - public function __construct(string $string) |
|
29 | - { |
|
30 | - if (!$this->_validateString($string)) { |
|
31 | - throw new \InvalidArgumentException( |
|
32 | - sprintf('Not a valid %s string.', |
|
33 | - self::tagToName($this->_typeTag))); |
|
34 | - } |
|
35 | - $this->_string = $string; |
|
36 | - } |
|
21 | + /** |
|
22 | + * Constructor. |
|
23 | + * |
|
24 | + * @param string $string |
|
25 | + * |
|
26 | + * @throws \InvalidArgumentException |
|
27 | + */ |
|
28 | + public function __construct(string $string) |
|
29 | + { |
|
30 | + if (!$this->_validateString($string)) { |
|
31 | + throw new \InvalidArgumentException( |
|
32 | + sprintf('Not a valid %s string.', |
|
33 | + self::tagToName($this->_typeTag))); |
|
34 | + } |
|
35 | + $this->_string = $string; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * {@inheritdoc} |
|
40 | - */ |
|
41 | - public function __toString(): string |
|
42 | - { |
|
43 | - return $this->string(); |
|
44 | - } |
|
38 | + /** |
|
39 | + * {@inheritdoc} |
|
40 | + */ |
|
41 | + public function __toString(): string |
|
42 | + { |
|
43 | + return $this->string(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the string value. |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function string(): string |
|
52 | - { |
|
53 | - return $this->_string; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Get the string value. |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function string(): string |
|
52 | + { |
|
53 | + return $this->_string; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Check whether string is valid for the concrete type. |
|
58 | - * |
|
59 | - * @param string $string |
|
60 | - * |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - protected function _validateString(string $string): bool |
|
64 | - { |
|
65 | - // Override in derived classes |
|
66 | - return true; |
|
67 | - } |
|
56 | + /** |
|
57 | + * Check whether string is valid for the concrete type. |
|
58 | + * |
|
59 | + * @param string $string |
|
60 | + * |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + protected function _validateString(string $string): bool |
|
64 | + { |
|
65 | + // Override in derived classes |
|
66 | + return true; |
|
67 | + } |
|
68 | 68 | } |