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