@@ -11,111 +11,111 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class BaseTime extends Element implements TimeType |
13 | 13 | { |
14 | - /** |
|
15 | - * UTC timezone. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - const TZ_UTC = 'UTC'; |
|
14 | + /** |
|
15 | + * UTC timezone. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + const TZ_UTC = 'UTC'; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Date and time. |
|
23 | - * |
|
24 | - * @var \DateTimeImmutable |
|
25 | - */ |
|
26 | - protected $_dateTime; |
|
21 | + /** |
|
22 | + * Date and time. |
|
23 | + * |
|
24 | + * @var \DateTimeImmutable |
|
25 | + */ |
|
26 | + protected $_dateTime; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param \DateTimeImmutable $dt |
|
32 | - */ |
|
33 | - public function __construct(\DateTimeImmutable $dt) |
|
34 | - { |
|
35 | - $this->_dateTime = $dt; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param \DateTimeImmutable $dt |
|
32 | + */ |
|
33 | + public function __construct(\DateTimeImmutable $dt) |
|
34 | + { |
|
35 | + $this->_dateTime = $dt; |
|
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 | - * Initialize from datetime string. |
|
48 | - * |
|
49 | - * @see http://php.net/manual/en/datetime.formats.php |
|
50 | - * |
|
51 | - * @param string $time Time string |
|
52 | - * @param null|string $tz timezone, if null use default |
|
53 | - * |
|
54 | - * @throws \RuntimeException |
|
55 | - * |
|
56 | - * @return self |
|
57 | - */ |
|
58 | - public static function fromString(string $time, ?string $tz = null): self |
|
59 | - { |
|
60 | - try { |
|
61 | - if (!isset($tz)) { |
|
62 | - $tz = date_default_timezone_get(); |
|
63 | - } |
|
64 | - return new static( |
|
65 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
66 | - } catch (\Exception $e) { |
|
67 | - throw new \RuntimeException( |
|
68 | - 'Failed to create DateTime: ' . |
|
69 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
70 | - } |
|
71 | - } |
|
46 | + /** |
|
47 | + * Initialize from datetime string. |
|
48 | + * |
|
49 | + * @see http://php.net/manual/en/datetime.formats.php |
|
50 | + * |
|
51 | + * @param string $time Time string |
|
52 | + * @param null|string $tz timezone, if null use default |
|
53 | + * |
|
54 | + * @throws \RuntimeException |
|
55 | + * |
|
56 | + * @return self |
|
57 | + */ |
|
58 | + public static function fromString(string $time, ?string $tz = null): self |
|
59 | + { |
|
60 | + try { |
|
61 | + if (!isset($tz)) { |
|
62 | + $tz = date_default_timezone_get(); |
|
63 | + } |
|
64 | + return new static( |
|
65 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
66 | + } catch (\Exception $e) { |
|
67 | + throw new \RuntimeException( |
|
68 | + 'Failed to create DateTime: ' . |
|
69 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Get the date and time. |
|
75 | - * |
|
76 | - * @return \DateTimeImmutable |
|
77 | - */ |
|
78 | - public function dateTime(): \DateTimeImmutable |
|
79 | - { |
|
80 | - return $this->_dateTime; |
|
81 | - } |
|
73 | + /** |
|
74 | + * Get the date and time. |
|
75 | + * |
|
76 | + * @return \DateTimeImmutable |
|
77 | + */ |
|
78 | + public function dateTime(): \DateTimeImmutable |
|
79 | + { |
|
80 | + return $this->_dateTime; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Get the date and time as a type specific string. |
|
85 | - * |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - public function string(): string |
|
89 | - { |
|
90 | - return $this->_encodedContentDER(); |
|
91 | - } |
|
83 | + /** |
|
84 | + * Get the date and time as a type specific string. |
|
85 | + * |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + public function string(): string |
|
89 | + { |
|
90 | + return $this->_encodedContentDER(); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Create `DateTimeZone` object from string. |
|
95 | - * |
|
96 | - * @param string $tz |
|
97 | - * |
|
98 | - * @throws \UnexpectedValueException If timezone is invalid |
|
99 | - * |
|
100 | - * @return \DateTimeZone |
|
101 | - */ |
|
102 | - protected static function _createTimeZone(string $tz): \DateTimeZone |
|
103 | - { |
|
104 | - try { |
|
105 | - return new \DateTimeZone($tz); |
|
106 | - } catch (\Exception $e) { |
|
107 | - throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
108 | - } |
|
109 | - } |
|
93 | + /** |
|
94 | + * Create `DateTimeZone` object from string. |
|
95 | + * |
|
96 | + * @param string $tz |
|
97 | + * |
|
98 | + * @throws \UnexpectedValueException If timezone is invalid |
|
99 | + * |
|
100 | + * @return \DateTimeZone |
|
101 | + */ |
|
102 | + protected static function _createTimeZone(string $tz): \DateTimeZone |
|
103 | + { |
|
104 | + try { |
|
105 | + return new \DateTimeZone($tz); |
|
106 | + } catch (\Exception $e) { |
|
107 | + throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
108 | + } |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Get last error caused by `DateTimeImmutable`. |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
117 | - { |
|
118 | - $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
119 | - return implode(', ', $errors); |
|
120 | - } |
|
111 | + /** |
|
112 | + * Get last error caused by `DateTimeImmutable`. |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
117 | + { |
|
118 | + $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
119 | + return implode(', ', $errors); |
|
120 | + } |
|
121 | 121 | } |
@@ -18,36 +18,36 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class PrimitiveString extends BaseString |
20 | 20 | { |
21 | - use PrimitiveType; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * {@inheritdoc} |
|
25 | - */ |
|
26 | - protected function _encodedContentDER(): string |
|
27 | - { |
|
28 | - return $this->_string; |
|
29 | - } |
|
23 | + /** |
|
24 | + * {@inheritdoc} |
|
25 | + */ |
|
26 | + protected function _encodedContentDER(): string |
|
27 | + { |
|
28 | + return $this->_string; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * {@inheritdoc} |
|
33 | - */ |
|
34 | - protected static function _decodeFromDER(Identifier $identifier, |
|
35 | - string $data, int &$offset): ElementBase |
|
36 | - { |
|
37 | - $idx = $offset; |
|
38 | - if (!$identifier->isPrimitive()) { |
|
39 | - throw new DecodeException('DER encoded string must be primitive.'); |
|
40 | - } |
|
41 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
42 | - $str = $length ? substr($data, $idx, $length) : ''; |
|
43 | - // substr should never return false, since length is |
|
44 | - // checked by Length::expectFromDER. |
|
45 | - assert(is_string($str), new DecodeException('substr')); |
|
46 | - $offset = $idx + $length; |
|
47 | - try { |
|
48 | - return new static($str); |
|
49 | - } catch (\InvalidArgumentException $e) { |
|
50 | - throw new DecodeException($e->getMessage(), 0, $e); |
|
51 | - } |
|
52 | - } |
|
31 | + /** |
|
32 | + * {@inheritdoc} |
|
33 | + */ |
|
34 | + protected static function _decodeFromDER(Identifier $identifier, |
|
35 | + string $data, int &$offset): ElementBase |
|
36 | + { |
|
37 | + $idx = $offset; |
|
38 | + if (!$identifier->isPrimitive()) { |
|
39 | + throw new DecodeException('DER encoded string must be primitive.'); |
|
40 | + } |
|
41 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
42 | + $str = $length ? substr($data, $idx, $length) : ''; |
|
43 | + // substr should never return false, since length is |
|
44 | + // checked by Length::expectFromDER. |
|
45 | + assert(is_string($str), new DecodeException('substr')); |
|
46 | + $offset = $idx + $length; |
|
47 | + try { |
|
48 | + return new static($str); |
|
49 | + } catch (\InvalidArgumentException $e) { |
|
50 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
51 | + } |
|
52 | + } |
|
53 | 53 | } |
@@ -17,70 +17,70 @@ |
||
17 | 17 | */ |
18 | 18 | class UTCTime extends BaseTime |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = '#^' . |
|
31 | - '(\d\d)' . // YY |
|
32 | - '(\d\d)' . // MM |
|
33 | - '(\d\d)' . // DD |
|
34 | - '(\d\d)' . // hh |
|
35 | - '(\d\d)' . // mm |
|
36 | - '(\d\d)' . // ss |
|
37 | - 'Z' . // TZ |
|
38 | - '$#'; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = '#^' . |
|
31 | + '(\d\d)' . // YY |
|
32 | + '(\d\d)' . // MM |
|
33 | + '(\d\d)' . // DD |
|
34 | + '(\d\d)' . // hh |
|
35 | + '(\d\d)' . // mm |
|
36 | + '(\d\d)' . // ss |
|
37 | + 'Z' . // TZ |
|
38 | + '$#'; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor. |
|
42 | - * |
|
43 | - * @param \DateTimeImmutable $dt |
|
44 | - */ |
|
45 | - public function __construct(\DateTimeImmutable $dt) |
|
46 | - { |
|
47 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | - parent::__construct($dt); |
|
49 | - } |
|
40 | + /** |
|
41 | + * Constructor. |
|
42 | + * |
|
43 | + * @param \DateTimeImmutable $dt |
|
44 | + */ |
|
45 | + public function __construct(\DateTimeImmutable $dt) |
|
46 | + { |
|
47 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | + parent::__construct($dt); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
57 | - return $dt->format('ymdHis\\Z'); |
|
58 | - } |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
57 | + return $dt->format('ymdHis\\Z'); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected static function _decodeFromDER(Identifier $identifier, |
|
64 | - string $data, int &$offset): ElementBase |
|
65 | - { |
|
66 | - $idx = $offset; |
|
67 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
68 | - $str = substr($data, $idx, $length); |
|
69 | - $idx += $length; |
|
70 | - /** @var string[] $match */ |
|
71 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
72 | - throw new DecodeException('Invalid UTCTime format.'); |
|
73 | - } |
|
74 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
75 | - $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
76 | - $dt = \DateTimeImmutable::createFromFormat('!ymdHisT', $time, |
|
77 | - self::_createTimeZone(self::TZ_UTC)); |
|
78 | - if (!$dt) { |
|
79 | - throw new DecodeException( |
|
80 | - 'Failed to decode UTCTime: ' . |
|
81 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
82 | - } |
|
83 | - $offset = $idx; |
|
84 | - return new self($dt); |
|
85 | - } |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected static function _decodeFromDER(Identifier $identifier, |
|
64 | + string $data, int &$offset): ElementBase |
|
65 | + { |
|
66 | + $idx = $offset; |
|
67 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
68 | + $str = substr($data, $idx, $length); |
|
69 | + $idx += $length; |
|
70 | + /** @var string[] $match */ |
|
71 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
72 | + throw new DecodeException('Invalid UTCTime format.'); |
|
73 | + } |
|
74 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
75 | + $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
76 | + $dt = \DateTimeImmutable::createFromFormat('!ymdHisT', $time, |
|
77 | + self::_createTimeZone(self::TZ_UTC)); |
|
78 | + if (!$dt) { |
|
79 | + throw new DecodeException( |
|
80 | + 'Failed to decode UTCTime: ' . |
|
81 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
82 | + } |
|
83 | + $offset = $idx; |
|
84 | + return new self($dt); |
|
85 | + } |
|
86 | 86 | } |
@@ -12,16 +12,16 @@ |
||
12 | 12 | */ |
13 | 13 | class CharacterString extends PrimitiveString |
14 | 14 | { |
15 | - use UniversalClass; |
|
15 | + use UniversalClass; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param string $string |
|
21 | - */ |
|
22 | - public function __construct(string $string) |
|
23 | - { |
|
24 | - $this->_typeTag = self::TYPE_CHARACTER_STRING; |
|
25 | - parent::__construct($string); |
|
26 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param string $string |
|
21 | + */ |
|
22 | + public function __construct(string $string) |
|
23 | + { |
|
24 | + $this->_typeTag = self::TYPE_CHARACTER_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | } |
@@ -17,194 +17,194 @@ |
||
17 | 17 | */ |
18 | 18 | class BitString extends BaseString |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Number of unused bits in the last octet. |
|
25 | - * |
|
26 | - * @var int |
|
27 | - */ |
|
28 | - protected $_unusedBits; |
|
23 | + /** |
|
24 | + * Number of unused bits in the last octet. |
|
25 | + * |
|
26 | + * @var int |
|
27 | + */ |
|
28 | + protected $_unusedBits; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $string Content octets |
|
34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
35 | - */ |
|
36 | - public function __construct(string $string, int $unused_bits = 0) |
|
37 | - { |
|
38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | - parent::__construct($string); |
|
40 | - $this->_unusedBits = $unused_bits; |
|
41 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $string Content octets |
|
34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
35 | + */ |
|
36 | + public function __construct(string $string, int $unused_bits = 0) |
|
37 | + { |
|
38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | + parent::__construct($string); |
|
40 | + $this->_unusedBits = $unused_bits; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the number of bits in the string. |
|
45 | - * |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - public function numBits(): int |
|
49 | - { |
|
50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Get the number of bits in the string. |
|
45 | + * |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + public function numBits(): int |
|
49 | + { |
|
50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the number of unused bits in the last octet of the string. |
|
55 | - * |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public function unusedBits(): int |
|
59 | - { |
|
60 | - return $this->_unusedBits; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Get the number of unused bits in the last octet of the string. |
|
55 | + * |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public function unusedBits(): int |
|
59 | + { |
|
60 | + return $this->_unusedBits; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Test whether bit is set. |
|
65 | - * |
|
66 | - * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
67 | - * |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function testBit(int $idx): bool |
|
71 | - { |
|
72 | - // octet index |
|
73 | - $oi = (int) floor($idx / 8); |
|
74 | - // if octet is outside range |
|
75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
76 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
77 | - } |
|
78 | - // bit index |
|
79 | - $bi = $idx % 8; |
|
80 | - // if tested bit is last octet's unused bit |
|
81 | - if ($oi === strlen($this->_string) - 1) { |
|
82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
83 | - throw new \OutOfBoundsException( |
|
84 | - 'Index refers to an unused bit.'); |
|
85 | - } |
|
86 | - } |
|
87 | - $byte = $this->_string[$oi]; |
|
88 | - // index 0 is the most significant bit in byte |
|
89 | - $mask = 0x01 << (7 - $bi); |
|
90 | - return (ord($byte) & $mask) > 0; |
|
91 | - } |
|
63 | + /** |
|
64 | + * Test whether bit is set. |
|
65 | + * |
|
66 | + * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
67 | + * |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function testBit(int $idx): bool |
|
71 | + { |
|
72 | + // octet index |
|
73 | + $oi = (int) floor($idx / 8); |
|
74 | + // if octet is outside range |
|
75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
76 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
77 | + } |
|
78 | + // bit index |
|
79 | + $bi = $idx % 8; |
|
80 | + // if tested bit is last octet's unused bit |
|
81 | + if ($oi === strlen($this->_string) - 1) { |
|
82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
83 | + throw new \OutOfBoundsException( |
|
84 | + 'Index refers to an unused bit.'); |
|
85 | + } |
|
86 | + } |
|
87 | + $byte = $this->_string[$oi]; |
|
88 | + // index 0 is the most significant bit in byte |
|
89 | + $mask = 0x01 << (7 - $bi); |
|
90 | + return (ord($byte) & $mask) > 0; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Get range of bits. |
|
95 | - * |
|
96 | - * @param int $start Index of first bit |
|
97 | - * @param int $length Number of bits in range |
|
98 | - * |
|
99 | - * @throws \OutOfBoundsException |
|
100 | - * |
|
101 | - * @return string Integer of $length bits |
|
102 | - */ |
|
103 | - public function range(int $start, int $length): string |
|
104 | - { |
|
105 | - if (!$length) { |
|
106 | - return '0'; |
|
107 | - } |
|
108 | - if ($start + $length > $this->numBits()) { |
|
109 | - throw new \OutOfBoundsException('Not enough bits.'); |
|
110 | - } |
|
111 | - $bits = gmp_init(0); |
|
112 | - $idx = $start; |
|
113 | - $end = $start + $length; |
|
114 | - while (true) { |
|
115 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
116 | - $bits |= $bit; |
|
117 | - if (++$idx >= $end) { |
|
118 | - break; |
|
119 | - } |
|
120 | - $bits <<= 1; |
|
121 | - } |
|
122 | - return gmp_strval($bits, 10); |
|
123 | - } |
|
93 | + /** |
|
94 | + * Get range of bits. |
|
95 | + * |
|
96 | + * @param int $start Index of first bit |
|
97 | + * @param int $length Number of bits in range |
|
98 | + * |
|
99 | + * @throws \OutOfBoundsException |
|
100 | + * |
|
101 | + * @return string Integer of $length bits |
|
102 | + */ |
|
103 | + public function range(int $start, int $length): string |
|
104 | + { |
|
105 | + if (!$length) { |
|
106 | + return '0'; |
|
107 | + } |
|
108 | + if ($start + $length > $this->numBits()) { |
|
109 | + throw new \OutOfBoundsException('Not enough bits.'); |
|
110 | + } |
|
111 | + $bits = gmp_init(0); |
|
112 | + $idx = $start; |
|
113 | + $end = $start + $length; |
|
114 | + while (true) { |
|
115 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
116 | + $bits |= $bit; |
|
117 | + if (++$idx >= $end) { |
|
118 | + break; |
|
119 | + } |
|
120 | + $bits <<= 1; |
|
121 | + } |
|
122 | + return gmp_strval($bits, 10); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Get a copy of the bit string with trailing zeroes removed. |
|
127 | - * |
|
128 | - * @return self |
|
129 | - */ |
|
130 | - public function withoutTrailingZeroes(): self |
|
131 | - { |
|
132 | - // if bit string was empty |
|
133 | - if (!strlen($this->_string)) { |
|
134 | - return new self(''); |
|
135 | - } |
|
136 | - $bits = $this->_string; |
|
137 | - // count number of empty trailing octets |
|
138 | - $unused_octets = 0; |
|
139 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
140 | - if ("\x0" !== $bits[$idx]) { |
|
141 | - break; |
|
142 | - } |
|
143 | - } |
|
144 | - // strip trailing octets |
|
145 | - if ($unused_octets) { |
|
146 | - $bits = substr($bits, 0, -$unused_octets); |
|
147 | - } |
|
148 | - // if bit string was full of zeroes |
|
149 | - if (!strlen($bits)) { |
|
150 | - return new self(''); |
|
151 | - } |
|
152 | - // count number of trailing zeroes in the last octet |
|
153 | - $unused_bits = 0; |
|
154 | - $byte = ord($bits[strlen($bits) - 1]); |
|
155 | - while (!($byte & 0x01)) { |
|
156 | - ++$unused_bits; |
|
157 | - $byte >>= 1; |
|
158 | - } |
|
159 | - return new self($bits, $unused_bits); |
|
160 | - } |
|
125 | + /** |
|
126 | + * Get a copy of the bit string with trailing zeroes removed. |
|
127 | + * |
|
128 | + * @return self |
|
129 | + */ |
|
130 | + public function withoutTrailingZeroes(): self |
|
131 | + { |
|
132 | + // if bit string was empty |
|
133 | + if (!strlen($this->_string)) { |
|
134 | + return new self(''); |
|
135 | + } |
|
136 | + $bits = $this->_string; |
|
137 | + // count number of empty trailing octets |
|
138 | + $unused_octets = 0; |
|
139 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
140 | + if ("\x0" !== $bits[$idx]) { |
|
141 | + break; |
|
142 | + } |
|
143 | + } |
|
144 | + // strip trailing octets |
|
145 | + if ($unused_octets) { |
|
146 | + $bits = substr($bits, 0, -$unused_octets); |
|
147 | + } |
|
148 | + // if bit string was full of zeroes |
|
149 | + if (!strlen($bits)) { |
|
150 | + return new self(''); |
|
151 | + } |
|
152 | + // count number of trailing zeroes in the last octet |
|
153 | + $unused_bits = 0; |
|
154 | + $byte = ord($bits[strlen($bits) - 1]); |
|
155 | + while (!($byte & 0x01)) { |
|
156 | + ++$unused_bits; |
|
157 | + $byte >>= 1; |
|
158 | + } |
|
159 | + return new self($bits, $unused_bits); |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * {@inheritdoc} |
|
164 | - */ |
|
165 | - protected function _encodedContentDER(): string |
|
166 | - { |
|
167 | - $der = chr($this->_unusedBits); |
|
168 | - $der .= $this->_string; |
|
169 | - if ($this->_unusedBits) { |
|
170 | - $octet = $der[strlen($der) - 1]; |
|
171 | - // set unused bits to zero |
|
172 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
173 | - $der[strlen($der) - 1] = $octet; |
|
174 | - } |
|
175 | - return $der; |
|
176 | - } |
|
162 | + /** |
|
163 | + * {@inheritdoc} |
|
164 | + */ |
|
165 | + protected function _encodedContentDER(): string |
|
166 | + { |
|
167 | + $der = chr($this->_unusedBits); |
|
168 | + $der .= $this->_string; |
|
169 | + if ($this->_unusedBits) { |
|
170 | + $octet = $der[strlen($der) - 1]; |
|
171 | + // set unused bits to zero |
|
172 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
173 | + $der[strlen($der) - 1] = $octet; |
|
174 | + } |
|
175 | + return $der; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * {@inheritdoc} |
|
180 | - */ |
|
181 | - protected static function _decodeFromDER(Identifier $identifier, |
|
182 | - string $data, int &$offset): ElementBase |
|
183 | - { |
|
184 | - $idx = $offset; |
|
185 | - $length = Length::expectFromDER($data, $idx); |
|
186 | - if ($length->intLength() < 1) { |
|
187 | - throw new DecodeException('Bit string length must be at least 1.'); |
|
188 | - } |
|
189 | - $unused_bits = ord($data[$idx++]); |
|
190 | - if ($unused_bits > 7) { |
|
191 | - throw new DecodeException( |
|
192 | - 'Unused bits in a bit string must be less than 8.'); |
|
193 | - } |
|
194 | - $str_len = $length->intLength() - 1; |
|
195 | - if ($str_len) { |
|
196 | - $str = substr($data, $idx, $str_len); |
|
197 | - if ($unused_bits) { |
|
198 | - $mask = (1 << $unused_bits) - 1; |
|
199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
200 | - throw new DecodeException( |
|
201 | - 'DER encoded bit string must have zero padding.'); |
|
202 | - } |
|
203 | - } |
|
204 | - } else { |
|
205 | - $str = ''; |
|
206 | - } |
|
207 | - $offset = $idx + $str_len; |
|
208 | - return new self($str, $unused_bits); |
|
209 | - } |
|
178 | + /** |
|
179 | + * {@inheritdoc} |
|
180 | + */ |
|
181 | + protected static function _decodeFromDER(Identifier $identifier, |
|
182 | + string $data, int &$offset): ElementBase |
|
183 | + { |
|
184 | + $idx = $offset; |
|
185 | + $length = Length::expectFromDER($data, $idx); |
|
186 | + if ($length->intLength() < 1) { |
|
187 | + throw new DecodeException('Bit string length must be at least 1.'); |
|
188 | + } |
|
189 | + $unused_bits = ord($data[$idx++]); |
|
190 | + if ($unused_bits > 7) { |
|
191 | + throw new DecodeException( |
|
192 | + 'Unused bits in a bit string must be less than 8.'); |
|
193 | + } |
|
194 | + $str_len = $length->intLength() - 1; |
|
195 | + if ($str_len) { |
|
196 | + $str = substr($data, $idx, $str_len); |
|
197 | + if ($unused_bits) { |
|
198 | + $mask = (1 << $unused_bits) - 1; |
|
199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
200 | + throw new DecodeException( |
|
201 | + 'DER encoded bit string must have zero padding.'); |
|
202 | + } |
|
203 | + } |
|
204 | + } else { |
|
205 | + $str = ''; |
|
206 | + } |
|
207 | + $offset = $idx + $str_len; |
|
208 | + return new self($str, $unused_bits); |
|
209 | + } |
|
210 | 210 | } |
@@ -17,111 +17,111 @@ |
||
17 | 17 | */ |
18 | 18 | class GeneralizedTime extends BaseTime |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = '#^' . |
|
31 | - '(\d\d\d\d)' . // YYYY |
|
32 | - '(\d\d)' . // MM |
|
33 | - '(\d\d)' . // DD |
|
34 | - '(\d\d)' . // hh |
|
35 | - '(\d\d)' . // mm |
|
36 | - '(\d\d)' . // ss |
|
37 | - '(?:\.(\d+))?' . // frac |
|
38 | - 'Z' . // TZ |
|
39 | - '$#'; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = '#^' . |
|
31 | + '(\d\d\d\d)' . // YYYY |
|
32 | + '(\d\d)' . // MM |
|
33 | + '(\d\d)' . // DD |
|
34 | + '(\d\d)' . // hh |
|
35 | + '(\d\d)' . // mm |
|
36 | + '(\d\d)' . // ss |
|
37 | + '(?:\.(\d+))?' . // frac |
|
38 | + 'Z' . // TZ |
|
39 | + '$#'; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Cached formatted date. |
|
43 | - * |
|
44 | - * @var null|string |
|
45 | - */ |
|
46 | - private $_formatted; |
|
41 | + /** |
|
42 | + * Cached formatted date. |
|
43 | + * |
|
44 | + * @var null|string |
|
45 | + */ |
|
46 | + private $_formatted; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param \DateTimeImmutable $dt |
|
52 | - */ |
|
53 | - public function __construct(\DateTimeImmutable $dt) |
|
54 | - { |
|
55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | - parent::__construct($dt); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param \DateTimeImmutable $dt |
|
52 | + */ |
|
53 | + public function __construct(\DateTimeImmutable $dt) |
|
54 | + { |
|
55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | + parent::__construct($dt); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Clear cached variables on clone. |
|
61 | - */ |
|
62 | - public function __clone() |
|
63 | - { |
|
64 | - $this->_formatted = null; |
|
65 | - } |
|
59 | + /** |
|
60 | + * Clear cached variables on clone. |
|
61 | + */ |
|
62 | + public function __clone() |
|
63 | + { |
|
64 | + $this->_formatted = null; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * {@inheritdoc} |
|
69 | - */ |
|
70 | - protected function _encodedContentDER(): string |
|
71 | - { |
|
72 | - if (!isset($this->_formatted)) { |
|
73 | - $dt = $this->_dateTime->setTimezone( |
|
74 | - self::_createTimeZone(self::TZ_UTC)); |
|
75 | - $this->_formatted = $dt->format('YmdHis'); |
|
76 | - // if fractions were used |
|
77 | - $frac = $dt->format('u'); |
|
78 | - if (0 !== intval($frac)) { |
|
79 | - $frac = rtrim($frac, '0'); |
|
80 | - $this->_formatted .= ".{$frac}"; |
|
81 | - } |
|
82 | - // timezone |
|
83 | - $this->_formatted .= 'Z'; |
|
84 | - } |
|
85 | - return $this->_formatted; |
|
86 | - } |
|
67 | + /** |
|
68 | + * {@inheritdoc} |
|
69 | + */ |
|
70 | + protected function _encodedContentDER(): string |
|
71 | + { |
|
72 | + if (!isset($this->_formatted)) { |
|
73 | + $dt = $this->_dateTime->setTimezone( |
|
74 | + self::_createTimeZone(self::TZ_UTC)); |
|
75 | + $this->_formatted = $dt->format('YmdHis'); |
|
76 | + // if fractions were used |
|
77 | + $frac = $dt->format('u'); |
|
78 | + if (0 !== intval($frac)) { |
|
79 | + $frac = rtrim($frac, '0'); |
|
80 | + $this->_formatted .= ".{$frac}"; |
|
81 | + } |
|
82 | + // timezone |
|
83 | + $this->_formatted .= 'Z'; |
|
84 | + } |
|
85 | + return $this->_formatted; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * {@inheritdoc} |
|
90 | - */ |
|
91 | - protected static function _decodeFromDER(Identifier $identifier, |
|
92 | - string $data, int &$offset): ElementBase |
|
93 | - { |
|
94 | - $idx = $offset; |
|
95 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
96 | - $str = substr($data, $idx, $length); |
|
97 | - $idx += $length; |
|
98 | - /** @var string[] $match */ |
|
99 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
100 | - throw new DecodeException('Invalid GeneralizedTime format.'); |
|
101 | - } |
|
102 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
103 | - // if fractions match, there's at least one digit |
|
104 | - if (isset($match[7])) { |
|
105 | - $frac = $match[7]; |
|
106 | - // DER restricts trailing zeroes in fractional seconds component |
|
107 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
108 | - throw new DecodeException( |
|
109 | - 'Fractional seconds must omit trailing zeroes.'); |
|
110 | - } |
|
111 | - $frac = $frac; |
|
112 | - } else { |
|
113 | - $frac = '0'; |
|
114 | - } |
|
115 | - $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
116 | - self::TZ_UTC; |
|
117 | - $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
118 | - self::_createTimeZone(self::TZ_UTC)); |
|
119 | - if (!$dt) { |
|
120 | - throw new DecodeException( |
|
121 | - 'Failed to decode GeneralizedTime: ' . |
|
122 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
123 | - } |
|
124 | - $offset = $idx; |
|
125 | - return new self($dt); |
|
126 | - } |
|
88 | + /** |
|
89 | + * {@inheritdoc} |
|
90 | + */ |
|
91 | + protected static function _decodeFromDER(Identifier $identifier, |
|
92 | + string $data, int &$offset): ElementBase |
|
93 | + { |
|
94 | + $idx = $offset; |
|
95 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
96 | + $str = substr($data, $idx, $length); |
|
97 | + $idx += $length; |
|
98 | + /** @var string[] $match */ |
|
99 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
100 | + throw new DecodeException('Invalid GeneralizedTime format.'); |
|
101 | + } |
|
102 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
103 | + // if fractions match, there's at least one digit |
|
104 | + if (isset($match[7])) { |
|
105 | + $frac = $match[7]; |
|
106 | + // DER restricts trailing zeroes in fractional seconds component |
|
107 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
108 | + throw new DecodeException( |
|
109 | + 'Fractional seconds must omit trailing zeroes.'); |
|
110 | + } |
|
111 | + $frac = $frac; |
|
112 | + } else { |
|
113 | + $frac = '0'; |
|
114 | + } |
|
115 | + $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
116 | + self::TZ_UTC; |
|
117 | + $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
118 | + self::_createTimeZone(self::TZ_UTC)); |
|
119 | + if (!$dt) { |
|
120 | + throw new DecodeException( |
|
121 | + 'Failed to decode GeneralizedTime: ' . |
|
122 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
123 | + } |
|
124 | + $offset = $idx; |
|
125 | + return new self($dt); |
|
126 | + } |
|
127 | 127 | } |
@@ -15,400 +15,400 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class Structure extends Element implements \Countable, \IteratorAggregate |
17 | 17 | { |
18 | - use UniversalClass; |
|
18 | + use UniversalClass; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Array of elements in the structure. |
|
22 | - * |
|
23 | - * @var Element[] |
|
24 | - */ |
|
25 | - protected $_elements; |
|
20 | + /** |
|
21 | + * Array of elements in the structure. |
|
22 | + * |
|
23 | + * @var Element[] |
|
24 | + */ |
|
25 | + protected $_elements; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Lookup table for the tagged elements. |
|
29 | - * |
|
30 | - * @var null|TaggedType[] |
|
31 | - */ |
|
32 | - private $_taggedMap; |
|
27 | + /** |
|
28 | + * Lookup table for the tagged elements. |
|
29 | + * |
|
30 | + * @var null|TaggedType[] |
|
31 | + */ |
|
32 | + private $_taggedMap; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Cache variable of elements wrapped into `UnspecifiedType` objects. |
|
36 | - * |
|
37 | - * @var null|UnspecifiedType[] |
|
38 | - */ |
|
39 | - private $_unspecifiedTypes; |
|
34 | + /** |
|
35 | + * Cache variable of elements wrapped into `UnspecifiedType` objects. |
|
36 | + * |
|
37 | + * @var null|UnspecifiedType[] |
|
38 | + */ |
|
39 | + private $_unspecifiedTypes; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Constructor. |
|
43 | - * |
|
44 | - * @param ElementBase ...$elements Any number of elements |
|
45 | - */ |
|
46 | - public function __construct(ElementBase ...$elements) |
|
47 | - { |
|
48 | - $this->_elements = array_map( |
|
49 | - function (ElementBase $el) { |
|
50 | - return $el->asElement(); |
|
51 | - }, $elements); |
|
52 | - } |
|
41 | + /** |
|
42 | + * Constructor. |
|
43 | + * |
|
44 | + * @param ElementBase ...$elements Any number of elements |
|
45 | + */ |
|
46 | + public function __construct(ElementBase ...$elements) |
|
47 | + { |
|
48 | + $this->_elements = array_map( |
|
49 | + function (ElementBase $el) { |
|
50 | + return $el->asElement(); |
|
51 | + }, $elements); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Clone magic method. |
|
56 | - */ |
|
57 | - public function __clone() |
|
58 | - { |
|
59 | - // clear cache-variables |
|
60 | - $this->_taggedMap = null; |
|
61 | - $this->_unspecifiedTypes = null; |
|
62 | - } |
|
54 | + /** |
|
55 | + * Clone magic method. |
|
56 | + */ |
|
57 | + public function __clone() |
|
58 | + { |
|
59 | + // clear cache-variables |
|
60 | + $this->_taggedMap = null; |
|
61 | + $this->_unspecifiedTypes = null; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function isConstructed(): bool |
|
68 | - { |
|
69 | - return true; |
|
70 | - } |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function isConstructed(): bool |
|
68 | + { |
|
69 | + return true; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Explode DER structure to DER encoded components that it contains. |
|
74 | - * |
|
75 | - * @param string $data |
|
76 | - * |
|
77 | - * @throws DecodeException |
|
78 | - * |
|
79 | - * @return string[] |
|
80 | - */ |
|
81 | - public static function explodeDER(string $data): array |
|
82 | - { |
|
83 | - $offset = 0; |
|
84 | - $identifier = Identifier::fromDER($data, $offset); |
|
85 | - if (!$identifier->isConstructed()) { |
|
86 | - throw new DecodeException('Element is not constructed.'); |
|
87 | - } |
|
88 | - $length = Length::expectFromDER($data, $offset); |
|
89 | - if ($length->isIndefinite()) { |
|
90 | - throw new DecodeException( |
|
91 | - 'Explode not implemented for indefinite length encoding.'); |
|
92 | - } |
|
93 | - $end = $offset + $length->intLength(); |
|
94 | - $parts = []; |
|
95 | - while ($offset < $end) { |
|
96 | - // start of the element |
|
97 | - $idx = $offset; |
|
98 | - // skip identifier |
|
99 | - Identifier::fromDER($data, $offset); |
|
100 | - // decode element length |
|
101 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
102 | - // extract der encoding of the element |
|
103 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
104 | - // update offset over content |
|
105 | - $offset += $length; |
|
106 | - } |
|
107 | - return $parts; |
|
108 | - } |
|
72 | + /** |
|
73 | + * Explode DER structure to DER encoded components that it contains. |
|
74 | + * |
|
75 | + * @param string $data |
|
76 | + * |
|
77 | + * @throws DecodeException |
|
78 | + * |
|
79 | + * @return string[] |
|
80 | + */ |
|
81 | + public static function explodeDER(string $data): array |
|
82 | + { |
|
83 | + $offset = 0; |
|
84 | + $identifier = Identifier::fromDER($data, $offset); |
|
85 | + if (!$identifier->isConstructed()) { |
|
86 | + throw new DecodeException('Element is not constructed.'); |
|
87 | + } |
|
88 | + $length = Length::expectFromDER($data, $offset); |
|
89 | + if ($length->isIndefinite()) { |
|
90 | + throw new DecodeException( |
|
91 | + 'Explode not implemented for indefinite length encoding.'); |
|
92 | + } |
|
93 | + $end = $offset + $length->intLength(); |
|
94 | + $parts = []; |
|
95 | + while ($offset < $end) { |
|
96 | + // start of the element |
|
97 | + $idx = $offset; |
|
98 | + // skip identifier |
|
99 | + Identifier::fromDER($data, $offset); |
|
100 | + // decode element length |
|
101 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
102 | + // extract der encoding of the element |
|
103 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
104 | + // update offset over content |
|
105 | + $offset += $length; |
|
106 | + } |
|
107 | + return $parts; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Get self with an element at the given index replaced by another. |
|
112 | - * |
|
113 | - * @param int $idx Element index |
|
114 | - * @param Element $el New element to insert into the structure |
|
115 | - * |
|
116 | - * @throws \OutOfBoundsException |
|
117 | - * |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public function withReplaced(int $idx, Element $el): self |
|
121 | - { |
|
122 | - if (!isset($this->_elements[$idx])) { |
|
123 | - throw new \OutOfBoundsException( |
|
124 | - "Structure doesn't have element at index {$idx}."); |
|
125 | - } |
|
126 | - $obj = clone $this; |
|
127 | - $obj->_elements[$idx] = $el; |
|
128 | - return $obj; |
|
129 | - } |
|
110 | + /** |
|
111 | + * Get self with an element at the given index replaced by another. |
|
112 | + * |
|
113 | + * @param int $idx Element index |
|
114 | + * @param Element $el New element to insert into the structure |
|
115 | + * |
|
116 | + * @throws \OutOfBoundsException |
|
117 | + * |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public function withReplaced(int $idx, Element $el): self |
|
121 | + { |
|
122 | + if (!isset($this->_elements[$idx])) { |
|
123 | + throw new \OutOfBoundsException( |
|
124 | + "Structure doesn't have element at index {$idx}."); |
|
125 | + } |
|
126 | + $obj = clone $this; |
|
127 | + $obj->_elements[$idx] = $el; |
|
128 | + return $obj; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Get self with an element inserted before the given index. |
|
133 | - * |
|
134 | - * @param int $idx Element index |
|
135 | - * @param Element $el New element to insert into the structure |
|
136 | - * |
|
137 | - * @throws \OutOfBoundsException |
|
138 | - * |
|
139 | - * @return self |
|
140 | - */ |
|
141 | - public function withInserted(int $idx, Element $el): self |
|
142 | - { |
|
143 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
144 | - throw new \OutOfBoundsException("Index {$idx} is out of bounds."); |
|
145 | - } |
|
146 | - $obj = clone $this; |
|
147 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
148 | - return $obj; |
|
149 | - } |
|
131 | + /** |
|
132 | + * Get self with an element inserted before the given index. |
|
133 | + * |
|
134 | + * @param int $idx Element index |
|
135 | + * @param Element $el New element to insert into the structure |
|
136 | + * |
|
137 | + * @throws \OutOfBoundsException |
|
138 | + * |
|
139 | + * @return self |
|
140 | + */ |
|
141 | + public function withInserted(int $idx, Element $el): self |
|
142 | + { |
|
143 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
144 | + throw new \OutOfBoundsException("Index {$idx} is out of bounds."); |
|
145 | + } |
|
146 | + $obj = clone $this; |
|
147 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
148 | + return $obj; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get self with an element appended to the end. |
|
153 | - * |
|
154 | - * @param Element $el Element to insert into the structure |
|
155 | - * |
|
156 | - * @return self |
|
157 | - */ |
|
158 | - public function withAppended(Element $el): self |
|
159 | - { |
|
160 | - $obj = clone $this; |
|
161 | - array_push($obj->_elements, $el); |
|
162 | - return $obj; |
|
163 | - } |
|
151 | + /** |
|
152 | + * Get self with an element appended to the end. |
|
153 | + * |
|
154 | + * @param Element $el Element to insert into the structure |
|
155 | + * |
|
156 | + * @return self |
|
157 | + */ |
|
158 | + public function withAppended(Element $el): self |
|
159 | + { |
|
160 | + $obj = clone $this; |
|
161 | + array_push($obj->_elements, $el); |
|
162 | + return $obj; |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * Get self with an element prepended in the beginning. |
|
167 | - * |
|
168 | - * @param Element $el Element to insert into the structure |
|
169 | - * |
|
170 | - * @return self |
|
171 | - */ |
|
172 | - public function withPrepended(Element $el): self |
|
173 | - { |
|
174 | - $obj = clone $this; |
|
175 | - array_unshift($obj->_elements, $el); |
|
176 | - return $obj; |
|
177 | - } |
|
165 | + /** |
|
166 | + * Get self with an element prepended in the beginning. |
|
167 | + * |
|
168 | + * @param Element $el Element to insert into the structure |
|
169 | + * |
|
170 | + * @return self |
|
171 | + */ |
|
172 | + public function withPrepended(Element $el): self |
|
173 | + { |
|
174 | + $obj = clone $this; |
|
175 | + array_unshift($obj->_elements, $el); |
|
176 | + return $obj; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Get self with an element at the given index removed. |
|
181 | - * |
|
182 | - * @param int $idx Element index |
|
183 | - * |
|
184 | - * @throws \OutOfBoundsException |
|
185 | - * |
|
186 | - * @return self |
|
187 | - */ |
|
188 | - public function withoutElement(int $idx): self |
|
189 | - { |
|
190 | - if (!isset($this->_elements[$idx])) { |
|
191 | - throw new \OutOfBoundsException( |
|
192 | - "Structure doesn't have element at index {$idx}."); |
|
193 | - } |
|
194 | - $obj = clone $this; |
|
195 | - array_splice($obj->_elements, $idx, 1); |
|
196 | - return $obj; |
|
197 | - } |
|
179 | + /** |
|
180 | + * Get self with an element at the given index removed. |
|
181 | + * |
|
182 | + * @param int $idx Element index |
|
183 | + * |
|
184 | + * @throws \OutOfBoundsException |
|
185 | + * |
|
186 | + * @return self |
|
187 | + */ |
|
188 | + public function withoutElement(int $idx): self |
|
189 | + { |
|
190 | + if (!isset($this->_elements[$idx])) { |
|
191 | + throw new \OutOfBoundsException( |
|
192 | + "Structure doesn't have element at index {$idx}."); |
|
193 | + } |
|
194 | + $obj = clone $this; |
|
195 | + array_splice($obj->_elements, $idx, 1); |
|
196 | + return $obj; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get elements in the structure. |
|
201 | - * |
|
202 | - * @return UnspecifiedType[] |
|
203 | - */ |
|
204 | - public function elements(): array |
|
205 | - { |
|
206 | - if (!isset($this->_unspecifiedTypes)) { |
|
207 | - $this->_unspecifiedTypes = array_map( |
|
208 | - function (Element $el) { |
|
209 | - return new UnspecifiedType($el); |
|
210 | - }, $this->_elements); |
|
211 | - } |
|
212 | - return $this->_unspecifiedTypes; |
|
213 | - } |
|
199 | + /** |
|
200 | + * Get elements in the structure. |
|
201 | + * |
|
202 | + * @return UnspecifiedType[] |
|
203 | + */ |
|
204 | + public function elements(): array |
|
205 | + { |
|
206 | + if (!isset($this->_unspecifiedTypes)) { |
|
207 | + $this->_unspecifiedTypes = array_map( |
|
208 | + function (Element $el) { |
|
209 | + return new UnspecifiedType($el); |
|
210 | + }, $this->_elements); |
|
211 | + } |
|
212 | + return $this->_unspecifiedTypes; |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * Check whether the structure has an element at the given index, optionally |
|
217 | - * satisfying given tag expectation. |
|
218 | - * |
|
219 | - * @param int $idx Index 0..n |
|
220 | - * @param null|int $expectedTag Optional type tag expectation |
|
221 | - * |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - public function has(int $idx, ?int $expectedTag = null): bool |
|
225 | - { |
|
226 | - if (!isset($this->_elements[$idx])) { |
|
227 | - return false; |
|
228 | - } |
|
229 | - if (isset($expectedTag)) { |
|
230 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
231 | - return false; |
|
232 | - } |
|
233 | - } |
|
234 | - return true; |
|
235 | - } |
|
215 | + /** |
|
216 | + * Check whether the structure has an element at the given index, optionally |
|
217 | + * satisfying given tag expectation. |
|
218 | + * |
|
219 | + * @param int $idx Index 0..n |
|
220 | + * @param null|int $expectedTag Optional type tag expectation |
|
221 | + * |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + public function has(int $idx, ?int $expectedTag = null): bool |
|
225 | + { |
|
226 | + if (!isset($this->_elements[$idx])) { |
|
227 | + return false; |
|
228 | + } |
|
229 | + if (isset($expectedTag)) { |
|
230 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
231 | + return false; |
|
232 | + } |
|
233 | + } |
|
234 | + return true; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Get the element at the given index, optionally checking that the element |
|
239 | - * has a given tag. |
|
240 | - * |
|
241 | - * @param int $idx Index 0..n |
|
242 | - * |
|
243 | - * @throws \OutOfBoundsException If element doesn't exists |
|
244 | - * @throws \UnexpectedValueException If expectation fails |
|
245 | - * |
|
246 | - * @return UnspecifiedType |
|
247 | - */ |
|
248 | - public function at(int $idx): UnspecifiedType |
|
249 | - { |
|
250 | - if (!isset($this->_elements[$idx])) { |
|
251 | - throw new \OutOfBoundsException( |
|
252 | - "Structure doesn't have an element at index {$idx}."); |
|
253 | - } |
|
254 | - return new UnspecifiedType($this->_elements[$idx]); |
|
255 | - } |
|
237 | + /** |
|
238 | + * Get the element at the given index, optionally checking that the element |
|
239 | + * has a given tag. |
|
240 | + * |
|
241 | + * @param int $idx Index 0..n |
|
242 | + * |
|
243 | + * @throws \OutOfBoundsException If element doesn't exists |
|
244 | + * @throws \UnexpectedValueException If expectation fails |
|
245 | + * |
|
246 | + * @return UnspecifiedType |
|
247 | + */ |
|
248 | + public function at(int $idx): UnspecifiedType |
|
249 | + { |
|
250 | + if (!isset($this->_elements[$idx])) { |
|
251 | + throw new \OutOfBoundsException( |
|
252 | + "Structure doesn't have an element at index {$idx}."); |
|
253 | + } |
|
254 | + return new UnspecifiedType($this->_elements[$idx]); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Check whether the structure contains a context specific element with a |
|
259 | - * given tag. |
|
260 | - * |
|
261 | - * @param int $tag Tag number |
|
262 | - * |
|
263 | - * @return bool |
|
264 | - */ |
|
265 | - public function hasTagged(int $tag): bool |
|
266 | - { |
|
267 | - // lazily build lookup map |
|
268 | - if (!isset($this->_taggedMap)) { |
|
269 | - $this->_taggedMap = []; |
|
270 | - foreach ($this->_elements as $element) { |
|
271 | - if ($element->isTagged()) { |
|
272 | - $this->_taggedMap[$element->tag()] = $element; |
|
273 | - } |
|
274 | - } |
|
275 | - } |
|
276 | - return isset($this->_taggedMap[$tag]); |
|
277 | - } |
|
257 | + /** |
|
258 | + * Check whether the structure contains a context specific element with a |
|
259 | + * given tag. |
|
260 | + * |
|
261 | + * @param int $tag Tag number |
|
262 | + * |
|
263 | + * @return bool |
|
264 | + */ |
|
265 | + public function hasTagged(int $tag): bool |
|
266 | + { |
|
267 | + // lazily build lookup map |
|
268 | + if (!isset($this->_taggedMap)) { |
|
269 | + $this->_taggedMap = []; |
|
270 | + foreach ($this->_elements as $element) { |
|
271 | + if ($element->isTagged()) { |
|
272 | + $this->_taggedMap[$element->tag()] = $element; |
|
273 | + } |
|
274 | + } |
|
275 | + } |
|
276 | + return isset($this->_taggedMap[$tag]); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * Get a context specific element tagged with a given tag. |
|
281 | - * |
|
282 | - * @param int $tag |
|
283 | - * |
|
284 | - * @throws \LogicException If tag doesn't exists |
|
285 | - * |
|
286 | - * @return TaggedType |
|
287 | - */ |
|
288 | - public function getTagged(int $tag): TaggedType |
|
289 | - { |
|
290 | - if (!$this->hasTagged($tag)) { |
|
291 | - throw new \LogicException("No tagged element for tag {$tag}."); |
|
292 | - } |
|
293 | - return $this->_taggedMap[$tag]; |
|
294 | - } |
|
279 | + /** |
|
280 | + * Get a context specific element tagged with a given tag. |
|
281 | + * |
|
282 | + * @param int $tag |
|
283 | + * |
|
284 | + * @throws \LogicException If tag doesn't exists |
|
285 | + * |
|
286 | + * @return TaggedType |
|
287 | + */ |
|
288 | + public function getTagged(int $tag): TaggedType |
|
289 | + { |
|
290 | + if (!$this->hasTagged($tag)) { |
|
291 | + throw new \LogicException("No tagged element for tag {$tag}."); |
|
292 | + } |
|
293 | + return $this->_taggedMap[$tag]; |
|
294 | + } |
|
295 | 295 | |
296 | - /** |
|
297 | - * @see \Countable::count() |
|
298 | - * |
|
299 | - * @return int |
|
300 | - */ |
|
301 | - public function count(): int |
|
302 | - { |
|
303 | - return count($this->_elements); |
|
304 | - } |
|
296 | + /** |
|
297 | + * @see \Countable::count() |
|
298 | + * |
|
299 | + * @return int |
|
300 | + */ |
|
301 | + public function count(): int |
|
302 | + { |
|
303 | + return count($this->_elements); |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
307 | - * Get an iterator for the `UnspecifiedElement` objects. |
|
308 | - * |
|
309 | - * @see \IteratorAggregate::getIterator() |
|
310 | - * |
|
311 | - * @return \ArrayIterator |
|
312 | - */ |
|
313 | - public function getIterator(): \ArrayIterator |
|
314 | - { |
|
315 | - return new \ArrayIterator($this->elements()); |
|
316 | - } |
|
306 | + /** |
|
307 | + * Get an iterator for the `UnspecifiedElement` objects. |
|
308 | + * |
|
309 | + * @see \IteratorAggregate::getIterator() |
|
310 | + * |
|
311 | + * @return \ArrayIterator |
|
312 | + */ |
|
313 | + public function getIterator(): \ArrayIterator |
|
314 | + { |
|
315 | + return new \ArrayIterator($this->elements()); |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * {@inheritdoc} |
|
320 | - */ |
|
321 | - protected function _encodedContentDER(): string |
|
322 | - { |
|
323 | - $data = ''; |
|
324 | - foreach ($this->_elements as $element) { |
|
325 | - $data .= $element->toDER(); |
|
326 | - } |
|
327 | - return $data; |
|
328 | - } |
|
318 | + /** |
|
319 | + * {@inheritdoc} |
|
320 | + */ |
|
321 | + protected function _encodedContentDER(): string |
|
322 | + { |
|
323 | + $data = ''; |
|
324 | + foreach ($this->_elements as $element) { |
|
325 | + $data .= $element->toDER(); |
|
326 | + } |
|
327 | + return $data; |
|
328 | + } |
|
329 | 329 | |
330 | - /** |
|
331 | - * {@inheritdoc} |
|
332 | - * |
|
333 | - * @return self |
|
334 | - */ |
|
335 | - protected static function _decodeFromDER(Identifier $identifier, |
|
336 | - string $data, int &$offset): ElementBase |
|
337 | - { |
|
338 | - if (!$identifier->isConstructed()) { |
|
339 | - throw new DecodeException( |
|
340 | - 'Structured element must have constructed bit set.'); |
|
341 | - } |
|
342 | - $idx = $offset; |
|
343 | - $length = Length::expectFromDER($data, $idx); |
|
344 | - if ($length->isIndefinite()) { |
|
345 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
346 | - } else { |
|
347 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
348 | - $length->intLength()); |
|
349 | - } |
|
350 | - $offset = $idx; |
|
351 | - return $type; |
|
352 | - } |
|
330 | + /** |
|
331 | + * {@inheritdoc} |
|
332 | + * |
|
333 | + * @return self |
|
334 | + */ |
|
335 | + protected static function _decodeFromDER(Identifier $identifier, |
|
336 | + string $data, int &$offset): ElementBase |
|
337 | + { |
|
338 | + if (!$identifier->isConstructed()) { |
|
339 | + throw new DecodeException( |
|
340 | + 'Structured element must have constructed bit set.'); |
|
341 | + } |
|
342 | + $idx = $offset; |
|
343 | + $length = Length::expectFromDER($data, $idx); |
|
344 | + if ($length->isIndefinite()) { |
|
345 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
346 | + } else { |
|
347 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
348 | + $length->intLength()); |
|
349 | + } |
|
350 | + $offset = $idx; |
|
351 | + return $type; |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * Decode elements for a definite length. |
|
356 | - * |
|
357 | - * @param string $data DER data |
|
358 | - * @param int $offset Offset to data |
|
359 | - * @param int $length Number of bytes to decode |
|
360 | - * |
|
361 | - * @throws DecodeException |
|
362 | - * |
|
363 | - * @return ElementBase |
|
364 | - */ |
|
365 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
366 | - int $length): ElementBase |
|
367 | - { |
|
368 | - $idx = $offset; |
|
369 | - $end = $idx + $length; |
|
370 | - $elements = []; |
|
371 | - while ($idx < $end) { |
|
372 | - $elements[] = Element::fromDER($data, $idx); |
|
373 | - // check that element didn't overflow length |
|
374 | - if ($idx > $end) { |
|
375 | - throw new DecodeException("Structure's content overflows length."); |
|
376 | - } |
|
377 | - } |
|
378 | - $offset = $idx; |
|
379 | - // return instance by static late binding |
|
380 | - return new static(...$elements); |
|
381 | - } |
|
354 | + /** |
|
355 | + * Decode elements for a definite length. |
|
356 | + * |
|
357 | + * @param string $data DER data |
|
358 | + * @param int $offset Offset to data |
|
359 | + * @param int $length Number of bytes to decode |
|
360 | + * |
|
361 | + * @throws DecodeException |
|
362 | + * |
|
363 | + * @return ElementBase |
|
364 | + */ |
|
365 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
366 | + int $length): ElementBase |
|
367 | + { |
|
368 | + $idx = $offset; |
|
369 | + $end = $idx + $length; |
|
370 | + $elements = []; |
|
371 | + while ($idx < $end) { |
|
372 | + $elements[] = Element::fromDER($data, $idx); |
|
373 | + // check that element didn't overflow length |
|
374 | + if ($idx > $end) { |
|
375 | + throw new DecodeException("Structure's content overflows length."); |
|
376 | + } |
|
377 | + } |
|
378 | + $offset = $idx; |
|
379 | + // return instance by static late binding |
|
380 | + return new static(...$elements); |
|
381 | + } |
|
382 | 382 | |
383 | - /** |
|
384 | - * Decode elements for an indefinite length. |
|
385 | - * |
|
386 | - * @param string $data DER data |
|
387 | - * @param int $offset Offset to data |
|
388 | - * |
|
389 | - * @throws DecodeException |
|
390 | - * |
|
391 | - * @return ElementBase |
|
392 | - */ |
|
393 | - private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase |
|
394 | - { |
|
395 | - $idx = $offset; |
|
396 | - $elements = []; |
|
397 | - $end = strlen($data); |
|
398 | - while (true) { |
|
399 | - if ($idx >= $end) { |
|
400 | - throw new DecodeException( |
|
401 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
402 | - } |
|
403 | - $el = Element::fromDER($data, $idx); |
|
404 | - if ($el->isType(self::TYPE_EOC)) { |
|
405 | - break; |
|
406 | - } |
|
407 | - $elements[] = $el; |
|
408 | - } |
|
409 | - $offset = $idx; |
|
410 | - $type = new static(...$elements); |
|
411 | - $type->_indefiniteLength = true; |
|
412 | - return $type; |
|
413 | - } |
|
383 | + /** |
|
384 | + * Decode elements for an indefinite length. |
|
385 | + * |
|
386 | + * @param string $data DER data |
|
387 | + * @param int $offset Offset to data |
|
388 | + * |
|
389 | + * @throws DecodeException |
|
390 | + * |
|
391 | + * @return ElementBase |
|
392 | + */ |
|
393 | + private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase |
|
394 | + { |
|
395 | + $idx = $offset; |
|
396 | + $elements = []; |
|
397 | + $end = strlen($data); |
|
398 | + while (true) { |
|
399 | + if ($idx >= $end) { |
|
400 | + throw new DecodeException( |
|
401 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
402 | + } |
|
403 | + $el = Element::fromDER($data, $idx); |
|
404 | + if ($el->isType(self::TYPE_EOC)) { |
|
405 | + break; |
|
406 | + } |
|
407 | + $elements[] = $el; |
|
408 | + } |
|
409 | + $offset = $idx; |
|
410 | + $type = new static(...$elements); |
|
411 | + $type->_indefiniteLength = true; |
|
412 | + return $type; |
|
413 | + } |
|
414 | 414 | } |
@@ -13,90 +13,90 @@ |
||
13 | 13 | */ |
14 | 14 | interface ElementBase extends Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Get the class of the ASN.1 type. |
|
18 | - * |
|
19 | - * One of `Identifier::CLASS_*` constants. |
|
20 | - * |
|
21 | - * @return int |
|
22 | - */ |
|
23 | - public function typeClass(): int; |
|
16 | + /** |
|
17 | + * Get the class of the ASN.1 type. |
|
18 | + * |
|
19 | + * One of `Identifier::CLASS_*` constants. |
|
20 | + * |
|
21 | + * @return int |
|
22 | + */ |
|
23 | + public function typeClass(): int; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Check whether the element is constructed. |
|
27 | - * |
|
28 | - * Otherwise it's primitive. |
|
29 | - * |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function isConstructed(): bool; |
|
25 | + /** |
|
26 | + * Check whether the element is constructed. |
|
27 | + * |
|
28 | + * Otherwise it's primitive. |
|
29 | + * |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function isConstructed(): bool; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the tag of the element. |
|
36 | - * |
|
37 | - * Interpretation of the tag depends on the context. For example it may |
|
38 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | - * tagged type. |
|
40 | - * |
|
41 | - * @return int |
|
42 | - */ |
|
43 | - public function tag(): int; |
|
34 | + /** |
|
35 | + * Get the tag of the element. |
|
36 | + * |
|
37 | + * Interpretation of the tag depends on the context. For example it may |
|
38 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | + * tagged type. |
|
40 | + * |
|
41 | + * @return int |
|
42 | + */ |
|
43 | + public function tag(): int; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Check whether the element is a type of a given tag. |
|
47 | - * |
|
48 | - * @param int $tag Type tag |
|
49 | - * |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - public function isType(int $tag): bool; |
|
45 | + /** |
|
46 | + * Check whether the element is a type of a given tag. |
|
47 | + * |
|
48 | + * @param int $tag Type tag |
|
49 | + * |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function isType(int $tag): bool; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Check whether the element is a type of a given tag. |
|
56 | - * |
|
57 | - * Throws an exception if expectation fails. |
|
58 | - * |
|
59 | - * @param int $tag Type tag |
|
60 | - * |
|
61 | - * @throws \UnexpectedValueException If the element type differs from the expected |
|
62 | - * |
|
63 | - * @return ElementBase |
|
64 | - */ |
|
65 | - public function expectType(int $tag): ElementBase; |
|
54 | + /** |
|
55 | + * Check whether the element is a type of a given tag. |
|
56 | + * |
|
57 | + * Throws an exception if expectation fails. |
|
58 | + * |
|
59 | + * @param int $tag Type tag |
|
60 | + * |
|
61 | + * @throws \UnexpectedValueException If the element type differs from the expected |
|
62 | + * |
|
63 | + * @return ElementBase |
|
64 | + */ |
|
65 | + public function expectType(int $tag): ElementBase; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Check whether the element is tagged (context specific). |
|
69 | - * |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - public function isTagged(): bool; |
|
67 | + /** |
|
68 | + * Check whether the element is tagged (context specific). |
|
69 | + * |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + public function isTagged(): bool; |
|
73 | 73 | |
74 | - /** |
|
75 | - * Check whether the element is tagged (context specific) and optionally has |
|
76 | - * a given tag. |
|
77 | - * |
|
78 | - * Throws an exception if the element is not tagged or tag differs from |
|
79 | - * the expected. |
|
80 | - * |
|
81 | - * @param null|int $tag Optional type tag |
|
82 | - * |
|
83 | - * @throws \UnexpectedValueException If expectation fails |
|
84 | - * |
|
85 | - * @return TaggedType |
|
86 | - */ |
|
87 | - public function expectTagged(?int $tag = null): TaggedType; |
|
74 | + /** |
|
75 | + * Check whether the element is tagged (context specific) and optionally has |
|
76 | + * a given tag. |
|
77 | + * |
|
78 | + * Throws an exception if the element is not tagged or tag differs from |
|
79 | + * the expected. |
|
80 | + * |
|
81 | + * @param null|int $tag Optional type tag |
|
82 | + * |
|
83 | + * @throws \UnexpectedValueException If expectation fails |
|
84 | + * |
|
85 | + * @return TaggedType |
|
86 | + */ |
|
87 | + public function expectTagged(?int $tag = null): TaggedType; |
|
88 | 88 | |
89 | - /** |
|
90 | - * Get the object as an abstract `Element` instance. |
|
91 | - * |
|
92 | - * @return Element |
|
93 | - */ |
|
94 | - public function asElement(): Element; |
|
89 | + /** |
|
90 | + * Get the object as an abstract `Element` instance. |
|
91 | + * |
|
92 | + * @return Element |
|
93 | + */ |
|
94 | + public function asElement(): Element; |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get the object as an `UnspecifiedType` instance. |
|
98 | - * |
|
99 | - * @return UnspecifiedType |
|
100 | - */ |
|
101 | - public function asUnspecified(): UnspecifiedType; |
|
96 | + /** |
|
97 | + * Get the object as an `UnspecifiedType` instance. |
|
98 | + * |
|
99 | + * @return UnspecifiedType |
|
100 | + */ |
|
101 | + public function asUnspecified(): UnspecifiedType; |
|
102 | 102 | } |
@@ -13,223 +13,223 @@ |
||
13 | 13 | */ |
14 | 14 | class Length implements Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Length. |
|
18 | - * |
|
19 | - * @var BigInt |
|
20 | - */ |
|
21 | - private $_length; |
|
16 | + /** |
|
17 | + * Length. |
|
18 | + * |
|
19 | + * @var BigInt |
|
20 | + */ |
|
21 | + private $_length; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Whether length is indefinite. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_indefinite; |
|
23 | + /** |
|
24 | + * Whether length is indefinite. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_indefinite; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $length Length |
|
34 | - * @param bool $indefinite Whether length is indefinite |
|
35 | - */ |
|
36 | - public function __construct($length, bool $indefinite = false) |
|
37 | - { |
|
38 | - $this->_length = new BigInt($length); |
|
39 | - $this->_indefinite = $indefinite; |
|
40 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $length Length |
|
34 | + * @param bool $indefinite Whether length is indefinite |
|
35 | + */ |
|
36 | + public function __construct($length, bool $indefinite = false) |
|
37 | + { |
|
38 | + $this->_length = new BigInt($length); |
|
39 | + $this->_indefinite = $indefinite; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Decode length component from DER data. |
|
44 | - * |
|
45 | - * @param string $data DER encoded data |
|
46 | - * @param null|int $offset Reference to the variable that contains offset |
|
47 | - * into the data where to start parsing. |
|
48 | - * Variable is updated to the offset next to the |
|
49 | - * parsed length component. If null, start from offset 0. |
|
50 | - * |
|
51 | - * @throws DecodeException If decoding fails |
|
52 | - * |
|
53 | - * @return self |
|
54 | - */ |
|
55 | - public static function fromDER(string $data, int &$offset = null): self |
|
56 | - { |
|
57 | - $idx = $offset ?? 0; |
|
58 | - $datalen = strlen($data); |
|
59 | - if ($idx >= $datalen) { |
|
60 | - throw new DecodeException( |
|
61 | - 'Unexpected end of data while decoding length.'); |
|
62 | - } |
|
63 | - $indefinite = false; |
|
64 | - $byte = ord($data[$idx++]); |
|
65 | - // bits 7 to 1 |
|
66 | - $length = (0x7f & $byte); |
|
67 | - // long form |
|
68 | - if (0x80 & $byte) { |
|
69 | - if (!$length) { |
|
70 | - $indefinite = true; |
|
71 | - } else { |
|
72 | - if ($idx + $length > $datalen) { |
|
73 | - throw new DecodeException( |
|
74 | - 'Unexpected end of data while decoding long form length.'); |
|
75 | - } |
|
76 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | - } |
|
78 | - } |
|
79 | - if (isset($offset)) { |
|
80 | - $offset = $idx; |
|
81 | - } |
|
82 | - return new self($length, $indefinite); |
|
83 | - } |
|
42 | + /** |
|
43 | + * Decode length component from DER data. |
|
44 | + * |
|
45 | + * @param string $data DER encoded data |
|
46 | + * @param null|int $offset Reference to the variable that contains offset |
|
47 | + * into the data where to start parsing. |
|
48 | + * Variable is updated to the offset next to the |
|
49 | + * parsed length component. If null, start from offset 0. |
|
50 | + * |
|
51 | + * @throws DecodeException If decoding fails |
|
52 | + * |
|
53 | + * @return self |
|
54 | + */ |
|
55 | + public static function fromDER(string $data, int &$offset = null): self |
|
56 | + { |
|
57 | + $idx = $offset ?? 0; |
|
58 | + $datalen = strlen($data); |
|
59 | + if ($idx >= $datalen) { |
|
60 | + throw new DecodeException( |
|
61 | + 'Unexpected end of data while decoding length.'); |
|
62 | + } |
|
63 | + $indefinite = false; |
|
64 | + $byte = ord($data[$idx++]); |
|
65 | + // bits 7 to 1 |
|
66 | + $length = (0x7f & $byte); |
|
67 | + // long form |
|
68 | + if (0x80 & $byte) { |
|
69 | + if (!$length) { |
|
70 | + $indefinite = true; |
|
71 | + } else { |
|
72 | + if ($idx + $length > $datalen) { |
|
73 | + throw new DecodeException( |
|
74 | + 'Unexpected end of data while decoding long form length.'); |
|
75 | + } |
|
76 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | + } |
|
78 | + } |
|
79 | + if (isset($offset)) { |
|
80 | + $offset = $idx; |
|
81 | + } |
|
82 | + return new self($length, $indefinite); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Decode length from DER. |
|
87 | - * |
|
88 | - * Throws an exception if length doesn't match with expected or if data |
|
89 | - * doesn't contain enough bytes. |
|
90 | - * |
|
91 | - * Requirement of definite length is relaxed contrary to the specification |
|
92 | - * (sect. 10.1). |
|
93 | - * |
|
94 | - * @see self::fromDER |
|
95 | - * |
|
96 | - * @param string $data DER data |
|
97 | - * @param int $offset Reference to the offset variable |
|
98 | - * @param null|int $expected Expected length, null to bypass checking |
|
99 | - * |
|
100 | - * @throws DecodeException If decoding or expectation fails |
|
101 | - * |
|
102 | - * @return self |
|
103 | - */ |
|
104 | - public static function expectFromDER(string $data, int &$offset, |
|
105 | - int $expected = null): self |
|
106 | - { |
|
107 | - $idx = $offset; |
|
108 | - $length = self::fromDER($data, $idx); |
|
109 | - // if certain length was expected |
|
110 | - if (isset($expected)) { |
|
111 | - if ($length->isIndefinite()) { |
|
112 | - throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | - $expected); |
|
114 | - } |
|
115 | - if ($expected !== $length->intLength()) { |
|
116 | - throw new DecodeException( |
|
117 | - sprintf('Expected length %d, got %d.', $expected, |
|
118 | - $length->intLength())); |
|
119 | - } |
|
120 | - } |
|
121 | - // check that enough data is available |
|
122 | - if (!$length->isIndefinite() && |
|
123 | - strlen($data) < $idx + $length->intLength()) { |
|
124 | - throw new DecodeException( |
|
125 | - sprintf('Length %d overflows data, %d bytes left.', |
|
126 | - $length->intLength(), strlen($data) - $idx)); |
|
127 | - } |
|
128 | - $offset = $idx; |
|
129 | - return $length; |
|
130 | - } |
|
85 | + /** |
|
86 | + * Decode length from DER. |
|
87 | + * |
|
88 | + * Throws an exception if length doesn't match with expected or if data |
|
89 | + * doesn't contain enough bytes. |
|
90 | + * |
|
91 | + * Requirement of definite length is relaxed contrary to the specification |
|
92 | + * (sect. 10.1). |
|
93 | + * |
|
94 | + * @see self::fromDER |
|
95 | + * |
|
96 | + * @param string $data DER data |
|
97 | + * @param int $offset Reference to the offset variable |
|
98 | + * @param null|int $expected Expected length, null to bypass checking |
|
99 | + * |
|
100 | + * @throws DecodeException If decoding or expectation fails |
|
101 | + * |
|
102 | + * @return self |
|
103 | + */ |
|
104 | + public static function expectFromDER(string $data, int &$offset, |
|
105 | + int $expected = null): self |
|
106 | + { |
|
107 | + $idx = $offset; |
|
108 | + $length = self::fromDER($data, $idx); |
|
109 | + // if certain length was expected |
|
110 | + if (isset($expected)) { |
|
111 | + if ($length->isIndefinite()) { |
|
112 | + throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | + $expected); |
|
114 | + } |
|
115 | + if ($expected !== $length->intLength()) { |
|
116 | + throw new DecodeException( |
|
117 | + sprintf('Expected length %d, got %d.', $expected, |
|
118 | + $length->intLength())); |
|
119 | + } |
|
120 | + } |
|
121 | + // check that enough data is available |
|
122 | + if (!$length->isIndefinite() && |
|
123 | + strlen($data) < $idx + $length->intLength()) { |
|
124 | + throw new DecodeException( |
|
125 | + sprintf('Length %d overflows data, %d bytes left.', |
|
126 | + $length->intLength(), strlen($data) - $idx)); |
|
127 | + } |
|
128 | + $offset = $idx; |
|
129 | + return $length; |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * {@inheritdoc} |
|
134 | - * |
|
135 | - * @throws \DomainException If length is too large to encode |
|
136 | - */ |
|
137 | - public function toDER(): string |
|
138 | - { |
|
139 | - $bytes = []; |
|
140 | - if ($this->_indefinite) { |
|
141 | - $bytes[] = 0x80; |
|
142 | - } else { |
|
143 | - $num = $this->_length->gmpObj(); |
|
144 | - // long form |
|
145 | - if ($num > 127) { |
|
146 | - $octets = []; |
|
147 | - for (; $num > 0; $num >>= 8) { |
|
148 | - $octets[] = gmp_intval(0xff & $num); |
|
149 | - } |
|
150 | - $count = count($octets); |
|
151 | - // first octet must not be 0xff |
|
152 | - if ($count >= 127) { |
|
153 | - throw new \DomainException('Too many length octets.'); |
|
154 | - } |
|
155 | - $bytes[] = 0x80 | $count; |
|
156 | - foreach (array_reverse($octets) as $octet) { |
|
157 | - $bytes[] = $octet; |
|
158 | - } |
|
159 | - } |
|
160 | - // short form |
|
161 | - else { |
|
162 | - $bytes[] = gmp_intval($num); |
|
163 | - } |
|
164 | - } |
|
165 | - return pack('C*', ...$bytes); |
|
166 | - } |
|
132 | + /** |
|
133 | + * {@inheritdoc} |
|
134 | + * |
|
135 | + * @throws \DomainException If length is too large to encode |
|
136 | + */ |
|
137 | + public function toDER(): string |
|
138 | + { |
|
139 | + $bytes = []; |
|
140 | + if ($this->_indefinite) { |
|
141 | + $bytes[] = 0x80; |
|
142 | + } else { |
|
143 | + $num = $this->_length->gmpObj(); |
|
144 | + // long form |
|
145 | + if ($num > 127) { |
|
146 | + $octets = []; |
|
147 | + for (; $num > 0; $num >>= 8) { |
|
148 | + $octets[] = gmp_intval(0xff & $num); |
|
149 | + } |
|
150 | + $count = count($octets); |
|
151 | + // first octet must not be 0xff |
|
152 | + if ($count >= 127) { |
|
153 | + throw new \DomainException('Too many length octets.'); |
|
154 | + } |
|
155 | + $bytes[] = 0x80 | $count; |
|
156 | + foreach (array_reverse($octets) as $octet) { |
|
157 | + $bytes[] = $octet; |
|
158 | + } |
|
159 | + } |
|
160 | + // short form |
|
161 | + else { |
|
162 | + $bytes[] = gmp_intval($num); |
|
163 | + } |
|
164 | + } |
|
165 | + return pack('C*', ...$bytes); |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Get the length. |
|
170 | - * |
|
171 | - * @throws \LogicException If length is indefinite |
|
172 | - * |
|
173 | - * @return string Length as an integer string |
|
174 | - */ |
|
175 | - public function length(): string |
|
176 | - { |
|
177 | - if ($this->_indefinite) { |
|
178 | - throw new \LogicException('Length is indefinite.'); |
|
179 | - } |
|
180 | - return $this->_length->base10(); |
|
181 | - } |
|
168 | + /** |
|
169 | + * Get the length. |
|
170 | + * |
|
171 | + * @throws \LogicException If length is indefinite |
|
172 | + * |
|
173 | + * @return string Length as an integer string |
|
174 | + */ |
|
175 | + public function length(): string |
|
176 | + { |
|
177 | + if ($this->_indefinite) { |
|
178 | + throw new \LogicException('Length is indefinite.'); |
|
179 | + } |
|
180 | + return $this->_length->base10(); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Get the length as an integer. |
|
185 | - * |
|
186 | - * @throws \LogicException If length is indefinite |
|
187 | - * @throws \RuntimeException If length overflows integer size |
|
188 | - * |
|
189 | - * @return int |
|
190 | - */ |
|
191 | - public function intLength(): int |
|
192 | - { |
|
193 | - if ($this->_indefinite) { |
|
194 | - throw new \LogicException('Length is indefinite.'); |
|
195 | - } |
|
196 | - return $this->_length->intVal(); |
|
197 | - } |
|
183 | + /** |
|
184 | + * Get the length as an integer. |
|
185 | + * |
|
186 | + * @throws \LogicException If length is indefinite |
|
187 | + * @throws \RuntimeException If length overflows integer size |
|
188 | + * |
|
189 | + * @return int |
|
190 | + */ |
|
191 | + public function intLength(): int |
|
192 | + { |
|
193 | + if ($this->_indefinite) { |
|
194 | + throw new \LogicException('Length is indefinite.'); |
|
195 | + } |
|
196 | + return $this->_length->intVal(); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Whether length is indefinite. |
|
201 | - * |
|
202 | - * @return bool |
|
203 | - */ |
|
204 | - public function isIndefinite(): bool |
|
205 | - { |
|
206 | - return $this->_indefinite; |
|
207 | - } |
|
199 | + /** |
|
200 | + * Whether length is indefinite. |
|
201 | + * |
|
202 | + * @return bool |
|
203 | + */ |
|
204 | + public function isIndefinite(): bool |
|
205 | + { |
|
206 | + return $this->_indefinite; |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Decode long form length. |
|
211 | - * |
|
212 | - * @param int $length Number of octets |
|
213 | - * @param string $data Data |
|
214 | - * @param int $offset reference to the variable containing offset to the data |
|
215 | - * |
|
216 | - * @throws DecodeException If decoding fails |
|
217 | - * |
|
218 | - * @return string Integer as a string |
|
219 | - */ |
|
220 | - private static function _decodeLongFormLength(int $length, string $data, |
|
221 | - int &$offset): string |
|
222 | - { |
|
223 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
224 | - if (127 === $length) { |
|
225 | - throw new DecodeException('Invalid number of length octets.'); |
|
226 | - } |
|
227 | - $num = gmp_init(0, 10); |
|
228 | - while (--$length >= 0) { |
|
229 | - $byte = ord($data[$offset++]); |
|
230 | - $num <<= 8; |
|
231 | - $num |= $byte; |
|
232 | - } |
|
233 | - return gmp_strval($num); |
|
234 | - } |
|
209 | + /** |
|
210 | + * Decode long form length. |
|
211 | + * |
|
212 | + * @param int $length Number of octets |
|
213 | + * @param string $data Data |
|
214 | + * @param int $offset reference to the variable containing offset to the data |
|
215 | + * |
|
216 | + * @throws DecodeException If decoding fails |
|
217 | + * |
|
218 | + * @return string Integer as a string |
|
219 | + */ |
|
220 | + private static function _decodeLongFormLength(int $length, string $data, |
|
221 | + int &$offset): string |
|
222 | + { |
|
223 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
224 | + if (127 === $length) { |
|
225 | + throw new DecodeException('Invalid number of length octets.'); |
|
226 | + } |
|
227 | + $num = gmp_init(0, 10); |
|
228 | + while (--$length >= 0) { |
|
229 | + $byte = ord($data[$offset++]); |
|
230 | + $num <<= 8; |
|
231 | + $num |= $byte; |
|
232 | + } |
|
233 | + return gmp_strval($num); |
|
234 | + } |
|
235 | 235 | } |