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