Rev 3 | Rev 13 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OID-Utilities for PHP |
||
12 | daniel-mar | 5 | * Copyright 2011-2020 Daniel Marschall, ViaThinkSoft |
6 | * Version 2020-06-11 |
||
3 | daniel-mar | 7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
21 | // All functions in this library are compatible with leading zeroes (not recommended) and leading dots |
||
22 | |||
12 | daniel-mar | 23 | // TODO: change some function names, so that they have a uniform naming schema, and rename "oid identifier" into "asn.1 alphanumeric identifier" |
24 | // oid_id_is_valid() => asn1_alpha_id_valid() |
||
3 | daniel-mar | 25 | |
26 | define('OID_DOT_FORBIDDEN', 0); |
||
27 | define('OID_DOT_OPTIONAL', 1); |
||
28 | define('OID_DOT_REQUIRED', 2); |
||
29 | |||
30 | /** |
||
31 | * Checks if an OID has a valid dot notation. |
||
32 | * @author Daniel Marschall, ViaThinkSoft |
||
33 | * @version 2014-12-09 |
||
34 | * @param $oid (string)<br /> |
||
35 | * An OID in dot notation. |
||
36 | * @param $allow_leading_zeroes (bool)<br /> |
||
37 | * true of leading zeroes are allowed or not. |
||
38 | * @param $allow_leading_dot (bool)<br /> |
||
39 | * true of leading dots are allowed or not. |
||
40 | * @return (bool) true if the dot notation is valid. |
||
41 | **/ |
||
42 | function oid_valid_dotnotation($oid, $allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) { |
||
43 | $regex = oid_validation_regex($allow_leading_zeroes, $allow_leading_dot, $min_len); |
||
44 | |||
45 | return preg_match($regex, $oid, $m) ? true : false; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Returns a full regular expression to validate an OID in dot-notation |
||
50 | * @author Daniel Marschall, ViaThinkSoft |
||
51 | * @version 2014-12-09 |
||
52 | * @param $allow_leading_zeroes (bool)<br /> |
||
53 | * true of leading zeroes are allowed or not. |
||
54 | * @param $allow_leading_dot (bool)<br /> |
||
55 | * true of leading dots are allowed or not. |
||
56 | * @return (string) The regular expression |
||
57 | **/ |
||
58 | function oid_validation_regex($allow_leading_zeroes=true, $allow_leading_dot=false, $min_len=0) { |
||
59 | $leading_dot_policy = $allow_leading_dot ? OID_DOT_OPTIONAL : OID_DOT_FORBIDDEN; |
||
60 | |||
61 | $part_regex = oid_part_regex($min_len, $allow_leading_zeroes, $leading_dot_policy); |
||
62 | |||
63 | return '@^'.$part_regex.'$@'; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Returns a partial regular expression which matches valid OIDs in dot notation. |
||
68 | * It can be inserted into regular expressions. |
||
69 | * @author Daniel Marschall, ViaThinkSoft |
||
70 | * @version 2014-12-09 |
||
71 | * @param $min_len (int)<br /> |
||
72 | * 0="." and greater will be recognized, but not ""<br /> |
||
73 | * 1=".2" and greater will be recognized<br /> |
||
74 | * 2=".2.999" and greater will be recognized (default)<br /> |
||
75 | * etc. |
||
76 | * @param $allow_leading_zeroes (bool)<br /> |
||
77 | * true: ".2.0999" will be recognized<br /> |
||
78 | * false: ".2.0999" won't be recognized (default) |
||
79 | * @param $leading_dot_policy (int)<br /> |
||
80 | * 0 (OID_DOT_FORBIDDEN): forbidden<br /> |
||
81 | * 1 (OID_DOT_OPTIONAL) : optional (default)<br /> |
||
82 | * 2 (OID_DOT_REQUIRED) : enforced |
||
83 | * @return (string) A regular expression which matches OIDs in dot notation |
||
84 | **/ |
||
85 | function oid_part_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL) { |
||
86 | switch ($leading_dot_policy) { |
||
87 | case 0: // forbidden |
||
88 | $lead_dot = ''; |
||
89 | break; |
||
90 | case 1: // optional |
||
91 | $lead_dot = '\\.{0,1}'; |
||
92 | break; |
||
93 | case 2: // enforced |
||
94 | $lead_dot = '\\.'; |
||
95 | break; |
||
96 | default: |
||
97 | assert(false); |
||
98 | break; |
||
99 | } |
||
100 | |||
101 | $lead_zero = $allow_leading_zeroes ? '0*' : ''; |
||
102 | $zero_till_thirtynine = '(([0-9])|([1-3][0-9]))'; // second arc is limited to 0..39 if root arc is 0..1 |
||
103 | $singledot_option = ($min_len == 0) && ($leading_dot_policy != OID_DOT_FORBIDDEN) ? '|\\.' : ''; |
||
104 | $only_root_option = ($min_len <= 1) ? '|('.$lead_dot.$lead_zero.'[0-2])' : ''; |
||
105 | |||
106 | $regex = ' |
||
107 | ( |
||
108 | ( |
||
109 | ( |
||
110 | ('.$lead_dot.$lead_zero.'[0-1]) |
||
111 | \\.'.$lead_zero.$zero_till_thirtynine.' |
||
112 | (\\.'.$lead_zero.'(0|[1-9][0-9]*)){'.max(0, $min_len-2).',} |
||
113 | )|( |
||
114 | ('.$lead_dot.$lead_zero.'[2]) |
||
115 | (\\.'.$lead_zero.'(0|[1-9][0-9]*)){'.max(0, $min_len-1).',} |
||
116 | ) |
||
117 | '.$only_root_option.' |
||
118 | '.$singledot_option.' |
||
119 | ) |
||
120 | )'; |
||
121 | |||
122 | // Remove the indentations which are used to maintain this large regular expression in a human friendly way |
||
123 | $regex = str_replace("\n", '', $regex); |
||
124 | $regex = str_replace("\r", '', $regex); |
||
125 | $regex = str_replace("\t", '', $regex); |
||
126 | $regex = str_replace(' ', '', $regex); |
||
127 | |||
128 | return $regex; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Searches all OIDs in $text and outputs them as array. |
||
133 | * @author Daniel Marschall, ViaThinkSoft |
||
134 | * @version 2014-12-09 |
||
135 | * @param $text (string)<br /> |
||
136 | * The text to be parsed |
||
137 | * @param $min_len (int)<br /> |
||
138 | * 0="." and greater will be recognized, but not ""<br /> |
||
139 | * 1=".2" and greater will be recognized<br /> |
||
140 | * 2=".2.999" and greater will be recognized (default)<br /> |
||
141 | * etc. |
||
142 | * @param $allow_leading_zeroes (bool)<br /> |
||
143 | * true: ".2.0999" will be recognized<br /> |
||
144 | * false: ".2.0999" won't be recognized (default) |
||
145 | * @param $leading_dot_policy (int)<br /> |
||
146 | * 0 (OID_DOT_FORBIDDEN): forbidden<br /> |
||
147 | * 1 (OID_DOT_OPTIONAL) : optional (default)<br /> |
||
148 | * 2 (OID_DOT_REQUIRED) : enforced |
||
149 | * @param $requires_whitespace_delimiters (bool)<br /> |
||
150 | * true: "2.999" will be recognized, as well as " 2.999 " (default)<br /> |
||
151 | * false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates) |
||
152 | * @return (array<string>) An array of OIDs in dot notation |
||
153 | **/ |
||
154 | function parse_oids($text, $min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) { |
||
155 | $regex = oid_detection_regex($min_len, $allow_leading_zeroes, $leading_dot_policy, $requires_whitespace_delimiters); |
||
156 | |||
157 | preg_match_all($regex, $text, $matches); |
||
158 | return $matches[1]; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns a full regular expression for detecting OIDs in dot notation inside a text. |
||
163 | * @author Daniel Marschall, ViaThinkSoft |
||
164 | * @version 2014-12-09 |
||
165 | * @param $min_len (int)<br /> |
||
166 | * 0="." and greater will be recognized, but not ""<br /> |
||
167 | * 1=".2" and greater will be recognized<br /> |
||
168 | * 2=".2.999" and greater will be recognized (default)<br /> |
||
169 | * etc. |
||
170 | * @param $allow_leading_zeroes (bool)<br /> |
||
171 | * true: ".2.0999" will be recognized<br /> |
||
172 | * false: ".2.0999" won't be recognized (default) |
||
173 | * @param $leading_dot_policy (int)<br /> |
||
174 | * 0 (OID_DOT_FORBIDDEN): forbidden<br /> |
||
175 | * 1 (OID_DOT_OPTIONAL) : optional (default)<br /> |
||
176 | * 2 (OID_DOT_REQUIRED) : enforced |
||
177 | * @param $requires_whitespace_delimiters (bool)<br /> |
||
178 | * true: "2.999" will be recognized, as well as " 2.999 " (default)<br /> |
||
179 | * false: "2.999!" will be reconigzed, as well as "2.999.c" (this might be used in in documentations with templates) |
||
180 | * @return (string) The regular expression |
||
181 | **/ |
||
182 | function oid_detection_regex($min_len=2, $allow_leading_zeroes=false, $leading_dot_policy=OID_DOT_OPTIONAL, $requires_whitespace_delimiters=true) { |
||
183 | if ($requires_whitespace_delimiters) { |
||
184 | // A fully qualified regular expression which can be used by preg_match() |
||
185 | $begin_condition = '(?<=^|\\s)'; |
||
186 | $end_condition = '(?=\\s|$)'; |
||
187 | } else { |
||
188 | // A partial expression which can be used inside another regular expression |
||
189 | $begin_condition = '(?<![\d])'; |
||
190 | $end_condition = '(?![\d])'; |
||
191 | } |
||
192 | |||
193 | $part_regex = oid_part_regex($min_len, $allow_leading_zeroes, $leading_dot_policy); |
||
194 | |||
195 | return '@'.$begin_condition.$part_regex.$end_condition.'@'; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Returns the parent of an OID in dot notation or the OID itself, if it is the root.<br /> |
||
200 | * Leading dots and leading zeroes are tolerated. |
||
201 | * @author Daniel Marschall, ViaThinkSoft |
||
202 | * @version 2014-12-16 |
||
203 | * @param $oid (string)<br /> |
||
204 | * An OID in dot notation. |
||
205 | * @return (string) The parent OID in dot notation. |
||
206 | **/ |
||
207 | function oid_up($oid) { |
||
208 | $oid = sanitizeOID($oid, 'auto'); |
||
209 | if ($oid === false) return false; |
||
210 | |||
211 | $p = strrpos($oid, '.'); |
||
212 | if ($p === false) return $oid; |
||
213 | if ($p == 0) return '.'; |
||
214 | |||
215 | return substr($oid, 0, $p); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Outputs the depth of an OID. |
||
220 | * @author Daniel Marschall, ViaThinkSoft |
||
221 | * @version 2014-12-09 |
||
222 | * @param $oid (string) An OID in dot notation (with or without leading dot) |
||
223 | * @return (int) The depth of the OID, e.g. 2.999 and .2.999 has the length 2. |
||
224 | **/ |
||
225 | function oid_len($oid) { |
||
226 | if ($oid == '') return 0; |
||
227 | if ($oid[0] == '.') $oid = substr($oid, 1); |
||
228 | return substr_count($oid, '.')+1; |
||
229 | } |
||
230 | function oid_depth($oid) { |
||
231 | return oid_len($oid); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Lists all parents of an OID. |
||
236 | * This function tolerates leading dots. The parent of '.' stays '.'. |
||
237 | * The OID will not be checked for validity! |
||
238 | * @author Daniel Marschall, ViaThinkSoft |
||
239 | * @version 2014-12-17 |
||
240 | * @param $oid (string)<br /> |
||
241 | * An OID in dot notation. |
||
242 | * @return (array<string>) An array with all parent OIDs. |
||
243 | **/ |
||
244 | function oid_parents($oid) { |
||
245 | $parents = array(); |
||
246 | |||
247 | while (oid_len($oid) > 1) { |
||
248 | $oid = oid_up($oid); |
||
249 | $parents[] = $oid; |
||
250 | } |
||
251 | |||
252 | if (substr($oid, 0, 1) == '.') $parents[] = '.'; |
||
253 | |||
254 | return $parents; |
||
255 | } |
||
256 | |||
257 | /* |
||
258 | assert(oid_parents('.1.2.999') == array('.1.2', '.1', '.')); |
||
259 | assert(oid_parents('1.2.999') == array('1.2', '1')); |
||
260 | assert(oid_parents('.') == array('.')); |
||
261 | assert(oid_parents('') == array()); |
||
262 | */ |
||
263 | |||
264 | /** |
||
265 | * Sorts an array containing OIDs in dot notation. |
||
266 | * @author Daniel Marschall, ViaThinkSoft |
||
267 | * @version 2014-12-09 |
||
268 | * @param $ary (array<string>)<br /> |
||
269 | * An array of OIDs in dot notation.<br /> |
||
270 | * This array will be changed by this method. |
||
271 | * @param $output_with_leading_dot (bool)<br /> |
||
272 | * true: The array will be normalized to OIDs with a leading dot. |
||
273 | * false: The array will be normalized to OIDs without a leading dot. (default) |
||
274 | * @return Nothing |
||
275 | **/ |
||
276 | function oidSort(&$ary, $output_with_leading_dot=false) { |
||
277 | $out = array(); |
||
278 | |||
279 | $none = $output_with_leading_dot ? '.' : ''; |
||
280 | |||
281 | $d = array(); |
||
282 | foreach ($ary as &$oid) { |
||
283 | if (($oid == '') || ($oid == '.')) { |
||
284 | $out[] = $none; |
||
285 | } else { |
||
286 | $oid = sanitizeOID($oid, 'auto'); // strike leading zeroes |
||
287 | $bry = explode('.', $oid, 2); |
||
288 | $firstarc = $bry[0]; |
||
289 | $rest = (isset($bry[1])) ? $bry[1] : ''; |
||
290 | $d[$firstarc][] = $rest; |
||
291 | } |
||
292 | } |
||
293 | unset($oid); |
||
294 | ksort($d); |
||
295 | |||
296 | foreach ($d as $firstarc => &$data) { |
||
297 | oidSort($data); |
||
298 | foreach ($data as &$rest) { |
||
299 | $out[] = ($output_with_leading_dot ? '.' : '')."$firstarc" . (($rest != $none) ? ".$rest" : ''); |
||
300 | } |
||
301 | } |
||
302 | unset($data); |
||
303 | |||
304 | $ary = $out; |
||
305 | } |
||
306 | |||
307 | /** |
||
12 | daniel-mar | 308 | * Checks if two OIDs in dot-notation are equal |
309 | * @author Daniel Marschall, ViaThinkSoft |
||
310 | * @version 2020-05-27 |
||
311 | * @param $oidA (string)<br /> |
||
312 | * First OID |
||
313 | * @param $oidB (string)<br /> |
||
314 | * Second OID |
||
315 | * @return (bool) True if the OIDs are equal |
||
316 | **/ |
||
317 | function oid_dotnotation_equal($oidA, $oidB) { |
||
318 | $oidA = sanitizeOID($oidA, false); |
||
319 | if ($oidA === false) return null; |
||
320 | |||
321 | $oidB = sanitizeOID($oidB, false); |
||
322 | if ($oidB === false) return null; |
||
323 | |||
324 | return $oidA === $oidB; |
||
325 | } |
||
326 | |||
327 | /** |
||
3 | daniel-mar | 328 | * Removes leading zeroes from an OID in dot notation. |
329 | * @author Daniel Marschall, ViaThinkSoft |
||
330 | * @version 2015-08-17 |
||
331 | * @param $oid (string)<br /> |
||
332 | * An OID in dot notation. |
||
333 | * @param $leading_dot (bool)<br /> |
||
334 | * true: The OID is valid, if it contains a leading dot.<br /> |
||
335 | * false (default): The OID is valid, if it does not contain a leading dot. |
||
336 | * 'auto: Allow both |
||
337 | * @return (mixed) The OID without leading dots, or <code>false</code> if the OID is syntactically wrong. |
||
338 | **/ |
||
339 | $oid_sanitize_cache = array(); |
||
340 | function sanitizeOID($oid, $leading_dot=false) { |
||
341 | if ($leading_dot) $leading_dot = substr($oid,0,1) == '.'; |
||
342 | |||
343 | // We are using a cache, since this function is used very often by OID+ |
||
344 | global $oid_sanitize_cache; |
||
345 | $v = ($leading_dot ? 'T' : 'F').$oid; |
||
346 | if (isset($oid_sanitize_cache[$v])) return $oid_sanitize_cache[$v]; |
||
347 | |||
348 | if ($leading_dot) { |
||
349 | if ($oid == '.') return ''; |
||
350 | } else { |
||
351 | if ($oid == '') return ''; |
||
352 | } |
||
353 | |||
354 | $out = ''; |
||
355 | $ary = explode('.', $oid); |
||
356 | foreach ($ary as $n => &$a) { |
||
357 | if (($leading_dot) && ($n == 0)) { |
||
358 | if ($a != '') return false; |
||
359 | continue; |
||
360 | } |
||
361 | |||
362 | if (!ctype_digit($a)) return false; // does contain something other than digits |
||
363 | |||
364 | // strike leading zeroes |
||
365 | $a = preg_replace("@^0+@", '', $a); |
||
366 | if ($a == '') $a = 0; |
||
367 | |||
368 | if (($leading_dot) || ($n != 0)) $out .= '.'; |
||
369 | $out .= $a; |
||
370 | } |
||
371 | unset($a); |
||
372 | unset($ary); |
||
373 | |||
374 | $oid_sanitize_cache[$v] = $out; |
||
375 | return $out; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Shows the top arc of an OID. |
||
380 | * This function tolerates leading dots. |
||
381 | * @author Daniel Marschall, ViaThinkSoft |
||
382 | * @version 2014-12-16 |
||
383 | * @param $oid (string)<br /> |
||
384 | * An OID in dot notation. |
||
385 | * @return (mixed) The top arc of the OID or empty string if it is already the root ('.') |
||
386 | **/ |
||
387 | function oid_toparc($oid) { |
||
388 | $leadingdot = substr($oid,0,1) == '.'; |
||
389 | |||
390 | $oid = sanitizeOID($oid, $leadingdot); |
||
391 | if ($oid === false) return false; |
||
392 | |||
393 | if (!$leadingdot) $oid = '.'.$oid; |
||
394 | |||
395 | $p = strrpos($oid, '.'); |
||
396 | if ($p === false) return false; |
||
397 | $r = substr($oid, $p+1); |
||
398 | |||
399 | if ($leadingdot) { |
||
400 | # if ($r == '') return '.'; |
||
401 | return $r; |
||
402 | } else { |
||
403 | return substr($r, 1); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Calculates the distance between two OIDs. |
||
409 | * This function tolerates leading dots and leading zeroes. |
||
410 | * @author Daniel Marschall, ViaThinkSoft |
||
411 | * @version 2014-12-20 |
||
412 | * @param $a (string)<br /> |
||
413 | * An OID. |
||
414 | * @param $b (string)<br /> |
||
415 | * An OID. |
||
416 | * @return (string) false if both OIDs do not have a child-parent or parent-child relation, e.g. oid_distance('2.999.1.2.3', '2.999.4.5') = false, or if one of the OIDs is syntactially invalid<br /> |
||
417 | * >0 if $a is more specific than $b , e.g. oid_distance('2.999.1.2', '2.999') = 2<br /> |
||
418 | * <0 if $a is more common than $b , e.g. oid_distance('2.999', '2.999.1.2') = -2 |
||
419 | **/ |
||
420 | function oid_distance($a, $b) { |
||
421 | if (substr($a,0,1) == '.') $a = substr($a,1); |
||
422 | if (substr($b,0,1) == '.') $b = substr($b,1); |
||
423 | |||
424 | $a = sanitizeOID($a, false); |
||
425 | if ($a === false) return false; |
||
426 | $b = sanitizeOID($b, false); |
||
427 | if ($b === false) return false; |
||
428 | |||
429 | $ary = explode('.', $a); |
||
430 | $bry = explode('.', $b); |
||
431 | |||
432 | $min_len = min(count($ary), count($bry)); |
||
433 | |||
434 | for ($i=0; $i<$min_len; $i++) { |
||
435 | if ($ary[$i] != $bry[$i]) return false; |
||
436 | } |
||
437 | |||
438 | return count($ary) - count($bry); |
||
439 | } |
||
440 | /* |
||
441 | assert(oid_distance('2.999.1.2.3', '2.999.4.5') === false); |
||
442 | assert(oid_distance('2.999.1.2', '2.999') === 2); |
||
443 | assert(oid_distance('2.999', '2.999.1.2') === -2); |
||
444 | */ |
||
445 | |||
446 | /** |
||
447 | * Adds a leading dot to an OID. |
||
448 | * Leading zeroes are tolerated. |
||
449 | * @author Daniel Marschall, ViaThinkSoft |
||
450 | * @version 2014-12-20 |
||
451 | * @param $oid (string)<br /> |
||
452 | * An OID. |
||
453 | * @return (string) The OID with a leading dot or false if the OID is syntactially wrong. |
||
454 | **/ |
||
455 | function oid_add_leading_dot($oid) { |
||
456 | $oid = sanitizeOID($oid, 'auto'); |
||
457 | if ($oid === false) return false; |
||
458 | |||
459 | if ($oid[0] != '.') $oid = '.'.$oid; |
||
460 | return $oid; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Removes a leading dot to an OID. |
||
465 | * Leading zeroes are tolerated. |
||
466 | * @author Daniel Marschall, ViaThinkSoft |
||
467 | * @version 2014-12-20 |
||
468 | * @param $oid (string)<br /> |
||
469 | * An OID. |
||
470 | * @return (string) The OID without a leading dot or false if the OID is syntactially wrong. |
||
471 | **/ |
||
472 | function oid_remove_leading_dot($oid) { |
||
473 | $oid = sanitizeOID($oid, 'auto'); |
||
474 | if ($oid === false) return false; |
||
475 | |||
476 | if (substr($oid,0,1) == '.') $oid = substr($oid, 1); |
||
477 | return $oid; |
||
478 | } |
||
479 | |||
12 | daniel-mar | 480 | /** |
481 | * Find the common ancestor of two or more OIDs |
||
482 | * @author Daniel Marschall, ViaThinkSoft |
||
483 | * @version 2020-05-27 |
||
484 | * @param $oids (array)<br /> |
||
485 | * An array of multiple OIDs, e.g. 2.999.1 and 2.999.2.3.4 |
||
486 | * @return (mixed) The common ancestor, e.g. 2.999, or false if there is no common ancestor. |
||
487 | **/ |
||
488 | function oid_common_ancestor(array $oids) { |
||
489 | $shared = array(); |
||
3 | daniel-mar | 490 | |
12 | daniel-mar | 491 | if (!is_array($oids)) return false; |
492 | if (count($oids) === 0) return false; |
||
493 | |||
494 | foreach ($oids as &$oid) { |
||
495 | $oid = sanitizeOID($oid, false); |
||
496 | if ($oid === false) return false; |
||
497 | $oid = explode('.', $oid); |
||
498 | } |
||
499 | |||
500 | $max_ok = count($oids[0]); |
||
501 | for ($i=1; $i<count($oids); $i++) { |
||
502 | for ($j=0; $j<min(count($oids[$i]),count($oids[0])); $j++) { |
||
503 | if ($oids[$i][$j] != $oids[0][$j]) { |
||
504 | if ($j < $max_ok) $max_ok = $j; |
||
505 | break; |
||
506 | } |
||
507 | } |
||
508 | if ($j < $max_ok) $max_ok = $j; |
||
509 | } |
||
510 | |||
511 | $out = array(); |
||
512 | for ($i=0; $i<$max_ok; $i++) { |
||
513 | $out[] = $oids[0][$i]; |
||
514 | } |
||
515 | return implode('.', $out); |
||
516 | } |
||
517 | /* |
||
518 | assert(oid_shared_ancestor(array('2.999.4.5.3', '2.999.4.5')) === "2.999.4.5"); |
||
519 | assert(oid_shared_ancestor(array('2.999.4.5', '2.999.4.5.3')) === "2.999.4.5"); |
||
520 | assert(oid_shared_ancestor(array('2.999.1.2.3', '2.999.4.5')) === "2.999"); |
||
521 | */ |
||
522 | |||
523 | |||
3 | daniel-mar | 524 | # === OID-IRI NOTATION FUNCTIONS === |
525 | |||
526 | if (!function_exists('mb_ord')) { |
||
527 | # http://stackoverflow.com/a/24755772/3544341 |
||
528 | function mb_ord($char, $encoding = 'UTF-8') { |
||
529 | if ($encoding === 'UCS-4BE') { |
||
530 | list(, $ord) = (strlen($char) === 4) ? @unpack('N', $char) : @unpack('n', $char); |
||
531 | return $ord; |
||
532 | } else { |
||
533 | return mb_ord(mb_convert_encoding($char, 'UCS-4BE', $encoding), 'UCS-4BE'); |
||
534 | } |
||
535 | } |
||
536 | } |
||
537 | |||
538 | function iri_char_valid($c, $firstchar, $lastchar) { |
||
539 | // see Rec. ITU-T X.660, clause 7.5 |
||
540 | |||
541 | if (($firstchar || $lastchar) && ($c == '-')) return false; |
||
542 | |||
543 | if ($c == '-') return true; |
||
544 | if ($c == '.') return true; |
||
545 | if ($c == '_') return true; |
||
546 | if ($c == '~') return true; |
||
547 | if (($c >= '0') && ($c <= '9') && (!$firstchar)) return true; |
||
548 | if (($c >= 'A') && ($c <= 'Z')) return true; |
||
549 | if (($c >= 'a') && ($c <= 'z')) return true; |
||
550 | |||
551 | $v = mb_ord($c); |
||
552 | |||
553 | if (($v >= 0x000000A0) && ($v <= 0x0000DFFE)) return true; |
||
554 | if (($v >= 0x0000F900) && ($v <= 0x0000FDCF)) return true; |
||
555 | if (($v >= 0x0000FDF0) && ($v <= 0x0000FFEF)) return true; |
||
556 | if (($v >= 0x00010000) && ($v <= 0x0001FFFD)) return true; |
||
557 | if (($v >= 0x00020000) && ($v <= 0x0002FFFD)) return true; |
||
558 | if (($v >= 0x00030000) && ($v <= 0x0003FFFD)) return true; |
||
559 | if (($v >= 0x00040000) && ($v <= 0x0004FFFD)) return true; |
||
560 | if (($v >= 0x00050000) && ($v <= 0x0005FFFD)) return true; |
||
561 | if (($v >= 0x00060000) && ($v <= 0x0006FFFD)) return true; |
||
562 | if (($v >= 0x00070000) && ($v <= 0x0007FFFD)) return true; |
||
563 | if (($v >= 0x00080000) && ($v <= 0x0008FFFD)) return true; |
||
564 | if (($v >= 0x00090000) && ($v <= 0x0009FFFD)) return true; |
||
565 | if (($v >= 0x000A0000) && ($v <= 0x000AFFFD)) return true; |
||
566 | if (($v >= 0x000B0000) && ($v <= 0x000BFFFD)) return true; |
||
567 | if (($v >= 0x000C0000) && ($v <= 0x000CFFFD)) return true; |
||
568 | if (($v >= 0x000D0000) && ($v <= 0x000DFFFD)) return true; |
||
569 | if (($v >= 0x000E1000) && ($v <= 0x000EFFFD)) return true; |
||
570 | |||
571 | // Note: Rec. ITU-T X.660, clause 7.5.3 would also forbid ranges which are marked in ISO/IEC 10646 as "(This position shall not be used)" |
||
572 | // But tool implementers should be tolerate them, since these limitations can be removed in future. |
||
573 | |||
574 | return false; |
||
575 | } |
||
576 | |||
577 | function iri_arc_valid($arc, $allow_numeric=true) { |
||
578 | if ($arc == '') return false; |
||
579 | |||
580 | if ($allow_numeric && preg_match('@^(\\d+)$@', $arc, $m)) return true; # numeric arc |
||
581 | |||
582 | // Question: Should we strip RTL/LTR characters? |
||
583 | |||
584 | if (mb_substr($arc, 2, 2) == '--') return false; // see Rec. ITU-T X.660, clause 7.5.4 |
||
585 | |||
586 | $array = array(); |
||
587 | preg_match_all('/./u', $arc, $array, PREG_SET_ORDER); |
||
588 | $len = count($array); |
||
589 | foreach ($array as $i => $char) { |
||
590 | if (!iri_char_valid($char[0], $i==0, $i==$len-1)) return false; |
||
591 | } |
||
592 | |||
593 | return true; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * Checks if an IRI identifier is valid or not. |
||
598 | * @author Daniel Marschall, ViaThinkSoft |
||
599 | * @version 2014-12-17 |
||
600 | * @param $iri (string)<br /> |
||
601 | * An OID in OID-IRI notation, e.g. /Example/test |
||
602 | * @return (bool) true if the IRI identifier is valid. |
||
603 | **/ |
||
604 | function iri_valid($iri) { |
||
605 | if ($iri == '/') return true; // OK? |
||
606 | |||
607 | if (substr($iri, 0, 1) != '/') return false; |
||
608 | |||
609 | $ary = explode('/', $iri); |
||
610 | array_shift($ary); |
||
611 | foreach ($ary as $a) { |
||
612 | if (!iri_arc_valid($a)) return false; |
||
613 | } |
||
614 | |||
615 | return true; |
||
616 | } |
||
617 | |||
618 | /* |
||
619 | assert(iri_arc_valid('ABCDEF')); |
||
620 | assert(!iri_arc_valid('-ABCDEF')); |
||
621 | assert(!iri_arc_valid('ABCDEF-')); |
||
622 | assert(!iri_arc_valid(' ABCDEF')); |
||
623 | assert(!iri_arc_valid('2 ABCDEF')); |
||
624 | assert(!iri_arc_valid('')); |
||
625 | |||
626 | assert(!iri_valid('')); |
||
627 | assert(iri_valid('/')); |
||
628 | assert(iri_valid('/hello/world')); |
||
629 | assert(iri_valid('/123/world')); |
||
630 | assert(!iri_valid('/hello/0world')); |
||
631 | assert(!iri_valid('/hello/xo--test')); |
||
632 | assert(!iri_valid('/hello/-super-/sd')); |
||
633 | */ |
||
634 | |||
635 | /** |
||
636 | * Returns an associative array in the form 'ASN.1' => '/2/1' . |
||
637 | * @author Daniel Marschall, ViaThinkSoft |
||
638 | * @version 2018-01-05 |
||
639 | * @see http://itu.int/go/X660 |
||
640 | * @return (array) An associative array in the form 'ASN.1' => '/2/1' . |
||
641 | **/ |
||
642 | function iri_get_long_arcs() { |
||
643 | $iri_long_arcs = array(); |
||
644 | $iri_long_arcs['ASN.1'] = '/2/1'; |
||
645 | $iri_long_arcs['Country'] = '/2/16'; |
||
646 | $iri_long_arcs['International-Organizations'] = '/2/23'; |
||
647 | $iri_long_arcs['UUID'] = '/2/25'; |
||
648 | $iri_long_arcs['Tag-Based'] = '/2/27'; |
||
649 | $iri_long_arcs['BIP'] = '/2/41'; |
||
650 | $iri_long_arcs['Telebiometrics'] = '/2/42'; |
||
651 | $iri_long_arcs['Cybersecurity'] = '/2/48'; |
||
652 | $iri_long_arcs['Alerting'] = '/2/49'; |
||
653 | $iri_long_arcs['OIDResolutionSystem'] = '/2/50'; |
||
654 | $iri_long_arcs['GS1'] = '/2/51'; |
||
655 | $iri_long_arcs['Example'] = '/2/999'; // English |
||
656 | $iri_long_arcs['Exemple'] = '/2/999'; // French |
||
657 | $iri_long_arcs['Ejemplo'] = '/2/999'; // Spanish |
||
658 | $iri_long_arcs["\u{0627}\u{0644}\u{0645}\u{062B}\u{0627}\u{0644}"] = '/2/999'; // Arabic |
||
659 | $iri_long_arcs["\u{8303}\u{4F8B}"] = '/2/999'; // Chinese |
||
660 | $iri_long_arcs["\u{041F}\u{0440}\u{0438}\u{043C}\u{0435}\u{0440}"] = '/2/999'; // Russian |
||
661 | $iri_long_arcs["\u{C608}\u{C81C}"] = '/2/999'; // Korean |
||
662 | $iri_long_arcs["\u{4F8B}"] = '/2/999'; // Japanese |
||
663 | $iri_long_arcs['Beispiel'] = '/2/999'; // German |
||
664 | return $iri_long_arcs; |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * Tries to shorten/simplify an IRI by applying "long arcs", e.g. /2/999/123 -> /Example/123 . |
||
669 | * @author Daniel Marschall, ViaThinkSoft |
||
12 | daniel-mar | 670 | * @version 2020-05-22 |
3 | daniel-mar | 671 | * @param $iri (string)<br /> |
672 | * An OID in OID-IRI notation, e.g. /Example/test |
||
673 | * @return (string) The modified IRI. |
||
674 | **/ |
||
675 | function iri_add_longarcs($iri) { |
||
676 | $iri_long_arcs = iri_get_long_arcs(); |
||
677 | |||
12 | daniel-mar | 678 | if (!iri_valid($iri)) return false; |
3 | daniel-mar | 679 | |
680 | $ary = explode('/', $iri); |
||
681 | |||
682 | $ary_number_iri = $ary; |
||
683 | if ($ary_number_iri[1] == 'Joint-ISO-ITU-T') $ary_number_iri[1] = '2'; |
||
12 | daniel-mar | 684 | |
3 | daniel-mar | 685 | $number_iri = implode('/', $ary_number_iri); |
686 | |||
687 | foreach ($iri_long_arcs as $cur_longarc => $cur_iri) { |
||
12 | daniel-mar | 688 | assert(iri_valid($cur_iri)); |
3 | daniel-mar | 689 | if (strpos($number_iri.'/', $cur_iri.'/') === 0) { |
690 | $cnt = substr_count($cur_iri, '/'); |
||
691 | for ($i=1; $i<$cnt; $i++) { |
||
692 | array_shift($ary); |
||
693 | } |
||
694 | $ary[0] = ''; |
||
695 | $ary[1] = $cur_longarc; |
||
696 | $iri = implode('/', $ary); |
||
697 | break; |
||
698 | } |
||
699 | } |
||
700 | |||
701 | return $iri; |
||
702 | } |
||
12 | daniel-mar | 703 | /* |
704 | assert(iri_add_longarcs('/2/999/123') === '/Example/123'); |
||
705 | */ |
||
3 | daniel-mar | 706 | |
707 | # === FUNCTIONS FOR OIDS IN ASN.1 NOTATION === |
||
708 | |||
709 | /** |
||
710 | * Checks if an ASN.1 identifier is valid. |
||
711 | * @author Daniel Marschall, ViaThinkSoft |
||
12 | daniel-mar | 712 | * @version 2020-05-22 |
3 | daniel-mar | 713 | * @param $id (string)<br /> |
714 | * An ASN.1 identifier, e.g. "example". Not "example(99)" or "99" and not a path like "{ 2 999 }" |
||
715 | * Note: Use asn1_path_valid() for validating a whole ASN.1 notation path. |
||
716 | * @return (bool) true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-" |
||
717 | **/ |
||
718 | function oid_id_is_valid($id) { |
||
12 | daniel-mar | 719 | // see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7 |
720 | // and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3 |
||
721 | if (substr($id,-1,1) == '-') return false; |
||
722 | if (strstr($id,'--')) return false; |
||
723 | return preg_match('/^([a-z][a-zA-Z0-9-]*)$/', $id) != 0; |
||
3 | daniel-mar | 724 | } |
725 | |||
726 | /** |
||
727 | * Checks if the ASN.1 notation of an OID is valid. |
||
728 | * This function does not tolerate leading zeros. |
||
729 | * This function will fail (return false) if there are unresolved symbols, e.g. {iso test} is not valid while { iso 123 } is valid. |
||
730 | * @author Daniel Marschall, ViaThinkSoft |
||
731 | * @version 2014-12-17 |
||
732 | * @param $asn (string)<br /> |
||
733 | * An OID in ASN.1 notation. |
||
734 | * @return (bools) true if the identifier is valid. |
||
735 | **/ |
||
736 | function asn1_path_valid($asn1) { |
||
737 | return asn1_to_dot($asn1) != false; |
||
738 | } |
||
739 | |||
740 | /** |
||
741 | * Returns an array of standardized ASN.1 alphanumeric identifiers which do not require a numeric identifier, e.g. { 2 example } |
||
742 | * The array has the form '0.0.a' -> '0.0.1' |
||
743 | * @author Daniel Marschall, ViaThinkSoft |
||
744 | * @version 2019-03-25 |
||
745 | * @see http://www.oid-info.com/name-forms.htm |
||
746 | * @return (array) Associative array of standardized ASN.1 alphanumeric identifiers |
||
747 | **/ |
||
748 | function asn1_get_standardized_array() { |
||
749 | |||
750 | // Taken from oid-info.com |
||
751 | // http://www.oid-info.com/name-forms.htm |
||
752 | $standardized = array(); |
||
753 | $standardized['itu-t'] = '0'; |
||
754 | $standardized['ccitt'] = '0'; |
||
755 | $standardized['iso'] = '1'; |
||
756 | $standardized['joint-iso-itu-t'] = '2'; |
||
757 | $standardized['joint-iso-ccitt'] = '2'; |
||
758 | $standardized['0.recommendation'] = '0.0'; |
||
759 | $standardized['0.0.a'] = '0.0.1'; |
||
760 | $standardized['0.0.b'] = '0.0.2'; |
||
761 | $standardized['0.0.c'] = '0.0.3'; |
||
762 | $standardized['0.0.d'] = '0.0.4'; |
||
763 | $standardized['0.0.e'] = '0.0.5'; |
||
764 | $standardized['0.0.f'] = '0.0.6'; |
||
765 | $standardized['0.0.g'] = '0.0.7'; |
||
766 | $standardized['0.0.h'] = '0.0.8'; |
||
767 | $standardized['0.0.i'] = '0.0.9'; |
||
768 | $standardized['0.0.j'] = '0.0.10'; |
||
769 | $standardized['0.0.k'] = '0.0.11'; |
||
770 | $standardized['0.0.l'] = '0.0.12'; |
||
771 | $standardized['0.0.m'] = '0.0.13'; |
||
772 | $standardized['0.0.n'] = '0.0.14'; |
||
773 | $standardized['0.0.o'] = '0.0.15'; |
||
774 | $standardized['0.0.p'] = '0.0.16'; |
||
775 | $standardized['0.0.q'] = '0.0.17'; |
||
776 | $standardized['0.0.r'] = '0.0.18'; |
||
777 | $standardized['0.0.s'] = '0.0.19'; |
||
778 | $standardized['0.0.t'] = '0.0.20'; |
||
779 | $standardized['0.0.u'] = '0.0.21'; |
||
780 | $standardized['0.0.v'] = '0.0.22'; |
||
781 | $standardized['0.0.w'] = '0.0.23'; // actually, this OID does not exist |
||
782 | $standardized['0.0.x'] = '0.0.24'; |
||
783 | $standardized['0.0.y'] = '0.0.25'; |
||
784 | $standardized['0.0.z'] = '0.0.26'; |
||
785 | $standardized['0.question'] = '0.1'; |
||
786 | $standardized['0.administration'] = '0.2'; |
||
787 | $standardized['0.network-operator'] = '0.3'; |
||
788 | $standardized['0.identified-organization'] = '0.4'; |
||
789 | $standardized['1.standard'] = '1.0'; |
||
790 | $standardized['1.registration-authority'] = '1.1'; |
||
791 | $standardized['1.member-body'] = '1.2'; |
||
792 | $standardized['1.identified-organization'] = '1.3'; |
||
793 | return $standardized; |
||
794 | } |
||
795 | |||
796 | /** |
||
797 | * Converts an OID in ASN.1 notation into an OID in dot notation and tries to resolve well-known identifiers.<br /> |
||
798 | * e.g. {joint-iso-itu-t(2) example(999) 1 2 3} --> 2.999.1.2.3<br /> |
||
799 | * e.g. {iso 3} --> 1.3 |
||
800 | * This function does not tolerate leading zeros. |
||
801 | * This function will fail (return false) if there are unresolved symbols, e.g. {iso test} will not be resolved to 1.test |
||
802 | * @author Daniel Marschall, ViaThinkSoft |
||
803 | * @version 2014-12-17 |
||
804 | * @param $asn (string)<br /> |
||
805 | * An OID in ASN.1 notation. |
||
806 | * @return (string) An OID in dot notation without leading dot or false if the path is invalid. |
||
807 | **/ |
||
808 | function asn1_to_dot($asn) { |
||
809 | $standardized = asn1_get_standardized_array(); |
||
810 | |||
811 | // Clean up |
||
812 | $asn = preg_replace('@^\\{(.+)\\}$@', '\\1', $asn, -1, $count); |
||
813 | if ($count == 0) return false; // { and } are required. The asn.1 path will NOT be trimmed by this function |
||
814 | |||
815 | // If identifier is set, apply it (no check if it overrides a standardized identifier) |
||
816 | $asn = preg_replace('|\s*([a-z][a-zA-Z0-9-]*)\s*\((\d+)\)|', ' \\2', $asn); |
||
817 | $asn = trim($asn); |
||
818 | |||
819 | // Set dots |
||
820 | $asn = preg_replace('|\s+|', '.', $asn); |
||
821 | |||
822 | // Apply standardized identifiers (case sensitive) |
||
823 | $asn .= '.'; |
||
824 | foreach ($standardized as $s => $r) { |
||
825 | $asn = preg_replace("|^$s|", $r, $asn); |
||
826 | } |
||
827 | $asn = substr($asn, 0, strlen($asn)-1); |
||
828 | |||
829 | // Check if all numbers are OK |
||
830 | // -> every arc must be resolved |
||
831 | // -> numeric arcs must not have a leading zero |
||
832 | // -> invalid stuff will be recognized, e.g. a "(1)" without an identifier in front of it |
||
833 | $ary = explode('.', $asn); |
||
834 | foreach ($ary as $a) { |
||
835 | if (!preg_match('@^(0|([1-9]\\d*))$@', $a, $m)) return false; |
||
836 | } |
||
837 | |||
838 | return $asn; |
||
839 | } |
||
840 | |||
841 | /* |
||
842 | assert(asn1_to_dot('{2 999 (1)}') == false); |
||
843 | assert(asn1_to_dot('{2 999 test}') == false); |
||
844 | assert(asn1_to_dot('{2 999 1}') == '2.999.1'); |
||
845 | assert(asn1_to_dot(' {2 999 1} ') == false); |
||
846 | assert(asn1_to_dot('2 999 1') == false); |
||
847 | assert(asn1_to_dot('{2 999 01}') == false); |
||
848 | assert(asn1_to_dot('{ 0 question 123 }') == '0.1.123'); |
||
849 | assert(asn1_to_dot('{ iso }') == '1'); |
||
850 | assert(asn1_to_dot('{ iso(1) }') == '1'); |
||
851 | assert(asn1_to_dot('{ iso(2) }') == '2'); |
||
852 | assert(asn1_to_dot('{ iso 3 }') == '1.3'); |
||
853 | */ |
||
854 | |||
855 | /** |
||
12 | daniel-mar | 856 | * Gets the last numeric identifier of an ASN.1 notation OID. |
857 | * @author Daniel Marschall, ViaThinkSoft |
||
858 | * @version 2020-06-11 |
||
859 | * @param $asn1id (string)<br /> |
||
860 | * An ASN.1 identifier string, e.g. { 2 example(999) test(1) } |
||
861 | * @return (int) The last numeric identifier arc, e.g. "1" |
||
862 | **/ |
||
863 | function asn1_last_identifier($asn1id) { |
||
864 | $asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id); |
||
865 | $asn1id = trim(str_replace(array('{', '}', "\t"), ' ', $asn1id)); |
||
866 | $ary = explode(' ', $asn1id); |
||
867 | $asn1id = $ary[count($ary)-1]; |
||
868 | return preg_match('#[^0-9]#',$asn1id) ? $asn1id : false; |
||
869 | } |
||
870 | |||
871 | /** |
||
3 | daniel-mar | 872 | * "Soft corrects" an invalid ASN.1 identifier.<br /> |
873 | * Attention, by "soft correcting" the ID, it is not authoritative anymore, and might not be able to be resolved by ORS. |
||
874 | * @author Daniel Marschall, ViaThinkSoft |
||
12 | daniel-mar | 875 | * @version 2020-05-22 |
3 | daniel-mar | 876 | * @param $id (string)<br /> |
877 | * An ASN.1 identifier. |
||
878 | * @param $append_id_prefix (bool)<br /> |
||
879 | * true (default): If the identifier doesn't start with a-Z, the problem will be solved by prepending "id-" to the identifier.<br /> |
||
880 | * false: If the identifier doesn't start with a-Z, then the problem cannot be solved (method returns empty string). |
||
881 | * @return (string) The "soft corrected" ASN.1 identifier.<br /> |
||
882 | * Invalid characters will be removed.<br /> |
||
883 | * Uncorrectable start elements (0-9 or "-") will be either removed or solved by prepending "id-" (see <code>$append_id_prefix</code>)<br /> |
||
884 | * If the identifier begins with an upper case letter, the letter will be converted into lower case. |
||
885 | **/ |
||
886 | function oid_soft_correct_id($id, $append_id_prefix = true) { |
||
887 | // Convert "_" to "-" |
||
888 | $id = str_replace('_', '-', $id); |
||
889 | |||
12 | daniel-mar | 890 | // Convert "--" to "-" |
891 | $id = str_replace('--', '-', $id); |
||
892 | |||
3 | daniel-mar | 893 | // Remove invalid characters |
894 | $id = preg_replace('/[^a-zA-Z0-9-]+/', '', $id); |
||
895 | |||
896 | // Remove uncorrectable start elements (0-9 or "-") |
||
897 | if ($append_id_prefix) { |
||
898 | $id = preg_replace('/^([^a-zA-Z]+)/', 'id-$1', $id); |
||
899 | } else { |
||
900 | $id = preg_replace('/^([^a-zA-Z]+)/', '', $id); |
||
901 | } |
||
902 | |||
903 | // "Correct" upper case beginning letter by converting it to lower case |
||
904 | if (preg_match('/^[A-Z]/', $id)) { |
||
905 | $id = strtolower($id[0]) . substr($id, 1); |
||
906 | } |
||
907 | |||
908 | return $id; |
||
909 | } |