@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -16,195 +16,195 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class BitString extends StringType |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Number of unused bits in the last octet. |
|
| 24 | - * |
|
| 25 | - * @var int $_unusedBits |
|
| 26 | - */ |
|
| 27 | - protected $_unusedBits; |
|
| 22 | + /** |
|
| 23 | + * Number of unused bits in the last octet. |
|
| 24 | + * |
|
| 25 | + * @var int $_unusedBits |
|
| 26 | + */ |
|
| 27 | + protected $_unusedBits; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param string $string Content octets |
|
| 33 | - * @param int $unused_bits Number of unused bits in the last octet |
|
| 34 | - */ |
|
| 35 | - public function __construct(string $string, int $unused_bits = 0) |
|
| 36 | - { |
|
| 37 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 38 | - parent::__construct($string); |
|
| 39 | - $this->_unusedBits = $unused_bits; |
|
| 40 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param string $string Content octets |
|
| 33 | + * @param int $unused_bits Number of unused bits in the last octet |
|
| 34 | + */ |
|
| 35 | + public function __construct(string $string, int $unused_bits = 0) |
|
| 36 | + { |
|
| 37 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 38 | + parent::__construct($string); |
|
| 39 | + $this->_unusedBits = $unused_bits; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Get the number of bits in the string. |
|
| 44 | - * |
|
| 45 | - * @return int |
|
| 46 | - */ |
|
| 47 | - public function numBits(): int |
|
| 48 | - { |
|
| 49 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 50 | - } |
|
| 42 | + /** |
|
| 43 | + * Get the number of bits in the string. |
|
| 44 | + * |
|
| 45 | + * @return int |
|
| 46 | + */ |
|
| 47 | + public function numBits(): int |
|
| 48 | + { |
|
| 49 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Get the number of unused bits in the last octet of the string. |
|
| 54 | - * |
|
| 55 | - * @return int |
|
| 56 | - */ |
|
| 57 | - public function unusedBits(): int |
|
| 58 | - { |
|
| 59 | - return $this->_unusedBits; |
|
| 60 | - } |
|
| 52 | + /** |
|
| 53 | + * Get the number of unused bits in the last octet of the string. |
|
| 54 | + * |
|
| 55 | + * @return int |
|
| 56 | + */ |
|
| 57 | + public function unusedBits(): int |
|
| 58 | + { |
|
| 59 | + return $this->_unusedBits; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Test whether bit is set. |
|
| 64 | - * |
|
| 65 | - * @param int $idx Bit index. |
|
| 66 | - * Most significant bit of the first octet is index 0. |
|
| 67 | - * @return bool |
|
| 68 | - */ |
|
| 69 | - public function testBit(int $idx): bool |
|
| 70 | - { |
|
| 71 | - // octet index |
|
| 72 | - $oi = (int) floor($idx / 8); |
|
| 73 | - // if octet is outside range |
|
| 74 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 75 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 76 | - } |
|
| 77 | - // bit index |
|
| 78 | - $bi = $idx % 8; |
|
| 79 | - // if tested bit is last octet's unused bit |
|
| 80 | - if ($oi == strlen($this->_string) - 1) { |
|
| 81 | - if ($bi >= 8 - $this->_unusedBits) { |
|
| 82 | - throw new \OutOfBoundsException( |
|
| 83 | - "Index refers to an unused bit."); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - $byte = $this->_string[$oi]; |
|
| 87 | - // index 0 is the most significant bit in byte |
|
| 88 | - $mask = 0x01 << (7 - $bi); |
|
| 89 | - return (ord($byte) & $mask) > 0; |
|
| 90 | - } |
|
| 62 | + /** |
|
| 63 | + * Test whether bit is set. |
|
| 64 | + * |
|
| 65 | + * @param int $idx Bit index. |
|
| 66 | + * Most significant bit of the first octet is index 0. |
|
| 67 | + * @return bool |
|
| 68 | + */ |
|
| 69 | + public function testBit(int $idx): bool |
|
| 70 | + { |
|
| 71 | + // octet index |
|
| 72 | + $oi = (int) floor($idx / 8); |
|
| 73 | + // if octet is outside range |
|
| 74 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 75 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 76 | + } |
|
| 77 | + // bit index |
|
| 78 | + $bi = $idx % 8; |
|
| 79 | + // if tested bit is last octet's unused bit |
|
| 80 | + if ($oi == strlen($this->_string) - 1) { |
|
| 81 | + if ($bi >= 8 - $this->_unusedBits) { |
|
| 82 | + throw new \OutOfBoundsException( |
|
| 83 | + "Index refers to an unused bit."); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + $byte = $this->_string[$oi]; |
|
| 87 | + // index 0 is the most significant bit in byte |
|
| 88 | + $mask = 0x01 << (7 - $bi); |
|
| 89 | + return (ord($byte) & $mask) > 0; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Get range of bits. |
|
| 94 | - * |
|
| 95 | - * @param int $start Index of first bit |
|
| 96 | - * @param int $length Number of bits in range |
|
| 97 | - * @throws \OutOfBoundsException |
|
| 98 | - * @return string Integer of $length bits |
|
| 99 | - */ |
|
| 100 | - public function range(int $start, int $length): string |
|
| 101 | - { |
|
| 102 | - if (!$length) { |
|
| 103 | - return "0"; |
|
| 104 | - } |
|
| 105 | - if ($start + $length > $this->numBits()) { |
|
| 106 | - throw new \OutOfBoundsException("Not enough bits."); |
|
| 107 | - } |
|
| 108 | - $bits = gmp_init(0); |
|
| 109 | - $idx = $start; |
|
| 110 | - $end = $start + $length; |
|
| 111 | - while (true) { |
|
| 112 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
| 113 | - $bits |= $bit; |
|
| 114 | - if (++$idx >= $end) { |
|
| 115 | - break; |
|
| 116 | - } |
|
| 117 | - $bits <<= 1; |
|
| 118 | - } |
|
| 119 | - return gmp_strval($bits, 10); |
|
| 120 | - } |
|
| 92 | + /** |
|
| 93 | + * Get range of bits. |
|
| 94 | + * |
|
| 95 | + * @param int $start Index of first bit |
|
| 96 | + * @param int $length Number of bits in range |
|
| 97 | + * @throws \OutOfBoundsException |
|
| 98 | + * @return string Integer of $length bits |
|
| 99 | + */ |
|
| 100 | + public function range(int $start, int $length): string |
|
| 101 | + { |
|
| 102 | + if (!$length) { |
|
| 103 | + return "0"; |
|
| 104 | + } |
|
| 105 | + if ($start + $length > $this->numBits()) { |
|
| 106 | + throw new \OutOfBoundsException("Not enough bits."); |
|
| 107 | + } |
|
| 108 | + $bits = gmp_init(0); |
|
| 109 | + $idx = $start; |
|
| 110 | + $end = $start + $length; |
|
| 111 | + while (true) { |
|
| 112 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
| 113 | + $bits |= $bit; |
|
| 114 | + if (++$idx >= $end) { |
|
| 115 | + break; |
|
| 116 | + } |
|
| 117 | + $bits <<= 1; |
|
| 118 | + } |
|
| 119 | + return gmp_strval($bits, 10); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Get a copy of the bit string with trailing zeroes removed. |
|
| 124 | - * |
|
| 125 | - * @return self |
|
| 126 | - */ |
|
| 127 | - public function withoutTrailingZeroes(): self |
|
| 128 | - { |
|
| 129 | - // if bit string was empty |
|
| 130 | - if (!strlen($this->_string)) { |
|
| 131 | - return new self(""); |
|
| 132 | - } |
|
| 133 | - $bits = $this->_string; |
|
| 134 | - // count number of empty trailing octets |
|
| 135 | - $unused_octets = 0; |
|
| 136 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 137 | - if ($bits[$idx] != "\x0") { |
|
| 138 | - break; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - // strip trailing octets |
|
| 142 | - if ($unused_octets) { |
|
| 143 | - $bits = substr($bits, 0, -$unused_octets); |
|
| 144 | - } |
|
| 145 | - // if bit string was full of zeroes |
|
| 146 | - if (!strlen($bits)) { |
|
| 147 | - return new self(""); |
|
| 148 | - } |
|
| 149 | - // count number of trailing zeroes in the last octet |
|
| 150 | - $unused_bits = 0; |
|
| 151 | - $byte = ord($bits[strlen($bits) - 1]); |
|
| 152 | - while (!($byte & 0x01)) { |
|
| 153 | - $unused_bits++; |
|
| 154 | - $byte >>= 1; |
|
| 155 | - } |
|
| 156 | - return new self($bits, $unused_bits); |
|
| 157 | - } |
|
| 122 | + /** |
|
| 123 | + * Get a copy of the bit string with trailing zeroes removed. |
|
| 124 | + * |
|
| 125 | + * @return self |
|
| 126 | + */ |
|
| 127 | + public function withoutTrailingZeroes(): self |
|
| 128 | + { |
|
| 129 | + // if bit string was empty |
|
| 130 | + if (!strlen($this->_string)) { |
|
| 131 | + return new self(""); |
|
| 132 | + } |
|
| 133 | + $bits = $this->_string; |
|
| 134 | + // count number of empty trailing octets |
|
| 135 | + $unused_octets = 0; |
|
| 136 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 137 | + if ($bits[$idx] != "\x0") { |
|
| 138 | + break; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + // strip trailing octets |
|
| 142 | + if ($unused_octets) { |
|
| 143 | + $bits = substr($bits, 0, -$unused_octets); |
|
| 144 | + } |
|
| 145 | + // if bit string was full of zeroes |
|
| 146 | + if (!strlen($bits)) { |
|
| 147 | + return new self(""); |
|
| 148 | + } |
|
| 149 | + // count number of trailing zeroes in the last octet |
|
| 150 | + $unused_bits = 0; |
|
| 151 | + $byte = ord($bits[strlen($bits) - 1]); |
|
| 152 | + while (!($byte & 0x01)) { |
|
| 153 | + $unused_bits++; |
|
| 154 | + $byte >>= 1; |
|
| 155 | + } |
|
| 156 | + return new self($bits, $unused_bits); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * |
|
| 161 | - * {@inheritdoc} |
|
| 162 | - */ |
|
| 163 | - protected function _encodedContentDER(): string |
|
| 164 | - { |
|
| 165 | - $der = chr($this->_unusedBits); |
|
| 166 | - $der .= $this->_string; |
|
| 167 | - if ($this->_unusedBits) { |
|
| 168 | - $octet = $der[strlen($der) - 1]; |
|
| 169 | - // set unused bits to zero |
|
| 170 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 171 | - $der[strlen($der) - 1] = $octet; |
|
| 172 | - } |
|
| 173 | - return $der; |
|
| 174 | - } |
|
| 159 | + /** |
|
| 160 | + * |
|
| 161 | + * {@inheritdoc} |
|
| 162 | + */ |
|
| 163 | + protected function _encodedContentDER(): string |
|
| 164 | + { |
|
| 165 | + $der = chr($this->_unusedBits); |
|
| 166 | + $der .= $this->_string; |
|
| 167 | + if ($this->_unusedBits) { |
|
| 168 | + $octet = $der[strlen($der) - 1]; |
|
| 169 | + // set unused bits to zero |
|
| 170 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 171 | + $der[strlen($der) - 1] = $octet; |
|
| 172 | + } |
|
| 173 | + return $der; |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * |
|
| 178 | - * {@inheritdoc} |
|
| 179 | - * @return self |
|
| 180 | - */ |
|
| 181 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 182 | - string $data, int &$offset): ElementBase |
|
| 183 | - { |
|
| 184 | - $idx = $offset; |
|
| 185 | - $length = Length::expectFromDER($data, $idx); |
|
| 186 | - if ($length->intLength() < 1) { |
|
| 187 | - throw new DecodeException("Bit string length must be at least 1."); |
|
| 188 | - } |
|
| 189 | - $unused_bits = ord($data[$idx++]); |
|
| 190 | - if ($unused_bits > 7) { |
|
| 191 | - throw new DecodeException( |
|
| 192 | - "Unused bits in a bit string must be less than 8."); |
|
| 193 | - } |
|
| 194 | - $str_len = $length->intLength() - 1; |
|
| 195 | - if ($str_len) { |
|
| 196 | - $str = substr($data, $idx, $str_len); |
|
| 197 | - if ($unused_bits) { |
|
| 198 | - $mask = (1 << $unused_bits) - 1; |
|
| 199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | - throw new DecodeException( |
|
| 201 | - "DER encoded bit string must have zero padding."); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - } else { |
|
| 205 | - $str = ""; |
|
| 206 | - } |
|
| 207 | - $offset = $idx + $str_len; |
|
| 208 | - return new self($str, $unused_bits); |
|
| 209 | - } |
|
| 176 | + /** |
|
| 177 | + * |
|
| 178 | + * {@inheritdoc} |
|
| 179 | + * @return self |
|
| 180 | + */ |
|
| 181 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 182 | + string $data, int &$offset): ElementBase |
|
| 183 | + { |
|
| 184 | + $idx = $offset; |
|
| 185 | + $length = Length::expectFromDER($data, $idx); |
|
| 186 | + if ($length->intLength() < 1) { |
|
| 187 | + throw new DecodeException("Bit string length must be at least 1."); |
|
| 188 | + } |
|
| 189 | + $unused_bits = ord($data[$idx++]); |
|
| 190 | + if ($unused_bits > 7) { |
|
| 191 | + throw new DecodeException( |
|
| 192 | + "Unused bits in a bit string must be less than 8."); |
|
| 193 | + } |
|
| 194 | + $str_len = $length->intLength() - 1; |
|
| 195 | + if ($str_len) { |
|
| 196 | + $str = substr($data, $idx, $str_len); |
|
| 197 | + if ($unused_bits) { |
|
| 198 | + $mask = (1 << $unused_bits) - 1; |
|
| 199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | + throw new DecodeException( |
|
| 201 | + "DER encoded bit string must have zero padding."); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + } else { |
|
| 205 | + $str = ""; |
|
| 206 | + } |
|
| 207 | + $offset = $idx + $str_len; |
|
| 208 | + return new self($str, $unused_bits); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -16,73 +16,73 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class UTCTime extends TimeType |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Regular expression to parse date. |
|
| 24 | - * |
|
| 25 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 30 | - '(\d\d)' . /* YY */ |
|
| 31 | - '(\d\d)' . /* MM */ |
|
| 32 | - '(\d\d)' . /* DD */ |
|
| 33 | - '(\d\d)' . /* hh */ |
|
| 34 | - '(\d\d)' . /* mm */ |
|
| 35 | - '(\d\d)' . /* ss */ |
|
| 36 | - 'Z' . /* TZ */ |
|
| 37 | - '$#' /* @formatter:on */; |
|
| 22 | + /** |
|
| 23 | + * Regular expression to parse date. |
|
| 24 | + * |
|
| 25 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 30 | + '(\d\d)' . /* YY */ |
|
| 31 | + '(\d\d)' . /* MM */ |
|
| 32 | + '(\d\d)' . /* DD */ |
|
| 33 | + '(\d\d)' . /* hh */ |
|
| 34 | + '(\d\d)' . /* mm */ |
|
| 35 | + '(\d\d)' . /* ss */ |
|
| 36 | + 'Z' . /* TZ */ |
|
| 37 | + '$#' /* @formatter:on */; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Constructor. |
|
| 41 | - * |
|
| 42 | - * @param \DateTimeImmutable $dt |
|
| 43 | - */ |
|
| 44 | - public function __construct(\DateTimeImmutable $dt) |
|
| 45 | - { |
|
| 46 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
| 47 | - parent::__construct($dt); |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * Constructor. |
|
| 41 | + * |
|
| 42 | + * @param \DateTimeImmutable $dt |
|
| 43 | + */ |
|
| 44 | + public function __construct(\DateTimeImmutable $dt) |
|
| 45 | + { |
|
| 46 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
| 47 | + parent::__construct($dt); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - */ |
|
| 54 | - protected function _encodedContentDER(): string |
|
| 55 | - { |
|
| 56 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
| 57 | - return $dt->format("ymdHis\Z"); |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + */ |
|
| 54 | + protected function _encodedContentDER(): string |
|
| 55 | + { |
|
| 56 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
| 57 | + return $dt->format("ymdHis\Z"); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * |
|
| 62 | - * {@inheritdoc} |
|
| 63 | - * @return self |
|
| 64 | - */ |
|
| 65 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 66 | - string $data, int &$offset): ElementBase |
|
| 67 | - { |
|
| 68 | - $idx = $offset; |
|
| 69 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 70 | - $str = substr($data, $idx, $length); |
|
| 71 | - $idx += $length; |
|
| 72 | - /** @var $match string[] */ |
|
| 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 | - } |
|
| 60 | + /** |
|
| 61 | + * |
|
| 62 | + * {@inheritdoc} |
|
| 63 | + * @return self |
|
| 64 | + */ |
|
| 65 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 66 | + string $data, int &$offset): ElementBase |
|
| 67 | + { |
|
| 68 | + $idx = $offset; |
|
| 69 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 70 | + $str = substr($data, $idx, $length); |
|
| 71 | + $idx += $length; |
|
| 72 | + /** @var $match string[] */ |
|
| 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 | } |
@@ -12,39 +12,39 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class RelativeOID extends ObjectIdentifier |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $oid OID in dotted format |
|
| 19 | - */ |
|
| 20 | - public function __construct(string $oid) |
|
| 21 | - { |
|
| 22 | - $this->_oid = $oid; |
|
| 23 | - $this->_subids = self::_explodeDottedOID($oid); |
|
| 24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 25 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $oid OID in dotted format |
|
| 19 | + */ |
|
| 20 | + public function __construct(string $oid) |
|
| 21 | + { |
|
| 22 | + $this->_oid = $oid; |
|
| 23 | + $this->_subids = self::_explodeDottedOID($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(...$this->_subids); |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + protected function _encodedContentDER(): string |
|
| 32 | + { |
|
| 33 | + return self::_encodeSubIDs(...$this->_subids); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - * @return self |
|
| 40 | - */ |
|
| 41 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 42 | - string $data, int &$offset): ElementBase |
|
| 43 | - { |
|
| 44 | - $idx = $offset; |
|
| 45 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 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, |
|
| 42 | + string $data, int &$offset): ElementBase |
|
| 43 | + { |
|
| 44 | + $idx = $offset; |
|
| 45 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 47 | + $offset = $idx + $len; |
|
| 48 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; |
| 6 | 6 | |
@@ -16,113 +16,113 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GeneralizedTime extends TimeType |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Regular expression to parse date. |
|
| 24 | - * |
|
| 25 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 30 | - '(\d\d\d\d)' . /* YYYY */ |
|
| 31 | - '(\d\d)' . /* MM */ |
|
| 32 | - '(\d\d)' . /* DD */ |
|
| 33 | - '(\d\d)' . /* hh */ |
|
| 34 | - '(\d\d)' . /* mm */ |
|
| 35 | - '(\d\d)' . /* ss */ |
|
| 36 | - '(?:\.(\d+))?' . /* frac */ |
|
| 37 | - 'Z' . /* TZ */ |
|
| 38 | - '$#' /* @formatter:on */; |
|
| 22 | + /** |
|
| 23 | + * Regular expression to parse date. |
|
| 24 | + * |
|
| 25 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 30 | + '(\d\d\d\d)' . /* YYYY */ |
|
| 31 | + '(\d\d)' . /* MM */ |
|
| 32 | + '(\d\d)' . /* DD */ |
|
| 33 | + '(\d\d)' . /* hh */ |
|
| 34 | + '(\d\d)' . /* mm */ |
|
| 35 | + '(\d\d)' . /* ss */ |
|
| 36 | + '(?:\.(\d+))?' . /* frac */ |
|
| 37 | + 'Z' . /* TZ */ |
|
| 38 | + '$#' /* @formatter:on */; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Cached formatted date. |
|
| 42 | - * |
|
| 43 | - * @var string|null |
|
| 44 | - */ |
|
| 45 | - private $_formatted; |
|
| 40 | + /** |
|
| 41 | + * Cached formatted date. |
|
| 42 | + * |
|
| 43 | + * @var string|null |
|
| 44 | + */ |
|
| 45 | + private $_formatted; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor. |
|
| 49 | - * |
|
| 50 | - * @param \DateTimeImmutable $dt |
|
| 51 | - */ |
|
| 52 | - public function __construct(\DateTimeImmutable $dt) |
|
| 53 | - { |
|
| 54 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 55 | - parent::__construct($dt); |
|
| 56 | - } |
|
| 47 | + /** |
|
| 48 | + * Constructor. |
|
| 49 | + * |
|
| 50 | + * @param \DateTimeImmutable $dt |
|
| 51 | + */ |
|
| 52 | + public function __construct(\DateTimeImmutable $dt) |
|
| 53 | + { |
|
| 54 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 55 | + parent::__construct($dt); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Clear cached variables on clone. |
|
| 60 | - */ |
|
| 61 | - public function __clone() |
|
| 62 | - { |
|
| 63 | - $this->_formatted = null; |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * Clear cached variables on clone. |
|
| 60 | + */ |
|
| 61 | + public function __clone() |
|
| 62 | + { |
|
| 63 | + $this->_formatted = null; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - */ |
|
| 70 | - protected function _encodedContentDER(): string |
|
| 71 | - { |
|
| 72 | - if (!isset($this->_formatted)) { |
|
| 73 | - $dt = $this->_dateTime->setTimezone( |
|
| 74 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | - $this->_formatted = $dt->format("YmdHis"); |
|
| 76 | - // if fractions were used |
|
| 77 | - $frac = $dt->format("u"); |
|
| 78 | - if ($frac != 0) { |
|
| 79 | - $frac = rtrim($frac, "0"); |
|
| 80 | - $this->_formatted .= ".$frac"; |
|
| 81 | - } |
|
| 82 | - // timezone |
|
| 83 | - $this->_formatted .= "Z"; |
|
| 84 | - } |
|
| 85 | - return $this->_formatted; |
|
| 86 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + */ |
|
| 70 | + protected function _encodedContentDER(): string |
|
| 71 | + { |
|
| 72 | + if (!isset($this->_formatted)) { |
|
| 73 | + $dt = $this->_dateTime->setTimezone( |
|
| 74 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | + $this->_formatted = $dt->format("YmdHis"); |
|
| 76 | + // if fractions were used |
|
| 77 | + $frac = $dt->format("u"); |
|
| 78 | + if ($frac != 0) { |
|
| 79 | + $frac = rtrim($frac, "0"); |
|
| 80 | + $this->_formatted .= ".$frac"; |
|
| 81 | + } |
|
| 82 | + // timezone |
|
| 83 | + $this->_formatted .= "Z"; |
|
| 84 | + } |
|
| 85 | + return $this->_formatted; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * |
|
| 90 | - * {@inheritdoc} |
|
| 91 | - * @return self |
|
| 92 | - */ |
|
| 93 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 94 | - string $data, int &$offset): ElementBase |
|
| 95 | - { |
|
| 96 | - $idx = $offset; |
|
| 97 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 98 | - $str = substr($data, $idx, $length); |
|
| 99 | - $idx += $length; |
|
| 100 | - /** @var $match string[] */ |
|
| 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 | - } |
|
| 88 | + /** |
|
| 89 | + * |
|
| 90 | + * {@inheritdoc} |
|
| 91 | + * @return self |
|
| 92 | + */ |
|
| 93 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 94 | + string $data, int &$offset): ElementBase |
|
| 95 | + { |
|
| 96 | + $idx = $offset; |
|
| 97 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 98 | + $str = substr($data, $idx, $length); |
|
| 99 | + $idx += $length; |
|
| 100 | + /** @var $match string[] */ |
|
| 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 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Tagged; |
| 6 | 6 | |
@@ -21,154 +21,154 @@ |
||
| 21 | 21 | * May be encoded back to complete DER encoding. |
| 22 | 22 | */ |
| 23 | 23 | class DERTaggedType extends TaggedType implements |
| 24 | - ExplicitTagging, |
|
| 25 | - ImplicitTagging |
|
| 24 | + ExplicitTagging, |
|
| 25 | + ImplicitTagging |
|
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * Identifier. |
|
| 29 | - * |
|
| 30 | - * @var Identifier |
|
| 31 | - */ |
|
| 32 | - private $_identifier; |
|
| 27 | + /** |
|
| 28 | + * Identifier. |
|
| 29 | + * |
|
| 30 | + * @var Identifier |
|
| 31 | + */ |
|
| 32 | + private $_identifier; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * DER data. |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private $_data; |
|
| 34 | + /** |
|
| 35 | + * DER data. |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private $_data; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Offset to next byte after identifier. |
|
| 43 | - * |
|
| 44 | - * @var int |
|
| 45 | - */ |
|
| 46 | - private $_offset; |
|
| 41 | + /** |
|
| 42 | + * Offset to next byte after identifier. |
|
| 43 | + * |
|
| 44 | + * @var int |
|
| 45 | + */ |
|
| 46 | + private $_offset; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Offset to content. |
|
| 50 | - * |
|
| 51 | - * @var int |
|
| 52 | - */ |
|
| 53 | - private $_valueOffset; |
|
| 48 | + /** |
|
| 49 | + * Offset to content. |
|
| 50 | + * |
|
| 51 | + * @var int |
|
| 52 | + */ |
|
| 53 | + private $_valueOffset; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Length of the content. |
|
| 57 | - * |
|
| 58 | - * @var int |
|
| 59 | - */ |
|
| 60 | - private $_valueLength; |
|
| 55 | + /** |
|
| 56 | + * Length of the content. |
|
| 57 | + * |
|
| 58 | + * @var int |
|
| 59 | + */ |
|
| 60 | + private $_valueLength; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Constructor. |
|
| 64 | - * |
|
| 65 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 66 | - * @param string $data DER data |
|
| 67 | - * @param int $offset Offset to next byte after identifier |
|
| 68 | - * @param int $value_offset Offset to content |
|
| 69 | - * @param int $value_length Content length |
|
| 70 | - */ |
|
| 71 | - public function __construct(Identifier $identifier, string $data, |
|
| 72 | - int $offset, int $value_offset, int $value_length, |
|
| 73 | - bool $indefinite_length) |
|
| 74 | - { |
|
| 75 | - $this->_identifier = $identifier; |
|
| 76 | - $this->_data = $data; |
|
| 77 | - $this->_offset = $offset; |
|
| 78 | - $this->_valueOffset = $value_offset; |
|
| 79 | - $this->_valueLength = $value_length; |
|
| 80 | - $this->_indefiniteLength = $indefinite_length; |
|
| 81 | - $this->_typeTag = $identifier->intTag(); |
|
| 82 | - } |
|
| 62 | + /** |
|
| 63 | + * Constructor. |
|
| 64 | + * |
|
| 65 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 66 | + * @param string $data DER data |
|
| 67 | + * @param int $offset Offset to next byte after identifier |
|
| 68 | + * @param int $value_offset Offset to content |
|
| 69 | + * @param int $value_length Content length |
|
| 70 | + */ |
|
| 71 | + public function __construct(Identifier $identifier, string $data, |
|
| 72 | + int $offset, int $value_offset, int $value_length, |
|
| 73 | + bool $indefinite_length) |
|
| 74 | + { |
|
| 75 | + $this->_identifier = $identifier; |
|
| 76 | + $this->_data = $data; |
|
| 77 | + $this->_offset = $offset; |
|
| 78 | + $this->_valueOffset = $value_offset; |
|
| 79 | + $this->_valueLength = $value_length; |
|
| 80 | + $this->_indefiniteLength = $indefinite_length; |
|
| 81 | + $this->_typeTag = $identifier->intTag(); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * |
|
| 86 | - * {@inheritdoc} |
|
| 87 | - */ |
|
| 88 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 89 | - string $data, int &$offset): ElementBase |
|
| 90 | - { |
|
| 91 | - $idx = $offset; |
|
| 92 | - $length = Length::expectFromDER($data, $idx); |
|
| 93 | - // offset to inner value |
|
| 94 | - $value_offset = $idx; |
|
| 95 | - if ($length->isIndefinite()) { |
|
| 96 | - if ($identifier->isPrimitive()) { |
|
| 97 | - throw new DecodeException( |
|
| 98 | - 'Primitive type with indefinite length is not supported.'); |
|
| 99 | - } |
|
| 100 | - while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
| 101 | - // EOC consists of two octets. |
|
| 102 | - $value_length = $idx - $value_offset - 2; |
|
| 103 | - } else { |
|
| 104 | - $value_length = $length->intLength(); |
|
| 105 | - $idx += $value_length; |
|
| 106 | - } |
|
| 107 | - // late static binding since ApplicationType and PrivateType extend this class |
|
| 108 | - $type = new static($identifier, $data, $offset, $value_offset, |
|
| 109 | - $value_length, $length->isIndefinite()); |
|
| 110 | - $offset = $idx; |
|
| 111 | - return $type; |
|
| 112 | - } |
|
| 84 | + /** |
|
| 85 | + * |
|
| 86 | + * {@inheritdoc} |
|
| 87 | + */ |
|
| 88 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 89 | + string $data, int &$offset): ElementBase |
|
| 90 | + { |
|
| 91 | + $idx = $offset; |
|
| 92 | + $length = Length::expectFromDER($data, $idx); |
|
| 93 | + // offset to inner value |
|
| 94 | + $value_offset = $idx; |
|
| 95 | + if ($length->isIndefinite()) { |
|
| 96 | + if ($identifier->isPrimitive()) { |
|
| 97 | + throw new DecodeException( |
|
| 98 | + 'Primitive type with indefinite length is not supported.'); |
|
| 99 | + } |
|
| 100 | + while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
| 101 | + // EOC consists of two octets. |
|
| 102 | + $value_length = $idx - $value_offset - 2; |
|
| 103 | + } else { |
|
| 104 | + $value_length = $length->intLength(); |
|
| 105 | + $idx += $value_length; |
|
| 106 | + } |
|
| 107 | + // late static binding since ApplicationType and PrivateType extend this class |
|
| 108 | + $type = new static($identifier, $data, $offset, $value_offset, |
|
| 109 | + $value_length, $length->isIndefinite()); |
|
| 110 | + $offset = $idx; |
|
| 111 | + return $type; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * |
|
| 116 | - * @see \ASN1\Element::typeClass() |
|
| 117 | - * @return int |
|
| 118 | - */ |
|
| 119 | - public function typeClass(): int |
|
| 120 | - { |
|
| 121 | - return $this->_identifier->typeClass(); |
|
| 122 | - } |
|
| 114 | + /** |
|
| 115 | + * |
|
| 116 | + * @see \ASN1\Element::typeClass() |
|
| 117 | + * @return int |
|
| 118 | + */ |
|
| 119 | + public function typeClass(): int |
|
| 120 | + { |
|
| 121 | + return $this->_identifier->typeClass(); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * |
|
| 126 | - * @see \ASN1\Element::isConstructed() |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function isConstructed(): bool |
|
| 130 | - { |
|
| 131 | - return $this->_identifier->isConstructed(); |
|
| 132 | - } |
|
| 124 | + /** |
|
| 125 | + * |
|
| 126 | + * @see \ASN1\Element::isConstructed() |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function isConstructed(): bool |
|
| 130 | + { |
|
| 131 | + return $this->_identifier->isConstructed(); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * |
|
| 136 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 137 | - * @return string |
|
| 138 | - */ |
|
| 139 | - protected function _encodedContentDER(): string |
|
| 140 | - { |
|
| 141 | - return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
| 142 | - } |
|
| 134 | + /** |
|
| 135 | + * |
|
| 136 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 137 | + * @return string |
|
| 138 | + */ |
|
| 139 | + protected function _encodedContentDER(): string |
|
| 140 | + { |
|
| 141 | + return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * |
|
| 146 | - * {@inheritdoc} |
|
| 147 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 148 | - * @return UnspecifiedType |
|
| 149 | - */ |
|
| 150 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 151 | - { |
|
| 152 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 153 | - $cls = self::_determineImplClass($identifier); |
|
| 154 | - $idx = $this->_offset; |
|
| 155 | - /** @var \ASN1\Feature\ElementBase $element */ |
|
| 156 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 157 | - return $element->asUnspecified(); |
|
| 158 | - } |
|
| 144 | + /** |
|
| 145 | + * |
|
| 146 | + * {@inheritdoc} |
|
| 147 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 148 | + * @return UnspecifiedType |
|
| 149 | + */ |
|
| 150 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 151 | + { |
|
| 152 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 153 | + $cls = self::_determineImplClass($identifier); |
|
| 154 | + $idx = $this->_offset; |
|
| 155 | + /** @var \ASN1\Feature\ElementBase $element */ |
|
| 156 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 157 | + return $element->asUnspecified(); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * |
|
| 162 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 163 | - * @return UnspecifiedType |
|
| 164 | - */ |
|
| 165 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 166 | - { |
|
| 167 | - $idx = $this->_valueOffset; |
|
| 168 | - $element = Element::fromDER($this->_data, $idx); |
|
| 169 | - if (isset($expectedTag)) { |
|
| 170 | - $element->expectType($expectedTag); |
|
| 171 | - } |
|
| 172 | - return $element->asUnspecified(); |
|
| 173 | - } |
|
| 160 | + /** |
|
| 161 | + * |
|
| 162 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 163 | + * @return UnspecifiedType |
|
| 164 | + */ |
|
| 165 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 166 | + { |
|
| 167 | + $idx = $this->_valueOffset; |
|
| 168 | + $element = Element::fromDER($this->_data, $idx); |
|
| 169 | + if (isset($expectedTag)) { |
|
| 170 | + $element->expectType($expectedTag); |
|
| 171 | + } |
|
| 172 | + return $element->asUnspecified(); |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Tagged; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Tagged; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type; |
| 6 | 6 | |
@@ -14,61 +14,61 @@ |
||
| 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, |
|
| 36 | - string $data, int &$offset): ElementBase |
|
| 37 | - { |
|
| 38 | - $idx = $offset; |
|
| 39 | - if (!$identifier->isPrimitive()) { |
|
| 40 | - $length = Length::expectFromDER($data, $idx); |
|
| 29 | + /** |
|
| 30 | + * |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + * @see \ASN1\Element::_decodeFromDER() |
|
| 33 | + * @return self |
|
| 34 | + */ |
|
| 35 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 36 | + string $data, int &$offset): ElementBase |
|
| 37 | + { |
|
| 38 | + $idx = $offset; |
|
| 39 | + if (!$identifier->isPrimitive()) { |
|
| 40 | + $length = Length::expectFromDER($data, $idx); |
|
| 41 | 41 | |
| 42 | - /* |
|
| 42 | + /* |
|
| 43 | 43 | * A primitive string is not a constructed type by definition. |
| 44 | 44 | * Alas, if it is encoded as constructed type (0x24), we expect that |
| 45 | 45 | * some primitive OCTET_STRINGs follow. So loop over them, create a |
| 46 | 46 | * concatenated big string and return it. |
| 47 | 47 | */ |
| 48 | - if ($length->isIndefinite()) { |
|
| 49 | - $str = ''; |
|
| 50 | - do { |
|
| 51 | - $offset = $idx; |
|
| 52 | - $element = Element::fromDER($data, $offset); |
|
| 53 | - if ($element instanceof PrimitiveString) { |
|
| 54 | - $str .= $element->string(); |
|
| 55 | - $idx = $offset; |
|
| 56 | - } |
|
| 57 | - } while ($element instanceof PrimitiveString); |
|
| 58 | - return new static($str); |
|
| 59 | - } |
|
| 60 | - } else { |
|
| 61 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 62 | - } |
|
| 63 | - $str = $length ? substr($data, $idx, $length) : ""; |
|
| 64 | - // substr should never return false, since length is |
|
| 65 | - // checked by Length::expectFromDER. |
|
| 66 | - assert(is_string($str), new DecodeException("substr")); |
|
| 67 | - $offset = $idx + $length; |
|
| 68 | - try { |
|
| 69 | - return new static($str); |
|
| 70 | - } catch (\InvalidArgumentException $e) { |
|
| 71 | - throw new DecodeException($e->getMessage(), 0, $e); |
|
| 72 | - } |
|
| 73 | - } |
|
| 48 | + if ($length->isIndefinite()) { |
|
| 49 | + $str = ''; |
|
| 50 | + do { |
|
| 51 | + $offset = $idx; |
|
| 52 | + $element = Element::fromDER($data, $offset); |
|
| 53 | + if ($element instanceof PrimitiveString) { |
|
| 54 | + $str .= $element->string(); |
|
| 55 | + $idx = $offset; |
|
| 56 | + } |
|
| 57 | + } while ($element instanceof PrimitiveString); |
|
| 58 | + return new static($str); |
|
| 59 | + } |
|
| 60 | + } else { |
|
| 61 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 62 | + } |
|
| 63 | + $str = $length ? substr($data, $idx, $length) : ""; |
|
| 64 | + // substr should never return false, since length is |
|
| 65 | + // checked by Length::expectFromDER. |
|
| 66 | + assert(is_string($str), new DecodeException("substr")); |
|
| 67 | + $offset = $idx + $length; |
|
| 68 | + try { |
|
| 69 | + return new static($str); |
|
| 70 | + } catch (\InvalidArgumentException $e) { |
|
| 71 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type; |
| 6 | 6 | |
@@ -16,665 +16,665 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class UnspecifiedType implements ElementBase |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The wrapped element. |
|
| 21 | - * |
|
| 22 | - * @var Element |
|
| 23 | - */ |
|
| 24 | - private $_element; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param Element $el |
|
| 30 | - */ |
|
| 31 | - public function __construct(Element $el) |
|
| 32 | - { |
|
| 33 | - $this->_element = $el; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Initialize from DER data. |
|
| 38 | - * |
|
| 39 | - * @param string $data DER encoded data |
|
| 40 | - * @return self |
|
| 41 | - */ |
|
| 42 | - public static function fromDER(string $data): self |
|
| 43 | - { |
|
| 44 | - return Element::fromDER($data)->asUnspecified(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Initialize from ElementBase interface. |
|
| 49 | - * |
|
| 50 | - * @param ElementBase $el |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - public static function fromElementBase(ElementBase $el): self |
|
| 54 | - { |
|
| 55 | - // if element is already wrapped |
|
| 56 | - if ($el instanceof self) { |
|
| 57 | - return $el; |
|
| 58 | - } |
|
| 59 | - return new self($el->asElement()); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Compatibility method to dispatch calls to the wrapped element. |
|
| 64 | - * |
|
| 65 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 66 | - * @param string $mtd Method name |
|
| 67 | - * @param array $args Arguments |
|
| 68 | - * @return mixed |
|
| 69 | - */ |
|
| 70 | - public function __call($mtd, array $args) |
|
| 71 | - { |
|
| 72 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get the wrapped element as a context specific tagged type. |
|
| 77 | - * |
|
| 78 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 79 | - * @return TaggedType |
|
| 80 | - */ |
|
| 81 | - public function asTagged(): TaggedType |
|
| 82 | - { |
|
| 83 | - if (!$this->_element instanceof TaggedType) { |
|
| 84 | - throw new \UnexpectedValueException( |
|
| 85 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 86 | - } |
|
| 87 | - return $this->_element; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get the wrapped element as an application specific type. |
|
| 92 | - * |
|
| 93 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 94 | - * @return \ASN1\Type\Tagged\ApplicationType |
|
| 95 | - */ |
|
| 96 | - public function asApplication(): Tagged\ApplicationType |
|
| 97 | - { |
|
| 98 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 99 | - throw new \UnexpectedValueException( |
|
| 100 | - "Application type expected, got " . |
|
| 101 | - $this->_typeDescriptorString()); |
|
| 102 | - } |
|
| 103 | - return $this->_element; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Get the wrapped element as a private tagged type. |
|
| 108 | - * |
|
| 109 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
| 110 | - * @return \ASN1\Type\Tagged\PrivateType |
|
| 111 | - */ |
|
| 112 | - public function asPrivate(): Tagged\PrivateType |
|
| 113 | - { |
|
| 114 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 115 | - throw new \UnexpectedValueException( |
|
| 116 | - "Private type expected, got " . $this->_typeDescriptorString()); |
|
| 117 | - } |
|
| 118 | - return $this->_element; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get the wrapped element as a boolean type. |
|
| 123 | - * |
|
| 124 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 125 | - * @return \ASN1\Type\Primitive\Boolean |
|
| 126 | - */ |
|
| 127 | - public function asBoolean(): Primitive\Boolean |
|
| 128 | - { |
|
| 129 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 130 | - throw new \UnexpectedValueException( |
|
| 131 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 132 | - } |
|
| 133 | - return $this->_element; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Get the wrapped element as an integer type. |
|
| 138 | - * |
|
| 139 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 140 | - * @return \ASN1\Type\Primitive\Integer |
|
| 141 | - */ |
|
| 142 | - public function asInteger(): Primitive\Integer |
|
| 143 | - { |
|
| 144 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 145 | - throw new \UnexpectedValueException( |
|
| 146 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 147 | - } |
|
| 148 | - return $this->_element; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get the wrapped element as a bit string type. |
|
| 153 | - * |
|
| 154 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 155 | - * @return \ASN1\Type\Primitive\BitString |
|
| 156 | - */ |
|
| 157 | - public function asBitString(): Primitive\BitString |
|
| 158 | - { |
|
| 159 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 160 | - throw new \UnexpectedValueException( |
|
| 161 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 162 | - } |
|
| 163 | - return $this->_element; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get the wrapped element as an octet string type. |
|
| 168 | - * |
|
| 169 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 170 | - * @return \ASN1\Type\Primitive\OctetString |
|
| 171 | - */ |
|
| 172 | - public function asOctetString(): Primitive\OctetString |
|
| 173 | - { |
|
| 174 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 175 | - throw new \UnexpectedValueException( |
|
| 176 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 177 | - } |
|
| 178 | - return $this->_element; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the wrapped element as a null type. |
|
| 183 | - * |
|
| 184 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 185 | - * @return \ASN1\Type\Primitive\NullType |
|
| 186 | - */ |
|
| 187 | - public function asNull(): Primitive\NullType |
|
| 188 | - { |
|
| 189 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 190 | - throw new \UnexpectedValueException( |
|
| 191 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 192 | - } |
|
| 193 | - return $this->_element; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Get the wrapped element as an object identifier type. |
|
| 198 | - * |
|
| 199 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 200 | - * identifier |
|
| 201 | - * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
| 202 | - */ |
|
| 203 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 204 | - { |
|
| 205 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 206 | - throw new \UnexpectedValueException( |
|
| 207 | - $this->_generateExceptionMessage( |
|
| 208 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 209 | - } |
|
| 210 | - return $this->_element; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Get the wrapped element as an object descriptor type. |
|
| 215 | - * |
|
| 216 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 217 | - * descriptor |
|
| 218 | - * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
| 219 | - */ |
|
| 220 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 221 | - { |
|
| 222 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 223 | - throw new \UnexpectedValueException( |
|
| 224 | - $this->_generateExceptionMessage( |
|
| 225 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 226 | - } |
|
| 227 | - return $this->_element; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the wrapped element as a real type. |
|
| 232 | - * |
|
| 233 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 234 | - * @return \ASN1\Type\Primitive\Real |
|
| 235 | - */ |
|
| 236 | - public function asReal(): Primitive\Real |
|
| 237 | - { |
|
| 238 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 239 | - throw new \UnexpectedValueException( |
|
| 240 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 241 | - } |
|
| 242 | - return $this->_element; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Get the wrapped element as an enumerated type. |
|
| 247 | - * |
|
| 248 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 249 | - * @return \ASN1\Type\Primitive\Enumerated |
|
| 250 | - */ |
|
| 251 | - public function asEnumerated(): Primitive\Enumerated |
|
| 252 | - { |
|
| 253 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 254 | - throw new \UnexpectedValueException( |
|
| 255 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 256 | - } |
|
| 257 | - return $this->_element; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Get the wrapped element as a UTF8 string type. |
|
| 262 | - * |
|
| 263 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 264 | - * @return \ASN1\Type\Primitive\UTF8String |
|
| 265 | - */ |
|
| 266 | - public function asUTF8String(): Primitive\UTF8String |
|
| 267 | - { |
|
| 268 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 269 | - throw new \UnexpectedValueException( |
|
| 270 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 271 | - } |
|
| 272 | - return $this->_element; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Get the wrapped element as a relative OID type. |
|
| 277 | - * |
|
| 278 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 279 | - * @return \ASN1\Type\Primitive\RelativeOID |
|
| 280 | - */ |
|
| 281 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 282 | - { |
|
| 283 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 284 | - throw new \UnexpectedValueException( |
|
| 285 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 286 | - } |
|
| 287 | - return $this->_element; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Get the wrapped element as a sequence type. |
|
| 292 | - * |
|
| 293 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 294 | - * @return \ASN1\Type\Constructed\Sequence |
|
| 295 | - */ |
|
| 296 | - public function asSequence(): Constructed\Sequence |
|
| 297 | - { |
|
| 298 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 299 | - throw new \UnexpectedValueException( |
|
| 300 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 301 | - } |
|
| 302 | - return $this->_element; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Get the wrapped element as a set type. |
|
| 307 | - * |
|
| 308 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 309 | - * @return \ASN1\Type\Constructed\Set |
|
| 310 | - */ |
|
| 311 | - public function asSet(): Constructed\Set |
|
| 312 | - { |
|
| 313 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 314 | - throw new \UnexpectedValueException( |
|
| 315 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 316 | - } |
|
| 317 | - return $this->_element; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Get the wrapped element as a numeric string type. |
|
| 322 | - * |
|
| 323 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 324 | - * @return \ASN1\Type\Primitive\NumericString |
|
| 325 | - */ |
|
| 326 | - public function asNumericString(): Primitive\NumericString |
|
| 327 | - { |
|
| 328 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 329 | - throw new \UnexpectedValueException( |
|
| 330 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 331 | - } |
|
| 332 | - return $this->_element; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Get the wrapped element as a printable string type. |
|
| 337 | - * |
|
| 338 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 339 | - * string |
|
| 340 | - * @return \ASN1\Type\Primitive\PrintableString |
|
| 341 | - */ |
|
| 342 | - public function asPrintableString(): Primitive\PrintableString |
|
| 343 | - { |
|
| 344 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 345 | - throw new \UnexpectedValueException( |
|
| 346 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 347 | - } |
|
| 348 | - return $this->_element; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Get the wrapped element as a T61 string type. |
|
| 353 | - * |
|
| 354 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 355 | - * @return \ASN1\Type\Primitive\T61String |
|
| 356 | - */ |
|
| 357 | - public function asT61String(): Primitive\T61String |
|
| 358 | - { |
|
| 359 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 360 | - throw new \UnexpectedValueException( |
|
| 361 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 362 | - } |
|
| 363 | - return $this->_element; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Get the wrapped element as a videotex string type. |
|
| 368 | - * |
|
| 369 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 370 | - * @return \ASN1\Type\Primitive\VideotexString |
|
| 371 | - */ |
|
| 372 | - public function asVideotexString(): Primitive\VideotexString |
|
| 373 | - { |
|
| 374 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 375 | - throw new \UnexpectedValueException( |
|
| 376 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 377 | - } |
|
| 378 | - return $this->_element; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Get the wrapped element as a IA5 string type. |
|
| 383 | - * |
|
| 384 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 385 | - * @return \ASN1\Type\Primitive\IA5String |
|
| 386 | - */ |
|
| 387 | - public function asIA5String(): Primitive\IA5String |
|
| 388 | - { |
|
| 389 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 390 | - throw new \UnexpectedValueException( |
|
| 391 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 392 | - } |
|
| 393 | - return $this->_element; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get the wrapped element as an UTC time type. |
|
| 398 | - * |
|
| 399 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 400 | - * @return \ASN1\Type\Primitive\UTCTime |
|
| 401 | - */ |
|
| 402 | - public function asUTCTime(): Primitive\UTCTime |
|
| 403 | - { |
|
| 404 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 405 | - throw new \UnexpectedValueException( |
|
| 406 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 407 | - } |
|
| 408 | - return $this->_element; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Get the wrapped element as a generalized time type. |
|
| 413 | - * |
|
| 414 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 415 | - * time |
|
| 416 | - * @return \ASN1\Type\Primitive\GeneralizedTime |
|
| 417 | - */ |
|
| 418 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 419 | - { |
|
| 420 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 421 | - throw new \UnexpectedValueException( |
|
| 422 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 423 | - } |
|
| 424 | - return $this->_element; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Get the wrapped element as a graphic string type. |
|
| 429 | - * |
|
| 430 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 431 | - * @return \ASN1\Type\Primitive\GraphicString |
|
| 432 | - */ |
|
| 433 | - public function asGraphicString(): Primitive\GraphicString |
|
| 434 | - { |
|
| 435 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 436 | - throw new \UnexpectedValueException( |
|
| 437 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 438 | - } |
|
| 439 | - return $this->_element; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Get the wrapped element as a visible string type. |
|
| 444 | - * |
|
| 445 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 446 | - * @return \ASN1\Type\Primitive\VisibleString |
|
| 447 | - */ |
|
| 448 | - public function asVisibleString(): Primitive\VisibleString |
|
| 449 | - { |
|
| 450 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 451 | - throw new \UnexpectedValueException( |
|
| 452 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 453 | - } |
|
| 454 | - return $this->_element; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * Get the wrapped element as a general string type. |
|
| 459 | - * |
|
| 460 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 461 | - * @return \ASN1\Type\Primitive\GeneralString |
|
| 462 | - */ |
|
| 463 | - public function asGeneralString(): Primitive\GeneralString |
|
| 464 | - { |
|
| 465 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 466 | - throw new \UnexpectedValueException( |
|
| 467 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 468 | - } |
|
| 469 | - return $this->_element; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Get the wrapped element as a universal string type. |
|
| 474 | - * |
|
| 475 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 476 | - * string |
|
| 477 | - * @return \ASN1\Type\Primitive\UniversalString |
|
| 478 | - */ |
|
| 479 | - public function asUniversalString(): Primitive\UniversalString |
|
| 480 | - { |
|
| 481 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 482 | - throw new \UnexpectedValueException( |
|
| 483 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 484 | - } |
|
| 485 | - return $this->_element; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Get the wrapped element as a character string type. |
|
| 490 | - * |
|
| 491 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 492 | - * string |
|
| 493 | - * @return \ASN1\Type\Primitive\CharacterString |
|
| 494 | - */ |
|
| 495 | - public function asCharacterString(): Primitive\CharacterString |
|
| 496 | - { |
|
| 497 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 498 | - throw new \UnexpectedValueException( |
|
| 499 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 500 | - } |
|
| 501 | - return $this->_element; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Get the wrapped element as a BMP string type. |
|
| 506 | - * |
|
| 507 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 508 | - * @return \ASN1\Type\Primitive\BMPString |
|
| 509 | - */ |
|
| 510 | - public function asBMPString(): Primitive\BMPString |
|
| 511 | - { |
|
| 512 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 513 | - throw new \UnexpectedValueException( |
|
| 514 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 515 | - } |
|
| 516 | - return $this->_element; |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Get the wrapped element as any string type. |
|
| 521 | - * |
|
| 522 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 523 | - * @return StringType |
|
| 524 | - */ |
|
| 525 | - public function asString(): StringType |
|
| 526 | - { |
|
| 527 | - if (!$this->_element instanceof StringType) { |
|
| 528 | - throw new \UnexpectedValueException( |
|
| 529 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 530 | - } |
|
| 531 | - return $this->_element; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Get the wrapped element as any time type. |
|
| 536 | - * |
|
| 537 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 538 | - * @return TimeType |
|
| 539 | - */ |
|
| 540 | - public function asTime(): TimeType |
|
| 541 | - { |
|
| 542 | - if (!$this->_element instanceof TimeType) { |
|
| 543 | - throw new \UnexpectedValueException( |
|
| 544 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 545 | - } |
|
| 546 | - return $this->_element; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 551 | - * |
|
| 552 | - * @param int $tag Type tag of the expected element |
|
| 553 | - * @return string |
|
| 554 | - */ |
|
| 555 | - private function _generateExceptionMessage(int $tag): string |
|
| 556 | - { |
|
| 557 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 558 | - $this->_typeDescriptorString()); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 563 | - * |
|
| 564 | - * @return string |
|
| 565 | - */ |
|
| 566 | - private function _typeDescriptorString(): string |
|
| 567 | - { |
|
| 568 | - $type_cls = $this->_element->typeClass(); |
|
| 569 | - $tag = $this->_element->tag(); |
|
| 570 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 571 | - return Element::tagToName($tag); |
|
| 572 | - } |
|
| 573 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * |
|
| 578 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 579 | - * @return string |
|
| 580 | - */ |
|
| 581 | - public function toDER(): string |
|
| 582 | - { |
|
| 583 | - return $this->_element->toDER(); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * |
|
| 588 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 589 | - * @return int |
|
| 590 | - */ |
|
| 591 | - public function typeClass(): int |
|
| 592 | - { |
|
| 593 | - return $this->_element->typeClass(); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * |
|
| 598 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 599 | - * @return bool |
|
| 600 | - */ |
|
| 601 | - public function isConstructed(): bool |
|
| 602 | - { |
|
| 603 | - return $this->_element->isConstructed(); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * |
|
| 608 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 609 | - * @return int |
|
| 610 | - */ |
|
| 611 | - public function tag(): int |
|
| 612 | - { |
|
| 613 | - return $this->_element->tag(); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * |
|
| 618 | - * {@inheritdoc} |
|
| 619 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 620 | - * @return bool |
|
| 621 | - */ |
|
| 622 | - public function isType(int $tag): bool |
|
| 623 | - { |
|
| 624 | - return $this->_element->isType($tag); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * |
|
| 629 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 630 | - * type strictness. |
|
| 631 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 632 | - * @return ElementBase |
|
| 633 | - */ |
|
| 634 | - public function expectType(int $tag): ElementBase |
|
| 635 | - { |
|
| 636 | - return $this->_element->expectType($tag); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * |
|
| 641 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 642 | - * @return bool |
|
| 643 | - */ |
|
| 644 | - public function isTagged(): bool |
|
| 645 | - { |
|
| 646 | - return $this->_element->isTagged(); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * |
|
| 651 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 652 | - * type strictness. |
|
| 653 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 654 | - * @return TaggedType |
|
| 655 | - */ |
|
| 656 | - public function expectTagged($tag = null): TaggedType |
|
| 657 | - { |
|
| 658 | - return $this->_element->expectTagged($tag); |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * |
|
| 663 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 664 | - * @return Element |
|
| 665 | - */ |
|
| 666 | - public function asElement(): Element |
|
| 667 | - { |
|
| 668 | - return $this->_element; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - /** |
|
| 672 | - * |
|
| 673 | - * {@inheritdoc} |
|
| 674 | - * @return UnspecifiedType |
|
| 675 | - */ |
|
| 676 | - public function asUnspecified(): UnspecifiedType |
|
| 677 | - { |
|
| 678 | - return $this; |
|
| 679 | - } |
|
| 19 | + /** |
|
| 20 | + * The wrapped element. |
|
| 21 | + * |
|
| 22 | + * @var Element |
|
| 23 | + */ |
|
| 24 | + private $_element; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param Element $el |
|
| 30 | + */ |
|
| 31 | + public function __construct(Element $el) |
|
| 32 | + { |
|
| 33 | + $this->_element = $el; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Initialize from DER data. |
|
| 38 | + * |
|
| 39 | + * @param string $data DER encoded data |
|
| 40 | + * @return self |
|
| 41 | + */ |
|
| 42 | + public static function fromDER(string $data): self |
|
| 43 | + { |
|
| 44 | + return Element::fromDER($data)->asUnspecified(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Initialize from ElementBase interface. |
|
| 49 | + * |
|
| 50 | + * @param ElementBase $el |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + public static function fromElementBase(ElementBase $el): self |
|
| 54 | + { |
|
| 55 | + // if element is already wrapped |
|
| 56 | + if ($el instanceof self) { |
|
| 57 | + return $el; |
|
| 58 | + } |
|
| 59 | + return new self($el->asElement()); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Compatibility method to dispatch calls to the wrapped element. |
|
| 64 | + * |
|
| 65 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 66 | + * @param string $mtd Method name |
|
| 67 | + * @param array $args Arguments |
|
| 68 | + * @return mixed |
|
| 69 | + */ |
|
| 70 | + public function __call($mtd, array $args) |
|
| 71 | + { |
|
| 72 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get the wrapped element as a context specific tagged type. |
|
| 77 | + * |
|
| 78 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 79 | + * @return TaggedType |
|
| 80 | + */ |
|
| 81 | + public function asTagged(): TaggedType |
|
| 82 | + { |
|
| 83 | + if (!$this->_element instanceof TaggedType) { |
|
| 84 | + throw new \UnexpectedValueException( |
|
| 85 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 86 | + } |
|
| 87 | + return $this->_element; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get the wrapped element as an application specific type. |
|
| 92 | + * |
|
| 93 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 94 | + * @return \ASN1\Type\Tagged\ApplicationType |
|
| 95 | + */ |
|
| 96 | + public function asApplication(): Tagged\ApplicationType |
|
| 97 | + { |
|
| 98 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 99 | + throw new \UnexpectedValueException( |
|
| 100 | + "Application type expected, got " . |
|
| 101 | + $this->_typeDescriptorString()); |
|
| 102 | + } |
|
| 103 | + return $this->_element; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Get the wrapped element as a private tagged type. |
|
| 108 | + * |
|
| 109 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
| 110 | + * @return \ASN1\Type\Tagged\PrivateType |
|
| 111 | + */ |
|
| 112 | + public function asPrivate(): Tagged\PrivateType |
|
| 113 | + { |
|
| 114 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 115 | + throw new \UnexpectedValueException( |
|
| 116 | + "Private type expected, got " . $this->_typeDescriptorString()); |
|
| 117 | + } |
|
| 118 | + return $this->_element; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get the wrapped element as a boolean type. |
|
| 123 | + * |
|
| 124 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 125 | + * @return \ASN1\Type\Primitive\Boolean |
|
| 126 | + */ |
|
| 127 | + public function asBoolean(): Primitive\Boolean |
|
| 128 | + { |
|
| 129 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 130 | + throw new \UnexpectedValueException( |
|
| 131 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 132 | + } |
|
| 133 | + return $this->_element; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Get the wrapped element as an integer type. |
|
| 138 | + * |
|
| 139 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 140 | + * @return \ASN1\Type\Primitive\Integer |
|
| 141 | + */ |
|
| 142 | + public function asInteger(): Primitive\Integer |
|
| 143 | + { |
|
| 144 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 145 | + throw new \UnexpectedValueException( |
|
| 146 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 147 | + } |
|
| 148 | + return $this->_element; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get the wrapped element as a bit string type. |
|
| 153 | + * |
|
| 154 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 155 | + * @return \ASN1\Type\Primitive\BitString |
|
| 156 | + */ |
|
| 157 | + public function asBitString(): Primitive\BitString |
|
| 158 | + { |
|
| 159 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 160 | + throw new \UnexpectedValueException( |
|
| 161 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 162 | + } |
|
| 163 | + return $this->_element; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get the wrapped element as an octet string type. |
|
| 168 | + * |
|
| 169 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 170 | + * @return \ASN1\Type\Primitive\OctetString |
|
| 171 | + */ |
|
| 172 | + public function asOctetString(): Primitive\OctetString |
|
| 173 | + { |
|
| 174 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 175 | + throw new \UnexpectedValueException( |
|
| 176 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 177 | + } |
|
| 178 | + return $this->_element; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the wrapped element as a null type. |
|
| 183 | + * |
|
| 184 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 185 | + * @return \ASN1\Type\Primitive\NullType |
|
| 186 | + */ |
|
| 187 | + public function asNull(): Primitive\NullType |
|
| 188 | + { |
|
| 189 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 190 | + throw new \UnexpectedValueException( |
|
| 191 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 192 | + } |
|
| 193 | + return $this->_element; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Get the wrapped element as an object identifier type. |
|
| 198 | + * |
|
| 199 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 200 | + * identifier |
|
| 201 | + * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
| 202 | + */ |
|
| 203 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 204 | + { |
|
| 205 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 206 | + throw new \UnexpectedValueException( |
|
| 207 | + $this->_generateExceptionMessage( |
|
| 208 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 209 | + } |
|
| 210 | + return $this->_element; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Get the wrapped element as an object descriptor type. |
|
| 215 | + * |
|
| 216 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 217 | + * descriptor |
|
| 218 | + * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
| 219 | + */ |
|
| 220 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 221 | + { |
|
| 222 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 223 | + throw new \UnexpectedValueException( |
|
| 224 | + $this->_generateExceptionMessage( |
|
| 225 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 226 | + } |
|
| 227 | + return $this->_element; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the wrapped element as a real type. |
|
| 232 | + * |
|
| 233 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 234 | + * @return \ASN1\Type\Primitive\Real |
|
| 235 | + */ |
|
| 236 | + public function asReal(): Primitive\Real |
|
| 237 | + { |
|
| 238 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 239 | + throw new \UnexpectedValueException( |
|
| 240 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 241 | + } |
|
| 242 | + return $this->_element; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Get the wrapped element as an enumerated type. |
|
| 247 | + * |
|
| 248 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 249 | + * @return \ASN1\Type\Primitive\Enumerated |
|
| 250 | + */ |
|
| 251 | + public function asEnumerated(): Primitive\Enumerated |
|
| 252 | + { |
|
| 253 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 254 | + throw new \UnexpectedValueException( |
|
| 255 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 256 | + } |
|
| 257 | + return $this->_element; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Get the wrapped element as a UTF8 string type. |
|
| 262 | + * |
|
| 263 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 264 | + * @return \ASN1\Type\Primitive\UTF8String |
|
| 265 | + */ |
|
| 266 | + public function asUTF8String(): Primitive\UTF8String |
|
| 267 | + { |
|
| 268 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 269 | + throw new \UnexpectedValueException( |
|
| 270 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 271 | + } |
|
| 272 | + return $this->_element; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Get the wrapped element as a relative OID type. |
|
| 277 | + * |
|
| 278 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 279 | + * @return \ASN1\Type\Primitive\RelativeOID |
|
| 280 | + */ |
|
| 281 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 282 | + { |
|
| 283 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 284 | + throw new \UnexpectedValueException( |
|
| 285 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 286 | + } |
|
| 287 | + return $this->_element; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Get the wrapped element as a sequence type. |
|
| 292 | + * |
|
| 293 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 294 | + * @return \ASN1\Type\Constructed\Sequence |
|
| 295 | + */ |
|
| 296 | + public function asSequence(): Constructed\Sequence |
|
| 297 | + { |
|
| 298 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 299 | + throw new \UnexpectedValueException( |
|
| 300 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 301 | + } |
|
| 302 | + return $this->_element; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Get the wrapped element as a set type. |
|
| 307 | + * |
|
| 308 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 309 | + * @return \ASN1\Type\Constructed\Set |
|
| 310 | + */ |
|
| 311 | + public function asSet(): Constructed\Set |
|
| 312 | + { |
|
| 313 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 314 | + throw new \UnexpectedValueException( |
|
| 315 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 316 | + } |
|
| 317 | + return $this->_element; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Get the wrapped element as a numeric string type. |
|
| 322 | + * |
|
| 323 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 324 | + * @return \ASN1\Type\Primitive\NumericString |
|
| 325 | + */ |
|
| 326 | + public function asNumericString(): Primitive\NumericString |
|
| 327 | + { |
|
| 328 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 329 | + throw new \UnexpectedValueException( |
|
| 330 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 331 | + } |
|
| 332 | + return $this->_element; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Get the wrapped element as a printable string type. |
|
| 337 | + * |
|
| 338 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 339 | + * string |
|
| 340 | + * @return \ASN1\Type\Primitive\PrintableString |
|
| 341 | + */ |
|
| 342 | + public function asPrintableString(): Primitive\PrintableString |
|
| 343 | + { |
|
| 344 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 345 | + throw new \UnexpectedValueException( |
|
| 346 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 347 | + } |
|
| 348 | + return $this->_element; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Get the wrapped element as a T61 string type. |
|
| 353 | + * |
|
| 354 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 355 | + * @return \ASN1\Type\Primitive\T61String |
|
| 356 | + */ |
|
| 357 | + public function asT61String(): Primitive\T61String |
|
| 358 | + { |
|
| 359 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 360 | + throw new \UnexpectedValueException( |
|
| 361 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 362 | + } |
|
| 363 | + return $this->_element; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Get the wrapped element as a videotex string type. |
|
| 368 | + * |
|
| 369 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 370 | + * @return \ASN1\Type\Primitive\VideotexString |
|
| 371 | + */ |
|
| 372 | + public function asVideotexString(): Primitive\VideotexString |
|
| 373 | + { |
|
| 374 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 375 | + throw new \UnexpectedValueException( |
|
| 376 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 377 | + } |
|
| 378 | + return $this->_element; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Get the wrapped element as a IA5 string type. |
|
| 383 | + * |
|
| 384 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 385 | + * @return \ASN1\Type\Primitive\IA5String |
|
| 386 | + */ |
|
| 387 | + public function asIA5String(): Primitive\IA5String |
|
| 388 | + { |
|
| 389 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 390 | + throw new \UnexpectedValueException( |
|
| 391 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 392 | + } |
|
| 393 | + return $this->_element; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get the wrapped element as an UTC time type. |
|
| 398 | + * |
|
| 399 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 400 | + * @return \ASN1\Type\Primitive\UTCTime |
|
| 401 | + */ |
|
| 402 | + public function asUTCTime(): Primitive\UTCTime |
|
| 403 | + { |
|
| 404 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 405 | + throw new \UnexpectedValueException( |
|
| 406 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 407 | + } |
|
| 408 | + return $this->_element; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Get the wrapped element as a generalized time type. |
|
| 413 | + * |
|
| 414 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 415 | + * time |
|
| 416 | + * @return \ASN1\Type\Primitive\GeneralizedTime |
|
| 417 | + */ |
|
| 418 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 419 | + { |
|
| 420 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 421 | + throw new \UnexpectedValueException( |
|
| 422 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 423 | + } |
|
| 424 | + return $this->_element; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Get the wrapped element as a graphic string type. |
|
| 429 | + * |
|
| 430 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 431 | + * @return \ASN1\Type\Primitive\GraphicString |
|
| 432 | + */ |
|
| 433 | + public function asGraphicString(): Primitive\GraphicString |
|
| 434 | + { |
|
| 435 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 436 | + throw new \UnexpectedValueException( |
|
| 437 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 438 | + } |
|
| 439 | + return $this->_element; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Get the wrapped element as a visible string type. |
|
| 444 | + * |
|
| 445 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 446 | + * @return \ASN1\Type\Primitive\VisibleString |
|
| 447 | + */ |
|
| 448 | + public function asVisibleString(): Primitive\VisibleString |
|
| 449 | + { |
|
| 450 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 451 | + throw new \UnexpectedValueException( |
|
| 452 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 453 | + } |
|
| 454 | + return $this->_element; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * Get the wrapped element as a general string type. |
|
| 459 | + * |
|
| 460 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 461 | + * @return \ASN1\Type\Primitive\GeneralString |
|
| 462 | + */ |
|
| 463 | + public function asGeneralString(): Primitive\GeneralString |
|
| 464 | + { |
|
| 465 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 466 | + throw new \UnexpectedValueException( |
|
| 467 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 468 | + } |
|
| 469 | + return $this->_element; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Get the wrapped element as a universal string type. |
|
| 474 | + * |
|
| 475 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 476 | + * string |
|
| 477 | + * @return \ASN1\Type\Primitive\UniversalString |
|
| 478 | + */ |
|
| 479 | + public function asUniversalString(): Primitive\UniversalString |
|
| 480 | + { |
|
| 481 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 482 | + throw new \UnexpectedValueException( |
|
| 483 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 484 | + } |
|
| 485 | + return $this->_element; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Get the wrapped element as a character string type. |
|
| 490 | + * |
|
| 491 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 492 | + * string |
|
| 493 | + * @return \ASN1\Type\Primitive\CharacterString |
|
| 494 | + */ |
|
| 495 | + public function asCharacterString(): Primitive\CharacterString |
|
| 496 | + { |
|
| 497 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 498 | + throw new \UnexpectedValueException( |
|
| 499 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 500 | + } |
|
| 501 | + return $this->_element; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Get the wrapped element as a BMP string type. |
|
| 506 | + * |
|
| 507 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 508 | + * @return \ASN1\Type\Primitive\BMPString |
|
| 509 | + */ |
|
| 510 | + public function asBMPString(): Primitive\BMPString |
|
| 511 | + { |
|
| 512 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 513 | + throw new \UnexpectedValueException( |
|
| 514 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 515 | + } |
|
| 516 | + return $this->_element; |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Get the wrapped element as any string type. |
|
| 521 | + * |
|
| 522 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 523 | + * @return StringType |
|
| 524 | + */ |
|
| 525 | + public function asString(): StringType |
|
| 526 | + { |
|
| 527 | + if (!$this->_element instanceof StringType) { |
|
| 528 | + throw new \UnexpectedValueException( |
|
| 529 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 530 | + } |
|
| 531 | + return $this->_element; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Get the wrapped element as any time type. |
|
| 536 | + * |
|
| 537 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 538 | + * @return TimeType |
|
| 539 | + */ |
|
| 540 | + public function asTime(): TimeType |
|
| 541 | + { |
|
| 542 | + if (!$this->_element instanceof TimeType) { |
|
| 543 | + throw new \UnexpectedValueException( |
|
| 544 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 545 | + } |
|
| 546 | + return $this->_element; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 551 | + * |
|
| 552 | + * @param int $tag Type tag of the expected element |
|
| 553 | + * @return string |
|
| 554 | + */ |
|
| 555 | + private function _generateExceptionMessage(int $tag): string |
|
| 556 | + { |
|
| 557 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 558 | + $this->_typeDescriptorString()); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 563 | + * |
|
| 564 | + * @return string |
|
| 565 | + */ |
|
| 566 | + private function _typeDescriptorString(): string |
|
| 567 | + { |
|
| 568 | + $type_cls = $this->_element->typeClass(); |
|
| 569 | + $tag = $this->_element->tag(); |
|
| 570 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 571 | + return Element::tagToName($tag); |
|
| 572 | + } |
|
| 573 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * |
|
| 578 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 579 | + * @return string |
|
| 580 | + */ |
|
| 581 | + public function toDER(): string |
|
| 582 | + { |
|
| 583 | + return $this->_element->toDER(); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * |
|
| 588 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 589 | + * @return int |
|
| 590 | + */ |
|
| 591 | + public function typeClass(): int |
|
| 592 | + { |
|
| 593 | + return $this->_element->typeClass(); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * |
|
| 598 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 599 | + * @return bool |
|
| 600 | + */ |
|
| 601 | + public function isConstructed(): bool |
|
| 602 | + { |
|
| 603 | + return $this->_element->isConstructed(); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * |
|
| 608 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 609 | + * @return int |
|
| 610 | + */ |
|
| 611 | + public function tag(): int |
|
| 612 | + { |
|
| 613 | + return $this->_element->tag(); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * |
|
| 618 | + * {@inheritdoc} |
|
| 619 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 620 | + * @return bool |
|
| 621 | + */ |
|
| 622 | + public function isType(int $tag): bool |
|
| 623 | + { |
|
| 624 | + return $this->_element->isType($tag); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * |
|
| 629 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 630 | + * type strictness. |
|
| 631 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 632 | + * @return ElementBase |
|
| 633 | + */ |
|
| 634 | + public function expectType(int $tag): ElementBase |
|
| 635 | + { |
|
| 636 | + return $this->_element->expectType($tag); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * |
|
| 641 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 642 | + * @return bool |
|
| 643 | + */ |
|
| 644 | + public function isTagged(): bool |
|
| 645 | + { |
|
| 646 | + return $this->_element->isTagged(); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * |
|
| 651 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 652 | + * type strictness. |
|
| 653 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 654 | + * @return TaggedType |
|
| 655 | + */ |
|
| 656 | + public function expectTagged($tag = null): TaggedType |
|
| 657 | + { |
|
| 658 | + return $this->_element->expectTagged($tag); |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * |
|
| 663 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 664 | + * @return Element |
|
| 665 | + */ |
|
| 666 | + public function asElement(): Element |
|
| 667 | + { |
|
| 668 | + return $this->_element; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + /** |
|
| 672 | + * |
|
| 673 | + * {@inheritdoc} |
|
| 674 | + * @return UnspecifiedType |
|
| 675 | + */ |
|
| 676 | + public function asUnspecified(): UnspecifiedType |
|
| 677 | + { |
|
| 678 | + return $this; |
|
| 679 | + } |
|
| 680 | 680 | } |