@@ -13,335 +13,335 @@ |
||
| 13 | 13 | * Base class for the constructed types. |
| 14 | 14 | */ |
| 15 | 15 | abstract class Structure extends Element implements |
| 16 | - \Countable, |
|
| 17 | - \IteratorAggregate |
|
| 16 | + \Countable, |
|
| 17 | + \IteratorAggregate |
|
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 19 | + use UniversalClass; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Array of elements in the structure. |
|
| 23 | - * |
|
| 24 | - * @var Element[] $_elements |
|
| 25 | - */ |
|
| 26 | - protected $_elements; |
|
| 21 | + /** |
|
| 22 | + * Array of elements in the structure. |
|
| 23 | + * |
|
| 24 | + * @var Element[] $_elements |
|
| 25 | + */ |
|
| 26 | + protected $_elements; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Lookup table for the tagged elements. |
|
| 30 | - * |
|
| 31 | - * @var TaggedType[]|null $_taggedMap |
|
| 32 | - */ |
|
| 33 | - private $_taggedMap; |
|
| 28 | + /** |
|
| 29 | + * Lookup table for the tagged elements. |
|
| 30 | + * |
|
| 31 | + * @var TaggedType[]|null $_taggedMap |
|
| 32 | + */ |
|
| 33 | + private $_taggedMap; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 37 | - * |
|
| 38 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 39 | - */ |
|
| 40 | - private $_unspecifiedTypes; |
|
| 35 | + /** |
|
| 36 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 37 | + * |
|
| 38 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 39 | + */ |
|
| 40 | + private $_unspecifiedTypes; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Constructor. |
|
| 44 | - * |
|
| 45 | - * @param Element[] $elements Any number of elements |
|
| 46 | - */ |
|
| 47 | - public function __construct(Element ...$elements) |
|
| 48 | - { |
|
| 49 | - $this->_elements = $elements; |
|
| 50 | - } |
|
| 42 | + /** |
|
| 43 | + * Constructor. |
|
| 44 | + * |
|
| 45 | + * @param Element[] $elements Any number of elements |
|
| 46 | + */ |
|
| 47 | + public function __construct(Element ...$elements) |
|
| 48 | + { |
|
| 49 | + $this->_elements = $elements; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Clone magic method. |
|
| 54 | - */ |
|
| 55 | - public function __clone() |
|
| 56 | - { |
|
| 57 | - // clear cache-variables |
|
| 58 | - $this->_taggedMap = null; |
|
| 59 | - $this->_unspecifiedTypes = null; |
|
| 60 | - } |
|
| 52 | + /** |
|
| 53 | + * Clone magic method. |
|
| 54 | + */ |
|
| 55 | + public function __clone() |
|
| 56 | + { |
|
| 57 | + // clear cache-variables |
|
| 58 | + $this->_taggedMap = null; |
|
| 59 | + $this->_unspecifiedTypes = null; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * |
|
| 64 | - * @see \ASN1\Element::isConstructed() |
|
| 65 | - * @return bool |
|
| 66 | - */ |
|
| 67 | - public function isConstructed(): bool |
|
| 68 | - { |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * |
|
| 64 | + * @see \ASN1\Element::isConstructed() |
|
| 65 | + * @return bool |
|
| 66 | + */ |
|
| 67 | + public function isConstructed(): bool |
|
| 68 | + { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * |
|
| 74 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - protected function _encodedContentDER(): string |
|
| 78 | - { |
|
| 79 | - $data = ""; |
|
| 80 | - foreach ($this->_elements as $element) { |
|
| 81 | - $data .= $element->toDER(); |
|
| 82 | - } |
|
| 83 | - return $data; |
|
| 84 | - } |
|
| 72 | + /** |
|
| 73 | + * |
|
| 74 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + protected function _encodedContentDER(): string |
|
| 78 | + { |
|
| 79 | + $data = ""; |
|
| 80 | + foreach ($this->_elements as $element) { |
|
| 81 | + $data .= $element->toDER(); |
|
| 82 | + } |
|
| 83 | + return $data; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * |
|
| 88 | - * @see \ASN1\Element::_decodeFromDER() |
|
| 89 | - * @return self |
|
| 90 | - */ |
|
| 91 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 92 | - int &$offset) |
|
| 93 | - { |
|
| 94 | - $idx = $offset; |
|
| 95 | - if (!$identifier->isConstructed()) { |
|
| 96 | - throw new DecodeException( |
|
| 97 | - "Structured element must have constructed bit set."); |
|
| 98 | - } |
|
| 99 | - $length = Length::expectFromDER($data, $idx); |
|
| 100 | - $end = $idx + $length->length(); |
|
| 101 | - $elements = []; |
|
| 102 | - while ($idx < $end) { |
|
| 103 | - $elements[] = Element::fromDER($data, $idx); |
|
| 104 | - // check that element didn't overflow length |
|
| 105 | - if ($idx > $end) { |
|
| 106 | - throw new DecodeException( |
|
| 107 | - "Structure's content overflows length."); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - $offset = $idx; |
|
| 111 | - // return instance by static late binding |
|
| 112 | - return new static(...$elements); |
|
| 113 | - } |
|
| 86 | + /** |
|
| 87 | + * |
|
| 88 | + * @see \ASN1\Element::_decodeFromDER() |
|
| 89 | + * @return self |
|
| 90 | + */ |
|
| 91 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 92 | + int &$offset) |
|
| 93 | + { |
|
| 94 | + $idx = $offset; |
|
| 95 | + if (!$identifier->isConstructed()) { |
|
| 96 | + throw new DecodeException( |
|
| 97 | + "Structured element must have constructed bit set."); |
|
| 98 | + } |
|
| 99 | + $length = Length::expectFromDER($data, $idx); |
|
| 100 | + $end = $idx + $length->length(); |
|
| 101 | + $elements = []; |
|
| 102 | + while ($idx < $end) { |
|
| 103 | + $elements[] = Element::fromDER($data, $idx); |
|
| 104 | + // check that element didn't overflow length |
|
| 105 | + if ($idx > $end) { |
|
| 106 | + throw new DecodeException( |
|
| 107 | + "Structure's content overflows length."); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + $offset = $idx; |
|
| 111 | + // return instance by static late binding |
|
| 112 | + return new static(...$elements); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Explode DER structure to DER encoded components that it contains. |
|
| 117 | - * |
|
| 118 | - * @param string $data |
|
| 119 | - * @throws DecodeException |
|
| 120 | - * @return string[] |
|
| 121 | - */ |
|
| 122 | - public static function explodeDER(string $data): array |
|
| 123 | - { |
|
| 124 | - $offset = 0; |
|
| 125 | - $identifier = Identifier::fromDER($data, $offset); |
|
| 126 | - if (!$identifier->isConstructed()) { |
|
| 127 | - throw new DecodeException("Element is not constructed."); |
|
| 128 | - } |
|
| 129 | - $length = Length::expectFromDER($data, $offset); |
|
| 130 | - $end = $offset + $length->length(); |
|
| 131 | - $parts = []; |
|
| 132 | - while ($offset < $end) { |
|
| 133 | - // start of the element |
|
| 134 | - $idx = $offset; |
|
| 135 | - // skip identifier |
|
| 136 | - Identifier::fromDER($data, $offset); |
|
| 137 | - // decode element length |
|
| 138 | - $length = Length::expectFromDER($data, $offset); |
|
| 139 | - // extract der encoding of the element |
|
| 140 | - $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 141 | - // update offset over content |
|
| 142 | - $offset += $length->length(); |
|
| 143 | - } |
|
| 144 | - return $parts; |
|
| 145 | - } |
|
| 115 | + /** |
|
| 116 | + * Explode DER structure to DER encoded components that it contains. |
|
| 117 | + * |
|
| 118 | + * @param string $data |
|
| 119 | + * @throws DecodeException |
|
| 120 | + * @return string[] |
|
| 121 | + */ |
|
| 122 | + public static function explodeDER(string $data): array |
|
| 123 | + { |
|
| 124 | + $offset = 0; |
|
| 125 | + $identifier = Identifier::fromDER($data, $offset); |
|
| 126 | + if (!$identifier->isConstructed()) { |
|
| 127 | + throw new DecodeException("Element is not constructed."); |
|
| 128 | + } |
|
| 129 | + $length = Length::expectFromDER($data, $offset); |
|
| 130 | + $end = $offset + $length->length(); |
|
| 131 | + $parts = []; |
|
| 132 | + while ($offset < $end) { |
|
| 133 | + // start of the element |
|
| 134 | + $idx = $offset; |
|
| 135 | + // skip identifier |
|
| 136 | + Identifier::fromDER($data, $offset); |
|
| 137 | + // decode element length |
|
| 138 | + $length = Length::expectFromDER($data, $offset); |
|
| 139 | + // extract der encoding of the element |
|
| 140 | + $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 141 | + // update offset over content |
|
| 142 | + $offset += $length->length(); |
|
| 143 | + } |
|
| 144 | + return $parts; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * Get self with an element at the given index replaced by another. |
|
| 149 | - * |
|
| 150 | - * @param int $idx Element index |
|
| 151 | - * @param Element $el New element to insert into the structure |
|
| 152 | - * @throws \OutOfBoundsException |
|
| 153 | - * @return self |
|
| 154 | - */ |
|
| 155 | - public function withReplaced(int $idx, Element $el) |
|
| 156 | - { |
|
| 157 | - if (!isset($this->_elements[$idx])) { |
|
| 158 | - throw new \OutOfBoundsException( |
|
| 159 | - "Structure doesn't have element at index $idx."); |
|
| 160 | - } |
|
| 161 | - $obj = clone $this; |
|
| 162 | - $obj->_elements[$idx] = $el; |
|
| 163 | - return $obj; |
|
| 164 | - } |
|
| 147 | + /** |
|
| 148 | + * Get self with an element at the given index replaced by another. |
|
| 149 | + * |
|
| 150 | + * @param int $idx Element index |
|
| 151 | + * @param Element $el New element to insert into the structure |
|
| 152 | + * @throws \OutOfBoundsException |
|
| 153 | + * @return self |
|
| 154 | + */ |
|
| 155 | + public function withReplaced(int $idx, Element $el) |
|
| 156 | + { |
|
| 157 | + if (!isset($this->_elements[$idx])) { |
|
| 158 | + throw new \OutOfBoundsException( |
|
| 159 | + "Structure doesn't have element at index $idx."); |
|
| 160 | + } |
|
| 161 | + $obj = clone $this; |
|
| 162 | + $obj->_elements[$idx] = $el; |
|
| 163 | + return $obj; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Get self with an element inserted before the given index. |
|
| 168 | - * |
|
| 169 | - * @param int $idx Element index |
|
| 170 | - * @param Element $el New element to insert into the structure |
|
| 171 | - * @throws \OutOfBoundsException |
|
| 172 | - * @return self |
|
| 173 | - */ |
|
| 174 | - public function withInserted(int $idx, Element $el) |
|
| 175 | - { |
|
| 176 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
| 177 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 178 | - } |
|
| 179 | - $obj = clone $this; |
|
| 180 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 181 | - return $obj; |
|
| 182 | - } |
|
| 166 | + /** |
|
| 167 | + * Get self with an element inserted before the given index. |
|
| 168 | + * |
|
| 169 | + * @param int $idx Element index |
|
| 170 | + * @param Element $el New element to insert into the structure |
|
| 171 | + * @throws \OutOfBoundsException |
|
| 172 | + * @return self |
|
| 173 | + */ |
|
| 174 | + public function withInserted(int $idx, Element $el) |
|
| 175 | + { |
|
| 176 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
| 177 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 178 | + } |
|
| 179 | + $obj = clone $this; |
|
| 180 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 181 | + return $obj; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Get self with an element appended to the end. |
|
| 186 | - * |
|
| 187 | - * @param Element $el Element to insert into the structure |
|
| 188 | - * @return self |
|
| 189 | - */ |
|
| 190 | - public function withAppended(Element $el) |
|
| 191 | - { |
|
| 192 | - $obj = clone $this; |
|
| 193 | - array_push($obj->_elements, $el); |
|
| 194 | - return $obj; |
|
| 195 | - } |
|
| 184 | + /** |
|
| 185 | + * Get self with an element appended to the end. |
|
| 186 | + * |
|
| 187 | + * @param Element $el Element to insert into the structure |
|
| 188 | + * @return self |
|
| 189 | + */ |
|
| 190 | + public function withAppended(Element $el) |
|
| 191 | + { |
|
| 192 | + $obj = clone $this; |
|
| 193 | + array_push($obj->_elements, $el); |
|
| 194 | + return $obj; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Get self with an element prepended in the beginning. |
|
| 199 | - * |
|
| 200 | - * @param Element $el Element to insert into the structure |
|
| 201 | - * @return self |
|
| 202 | - */ |
|
| 203 | - public function withPrepended(Element $el) |
|
| 204 | - { |
|
| 205 | - $obj = clone $this; |
|
| 206 | - array_unshift($obj->_elements, $el); |
|
| 207 | - return $obj; |
|
| 208 | - } |
|
| 197 | + /** |
|
| 198 | + * Get self with an element prepended in the beginning. |
|
| 199 | + * |
|
| 200 | + * @param Element $el Element to insert into the structure |
|
| 201 | + * @return self |
|
| 202 | + */ |
|
| 203 | + public function withPrepended(Element $el) |
|
| 204 | + { |
|
| 205 | + $obj = clone $this; |
|
| 206 | + array_unshift($obj->_elements, $el); |
|
| 207 | + return $obj; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Get self with an element at the given index removed. |
|
| 212 | - * |
|
| 213 | - * @param int $idx Element index |
|
| 214 | - * @throws \OutOfBoundsException |
|
| 215 | - * @return self |
|
| 216 | - */ |
|
| 217 | - public function withoutElement($idx) |
|
| 218 | - { |
|
| 219 | - if (!isset($this->_elements[$idx])) { |
|
| 220 | - throw new \OutOfBoundsException( |
|
| 221 | - "Structure doesn't have element at index $idx."); |
|
| 222 | - } |
|
| 223 | - $obj = clone $this; |
|
| 224 | - array_splice($obj->_elements, $idx, 1); |
|
| 225 | - return $obj; |
|
| 226 | - } |
|
| 210 | + /** |
|
| 211 | + * Get self with an element at the given index removed. |
|
| 212 | + * |
|
| 213 | + * @param int $idx Element index |
|
| 214 | + * @throws \OutOfBoundsException |
|
| 215 | + * @return self |
|
| 216 | + */ |
|
| 217 | + public function withoutElement($idx) |
|
| 218 | + { |
|
| 219 | + if (!isset($this->_elements[$idx])) { |
|
| 220 | + throw new \OutOfBoundsException( |
|
| 221 | + "Structure doesn't have element at index $idx."); |
|
| 222 | + } |
|
| 223 | + $obj = clone $this; |
|
| 224 | + array_splice($obj->_elements, $idx, 1); |
|
| 225 | + return $obj; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Get elements in the structure. |
|
| 230 | - * |
|
| 231 | - * @return UnspecifiedType[] |
|
| 232 | - */ |
|
| 233 | - public function elements(): array |
|
| 234 | - { |
|
| 235 | - if (!isset($this->_unspecifiedTypes)) { |
|
| 236 | - $this->_unspecifiedTypes = array_map( |
|
| 237 | - function (Element $el) { |
|
| 238 | - return new UnspecifiedType($el); |
|
| 239 | - }, $this->_elements); |
|
| 240 | - } |
|
| 241 | - return $this->_unspecifiedTypes; |
|
| 242 | - } |
|
| 228 | + /** |
|
| 229 | + * Get elements in the structure. |
|
| 230 | + * |
|
| 231 | + * @return UnspecifiedType[] |
|
| 232 | + */ |
|
| 233 | + public function elements(): array |
|
| 234 | + { |
|
| 235 | + if (!isset($this->_unspecifiedTypes)) { |
|
| 236 | + $this->_unspecifiedTypes = array_map( |
|
| 237 | + function (Element $el) { |
|
| 238 | + return new UnspecifiedType($el); |
|
| 239 | + }, $this->_elements); |
|
| 240 | + } |
|
| 241 | + return $this->_unspecifiedTypes; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * Check whether the structure has an element at the given index, optionally |
|
| 246 | - * satisfying given tag expectation. |
|
| 247 | - * |
|
| 248 | - * @param int $idx Index 0..n |
|
| 249 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 250 | - * @return bool |
|
| 251 | - */ |
|
| 252 | - public function has(int $idx, $expectedTag = null): bool |
|
| 253 | - { |
|
| 254 | - if (!isset($this->_elements[$idx])) { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - if (isset($expectedTag)) { |
|
| 258 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 259 | - return false; |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - return true; |
|
| 263 | - } |
|
| 244 | + /** |
|
| 245 | + * Check whether the structure has an element at the given index, optionally |
|
| 246 | + * satisfying given tag expectation. |
|
| 247 | + * |
|
| 248 | + * @param int $idx Index 0..n |
|
| 249 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 250 | + * @return bool |
|
| 251 | + */ |
|
| 252 | + public function has(int $idx, $expectedTag = null): bool |
|
| 253 | + { |
|
| 254 | + if (!isset($this->_elements[$idx])) { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + if (isset($expectedTag)) { |
|
| 258 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 259 | + return false; |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + return true; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Get the element at the given index, optionally checking that the element |
|
| 267 | - * has a given tag. |
|
| 268 | - * |
|
| 269 | - * NOTE! Expectation checking is deprecated and should be done |
|
| 270 | - * with UnspecifiedType. |
|
| 271 | - * |
|
| 272 | - * @param int $idx Index 0..n |
|
| 273 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 274 | - * @throws \OutOfBoundsException If element doesn't exists |
|
| 275 | - * @throws \UnexpectedValueException If expectation fails |
|
| 276 | - * @return UnspecifiedType |
|
| 277 | - */ |
|
| 278 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
| 279 | - { |
|
| 280 | - if (!isset($this->_elements[$idx])) { |
|
| 281 | - throw new \OutOfBoundsException( |
|
| 282 | - "Structure doesn't have an element at index $idx."); |
|
| 283 | - } |
|
| 284 | - $element = $this->_elements[$idx]; |
|
| 285 | - if (isset($expectedTag)) { |
|
| 286 | - $element->expectType($expectedTag); |
|
| 287 | - } |
|
| 288 | - return new UnspecifiedType($element); |
|
| 289 | - } |
|
| 265 | + /** |
|
| 266 | + * Get the element at the given index, optionally checking that the element |
|
| 267 | + * has a given tag. |
|
| 268 | + * |
|
| 269 | + * NOTE! Expectation checking is deprecated and should be done |
|
| 270 | + * with UnspecifiedType. |
|
| 271 | + * |
|
| 272 | + * @param int $idx Index 0..n |
|
| 273 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 274 | + * @throws \OutOfBoundsException If element doesn't exists |
|
| 275 | + * @throws \UnexpectedValueException If expectation fails |
|
| 276 | + * @return UnspecifiedType |
|
| 277 | + */ |
|
| 278 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
| 279 | + { |
|
| 280 | + if (!isset($this->_elements[$idx])) { |
|
| 281 | + throw new \OutOfBoundsException( |
|
| 282 | + "Structure doesn't have an element at index $idx."); |
|
| 283 | + } |
|
| 284 | + $element = $this->_elements[$idx]; |
|
| 285 | + if (isset($expectedTag)) { |
|
| 286 | + $element->expectType($expectedTag); |
|
| 287 | + } |
|
| 288 | + return new UnspecifiedType($element); |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * Check whether the structure contains a context specific element with a |
|
| 293 | - * given tag. |
|
| 294 | - * |
|
| 295 | - * @param int $tag Tag number |
|
| 296 | - * @return boolean |
|
| 297 | - */ |
|
| 298 | - public function hasTagged($tag): bool |
|
| 299 | - { |
|
| 300 | - // lazily build lookup map |
|
| 301 | - if (!isset($this->_taggedMap)) { |
|
| 302 | - $this->_taggedMap = []; |
|
| 303 | - foreach ($this->_elements as $element) { |
|
| 304 | - if ($element->isTagged()) { |
|
| 305 | - $this->_taggedMap[$element->tag()] = $element; |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - return isset($this->_taggedMap[$tag]); |
|
| 310 | - } |
|
| 291 | + /** |
|
| 292 | + * Check whether the structure contains a context specific element with a |
|
| 293 | + * given tag. |
|
| 294 | + * |
|
| 295 | + * @param int $tag Tag number |
|
| 296 | + * @return boolean |
|
| 297 | + */ |
|
| 298 | + public function hasTagged($tag): bool |
|
| 299 | + { |
|
| 300 | + // lazily build lookup map |
|
| 301 | + if (!isset($this->_taggedMap)) { |
|
| 302 | + $this->_taggedMap = []; |
|
| 303 | + foreach ($this->_elements as $element) { |
|
| 304 | + if ($element->isTagged()) { |
|
| 305 | + $this->_taggedMap[$element->tag()] = $element; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + return isset($this->_taggedMap[$tag]); |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | - /** |
|
| 313 | - * Get a context specific element tagged with a given tag. |
|
| 314 | - * |
|
| 315 | - * @param int $tag |
|
| 316 | - * @throws \LogicException If tag doesn't exists |
|
| 317 | - * @return TaggedType |
|
| 318 | - */ |
|
| 319 | - public function getTagged($tag): TaggedType |
|
| 320 | - { |
|
| 321 | - if (!$this->hasTagged($tag)) { |
|
| 322 | - throw new \LogicException("No tagged element for tag $tag."); |
|
| 323 | - } |
|
| 324 | - return $this->_taggedMap[$tag]; |
|
| 325 | - } |
|
| 312 | + /** |
|
| 313 | + * Get a context specific element tagged with a given tag. |
|
| 314 | + * |
|
| 315 | + * @param int $tag |
|
| 316 | + * @throws \LogicException If tag doesn't exists |
|
| 317 | + * @return TaggedType |
|
| 318 | + */ |
|
| 319 | + public function getTagged($tag): TaggedType |
|
| 320 | + { |
|
| 321 | + if (!$this->hasTagged($tag)) { |
|
| 322 | + throw new \LogicException("No tagged element for tag $tag."); |
|
| 323 | + } |
|
| 324 | + return $this->_taggedMap[$tag]; |
|
| 325 | + } |
|
| 326 | 326 | |
| 327 | - /** |
|
| 328 | - * |
|
| 329 | - * @see \Countable::count() |
|
| 330 | - * @return int |
|
| 331 | - */ |
|
| 332 | - public function count(): int |
|
| 333 | - { |
|
| 334 | - return count($this->_elements); |
|
| 335 | - } |
|
| 327 | + /** |
|
| 328 | + * |
|
| 329 | + * @see \Countable::count() |
|
| 330 | + * @return int |
|
| 331 | + */ |
|
| 332 | + public function count(): int |
|
| 333 | + { |
|
| 334 | + return count($this->_elements); |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * Get an iterator for the UnspecifiedElement objects. |
|
| 339 | - * |
|
| 340 | - * @see \IteratorAggregate::getIterator() |
|
| 341 | - * @return \ArrayIterator |
|
| 342 | - */ |
|
| 343 | - public function getIterator(): \ArrayIterator |
|
| 344 | - { |
|
| 345 | - return new \ArrayIterator($this->elements()); |
|
| 346 | - } |
|
| 337 | + /** |
|
| 338 | + * Get an iterator for the UnspecifiedElement objects. |
|
| 339 | + * |
|
| 340 | + * @see \IteratorAggregate::getIterator() |
|
| 341 | + * @return \ArrayIterator |
|
| 342 | + */ |
|
| 343 | + public function getIterator(): \ArrayIterator |
|
| 344 | + { |
|
| 345 | + return new \ArrayIterator($this->elements()); |
|
| 346 | + } |
|
| 347 | 347 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type; |
| 6 | 6 | |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * @return self |
| 90 | 90 | */ |
| 91 | 91 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 92 | - int &$offset) |
|
| 92 | + int & $offset) |
|
| 93 | 93 | { |
| 94 | 94 | $idx = $offset; |
| 95 | 95 | if (!$identifier->isConstructed()) { |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | { |
| 235 | 235 | if (!isset($this->_unspecifiedTypes)) { |
| 236 | 236 | $this->_unspecifiedTypes = array_map( |
| 237 | - function (Element $el) { |
|
| 237 | + function(Element $el) { |
|
| 238 | 238 | return new UnspecifiedType($el); |
| 239 | 239 | }, $this->_elements); |
| 240 | 240 | } |
@@ -11,87 +11,87 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class TimeType extends Element |
| 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 $_dateTime |
|
| 25 | - */ |
|
| 26 | - protected $_dateTime; |
|
| 21 | + /** |
|
| 22 | + * Date and time. |
|
| 23 | + * |
|
| 24 | + * @var \DateTimeImmutable $_dateTime |
|
| 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 | - * Initialize from datetime string. |
|
| 40 | - * |
|
| 41 | - * @param string $time Time string |
|
| 42 | - * @param string|null $tz Timezone, if null use default. |
|
| 43 | - * @throws \RuntimeException |
|
| 44 | - * @return self |
|
| 45 | - */ |
|
| 46 | - public static function fromString(string $time, string $tz = null) |
|
| 47 | - { |
|
| 48 | - try { |
|
| 49 | - if (!isset($tz)) { |
|
| 50 | - $tz = date_default_timezone_get(); |
|
| 51 | - } |
|
| 52 | - return new static( |
|
| 53 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
| 54 | - } catch (\Exception $e) { |
|
| 55 | - throw new \RuntimeException( |
|
| 56 | - "Failed to create DateTime: " . |
|
| 57 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 58 | - } |
|
| 59 | - } |
|
| 38 | + /** |
|
| 39 | + * Initialize from datetime string. |
|
| 40 | + * |
|
| 41 | + * @param string $time Time string |
|
| 42 | + * @param string|null $tz Timezone, if null use default. |
|
| 43 | + * @throws \RuntimeException |
|
| 44 | + * @return self |
|
| 45 | + */ |
|
| 46 | + public static function fromString(string $time, string $tz = null) |
|
| 47 | + { |
|
| 48 | + try { |
|
| 49 | + if (!isset($tz)) { |
|
| 50 | + $tz = date_default_timezone_get(); |
|
| 51 | + } |
|
| 52 | + return new static( |
|
| 53 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
| 54 | + } catch (\Exception $e) { |
|
| 55 | + throw new \RuntimeException( |
|
| 56 | + "Failed to create DateTime: " . |
|
| 57 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get the date and time. |
|
| 63 | - * |
|
| 64 | - * @return \DateTimeImmutable |
|
| 65 | - */ |
|
| 66 | - public function dateTime(): \DateTimeImmutable |
|
| 67 | - { |
|
| 68 | - return $this->_dateTime; |
|
| 69 | - } |
|
| 61 | + /** |
|
| 62 | + * Get the date and time. |
|
| 63 | + * |
|
| 64 | + * @return \DateTimeImmutable |
|
| 65 | + */ |
|
| 66 | + public function dateTime(): \DateTimeImmutable |
|
| 67 | + { |
|
| 68 | + return $this->_dateTime; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Create DateTimeZone object from string. |
|
| 73 | - * |
|
| 74 | - * @param string $tz |
|
| 75 | - * @throws \UnexpectedValueException If timezone is invalid |
|
| 76 | - * @return \DateTimeZone |
|
| 77 | - */ |
|
| 78 | - protected static function _createTimeZone($tz): \DateTimeZone |
|
| 79 | - { |
|
| 80 | - try { |
|
| 81 | - return new \DateTimeZone($tz); |
|
| 82 | - } catch (\Exception $e) { |
|
| 83 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 84 | - } |
|
| 85 | - } |
|
| 71 | + /** |
|
| 72 | + * Create DateTimeZone object from string. |
|
| 73 | + * |
|
| 74 | + * @param string $tz |
|
| 75 | + * @throws \UnexpectedValueException If timezone is invalid |
|
| 76 | + * @return \DateTimeZone |
|
| 77 | + */ |
|
| 78 | + protected static function _createTimeZone($tz): \DateTimeZone |
|
| 79 | + { |
|
| 80 | + try { |
|
| 81 | + return new \DateTimeZone($tz); |
|
| 82 | + } catch (\Exception $e) { |
|
| 83 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Get last error caused by DateTimeImmutable. |
|
| 89 | - * |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 93 | - { |
|
| 94 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 95 | - return implode(", ", $errors); |
|
| 96 | - } |
|
| 87 | + /** |
|
| 88 | + * Get last error caused by DateTimeImmutable. |
|
| 89 | + * |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 93 | + { |
|
| 94 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 95 | + return implode(", ", $errors); |
|
| 96 | + } |
|
| 97 | 97 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type; |
| 6 | 6 | |
@@ -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 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type; |
| 6 | 6 | |
@@ -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 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Constructed; |
| 6 | 6 | |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | { |
| 34 | 34 | $obj = clone $this; |
| 35 | 35 | usort($obj->_elements, |
| 36 | - function (Element $a, Element $b) { |
|
| 36 | + function(Element $a, Element $b) { |
|
| 37 | 37 | if ($a->typeClass() != $b->typeClass()) { |
| 38 | 38 | return $a->typeClass() < $b->typeClass() ? -1 : 1; |
| 39 | 39 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | { |
| 56 | 56 | $obj = clone $this; |
| 57 | 57 | usort($obj->_elements, |
| 58 | - function (Element $a, Element $b) { |
|
| 58 | + function(Element $a, Element $b) { |
|
| 59 | 59 | $a_der = $a->toDER(); |
| 60 | 60 | $b_der = $b->toDER(); |
| 61 | 61 | return strcmp($a_der, $b_der); |
@@ -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 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Constructed; |
| 6 | 6 | |
@@ -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 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @return self |
| 69 | 69 | */ |
| 70 | 70 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 71 | - int &$offset) |
|
| 71 | + int & $offset) |
|
| 72 | 72 | { |
| 73 | 73 | $idx = $offset; |
| 74 | 74 | $len = Length::expectFromDER($data, $idx)->length(); |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | { |
| 109 | 109 | return implode(".", |
| 110 | 110 | array_map( |
| 111 | - function ($num) { |
|
| 111 | + function($num) { |
|
| 112 | 112 | return gmp_strval($num, 10); |
| 113 | 113 | }, $subids)); |
| 114 | 114 | } |
@@ -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 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -121,11 +121,11 @@ discard block |
||
| 121 | 121 | * @return self |
| 122 | 122 | */ |
| 123 | 123 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 124 | - int &$offset) |
|
| 124 | + int & $offset) |
|
| 125 | 125 | { |
| 126 | 126 | $idx = $offset; |
| 127 | 127 | $length = Length::expectFromDER($data, $idx); |
| 128 | - if (gmp_cmp(gmp_init($length->length(),10), gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
| 128 | + if (gmp_cmp(gmp_init($length->length(), 10), gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
| 129 | 129 | throw new \RuntimeException("Integer length too large"); |
| 130 | 130 | } |
| 131 | 131 | |
@@ -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 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | * @return self |
| 179 | 179 | */ |
| 180 | 180 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 181 | - int &$offset) |
|
| 181 | + int & $offset) |
|
| 182 | 182 | { |
| 183 | 183 | $idx = $offset; |
| 184 | 184 | $length = Length::expectFromDER($data, $idx); |
@@ -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 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |