@@ -10,13 +10,13 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | interface ImplicitTagging extends ElementBase |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Get implicitly tagged wrapped element. |
|
| 15 | - * |
|
| 16 | - * @param int $tag Tag of the element |
|
| 17 | - * @param int $class Expected type class of the element |
|
| 18 | - * @throws \UnexpectedValueException If expectation fails |
|
| 19 | - * @return \ASN1\Type\UnspecifiedType |
|
| 20 | - */ |
|
| 21 | - public function implicit($tag, $class = Identifier::CLASS_UNIVERSAL); |
|
| 13 | + /** |
|
| 14 | + * Get implicitly tagged wrapped element. |
|
| 15 | + * |
|
| 16 | + * @param int $tag Tag of the element |
|
| 17 | + * @param int $class Expected type class of the element |
|
| 18 | + * @throws \UnexpectedValueException If expectation fails |
|
| 19 | + * @return \ASN1\Type\UnspecifiedType |
|
| 20 | + */ |
|
| 21 | + public function implicit($tag, $class = Identifier::CLASS_UNIVERSAL); |
|
| 22 | 22 | } |
@@ -15,612 +15,612 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class UnspecifiedType implements ElementBase |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * The wrapped element. |
|
| 20 | - * |
|
| 21 | - * @var Element |
|
| 22 | - */ |
|
| 23 | - private $_element; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Constructor. |
|
| 27 | - * |
|
| 28 | - * @param Element $el |
|
| 29 | - */ |
|
| 30 | - public function __construct(Element $el) |
|
| 31 | - { |
|
| 32 | - $this->_element = $el; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Initialize from ElementBase interface. |
|
| 37 | - * |
|
| 38 | - * @param ElementBase $el |
|
| 39 | - * @return self |
|
| 40 | - */ |
|
| 41 | - public static function fromElementBase(ElementBase $el) |
|
| 42 | - { |
|
| 43 | - // if element is already wrapped |
|
| 44 | - if ($el instanceof self) { |
|
| 45 | - return $el; |
|
| 46 | - } |
|
| 47 | - return new self($el->asElement()); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Compatibility method to dispatch calls to the wrapped element. |
|
| 52 | - * |
|
| 53 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 54 | - * @param string $mtd Method name |
|
| 55 | - * @param array $args Arguments |
|
| 56 | - * @return mixed |
|
| 57 | - */ |
|
| 58 | - public function __call($mtd, array $args) |
|
| 59 | - { |
|
| 60 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get the wrapped element as a context specific tagged type. |
|
| 65 | - * |
|
| 66 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 67 | - * @return TaggedType |
|
| 68 | - */ |
|
| 69 | - public function asTagged() |
|
| 70 | - { |
|
| 71 | - if (!$this->_element instanceof TaggedType) { |
|
| 72 | - throw new \UnexpectedValueException( |
|
| 73 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 74 | - } |
|
| 75 | - return $this->_element; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get the wrapped element as a boolean type. |
|
| 80 | - * |
|
| 81 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 82 | - * @return Primitive\Boolean |
|
| 83 | - */ |
|
| 84 | - public function asBoolean() |
|
| 85 | - { |
|
| 86 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 87 | - throw new \UnexpectedValueException( |
|
| 88 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 89 | - } |
|
| 90 | - return $this->_element; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the wrapped element as an integer type. |
|
| 95 | - * |
|
| 96 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 97 | - * @return Primitive\Integer |
|
| 98 | - */ |
|
| 99 | - public function asInteger() |
|
| 100 | - { |
|
| 101 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 102 | - throw new \UnexpectedValueException( |
|
| 103 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 104 | - } |
|
| 105 | - return $this->_element; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the wrapped element as a bit string type. |
|
| 110 | - * |
|
| 111 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 112 | - * @return Primitive\BitString |
|
| 113 | - */ |
|
| 114 | - public function asBitString() |
|
| 115 | - { |
|
| 116 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 117 | - throw new \UnexpectedValueException( |
|
| 118 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 119 | - } |
|
| 120 | - return $this->_element; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get the wrapped element as an octet string type. |
|
| 125 | - * |
|
| 126 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 127 | - * @return Primitive\OctetString |
|
| 128 | - */ |
|
| 129 | - public function asOctetString() |
|
| 130 | - { |
|
| 131 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 132 | - throw new \UnexpectedValueException( |
|
| 133 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 134 | - } |
|
| 135 | - return $this->_element; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get the wrapped element as a null type. |
|
| 140 | - * |
|
| 141 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 142 | - * @return Primitive\NullType |
|
| 143 | - */ |
|
| 144 | - public function asNull() |
|
| 145 | - { |
|
| 146 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 147 | - throw new \UnexpectedValueException( |
|
| 148 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 149 | - } |
|
| 150 | - return $this->_element; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the wrapped element as an object identifier type. |
|
| 155 | - * |
|
| 156 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 157 | - * identifier |
|
| 158 | - * @return Primitive\ObjectIdentifier |
|
| 159 | - */ |
|
| 160 | - public function asObjectIdentifier() |
|
| 161 | - { |
|
| 162 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 163 | - throw new \UnexpectedValueException( |
|
| 164 | - $this->_generateExceptionMessage( |
|
| 165 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 166 | - } |
|
| 167 | - return $this->_element; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Get the wrapped element as an object descriptor type. |
|
| 172 | - * |
|
| 173 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 174 | - * descriptor |
|
| 175 | - * @return Primitive\ObjectDescriptor |
|
| 176 | - */ |
|
| 177 | - public function asObjectDescriptor() |
|
| 178 | - { |
|
| 179 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 180 | - throw new \UnexpectedValueException( |
|
| 181 | - $this->_generateExceptionMessage( |
|
| 182 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 183 | - } |
|
| 184 | - return $this->_element; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Get the wrapped element as a real type. |
|
| 189 | - * |
|
| 190 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 191 | - * @return Primitive\Real |
|
| 192 | - */ |
|
| 193 | - public function asReal() |
|
| 194 | - { |
|
| 195 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 196 | - throw new \UnexpectedValueException( |
|
| 197 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 198 | - } |
|
| 199 | - return $this->_element; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Get the wrapped element as an enumerated type. |
|
| 204 | - * |
|
| 205 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 206 | - * @return Primitive\Enumerated |
|
| 207 | - */ |
|
| 208 | - public function asEnumerated() |
|
| 209 | - { |
|
| 210 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 211 | - throw new \UnexpectedValueException( |
|
| 212 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 213 | - } |
|
| 214 | - return $this->_element; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Get the wrapped element as a UTF8 string type. |
|
| 219 | - * |
|
| 220 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 221 | - * @return Primitive\UTF8String |
|
| 222 | - */ |
|
| 223 | - public function asUTF8String() |
|
| 224 | - { |
|
| 225 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 226 | - throw new \UnexpectedValueException( |
|
| 227 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 228 | - } |
|
| 229 | - return $this->_element; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Get the wrapped element as a relative OID type. |
|
| 234 | - * |
|
| 235 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 236 | - * @return Primitive\RelativeOID |
|
| 237 | - */ |
|
| 238 | - public function asRelativeOID() |
|
| 239 | - { |
|
| 240 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 241 | - throw new \UnexpectedValueException( |
|
| 242 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 243 | - } |
|
| 244 | - return $this->_element; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Get the wrapped element as a sequence type. |
|
| 249 | - * |
|
| 250 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 251 | - * @return Constructed\Sequence |
|
| 252 | - */ |
|
| 253 | - public function asSequence() |
|
| 254 | - { |
|
| 255 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 256 | - throw new \UnexpectedValueException( |
|
| 257 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 258 | - } |
|
| 259 | - return $this->_element; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Get the wrapped element as a set type. |
|
| 264 | - * |
|
| 265 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 266 | - * @return Constructed\Set |
|
| 267 | - */ |
|
| 268 | - public function asSet() |
|
| 269 | - { |
|
| 270 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 271 | - throw new \UnexpectedValueException( |
|
| 272 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 273 | - } |
|
| 274 | - return $this->_element; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Get the wrapped element as a numeric string type. |
|
| 279 | - * |
|
| 280 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 281 | - * @return Primitive\NumericString |
|
| 282 | - */ |
|
| 283 | - public function asNumericString() |
|
| 284 | - { |
|
| 285 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 286 | - throw new \UnexpectedValueException( |
|
| 287 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 288 | - } |
|
| 289 | - return $this->_element; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Get the wrapped element as a printable string type. |
|
| 294 | - * |
|
| 295 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 296 | - * string |
|
| 297 | - * @return Primitive\PrintableString |
|
| 298 | - */ |
|
| 299 | - public function asPrintableString() |
|
| 300 | - { |
|
| 301 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 302 | - throw new \UnexpectedValueException( |
|
| 303 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 304 | - } |
|
| 305 | - return $this->_element; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Get the wrapped element as a T61 string type. |
|
| 310 | - * |
|
| 311 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 312 | - * @return Primitive\T61String |
|
| 313 | - */ |
|
| 314 | - public function asT61String() |
|
| 315 | - { |
|
| 316 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 317 | - throw new \UnexpectedValueException( |
|
| 318 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 319 | - } |
|
| 320 | - return $this->_element; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Get the wrapped element as a videotex string type. |
|
| 325 | - * |
|
| 326 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 327 | - * @return Primitive\VideotexString |
|
| 328 | - */ |
|
| 329 | - public function asVideotexString() |
|
| 330 | - { |
|
| 331 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 332 | - throw new \UnexpectedValueException( |
|
| 333 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 334 | - } |
|
| 335 | - return $this->_element; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Get the wrapped element as a IA5 string type. |
|
| 340 | - * |
|
| 341 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 342 | - * @return Primitive\IA5String |
|
| 343 | - */ |
|
| 344 | - public function asIA5String() |
|
| 345 | - { |
|
| 346 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 347 | - throw new \UnexpectedValueException( |
|
| 348 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 349 | - } |
|
| 350 | - return $this->_element; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Get the wrapped element as an UTC time type. |
|
| 355 | - * |
|
| 356 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 357 | - * @return Primitive\UTCTime |
|
| 358 | - */ |
|
| 359 | - public function asUTCTime() |
|
| 360 | - { |
|
| 361 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 362 | - throw new \UnexpectedValueException( |
|
| 363 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 364 | - } |
|
| 365 | - return $this->_element; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Get the wrapped element as a generalized time type. |
|
| 370 | - * |
|
| 371 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 372 | - * time |
|
| 373 | - * @return Primitive\GeneralizedTime |
|
| 374 | - */ |
|
| 375 | - public function asGeneralizedTime() |
|
| 376 | - { |
|
| 377 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 378 | - throw new \UnexpectedValueException( |
|
| 379 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 380 | - } |
|
| 381 | - return $this->_element; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Get the wrapped element as a graphic string type. |
|
| 386 | - * |
|
| 387 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 388 | - * @return Primitive\GraphicString |
|
| 389 | - */ |
|
| 390 | - public function asGraphicString() |
|
| 391 | - { |
|
| 392 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 393 | - throw new \UnexpectedValueException( |
|
| 394 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 395 | - } |
|
| 396 | - return $this->_element; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Get the wrapped element as a visible string type. |
|
| 401 | - * |
|
| 402 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 403 | - * @return Primitive\VisibleString |
|
| 404 | - */ |
|
| 405 | - public function asVisibleString() |
|
| 406 | - { |
|
| 407 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 408 | - throw new \UnexpectedValueException( |
|
| 409 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 410 | - } |
|
| 411 | - return $this->_element; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Get the wrapped element as a general string type. |
|
| 416 | - * |
|
| 417 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 418 | - * @return Primitive\GeneralString |
|
| 419 | - */ |
|
| 420 | - public function asGeneralString() |
|
| 421 | - { |
|
| 422 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 423 | - throw new \UnexpectedValueException( |
|
| 424 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 425 | - } |
|
| 426 | - return $this->_element; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Get the wrapped element as a universal string type. |
|
| 431 | - * |
|
| 432 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 433 | - * string |
|
| 434 | - * @return Primitive\UniversalString |
|
| 435 | - */ |
|
| 436 | - public function asUniversalString() |
|
| 437 | - { |
|
| 438 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 439 | - throw new \UnexpectedValueException( |
|
| 440 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 441 | - } |
|
| 442 | - return $this->_element; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Get the wrapped element as a character string type. |
|
| 447 | - * |
|
| 448 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 449 | - * string |
|
| 450 | - * @return Primitive\CharacterString |
|
| 451 | - */ |
|
| 452 | - public function asCharacterString() |
|
| 453 | - { |
|
| 454 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 455 | - throw new \UnexpectedValueException( |
|
| 456 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 457 | - } |
|
| 458 | - return $this->_element; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Get the wrapped element as a BMP string type. |
|
| 463 | - * |
|
| 464 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 465 | - * @return Primitive\BMPString |
|
| 466 | - */ |
|
| 467 | - public function asBMPString() |
|
| 468 | - { |
|
| 469 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 470 | - throw new \UnexpectedValueException( |
|
| 471 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 472 | - } |
|
| 473 | - return $this->_element; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Get the wrapped element as any string type. |
|
| 478 | - * |
|
| 479 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 480 | - * @return StringType |
|
| 481 | - */ |
|
| 482 | - public function asString() |
|
| 483 | - { |
|
| 484 | - if (!$this->_element instanceof StringType) { |
|
| 485 | - throw new \UnexpectedValueException( |
|
| 486 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 487 | - } |
|
| 488 | - return $this->_element; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Get the wrapped element as any time type. |
|
| 493 | - * |
|
| 494 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 495 | - * @return TimeType |
|
| 496 | - */ |
|
| 497 | - public function asTime() |
|
| 498 | - { |
|
| 499 | - if (!$this->_element instanceof TimeType) { |
|
| 500 | - throw new \UnexpectedValueException( |
|
| 501 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 502 | - } |
|
| 503 | - return $this->_element; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 508 | - * |
|
| 509 | - * @param int $tag Type tag of the expected element |
|
| 510 | - * @return string |
|
| 511 | - */ |
|
| 512 | - private function _generateExceptionMessage($tag) |
|
| 513 | - { |
|
| 514 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 515 | - $this->_typeDescriptorString()); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 520 | - * |
|
| 521 | - * @return string |
|
| 522 | - */ |
|
| 523 | - private function _typeDescriptorString() |
|
| 524 | - { |
|
| 525 | - $type_cls = $this->_element->typeClass(); |
|
| 526 | - $tag = $this->_element->tag(); |
|
| 527 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 528 | - return Element::tagToName($tag); |
|
| 529 | - } |
|
| 530 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * |
|
| 535 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 536 | - * @return string |
|
| 537 | - */ |
|
| 538 | - public function toDER() |
|
| 539 | - { |
|
| 540 | - return $this->_element->toDER(); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * |
|
| 545 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 546 | - * @return int |
|
| 547 | - */ |
|
| 548 | - public function typeClass() |
|
| 549 | - { |
|
| 550 | - return $this->_element->typeClass(); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * |
|
| 555 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 556 | - * @return bool |
|
| 557 | - */ |
|
| 558 | - public function isConstructed() |
|
| 559 | - { |
|
| 560 | - return $this->_element->isConstructed(); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * |
|
| 565 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 566 | - * @return int |
|
| 567 | - */ |
|
| 568 | - public function tag() |
|
| 569 | - { |
|
| 570 | - return $this->_element->tag(); |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * |
|
| 575 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 576 | - * @return bool |
|
| 577 | - */ |
|
| 578 | - public function isType($tag) |
|
| 579 | - { |
|
| 580 | - return $this->_element->isType($tag); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * |
|
| 585 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 586 | - * type strictness. |
|
| 587 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 588 | - * @return ElementBase |
|
| 589 | - */ |
|
| 590 | - public function expectType($tag) |
|
| 591 | - { |
|
| 592 | - return $this->_element->expectType($tag); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * |
|
| 597 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 598 | - * @return bool |
|
| 599 | - */ |
|
| 600 | - public function isTagged() |
|
| 601 | - { |
|
| 602 | - return $this->_element->isTagged(); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * |
|
| 607 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 608 | - * type strictness. |
|
| 609 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 610 | - * @return TaggedType |
|
| 611 | - */ |
|
| 612 | - public function expectTagged($tag = null) |
|
| 613 | - { |
|
| 614 | - return $this->_element->expectTagged($tag); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * |
|
| 619 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 620 | - * @return Element |
|
| 621 | - */ |
|
| 622 | - public function asElement() |
|
| 623 | - { |
|
| 624 | - return $this->_element; |
|
| 625 | - } |
|
| 18 | + /** |
|
| 19 | + * The wrapped element. |
|
| 20 | + * |
|
| 21 | + * @var Element |
|
| 22 | + */ |
|
| 23 | + private $_element; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Constructor. |
|
| 27 | + * |
|
| 28 | + * @param Element $el |
|
| 29 | + */ |
|
| 30 | + public function __construct(Element $el) |
|
| 31 | + { |
|
| 32 | + $this->_element = $el; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Initialize from ElementBase interface. |
|
| 37 | + * |
|
| 38 | + * @param ElementBase $el |
|
| 39 | + * @return self |
|
| 40 | + */ |
|
| 41 | + public static function fromElementBase(ElementBase $el) |
|
| 42 | + { |
|
| 43 | + // if element is already wrapped |
|
| 44 | + if ($el instanceof self) { |
|
| 45 | + return $el; |
|
| 46 | + } |
|
| 47 | + return new self($el->asElement()); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Compatibility method to dispatch calls to the wrapped element. |
|
| 52 | + * |
|
| 53 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 54 | + * @param string $mtd Method name |
|
| 55 | + * @param array $args Arguments |
|
| 56 | + * @return mixed |
|
| 57 | + */ |
|
| 58 | + public function __call($mtd, array $args) |
|
| 59 | + { |
|
| 60 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get the wrapped element as a context specific tagged type. |
|
| 65 | + * |
|
| 66 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 67 | + * @return TaggedType |
|
| 68 | + */ |
|
| 69 | + public function asTagged() |
|
| 70 | + { |
|
| 71 | + if (!$this->_element instanceof TaggedType) { |
|
| 72 | + throw new \UnexpectedValueException( |
|
| 73 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 74 | + } |
|
| 75 | + return $this->_element; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get the wrapped element as a boolean type. |
|
| 80 | + * |
|
| 81 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 82 | + * @return Primitive\Boolean |
|
| 83 | + */ |
|
| 84 | + public function asBoolean() |
|
| 85 | + { |
|
| 86 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 87 | + throw new \UnexpectedValueException( |
|
| 88 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 89 | + } |
|
| 90 | + return $this->_element; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the wrapped element as an integer type. |
|
| 95 | + * |
|
| 96 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 97 | + * @return Primitive\Integer |
|
| 98 | + */ |
|
| 99 | + public function asInteger() |
|
| 100 | + { |
|
| 101 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 102 | + throw new \UnexpectedValueException( |
|
| 103 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 104 | + } |
|
| 105 | + return $this->_element; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the wrapped element as a bit string type. |
|
| 110 | + * |
|
| 111 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 112 | + * @return Primitive\BitString |
|
| 113 | + */ |
|
| 114 | + public function asBitString() |
|
| 115 | + { |
|
| 116 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 117 | + throw new \UnexpectedValueException( |
|
| 118 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 119 | + } |
|
| 120 | + return $this->_element; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get the wrapped element as an octet string type. |
|
| 125 | + * |
|
| 126 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 127 | + * @return Primitive\OctetString |
|
| 128 | + */ |
|
| 129 | + public function asOctetString() |
|
| 130 | + { |
|
| 131 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 132 | + throw new \UnexpectedValueException( |
|
| 133 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 134 | + } |
|
| 135 | + return $this->_element; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get the wrapped element as a null type. |
|
| 140 | + * |
|
| 141 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 142 | + * @return Primitive\NullType |
|
| 143 | + */ |
|
| 144 | + public function asNull() |
|
| 145 | + { |
|
| 146 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 147 | + throw new \UnexpectedValueException( |
|
| 148 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 149 | + } |
|
| 150 | + return $this->_element; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the wrapped element as an object identifier type. |
|
| 155 | + * |
|
| 156 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 157 | + * identifier |
|
| 158 | + * @return Primitive\ObjectIdentifier |
|
| 159 | + */ |
|
| 160 | + public function asObjectIdentifier() |
|
| 161 | + { |
|
| 162 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 163 | + throw new \UnexpectedValueException( |
|
| 164 | + $this->_generateExceptionMessage( |
|
| 165 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 166 | + } |
|
| 167 | + return $this->_element; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Get the wrapped element as an object descriptor type. |
|
| 172 | + * |
|
| 173 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 174 | + * descriptor |
|
| 175 | + * @return Primitive\ObjectDescriptor |
|
| 176 | + */ |
|
| 177 | + public function asObjectDescriptor() |
|
| 178 | + { |
|
| 179 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 180 | + throw new \UnexpectedValueException( |
|
| 181 | + $this->_generateExceptionMessage( |
|
| 182 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 183 | + } |
|
| 184 | + return $this->_element; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Get the wrapped element as a real type. |
|
| 189 | + * |
|
| 190 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 191 | + * @return Primitive\Real |
|
| 192 | + */ |
|
| 193 | + public function asReal() |
|
| 194 | + { |
|
| 195 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 196 | + throw new \UnexpectedValueException( |
|
| 197 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 198 | + } |
|
| 199 | + return $this->_element; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Get the wrapped element as an enumerated type. |
|
| 204 | + * |
|
| 205 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 206 | + * @return Primitive\Enumerated |
|
| 207 | + */ |
|
| 208 | + public function asEnumerated() |
|
| 209 | + { |
|
| 210 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 211 | + throw new \UnexpectedValueException( |
|
| 212 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 213 | + } |
|
| 214 | + return $this->_element; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Get the wrapped element as a UTF8 string type. |
|
| 219 | + * |
|
| 220 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 221 | + * @return Primitive\UTF8String |
|
| 222 | + */ |
|
| 223 | + public function asUTF8String() |
|
| 224 | + { |
|
| 225 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 226 | + throw new \UnexpectedValueException( |
|
| 227 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 228 | + } |
|
| 229 | + return $this->_element; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Get the wrapped element as a relative OID type. |
|
| 234 | + * |
|
| 235 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 236 | + * @return Primitive\RelativeOID |
|
| 237 | + */ |
|
| 238 | + public function asRelativeOID() |
|
| 239 | + { |
|
| 240 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 241 | + throw new \UnexpectedValueException( |
|
| 242 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 243 | + } |
|
| 244 | + return $this->_element; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Get the wrapped element as a sequence type. |
|
| 249 | + * |
|
| 250 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 251 | + * @return Constructed\Sequence |
|
| 252 | + */ |
|
| 253 | + public function asSequence() |
|
| 254 | + { |
|
| 255 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 256 | + throw new \UnexpectedValueException( |
|
| 257 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 258 | + } |
|
| 259 | + return $this->_element; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Get the wrapped element as a set type. |
|
| 264 | + * |
|
| 265 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 266 | + * @return Constructed\Set |
|
| 267 | + */ |
|
| 268 | + public function asSet() |
|
| 269 | + { |
|
| 270 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 271 | + throw new \UnexpectedValueException( |
|
| 272 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 273 | + } |
|
| 274 | + return $this->_element; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Get the wrapped element as a numeric string type. |
|
| 279 | + * |
|
| 280 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 281 | + * @return Primitive\NumericString |
|
| 282 | + */ |
|
| 283 | + public function asNumericString() |
|
| 284 | + { |
|
| 285 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 286 | + throw new \UnexpectedValueException( |
|
| 287 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 288 | + } |
|
| 289 | + return $this->_element; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Get the wrapped element as a printable string type. |
|
| 294 | + * |
|
| 295 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 296 | + * string |
|
| 297 | + * @return Primitive\PrintableString |
|
| 298 | + */ |
|
| 299 | + public function asPrintableString() |
|
| 300 | + { |
|
| 301 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 302 | + throw new \UnexpectedValueException( |
|
| 303 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 304 | + } |
|
| 305 | + return $this->_element; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Get the wrapped element as a T61 string type. |
|
| 310 | + * |
|
| 311 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 312 | + * @return Primitive\T61String |
|
| 313 | + */ |
|
| 314 | + public function asT61String() |
|
| 315 | + { |
|
| 316 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 317 | + throw new \UnexpectedValueException( |
|
| 318 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 319 | + } |
|
| 320 | + return $this->_element; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Get the wrapped element as a videotex string type. |
|
| 325 | + * |
|
| 326 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 327 | + * @return Primitive\VideotexString |
|
| 328 | + */ |
|
| 329 | + public function asVideotexString() |
|
| 330 | + { |
|
| 331 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 332 | + throw new \UnexpectedValueException( |
|
| 333 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 334 | + } |
|
| 335 | + return $this->_element; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Get the wrapped element as a IA5 string type. |
|
| 340 | + * |
|
| 341 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 342 | + * @return Primitive\IA5String |
|
| 343 | + */ |
|
| 344 | + public function asIA5String() |
|
| 345 | + { |
|
| 346 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 347 | + throw new \UnexpectedValueException( |
|
| 348 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 349 | + } |
|
| 350 | + return $this->_element; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Get the wrapped element as an UTC time type. |
|
| 355 | + * |
|
| 356 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 357 | + * @return Primitive\UTCTime |
|
| 358 | + */ |
|
| 359 | + public function asUTCTime() |
|
| 360 | + { |
|
| 361 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 362 | + throw new \UnexpectedValueException( |
|
| 363 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 364 | + } |
|
| 365 | + return $this->_element; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Get the wrapped element as a generalized time type. |
|
| 370 | + * |
|
| 371 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 372 | + * time |
|
| 373 | + * @return Primitive\GeneralizedTime |
|
| 374 | + */ |
|
| 375 | + public function asGeneralizedTime() |
|
| 376 | + { |
|
| 377 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 378 | + throw new \UnexpectedValueException( |
|
| 379 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 380 | + } |
|
| 381 | + return $this->_element; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Get the wrapped element as a graphic string type. |
|
| 386 | + * |
|
| 387 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 388 | + * @return Primitive\GraphicString |
|
| 389 | + */ |
|
| 390 | + public function asGraphicString() |
|
| 391 | + { |
|
| 392 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 393 | + throw new \UnexpectedValueException( |
|
| 394 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 395 | + } |
|
| 396 | + return $this->_element; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Get the wrapped element as a visible string type. |
|
| 401 | + * |
|
| 402 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 403 | + * @return Primitive\VisibleString |
|
| 404 | + */ |
|
| 405 | + public function asVisibleString() |
|
| 406 | + { |
|
| 407 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 408 | + throw new \UnexpectedValueException( |
|
| 409 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 410 | + } |
|
| 411 | + return $this->_element; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Get the wrapped element as a general string type. |
|
| 416 | + * |
|
| 417 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 418 | + * @return Primitive\GeneralString |
|
| 419 | + */ |
|
| 420 | + public function asGeneralString() |
|
| 421 | + { |
|
| 422 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 423 | + throw new \UnexpectedValueException( |
|
| 424 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 425 | + } |
|
| 426 | + return $this->_element; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Get the wrapped element as a universal string type. |
|
| 431 | + * |
|
| 432 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 433 | + * string |
|
| 434 | + * @return Primitive\UniversalString |
|
| 435 | + */ |
|
| 436 | + public function asUniversalString() |
|
| 437 | + { |
|
| 438 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 439 | + throw new \UnexpectedValueException( |
|
| 440 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 441 | + } |
|
| 442 | + return $this->_element; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Get the wrapped element as a character string type. |
|
| 447 | + * |
|
| 448 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 449 | + * string |
|
| 450 | + * @return Primitive\CharacterString |
|
| 451 | + */ |
|
| 452 | + public function asCharacterString() |
|
| 453 | + { |
|
| 454 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 455 | + throw new \UnexpectedValueException( |
|
| 456 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 457 | + } |
|
| 458 | + return $this->_element; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Get the wrapped element as a BMP string type. |
|
| 463 | + * |
|
| 464 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 465 | + * @return Primitive\BMPString |
|
| 466 | + */ |
|
| 467 | + public function asBMPString() |
|
| 468 | + { |
|
| 469 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 470 | + throw new \UnexpectedValueException( |
|
| 471 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 472 | + } |
|
| 473 | + return $this->_element; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Get the wrapped element as any string type. |
|
| 478 | + * |
|
| 479 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 480 | + * @return StringType |
|
| 481 | + */ |
|
| 482 | + public function asString() |
|
| 483 | + { |
|
| 484 | + if (!$this->_element instanceof StringType) { |
|
| 485 | + throw new \UnexpectedValueException( |
|
| 486 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 487 | + } |
|
| 488 | + return $this->_element; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Get the wrapped element as any time type. |
|
| 493 | + * |
|
| 494 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 495 | + * @return TimeType |
|
| 496 | + */ |
|
| 497 | + public function asTime() |
|
| 498 | + { |
|
| 499 | + if (!$this->_element instanceof TimeType) { |
|
| 500 | + throw new \UnexpectedValueException( |
|
| 501 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 502 | + } |
|
| 503 | + return $this->_element; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 508 | + * |
|
| 509 | + * @param int $tag Type tag of the expected element |
|
| 510 | + * @return string |
|
| 511 | + */ |
|
| 512 | + private function _generateExceptionMessage($tag) |
|
| 513 | + { |
|
| 514 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 515 | + $this->_typeDescriptorString()); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 520 | + * |
|
| 521 | + * @return string |
|
| 522 | + */ |
|
| 523 | + private function _typeDescriptorString() |
|
| 524 | + { |
|
| 525 | + $type_cls = $this->_element->typeClass(); |
|
| 526 | + $tag = $this->_element->tag(); |
|
| 527 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 528 | + return Element::tagToName($tag); |
|
| 529 | + } |
|
| 530 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * |
|
| 535 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 536 | + * @return string |
|
| 537 | + */ |
|
| 538 | + public function toDER() |
|
| 539 | + { |
|
| 540 | + return $this->_element->toDER(); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * |
|
| 545 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 546 | + * @return int |
|
| 547 | + */ |
|
| 548 | + public function typeClass() |
|
| 549 | + { |
|
| 550 | + return $this->_element->typeClass(); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * |
|
| 555 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 556 | + * @return bool |
|
| 557 | + */ |
|
| 558 | + public function isConstructed() |
|
| 559 | + { |
|
| 560 | + return $this->_element->isConstructed(); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * |
|
| 565 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 566 | + * @return int |
|
| 567 | + */ |
|
| 568 | + public function tag() |
|
| 569 | + { |
|
| 570 | + return $this->_element->tag(); |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * |
|
| 575 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 576 | + * @return bool |
|
| 577 | + */ |
|
| 578 | + public function isType($tag) |
|
| 579 | + { |
|
| 580 | + return $this->_element->isType($tag); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * |
|
| 585 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 586 | + * type strictness. |
|
| 587 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 588 | + * @return ElementBase |
|
| 589 | + */ |
|
| 590 | + public function expectType($tag) |
|
| 591 | + { |
|
| 592 | + return $this->_element->expectType($tag); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * |
|
| 597 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 598 | + * @return bool |
|
| 599 | + */ |
|
| 600 | + public function isTagged() |
|
| 601 | + { |
|
| 602 | + return $this->_element->isTagged(); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * |
|
| 607 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 608 | + * type strictness. |
|
| 609 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 610 | + * @return TaggedType |
|
| 611 | + */ |
|
| 612 | + public function expectTagged($tag = null) |
|
| 613 | + { |
|
| 614 | + return $this->_element->expectTagged($tag); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * |
|
| 619 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 620 | + * @return Element |
|
| 621 | + */ |
|
| 622 | + public function asElement() |
|
| 623 | + { |
|
| 624 | + return $this->_element; |
|
| 625 | + } |
|
| 626 | 626 | } |
@@ -11,335 +11,335 @@ |
||
| 11 | 11 | * Base class for the constructed types. |
| 12 | 12 | */ |
| 13 | 13 | abstract class Structure extends Element implements |
| 14 | - \Countable, |
|
| 15 | - \IteratorAggregate |
|
| 14 | + \Countable, |
|
| 15 | + \IteratorAggregate |
|
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Array of elements in the structure. |
|
| 21 | - * |
|
| 22 | - * @var Element[] $_elements |
|
| 23 | - */ |
|
| 24 | - protected $_elements; |
|
| 19 | + /** |
|
| 20 | + * Array of elements in the structure. |
|
| 21 | + * |
|
| 22 | + * @var Element[] $_elements |
|
| 23 | + */ |
|
| 24 | + protected $_elements; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Lookup table for the tagged elements. |
|
| 28 | - * |
|
| 29 | - * @var TaggedType[]|null $_taggedMap |
|
| 30 | - */ |
|
| 31 | - private $_taggedMap; |
|
| 26 | + /** |
|
| 27 | + * Lookup table for the tagged elements. |
|
| 28 | + * |
|
| 29 | + * @var TaggedType[]|null $_taggedMap |
|
| 30 | + */ |
|
| 31 | + private $_taggedMap; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 35 | - * |
|
| 36 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 37 | - */ |
|
| 38 | - private $_unspecifiedTypes; |
|
| 33 | + /** |
|
| 34 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 35 | + * |
|
| 36 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 37 | + */ |
|
| 38 | + private $_unspecifiedTypes; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Constructor. |
|
| 42 | - * |
|
| 43 | - * @param Element ...$elements Any number of elements |
|
| 44 | - */ |
|
| 45 | - public function __construct(Element ...$elements) |
|
| 46 | - { |
|
| 47 | - $this->_elements = $elements; |
|
| 48 | - } |
|
| 40 | + /** |
|
| 41 | + * Constructor. |
|
| 42 | + * |
|
| 43 | + * @param Element ...$elements Any number of elements |
|
| 44 | + */ |
|
| 45 | + public function __construct(Element ...$elements) |
|
| 46 | + { |
|
| 47 | + $this->_elements = $elements; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Clone magic method. |
|
| 52 | - */ |
|
| 53 | - public function __clone() |
|
| 54 | - { |
|
| 55 | - // clear cache-variables |
|
| 56 | - $this->_taggedMap = null; |
|
| 57 | - $this->_unspecifiedTypes = null; |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * Clone magic method. |
|
| 52 | + */ |
|
| 53 | + public function __clone() |
|
| 54 | + { |
|
| 55 | + // clear cache-variables |
|
| 56 | + $this->_taggedMap = null; |
|
| 57 | + $this->_unspecifiedTypes = null; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * |
|
| 62 | - * @see \ASN1\Element::isConstructed() |
|
| 63 | - * @return bool |
|
| 64 | - */ |
|
| 65 | - public function isConstructed() |
|
| 66 | - { |
|
| 67 | - return true; |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * |
|
| 62 | + * @see \ASN1\Element::isConstructed() |
|
| 63 | + * @return bool |
|
| 64 | + */ |
|
| 65 | + public function isConstructed() |
|
| 66 | + { |
|
| 67 | + return true; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * |
|
| 72 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - protected function _encodedContentDER() |
|
| 76 | - { |
|
| 77 | - $data = ""; |
|
| 78 | - foreach ($this->_elements as $element) { |
|
| 79 | - $data .= $element->toDER(); |
|
| 80 | - } |
|
| 81 | - return $data; |
|
| 82 | - } |
|
| 70 | + /** |
|
| 71 | + * |
|
| 72 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + protected function _encodedContentDER() |
|
| 76 | + { |
|
| 77 | + $data = ""; |
|
| 78 | + foreach ($this->_elements as $element) { |
|
| 79 | + $data .= $element->toDER(); |
|
| 80 | + } |
|
| 81 | + return $data; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * |
|
| 86 | - * @see \ASN1\Element::_decodeFromDER() |
|
| 87 | - * @return self |
|
| 88 | - */ |
|
| 89 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 90 | - &$offset) |
|
| 91 | - { |
|
| 92 | - $idx = $offset; |
|
| 93 | - if (!$identifier->isConstructed()) { |
|
| 94 | - throw new DecodeException( |
|
| 95 | - "Structured element must have constructed bit set."); |
|
| 96 | - } |
|
| 97 | - $length = Length::expectFromDER($data, $idx); |
|
| 98 | - $end = $idx + $length->length(); |
|
| 99 | - $elements = array(); |
|
| 100 | - while ($idx < $end) { |
|
| 101 | - $elements[] = Element::fromDER($data, $idx); |
|
| 102 | - // check that element didn't overflow length |
|
| 103 | - if ($idx > $end) { |
|
| 104 | - throw new DecodeException( |
|
| 105 | - "Structure's content overflows length."); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - $offset = $idx; |
|
| 109 | - // return instance by static late binding |
|
| 110 | - return new static(...$elements); |
|
| 111 | - } |
|
| 84 | + /** |
|
| 85 | + * |
|
| 86 | + * @see \ASN1\Element::_decodeFromDER() |
|
| 87 | + * @return self |
|
| 88 | + */ |
|
| 89 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 90 | + &$offset) |
|
| 91 | + { |
|
| 92 | + $idx = $offset; |
|
| 93 | + if (!$identifier->isConstructed()) { |
|
| 94 | + throw new DecodeException( |
|
| 95 | + "Structured element must have constructed bit set."); |
|
| 96 | + } |
|
| 97 | + $length = Length::expectFromDER($data, $idx); |
|
| 98 | + $end = $idx + $length->length(); |
|
| 99 | + $elements = array(); |
|
| 100 | + while ($idx < $end) { |
|
| 101 | + $elements[] = Element::fromDER($data, $idx); |
|
| 102 | + // check that element didn't overflow length |
|
| 103 | + if ($idx > $end) { |
|
| 104 | + throw new DecodeException( |
|
| 105 | + "Structure's content overflows length."); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + $offset = $idx; |
|
| 109 | + // return instance by static late binding |
|
| 110 | + return new static(...$elements); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Explode DER structure to DER encoded components that it contains. |
|
| 115 | - * |
|
| 116 | - * @param string $data |
|
| 117 | - * @throws DecodeException |
|
| 118 | - * @return string[] |
|
| 119 | - */ |
|
| 120 | - public static function explodeDER($data) |
|
| 121 | - { |
|
| 122 | - $offset = 0; |
|
| 123 | - $identifier = Identifier::fromDER($data, $offset); |
|
| 124 | - if (!$identifier->isConstructed()) { |
|
| 125 | - throw new DecodeException("Element is not constructed."); |
|
| 126 | - } |
|
| 127 | - $length = Length::expectFromDER($data, $offset); |
|
| 128 | - $end = $offset + $length->length(); |
|
| 129 | - $parts = array(); |
|
| 130 | - while ($offset < $end) { |
|
| 131 | - // start of the element |
|
| 132 | - $idx = $offset; |
|
| 133 | - // skip identifier |
|
| 134 | - Identifier::fromDER($data, $offset); |
|
| 135 | - // decode element length |
|
| 136 | - $length = Length::expectFromDER($data, $offset); |
|
| 137 | - // extract der encoding of the element |
|
| 138 | - $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 139 | - // update offset over content |
|
| 140 | - $offset += $length->length(); |
|
| 141 | - } |
|
| 142 | - return $parts; |
|
| 143 | - } |
|
| 113 | + /** |
|
| 114 | + * Explode DER structure to DER encoded components that it contains. |
|
| 115 | + * |
|
| 116 | + * @param string $data |
|
| 117 | + * @throws DecodeException |
|
| 118 | + * @return string[] |
|
| 119 | + */ |
|
| 120 | + public static function explodeDER($data) |
|
| 121 | + { |
|
| 122 | + $offset = 0; |
|
| 123 | + $identifier = Identifier::fromDER($data, $offset); |
|
| 124 | + if (!$identifier->isConstructed()) { |
|
| 125 | + throw new DecodeException("Element is not constructed."); |
|
| 126 | + } |
|
| 127 | + $length = Length::expectFromDER($data, $offset); |
|
| 128 | + $end = $offset + $length->length(); |
|
| 129 | + $parts = array(); |
|
| 130 | + while ($offset < $end) { |
|
| 131 | + // start of the element |
|
| 132 | + $idx = $offset; |
|
| 133 | + // skip identifier |
|
| 134 | + Identifier::fromDER($data, $offset); |
|
| 135 | + // decode element length |
|
| 136 | + $length = Length::expectFromDER($data, $offset); |
|
| 137 | + // extract der encoding of the element |
|
| 138 | + $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 139 | + // update offset over content |
|
| 140 | + $offset += $length->length(); |
|
| 141 | + } |
|
| 142 | + return $parts; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Get self with an element at the given index replaced by another. |
|
| 147 | - * |
|
| 148 | - * @param int $idx Element index |
|
| 149 | - * @param Element $el New element to insert into the structure |
|
| 150 | - * @throws \OutOfBoundsException |
|
| 151 | - * @return self |
|
| 152 | - */ |
|
| 153 | - public function withReplaced($idx, Element $el) |
|
| 154 | - { |
|
| 155 | - if (!isset($this->_elements[$idx])) { |
|
| 156 | - throw new \OutOfBoundsException( |
|
| 157 | - "Structure doesn't have element at index $idx."); |
|
| 158 | - } |
|
| 159 | - $obj = clone $this; |
|
| 160 | - $obj->_elements[$idx] = $el; |
|
| 161 | - return $obj; |
|
| 162 | - } |
|
| 145 | + /** |
|
| 146 | + * Get self with an element at the given index replaced by another. |
|
| 147 | + * |
|
| 148 | + * @param int $idx Element index |
|
| 149 | + * @param Element $el New element to insert into the structure |
|
| 150 | + * @throws \OutOfBoundsException |
|
| 151 | + * @return self |
|
| 152 | + */ |
|
| 153 | + public function withReplaced($idx, Element $el) |
|
| 154 | + { |
|
| 155 | + if (!isset($this->_elements[$idx])) { |
|
| 156 | + throw new \OutOfBoundsException( |
|
| 157 | + "Structure doesn't have element at index $idx."); |
|
| 158 | + } |
|
| 159 | + $obj = clone $this; |
|
| 160 | + $obj->_elements[$idx] = $el; |
|
| 161 | + return $obj; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Get self with an element inserted before the given index. |
|
| 166 | - * |
|
| 167 | - * @param int $idx Element index |
|
| 168 | - * @param Element $el New element to insert into the structure |
|
| 169 | - * @throws \OutOfBoundsException |
|
| 170 | - * @return self |
|
| 171 | - */ |
|
| 172 | - public function withInserted($idx, Element $el) |
|
| 173 | - { |
|
| 174 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
| 175 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 176 | - } |
|
| 177 | - $obj = clone $this; |
|
| 178 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 179 | - return $obj; |
|
| 180 | - } |
|
| 164 | + /** |
|
| 165 | + * Get self with an element inserted before the given index. |
|
| 166 | + * |
|
| 167 | + * @param int $idx Element index |
|
| 168 | + * @param Element $el New element to insert into the structure |
|
| 169 | + * @throws \OutOfBoundsException |
|
| 170 | + * @return self |
|
| 171 | + */ |
|
| 172 | + public function withInserted($idx, Element $el) |
|
| 173 | + { |
|
| 174 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
| 175 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 176 | + } |
|
| 177 | + $obj = clone $this; |
|
| 178 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 179 | + return $obj; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get self with an element appended to the end. |
|
| 184 | - * |
|
| 185 | - * @param Element $el Element to insert into the structure |
|
| 186 | - * @return self |
|
| 187 | - */ |
|
| 188 | - public function withAppended(Element $el) |
|
| 189 | - { |
|
| 190 | - $obj = clone $this; |
|
| 191 | - array_push($obj->_elements, $el); |
|
| 192 | - return $obj; |
|
| 193 | - } |
|
| 182 | + /** |
|
| 183 | + * Get self with an element appended to the end. |
|
| 184 | + * |
|
| 185 | + * @param Element $el Element to insert into the structure |
|
| 186 | + * @return self |
|
| 187 | + */ |
|
| 188 | + public function withAppended(Element $el) |
|
| 189 | + { |
|
| 190 | + $obj = clone $this; |
|
| 191 | + array_push($obj->_elements, $el); |
|
| 192 | + return $obj; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Get self with an element prepended in the beginning. |
|
| 197 | - * |
|
| 198 | - * @param Element $el Element to insert into the structure |
|
| 199 | - * @return self |
|
| 200 | - */ |
|
| 201 | - public function withPrepended(Element $el) |
|
| 202 | - { |
|
| 203 | - $obj = clone $this; |
|
| 204 | - array_unshift($obj->_elements, $el); |
|
| 205 | - return $obj; |
|
| 206 | - } |
|
| 195 | + /** |
|
| 196 | + * Get self with an element prepended in the beginning. |
|
| 197 | + * |
|
| 198 | + * @param Element $el Element to insert into the structure |
|
| 199 | + * @return self |
|
| 200 | + */ |
|
| 201 | + public function withPrepended(Element $el) |
|
| 202 | + { |
|
| 203 | + $obj = clone $this; |
|
| 204 | + array_unshift($obj->_elements, $el); |
|
| 205 | + return $obj; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Get self with an element at the given index removed. |
|
| 210 | - * |
|
| 211 | - * @param int $idx Element index |
|
| 212 | - * @throws \OutOfBoundsException |
|
| 213 | - * @return self |
|
| 214 | - */ |
|
| 215 | - public function withoutElement($idx) |
|
| 216 | - { |
|
| 217 | - if (!isset($this->_elements[$idx])) { |
|
| 218 | - throw new \OutOfBoundsException( |
|
| 219 | - "Structure doesn't have element at index $idx."); |
|
| 220 | - } |
|
| 221 | - $obj = clone $this; |
|
| 222 | - array_splice($obj->_elements, $idx, 1); |
|
| 223 | - return $obj; |
|
| 224 | - } |
|
| 208 | + /** |
|
| 209 | + * Get self with an element at the given index removed. |
|
| 210 | + * |
|
| 211 | + * @param int $idx Element index |
|
| 212 | + * @throws \OutOfBoundsException |
|
| 213 | + * @return self |
|
| 214 | + */ |
|
| 215 | + public function withoutElement($idx) |
|
| 216 | + { |
|
| 217 | + if (!isset($this->_elements[$idx])) { |
|
| 218 | + throw new \OutOfBoundsException( |
|
| 219 | + "Structure doesn't have element at index $idx."); |
|
| 220 | + } |
|
| 221 | + $obj = clone $this; |
|
| 222 | + array_splice($obj->_elements, $idx, 1); |
|
| 223 | + return $obj; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Get elements in the structure. |
|
| 228 | - * |
|
| 229 | - * @return UnspecifiedType[] |
|
| 230 | - */ |
|
| 231 | - public function elements() |
|
| 232 | - { |
|
| 233 | - if (!isset($this->_unspecifiedTypes)) { |
|
| 234 | - $this->_unspecifiedTypes = array_map( |
|
| 235 | - function (Element $el) { |
|
| 236 | - return new UnspecifiedType($el); |
|
| 237 | - }, $this->_elements); |
|
| 238 | - } |
|
| 239 | - return $this->_unspecifiedTypes; |
|
| 240 | - } |
|
| 226 | + /** |
|
| 227 | + * Get elements in the structure. |
|
| 228 | + * |
|
| 229 | + * @return UnspecifiedType[] |
|
| 230 | + */ |
|
| 231 | + public function elements() |
|
| 232 | + { |
|
| 233 | + if (!isset($this->_unspecifiedTypes)) { |
|
| 234 | + $this->_unspecifiedTypes = array_map( |
|
| 235 | + function (Element $el) { |
|
| 236 | + return new UnspecifiedType($el); |
|
| 237 | + }, $this->_elements); |
|
| 238 | + } |
|
| 239 | + return $this->_unspecifiedTypes; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * Check whether the structure has an element at the given index, optionally |
|
| 244 | - * satisfying given tag expectation. |
|
| 245 | - * |
|
| 246 | - * @param int $idx Index 0..n |
|
| 247 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 248 | - * @return bool |
|
| 249 | - */ |
|
| 250 | - public function has($idx, $expectedTag = null) |
|
| 251 | - { |
|
| 252 | - if (!isset($this->_elements[$idx])) { |
|
| 253 | - return false; |
|
| 254 | - } |
|
| 255 | - if (isset($expectedTag)) { |
|
| 256 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 257 | - return false; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - return true; |
|
| 261 | - } |
|
| 242 | + /** |
|
| 243 | + * Check whether the structure has an element at the given index, optionally |
|
| 244 | + * satisfying given tag expectation. |
|
| 245 | + * |
|
| 246 | + * @param int $idx Index 0..n |
|
| 247 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 248 | + * @return bool |
|
| 249 | + */ |
|
| 250 | + public function has($idx, $expectedTag = null) |
|
| 251 | + { |
|
| 252 | + if (!isset($this->_elements[$idx])) { |
|
| 253 | + return false; |
|
| 254 | + } |
|
| 255 | + if (isset($expectedTag)) { |
|
| 256 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 257 | + return false; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + return true; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Get the element at the given index, optionally checking that the element |
|
| 265 | - * has a given tag. |
|
| 266 | - * |
|
| 267 | - * NOTE! Expectation checking is deprecated and should be done |
|
| 268 | - * with UnspecifiedType. |
|
| 269 | - * |
|
| 270 | - * @param int $idx Index 0..n |
|
| 271 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 272 | - * @throws \OutOfBoundsException If element doesn't exists |
|
| 273 | - * @throws \UnexpectedValueException If expectation fails |
|
| 274 | - * @return UnspecifiedType |
|
| 275 | - */ |
|
| 276 | - public function at($idx, $expectedTag = null) |
|
| 277 | - { |
|
| 278 | - if (!isset($this->_elements[$idx])) { |
|
| 279 | - throw new \OutOfBoundsException( |
|
| 280 | - "Structure doesn't have an element at index $idx."); |
|
| 281 | - } |
|
| 282 | - $element = $this->_elements[$idx]; |
|
| 283 | - if (isset($expectedTag)) { |
|
| 284 | - $element->expectType($expectedTag); |
|
| 285 | - } |
|
| 286 | - return new UnspecifiedType($element); |
|
| 287 | - } |
|
| 263 | + /** |
|
| 264 | + * Get the element at the given index, optionally checking that the element |
|
| 265 | + * has a given tag. |
|
| 266 | + * |
|
| 267 | + * NOTE! Expectation checking is deprecated and should be done |
|
| 268 | + * with UnspecifiedType. |
|
| 269 | + * |
|
| 270 | + * @param int $idx Index 0..n |
|
| 271 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 272 | + * @throws \OutOfBoundsException If element doesn't exists |
|
| 273 | + * @throws \UnexpectedValueException If expectation fails |
|
| 274 | + * @return UnspecifiedType |
|
| 275 | + */ |
|
| 276 | + public function at($idx, $expectedTag = null) |
|
| 277 | + { |
|
| 278 | + if (!isset($this->_elements[$idx])) { |
|
| 279 | + throw new \OutOfBoundsException( |
|
| 280 | + "Structure doesn't have an element at index $idx."); |
|
| 281 | + } |
|
| 282 | + $element = $this->_elements[$idx]; |
|
| 283 | + if (isset($expectedTag)) { |
|
| 284 | + $element->expectType($expectedTag); |
|
| 285 | + } |
|
| 286 | + return new UnspecifiedType($element); |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * Check whether the structure contains a context specific element with a |
|
| 291 | - * given tag. |
|
| 292 | - * |
|
| 293 | - * @param int $tag Tag number |
|
| 294 | - * @return boolean |
|
| 295 | - */ |
|
| 296 | - public function hasTagged($tag) |
|
| 297 | - { |
|
| 298 | - // lazily build lookup map |
|
| 299 | - if (!isset($this->_taggedMap)) { |
|
| 300 | - $this->_taggedMap = array(); |
|
| 301 | - foreach ($this->_elements as $element) { |
|
| 302 | - if ($element->isTagged()) { |
|
| 303 | - $this->_taggedMap[$element->tag()] = $element; |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - return isset($this->_taggedMap[$tag]); |
|
| 308 | - } |
|
| 289 | + /** |
|
| 290 | + * Check whether the structure contains a context specific element with a |
|
| 291 | + * given tag. |
|
| 292 | + * |
|
| 293 | + * @param int $tag Tag number |
|
| 294 | + * @return boolean |
|
| 295 | + */ |
|
| 296 | + public function hasTagged($tag) |
|
| 297 | + { |
|
| 298 | + // lazily build lookup map |
|
| 299 | + if (!isset($this->_taggedMap)) { |
|
| 300 | + $this->_taggedMap = array(); |
|
| 301 | + foreach ($this->_elements as $element) { |
|
| 302 | + if ($element->isTagged()) { |
|
| 303 | + $this->_taggedMap[$element->tag()] = $element; |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + return isset($this->_taggedMap[$tag]); |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - /** |
|
| 311 | - * Get a context specific element tagged with a given tag. |
|
| 312 | - * |
|
| 313 | - * @param int $tag |
|
| 314 | - * @throws \LogicException If tag doesn't exists |
|
| 315 | - * @return TaggedType |
|
| 316 | - */ |
|
| 317 | - public function getTagged($tag) |
|
| 318 | - { |
|
| 319 | - if (!$this->hasTagged($tag)) { |
|
| 320 | - throw new \LogicException("No tagged element for tag $tag."); |
|
| 321 | - } |
|
| 322 | - return $this->_taggedMap[$tag]; |
|
| 323 | - } |
|
| 310 | + /** |
|
| 311 | + * Get a context specific element tagged with a given tag. |
|
| 312 | + * |
|
| 313 | + * @param int $tag |
|
| 314 | + * @throws \LogicException If tag doesn't exists |
|
| 315 | + * @return TaggedType |
|
| 316 | + */ |
|
| 317 | + public function getTagged($tag) |
|
| 318 | + { |
|
| 319 | + if (!$this->hasTagged($tag)) { |
|
| 320 | + throw new \LogicException("No tagged element for tag $tag."); |
|
| 321 | + } |
|
| 322 | + return $this->_taggedMap[$tag]; |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** |
|
| 326 | - * |
|
| 327 | - * @see \Countable::count() |
|
| 328 | - * @return int |
|
| 329 | - */ |
|
| 330 | - public function count() |
|
| 331 | - { |
|
| 332 | - return count($this->_elements); |
|
| 333 | - } |
|
| 325 | + /** |
|
| 326 | + * |
|
| 327 | + * @see \Countable::count() |
|
| 328 | + * @return int |
|
| 329 | + */ |
|
| 330 | + public function count() |
|
| 331 | + { |
|
| 332 | + return count($this->_elements); |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Get an iterator for the UnspecifiedElement objects. |
|
| 337 | - * |
|
| 338 | - * @see \IteratorAggregate::getIterator() |
|
| 339 | - * @return \ArrayIterator |
|
| 340 | - */ |
|
| 341 | - public function getIterator() |
|
| 342 | - { |
|
| 343 | - return new \ArrayIterator($this->elements()); |
|
| 344 | - } |
|
| 335 | + /** |
|
| 336 | + * Get an iterator for the UnspecifiedElement objects. |
|
| 337 | + * |
|
| 338 | + * @see \IteratorAggregate::getIterator() |
|
| 339 | + * @return \ArrayIterator |
|
| 340 | + */ |
|
| 341 | + public function getIterator() |
|
| 342 | + { |
|
| 343 | + return new \ArrayIterator($this->elements()); |
|
| 344 | + } |
|
| 345 | 345 | } |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait UniversalClass |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * |
|
| 14 | - * @see \ASN1\Element::typeClass() |
|
| 15 | - * @return int |
|
| 16 | - */ |
|
| 17 | - public function typeClass() |
|
| 18 | - { |
|
| 19 | - return Identifier::CLASS_UNIVERSAL; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * |
|
| 14 | + * @see \ASN1\Element::typeClass() |
|
| 15 | + * @return int |
|
| 16 | + */ |
|
| 17 | + public function typeClass() |
|
| 18 | + { |
|
| 19 | + return Identifier::CLASS_UNIVERSAL; |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -14,85 +14,85 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | abstract class TaggedType extends Element |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * {@inheritdoc} |
|
| 20 | - */ |
|
| 21 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 22 | - &$offset) |
|
| 23 | - { |
|
| 24 | - $idx = $offset; |
|
| 25 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
| 26 | - $length = Length::expectFromDER($data, $idx); |
|
| 27 | - $offset = $idx + $length->length(); |
|
| 28 | - return $type; |
|
| 29 | - } |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * {@inheritdoc} |
|
| 20 | + */ |
|
| 21 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 22 | + &$offset) |
|
| 23 | + { |
|
| 24 | + $idx = $offset; |
|
| 25 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
| 26 | + $length = Length::expectFromDER($data, $idx); |
|
| 27 | + $offset = $idx + $length->length(); |
|
| 28 | + return $type; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Check whether element supports explicit tagging. |
|
| 33 | - * |
|
| 34 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 35 | - * @throws \UnexpectedValueException If expectation fails |
|
| 36 | - * @return ExplicitTagging |
|
| 37 | - */ |
|
| 38 | - public function expectExplicit($expectedTag = null) |
|
| 39 | - { |
|
| 40 | - $el = $this; |
|
| 41 | - if (!$el instanceof ExplicitTagging) { |
|
| 42 | - throw new \UnexpectedValueException( |
|
| 43 | - "Element doesn't implement explicit tagging."); |
|
| 44 | - } |
|
| 45 | - if (isset($expectedTag)) { |
|
| 46 | - $el->expectTagged($expectedTag); |
|
| 47 | - } |
|
| 48 | - return $el; |
|
| 49 | - } |
|
| 31 | + /** |
|
| 32 | + * Check whether element supports explicit tagging. |
|
| 33 | + * |
|
| 34 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 35 | + * @throws \UnexpectedValueException If expectation fails |
|
| 36 | + * @return ExplicitTagging |
|
| 37 | + */ |
|
| 38 | + public function expectExplicit($expectedTag = null) |
|
| 39 | + { |
|
| 40 | + $el = $this; |
|
| 41 | + if (!$el instanceof ExplicitTagging) { |
|
| 42 | + throw new \UnexpectedValueException( |
|
| 43 | + "Element doesn't implement explicit tagging."); |
|
| 44 | + } |
|
| 45 | + if (isset($expectedTag)) { |
|
| 46 | + $el->expectTagged($expectedTag); |
|
| 47 | + } |
|
| 48 | + return $el; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Get the wrapped inner element employing explicit tagging. |
|
| 53 | - * |
|
| 54 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 55 | - * @throws \UnexpectedValueException If expectation fails |
|
| 56 | - * @return UnspecifiedType |
|
| 57 | - */ |
|
| 58 | - public function asExplicit($expectedTag = null) |
|
| 59 | - { |
|
| 60 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
| 61 | - } |
|
| 51 | + /** |
|
| 52 | + * Get the wrapped inner element employing explicit tagging. |
|
| 53 | + * |
|
| 54 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 55 | + * @throws \UnexpectedValueException If expectation fails |
|
| 56 | + * @return UnspecifiedType |
|
| 57 | + */ |
|
| 58 | + public function asExplicit($expectedTag = null) |
|
| 59 | + { |
|
| 60 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Check whether element supports implicit tagging. |
|
| 65 | - * |
|
| 66 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 67 | - * @throws \UnexpectedValueException If expectation fails |
|
| 68 | - * @return ImplicitTagging |
|
| 69 | - */ |
|
| 70 | - public function expectImplicit($expectedTag = null) |
|
| 71 | - { |
|
| 72 | - $el = $this; |
|
| 73 | - if (!$el instanceof ImplicitTagging) { |
|
| 74 | - throw new \UnexpectedValueException( |
|
| 75 | - "Element doesn't implement implicit tagging."); |
|
| 76 | - } |
|
| 77 | - if (isset($expectedTag)) { |
|
| 78 | - $el->expectTagged($expectedTag); |
|
| 79 | - } |
|
| 80 | - return $el; |
|
| 81 | - } |
|
| 63 | + /** |
|
| 64 | + * Check whether element supports implicit tagging. |
|
| 65 | + * |
|
| 66 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 67 | + * @throws \UnexpectedValueException If expectation fails |
|
| 68 | + * @return ImplicitTagging |
|
| 69 | + */ |
|
| 70 | + public function expectImplicit($expectedTag = null) |
|
| 71 | + { |
|
| 72 | + $el = $this; |
|
| 73 | + if (!$el instanceof ImplicitTagging) { |
|
| 74 | + throw new \UnexpectedValueException( |
|
| 75 | + "Element doesn't implement implicit tagging."); |
|
| 76 | + } |
|
| 77 | + if (isset($expectedTag)) { |
|
| 78 | + $el->expectTagged($expectedTag); |
|
| 79 | + } |
|
| 80 | + return $el; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Get the wrapped inner element employing implicit tagging. |
|
| 85 | - * |
|
| 86 | - * @param int $tag Type tag of the inner element |
|
| 87 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 88 | - * @param int $expectedClass Optional inner type class expectation |
|
| 89 | - * @throws \UnexpectedValueException If expectation fails |
|
| 90 | - * @return UnspecifiedType |
|
| 91 | - */ |
|
| 92 | - public function asImplicit($tag, $expectedTag = null, |
|
| 93 | - $expectedClass = Identifier::CLASS_UNIVERSAL) |
|
| 94 | - { |
|
| 95 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 96 | - $expectedClass); |
|
| 97 | - } |
|
| 83 | + /** |
|
| 84 | + * Get the wrapped inner element employing implicit tagging. |
|
| 85 | + * |
|
| 86 | + * @param int $tag Type tag of the inner element |
|
| 87 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 88 | + * @param int $expectedClass Optional inner type class expectation |
|
| 89 | + * @throws \UnexpectedValueException If expectation fails |
|
| 90 | + * @return UnspecifiedType |
|
| 91 | + */ |
|
| 92 | + public function asImplicit($tag, $expectedTag = null, |
|
| 93 | + $expectedClass = Identifier::CLASS_UNIVERSAL) |
|
| 94 | + { |
|
| 95 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 96 | + $expectedClass); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -12,82 +12,82 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class DERData extends Element |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * DER encoded data. |
|
| 17 | - * |
|
| 18 | - * @var string $_der |
|
| 19 | - */ |
|
| 20 | - protected $_der; |
|
| 15 | + /** |
|
| 16 | + * DER encoded data. |
|
| 17 | + * |
|
| 18 | + * @var string $_der |
|
| 19 | + */ |
|
| 20 | + protected $_der; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Identifier of the underlying type. |
|
| 24 | - * |
|
| 25 | - * @var Identifier $_identifier |
|
| 26 | - */ |
|
| 27 | - protected $_identifier; |
|
| 22 | + /** |
|
| 23 | + * Identifier of the underlying type. |
|
| 24 | + * |
|
| 25 | + * @var Identifier $_identifier |
|
| 26 | + */ |
|
| 27 | + protected $_identifier; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Offset to the content in DER data. |
|
| 31 | - * |
|
| 32 | - * @var int $_contentOffset |
|
| 33 | - */ |
|
| 34 | - protected $_contentOffset = 0; |
|
| 29 | + /** |
|
| 30 | + * Offset to the content in DER data. |
|
| 31 | + * |
|
| 32 | + * @var int $_contentOffset |
|
| 33 | + */ |
|
| 34 | + protected $_contentOffset = 0; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Constructor. |
|
| 38 | - * |
|
| 39 | - * @param string $data DER encoded data |
|
| 40 | - * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 41 | - */ |
|
| 42 | - public function __construct($data) |
|
| 43 | - { |
|
| 44 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 45 | - Length::expectFromDER($data, $this->_contentOffset); |
|
| 46 | - $this->_der = $data; |
|
| 47 | - $this->_typeTag = intval($this->_identifier->tag()); |
|
| 48 | - } |
|
| 36 | + /** |
|
| 37 | + * Constructor. |
|
| 38 | + * |
|
| 39 | + * @param string $data DER encoded data |
|
| 40 | + * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 41 | + */ |
|
| 42 | + public function __construct($data) |
|
| 43 | + { |
|
| 44 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 45 | + Length::expectFromDER($data, $this->_contentOffset); |
|
| 46 | + $this->_der = $data; |
|
| 47 | + $this->_typeTag = intval($this->_identifier->tag()); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * |
|
| 52 | - * @see \ASN1\Element::typeClass() |
|
| 53 | - * @return int |
|
| 54 | - */ |
|
| 55 | - public function typeClass() |
|
| 56 | - { |
|
| 57 | - return $this->_identifier->typeClass(); |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * |
|
| 52 | + * @see \ASN1\Element::typeClass() |
|
| 53 | + * @return int |
|
| 54 | + */ |
|
| 55 | + public function typeClass() |
|
| 56 | + { |
|
| 57 | + return $this->_identifier->typeClass(); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * |
|
| 62 | - * @see \ASN1\Element::isConstructed() |
|
| 63 | - * @return bool |
|
| 64 | - */ |
|
| 65 | - public function isConstructed() |
|
| 66 | - { |
|
| 67 | - return $this->_identifier->isConstructed(); |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * |
|
| 62 | + * @see \ASN1\Element::isConstructed() |
|
| 63 | + * @return bool |
|
| 64 | + */ |
|
| 65 | + public function isConstructed() |
|
| 66 | + { |
|
| 67 | + return $this->_identifier->isConstructed(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * |
|
| 72 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - protected function _encodedContentDER() |
|
| 76 | - { |
|
| 77 | - // if there's no content payload |
|
| 78 | - if (strlen($this->_der) == $this->_contentOffset) { |
|
| 79 | - return ""; |
|
| 80 | - } |
|
| 81 | - return substr($this->_der, $this->_contentOffset); |
|
| 82 | - } |
|
| 70 | + /** |
|
| 71 | + * |
|
| 72 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + protected function _encodedContentDER() |
|
| 76 | + { |
|
| 77 | + // if there's no content payload |
|
| 78 | + if (strlen($this->_der) == $this->_contentOffset) { |
|
| 79 | + return ""; |
|
| 80 | + } |
|
| 81 | + return substr($this->_der, $this->_contentOffset); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * |
|
| 86 | - * @see \ASN1\Element::toDER() |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - public function toDER() |
|
| 90 | - { |
|
| 91 | - return $this->_der; |
|
| 92 | - } |
|
| 84 | + /** |
|
| 85 | + * |
|
| 86 | + * @see \ASN1\Element::toDER() |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + public function toDER() |
|
| 90 | + { |
|
| 91 | + return $this->_der; |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -18,440 +18,440 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class Element implements ElementBase |
| 20 | 20 | { |
| 21 | - // Universal type tags |
|
| 22 | - const TYPE_EOC = 0x00; |
|
| 23 | - const TYPE_BOOLEAN = 0x01; |
|
| 24 | - const TYPE_INTEGER = 0x02; |
|
| 25 | - const TYPE_BIT_STRING = 0x03; |
|
| 26 | - const TYPE_OCTET_STRING = 0x04; |
|
| 27 | - const TYPE_NULL = 0x05; |
|
| 28 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 29 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 30 | - const TYPE_EXTERNAL = 0x08; |
|
| 31 | - const TYPE_REAL = 0x09; |
|
| 32 | - const TYPE_ENUMERATED = 0x0a; |
|
| 33 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 34 | - const TYPE_UTF8_STRING = 0x0c; |
|
| 35 | - const TYPE_RELATIVE_OID = 0x0d; |
|
| 36 | - const TYPE_SEQUENCE = 0x10; |
|
| 37 | - const TYPE_SET = 0x11; |
|
| 38 | - const TYPE_NUMERIC_STRING = 0x12; |
|
| 39 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
| 40 | - const TYPE_T61_STRING = 0x14; |
|
| 41 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 42 | - const TYPE_IA5_STRING = 0x16; |
|
| 43 | - const TYPE_UTC_TIME = 0x17; |
|
| 44 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
| 45 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
| 46 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
| 47 | - const TYPE_GENERAL_STRING = 0x1b; |
|
| 48 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 49 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
| 50 | - const TYPE_BMP_STRING = 0x1e; |
|
| 21 | + // Universal type tags |
|
| 22 | + const TYPE_EOC = 0x00; |
|
| 23 | + const TYPE_BOOLEAN = 0x01; |
|
| 24 | + const TYPE_INTEGER = 0x02; |
|
| 25 | + const TYPE_BIT_STRING = 0x03; |
|
| 26 | + const TYPE_OCTET_STRING = 0x04; |
|
| 27 | + const TYPE_NULL = 0x05; |
|
| 28 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 29 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 30 | + const TYPE_EXTERNAL = 0x08; |
|
| 31 | + const TYPE_REAL = 0x09; |
|
| 32 | + const TYPE_ENUMERATED = 0x0a; |
|
| 33 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 34 | + const TYPE_UTF8_STRING = 0x0c; |
|
| 35 | + const TYPE_RELATIVE_OID = 0x0d; |
|
| 36 | + const TYPE_SEQUENCE = 0x10; |
|
| 37 | + const TYPE_SET = 0x11; |
|
| 38 | + const TYPE_NUMERIC_STRING = 0x12; |
|
| 39 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
| 40 | + const TYPE_T61_STRING = 0x14; |
|
| 41 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 42 | + const TYPE_IA5_STRING = 0x16; |
|
| 43 | + const TYPE_UTC_TIME = 0x17; |
|
| 44 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
| 45 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
| 46 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
| 47 | + const TYPE_GENERAL_STRING = 0x1b; |
|
| 48 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 49 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
| 50 | + const TYPE_BMP_STRING = 0x1e; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Mapping from universal type tag to implementation class name. |
|
| 54 | - * |
|
| 55 | - * @internal |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - const MAP_TAG_TO_CLASS = array( |
|
| 60 | - /* @formatter:off */ |
|
| 61 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 62 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 63 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 64 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 65 | - self::TYPE_NULL => Primitive\NullType::class, |
|
| 66 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 67 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 68 | - self::TYPE_REAL => Primitive\Real::class, |
|
| 69 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 70 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 71 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 72 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 73 | - self::TYPE_SET => Constructed\Set::class, |
|
| 74 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 75 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 76 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 77 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 78 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 79 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 80 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 81 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 82 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 83 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 84 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 85 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 86 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 87 | - /* @formatter:on */ |
|
| 88 | - ); |
|
| 52 | + /** |
|
| 53 | + * Mapping from universal type tag to implementation class name. |
|
| 54 | + * |
|
| 55 | + * @internal |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + const MAP_TAG_TO_CLASS = array( |
|
| 60 | + /* @formatter:off */ |
|
| 61 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 62 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 63 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 64 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 65 | + self::TYPE_NULL => Primitive\NullType::class, |
|
| 66 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 67 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 68 | + self::TYPE_REAL => Primitive\Real::class, |
|
| 69 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 70 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 71 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 72 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 73 | + self::TYPE_SET => Constructed\Set::class, |
|
| 74 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 75 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 76 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 77 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 78 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 79 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 80 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 81 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 82 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 83 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 84 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 85 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 86 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 87 | + /* @formatter:on */ |
|
| 88 | + ); |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Pseudotype for all string types. |
|
| 92 | - * |
|
| 93 | - * May be used as an expectation parameter. |
|
| 94 | - * |
|
| 95 | - * @var int |
|
| 96 | - */ |
|
| 97 | - const TYPE_STRING = -1; |
|
| 90 | + /** |
|
| 91 | + * Pseudotype for all string types. |
|
| 92 | + * |
|
| 93 | + * May be used as an expectation parameter. |
|
| 94 | + * |
|
| 95 | + * @var int |
|
| 96 | + */ |
|
| 97 | + const TYPE_STRING = -1; |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Pseudotype for all time types. |
|
| 101 | - * |
|
| 102 | - * May be used as an expectation parameter. |
|
| 103 | - * |
|
| 104 | - * @var int |
|
| 105 | - */ |
|
| 106 | - const TYPE_TIME = -2; |
|
| 99 | + /** |
|
| 100 | + * Pseudotype for all time types. |
|
| 101 | + * |
|
| 102 | + * May be used as an expectation parameter. |
|
| 103 | + * |
|
| 104 | + * @var int |
|
| 105 | + */ |
|
| 106 | + const TYPE_TIME = -2; |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Mapping from universal type tag to human readable name. |
|
| 110 | - * |
|
| 111 | - * @internal |
|
| 112 | - * |
|
| 113 | - * @var array |
|
| 114 | - */ |
|
| 115 | - const MAP_TYPE_TO_NAME = array( |
|
| 116 | - /* @formatter:off */ |
|
| 117 | - self::TYPE_EOC => "EOC", |
|
| 118 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 119 | - self::TYPE_INTEGER => "INTEGER", |
|
| 120 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
| 121 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 122 | - self::TYPE_NULL => "NULL", |
|
| 123 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 124 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 125 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 126 | - self::TYPE_REAL => "REAL", |
|
| 127 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 128 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 129 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
| 130 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 131 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 132 | - self::TYPE_SET => "SET", |
|
| 133 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 134 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 135 | - self::TYPE_T61_STRING => "T61String", |
|
| 136 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 137 | - self::TYPE_IA5_STRING => "IA5String", |
|
| 138 | - self::TYPE_UTC_TIME => "UTCTime", |
|
| 139 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 140 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 141 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 142 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 143 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 144 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 145 | - self::TYPE_BMP_STRING => "BMPString", |
|
| 146 | - self::TYPE_STRING => "Any String", |
|
| 147 | - self::TYPE_TIME => "Any Time" |
|
| 148 | - /* @formatter:on */ |
|
| 149 | - ); |
|
| 108 | + /** |
|
| 109 | + * Mapping from universal type tag to human readable name. |
|
| 110 | + * |
|
| 111 | + * @internal |
|
| 112 | + * |
|
| 113 | + * @var array |
|
| 114 | + */ |
|
| 115 | + const MAP_TYPE_TO_NAME = array( |
|
| 116 | + /* @formatter:off */ |
|
| 117 | + self::TYPE_EOC => "EOC", |
|
| 118 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 119 | + self::TYPE_INTEGER => "INTEGER", |
|
| 120 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
| 121 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 122 | + self::TYPE_NULL => "NULL", |
|
| 123 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 124 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 125 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 126 | + self::TYPE_REAL => "REAL", |
|
| 127 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 128 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 129 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
| 130 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 131 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 132 | + self::TYPE_SET => "SET", |
|
| 133 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 134 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 135 | + self::TYPE_T61_STRING => "T61String", |
|
| 136 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 137 | + self::TYPE_IA5_STRING => "IA5String", |
|
| 138 | + self::TYPE_UTC_TIME => "UTCTime", |
|
| 139 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 140 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 141 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 142 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 143 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 144 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 145 | + self::TYPE_BMP_STRING => "BMPString", |
|
| 146 | + self::TYPE_STRING => "Any String", |
|
| 147 | + self::TYPE_TIME => "Any Time" |
|
| 148 | + /* @formatter:on */ |
|
| 149 | + ); |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Element's type tag. |
|
| 153 | - * |
|
| 154 | - * @var int |
|
| 155 | - */ |
|
| 156 | - protected $_typeTag; |
|
| 151 | + /** |
|
| 152 | + * Element's type tag. |
|
| 153 | + * |
|
| 154 | + * @var int |
|
| 155 | + */ |
|
| 156 | + protected $_typeTag; |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * |
|
| 160 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 161 | - * @return int |
|
| 162 | - */ |
|
| 163 | - abstract public function typeClass(); |
|
| 158 | + /** |
|
| 159 | + * |
|
| 160 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 161 | + * @return int |
|
| 162 | + */ |
|
| 163 | + abstract public function typeClass(); |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * |
|
| 167 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 168 | - * @return bool |
|
| 169 | - */ |
|
| 170 | - abstract public function isConstructed(); |
|
| 165 | + /** |
|
| 166 | + * |
|
| 167 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 168 | + * @return bool |
|
| 169 | + */ |
|
| 170 | + abstract public function isConstructed(); |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Get the content encoded in DER. |
|
| 174 | - * |
|
| 175 | - * Returns the DER encoded content without identifier and length header |
|
| 176 | - * octets. |
|
| 177 | - * |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - abstract protected function _encodedContentDER(); |
|
| 172 | + /** |
|
| 173 | + * Get the content encoded in DER. |
|
| 174 | + * |
|
| 175 | + * Returns the DER encoded content without identifier and length header |
|
| 176 | + * octets. |
|
| 177 | + * |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + abstract protected function _encodedContentDER(); |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Decode type-specific element from DER. |
|
| 184 | - * |
|
| 185 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 186 | - * @param string $data DER data |
|
| 187 | - * @param int $offset Offset in data to the next byte after identifier |
|
| 188 | - * @throws DecodeException If decoding fails |
|
| 189 | - * @return Element |
|
| 190 | - */ |
|
| 191 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 192 | - &$offset) |
|
| 193 | - { |
|
| 194 | - throw new \BadMethodCallException( |
|
| 195 | - __METHOD__ . " must be implemented in derived class."); |
|
| 196 | - } |
|
| 182 | + /** |
|
| 183 | + * Decode type-specific element from DER. |
|
| 184 | + * |
|
| 185 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 186 | + * @param string $data DER data |
|
| 187 | + * @param int $offset Offset in data to the next byte after identifier |
|
| 188 | + * @throws DecodeException If decoding fails |
|
| 189 | + * @return Element |
|
| 190 | + */ |
|
| 191 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 192 | + &$offset) |
|
| 193 | + { |
|
| 194 | + throw new \BadMethodCallException( |
|
| 195 | + __METHOD__ . " must be implemented in derived class."); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Decode element from DER data. |
|
| 200 | - * |
|
| 201 | - * @param string $data DER encoded data |
|
| 202 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 203 | - * into the data where to start parsing. Variable is updated to |
|
| 204 | - * the offset next to the parsed element. If null, start from offset |
|
| 205 | - * 0. |
|
| 206 | - * @throws DecodeException If decoding fails |
|
| 207 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
| 208 | - * type, but decoding yields another type |
|
| 209 | - * @return self |
|
| 210 | - */ |
|
| 211 | - public static function fromDER($data, &$offset = null) |
|
| 212 | - { |
|
| 213 | - assert('is_string($data)', "got " . gettype($data)); |
|
| 214 | - // decode identifier |
|
| 215 | - $idx = $offset ? $offset : 0; |
|
| 216 | - $identifier = Identifier::fromDER($data, $idx); |
|
| 217 | - // determine class that implements type specific decoding |
|
| 218 | - $cls = self::_determineImplClass($identifier); |
|
| 219 | - try { |
|
| 220 | - // decode remaining element |
|
| 221 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 222 | - } catch (\LogicException $e) { |
|
| 223 | - // rethrow as a RuntimeException for unified exception handling |
|
| 224 | - throw new DecodeException( |
|
| 225 | - sprintf("Error while decoding %s.", |
|
| 226 | - self::tagToName($identifier->tag())), 0, $e); |
|
| 227 | - } |
|
| 228 | - // if called in the context of a concrete class, check |
|
| 229 | - // that decoded type matches the type of a calling class |
|
| 230 | - $called_class = get_called_class(); |
|
| 231 | - if (__CLASS__ != $called_class) { |
|
| 232 | - if (!$element instanceof $called_class) { |
|
| 233 | - throw new \UnexpectedValueException( |
|
| 234 | - sprintf("%s expected, got %s.", $called_class, |
|
| 235 | - get_class($element))); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - // update offset for the caller |
|
| 239 | - if (isset($offset)) { |
|
| 240 | - $offset = $idx; |
|
| 241 | - } |
|
| 242 | - return $element; |
|
| 243 | - } |
|
| 198 | + /** |
|
| 199 | + * Decode element from DER data. |
|
| 200 | + * |
|
| 201 | + * @param string $data DER encoded data |
|
| 202 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 203 | + * into the data where to start parsing. Variable is updated to |
|
| 204 | + * the offset next to the parsed element. If null, start from offset |
|
| 205 | + * 0. |
|
| 206 | + * @throws DecodeException If decoding fails |
|
| 207 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
| 208 | + * type, but decoding yields another type |
|
| 209 | + * @return self |
|
| 210 | + */ |
|
| 211 | + public static function fromDER($data, &$offset = null) |
|
| 212 | + { |
|
| 213 | + assert('is_string($data)', "got " . gettype($data)); |
|
| 214 | + // decode identifier |
|
| 215 | + $idx = $offset ? $offset : 0; |
|
| 216 | + $identifier = Identifier::fromDER($data, $idx); |
|
| 217 | + // determine class that implements type specific decoding |
|
| 218 | + $cls = self::_determineImplClass($identifier); |
|
| 219 | + try { |
|
| 220 | + // decode remaining element |
|
| 221 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 222 | + } catch (\LogicException $e) { |
|
| 223 | + // rethrow as a RuntimeException for unified exception handling |
|
| 224 | + throw new DecodeException( |
|
| 225 | + sprintf("Error while decoding %s.", |
|
| 226 | + self::tagToName($identifier->tag())), 0, $e); |
|
| 227 | + } |
|
| 228 | + // if called in the context of a concrete class, check |
|
| 229 | + // that decoded type matches the type of a calling class |
|
| 230 | + $called_class = get_called_class(); |
|
| 231 | + if (__CLASS__ != $called_class) { |
|
| 232 | + if (!$element instanceof $called_class) { |
|
| 233 | + throw new \UnexpectedValueException( |
|
| 234 | + sprintf("%s expected, got %s.", $called_class, |
|
| 235 | + get_class($element))); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + // update offset for the caller |
|
| 239 | + if (isset($offset)) { |
|
| 240 | + $offset = $idx; |
|
| 241 | + } |
|
| 242 | + return $element; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * |
|
| 247 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public function toDER() |
|
| 251 | - { |
|
| 252 | - $identifier = new Identifier($this->typeClass(), |
|
| 253 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 254 | - $this->_typeTag); |
|
| 255 | - $content = $this->_encodedContentDER(); |
|
| 256 | - $length = new Length(strlen($content)); |
|
| 257 | - return $identifier->toDER() . $length->toDER() . $content; |
|
| 258 | - } |
|
| 245 | + /** |
|
| 246 | + * |
|
| 247 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public function toDER() |
|
| 251 | + { |
|
| 252 | + $identifier = new Identifier($this->typeClass(), |
|
| 253 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 254 | + $this->_typeTag); |
|
| 255 | + $content = $this->_encodedContentDER(); |
|
| 256 | + $length = new Length(strlen($content)); |
|
| 257 | + return $identifier->toDER() . $length->toDER() . $content; |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * |
|
| 262 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 263 | - * @return int |
|
| 264 | - */ |
|
| 265 | - public function tag() |
|
| 266 | - { |
|
| 267 | - return $this->_typeTag; |
|
| 268 | - } |
|
| 260 | + /** |
|
| 261 | + * |
|
| 262 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 263 | + * @return int |
|
| 264 | + */ |
|
| 265 | + public function tag() |
|
| 266 | + { |
|
| 267 | + return $this->_typeTag; |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * |
|
| 272 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 273 | - * @return bool |
|
| 274 | - */ |
|
| 275 | - public function isType($tag) |
|
| 276 | - { |
|
| 277 | - // if element is context specific |
|
| 278 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 279 | - return false; |
|
| 280 | - } |
|
| 281 | - // negative tags identify an abstract pseudotype |
|
| 282 | - if ($tag < 0) { |
|
| 283 | - return $this->_isPseudoType($tag); |
|
| 284 | - } |
|
| 285 | - return $this->_isConcreteType($tag); |
|
| 286 | - } |
|
| 270 | + /** |
|
| 271 | + * |
|
| 272 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 273 | + * @return bool |
|
| 274 | + */ |
|
| 275 | + public function isType($tag) |
|
| 276 | + { |
|
| 277 | + // if element is context specific |
|
| 278 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 279 | + return false; |
|
| 280 | + } |
|
| 281 | + // negative tags identify an abstract pseudotype |
|
| 282 | + if ($tag < 0) { |
|
| 283 | + return $this->_isPseudoType($tag); |
|
| 284 | + } |
|
| 285 | + return $this->_isConcreteType($tag); |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - /** |
|
| 289 | - * |
|
| 290 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 291 | - * @return ElementBase |
|
| 292 | - */ |
|
| 293 | - public function expectType($tag) |
|
| 294 | - { |
|
| 295 | - if (!$this->isType($tag)) { |
|
| 296 | - throw new \UnexpectedValueException( |
|
| 297 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 298 | - $this->_typeDescriptorString())); |
|
| 299 | - } |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 288 | + /** |
|
| 289 | + * |
|
| 290 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 291 | + * @return ElementBase |
|
| 292 | + */ |
|
| 293 | + public function expectType($tag) |
|
| 294 | + { |
|
| 295 | + if (!$this->isType($tag)) { |
|
| 296 | + throw new \UnexpectedValueException( |
|
| 297 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 298 | + $this->_typeDescriptorString())); |
|
| 299 | + } |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Check whether the element is a concrete type of a given tag. |
|
| 305 | - * |
|
| 306 | - * @param int $tag |
|
| 307 | - * @return bool |
|
| 308 | - */ |
|
| 309 | - private function _isConcreteType($tag) |
|
| 310 | - { |
|
| 311 | - // if tag doesn't match |
|
| 312 | - if ($this->tag() != $tag) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - // if type is universal check that instance is of a correct class |
|
| 316 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 317 | - $cls = self::_determineUniversalImplClass($tag); |
|
| 318 | - if (!$this instanceof $cls) { |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - return true; |
|
| 323 | - } |
|
| 303 | + /** |
|
| 304 | + * Check whether the element is a concrete type of a given tag. |
|
| 305 | + * |
|
| 306 | + * @param int $tag |
|
| 307 | + * @return bool |
|
| 308 | + */ |
|
| 309 | + private function _isConcreteType($tag) |
|
| 310 | + { |
|
| 311 | + // if tag doesn't match |
|
| 312 | + if ($this->tag() != $tag) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + // if type is universal check that instance is of a correct class |
|
| 316 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 317 | + $cls = self::_determineUniversalImplClass($tag); |
|
| 318 | + if (!$this instanceof $cls) { |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + return true; |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** |
|
| 326 | - * Check whether the element is a pseudotype. |
|
| 327 | - * |
|
| 328 | - * @param int $tag |
|
| 329 | - * @return bool |
|
| 330 | - */ |
|
| 331 | - private function _isPseudoType($tag) |
|
| 332 | - { |
|
| 333 | - switch ($tag) { |
|
| 334 | - case self::TYPE_STRING: |
|
| 335 | - return $this instanceof StringType; |
|
| 336 | - case self::TYPE_TIME: |
|
| 337 | - return $this instanceof TimeType; |
|
| 338 | - } |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 325 | + /** |
|
| 326 | + * Check whether the element is a pseudotype. |
|
| 327 | + * |
|
| 328 | + * @param int $tag |
|
| 329 | + * @return bool |
|
| 330 | + */ |
|
| 331 | + private function _isPseudoType($tag) |
|
| 332 | + { |
|
| 333 | + switch ($tag) { |
|
| 334 | + case self::TYPE_STRING: |
|
| 335 | + return $this instanceof StringType; |
|
| 336 | + case self::TYPE_TIME: |
|
| 337 | + return $this instanceof TimeType; |
|
| 338 | + } |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | 341 | |
| 342 | - /** |
|
| 343 | - * |
|
| 344 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 345 | - * @return bool |
|
| 346 | - */ |
|
| 347 | - public function isTagged() |
|
| 348 | - { |
|
| 349 | - return $this instanceof TaggedType; |
|
| 350 | - } |
|
| 342 | + /** |
|
| 343 | + * |
|
| 344 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 345 | + * @return bool |
|
| 346 | + */ |
|
| 347 | + public function isTagged() |
|
| 348 | + { |
|
| 349 | + return $this instanceof TaggedType; |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - /** |
|
| 353 | - * |
|
| 354 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 355 | - * @return TaggedType |
|
| 356 | - */ |
|
| 357 | - public function expectTagged($tag = null) |
|
| 358 | - { |
|
| 359 | - if (!$this->isTagged()) { |
|
| 360 | - throw new \UnexpectedValueException( |
|
| 361 | - sprintf("Context specific element expected, got %s.", |
|
| 362 | - Identifier::classToName($this->typeClass()))); |
|
| 363 | - } |
|
| 364 | - if (isset($tag) && $this->tag() != $tag) { |
|
| 365 | - throw new \UnexpectedValueException( |
|
| 366 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 367 | - } |
|
| 368 | - return $this; |
|
| 369 | - } |
|
| 352 | + /** |
|
| 353 | + * |
|
| 354 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 355 | + * @return TaggedType |
|
| 356 | + */ |
|
| 357 | + public function expectTagged($tag = null) |
|
| 358 | + { |
|
| 359 | + if (!$this->isTagged()) { |
|
| 360 | + throw new \UnexpectedValueException( |
|
| 361 | + sprintf("Context specific element expected, got %s.", |
|
| 362 | + Identifier::classToName($this->typeClass()))); |
|
| 363 | + } |
|
| 364 | + if (isset($tag) && $this->tag() != $tag) { |
|
| 365 | + throw new \UnexpectedValueException( |
|
| 366 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 367 | + } |
|
| 368 | + return $this; |
|
| 369 | + } |
|
| 370 | 370 | |
| 371 | - /** |
|
| 372 | - * |
|
| 373 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 374 | - * @return Element |
|
| 375 | - */ |
|
| 376 | - final public function asElement() |
|
| 377 | - { |
|
| 378 | - return $this; |
|
| 379 | - } |
|
| 371 | + /** |
|
| 372 | + * |
|
| 373 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 374 | + * @return Element |
|
| 375 | + */ |
|
| 376 | + final public function asElement() |
|
| 377 | + { |
|
| 378 | + return $this; |
|
| 379 | + } |
|
| 380 | 380 | |
| 381 | - /** |
|
| 382 | - * Get element decorated with UnspecifiedType object. |
|
| 383 | - * |
|
| 384 | - * @return UnspecifiedType |
|
| 385 | - */ |
|
| 386 | - public function asUnspecified() |
|
| 387 | - { |
|
| 388 | - return new UnspecifiedType($this); |
|
| 389 | - } |
|
| 381 | + /** |
|
| 382 | + * Get element decorated with UnspecifiedType object. |
|
| 383 | + * |
|
| 384 | + * @return UnspecifiedType |
|
| 385 | + */ |
|
| 386 | + public function asUnspecified() |
|
| 387 | + { |
|
| 388 | + return new UnspecifiedType($this); |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * Determine the class that implements the type. |
|
| 393 | - * |
|
| 394 | - * @param Identifier $identifier |
|
| 395 | - * @return string Class name |
|
| 396 | - */ |
|
| 397 | - protected static function _determineImplClass(Identifier $identifier) |
|
| 398 | - { |
|
| 399 | - // tagged type |
|
| 400 | - if ($identifier->isContextSpecific()) { |
|
| 401 | - return TaggedType::class; |
|
| 402 | - } |
|
| 403 | - // universal class |
|
| 404 | - if ($identifier->isUniversal()) { |
|
| 405 | - return self::_determineUniversalImplClass( |
|
| 406 | - intval($identifier->tag())); |
|
| 407 | - } |
|
| 408 | - throw new \UnexpectedValueException( |
|
| 409 | - sprintf("%s %d not implemented.", |
|
| 410 | - Identifier::classToName($identifier->typeClass()), |
|
| 411 | - $identifier->tag())); |
|
| 412 | - } |
|
| 391 | + /** |
|
| 392 | + * Determine the class that implements the type. |
|
| 393 | + * |
|
| 394 | + * @param Identifier $identifier |
|
| 395 | + * @return string Class name |
|
| 396 | + */ |
|
| 397 | + protected static function _determineImplClass(Identifier $identifier) |
|
| 398 | + { |
|
| 399 | + // tagged type |
|
| 400 | + if ($identifier->isContextSpecific()) { |
|
| 401 | + return TaggedType::class; |
|
| 402 | + } |
|
| 403 | + // universal class |
|
| 404 | + if ($identifier->isUniversal()) { |
|
| 405 | + return self::_determineUniversalImplClass( |
|
| 406 | + intval($identifier->tag())); |
|
| 407 | + } |
|
| 408 | + throw new \UnexpectedValueException( |
|
| 409 | + sprintf("%s %d not implemented.", |
|
| 410 | + Identifier::classToName($identifier->typeClass()), |
|
| 411 | + $identifier->tag())); |
|
| 412 | + } |
|
| 413 | 413 | |
| 414 | - /** |
|
| 415 | - * Determine the class that implements an universal type of the given tag. |
|
| 416 | - * |
|
| 417 | - * @param int $tag |
|
| 418 | - * @throws \UnexpectedValueException |
|
| 419 | - * @return string Class name |
|
| 420 | - */ |
|
| 421 | - protected static function _determineUniversalImplClass($tag) |
|
| 422 | - { |
|
| 423 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 424 | - throw new \UnexpectedValueException( |
|
| 425 | - "Universal tag $tag not implemented."); |
|
| 426 | - } |
|
| 427 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 428 | - } |
|
| 414 | + /** |
|
| 415 | + * Determine the class that implements an universal type of the given tag. |
|
| 416 | + * |
|
| 417 | + * @param int $tag |
|
| 418 | + * @throws \UnexpectedValueException |
|
| 419 | + * @return string Class name |
|
| 420 | + */ |
|
| 421 | + protected static function _determineUniversalImplClass($tag) |
|
| 422 | + { |
|
| 423 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 424 | + throw new \UnexpectedValueException( |
|
| 425 | + "Universal tag $tag not implemented."); |
|
| 426 | + } |
|
| 427 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | - /** |
|
| 431 | - * Get textual description of the type for debugging purposes. |
|
| 432 | - * |
|
| 433 | - * @return string |
|
| 434 | - */ |
|
| 435 | - protected function _typeDescriptorString() |
|
| 436 | - { |
|
| 437 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 438 | - return self::tagToName($this->_typeTag); |
|
| 439 | - } |
|
| 440 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 441 | - $this->_typeTag); |
|
| 442 | - } |
|
| 430 | + /** |
|
| 431 | + * Get textual description of the type for debugging purposes. |
|
| 432 | + * |
|
| 433 | + * @return string |
|
| 434 | + */ |
|
| 435 | + protected function _typeDescriptorString() |
|
| 436 | + { |
|
| 437 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 438 | + return self::tagToName($this->_typeTag); |
|
| 439 | + } |
|
| 440 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 441 | + $this->_typeTag); |
|
| 442 | + } |
|
| 443 | 443 | |
| 444 | - /** |
|
| 445 | - * Get human readable name for an universal tag. |
|
| 446 | - * |
|
| 447 | - * @param int $tag |
|
| 448 | - * @return string |
|
| 449 | - */ |
|
| 450 | - public static function tagToName($tag) |
|
| 451 | - { |
|
| 452 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 453 | - return "TAG $tag"; |
|
| 454 | - } |
|
| 455 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 456 | - } |
|
| 444 | + /** |
|
| 445 | + * Get human readable name for an universal tag. |
|
| 446 | + * |
|
| 447 | + * @param int $tag |
|
| 448 | + * @return string |
|
| 449 | + */ |
|
| 450 | + public static function tagToName($tag) |
|
| 451 | + { |
|
| 452 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 453 | + return "TAG $tag"; |
|
| 454 | + } |
|
| 455 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 456 | + } |
|
| 457 | 457 | } |