@@ -11,13 +11,13 @@ |
||
11 | 11 | */ |
12 | 12 | trait UniversalClass |
13 | 13 | { |
14 | - /** |
|
15 | - * |
|
16 | - * @see \ASN1\Element::typeClass() |
|
17 | - * @return int |
|
18 | - */ |
|
19 | - public function typeClass(): int |
|
20 | - { |
|
21 | - return Identifier::CLASS_UNIVERSAL; |
|
22 | - } |
|
14 | + /** |
|
15 | + * |
|
16 | + * @see \ASN1\Element::typeClass() |
|
17 | + * @return int |
|
18 | + */ |
|
19 | + public function typeClass(): int |
|
20 | + { |
|
21 | + return Identifier::CLASS_UNIVERSAL; |
|
22 | + } |
|
23 | 23 | } |
@@ -12,54 +12,54 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor |
|
17 | - * |
|
18 | - * @param Element[] $elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor |
|
17 | + * |
|
18 | + * @param Element[] $elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * Used for DER encoding of SET type. |
|
29 | - * |
|
30 | - * @return self |
|
31 | - */ |
|
32 | - public function sortedSet() |
|
33 | - { |
|
34 | - $obj = clone $this; |
|
35 | - usort($obj->_elements, |
|
36 | - function (Element $a, Element $b) { |
|
37 | - if ($a->typeClass() != $b->typeClass()) { |
|
38 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
39 | - } |
|
40 | - if ($a->tag() == $b->tag()) { |
|
41 | - return 0; |
|
42 | - } |
|
43 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
44 | - }); |
|
45 | - return $obj; |
|
46 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * Used for DER encoding of SET type. |
|
29 | + * |
|
30 | + * @return self |
|
31 | + */ |
|
32 | + public function sortedSet() |
|
33 | + { |
|
34 | + $obj = clone $this; |
|
35 | + usort($obj->_elements, |
|
36 | + function (Element $a, Element $b) { |
|
37 | + if ($a->typeClass() != $b->typeClass()) { |
|
38 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
39 | + } |
|
40 | + if ($a->tag() == $b->tag()) { |
|
41 | + return 0; |
|
42 | + } |
|
43 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
44 | + }); |
|
45 | + return $obj; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Sort by encoding ascending order. |
|
50 | - * Used for DER encoding of SET OF type. |
|
51 | - * |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public function sortedSetOf() |
|
55 | - { |
|
56 | - $obj = clone $this; |
|
57 | - usort($obj->_elements, |
|
58 | - function (Element $a, Element $b) { |
|
59 | - $a_der = $a->toDER(); |
|
60 | - $b_der = $b->toDER(); |
|
61 | - return strcmp($a_der, $b_der); |
|
62 | - }); |
|
63 | - return $obj; |
|
64 | - } |
|
48 | + /** |
|
49 | + * Sort by encoding ascending order. |
|
50 | + * Used for DER encoding of SET OF type. |
|
51 | + * |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public function sortedSetOf() |
|
55 | + { |
|
56 | + $obj = clone $this; |
|
57 | + usort($obj->_elements, |
|
58 | + function (Element $a, Element $b) { |
|
59 | + $a_der = $a->toDER(); |
|
60 | + $b_der = $b->toDER(); |
|
61 | + return strcmp($a_der, $b_der); |
|
62 | + }); |
|
63 | + return $obj; |
|
64 | + } |
|
65 | 65 | } |
@@ -12,14 +12,14 @@ |
||
12 | 12 | */ |
13 | 13 | class Sequence extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor |
|
17 | - * |
|
18 | - * @param Element[] $elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SEQUENCE; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor |
|
17 | + * |
|
18 | + * @param Element[] $elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SEQUENCE; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | } |
@@ -16,160 +16,160 @@ |
||
16 | 16 | */ |
17 | 17 | class ObjectIdentifier extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Object identifier in dotted format. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $_oid; |
|
22 | + /** |
|
23 | + * Object identifier in dotted format. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $_oid; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param string $oid OID in dotted format |
|
33 | - */ |
|
34 | - public function __construct(string $oid) |
|
35 | - { |
|
36 | - $this->_oid = $oid; |
|
37 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
38 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param string $oid OID in dotted format |
|
33 | + */ |
|
34 | + public function __construct(string $oid) |
|
35 | + { |
|
36 | + $this->_oid = $oid; |
|
37 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get OID in dotted format. |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function oid(): string |
|
46 | - { |
|
47 | - return $this->_oid; |
|
48 | - } |
|
40 | + /** |
|
41 | + * Get OID in dotted format. |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function oid(): string |
|
46 | + { |
|
47 | + return $this->_oid; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - $subids = self::_explodeDottedOID($this->_oid); |
|
57 | - // encode first two subids to one according to spec section 8.19.4 |
|
58 | - if (count($subids) >= 2) { |
|
59 | - $num = ($subids[0] * 40) + $subids[1]; |
|
60 | - array_splice($subids, 0, 2, array($num)); |
|
61 | - } |
|
62 | - return self::_encodeSubIDs(...$subids); |
|
63 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + $subids = self::_explodeDottedOID($this->_oid); |
|
57 | + // encode first two subids to one according to spec section 8.19.4 |
|
58 | + if (count($subids) >= 2) { |
|
59 | + $num = ($subids[0] * 40) + $subids[1]; |
|
60 | + array_splice($subids, 0, 2, array($num)); |
|
61 | + } |
|
62 | + return self::_encodeSubIDs(...$subids); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - * @return self |
|
69 | - */ |
|
70 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
71 | - int &$offset) |
|
72 | - { |
|
73 | - $idx = $offset; |
|
74 | - $len = Length::expectFromDER($data, $idx)->length(); |
|
75 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
76 | - $idx += $len; |
|
77 | - // decode first subidentifier according to spec section 8.19.4 |
|
78 | - if (isset($subids[0])) { |
|
79 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
80 | - array_splice($subids, 0, 1, array($x, $y)); |
|
81 | - } |
|
82 | - $offset = $idx; |
|
83 | - return new self(self::_implodeSubIDs(...$subids)); |
|
84 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + * @return self |
|
69 | + */ |
|
70 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
71 | + int &$offset) |
|
72 | + { |
|
73 | + $idx = $offset; |
|
74 | + $len = Length::expectFromDER($data, $idx)->length(); |
|
75 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
76 | + $idx += $len; |
|
77 | + // decode first subidentifier according to spec section 8.19.4 |
|
78 | + if (isset($subids[0])) { |
|
79 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
80 | + array_splice($subids, 0, 1, array($x, $y)); |
|
81 | + } |
|
82 | + $offset = $idx; |
|
83 | + return new self(self::_implodeSubIDs(...$subids)); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Explode dotted OID to an array of sub ID's. |
|
88 | - * |
|
89 | - * @param string $oid OID in dotted format |
|
90 | - * @return \GMP[] Array of GMP numbers |
|
91 | - */ |
|
92 | - protected static function _explodeDottedOID($oid): array |
|
93 | - { |
|
94 | - $subids = []; |
|
95 | - foreach (explode(".", $oid) as $subid) { |
|
96 | - $subids[] = gmp_init($subid, 10); |
|
97 | - } |
|
98 | - return $subids; |
|
99 | - } |
|
86 | + /** |
|
87 | + * Explode dotted OID to an array of sub ID's. |
|
88 | + * |
|
89 | + * @param string $oid OID in dotted format |
|
90 | + * @return \GMP[] Array of GMP numbers |
|
91 | + */ |
|
92 | + protected static function _explodeDottedOID($oid): array |
|
93 | + { |
|
94 | + $subids = []; |
|
95 | + foreach (explode(".", $oid) as $subid) { |
|
96 | + $subids[] = gmp_init($subid, 10); |
|
97 | + } |
|
98 | + return $subids; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Implode an array of sub IDs to dotted OID format. |
|
103 | - * |
|
104 | - * @param \GMP[] $subids |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
108 | - { |
|
109 | - return implode(".", |
|
110 | - array_map( |
|
111 | - function ($num) { |
|
112 | - return gmp_strval($num, 10); |
|
113 | - }, $subids)); |
|
114 | - } |
|
101 | + /** |
|
102 | + * Implode an array of sub IDs to dotted OID format. |
|
103 | + * |
|
104 | + * @param \GMP[] $subids |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
108 | + { |
|
109 | + return implode(".", |
|
110 | + array_map( |
|
111 | + function ($num) { |
|
112 | + return gmp_strval($num, 10); |
|
113 | + }, $subids)); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Encode sub ID's to DER. |
|
118 | - * |
|
119 | - * @param \GMP[] $subids |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
123 | - { |
|
124 | - $data = ""; |
|
125 | - foreach ($subids as $subid) { |
|
126 | - // if number fits to one base 128 byte |
|
127 | - if ($subid < 128) { |
|
128 | - $data .= chr(intval($subid)); |
|
129 | - } else { // encode to multiple bytes |
|
130 | - $bytes = []; |
|
131 | - do { |
|
132 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
133 | - $subid >>= 7; |
|
134 | - } while ($subid > 0); |
|
135 | - // all bytes except last must have bit 8 set to one |
|
136 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
137 | - $data .= chr(0x80 | $byte); |
|
138 | - } |
|
139 | - $data .= chr(reset($bytes)); |
|
140 | - } |
|
141 | - } |
|
142 | - return $data; |
|
143 | - } |
|
116 | + /** |
|
117 | + * Encode sub ID's to DER. |
|
118 | + * |
|
119 | + * @param \GMP[] $subids |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
123 | + { |
|
124 | + $data = ""; |
|
125 | + foreach ($subids as $subid) { |
|
126 | + // if number fits to one base 128 byte |
|
127 | + if ($subid < 128) { |
|
128 | + $data .= chr(intval($subid)); |
|
129 | + } else { // encode to multiple bytes |
|
130 | + $bytes = []; |
|
131 | + do { |
|
132 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
133 | + $subid >>= 7; |
|
134 | + } while ($subid > 0); |
|
135 | + // all bytes except last must have bit 8 set to one |
|
136 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
137 | + $data .= chr(0x80 | $byte); |
|
138 | + } |
|
139 | + $data .= chr(reset($bytes)); |
|
140 | + } |
|
141 | + } |
|
142 | + return $data; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * Decode sub ID's from DER data. |
|
147 | - * |
|
148 | - * @param string $data |
|
149 | - * @throws DecodeException |
|
150 | - * @return \GMP[] Array of GMP numbers |
|
151 | - */ |
|
152 | - protected static function _decodeSubIDs($data): array |
|
153 | - { |
|
154 | - $subids = []; |
|
155 | - $idx = 0; |
|
156 | - $end = strlen($data); |
|
157 | - while ($idx < $end) { |
|
158 | - $num = gmp_init("0", 10); |
|
159 | - while (true) { |
|
160 | - if ($idx >= $end) { |
|
161 | - throw new DecodeException("Unexpected end of data."); |
|
162 | - } |
|
163 | - $byte = ord($data[$idx++]); |
|
164 | - $num |= $byte & 0x7f; |
|
165 | - // bit 8 of the last octet is zero |
|
166 | - if (!($byte & 0x80)) { |
|
167 | - break; |
|
168 | - } |
|
169 | - $num <<= 7; |
|
170 | - } |
|
171 | - $subids[] = $num; |
|
172 | - } |
|
173 | - return $subids; |
|
174 | - } |
|
145 | + /** |
|
146 | + * Decode sub ID's from DER data. |
|
147 | + * |
|
148 | + * @param string $data |
|
149 | + * @throws DecodeException |
|
150 | + * @return \GMP[] Array of GMP numbers |
|
151 | + */ |
|
152 | + protected static function _decodeSubIDs($data): array |
|
153 | + { |
|
154 | + $subids = []; |
|
155 | + $idx = 0; |
|
156 | + $end = strlen($data); |
|
157 | + while ($idx < $end) { |
|
158 | + $num = gmp_init("0", 10); |
|
159 | + while (true) { |
|
160 | + if ($idx >= $end) { |
|
161 | + throw new DecodeException("Unexpected end of data."); |
|
162 | + } |
|
163 | + $byte = ord($data[$idx++]); |
|
164 | + $num |= $byte & 0x7f; |
|
165 | + // bit 8 of the last octet is zero |
|
166 | + if (!($byte & 0x80)) { |
|
167 | + break; |
|
168 | + } |
|
169 | + $num <<= 7; |
|
170 | + } |
|
171 | + $subids[] = $num; |
|
172 | + } |
|
173 | + return $subids; |
|
174 | + } |
|
175 | 175 | } |
@@ -15,155 +15,155 @@ |
||
15 | 15 | */ |
16 | 16 | class Integer extends Element |
17 | 17 | { |
18 | - use UniversalClass; |
|
19 | - use PrimitiveType; |
|
18 | + use UniversalClass; |
|
19 | + use PrimitiveType; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Number as a base 10. |
|
23 | - * |
|
24 | - * @var int|string |
|
25 | - */ |
|
26 | - private $_number; |
|
21 | + /** |
|
22 | + * Number as a base 10. |
|
23 | + * |
|
24 | + * @var int|string |
|
25 | + */ |
|
26 | + private $_number; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param int|string $number Base 10 integer |
|
32 | - */ |
|
33 | - public function __construct($number) |
|
34 | - { |
|
35 | - $this->_typeTag = self::TYPE_INTEGER; |
|
36 | - if (!self::_validateNumber($number)) { |
|
37 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
38 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
39 | - } |
|
40 | - $this->_number = $number; |
|
41 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param int|string $number Base 10 integer |
|
32 | + */ |
|
33 | + public function __construct($number) |
|
34 | + { |
|
35 | + $this->_typeTag = self::TYPE_INTEGER; |
|
36 | + if (!self::_validateNumber($number)) { |
|
37 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
38 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
39 | + } |
|
40 | + $this->_number = $number; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the number as a base 10. |
|
45 | - * |
|
46 | - * @return int|string |
|
47 | - */ |
|
48 | - public function number() |
|
49 | - { |
|
50 | - return $this->_number; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Get the number as a base 10. |
|
45 | + * |
|
46 | + * @return int|string |
|
47 | + */ |
|
48 | + public function number() |
|
49 | + { |
|
50 | + return $this->_number; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * |
|
55 | - * {@inheritdoc} |
|
56 | - */ |
|
57 | - protected function _encodedContentDER(): string |
|
58 | - { |
|
59 | - $num = gmp_init($this->_number, 10); |
|
60 | - switch (gmp_sign($num)) { |
|
61 | - // positive |
|
62 | - case 1: |
|
63 | - return self::_encodePositiveInteger($num); |
|
64 | - // negative |
|
65 | - case -1: |
|
66 | - return self::_encodeNegativeInteger($num); |
|
67 | - } |
|
68 | - // zero |
|
69 | - return "\0"; |
|
70 | - } |
|
53 | + /** |
|
54 | + * |
|
55 | + * {@inheritdoc} |
|
56 | + */ |
|
57 | + protected function _encodedContentDER(): string |
|
58 | + { |
|
59 | + $num = gmp_init($this->_number, 10); |
|
60 | + switch (gmp_sign($num)) { |
|
61 | + // positive |
|
62 | + case 1: |
|
63 | + return self::_encodePositiveInteger($num); |
|
64 | + // negative |
|
65 | + case -1: |
|
66 | + return self::_encodeNegativeInteger($num); |
|
67 | + } |
|
68 | + // zero |
|
69 | + return "\0"; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Encode positive integer to DER content. |
|
74 | - * |
|
75 | - * @param \GMP|resource $num |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - private static function _encodePositiveInteger(\GMP $num): string |
|
79 | - { |
|
80 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
81 | - // if first bit is 1, prepend full zero byte |
|
82 | - // to represent positive two's complement |
|
83 | - if (ord($bin[0]) & 0x80) { |
|
84 | - $bin = chr(0x00) . $bin; |
|
85 | - } |
|
86 | - return $bin; |
|
87 | - } |
|
72 | + /** |
|
73 | + * Encode positive integer to DER content. |
|
74 | + * |
|
75 | + * @param \GMP|resource $num |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + private static function _encodePositiveInteger(\GMP $num): string |
|
79 | + { |
|
80 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
81 | + // if first bit is 1, prepend full zero byte |
|
82 | + // to represent positive two's complement |
|
83 | + if (ord($bin[0]) & 0x80) { |
|
84 | + $bin = chr(0x00) . $bin; |
|
85 | + } |
|
86 | + return $bin; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Encode negative integer to DER content. |
|
91 | - * |
|
92 | - * @param \GMP|resource $num |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
96 | - { |
|
97 | - $num = gmp_abs($num); |
|
98 | - // compute number of bytes required |
|
99 | - $width = 1; |
|
100 | - if ($num > 128) { |
|
101 | - $tmp = $num; |
|
102 | - do { |
|
103 | - $width++; |
|
104 | - $tmp >>= 8; |
|
105 | - } while ($tmp > 128); |
|
106 | - } |
|
107 | - // compute two's complement 2^n - x |
|
108 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
109 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
110 | - // if first bit is 0, prepend full inverted byte |
|
111 | - // to represent negative two's complement |
|
112 | - if (!(ord($bin[0]) & 0x80)) { |
|
113 | - $bin = chr(0xff) . $bin; |
|
114 | - } |
|
115 | - return $bin; |
|
116 | - } |
|
89 | + /** |
|
90 | + * Encode negative integer to DER content. |
|
91 | + * |
|
92 | + * @param \GMP|resource $num |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
96 | + { |
|
97 | + $num = gmp_abs($num); |
|
98 | + // compute number of bytes required |
|
99 | + $width = 1; |
|
100 | + if ($num > 128) { |
|
101 | + $tmp = $num; |
|
102 | + do { |
|
103 | + $width++; |
|
104 | + $tmp >>= 8; |
|
105 | + } while ($tmp > 128); |
|
106 | + } |
|
107 | + // compute two's complement 2^n - x |
|
108 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
109 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
110 | + // if first bit is 0, prepend full inverted byte |
|
111 | + // to represent negative two's complement |
|
112 | + if (!(ord($bin[0]) & 0x80)) { |
|
113 | + $bin = chr(0xff) . $bin; |
|
114 | + } |
|
115 | + return $bin; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * |
|
120 | - * {@inheritdoc} |
|
121 | - * @return self |
|
122 | - */ |
|
123 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
124 | - int &$offset) |
|
125 | - { |
|
126 | - $idx = $offset; |
|
127 | - $length = Length::expectFromDER($data, $idx); |
|
128 | - if (gmp_cmp(gmp_init($length->length(),10), gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
129 | - throw new \RuntimeException("Integer length too large"); |
|
130 | - } |
|
118 | + /** |
|
119 | + * |
|
120 | + * {@inheritdoc} |
|
121 | + * @return self |
|
122 | + */ |
|
123 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
124 | + int &$offset) |
|
125 | + { |
|
126 | + $idx = $offset; |
|
127 | + $length = Length::expectFromDER($data, $idx); |
|
128 | + if (gmp_cmp(gmp_init($length->length(),10), gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
129 | + throw new \RuntimeException("Integer length too large"); |
|
130 | + } |
|
131 | 131 | |
132 | - $bytes = substr($data, $idx, (int) $length->length()); |
|
133 | - $idx += $length->length(); |
|
134 | - $neg = ord($bytes[0]) & 0x80; |
|
135 | - // negative, apply inversion of two's complement |
|
136 | - if ($neg) { |
|
137 | - $len = strlen($bytes); |
|
138 | - for ($i = 0; $i < $len; $i++) { |
|
139 | - $bytes[$i] = ~$bytes[$i]; |
|
140 | - } |
|
141 | - } |
|
142 | - $num = gmp_init(bin2hex($bytes), 16); |
|
143 | - // negative, apply addition of two's complement |
|
144 | - // and produce negative result |
|
145 | - if ($neg) { |
|
146 | - $num = gmp_neg($num + 1); |
|
147 | - } |
|
148 | - $offset = $idx; |
|
149 | - // late static binding since enumerated extends integer type |
|
150 | - return new static(gmp_strval($num, 10)); |
|
151 | - } |
|
132 | + $bytes = substr($data, $idx, (int) $length->length()); |
|
133 | + $idx += $length->length(); |
|
134 | + $neg = ord($bytes[0]) & 0x80; |
|
135 | + // negative, apply inversion of two's complement |
|
136 | + if ($neg) { |
|
137 | + $len = strlen($bytes); |
|
138 | + for ($i = 0; $i < $len; $i++) { |
|
139 | + $bytes[$i] = ~$bytes[$i]; |
|
140 | + } |
|
141 | + } |
|
142 | + $num = gmp_init(bin2hex($bytes), 16); |
|
143 | + // negative, apply addition of two's complement |
|
144 | + // and produce negative result |
|
145 | + if ($neg) { |
|
146 | + $num = gmp_neg($num + 1); |
|
147 | + } |
|
148 | + $offset = $idx; |
|
149 | + // late static binding since enumerated extends integer type |
|
150 | + return new static(gmp_strval($num, 10)); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Test that number is valid for this context. |
|
155 | - * |
|
156 | - * @param mixed $num |
|
157 | - * @return boolean |
|
158 | - */ |
|
159 | - private static function _validateNumber($num): bool |
|
160 | - { |
|
161 | - if (is_int($num)) { |
|
162 | - return true; |
|
163 | - } |
|
164 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
165 | - return true; |
|
166 | - } |
|
167 | - return false; |
|
168 | - } |
|
153 | + /** |
|
154 | + * Test that number is valid for this context. |
|
155 | + * |
|
156 | + * @param mixed $num |
|
157 | + * @return boolean |
|
158 | + */ |
|
159 | + private static function _validateNumber($num): bool |
|
160 | + { |
|
161 | + if (is_int($num)) { |
|
162 | + return true; |
|
163 | + } |
|
164 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
165 | + return true; |
|
166 | + } |
|
167 | + return false; |
|
168 | + } |
|
169 | 169 | } |
@@ -16,194 +16,194 @@ |
||
16 | 16 | */ |
17 | 17 | class BitString extends StringType |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Number of unused bits in the last octet. |
|
24 | - * |
|
25 | - * @var int $_unusedBits |
|
26 | - */ |
|
27 | - protected $_unusedBits; |
|
22 | + /** |
|
23 | + * Number of unused bits in the last octet. |
|
24 | + * |
|
25 | + * @var int $_unusedBits |
|
26 | + */ |
|
27 | + protected $_unusedBits; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param string $string Content octets |
|
33 | - * @param int $unused_bits Number of unused bits in the last octet |
|
34 | - */ |
|
35 | - public function __construct(string $string, int $unused_bits = 0) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
38 | - parent::__construct($string); |
|
39 | - $this->_unusedBits = $unused_bits; |
|
40 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param string $string Content octets |
|
33 | + * @param int $unused_bits Number of unused bits in the last octet |
|
34 | + */ |
|
35 | + public function __construct(string $string, int $unused_bits = 0) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
38 | + parent::__construct($string); |
|
39 | + $this->_unusedBits = $unused_bits; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Get the number of bits in the string. |
|
44 | - * |
|
45 | - * @return int |
|
46 | - */ |
|
47 | - public function numBits(): int |
|
48 | - { |
|
49 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
50 | - } |
|
42 | + /** |
|
43 | + * Get the number of bits in the string. |
|
44 | + * |
|
45 | + * @return int |
|
46 | + */ |
|
47 | + public function numBits(): int |
|
48 | + { |
|
49 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Get the number of unused bits in the last octet of the string. |
|
54 | - * |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function unusedBits(): int |
|
58 | - { |
|
59 | - return $this->_unusedBits; |
|
60 | - } |
|
52 | + /** |
|
53 | + * Get the number of unused bits in the last octet of the string. |
|
54 | + * |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function unusedBits(): int |
|
58 | + { |
|
59 | + return $this->_unusedBits; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Test whether bit is set. |
|
64 | - * |
|
65 | - * @param int $idx Bit index. |
|
66 | - * Most significant bit of the first octet is index 0. |
|
67 | - * @return boolean |
|
68 | - */ |
|
69 | - public function testBit($idx): bool |
|
70 | - { |
|
71 | - // octet index |
|
72 | - $oi = (int) floor($idx / 8); |
|
73 | - // if octet is outside range |
|
74 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
75 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
76 | - } |
|
77 | - // bit index |
|
78 | - $bi = $idx % 8; |
|
79 | - // if tested bit is last octet's unused bit |
|
80 | - if ($oi == strlen($this->_string) - 1) { |
|
81 | - if ($bi >= 8 - $this->_unusedBits) { |
|
82 | - throw new \OutOfBoundsException("Index refers to an unused bit."); |
|
83 | - } |
|
84 | - } |
|
85 | - $byte = $this->_string[$oi]; |
|
86 | - // index 0 is the most significant bit in byte |
|
87 | - $mask = 0x01 << (7 - $bi); |
|
88 | - return (ord($byte) & $mask) > 0; |
|
89 | - } |
|
62 | + /** |
|
63 | + * Test whether bit is set. |
|
64 | + * |
|
65 | + * @param int $idx Bit index. |
|
66 | + * Most significant bit of the first octet is index 0. |
|
67 | + * @return boolean |
|
68 | + */ |
|
69 | + public function testBit($idx): bool |
|
70 | + { |
|
71 | + // octet index |
|
72 | + $oi = (int) floor($idx / 8); |
|
73 | + // if octet is outside range |
|
74 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
75 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
76 | + } |
|
77 | + // bit index |
|
78 | + $bi = $idx % 8; |
|
79 | + // if tested bit is last octet's unused bit |
|
80 | + if ($oi == strlen($this->_string) - 1) { |
|
81 | + if ($bi >= 8 - $this->_unusedBits) { |
|
82 | + throw new \OutOfBoundsException("Index refers to an unused bit."); |
|
83 | + } |
|
84 | + } |
|
85 | + $byte = $this->_string[$oi]; |
|
86 | + // index 0 is the most significant bit in byte |
|
87 | + $mask = 0x01 << (7 - $bi); |
|
88 | + return (ord($byte) & $mask) > 0; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Get range of bits. |
|
93 | - * |
|
94 | - * @param int $start Index of first bit |
|
95 | - * @param int $length Number of bits in range |
|
96 | - * @throws \OutOfBoundsException |
|
97 | - * @return number Integer of $length bits |
|
98 | - */ |
|
99 | - public function range(int $start, int $length) |
|
100 | - { |
|
101 | - if (!$length) { |
|
102 | - return 0; |
|
103 | - } |
|
104 | - if ($start + $length > $this->numBits()) { |
|
105 | - throw new \OutOfBoundsException("Not enough bits."); |
|
106 | - } |
|
107 | - $bits = gmp_init(0); |
|
108 | - $idx = $start; |
|
109 | - $end = $start + $length; |
|
110 | - while (true) { |
|
111 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
112 | - $bits |= $bit; |
|
113 | - if (++$idx >= $end) { |
|
114 | - break; |
|
115 | - } |
|
116 | - $bits <<= 1; |
|
117 | - } |
|
118 | - return gmp_strval($bits, 10); |
|
119 | - } |
|
91 | + /** |
|
92 | + * Get range of bits. |
|
93 | + * |
|
94 | + * @param int $start Index of first bit |
|
95 | + * @param int $length Number of bits in range |
|
96 | + * @throws \OutOfBoundsException |
|
97 | + * @return number Integer of $length bits |
|
98 | + */ |
|
99 | + public function range(int $start, int $length) |
|
100 | + { |
|
101 | + if (!$length) { |
|
102 | + return 0; |
|
103 | + } |
|
104 | + if ($start + $length > $this->numBits()) { |
|
105 | + throw new \OutOfBoundsException("Not enough bits."); |
|
106 | + } |
|
107 | + $bits = gmp_init(0); |
|
108 | + $idx = $start; |
|
109 | + $end = $start + $length; |
|
110 | + while (true) { |
|
111 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
112 | + $bits |= $bit; |
|
113 | + if (++$idx >= $end) { |
|
114 | + break; |
|
115 | + } |
|
116 | + $bits <<= 1; |
|
117 | + } |
|
118 | + return gmp_strval($bits, 10); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Get a copy of the bit string with trailing zeroes removed. |
|
123 | - * |
|
124 | - * @return self |
|
125 | - */ |
|
126 | - public function withoutTrailingZeroes() |
|
127 | - { |
|
128 | - // if bit string was empty |
|
129 | - if (!strlen($this->_string)) { |
|
130 | - return new self(""); |
|
131 | - } |
|
132 | - $bits = $this->_string; |
|
133 | - // count number of empty trailing octets |
|
134 | - $unused_octets = 0; |
|
135 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
136 | - if ($bits[$idx] != "\x0") { |
|
137 | - break; |
|
138 | - } |
|
139 | - } |
|
140 | - // strip trailing octets |
|
141 | - if ($unused_octets) { |
|
142 | - $bits = substr($bits, 0, -$unused_octets); |
|
143 | - } |
|
144 | - // if bit string was full of zeroes |
|
145 | - if (!strlen($bits)) { |
|
146 | - return new self(""); |
|
147 | - } |
|
148 | - // count number of trailing zeroes in the last octet |
|
149 | - $unused_bits = 0; |
|
150 | - $byte = ord($bits[strlen($bits) - 1]); |
|
151 | - while (!($byte & 0x01)) { |
|
152 | - $unused_bits++; |
|
153 | - $byte >>= 1; |
|
154 | - } |
|
155 | - return new self($bits, $unused_bits); |
|
156 | - } |
|
121 | + /** |
|
122 | + * Get a copy of the bit string with trailing zeroes removed. |
|
123 | + * |
|
124 | + * @return self |
|
125 | + */ |
|
126 | + public function withoutTrailingZeroes() |
|
127 | + { |
|
128 | + // if bit string was empty |
|
129 | + if (!strlen($this->_string)) { |
|
130 | + return new self(""); |
|
131 | + } |
|
132 | + $bits = $this->_string; |
|
133 | + // count number of empty trailing octets |
|
134 | + $unused_octets = 0; |
|
135 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
136 | + if ($bits[$idx] != "\x0") { |
|
137 | + break; |
|
138 | + } |
|
139 | + } |
|
140 | + // strip trailing octets |
|
141 | + if ($unused_octets) { |
|
142 | + $bits = substr($bits, 0, -$unused_octets); |
|
143 | + } |
|
144 | + // if bit string was full of zeroes |
|
145 | + if (!strlen($bits)) { |
|
146 | + return new self(""); |
|
147 | + } |
|
148 | + // count number of trailing zeroes in the last octet |
|
149 | + $unused_bits = 0; |
|
150 | + $byte = ord($bits[strlen($bits) - 1]); |
|
151 | + while (!($byte & 0x01)) { |
|
152 | + $unused_bits++; |
|
153 | + $byte >>= 1; |
|
154 | + } |
|
155 | + return new self($bits, $unused_bits); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * |
|
160 | - * {@inheritdoc} |
|
161 | - */ |
|
162 | - protected function _encodedContentDER(): string |
|
163 | - { |
|
164 | - $der = chr($this->_unusedBits); |
|
165 | - $der .= $this->_string; |
|
166 | - if ($this->_unusedBits) { |
|
167 | - $octet = $der[strlen($der) - 1]; |
|
168 | - // set unused bits to zero |
|
169 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
170 | - $der[strlen($der) - 1] = $octet; |
|
171 | - } |
|
172 | - return $der; |
|
173 | - } |
|
158 | + /** |
|
159 | + * |
|
160 | + * {@inheritdoc} |
|
161 | + */ |
|
162 | + protected function _encodedContentDER(): string |
|
163 | + { |
|
164 | + $der = chr($this->_unusedBits); |
|
165 | + $der .= $this->_string; |
|
166 | + if ($this->_unusedBits) { |
|
167 | + $octet = $der[strlen($der) - 1]; |
|
168 | + // set unused bits to zero |
|
169 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
170 | + $der[strlen($der) - 1] = $octet; |
|
171 | + } |
|
172 | + return $der; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * |
|
177 | - * {@inheritdoc} |
|
178 | - * @return self |
|
179 | - */ |
|
180 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
181 | - int &$offset) |
|
182 | - { |
|
183 | - $idx = $offset; |
|
184 | - $length = Length::expectFromDER($data, $idx); |
|
185 | - if ($length->length() < 1) { |
|
186 | - throw new DecodeException("Bit string length must be at least 1."); |
|
187 | - } |
|
188 | - $unused_bits = ord($data[$idx++]); |
|
189 | - if ($unused_bits > 7) { |
|
190 | - throw new DecodeException( |
|
191 | - "Unused bits in a bit string must be less than 8."); |
|
192 | - } |
|
193 | - $str_len = $length->length() - 1; |
|
194 | - if ($str_len) { |
|
195 | - $str = substr($data, $idx, $str_len); |
|
196 | - if ($unused_bits) { |
|
197 | - $mask = (1 << $unused_bits) - 1; |
|
198 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
199 | - throw new DecodeException( |
|
200 | - "DER encoded bit string must have zero padding."); |
|
201 | - } |
|
202 | - } |
|
203 | - } else { |
|
204 | - $str = ""; |
|
205 | - } |
|
206 | - $offset = $idx + $str_len; |
|
207 | - return new self($str, $unused_bits); |
|
208 | - } |
|
175 | + /** |
|
176 | + * |
|
177 | + * {@inheritdoc} |
|
178 | + * @return self |
|
179 | + */ |
|
180 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
181 | + int &$offset) |
|
182 | + { |
|
183 | + $idx = $offset; |
|
184 | + $length = Length::expectFromDER($data, $idx); |
|
185 | + if ($length->length() < 1) { |
|
186 | + throw new DecodeException("Bit string length must be at least 1."); |
|
187 | + } |
|
188 | + $unused_bits = ord($data[$idx++]); |
|
189 | + if ($unused_bits > 7) { |
|
190 | + throw new DecodeException( |
|
191 | + "Unused bits in a bit string must be less than 8."); |
|
192 | + } |
|
193 | + $str_len = $length->length() - 1; |
|
194 | + if ($str_len) { |
|
195 | + $str = substr($data, $idx, $str_len); |
|
196 | + if ($unused_bits) { |
|
197 | + $mask = (1 << $unused_bits) - 1; |
|
198 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
199 | + throw new DecodeException( |
|
200 | + "DER encoded bit string must have zero padding."); |
|
201 | + } |
|
202 | + } |
|
203 | + } else { |
|
204 | + $str = ""; |
|
205 | + } |
|
206 | + $offset = $idx + $str_len; |
|
207 | + return new self($str, $unused_bits); |
|
208 | + } |
|
209 | 209 | } |
@@ -12,26 +12,26 @@ |
||
12 | 12 | */ |
13 | 13 | class GeneralString 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_GENERAL_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_GENERAL_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - protected function _validateString(string $string): bool |
|
33 | - { |
|
34 | - // allow everything |
|
35 | - return true; |
|
36 | - } |
|
28 | + /** |
|
29 | + * |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + protected function _validateString(string $string): bool |
|
33 | + { |
|
34 | + // allow everything |
|
35 | + return true; |
|
36 | + } |
|
37 | 37 | } |
@@ -16,64 +16,64 @@ |
||
16 | 16 | */ |
17 | 17 | class Boolean extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Value. |
|
24 | - * |
|
25 | - * @var bool |
|
26 | - */ |
|
27 | - private $_bool; |
|
22 | + /** |
|
23 | + * Value. |
|
24 | + * |
|
25 | + * @var bool |
|
26 | + */ |
|
27 | + private $_bool; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param bool $bool |
|
33 | - */ |
|
34 | - public function __construct(bool $bool) |
|
35 | - { |
|
36 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
37 | - $this->_bool = $bool; |
|
38 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param bool $bool |
|
33 | + */ |
|
34 | + public function __construct(bool $bool) |
|
35 | + { |
|
36 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
37 | + $this->_bool = $bool; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get the value. |
|
42 | - * |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function value(): bool |
|
46 | - { |
|
47 | - return $this->_bool; |
|
48 | - } |
|
40 | + /** |
|
41 | + * Get the value. |
|
42 | + * |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function value(): bool |
|
46 | + { |
|
47 | + return $this->_bool; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - return $this->_bool ? chr(0xff) : chr(0); |
|
57 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + return $this->_bool ? chr(0xff) : chr(0); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * |
|
61 | - * {@inheritdoc} |
|
62 | - * @return self |
|
63 | - */ |
|
64 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
65 | - int &$offset) |
|
66 | - { |
|
67 | - $idx = $offset; |
|
68 | - Length::expectFromDER($data, $idx, 1); |
|
69 | - $byte = ord($data[$idx++]); |
|
70 | - if ($byte !== 0) { |
|
71 | - if ($byte != 0xff) { |
|
72 | - throw new DecodeException( |
|
73 | - "DER encoded boolean true must have all bits set to 1."); |
|
74 | - } |
|
75 | - } |
|
76 | - $offset = $idx; |
|
77 | - return new self($byte !== 0); |
|
78 | - } |
|
59 | + /** |
|
60 | + * |
|
61 | + * {@inheritdoc} |
|
62 | + * @return self |
|
63 | + */ |
|
64 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
65 | + int &$offset) |
|
66 | + { |
|
67 | + $idx = $offset; |
|
68 | + Length::expectFromDER($data, $idx, 1); |
|
69 | + $byte = ord($data[$idx++]); |
|
70 | + if ($byte !== 0) { |
|
71 | + if ($byte != 0xff) { |
|
72 | + throw new DecodeException( |
|
73 | + "DER encoded boolean true must have all bits set to 1."); |
|
74 | + } |
|
75 | + } |
|
76 | + $offset = $idx; |
|
77 | + return new self($byte !== 0); |
|
78 | + } |
|
79 | 79 | } |
@@ -12,16 +12,16 @@ |
||
12 | 12 | */ |
13 | 13 | class OctetString 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_OCTET_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_OCTET_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | } |