@@ -9,12 +9,12 @@ |
||
9 | 9 | */ |
10 | 10 | class BinaryMatch extends MatchingRule |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - public function compare($assertion, $value) |
|
17 | - { |
|
18 | - return strcmp($assertion, $value) == 0; |
|
19 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + public function compare($assertion, $value) |
|
17 | + { |
|
18 | + return strcmp($assertion, $value) == 0; |
|
19 | + } |
|
20 | 20 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | abstract class MatchingRule |
11 | 11 | { |
12 | - /** |
|
13 | - * Compare attribute value to assertion. |
|
14 | - * |
|
15 | - * @param mixed $assertion Value to assert |
|
16 | - * @param mixed $value Attribute value |
|
17 | - * @return bool|null True if value matches. Null shall be returned if match |
|
18 | - * evaluates to Undefined. |
|
19 | - */ |
|
20 | - abstract public function compare($assertion, $value); |
|
12 | + /** |
|
13 | + * Compare attribute value to assertion. |
|
14 | + * |
|
15 | + * @param mixed $assertion Value to assert |
|
16 | + * @param mixed $value Attribute value |
|
17 | + * @return bool|null True if value matches. Null shall be returned if match |
|
18 | + * evaluates to Undefined. |
|
19 | + */ |
|
20 | + abstract public function compare($assertion, $value); |
|
21 | 21 | } |
@@ -9,31 +9,31 @@ |
||
9 | 9 | */ |
10 | 10 | abstract class StringPrepMatchingRule extends MatchingRule |
11 | 11 | { |
12 | - /** |
|
13 | - * String preparer. |
|
14 | - * |
|
15 | - * @var StringPreparer $_prep |
|
16 | - */ |
|
17 | - protected $_prep; |
|
12 | + /** |
|
13 | + * String preparer. |
|
14 | + * |
|
15 | + * @var StringPreparer $_prep |
|
16 | + */ |
|
17 | + protected $_prep; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param StringPreparer $preparer |
|
23 | - */ |
|
24 | - public function __construct(StringPreparer $preparer) |
|
25 | - { |
|
26 | - $this->_prep = $preparer; |
|
27 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param StringPreparer $preparer |
|
23 | + */ |
|
24 | + public function __construct(StringPreparer $preparer) |
|
25 | + { |
|
26 | + $this->_prep = $preparer; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - public function compare($assertion, $value) |
|
34 | - { |
|
35 | - $assertion = $this->_prep->prepare($assertion); |
|
36 | - $value = $this->_prep->prepare($value); |
|
37 | - return strcmp($assertion, $value) == 0; |
|
38 | - } |
|
29 | + /** |
|
30 | + * |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + public function compare($assertion, $value) |
|
34 | + { |
|
35 | + $assertion = $this->_prep->prepare($assertion); |
|
36 | + $value = $this->_prep->prepare($value); |
|
37 | + return strcmp($assertion, $value) == 0; |
|
38 | + } |
|
39 | 39 | } |
@@ -13,14 +13,14 @@ |
||
13 | 13 | */ |
14 | 14 | class CaseIgnoreMatch extends StringPrepMatchingRule |
15 | 15 | { |
16 | - /** |
|
17 | - * Constructor. |
|
18 | - * |
|
19 | - * @param int $string_type ASN.1 string type tag |
|
20 | - */ |
|
21 | - public function __construct(int $string_type) |
|
22 | - { |
|
23 | - parent::__construct( |
|
24 | - StringPreparer::forStringType($string_type)->withCaseFolding(true)); |
|
25 | - } |
|
16 | + /** |
|
17 | + * Constructor. |
|
18 | + * |
|
19 | + * @param int $string_type ASN.1 string type tag |
|
20 | + */ |
|
21 | + public function __construct(int $string_type) |
|
22 | + { |
|
23 | + parent::__construct( |
|
24 | + StringPreparer::forStringType($string_type)->withCaseFolding(true)); |
|
25 | + } |
|
26 | 26 | } |
@@ -13,13 +13,13 @@ |
||
13 | 13 | */ |
14 | 14 | class CaseExactMatch extends StringPrepMatchingRule |
15 | 15 | { |
16 | - /** |
|
17 | - * Constructor. |
|
18 | - * |
|
19 | - * @param int $string_type ASN.1 string type tag |
|
20 | - */ |
|
21 | - public function __construct(int $string_type) |
|
22 | - { |
|
23 | - parent::__construct(StringPreparer::forStringType($string_type)); |
|
24 | - } |
|
16 | + /** |
|
17 | + * Constructor. |
|
18 | + * |
|
19 | + * @param int $string_type ASN.1 string type tag |
|
20 | + */ |
|
21 | + public function __construct(int $string_type) |
|
22 | + { |
|
23 | + parent::__construct(StringPreparer::forStringType($string_type)); |
|
24 | + } |
|
25 | 25 | } |
@@ -15,390 +15,390 @@ |
||
15 | 15 | */ |
16 | 16 | class DNParser |
17 | 17 | { |
18 | - /** |
|
19 | - * DN string. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $_dn; |
|
18 | + /** |
|
19 | + * DN string. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $_dn; |
|
24 | 24 | |
25 | - /** |
|
26 | - * DN string length. |
|
27 | - * |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - private $_len; |
|
25 | + /** |
|
26 | + * DN string length. |
|
27 | + * |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + private $_len; |
|
31 | 31 | |
32 | - /** |
|
33 | - * RFC 2253 special characters. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - const SPECIAL_CHARS = ",=+<>#;"; |
|
32 | + /** |
|
33 | + * RFC 2253 special characters. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + const SPECIAL_CHARS = ",=+<>#;"; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Parse distinguished name string to name-components. |
|
41 | - * |
|
42 | - * @param string $dn |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public static function parseString(string $dn): array |
|
46 | - { |
|
47 | - $parser = new self($dn); |
|
48 | - return $parser->parse(); |
|
49 | - } |
|
39 | + /** |
|
40 | + * Parse distinguished name string to name-components. |
|
41 | + * |
|
42 | + * @param string $dn |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public static function parseString(string $dn): array |
|
46 | + { |
|
47 | + $parser = new self($dn); |
|
48 | + return $parser->parse(); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Escape a AttributeValue string conforming to RFC 2253. |
|
53 | - * |
|
54 | - * @link https://tools.ietf.org/html/rfc2253#section-2.4 |
|
55 | - * @param string $str |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public static function escapeString(string $str): string |
|
59 | - { |
|
60 | - // one of the characters ",", "+", """, "\", "<", ">" or ";" |
|
61 | - $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str); |
|
62 | - // a space character occurring at the end of the string |
|
63 | - $str = preg_replace('/( )$/u', '\\\\$1', $str); |
|
64 | - // a space or "#" character occurring at the beginning of the string |
|
65 | - $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
|
66 | - // implementation specific special characters |
|
67 | - $str = preg_replace_callback('/([\pC])/u', |
|
68 | - function ($m) { |
|
69 | - $octets = str_split(bin2hex($m[1]), 2); |
|
70 | - return implode("", |
|
71 | - array_map( |
|
72 | - function ($octet) { |
|
73 | - return '\\' . strtoupper($octet); |
|
74 | - }, $octets)); |
|
75 | - }, $str); |
|
76 | - return $str; |
|
77 | - } |
|
51 | + /** |
|
52 | + * Escape a AttributeValue string conforming to RFC 2253. |
|
53 | + * |
|
54 | + * @link https://tools.ietf.org/html/rfc2253#section-2.4 |
|
55 | + * @param string $str |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public static function escapeString(string $str): string |
|
59 | + { |
|
60 | + // one of the characters ",", "+", """, "\", "<", ">" or ";" |
|
61 | + $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str); |
|
62 | + // a space character occurring at the end of the string |
|
63 | + $str = preg_replace('/( )$/u', '\\\\$1', $str); |
|
64 | + // a space or "#" character occurring at the beginning of the string |
|
65 | + $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
|
66 | + // implementation specific special characters |
|
67 | + $str = preg_replace_callback('/([\pC])/u', |
|
68 | + function ($m) { |
|
69 | + $octets = str_split(bin2hex($m[1]), 2); |
|
70 | + return implode("", |
|
71 | + array_map( |
|
72 | + function ($octet) { |
|
73 | + return '\\' . strtoupper($octet); |
|
74 | + }, $octets)); |
|
75 | + }, $str); |
|
76 | + return $str; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Constructor. |
|
81 | - * |
|
82 | - * @param string $dn Distinguised name |
|
83 | - */ |
|
84 | - protected function __construct(string $dn) |
|
85 | - { |
|
86 | - $this->_dn = $dn; |
|
87 | - $this->_len = strlen($dn); |
|
88 | - } |
|
79 | + /** |
|
80 | + * Constructor. |
|
81 | + * |
|
82 | + * @param string $dn Distinguised name |
|
83 | + */ |
|
84 | + protected function __construct(string $dn) |
|
85 | + { |
|
86 | + $this->_dn = $dn; |
|
87 | + $this->_len = strlen($dn); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Parse DN to name-components. |
|
92 | - * |
|
93 | - * @throws \RuntimeException |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function parse(): array |
|
97 | - { |
|
98 | - $offset = 0; |
|
99 | - $name = $this->_parseName($offset); |
|
100 | - if ($offset < $this->_len) { |
|
101 | - $remains = substr($this->_dn, $offset); |
|
102 | - throw new \UnexpectedValueException( |
|
103 | - "Parser finished before the end of string" . |
|
104 | - ", remaining: '$remains'."); |
|
105 | - } |
|
106 | - return $name; |
|
107 | - } |
|
90 | + /** |
|
91 | + * Parse DN to name-components. |
|
92 | + * |
|
93 | + * @throws \RuntimeException |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function parse(): array |
|
97 | + { |
|
98 | + $offset = 0; |
|
99 | + $name = $this->_parseName($offset); |
|
100 | + if ($offset < $this->_len) { |
|
101 | + $remains = substr($this->_dn, $offset); |
|
102 | + throw new \UnexpectedValueException( |
|
103 | + "Parser finished before the end of string" . |
|
104 | + ", remaining: '$remains'."); |
|
105 | + } |
|
106 | + return $name; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Parse 'name'. |
|
111 | - * |
|
112 | - * name-component *("," name-component) |
|
113 | - * |
|
114 | - * @param int $offset |
|
115 | - * @return array Array of name-components |
|
116 | - */ |
|
117 | - private function _parseName(int &$offset): array |
|
118 | - { |
|
119 | - $idx = $offset; |
|
120 | - $names = array(); |
|
121 | - while ($idx < $this->_len) { |
|
122 | - $names[] = $this->_parseNameComponent($idx); |
|
123 | - if ($idx >= $this->_len) { |
|
124 | - break; |
|
125 | - } |
|
126 | - $this->_skipWs($idx); |
|
127 | - if ("," != $this->_dn[$idx] && ";" != $this->_dn[$idx]) { |
|
128 | - break; |
|
129 | - } |
|
130 | - $idx++; |
|
131 | - $this->_skipWs($idx); |
|
132 | - } |
|
133 | - $offset = $idx; |
|
134 | - return array_reverse($names); |
|
135 | - } |
|
109 | + /** |
|
110 | + * Parse 'name'. |
|
111 | + * |
|
112 | + * name-component *("," name-component) |
|
113 | + * |
|
114 | + * @param int $offset |
|
115 | + * @return array Array of name-components |
|
116 | + */ |
|
117 | + private function _parseName(int &$offset): array |
|
118 | + { |
|
119 | + $idx = $offset; |
|
120 | + $names = array(); |
|
121 | + while ($idx < $this->_len) { |
|
122 | + $names[] = $this->_parseNameComponent($idx); |
|
123 | + if ($idx >= $this->_len) { |
|
124 | + break; |
|
125 | + } |
|
126 | + $this->_skipWs($idx); |
|
127 | + if ("," != $this->_dn[$idx] && ";" != $this->_dn[$idx]) { |
|
128 | + break; |
|
129 | + } |
|
130 | + $idx++; |
|
131 | + $this->_skipWs($idx); |
|
132 | + } |
|
133 | + $offset = $idx; |
|
134 | + return array_reverse($names); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Parse 'name-component'. |
|
139 | - * |
|
140 | - * attributeTypeAndValue *("+" attributeTypeAndValue) |
|
141 | - * |
|
142 | - * @param int $offset |
|
143 | - * @return array Array of [type, value] tuples |
|
144 | - */ |
|
145 | - private function _parseNameComponent(int &$offset): array |
|
146 | - { |
|
147 | - $idx = $offset; |
|
148 | - $tvpairs = array(); |
|
149 | - while ($idx < $this->_len) { |
|
150 | - $tvpairs[] = $this->_parseAttrTypeAndValue($idx); |
|
151 | - $this->_skipWs($idx); |
|
152 | - if ($idx >= $this->_len || "+" != $this->_dn[$idx]) { |
|
153 | - break; |
|
154 | - } |
|
155 | - ++$idx; |
|
156 | - $this->_skipWs($idx); |
|
157 | - } |
|
158 | - $offset = $idx; |
|
159 | - return $tvpairs; |
|
160 | - } |
|
137 | + /** |
|
138 | + * Parse 'name-component'. |
|
139 | + * |
|
140 | + * attributeTypeAndValue *("+" attributeTypeAndValue) |
|
141 | + * |
|
142 | + * @param int $offset |
|
143 | + * @return array Array of [type, value] tuples |
|
144 | + */ |
|
145 | + private function _parseNameComponent(int &$offset): array |
|
146 | + { |
|
147 | + $idx = $offset; |
|
148 | + $tvpairs = array(); |
|
149 | + while ($idx < $this->_len) { |
|
150 | + $tvpairs[] = $this->_parseAttrTypeAndValue($idx); |
|
151 | + $this->_skipWs($idx); |
|
152 | + if ($idx >= $this->_len || "+" != $this->_dn[$idx]) { |
|
153 | + break; |
|
154 | + } |
|
155 | + ++$idx; |
|
156 | + $this->_skipWs($idx); |
|
157 | + } |
|
158 | + $offset = $idx; |
|
159 | + return $tvpairs; |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * Parse 'attributeTypeAndValue'. |
|
164 | - * |
|
165 | - * attributeType "=" attributeValue |
|
166 | - * |
|
167 | - * @param int $offset |
|
168 | - * @throws \UnexpectedValueException |
|
169 | - * @return array A tuple of [type, value]. Value may be either a string or |
|
170 | - * an Element, if it's encoded as hexstring. |
|
171 | - */ |
|
172 | - private function _parseAttrTypeAndValue(int &$offset): array |
|
173 | - { |
|
174 | - $idx = $offset; |
|
175 | - $type = $this->_parseAttrType($idx); |
|
176 | - $this->_skipWs($idx); |
|
177 | - if ($idx >= $this->_len || "=" != $this->_dn[$idx++]) { |
|
178 | - throw new \UnexpectedValueException("Invalid type and value pair."); |
|
179 | - } |
|
180 | - $this->_skipWs($idx); |
|
181 | - // hexstring |
|
182 | - if ($idx < $this->_len && "#" == $this->_dn[$idx]) { |
|
183 | - ++$idx; |
|
184 | - $data = $this->_parseAttrHexValue($idx); |
|
185 | - try { |
|
186 | - $value = Element::fromDER($data); |
|
187 | - } catch (DecodeException $e) { |
|
188 | - throw new \UnexpectedValueException( |
|
189 | - "Invalid DER encoding from hexstring.", 0, $e); |
|
190 | - } |
|
191 | - } else { |
|
192 | - $value = $this->_parseAttrStringValue($idx); |
|
193 | - } |
|
194 | - $offset = $idx; |
|
195 | - return array($type, $value); |
|
196 | - } |
|
162 | + /** |
|
163 | + * Parse 'attributeTypeAndValue'. |
|
164 | + * |
|
165 | + * attributeType "=" attributeValue |
|
166 | + * |
|
167 | + * @param int $offset |
|
168 | + * @throws \UnexpectedValueException |
|
169 | + * @return array A tuple of [type, value]. Value may be either a string or |
|
170 | + * an Element, if it's encoded as hexstring. |
|
171 | + */ |
|
172 | + private function _parseAttrTypeAndValue(int &$offset): array |
|
173 | + { |
|
174 | + $idx = $offset; |
|
175 | + $type = $this->_parseAttrType($idx); |
|
176 | + $this->_skipWs($idx); |
|
177 | + if ($idx >= $this->_len || "=" != $this->_dn[$idx++]) { |
|
178 | + throw new \UnexpectedValueException("Invalid type and value pair."); |
|
179 | + } |
|
180 | + $this->_skipWs($idx); |
|
181 | + // hexstring |
|
182 | + if ($idx < $this->_len && "#" == $this->_dn[$idx]) { |
|
183 | + ++$idx; |
|
184 | + $data = $this->_parseAttrHexValue($idx); |
|
185 | + try { |
|
186 | + $value = Element::fromDER($data); |
|
187 | + } catch (DecodeException $e) { |
|
188 | + throw new \UnexpectedValueException( |
|
189 | + "Invalid DER encoding from hexstring.", 0, $e); |
|
190 | + } |
|
191 | + } else { |
|
192 | + $value = $this->_parseAttrStringValue($idx); |
|
193 | + } |
|
194 | + $offset = $idx; |
|
195 | + return array($type, $value); |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * Parse 'attributeType'. |
|
200 | - * |
|
201 | - * (ALPHA 1*keychar) / oid |
|
202 | - * |
|
203 | - * @param int $offset |
|
204 | - * @throws \UnexpectedValueException |
|
205 | - * @return string |
|
206 | - */ |
|
207 | - private function _parseAttrType(int &$offset): string |
|
208 | - { |
|
209 | - $idx = $offset; |
|
210 | - // dotted OID |
|
211 | - $type = $this->_regexMatch('/^(?:oid\.)?([0-9]+(?:\.[0-9]+)*)/i', $idx); |
|
212 | - if (null === $type) { |
|
213 | - // name |
|
214 | - $type = $this->_regexMatch('/^[a-z][a-z0-9\-]*/i', $idx); |
|
215 | - if (null === $type) { |
|
216 | - throw new \UnexpectedValueException("Invalid attribute type."); |
|
217 | - } |
|
218 | - } |
|
219 | - $offset = $idx; |
|
220 | - return $type; |
|
221 | - } |
|
198 | + /** |
|
199 | + * Parse 'attributeType'. |
|
200 | + * |
|
201 | + * (ALPHA 1*keychar) / oid |
|
202 | + * |
|
203 | + * @param int $offset |
|
204 | + * @throws \UnexpectedValueException |
|
205 | + * @return string |
|
206 | + */ |
|
207 | + private function _parseAttrType(int &$offset): string |
|
208 | + { |
|
209 | + $idx = $offset; |
|
210 | + // dotted OID |
|
211 | + $type = $this->_regexMatch('/^(?:oid\.)?([0-9]+(?:\.[0-9]+)*)/i', $idx); |
|
212 | + if (null === $type) { |
|
213 | + // name |
|
214 | + $type = $this->_regexMatch('/^[a-z][a-z0-9\-]*/i', $idx); |
|
215 | + if (null === $type) { |
|
216 | + throw new \UnexpectedValueException("Invalid attribute type."); |
|
217 | + } |
|
218 | + } |
|
219 | + $offset = $idx; |
|
220 | + return $type; |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * Parse 'attributeValue' of string type. |
|
225 | - * |
|
226 | - * @param int $offset |
|
227 | - * @throws \UnexpectedValueException |
|
228 | - * @return string |
|
229 | - */ |
|
230 | - private function _parseAttrStringValue(int &$offset): string |
|
231 | - { |
|
232 | - $idx = $offset; |
|
233 | - if ($idx >= $this->_len) { |
|
234 | - return ""; |
|
235 | - } |
|
236 | - if ('"' == $this->_dn[$idx]) { // quoted string |
|
237 | - $val = $this->_parseQuotedAttrString($idx); |
|
238 | - } else { // string |
|
239 | - $val = $this->_parseAttrString($idx); |
|
240 | - } |
|
241 | - $offset = $idx; |
|
242 | - return $val; |
|
243 | - } |
|
223 | + /** |
|
224 | + * Parse 'attributeValue' of string type. |
|
225 | + * |
|
226 | + * @param int $offset |
|
227 | + * @throws \UnexpectedValueException |
|
228 | + * @return string |
|
229 | + */ |
|
230 | + private function _parseAttrStringValue(int &$offset): string |
|
231 | + { |
|
232 | + $idx = $offset; |
|
233 | + if ($idx >= $this->_len) { |
|
234 | + return ""; |
|
235 | + } |
|
236 | + if ('"' == $this->_dn[$idx]) { // quoted string |
|
237 | + $val = $this->_parseQuotedAttrString($idx); |
|
238 | + } else { // string |
|
239 | + $val = $this->_parseAttrString($idx); |
|
240 | + } |
|
241 | + $offset = $idx; |
|
242 | + return $val; |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * Parse plain 'attributeValue' string. |
|
247 | - * |
|
248 | - * @param int $offset |
|
249 | - * @throws \UnexpectedValueException |
|
250 | - * @return string |
|
251 | - */ |
|
252 | - private function _parseAttrString(int &$offset): string |
|
253 | - { |
|
254 | - $idx = $offset; |
|
255 | - $val = ""; |
|
256 | - $wsidx = null; |
|
257 | - while ($idx < $this->_len) { |
|
258 | - $c = $this->_dn[$idx]; |
|
259 | - // pair (escape sequence) |
|
260 | - if ("\\" == $c) { |
|
261 | - ++$idx; |
|
262 | - $val .= $this->_parsePairAfterSlash($idx); |
|
263 | - $wsidx = null; |
|
264 | - continue; |
|
265 | - } else if ('"' == $c) { |
|
266 | - throw new \UnexpectedValueException("Unexpected quotation."); |
|
267 | - } else if (false !== strpos(self::SPECIAL_CHARS, $c)) { |
|
268 | - break; |
|
269 | - } |
|
270 | - // keep track of the first consecutive whitespace |
|
271 | - if (' ' == $c) { |
|
272 | - if (null === $wsidx) { |
|
273 | - $wsidx = $idx; |
|
274 | - } |
|
275 | - } else { |
|
276 | - $wsidx = null; |
|
277 | - } |
|
278 | - // stringchar |
|
279 | - $val .= $c; |
|
280 | - ++$idx; |
|
281 | - } |
|
282 | - // if there was non-escaped whitespace in the end of the value |
|
283 | - if (null !== $wsidx) { |
|
284 | - $val = substr($val, 0, -($idx - $wsidx)); |
|
285 | - } |
|
286 | - $offset = $idx; |
|
287 | - return $val; |
|
288 | - } |
|
245 | + /** |
|
246 | + * Parse plain 'attributeValue' string. |
|
247 | + * |
|
248 | + * @param int $offset |
|
249 | + * @throws \UnexpectedValueException |
|
250 | + * @return string |
|
251 | + */ |
|
252 | + private function _parseAttrString(int &$offset): string |
|
253 | + { |
|
254 | + $idx = $offset; |
|
255 | + $val = ""; |
|
256 | + $wsidx = null; |
|
257 | + while ($idx < $this->_len) { |
|
258 | + $c = $this->_dn[$idx]; |
|
259 | + // pair (escape sequence) |
|
260 | + if ("\\" == $c) { |
|
261 | + ++$idx; |
|
262 | + $val .= $this->_parsePairAfterSlash($idx); |
|
263 | + $wsidx = null; |
|
264 | + continue; |
|
265 | + } else if ('"' == $c) { |
|
266 | + throw new \UnexpectedValueException("Unexpected quotation."); |
|
267 | + } else if (false !== strpos(self::SPECIAL_CHARS, $c)) { |
|
268 | + break; |
|
269 | + } |
|
270 | + // keep track of the first consecutive whitespace |
|
271 | + if (' ' == $c) { |
|
272 | + if (null === $wsidx) { |
|
273 | + $wsidx = $idx; |
|
274 | + } |
|
275 | + } else { |
|
276 | + $wsidx = null; |
|
277 | + } |
|
278 | + // stringchar |
|
279 | + $val .= $c; |
|
280 | + ++$idx; |
|
281 | + } |
|
282 | + // if there was non-escaped whitespace in the end of the value |
|
283 | + if (null !== $wsidx) { |
|
284 | + $val = substr($val, 0, -($idx - $wsidx)); |
|
285 | + } |
|
286 | + $offset = $idx; |
|
287 | + return $val; |
|
288 | + } |
|
289 | 289 | |
290 | - /** |
|
291 | - * Parse quoted 'attributeValue' string. |
|
292 | - * |
|
293 | - * @param int $offset Offset to starting quote |
|
294 | - * @throws \UnexpectedValueException |
|
295 | - * @return string |
|
296 | - */ |
|
297 | - private function _parseQuotedAttrString(int &$offset): string |
|
298 | - { |
|
299 | - $idx = $offset + 1; |
|
300 | - $val = ""; |
|
301 | - while ($idx < $this->_len) { |
|
302 | - $c = $this->_dn[$idx]; |
|
303 | - if ("\\" == $c) { // pair |
|
304 | - ++$idx; |
|
305 | - $val .= $this->_parsePairAfterSlash($idx); |
|
306 | - continue; |
|
307 | - } else if ('"' == $c) { |
|
308 | - ++$idx; |
|
309 | - break; |
|
310 | - } |
|
311 | - $val .= $c; |
|
312 | - ++$idx; |
|
313 | - } |
|
314 | - $offset = $idx; |
|
315 | - return $val; |
|
316 | - } |
|
290 | + /** |
|
291 | + * Parse quoted 'attributeValue' string. |
|
292 | + * |
|
293 | + * @param int $offset Offset to starting quote |
|
294 | + * @throws \UnexpectedValueException |
|
295 | + * @return string |
|
296 | + */ |
|
297 | + private function _parseQuotedAttrString(int &$offset): string |
|
298 | + { |
|
299 | + $idx = $offset + 1; |
|
300 | + $val = ""; |
|
301 | + while ($idx < $this->_len) { |
|
302 | + $c = $this->_dn[$idx]; |
|
303 | + if ("\\" == $c) { // pair |
|
304 | + ++$idx; |
|
305 | + $val .= $this->_parsePairAfterSlash($idx); |
|
306 | + continue; |
|
307 | + } else if ('"' == $c) { |
|
308 | + ++$idx; |
|
309 | + break; |
|
310 | + } |
|
311 | + $val .= $c; |
|
312 | + ++$idx; |
|
313 | + } |
|
314 | + $offset = $idx; |
|
315 | + return $val; |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * Parse 'attributeValue' of binary type. |
|
320 | - * |
|
321 | - * @param int $offset |
|
322 | - * @throws \UnexpectedValueException |
|
323 | - * @return string |
|
324 | - */ |
|
325 | - private function _parseAttrHexValue(int &$offset): string |
|
326 | - { |
|
327 | - $idx = $offset; |
|
328 | - $hexstr = $this->_regexMatch('/^(?:[0-9a-f]{2})+/i', $idx); |
|
329 | - if (null === $hexstr) { |
|
330 | - throw new \UnexpectedValueException("Invalid hexstring."); |
|
331 | - } |
|
332 | - $data = hex2bin($hexstr); |
|
333 | - $offset = $idx; |
|
334 | - return $data; |
|
335 | - } |
|
318 | + /** |
|
319 | + * Parse 'attributeValue' of binary type. |
|
320 | + * |
|
321 | + * @param int $offset |
|
322 | + * @throws \UnexpectedValueException |
|
323 | + * @return string |
|
324 | + */ |
|
325 | + private function _parseAttrHexValue(int &$offset): string |
|
326 | + { |
|
327 | + $idx = $offset; |
|
328 | + $hexstr = $this->_regexMatch('/^(?:[0-9a-f]{2})+/i', $idx); |
|
329 | + if (null === $hexstr) { |
|
330 | + throw new \UnexpectedValueException("Invalid hexstring."); |
|
331 | + } |
|
332 | + $data = hex2bin($hexstr); |
|
333 | + $offset = $idx; |
|
334 | + return $data; |
|
335 | + } |
|
336 | 336 | |
337 | - /** |
|
338 | - * Parse 'pair' after leading slash. |
|
339 | - * |
|
340 | - * @param int $offset |
|
341 | - * @throws \UnexpectedValueException |
|
342 | - * @return string |
|
343 | - */ |
|
344 | - private function _parsePairAfterSlash(int &$offset): string |
|
345 | - { |
|
346 | - $idx = $offset; |
|
347 | - if ($idx >= $this->_len) { |
|
348 | - throw new \UnexpectedValueException( |
|
349 | - "Unexpected end of escape sequence."); |
|
350 | - } |
|
351 | - $c = $this->_dn[$idx++]; |
|
352 | - // special | \ | " | SPACE |
|
353 | - if (false !== strpos(self::SPECIAL_CHARS . '\\" ', $c)) { |
|
354 | - $val = $c; |
|
355 | - } else { // hexpair |
|
356 | - if ($idx >= $this->_len) { |
|
357 | - throw new \UnexpectedValueException("Unexpected end of hexpair."); |
|
358 | - } |
|
359 | - $val = @hex2bin($c . $this->_dn[$idx++]); |
|
360 | - if (false === $val) { |
|
361 | - throw new \UnexpectedValueException("Invalid hexpair."); |
|
362 | - } |
|
363 | - } |
|
364 | - $offset = $idx; |
|
365 | - return $val; |
|
366 | - } |
|
337 | + /** |
|
338 | + * Parse 'pair' after leading slash. |
|
339 | + * |
|
340 | + * @param int $offset |
|
341 | + * @throws \UnexpectedValueException |
|
342 | + * @return string |
|
343 | + */ |
|
344 | + private function _parsePairAfterSlash(int &$offset): string |
|
345 | + { |
|
346 | + $idx = $offset; |
|
347 | + if ($idx >= $this->_len) { |
|
348 | + throw new \UnexpectedValueException( |
|
349 | + "Unexpected end of escape sequence."); |
|
350 | + } |
|
351 | + $c = $this->_dn[$idx++]; |
|
352 | + // special | \ | " | SPACE |
|
353 | + if (false !== strpos(self::SPECIAL_CHARS . '\\" ', $c)) { |
|
354 | + $val = $c; |
|
355 | + } else { // hexpair |
|
356 | + if ($idx >= $this->_len) { |
|
357 | + throw new \UnexpectedValueException("Unexpected end of hexpair."); |
|
358 | + } |
|
359 | + $val = @hex2bin($c . $this->_dn[$idx++]); |
|
360 | + if (false === $val) { |
|
361 | + throw new \UnexpectedValueException("Invalid hexpair."); |
|
362 | + } |
|
363 | + } |
|
364 | + $offset = $idx; |
|
365 | + return $val; |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * Match DN to pattern and extract the last capture group. |
|
370 | - * |
|
371 | - * Updates offset to fully matched pattern. |
|
372 | - * |
|
373 | - * @param string $pattern |
|
374 | - * @param int $offset |
|
375 | - * @return string|null Null if pattern doesn't match |
|
376 | - */ |
|
377 | - private function _regexMatch(string $pattern, int &$offset) |
|
378 | - { |
|
379 | - $idx = $offset; |
|
380 | - if (!preg_match($pattern, substr($this->_dn, $idx), $match)) { |
|
381 | - return null; |
|
382 | - } |
|
383 | - $idx += strlen($match[0]); |
|
384 | - $offset = $idx; |
|
385 | - return end($match); |
|
386 | - } |
|
368 | + /** |
|
369 | + * Match DN to pattern and extract the last capture group. |
|
370 | + * |
|
371 | + * Updates offset to fully matched pattern. |
|
372 | + * |
|
373 | + * @param string $pattern |
|
374 | + * @param int $offset |
|
375 | + * @return string|null Null if pattern doesn't match |
|
376 | + */ |
|
377 | + private function _regexMatch(string $pattern, int &$offset) |
|
378 | + { |
|
379 | + $idx = $offset; |
|
380 | + if (!preg_match($pattern, substr($this->_dn, $idx), $match)) { |
|
381 | + return null; |
|
382 | + } |
|
383 | + $idx += strlen($match[0]); |
|
384 | + $offset = $idx; |
|
385 | + return end($match); |
|
386 | + } |
|
387 | 387 | |
388 | - /** |
|
389 | - * Skip consecutive spaces. |
|
390 | - * |
|
391 | - * @param int $offset |
|
392 | - */ |
|
393 | - private function _skipWs(int &$offset) |
|
394 | - { |
|
395 | - $idx = $offset; |
|
396 | - while ($idx < $this->_len) { |
|
397 | - if (" " != $this->_dn[$idx]) { |
|
398 | - break; |
|
399 | - } |
|
400 | - ++$idx; |
|
401 | - } |
|
402 | - $offset = $idx; |
|
403 | - } |
|
388 | + /** |
|
389 | + * Skip consecutive spaces. |
|
390 | + * |
|
391 | + * @param int $offset |
|
392 | + */ |
|
393 | + private function _skipWs(int &$offset) |
|
394 | + { |
|
395 | + $idx = $offset; |
|
396 | + while ($idx < $this->_len) { |
|
397 | + if (" " != $this->_dn[$idx]) { |
|
398 | + break; |
|
399 | + } |
|
400 | + ++$idx; |
|
401 | + } |
|
402 | + $offset = $idx; |
|
403 | + } |
|
404 | 404 | } |
@@ -11,77 +11,77 @@ |
||
11 | 11 | */ |
12 | 12 | class StringPreparer |
13 | 13 | { |
14 | - const STEP_TRANSCODE = 1; |
|
15 | - const STEP_MAP = 2; |
|
16 | - const STEP_NORMALIZE = 3; |
|
17 | - const STEP_PROHIBIT = 4; |
|
18 | - const STEP_CHECK_BIDI = 5; |
|
19 | - const STEP_INSIGNIFICANT_CHARS = 6; |
|
14 | + const STEP_TRANSCODE = 1; |
|
15 | + const STEP_MAP = 2; |
|
16 | + const STEP_NORMALIZE = 3; |
|
17 | + const STEP_PROHIBIT = 4; |
|
18 | + const STEP_CHECK_BIDI = 5; |
|
19 | + const STEP_INSIGNIFICANT_CHARS = 6; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Preparation steps. |
|
23 | - * |
|
24 | - * @var PrepareStep[] $_steps |
|
25 | - */ |
|
26 | - protected $_steps; |
|
21 | + /** |
|
22 | + * Preparation steps. |
|
23 | + * |
|
24 | + * @var PrepareStep[] $_steps |
|
25 | + */ |
|
26 | + protected $_steps; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param PrepareStep[] $steps Preparation steps to apply |
|
32 | - */ |
|
33 | - protected function __construct(array $steps) |
|
34 | - { |
|
35 | - $this->_steps = $steps; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param PrepareStep[] $steps Preparation steps to apply |
|
32 | + */ |
|
33 | + protected function __construct(array $steps) |
|
34 | + { |
|
35 | + $this->_steps = $steps; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Get default instance for given string type. |
|
40 | - * |
|
41 | - * @param int $string_type ASN.1 string type tag. |
|
42 | - * @return self |
|
43 | - */ |
|
44 | - public static function forStringType(int $string_type): self |
|
45 | - { |
|
46 | - $steps = array( |
|
47 | - /* @formatter:off */ |
|
48 | - self::STEP_TRANSCODE => new TranscodeStep($string_type), |
|
49 | - self::STEP_MAP => new MapStep(), |
|
50 | - self::STEP_NORMALIZE => new NormalizeStep(), |
|
51 | - self::STEP_PROHIBIT => new ProhibitStep(), |
|
52 | - self::STEP_CHECK_BIDI => new CheckBidiStep(), |
|
53 | - // @todo Vary by string type |
|
54 | - self::STEP_INSIGNIFICANT_CHARS => |
|
55 | - new InsignificantNonSubstringSpaceStep() |
|
56 | - /* @formatter:on */ |
|
57 | - ); |
|
58 | - return new self($steps); |
|
59 | - } |
|
38 | + /** |
|
39 | + * Get default instance for given string type. |
|
40 | + * |
|
41 | + * @param int $string_type ASN.1 string type tag. |
|
42 | + * @return self |
|
43 | + */ |
|
44 | + public static function forStringType(int $string_type): self |
|
45 | + { |
|
46 | + $steps = array( |
|
47 | + /* @formatter:off */ |
|
48 | + self::STEP_TRANSCODE => new TranscodeStep($string_type), |
|
49 | + self::STEP_MAP => new MapStep(), |
|
50 | + self::STEP_NORMALIZE => new NormalizeStep(), |
|
51 | + self::STEP_PROHIBIT => new ProhibitStep(), |
|
52 | + self::STEP_CHECK_BIDI => new CheckBidiStep(), |
|
53 | + // @todo Vary by string type |
|
54 | + self::STEP_INSIGNIFICANT_CHARS => |
|
55 | + new InsignificantNonSubstringSpaceStep() |
|
56 | + /* @formatter:on */ |
|
57 | + ); |
|
58 | + return new self($steps); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Get self with case folding set. |
|
63 | - * |
|
64 | - * @param bool $fold True to apply case folding |
|
65 | - * @return self |
|
66 | - */ |
|
67 | - public function withCaseFolding(bool $fold): self |
|
68 | - { |
|
69 | - $obj = clone $this; |
|
70 | - $obj->_steps[self::STEP_MAP] = new MapStep($fold); |
|
71 | - return $obj; |
|
72 | - } |
|
61 | + /** |
|
62 | + * Get self with case folding set. |
|
63 | + * |
|
64 | + * @param bool $fold True to apply case folding |
|
65 | + * @return self |
|
66 | + */ |
|
67 | + public function withCaseFolding(bool $fold): self |
|
68 | + { |
|
69 | + $obj = clone $this; |
|
70 | + $obj->_steps[self::STEP_MAP] = new MapStep($fold); |
|
71 | + return $obj; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Prepare string. |
|
76 | - * |
|
77 | - * @param string $string |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public function prepare(string $string): string |
|
81 | - { |
|
82 | - foreach ($this->_steps as $step) { |
|
83 | - $string = $step->apply($string); |
|
84 | - } |
|
85 | - return $string; |
|
86 | - } |
|
74 | + /** |
|
75 | + * Prepare string. |
|
76 | + * |
|
77 | + * @param string $string |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public function prepare(string $string): string |
|
81 | + { |
|
82 | + foreach ($this->_steps as $step) { |
|
83 | + $string = $step->apply($string); |
|
84 | + } |
|
85 | + return $string; |
|
86 | + } |
|
87 | 87 | } |
@@ -12,13 +12,13 @@ |
||
12 | 12 | */ |
13 | 13 | class NormalizeStep implements PrepareStep |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * @param string $string UTF-8 encoded string |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - public function apply(string $string): string |
|
21 | - { |
|
22 | - return normalizer_normalize($string, \Normalizer::NFKC); |
|
23 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * @param string $string UTF-8 encoded string |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + public function apply(string $string): string |
|
21 | + { |
|
22 | + return normalizer_normalize($string, \Normalizer::NFKC); |
|
23 | + } |
|
24 | 24 | } |
@@ -15,51 +15,51 @@ |
||
15 | 15 | */ |
16 | 16 | class TranscodeStep implements PrepareStep |
17 | 17 | { |
18 | - /** |
|
19 | - * ASN.1 type of the string. |
|
20 | - * |
|
21 | - * @var int $_type |
|
22 | - */ |
|
23 | - protected $_type; |
|
18 | + /** |
|
19 | + * ASN.1 type of the string. |
|
20 | + * |
|
21 | + * @var int $_type |
|
22 | + */ |
|
23 | + protected $_type; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param int $type ASN.1 type tag of the string |
|
29 | - */ |
|
30 | - public function __construct(int $type) |
|
31 | - { |
|
32 | - $this->_type = $type; |
|
33 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param int $type ASN.1 type tag of the string |
|
29 | + */ |
|
30 | + public function __construct(int $type) |
|
31 | + { |
|
32 | + $this->_type = $type; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * |
|
37 | - * @throws \LogicException If string type is not supported |
|
38 | - * @param string $string String to prepare |
|
39 | - * @return string UTF-8 encoded string |
|
40 | - */ |
|
41 | - public function apply(string $string): string |
|
42 | - { |
|
43 | - switch ($this->_type) { |
|
44 | - // UTF-8 string as is |
|
45 | - case Element::TYPE_UTF8_STRING: |
|
46 | - return $string; |
|
47 | - // PrintableString maps directly to UTF-8 |
|
48 | - case Element::TYPE_PRINTABLE_STRING: |
|
49 | - return $string; |
|
50 | - // UCS-2 to UTF-8 |
|
51 | - case Element::TYPE_BMP_STRING: |
|
52 | - return mb_convert_encoding($string, "UTF-8", "UCS-2BE"); |
|
53 | - // UCS-4 to UTF-8 |
|
54 | - case Element::TYPE_UNIVERSAL_STRING: |
|
55 | - return mb_convert_encoding($string, "UTF-8", "UCS-4BE"); |
|
56 | - // TeletexString mapping is a local matter. |
|
57 | - // We take a shortcut here and encode it as a hexstring. |
|
58 | - case Element::TYPE_T61_STRING: |
|
59 | - $el = new T61String($string); |
|
60 | - return "#" . bin2hex($el->toDER()); |
|
61 | - } |
|
62 | - throw new \LogicException( |
|
63 | - "Unsupported string type " . Element::tagToName($this->_type) . "."); |
|
64 | - } |
|
35 | + /** |
|
36 | + * |
|
37 | + * @throws \LogicException If string type is not supported |
|
38 | + * @param string $string String to prepare |
|
39 | + * @return string UTF-8 encoded string |
|
40 | + */ |
|
41 | + public function apply(string $string): string |
|
42 | + { |
|
43 | + switch ($this->_type) { |
|
44 | + // UTF-8 string as is |
|
45 | + case Element::TYPE_UTF8_STRING: |
|
46 | + return $string; |
|
47 | + // PrintableString maps directly to UTF-8 |
|
48 | + case Element::TYPE_PRINTABLE_STRING: |
|
49 | + return $string; |
|
50 | + // UCS-2 to UTF-8 |
|
51 | + case Element::TYPE_BMP_STRING: |
|
52 | + return mb_convert_encoding($string, "UTF-8", "UCS-2BE"); |
|
53 | + // UCS-4 to UTF-8 |
|
54 | + case Element::TYPE_UNIVERSAL_STRING: |
|
55 | + return mb_convert_encoding($string, "UTF-8", "UCS-4BE"); |
|
56 | + // TeletexString mapping is a local matter. |
|
57 | + // We take a shortcut here and encode it as a hexstring. |
|
58 | + case Element::TYPE_T61_STRING: |
|
59 | + $el = new T61String($string); |
|
60 | + return "#" . bin2hex($el->toDER()); |
|
61 | + } |
|
62 | + throw new \LogicException( |
|
63 | + "Unsupported string type " . Element::tagToName($this->_type) . "."); |
|
64 | + } |
|
65 | 65 | } |