@@ -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 | } |
@@ -179,7 +179,7 @@ |
||
| 179 | 179 | * @return self |
| 180 | 180 | */ |
| 181 | 181 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 182 | - int &$offset): ElementBase |
|
| 182 | + int & $offset): ElementBase |
|
| 183 | 183 | { |
| 184 | 184 | $idx = $offset; |
| 185 | 185 | $length = Length::expectFromDER($data, $idx); |
@@ -17,112 +17,112 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class GeneralizedTime extends TimeType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regular expression to parse date. |
|
| 25 | - * |
|
| 26 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 31 | - '(\d\d\d\d)' . /* YYYY */ |
|
| 32 | - '(\d\d)' . /* MM */ |
|
| 33 | - '(\d\d)' . /* DD */ |
|
| 34 | - '(\d\d)' . /* hh */ |
|
| 35 | - '(\d\d)' . /* mm */ |
|
| 36 | - '(\d\d)' . /* ss */ |
|
| 37 | - '(?:\.(\d+))?' . /* frac */ |
|
| 38 | - 'Z' . /* TZ */ |
|
| 39 | - '$#' /* @formatter:on */; |
|
| 23 | + /** |
|
| 24 | + * Regular expression to parse date. |
|
| 25 | + * |
|
| 26 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 31 | + '(\d\d\d\d)' . /* YYYY */ |
|
| 32 | + '(\d\d)' . /* MM */ |
|
| 33 | + '(\d\d)' . /* DD */ |
|
| 34 | + '(\d\d)' . /* hh */ |
|
| 35 | + '(\d\d)' . /* mm */ |
|
| 36 | + '(\d\d)' . /* ss */ |
|
| 37 | + '(?:\.(\d+))?' . /* frac */ |
|
| 38 | + 'Z' . /* TZ */ |
|
| 39 | + '$#' /* @formatter:on */; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Cached formatted date. |
|
| 43 | - * |
|
| 44 | - * @var string|null |
|
| 45 | - */ |
|
| 46 | - private $_formatted; |
|
| 41 | + /** |
|
| 42 | + * Cached formatted date. |
|
| 43 | + * |
|
| 44 | + * @var string|null |
|
| 45 | + */ |
|
| 46 | + private $_formatted; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Constructor. |
|
| 50 | - * |
|
| 51 | - * @param \DateTimeImmutable $dt |
|
| 52 | - */ |
|
| 53 | - public function __construct(\DateTimeImmutable $dt) |
|
| 54 | - { |
|
| 55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | - parent::__construct($dt); |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * Constructor. |
|
| 50 | + * |
|
| 51 | + * @param \DateTimeImmutable $dt |
|
| 52 | + */ |
|
| 53 | + public function __construct(\DateTimeImmutable $dt) |
|
| 54 | + { |
|
| 55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | + parent::__construct($dt); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Clear cached variables on clone. |
|
| 61 | - */ |
|
| 62 | - public function __clone() |
|
| 63 | - { |
|
| 64 | - $this->_formatted = null; |
|
| 65 | - } |
|
| 59 | + /** |
|
| 60 | + * Clear cached variables on clone. |
|
| 61 | + */ |
|
| 62 | + public function __clone() |
|
| 63 | + { |
|
| 64 | + $this->_formatted = null; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * |
|
| 69 | - * {@inheritdoc} |
|
| 70 | - */ |
|
| 71 | - protected function _encodedContentDER(): string |
|
| 72 | - { |
|
| 73 | - if (!isset($this->_formatted)) { |
|
| 74 | - $dt = $this->_dateTime->setTimezone( |
|
| 75 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 76 | - $this->_formatted = $dt->format("YmdHis"); |
|
| 77 | - // if fractions were used |
|
| 78 | - $frac = $dt->format("u"); |
|
| 79 | - if ($frac != 0) { |
|
| 80 | - $frac = rtrim($frac, "0"); |
|
| 81 | - $this->_formatted .= ".$frac"; |
|
| 82 | - } |
|
| 83 | - // timezone |
|
| 84 | - $this->_formatted .= "Z"; |
|
| 85 | - } |
|
| 86 | - return $this->_formatted; |
|
| 87 | - } |
|
| 67 | + /** |
|
| 68 | + * |
|
| 69 | + * {@inheritdoc} |
|
| 70 | + */ |
|
| 71 | + protected function _encodedContentDER(): string |
|
| 72 | + { |
|
| 73 | + if (!isset($this->_formatted)) { |
|
| 74 | + $dt = $this->_dateTime->setTimezone( |
|
| 75 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 76 | + $this->_formatted = $dt->format("YmdHis"); |
|
| 77 | + // if fractions were used |
|
| 78 | + $frac = $dt->format("u"); |
|
| 79 | + if ($frac != 0) { |
|
| 80 | + $frac = rtrim($frac, "0"); |
|
| 81 | + $this->_formatted .= ".$frac"; |
|
| 82 | + } |
|
| 83 | + // timezone |
|
| 84 | + $this->_formatted .= "Z"; |
|
| 85 | + } |
|
| 86 | + return $this->_formatted; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * |
|
| 91 | - * {@inheritdoc} |
|
| 92 | - * @return self |
|
| 93 | - */ |
|
| 94 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 95 | - int &$offset): ElementBase |
|
| 96 | - { |
|
| 97 | - $idx = $offset; |
|
| 98 | - $length = Length::expectFromDER($data, $idx); |
|
| 99 | - $str = substr($data, $idx, (int) $length->length()); |
|
| 100 | - $idx += $length->length(); |
|
| 101 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 102 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 103 | - } |
|
| 104 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 105 | - if (isset($match[7])) { |
|
| 106 | - $frac = $match[7]; |
|
| 107 | - // DER restricts trailing zeroes in fractional seconds component |
|
| 108 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
| 109 | - throw new DecodeException( |
|
| 110 | - "Fractional seconds must omit trailing zeroes."); |
|
| 111 | - } |
|
| 112 | - $frac = (int) $frac; |
|
| 113 | - } else { |
|
| 114 | - $frac = 0; |
|
| 115 | - } |
|
| 116 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 117 | - self::TZ_UTC; |
|
| 118 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 119 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 120 | - if (!$dt) { |
|
| 121 | - throw new DecodeException( |
|
| 122 | - "Failed to decode GeneralizedTime: " . |
|
| 123 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 124 | - } |
|
| 125 | - $offset = $idx; |
|
| 126 | - return new self($dt); |
|
| 127 | - } |
|
| 89 | + /** |
|
| 90 | + * |
|
| 91 | + * {@inheritdoc} |
|
| 92 | + * @return self |
|
| 93 | + */ |
|
| 94 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 95 | + int &$offset): ElementBase |
|
| 96 | + { |
|
| 97 | + $idx = $offset; |
|
| 98 | + $length = Length::expectFromDER($data, $idx); |
|
| 99 | + $str = substr($data, $idx, (int) $length->length()); |
|
| 100 | + $idx += $length->length(); |
|
| 101 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 102 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 103 | + } |
|
| 104 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 105 | + if (isset($match[7])) { |
|
| 106 | + $frac = $match[7]; |
|
| 107 | + // DER restricts trailing zeroes in fractional seconds component |
|
| 108 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
| 109 | + throw new DecodeException( |
|
| 110 | + "Fractional seconds must omit trailing zeroes."); |
|
| 111 | + } |
|
| 112 | + $frac = (int) $frac; |
|
| 113 | + } else { |
|
| 114 | + $frac = 0; |
|
| 115 | + } |
|
| 116 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 117 | + self::TZ_UTC; |
|
| 118 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 119 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 120 | + if (!$dt) { |
|
| 121 | + throw new DecodeException( |
|
| 122 | + "Failed to decode GeneralizedTime: " . |
|
| 123 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 124 | + } |
|
| 125 | + $offset = $idx; |
|
| 126 | + return new self($dt); |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -13,38 +13,38 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RelativeOID extends ObjectIdentifier |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $oid OID in dotted format |
|
| 20 | - */ |
|
| 21 | - public function __construct(string $oid) |
|
| 22 | - { |
|
| 23 | - $this->_oid = $oid; |
|
| 24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $oid OID in dotted format |
|
| 20 | + */ |
|
| 21 | + public function __construct(string $oid) |
|
| 22 | + { |
|
| 23 | + $this->_oid = $oid; |
|
| 24 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - protected function _encodedContentDER(): string |
|
| 32 | - { |
|
| 33 | - return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + protected function _encodedContentDER(): string |
|
| 32 | + { |
|
| 33 | + return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - * @return self |
|
| 40 | - */ |
|
| 41 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 42 | - int &$offset): ElementBase |
|
| 43 | - { |
|
| 44 | - $idx = $offset; |
|
| 45 | - $len = (int) Length::expectFromDER($data, $idx)->length(); |
|
| 46 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 47 | - $offset = $idx + $len; |
|
| 48 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 49 | - } |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + * {@inheritdoc} |
|
| 39 | + * @return self |
|
| 40 | + */ |
|
| 41 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 42 | + int &$offset): ElementBase |
|
| 43 | + { |
|
| 44 | + $idx = $offset; |
|
| 45 | + $len = (int) Length::expectFromDER($data, $idx)->length(); |
|
| 46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 47 | + $offset = $idx + $len; |
|
| 48 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -39,7 +39,7 @@ |
||
| 39 | 39 | * @return self |
| 40 | 40 | */ |
| 41 | 41 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 42 | - int &$offset): ElementBase |
|
| 42 | + int & $offset): ElementBase |
|
| 43 | 43 | { |
| 44 | 44 | $idx = $offset; |
| 45 | 45 | $len = (int) Length::expectFromDER($data, $idx)->length(); |
@@ -17,160 +17,160 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class ObjectIdentifier extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Object identifier in dotted format. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $_oid; |
|
| 23 | + /** |
|
| 24 | + * Object identifier in dotted format. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $_oid; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $oid OID in dotted format |
|
| 34 | - */ |
|
| 35 | - public function __construct(string $oid) |
|
| 36 | - { |
|
| 37 | - $this->_oid = $oid; |
|
| 38 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $oid OID in dotted format |
|
| 34 | + */ |
|
| 35 | + public function __construct(string $oid) |
|
| 36 | + { |
|
| 37 | + $this->_oid = $oid; |
|
| 38 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get OID in dotted format. |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - public function oid(): string |
|
| 47 | - { |
|
| 48 | - return $this->_oid; |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Get OID in dotted format. |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + public function oid(): string |
|
| 47 | + { |
|
| 48 | + return $this->_oid; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - */ |
|
| 55 | - protected function _encodedContentDER(): string |
|
| 56 | - { |
|
| 57 | - $subids = self::_explodeDottedOID($this->_oid); |
|
| 58 | - // encode first two subids to one according to spec section 8.19.4 |
|
| 59 | - if (count($subids) >= 2) { |
|
| 60 | - $num = ($subids[0] * 40) + $subids[1]; |
|
| 61 | - array_splice($subids, 0, 2, array($num)); |
|
| 62 | - } |
|
| 63 | - return self::_encodeSubIDs(...$subids); |
|
| 64 | - } |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + */ |
|
| 55 | + protected function _encodedContentDER(): string |
|
| 56 | + { |
|
| 57 | + $subids = self::_explodeDottedOID($this->_oid); |
|
| 58 | + // encode first two subids to one according to spec section 8.19.4 |
|
| 59 | + if (count($subids) >= 2) { |
|
| 60 | + $num = ($subids[0] * 40) + $subids[1]; |
|
| 61 | + array_splice($subids, 0, 2, array($num)); |
|
| 62 | + } |
|
| 63 | + return self::_encodeSubIDs(...$subids); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - * @return self |
|
| 70 | - */ |
|
| 71 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 72 | - int &$offset): ElementBase |
|
| 73 | - { |
|
| 74 | - $idx = $offset; |
|
| 75 | - $len = (int) Length::expectFromDER($data, $idx)->length(); |
|
| 76 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 77 | - $idx += $len; |
|
| 78 | - // decode first subidentifier according to spec section 8.19.4 |
|
| 79 | - if (isset($subids[0])) { |
|
| 80 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
| 81 | - array_splice($subids, 0, 1, array($x, $y)); |
|
| 82 | - } |
|
| 83 | - $offset = $idx; |
|
| 84 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 85 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + * @return self |
|
| 70 | + */ |
|
| 71 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 72 | + int &$offset): ElementBase |
|
| 73 | + { |
|
| 74 | + $idx = $offset; |
|
| 75 | + $len = (int) Length::expectFromDER($data, $idx)->length(); |
|
| 76 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 77 | + $idx += $len; |
|
| 78 | + // decode first subidentifier according to spec section 8.19.4 |
|
| 79 | + if (isset($subids[0])) { |
|
| 80 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
| 81 | + array_splice($subids, 0, 1, array($x, $y)); |
|
| 82 | + } |
|
| 83 | + $offset = $idx; |
|
| 84 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Explode dotted OID to an array of sub ID's. |
|
| 89 | - * |
|
| 90 | - * @param string $oid OID in dotted format |
|
| 91 | - * @return \GMP[] Array of GMP numbers |
|
| 92 | - */ |
|
| 93 | - protected static function _explodeDottedOID($oid): array |
|
| 94 | - { |
|
| 95 | - $subids = []; |
|
| 96 | - foreach (explode(".", $oid) as $subid) { |
|
| 97 | - $subids[] = gmp_init($subid, 10); |
|
| 98 | - } |
|
| 99 | - return $subids; |
|
| 100 | - } |
|
| 87 | + /** |
|
| 88 | + * Explode dotted OID to an array of sub ID's. |
|
| 89 | + * |
|
| 90 | + * @param string $oid OID in dotted format |
|
| 91 | + * @return \GMP[] Array of GMP numbers |
|
| 92 | + */ |
|
| 93 | + protected static function _explodeDottedOID($oid): array |
|
| 94 | + { |
|
| 95 | + $subids = []; |
|
| 96 | + foreach (explode(".", $oid) as $subid) { |
|
| 97 | + $subids[] = gmp_init($subid, 10); |
|
| 98 | + } |
|
| 99 | + return $subids; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Implode an array of sub IDs to dotted OID format. |
|
| 104 | - * |
|
| 105 | - * @param \GMP[] $subids |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
| 109 | - { |
|
| 110 | - return implode(".", |
|
| 111 | - array_map( |
|
| 112 | - function ($num) { |
|
| 113 | - return gmp_strval($num, 10); |
|
| 114 | - }, $subids)); |
|
| 115 | - } |
|
| 102 | + /** |
|
| 103 | + * Implode an array of sub IDs to dotted OID format. |
|
| 104 | + * |
|
| 105 | + * @param \GMP[] $subids |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
| 109 | + { |
|
| 110 | + return implode(".", |
|
| 111 | + array_map( |
|
| 112 | + function ($num) { |
|
| 113 | + return gmp_strval($num, 10); |
|
| 114 | + }, $subids)); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Encode sub ID's to DER. |
|
| 119 | - * |
|
| 120 | - * @param \GMP[] $subids |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
| 124 | - { |
|
| 125 | - $data = ""; |
|
| 126 | - foreach ($subids as $subid) { |
|
| 127 | - // if number fits to one base 128 byte |
|
| 128 | - if ($subid < 128) { |
|
| 129 | - $data .= chr(intval($subid)); |
|
| 130 | - } else { // encode to multiple bytes |
|
| 131 | - $bytes = []; |
|
| 132 | - do { |
|
| 133 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
| 134 | - $subid >>= 7; |
|
| 135 | - } while ($subid > 0); |
|
| 136 | - // all bytes except last must have bit 8 set to one |
|
| 137 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
| 138 | - $data .= chr(0x80 | $byte); |
|
| 139 | - } |
|
| 140 | - $data .= chr(reset($bytes)); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - return $data; |
|
| 144 | - } |
|
| 117 | + /** |
|
| 118 | + * Encode sub ID's to DER. |
|
| 119 | + * |
|
| 120 | + * @param \GMP[] $subids |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
| 124 | + { |
|
| 125 | + $data = ""; |
|
| 126 | + foreach ($subids as $subid) { |
|
| 127 | + // if number fits to one base 128 byte |
|
| 128 | + if ($subid < 128) { |
|
| 129 | + $data .= chr(intval($subid)); |
|
| 130 | + } else { // encode to multiple bytes |
|
| 131 | + $bytes = []; |
|
| 132 | + do { |
|
| 133 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
| 134 | + $subid >>= 7; |
|
| 135 | + } while ($subid > 0); |
|
| 136 | + // all bytes except last must have bit 8 set to one |
|
| 137 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
| 138 | + $data .= chr(0x80 | $byte); |
|
| 139 | + } |
|
| 140 | + $data .= chr(reset($bytes)); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + return $data; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Decode sub ID's from DER data. |
|
| 148 | - * |
|
| 149 | - * @param string $data |
|
| 150 | - * @throws DecodeException |
|
| 151 | - * @return \GMP[] Array of GMP numbers |
|
| 152 | - */ |
|
| 153 | - protected static function _decodeSubIDs($data): array |
|
| 154 | - { |
|
| 155 | - $subids = []; |
|
| 156 | - $idx = 0; |
|
| 157 | - $end = strlen($data); |
|
| 158 | - while ($idx < $end) { |
|
| 159 | - $num = gmp_init("0", 10); |
|
| 160 | - while (true) { |
|
| 161 | - if ($idx >= $end) { |
|
| 162 | - throw new DecodeException("Unexpected end of data."); |
|
| 163 | - } |
|
| 164 | - $byte = ord($data[$idx++]); |
|
| 165 | - $num |= $byte & 0x7f; |
|
| 166 | - // bit 8 of the last octet is zero |
|
| 167 | - if (!($byte & 0x80)) { |
|
| 168 | - break; |
|
| 169 | - } |
|
| 170 | - $num <<= 7; |
|
| 171 | - } |
|
| 172 | - $subids[] = $num; |
|
| 173 | - } |
|
| 174 | - return $subids; |
|
| 175 | - } |
|
| 146 | + /** |
|
| 147 | + * Decode sub ID's from DER data. |
|
| 148 | + * |
|
| 149 | + * @param string $data |
|
| 150 | + * @throws DecodeException |
|
| 151 | + * @return \GMP[] Array of GMP numbers |
|
| 152 | + */ |
|
| 153 | + protected static function _decodeSubIDs($data): array |
|
| 154 | + { |
|
| 155 | + $subids = []; |
|
| 156 | + $idx = 0; |
|
| 157 | + $end = strlen($data); |
|
| 158 | + while ($idx < $end) { |
|
| 159 | + $num = gmp_init("0", 10); |
|
| 160 | + while (true) { |
|
| 161 | + if ($idx >= $end) { |
|
| 162 | + throw new DecodeException("Unexpected end of data."); |
|
| 163 | + } |
|
| 164 | + $byte = ord($data[$idx++]); |
|
| 165 | + $num |= $byte & 0x7f; |
|
| 166 | + // bit 8 of the last octet is zero |
|
| 167 | + if (!($byte & 0x80)) { |
|
| 168 | + break; |
|
| 169 | + } |
|
| 170 | + $num <<= 7; |
|
| 171 | + } |
|
| 172 | + $subids[] = $num; |
|
| 173 | + } |
|
| 174 | + return $subids; |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @return self |
| 70 | 70 | */ |
| 71 | 71 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 72 | - int &$offset): ElementBase |
|
| 72 | + int & $offset): ElementBase |
|
| 73 | 73 | { |
| 74 | 74 | $idx = $offset; |
| 75 | 75 | $len = (int) Length::expectFromDER($data, $idx)->length(); |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | { |
| 110 | 110 | return implode(".", |
| 111 | 111 | array_map( |
| 112 | - function ($num) { |
|
| 112 | + function($num) { |
|
| 113 | 113 | return gmp_strval($num, 10); |
| 114 | 114 | }, $subids)); |
| 115 | 115 | } |
@@ -17,72 +17,72 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UTCTime extends TimeType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regular expression to parse date. |
|
| 25 | - * |
|
| 26 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 31 | - '(\d\d)' . /* YY */ |
|
| 32 | - '(\d\d)' . /* MM */ |
|
| 33 | - '(\d\d)' . /* DD */ |
|
| 34 | - '(\d\d)' . /* hh */ |
|
| 35 | - '(\d\d)' . /* mm */ |
|
| 36 | - '(\d\d)' . /* ss */ |
|
| 37 | - 'Z' . /* TZ */ |
|
| 38 | - '$#' /* @formatter:on */; |
|
| 23 | + /** |
|
| 24 | + * Regular expression to parse date. |
|
| 25 | + * |
|
| 26 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 31 | + '(\d\d)' . /* YY */ |
|
| 32 | + '(\d\d)' . /* MM */ |
|
| 33 | + '(\d\d)' . /* DD */ |
|
| 34 | + '(\d\d)' . /* hh */ |
|
| 35 | + '(\d\d)' . /* mm */ |
|
| 36 | + '(\d\d)' . /* ss */ |
|
| 37 | + 'Z' . /* TZ */ |
|
| 38 | + '$#' /* @formatter:on */; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Constructor. |
|
| 42 | - * |
|
| 43 | - * @param \DateTimeImmutable $dt |
|
| 44 | - */ |
|
| 45 | - public function __construct(\DateTimeImmutable $dt) |
|
| 46 | - { |
|
| 47 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
| 48 | - parent::__construct($dt); |
|
| 49 | - } |
|
| 40 | + /** |
|
| 41 | + * Constructor. |
|
| 42 | + * |
|
| 43 | + * @param \DateTimeImmutable $dt |
|
| 44 | + */ |
|
| 45 | + public function __construct(\DateTimeImmutable $dt) |
|
| 46 | + { |
|
| 47 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
| 48 | + parent::__construct($dt); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - */ |
|
| 55 | - protected function _encodedContentDER(): string |
|
| 56 | - { |
|
| 57 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
| 58 | - return $dt->format("ymdHis\Z"); |
|
| 59 | - } |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + */ |
|
| 55 | + protected function _encodedContentDER(): string |
|
| 56 | + { |
|
| 57 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
| 58 | + return $dt->format("ymdHis\Z"); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - * @return self |
|
| 65 | - */ |
|
| 66 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 67 | - int &$offset): ElementBase |
|
| 68 | - { |
|
| 69 | - $idx = $offset; |
|
| 70 | - $length = (int) Length::expectFromDER($data, $idx)->length(); |
|
| 71 | - $str = substr($data, $idx, $length); |
|
| 72 | - $idx += $length; |
|
| 73 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 74 | - throw new DecodeException("Invalid UTCTime format."); |
|
| 75 | - } |
|
| 76 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 77 | - $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
| 78 | - $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
| 79 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 80 | - if (!$dt) { |
|
| 81 | - throw new DecodeException( |
|
| 82 | - "Failed to decode UTCTime: " . |
|
| 83 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 84 | - } |
|
| 85 | - $offset = $idx; |
|
| 86 | - return new self($dt); |
|
| 87 | - } |
|
| 61 | + /** |
|
| 62 | + * |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + * @return self |
|
| 65 | + */ |
|
| 66 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 67 | + int &$offset): ElementBase |
|
| 68 | + { |
|
| 69 | + $idx = $offset; |
|
| 70 | + $length = (int) Length::expectFromDER($data, $idx)->length(); |
|
| 71 | + $str = substr($data, $idx, $length); |
|
| 72 | + $idx += $length; |
|
| 73 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 74 | + throw new DecodeException("Invalid UTCTime format."); |
|
| 75 | + } |
|
| 76 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 77 | + $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
| 78 | + $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
| 79 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 80 | + if (!$dt) { |
|
| 81 | + throw new DecodeException( |
|
| 82 | + "Failed to decode UTCTime: " . |
|
| 83 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 84 | + } |
|
| 85 | + $offset = $idx; |
|
| 86 | + return new self($dt); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | * @return self |
| 65 | 65 | */ |
| 66 | 66 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 67 | - int &$offset): ElementBase |
|
| 67 | + int & $offset): ElementBase |
|
| 68 | 68 | { |
| 69 | 69 | $idx = $offset; |
| 70 | 70 | $length = (int) Length::expectFromDER($data, $idx)->length(); |
@@ -20,105 +20,105 @@ |
||
| 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 = (int) Length::expectFromDER($this->_data, $idx)->length(); |
|
| 91 | - return substr($this->_data, $idx, $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 = (int) Length::expectFromDER($this->_data, $idx)->length(); |
|
| 91 | + return substr($this->_data, $idx, $length); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * |
|
| 96 | - * {@inheritdoc} |
|
| 97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | - * @return UnspecifiedType |
|
| 99 | - */ |
|
| 100 | - public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | - { |
|
| 102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | - $cls = self::_determineImplClass($identifier); |
|
| 104 | - $idx = $this->_offset; |
|
| 105 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 106 | - return new UnspecifiedType($element); |
|
| 107 | - } |
|
| 94 | + /** |
|
| 95 | + * |
|
| 96 | + * {@inheritdoc} |
|
| 97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | + * @return UnspecifiedType |
|
| 99 | + */ |
|
| 100 | + public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | + { |
|
| 102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | + $cls = self::_determineImplClass($identifier); |
|
| 104 | + $idx = $this->_offset; |
|
| 105 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 106 | + return new UnspecifiedType($element); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * |
|
| 111 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 112 | - * @return UnspecifiedType |
|
| 113 | - */ |
|
| 114 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 115 | - { |
|
| 116 | - $idx = $this->_offset; |
|
| 117 | - Length::expectFromDER($this->_data, $idx); |
|
| 118 | - $element = Element::fromDER($this->_data, $idx); |
|
| 119 | - if (isset($expectedTag)) { |
|
| 120 | - $element->expectType($expectedTag); |
|
| 121 | - } |
|
| 122 | - return new UnspecifiedType($element); |
|
| 123 | - } |
|
| 109 | + /** |
|
| 110 | + * |
|
| 111 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 112 | + * @return UnspecifiedType |
|
| 113 | + */ |
|
| 114 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 115 | + { |
|
| 116 | + $idx = $this->_offset; |
|
| 117 | + Length::expectFromDER($this->_data, $idx); |
|
| 118 | + $element = Element::fromDER($this->_data, $idx); |
|
| 119 | + if (isset($expectedTag)) { |
|
| 120 | + $element->expectType($expectedTag); |
|
| 121 | + } |
|
| 122 | + return new UnspecifiedType($element); |
|
| 123 | + } |
|
| 124 | 124 | } |