@@ -11,27 +11,27 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class TaggedTypeWrap extends TaggedType |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Wrapped element. |
|
| 16 | - * |
|
| 17 | - * @var \ASN1\Element $_element |
|
| 18 | - */ |
|
| 19 | - protected $_element; |
|
| 14 | + /** |
|
| 15 | + * Wrapped element. |
|
| 16 | + * |
|
| 17 | + * @var \ASN1\Element $_element |
|
| 18 | + */ |
|
| 19 | + protected $_element; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Type class. |
|
| 23 | - * |
|
| 24 | - * @var int |
|
| 25 | - */ |
|
| 26 | - protected $_class; |
|
| 21 | + /** |
|
| 22 | + * Type class. |
|
| 23 | + * |
|
| 24 | + * @var int |
|
| 25 | + */ |
|
| 26 | + protected $_class; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * @see \ASN1\Element::typeClass() |
|
| 31 | - * @return int |
|
| 32 | - */ |
|
| 33 | - public function typeClass(): int |
|
| 34 | - { |
|
| 35 | - return $this->_class; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * @see \ASN1\Element::typeClass() |
|
| 31 | + * @return int |
|
| 32 | + */ |
|
| 33 | + public function typeClass(): int |
|
| 34 | + { |
|
| 35 | + return $this->_class; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -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 | |
@@ -274,7 +274,7 @@ |
||
| 274 | 274 | /** |
| 275 | 275 | * Test that number is valid for this context. |
| 276 | 276 | * |
| 277 | - * @param mixed $num |
|
| 277 | + * @param string $num |
|
| 278 | 278 | * @return bool |
| 279 | 279 | */ |
| 280 | 280 | private static function _validateNumber($num): bool |
@@ -17,271 +17,271 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Real extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 23 | + /** |
|
| 24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Regex pattern to parse PHP exponent number format. |
|
| 32 | - * |
|
| 33 | - * @link http://php.net/manual/en/language.types.float.php |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | - '([+\-]?'. // sign |
|
| 38 | - '(?:'. |
|
| 39 | - '\d+'. // LNUM |
|
| 40 | - '|'. |
|
| 41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | - '))[eE]'. |
|
| 43 | - '([+\-]?\d+)'. // exponent |
|
| 44 | - '$/'; /* @formatter:on */ |
|
| 30 | + /** |
|
| 31 | + * Regex pattern to parse PHP exponent number format. |
|
| 32 | + * |
|
| 33 | + * @link http://php.net/manual/en/language.types.float.php |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | + '([+\-]?'. // sign |
|
| 38 | + '(?:'. |
|
| 39 | + '\d+'. // LNUM |
|
| 40 | + '|'. |
|
| 41 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | + '))[eE]'. |
|
| 43 | + '([+\-]?\d+)'. // exponent |
|
| 44 | + '$/'; /* @formatter:on */ |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Number zero represented in NR3 form. |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - const NR3_ZERO = ".E+0"; |
|
| 46 | + /** |
|
| 47 | + * Number zero represented in NR3 form. |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + const NR3_ZERO = ".E+0"; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Number in NR3 form. |
|
| 55 | - * |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $_number; |
|
| 53 | + /** |
|
| 54 | + * Number in NR3 form. |
|
| 55 | + * |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $_number; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Constructor. |
|
| 62 | - * |
|
| 63 | - * @param string $number Number in NR3 form. |
|
| 64 | - */ |
|
| 65 | - public function __construct(string $number) |
|
| 66 | - { |
|
| 67 | - $this->_typeTag = self::TYPE_REAL; |
|
| 68 | - if (!self::_validateNumber($number)) { |
|
| 69 | - throw new \InvalidArgumentException( |
|
| 70 | - "'$number' is not a valid NR3 form real."); |
|
| 71 | - } |
|
| 72 | - $this->_number = $number; |
|
| 73 | - } |
|
| 60 | + /** |
|
| 61 | + * Constructor. |
|
| 62 | + * |
|
| 63 | + * @param string $number Number in NR3 form. |
|
| 64 | + */ |
|
| 65 | + public function __construct(string $number) |
|
| 66 | + { |
|
| 67 | + $this->_typeTag = self::TYPE_REAL; |
|
| 68 | + if (!self::_validateNumber($number)) { |
|
| 69 | + throw new \InvalidArgumentException( |
|
| 70 | + "'$number' is not a valid NR3 form real."); |
|
| 71 | + } |
|
| 72 | + $this->_number = $number; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Initialize from float. |
|
| 77 | - * |
|
| 78 | - * @param float $number |
|
| 79 | - * @return self |
|
| 80 | - */ |
|
| 81 | - public static function fromFloat(float $number): self |
|
| 82 | - { |
|
| 83 | - return new self(self::_decimalToNR3(strval($number))); |
|
| 84 | - } |
|
| 75 | + /** |
|
| 76 | + * Initialize from float. |
|
| 77 | + * |
|
| 78 | + * @param float $number |
|
| 79 | + * @return self |
|
| 80 | + */ |
|
| 81 | + public static function fromFloat(float $number): self |
|
| 82 | + { |
|
| 83 | + return new self(self::_decimalToNR3(strval($number))); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get number as a float. |
|
| 88 | - * |
|
| 89 | - * @return float |
|
| 90 | - */ |
|
| 91 | - public function float(): float |
|
| 92 | - { |
|
| 93 | - return self::_nr3ToDecimal($this->_number); |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Get number as a float. |
|
| 88 | + * |
|
| 89 | + * @return float |
|
| 90 | + */ |
|
| 91 | + public function float(): float |
|
| 92 | + { |
|
| 93 | + return self::_nr3ToDecimal($this->_number); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * |
|
| 98 | - * {@inheritdoc} |
|
| 99 | - */ |
|
| 100 | - protected function _encodedContentDER(): string |
|
| 101 | - { |
|
| 102 | - /* if the real value is the value zero, there shall be no contents |
|
| 96 | + /** |
|
| 97 | + * |
|
| 98 | + * {@inheritdoc} |
|
| 99 | + */ |
|
| 100 | + protected function _encodedContentDER(): string |
|
| 101 | + { |
|
| 102 | + /* if the real value is the value zero, there shall be no contents |
|
| 103 | 103 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
| 104 | - if (self::NR3_ZERO == $this->_number) { |
|
| 105 | - return ""; |
|
| 106 | - } |
|
| 107 | - // encode in NR3 decimal encoding |
|
| 108 | - $data = chr(0x03) . $this->_number; |
|
| 109 | - return $data; |
|
| 110 | - } |
|
| 104 | + if (self::NR3_ZERO == $this->_number) { |
|
| 105 | + return ""; |
|
| 106 | + } |
|
| 107 | + // encode in NR3 decimal encoding |
|
| 108 | + $data = chr(0x03) . $this->_number; |
|
| 109 | + return $data; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 118 | - int &$offset): ElementBase |
|
| 119 | - { |
|
| 120 | - $idx = $offset; |
|
| 121 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 122 | - // if length is zero, value is zero (spec 8.5.2) |
|
| 123 | - if (!$length) { |
|
| 124 | - $obj = new self(self::NR3_ZERO); |
|
| 125 | - } else { |
|
| 126 | - $bytes = substr($data, $idx, $length); |
|
| 127 | - $byte = ord($bytes[0]); |
|
| 128 | - if (0x80 & $byte) { // bit 8 = 1 |
|
| 129 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
| 130 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 131 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
| 132 | - } else { // bit 8 = 0, bit 7 = 1 |
|
| 133 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - $offset = $idx + $length; |
|
| 137 | - return $obj; |
|
| 138 | - } |
|
| 112 | + /** |
|
| 113 | + * |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 118 | + int &$offset): ElementBase |
|
| 119 | + { |
|
| 120 | + $idx = $offset; |
|
| 121 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 122 | + // if length is zero, value is zero (spec 8.5.2) |
|
| 123 | + if (!$length) { |
|
| 124 | + $obj = new self(self::NR3_ZERO); |
|
| 125 | + } else { |
|
| 126 | + $bytes = substr($data, $idx, $length); |
|
| 127 | + $byte = ord($bytes[0]); |
|
| 128 | + if (0x80 & $byte) { // bit 8 = 1 |
|
| 129 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
| 130 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 131 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
| 132 | + } else { // bit 8 = 0, bit 7 = 1 |
|
| 133 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + $offset = $idx + $length; |
|
| 137 | + return $obj; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * |
|
| 142 | - * @todo Implement |
|
| 143 | - * @param string $data |
|
| 144 | - */ |
|
| 145 | - protected static function _decodeBinaryEncoding(string $data) |
|
| 146 | - { |
|
| 147 | - throw new \RuntimeException( |
|
| 148 | - "Binary encoding of REAL is not implemented."); |
|
| 149 | - } |
|
| 140 | + /** |
|
| 141 | + * |
|
| 142 | + * @todo Implement |
|
| 143 | + * @param string $data |
|
| 144 | + */ |
|
| 145 | + protected static function _decodeBinaryEncoding(string $data) |
|
| 146 | + { |
|
| 147 | + throw new \RuntimeException( |
|
| 148 | + "Binary encoding of REAL is not implemented."); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * |
|
| 153 | - * @param string $data |
|
| 154 | - * @throws \RuntimeException |
|
| 155 | - * @return \ASN1\Type\Primitive\Real |
|
| 156 | - */ |
|
| 157 | - protected static function _decodeDecimalEncoding(string $data): Real |
|
| 158 | - { |
|
| 159 | - $nr = ord($data[0]) & 0x03; |
|
| 160 | - if ($nr != 0x03) { |
|
| 161 | - throw new \RuntimeException("Only NR3 form supported."); |
|
| 162 | - } |
|
| 163 | - $str = substr($data, 1); |
|
| 164 | - return new self($str); |
|
| 165 | - } |
|
| 151 | + /** |
|
| 152 | + * |
|
| 153 | + * @param string $data |
|
| 154 | + * @throws \RuntimeException |
|
| 155 | + * @return \ASN1\Type\Primitive\Real |
|
| 156 | + */ |
|
| 157 | + protected static function _decodeDecimalEncoding(string $data): Real |
|
| 158 | + { |
|
| 159 | + $nr = ord($data[0]) & 0x03; |
|
| 160 | + if ($nr != 0x03) { |
|
| 161 | + throw new \RuntimeException("Only NR3 form supported."); |
|
| 162 | + } |
|
| 163 | + $str = substr($data, 1); |
|
| 164 | + return new self($str); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * |
|
| 169 | - * @todo Implement |
|
| 170 | - * @param string $data |
|
| 171 | - */ |
|
| 172 | - protected static function _decodeSpecialRealValue(string $data) |
|
| 173 | - { |
|
| 174 | - if (strlen($data) != 1) { |
|
| 175 | - throw new DecodeException( |
|
| 176 | - "SpecialRealValue must have one content octet."); |
|
| 177 | - } |
|
| 178 | - $byte = ord($data[0]); |
|
| 179 | - if ($byte == 0x40) { // positive infinity |
|
| 180 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 181 | - } else if ($byte == 0x41) { // negative infinity |
|
| 182 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 183 | - } else { |
|
| 184 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 185 | - } |
|
| 186 | - } |
|
| 167 | + /** |
|
| 168 | + * |
|
| 169 | + * @todo Implement |
|
| 170 | + * @param string $data |
|
| 171 | + */ |
|
| 172 | + protected static function _decodeSpecialRealValue(string $data) |
|
| 173 | + { |
|
| 174 | + if (strlen($data) != 1) { |
|
| 175 | + throw new DecodeException( |
|
| 176 | + "SpecialRealValue must have one content octet."); |
|
| 177 | + } |
|
| 178 | + $byte = ord($data[0]); |
|
| 179 | + if ($byte == 0x40) { // positive infinity |
|
| 180 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 181 | + } else if ($byte == 0x41) { // negative infinity |
|
| 182 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 183 | + } else { |
|
| 184 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Convert decimal number string to NR3 form. |
|
| 190 | - * |
|
| 191 | - * @param string $str |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - private static function _decimalToNR3(string $str): string |
|
| 195 | - { |
|
| 196 | - // if number is in exponent form |
|
| 197 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 198 | - $parts = explode(".", $match[1]); |
|
| 199 | - $m = ltrim($parts[0], "0"); |
|
| 200 | - $e = intval($match[2]); |
|
| 201 | - // if mantissa had decimals |
|
| 202 | - if (count($parts) == 2) { |
|
| 203 | - $d = rtrim($parts[1], "0"); |
|
| 204 | - $e -= strlen($d); |
|
| 205 | - $m .= $d; |
|
| 206 | - } |
|
| 207 | - } else { |
|
| 208 | - // explode from decimal |
|
| 209 | - $parts = explode(".", $str); |
|
| 210 | - $m = ltrim($parts[0], "0"); |
|
| 211 | - // if number had decimals |
|
| 212 | - if (count($parts) == 2) { |
|
| 213 | - // exponent is negative number of the decimals |
|
| 214 | - $e = -strlen($parts[1]); |
|
| 215 | - // append decimals to the mantissa |
|
| 216 | - $m .= $parts[1]; |
|
| 217 | - } else { |
|
| 218 | - $e = 0; |
|
| 219 | - } |
|
| 220 | - // shift trailing zeroes from the mantissa to the exponent |
|
| 221 | - while (substr($m, -1) === "0") { |
|
| 222 | - $e++; |
|
| 223 | - $m = substr($m, 0, -1); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 188 | + /** |
|
| 189 | + * Convert decimal number string to NR3 form. |
|
| 190 | + * |
|
| 191 | + * @param string $str |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + private static function _decimalToNR3(string $str): string |
|
| 195 | + { |
|
| 196 | + // if number is in exponent form |
|
| 197 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 198 | + $parts = explode(".", $match[1]); |
|
| 199 | + $m = ltrim($parts[0], "0"); |
|
| 200 | + $e = intval($match[2]); |
|
| 201 | + // if mantissa had decimals |
|
| 202 | + if (count($parts) == 2) { |
|
| 203 | + $d = rtrim($parts[1], "0"); |
|
| 204 | + $e -= strlen($d); |
|
| 205 | + $m .= $d; |
|
| 206 | + } |
|
| 207 | + } else { |
|
| 208 | + // explode from decimal |
|
| 209 | + $parts = explode(".", $str); |
|
| 210 | + $m = ltrim($parts[0], "0"); |
|
| 211 | + // if number had decimals |
|
| 212 | + if (count($parts) == 2) { |
|
| 213 | + // exponent is negative number of the decimals |
|
| 214 | + $e = -strlen($parts[1]); |
|
| 215 | + // append decimals to the mantissa |
|
| 216 | + $m .= $parts[1]; |
|
| 217 | + } else { |
|
| 218 | + $e = 0; |
|
| 219 | + } |
|
| 220 | + // shift trailing zeroes from the mantissa to the exponent |
|
| 221 | + while (substr($m, -1) === "0") { |
|
| 222 | + $e++; |
|
| 223 | + $m = substr($m, 0, -1); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 227 | 227 | (X.690 07-2002, section 11.3.2.6) */ |
| 228 | - if (0 == $e) { |
|
| 229 | - $es = "+"; |
|
| 230 | - } else { |
|
| 231 | - $es = $e < 0 ? "-" : ""; |
|
| 232 | - } |
|
| 233 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 234 | - } |
|
| 228 | + if (0 == $e) { |
|
| 229 | + $es = "+"; |
|
| 230 | + } else { |
|
| 231 | + $es = $e < 0 ? "-" : ""; |
|
| 232 | + } |
|
| 233 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Convert NR3 form number to decimal. |
|
| 238 | - * |
|
| 239 | - * @param string $str |
|
| 240 | - * @throws \UnexpectedValueException |
|
| 241 | - * @return float |
|
| 242 | - */ |
|
| 243 | - private static function _nr3ToDecimal(string $str): float |
|
| 244 | - { |
|
| 245 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 246 | - throw new \UnexpectedValueException( |
|
| 247 | - "'$str' is not a valid NR3 form real."); |
|
| 248 | - } |
|
| 249 | - $m = $match[2]; |
|
| 250 | - // if number started with minus sign |
|
| 251 | - $inv = $match[1] == "-"; |
|
| 252 | - $e = intval($match[3]); |
|
| 253 | - // positive exponent |
|
| 254 | - if ($e > 0) { |
|
| 255 | - // pad with trailing zeroes |
|
| 256 | - $num = $m . str_repeat("0", $e); |
|
| 257 | - } else if ($e < 0) { |
|
| 258 | - // pad with leading zeroes |
|
| 259 | - if (strlen($m) < abs($e)) { |
|
| 260 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 261 | - } |
|
| 262 | - // insert decimal point |
|
| 263 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 264 | - } else { |
|
| 265 | - $num = empty($m) ? "0" : $m; |
|
| 266 | - } |
|
| 267 | - // if number is negative |
|
| 268 | - if ($inv) { |
|
| 269 | - $num = "-$num"; |
|
| 270 | - } |
|
| 271 | - return floatval($num); |
|
| 272 | - } |
|
| 236 | + /** |
|
| 237 | + * Convert NR3 form number to decimal. |
|
| 238 | + * |
|
| 239 | + * @param string $str |
|
| 240 | + * @throws \UnexpectedValueException |
|
| 241 | + * @return float |
|
| 242 | + */ |
|
| 243 | + private static function _nr3ToDecimal(string $str): float |
|
| 244 | + { |
|
| 245 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 246 | + throw new \UnexpectedValueException( |
|
| 247 | + "'$str' is not a valid NR3 form real."); |
|
| 248 | + } |
|
| 249 | + $m = $match[2]; |
|
| 250 | + // if number started with minus sign |
|
| 251 | + $inv = $match[1] == "-"; |
|
| 252 | + $e = intval($match[3]); |
|
| 253 | + // positive exponent |
|
| 254 | + if ($e > 0) { |
|
| 255 | + // pad with trailing zeroes |
|
| 256 | + $num = $m . str_repeat("0", $e); |
|
| 257 | + } else if ($e < 0) { |
|
| 258 | + // pad with leading zeroes |
|
| 259 | + if (strlen($m) < abs($e)) { |
|
| 260 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 261 | + } |
|
| 262 | + // insert decimal point |
|
| 263 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 264 | + } else { |
|
| 265 | + $num = empty($m) ? "0" : $m; |
|
| 266 | + } |
|
| 267 | + // if number is negative |
|
| 268 | + if ($inv) { |
|
| 269 | + $num = "-$num"; |
|
| 270 | + } |
|
| 271 | + return floatval($num); |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Test that number is valid for this context. |
|
| 276 | - * |
|
| 277 | - * @param mixed $num |
|
| 278 | - * @return bool |
|
| 279 | - */ |
|
| 280 | - private static function _validateNumber($num): bool |
|
| 281 | - { |
|
| 282 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 283 | - return false; |
|
| 284 | - } |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 274 | + /** |
|
| 275 | + * Test that number is valid for this context. |
|
| 276 | + * |
|
| 277 | + * @param mixed $num |
|
| 278 | + * @return bool |
|
| 279 | + */ |
|
| 280 | + private static function _validateNumber($num): bool |
|
| 281 | + { |
|
| 282 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 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 | |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | * @link http://php.net/manual/en/language.types.float.php |
| 34 | 34 | * @var string |
| 35 | 35 | */ |
| 36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | - '([+\-]?'. // sign |
|
| 38 | - '(?:'. |
|
| 39 | - '\d+'. // LNUM |
|
| 40 | - '|'. |
|
| 41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | - '))[eE]'. |
|
| 43 | - '([+\-]?\d+)'. // exponent |
|
| 36 | + const PHP_EXPONENT_DNUM = '/^' . /* @formatter:off */ |
|
| 37 | + '([+\-]?' . // sign |
|
| 38 | + '(?:' . |
|
| 39 | + '\d+' . // LNUM |
|
| 40 | + '|' . |
|
| 41 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
| 42 | + '))[eE]' . |
|
| 43 | + '([+\-]?\d+)' . // exponent |
|
| 44 | 44 | '$/'; /* @formatter:on */ |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -13,17 +13,17 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ApplicationType extends DERTaggedType |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 21 | - int &$offset): ElementBase |
|
| 22 | - { |
|
| 23 | - $idx = $offset; |
|
| 24 | - $type = new self($identifier, $data, $idx); |
|
| 25 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 26 | - $offset = $idx + $length; |
|
| 27 | - return $type; |
|
| 28 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 21 | + int &$offset): ElementBase |
|
| 22 | + { |
|
| 23 | + $idx = $offset; |
|
| 24 | + $type = new self($identifier, $data, $idx); |
|
| 25 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 26 | + $offset = $idx + $length; |
|
| 27 | + return $type; |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -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 | |
@@ -12,15 +12,15 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | interface ExplicitTagging extends ElementBase |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Get explicitly tagged wrapped element. |
|
| 17 | - * |
|
| 18 | - * <strong>NOTE!</strong> Expectation checking is deprecated and shall be |
|
| 19 | - * done with <code>UnspecifiedType</code>. |
|
| 20 | - * |
|
| 21 | - * @param int|null $expectedTag Expected tag of the underlying type |
|
| 22 | - * @throws \UnexpectedValueException If expectation fails |
|
| 23 | - * @return \ASN1\Type\UnspecifiedType |
|
| 24 | - */ |
|
| 25 | - public function explicit($expectedTag = null): UnspecifiedType; |
|
| 15 | + /** |
|
| 16 | + * Get explicitly tagged wrapped element. |
|
| 17 | + * |
|
| 18 | + * <strong>NOTE!</strong> Expectation checking is deprecated and shall be |
|
| 19 | + * done with <code>UnspecifiedType</code>. |
|
| 20 | + * |
|
| 21 | + * @param int|null $expectedTag Expected tag of the underlying type |
|
| 22 | + * @throws \UnexpectedValueException If expectation fails |
|
| 23 | + * @return \ASN1\Type\UnspecifiedType |
|
| 24 | + */ |
|
| 25 | + public function explicit($expectedTag = null): UnspecifiedType; |
|
| 26 | 26 | } |
@@ -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 | |
@@ -13,17 +13,17 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class PrivateType extends DERTaggedType |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 21 | - int &$offset): ElementBase |
|
| 22 | - { |
|
| 23 | - $idx = $offset; |
|
| 24 | - $type = new self($identifier, $data, $idx); |
|
| 25 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 26 | - $offset = $idx + $length; |
|
| 27 | - return $type; |
|
| 28 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 21 | + int &$offset): ElementBase |
|
| 22 | + { |
|
| 23 | + $idx = $offset; |
|
| 24 | + $type = new self($identifier, $data, $idx); |
|
| 25 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 26 | + $offset = $idx + $length; |
|
| 27 | + return $type; |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -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 | |
@@ -17,194 +17,194 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class BitString extends StringType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Number of unused bits in the last octet. |
|
| 25 | - * |
|
| 26 | - * @var int $_unusedBits |
|
| 27 | - */ |
|
| 28 | - protected $_unusedBits; |
|
| 23 | + /** |
|
| 24 | + * Number of unused bits in the last octet. |
|
| 25 | + * |
|
| 26 | + * @var int $_unusedBits |
|
| 27 | + */ |
|
| 28 | + protected $_unusedBits; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $string Content octets |
|
| 34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | - */ |
|
| 36 | - public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | - { |
|
| 38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | - parent::__construct($string); |
|
| 40 | - $this->_unusedBits = $unused_bits; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $string Content octets |
|
| 34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | + */ |
|
| 36 | + public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | + { |
|
| 38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | + parent::__construct($string); |
|
| 40 | + $this->_unusedBits = $unused_bits; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the number of bits in the string. |
|
| 45 | - * |
|
| 46 | - * @return int |
|
| 47 | - */ |
|
| 48 | - public function numBits(): int |
|
| 49 | - { |
|
| 50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Get the number of bits in the string. |
|
| 45 | + * |
|
| 46 | + * @return int |
|
| 47 | + */ |
|
| 48 | + public function numBits(): int |
|
| 49 | + { |
|
| 50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Get the number of unused bits in the last octet of the string. |
|
| 55 | - * |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public function unusedBits(): int |
|
| 59 | - { |
|
| 60 | - return $this->_unusedBits; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Get the number of unused bits in the last octet of the string. |
|
| 55 | + * |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public function unusedBits(): int |
|
| 59 | + { |
|
| 60 | + return $this->_unusedBits; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Test whether bit is set. |
|
| 65 | - * |
|
| 66 | - * @param int $idx Bit index. |
|
| 67 | - * Most significant bit of the first octet is index 0. |
|
| 68 | - * @return bool |
|
| 69 | - */ |
|
| 70 | - public function testBit(int $idx): bool |
|
| 71 | - { |
|
| 72 | - // octet index |
|
| 73 | - $oi = (int) floor($idx / 8); |
|
| 74 | - // if octet is outside range |
|
| 75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 77 | - } |
|
| 78 | - // bit index |
|
| 79 | - $bi = $idx % 8; |
|
| 80 | - // if tested bit is last octet's unused bit |
|
| 81 | - if ($oi == strlen($this->_string) - 1) { |
|
| 82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | - throw new \OutOfBoundsException("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 | - } |
|
| 63 | + /** |
|
| 64 | + * Test whether bit is set. |
|
| 65 | + * |
|
| 66 | + * @param int $idx Bit index. |
|
| 67 | + * Most significant bit of the first octet is index 0. |
|
| 68 | + * @return bool |
|
| 69 | + */ |
|
| 70 | + public function testBit(int $idx): bool |
|
| 71 | + { |
|
| 72 | + // octet index |
|
| 73 | + $oi = (int) floor($idx / 8); |
|
| 74 | + // if octet is outside range |
|
| 75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 77 | + } |
|
| 78 | + // bit index |
|
| 79 | + $bi = $idx % 8; |
|
| 80 | + // if tested bit is last octet's unused bit |
|
| 81 | + if ($oi == strlen($this->_string) - 1) { |
|
| 82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | + throw new \OutOfBoundsException("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, string $data, |
|
| 182 | - 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, string $data, |
|
| 182 | + 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 | |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class IA5String extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_IA5_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_IA5_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - return preg_match('/[^\x01-\x7f]/', $string) == 0; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + return preg_match('/[^\x01-\x7f]/', $string) == 0; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -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 | |
@@ -11,88 +11,88 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class TimeType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * UTC timezone. |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - const TZ_UTC = "UTC"; |
|
| 14 | + /** |
|
| 15 | + * UTC timezone. |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + const TZ_UTC = "UTC"; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Date and time. |
|
| 23 | - * |
|
| 24 | - * @var \DateTimeImmutable $_dateTime |
|
| 25 | - */ |
|
| 26 | - protected $_dateTime; |
|
| 21 | + /** |
|
| 22 | + * Date and time. |
|
| 23 | + * |
|
| 24 | + * @var \DateTimeImmutable $_dateTime |
|
| 25 | + */ |
|
| 26 | + protected $_dateTime; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param \DateTimeImmutable $dt |
|
| 32 | - */ |
|
| 33 | - public function __construct(\DateTimeImmutable $dt) |
|
| 34 | - { |
|
| 35 | - $this->_dateTime = $dt; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param \DateTimeImmutable $dt |
|
| 32 | + */ |
|
| 33 | + public function __construct(\DateTimeImmutable $dt) |
|
| 34 | + { |
|
| 35 | + $this->_dateTime = $dt; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Initialize from datetime string. |
|
| 40 | - * |
|
| 41 | - * @link http://php.net/manual/en/datetime.formats.php |
|
| 42 | - * @param string $time Time string |
|
| 43 | - * @param string|null $tz Timezone, if null use default. |
|
| 44 | - * @throws \RuntimeException |
|
| 45 | - * @return self |
|
| 46 | - */ |
|
| 47 | - public static function fromString(string $time, string $tz = null): self |
|
| 48 | - { |
|
| 49 | - try { |
|
| 50 | - if (!isset($tz)) { |
|
| 51 | - $tz = date_default_timezone_get(); |
|
| 52 | - } |
|
| 53 | - return new static( |
|
| 54 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
| 55 | - } catch (\Exception $e) { |
|
| 56 | - throw new \RuntimeException( |
|
| 57 | - "Failed to create DateTime: " . |
|
| 58 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 59 | - } |
|
| 60 | - } |
|
| 38 | + /** |
|
| 39 | + * Initialize from datetime string. |
|
| 40 | + * |
|
| 41 | + * @link http://php.net/manual/en/datetime.formats.php |
|
| 42 | + * @param string $time Time string |
|
| 43 | + * @param string|null $tz Timezone, if null use default. |
|
| 44 | + * @throws \RuntimeException |
|
| 45 | + * @return self |
|
| 46 | + */ |
|
| 47 | + public static function fromString(string $time, string $tz = null): self |
|
| 48 | + { |
|
| 49 | + try { |
|
| 50 | + if (!isset($tz)) { |
|
| 51 | + $tz = date_default_timezone_get(); |
|
| 52 | + } |
|
| 53 | + return new static( |
|
| 54 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
| 55 | + } catch (\Exception $e) { |
|
| 56 | + throw new \RuntimeException( |
|
| 57 | + "Failed to create DateTime: " . |
|
| 58 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get the date and time. |
|
| 64 | - * |
|
| 65 | - * @return \DateTimeImmutable |
|
| 66 | - */ |
|
| 67 | - public function dateTime(): \DateTimeImmutable |
|
| 68 | - { |
|
| 69 | - return $this->_dateTime; |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * Get the date and time. |
|
| 64 | + * |
|
| 65 | + * @return \DateTimeImmutable |
|
| 66 | + */ |
|
| 67 | + public function dateTime(): \DateTimeImmutable |
|
| 68 | + { |
|
| 69 | + return $this->_dateTime; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Create DateTimeZone object from string. |
|
| 74 | - * |
|
| 75 | - * @param string $tz |
|
| 76 | - * @throws \UnexpectedValueException If timezone is invalid |
|
| 77 | - * @return \DateTimeZone |
|
| 78 | - */ |
|
| 79 | - protected static function _createTimeZone(string $tz): \DateTimeZone |
|
| 80 | - { |
|
| 81 | - try { |
|
| 82 | - return new \DateTimeZone($tz); |
|
| 83 | - } catch (\Exception $e) { |
|
| 84 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 85 | - } |
|
| 86 | - } |
|
| 72 | + /** |
|
| 73 | + * Create DateTimeZone object from string. |
|
| 74 | + * |
|
| 75 | + * @param string $tz |
|
| 76 | + * @throws \UnexpectedValueException If timezone is invalid |
|
| 77 | + * @return \DateTimeZone |
|
| 78 | + */ |
|
| 79 | + protected static function _createTimeZone(string $tz): \DateTimeZone |
|
| 80 | + { |
|
| 81 | + try { |
|
| 82 | + return new \DateTimeZone($tz); |
|
| 83 | + } catch (\Exception $e) { |
|
| 84 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get last error caused by DateTimeImmutable. |
|
| 90 | - * |
|
| 91 | - * @return string |
|
| 92 | - */ |
|
| 93 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 94 | - { |
|
| 95 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 96 | - return implode(", ", $errors); |
|
| 97 | - } |
|
| 88 | + /** |
|
| 89 | + * Get last error caused by DateTimeImmutable. |
|
| 90 | + * |
|
| 91 | + * @return string |
|
| 92 | + */ |
|
| 93 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 94 | + { |
|
| 95 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 96 | + return implode(", ", $errors); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -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 | |
@@ -17,665 +17,665 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UnspecifiedType implements ElementBase |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * The wrapped element. |
|
| 22 | - * |
|
| 23 | - * @var Element |
|
| 24 | - */ |
|
| 25 | - private $_element; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param Element $el |
|
| 31 | - */ |
|
| 32 | - public function __construct(Element $el) |
|
| 33 | - { |
|
| 34 | - $this->_element = $el; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Initialize from DER data. |
|
| 39 | - * |
|
| 40 | - * @param string $data DER encoded data |
|
| 41 | - * @return self |
|
| 42 | - */ |
|
| 43 | - public static function fromDER(string $data): self |
|
| 44 | - { |
|
| 45 | - return Element::fromDER($data)->asUnspecified(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Initialize from ElementBase interface. |
|
| 50 | - * |
|
| 51 | - * @param ElementBase $el |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public static function fromElementBase(ElementBase $el): self |
|
| 55 | - { |
|
| 56 | - // if element is already wrapped |
|
| 57 | - if ($el instanceof self) { |
|
| 58 | - return $el; |
|
| 59 | - } |
|
| 60 | - return new self($el->asElement()); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Compatibility method to dispatch calls to the wrapped element. |
|
| 65 | - * |
|
| 66 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 67 | - * @param string $mtd Method name |
|
| 68 | - * @param array $args Arguments |
|
| 69 | - * @return mixed |
|
| 70 | - */ |
|
| 71 | - public function __call($mtd, array $args) |
|
| 72 | - { |
|
| 73 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Get the wrapped element as a context specific tagged type. |
|
| 78 | - * |
|
| 79 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 80 | - * @return TaggedType |
|
| 81 | - */ |
|
| 82 | - public function asTagged(): TaggedType |
|
| 83 | - { |
|
| 84 | - if (!$this->_element instanceof TaggedType) { |
|
| 85 | - throw new \UnexpectedValueException( |
|
| 86 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 87 | - } |
|
| 88 | - return $this->_element; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the wrapped element as an application specific type. |
|
| 93 | - * |
|
| 94 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 95 | - * @return \ASN1\Type\Tagged\ApplicationType |
|
| 96 | - */ |
|
| 97 | - public function asApplication(): Tagged\ApplicationType |
|
| 98 | - { |
|
| 99 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 100 | - throw new \UnexpectedValueException( |
|
| 101 | - "Application type expected, got " . |
|
| 102 | - $this->_typeDescriptorString()); |
|
| 103 | - } |
|
| 104 | - return $this->_element; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the wrapped element as a private tagged type. |
|
| 109 | - * |
|
| 110 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
| 111 | - * @return \ASN1\Type\Tagged\PrivateType |
|
| 112 | - */ |
|
| 113 | - public function asPrivate(): Tagged\PrivateType |
|
| 114 | - { |
|
| 115 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 116 | - throw new \UnexpectedValueException( |
|
| 117 | - "Private type expected, got " . $this->_typeDescriptorString()); |
|
| 118 | - } |
|
| 119 | - return $this->_element; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Get the wrapped element as a boolean type. |
|
| 124 | - * |
|
| 125 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 126 | - * @return \ASN1\Type\Primitive\Boolean |
|
| 127 | - */ |
|
| 128 | - public function asBoolean(): Primitive\Boolean |
|
| 129 | - { |
|
| 130 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 131 | - throw new \UnexpectedValueException( |
|
| 132 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 133 | - } |
|
| 134 | - return $this->_element; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Get the wrapped element as an integer type. |
|
| 139 | - * |
|
| 140 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 141 | - * @return \ASN1\Type\Primitive\Integer |
|
| 142 | - */ |
|
| 143 | - public function asInteger(): Primitive\Integer |
|
| 144 | - { |
|
| 145 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 146 | - throw new \UnexpectedValueException( |
|
| 147 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 148 | - } |
|
| 149 | - return $this->_element; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get the wrapped element as a bit string type. |
|
| 154 | - * |
|
| 155 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 156 | - * @return \ASN1\Type\Primitive\BitString |
|
| 157 | - */ |
|
| 158 | - public function asBitString(): Primitive\BitString |
|
| 159 | - { |
|
| 160 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 161 | - throw new \UnexpectedValueException( |
|
| 162 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 163 | - } |
|
| 164 | - return $this->_element; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get the wrapped element as an octet string type. |
|
| 169 | - * |
|
| 170 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 171 | - * @return \ASN1\Type\Primitive\OctetString |
|
| 172 | - */ |
|
| 173 | - public function asOctetString(): Primitive\OctetString |
|
| 174 | - { |
|
| 175 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 176 | - throw new \UnexpectedValueException( |
|
| 177 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 178 | - } |
|
| 179 | - return $this->_element; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the wrapped element as a null type. |
|
| 184 | - * |
|
| 185 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 186 | - * @return \ASN1\Type\Primitive\NullType |
|
| 187 | - */ |
|
| 188 | - public function asNull(): Primitive\NullType |
|
| 189 | - { |
|
| 190 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 191 | - throw new \UnexpectedValueException( |
|
| 192 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 193 | - } |
|
| 194 | - return $this->_element; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Get the wrapped element as an object identifier type. |
|
| 199 | - * |
|
| 200 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 201 | - * identifier |
|
| 202 | - * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
| 203 | - */ |
|
| 204 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 205 | - { |
|
| 206 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 207 | - throw new \UnexpectedValueException( |
|
| 208 | - $this->_generateExceptionMessage( |
|
| 209 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 210 | - } |
|
| 211 | - return $this->_element; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get the wrapped element as an object descriptor type. |
|
| 216 | - * |
|
| 217 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 218 | - * descriptor |
|
| 219 | - * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
| 220 | - */ |
|
| 221 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 222 | - { |
|
| 223 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 224 | - throw new \UnexpectedValueException( |
|
| 225 | - $this->_generateExceptionMessage( |
|
| 226 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 227 | - } |
|
| 228 | - return $this->_element; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Get the wrapped element as a real type. |
|
| 233 | - * |
|
| 234 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 235 | - * @return \ASN1\Type\Primitive\Real |
|
| 236 | - */ |
|
| 237 | - public function asReal(): Primitive\Real |
|
| 238 | - { |
|
| 239 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 240 | - throw new \UnexpectedValueException( |
|
| 241 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 242 | - } |
|
| 243 | - return $this->_element; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the wrapped element as an enumerated type. |
|
| 248 | - * |
|
| 249 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 250 | - * @return \ASN1\Type\Primitive\Enumerated |
|
| 251 | - */ |
|
| 252 | - public function asEnumerated(): Primitive\Enumerated |
|
| 253 | - { |
|
| 254 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 255 | - throw new \UnexpectedValueException( |
|
| 256 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 257 | - } |
|
| 258 | - return $this->_element; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Get the wrapped element as a UTF8 string type. |
|
| 263 | - * |
|
| 264 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 265 | - * @return \ASN1\Type\Primitive\UTF8String |
|
| 266 | - */ |
|
| 267 | - public function asUTF8String(): Primitive\UTF8String |
|
| 268 | - { |
|
| 269 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 270 | - throw new \UnexpectedValueException( |
|
| 271 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 272 | - } |
|
| 273 | - return $this->_element; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Get the wrapped element as a relative OID type. |
|
| 278 | - * |
|
| 279 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 280 | - * @return \ASN1\Type\Primitive\RelativeOID |
|
| 281 | - */ |
|
| 282 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 283 | - { |
|
| 284 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 285 | - throw new \UnexpectedValueException( |
|
| 286 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 287 | - } |
|
| 288 | - return $this->_element; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Get the wrapped element as a sequence type. |
|
| 293 | - * |
|
| 294 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 295 | - * @return \ASN1\Type\Constructed\Sequence |
|
| 296 | - */ |
|
| 297 | - public function asSequence(): Constructed\Sequence |
|
| 298 | - { |
|
| 299 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 300 | - throw new \UnexpectedValueException( |
|
| 301 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 302 | - } |
|
| 303 | - return $this->_element; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Get the wrapped element as a set type. |
|
| 308 | - * |
|
| 309 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 310 | - * @return \ASN1\Type\Constructed\Set |
|
| 311 | - */ |
|
| 312 | - public function asSet(): Constructed\Set |
|
| 313 | - { |
|
| 314 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 315 | - throw new \UnexpectedValueException( |
|
| 316 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 317 | - } |
|
| 318 | - return $this->_element; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Get the wrapped element as a numeric string type. |
|
| 323 | - * |
|
| 324 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 325 | - * @return \ASN1\Type\Primitive\NumericString |
|
| 326 | - */ |
|
| 327 | - public function asNumericString(): Primitive\NumericString |
|
| 328 | - { |
|
| 329 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 330 | - throw new \UnexpectedValueException( |
|
| 331 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 332 | - } |
|
| 333 | - return $this->_element; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Get the wrapped element as a printable string type. |
|
| 338 | - * |
|
| 339 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 340 | - * string |
|
| 341 | - * @return \ASN1\Type\Primitive\PrintableString |
|
| 342 | - */ |
|
| 343 | - public function asPrintableString(): Primitive\PrintableString |
|
| 344 | - { |
|
| 345 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 346 | - throw new \UnexpectedValueException( |
|
| 347 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 348 | - } |
|
| 349 | - return $this->_element; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get the wrapped element as a T61 string type. |
|
| 354 | - * |
|
| 355 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 356 | - * @return \ASN1\Type\Primitive\T61String |
|
| 357 | - */ |
|
| 358 | - public function asT61String(): Primitive\T61String |
|
| 359 | - { |
|
| 360 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 361 | - throw new \UnexpectedValueException( |
|
| 362 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 363 | - } |
|
| 364 | - return $this->_element; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Get the wrapped element as a videotex string type. |
|
| 369 | - * |
|
| 370 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 371 | - * @return \ASN1\Type\Primitive\VideotexString |
|
| 372 | - */ |
|
| 373 | - public function asVideotexString(): Primitive\VideotexString |
|
| 374 | - { |
|
| 375 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 376 | - throw new \UnexpectedValueException( |
|
| 377 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 378 | - } |
|
| 379 | - return $this->_element; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Get the wrapped element as a IA5 string type. |
|
| 384 | - * |
|
| 385 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 386 | - * @return \ASN1\Type\Primitive\IA5String |
|
| 387 | - */ |
|
| 388 | - public function asIA5String(): Primitive\IA5String |
|
| 389 | - { |
|
| 390 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 391 | - throw new \UnexpectedValueException( |
|
| 392 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 393 | - } |
|
| 394 | - return $this->_element; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Get the wrapped element as an UTC time type. |
|
| 399 | - * |
|
| 400 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 401 | - * @return \ASN1\Type\Primitive\UTCTime |
|
| 402 | - */ |
|
| 403 | - public function asUTCTime(): Primitive\UTCTime |
|
| 404 | - { |
|
| 405 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 406 | - throw new \UnexpectedValueException( |
|
| 407 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 408 | - } |
|
| 409 | - return $this->_element; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get the wrapped element as a generalized time type. |
|
| 414 | - * |
|
| 415 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 416 | - * time |
|
| 417 | - * @return \ASN1\Type\Primitive\GeneralizedTime |
|
| 418 | - */ |
|
| 419 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 420 | - { |
|
| 421 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 422 | - throw new \UnexpectedValueException( |
|
| 423 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 424 | - } |
|
| 425 | - return $this->_element; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Get the wrapped element as a graphic string type. |
|
| 430 | - * |
|
| 431 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 432 | - * @return \ASN1\Type\Primitive\GraphicString |
|
| 433 | - */ |
|
| 434 | - public function asGraphicString(): Primitive\GraphicString |
|
| 435 | - { |
|
| 436 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 437 | - throw new \UnexpectedValueException( |
|
| 438 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 439 | - } |
|
| 440 | - return $this->_element; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Get the wrapped element as a visible string type. |
|
| 445 | - * |
|
| 446 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 447 | - * @return \ASN1\Type\Primitive\VisibleString |
|
| 448 | - */ |
|
| 449 | - public function asVisibleString(): Primitive\VisibleString |
|
| 450 | - { |
|
| 451 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 452 | - throw new \UnexpectedValueException( |
|
| 453 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 454 | - } |
|
| 455 | - return $this->_element; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Get the wrapped element as a general string type. |
|
| 460 | - * |
|
| 461 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 462 | - * @return \ASN1\Type\Primitive\GeneralString |
|
| 463 | - */ |
|
| 464 | - public function asGeneralString(): Primitive\GeneralString |
|
| 465 | - { |
|
| 466 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 467 | - throw new \UnexpectedValueException( |
|
| 468 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 469 | - } |
|
| 470 | - return $this->_element; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Get the wrapped element as a universal string type. |
|
| 475 | - * |
|
| 476 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 477 | - * string |
|
| 478 | - * @return \ASN1\Type\Primitive\UniversalString |
|
| 479 | - */ |
|
| 480 | - public function asUniversalString(): Primitive\UniversalString |
|
| 481 | - { |
|
| 482 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 483 | - throw new \UnexpectedValueException( |
|
| 484 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 485 | - } |
|
| 486 | - return $this->_element; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Get the wrapped element as a character string type. |
|
| 491 | - * |
|
| 492 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 493 | - * string |
|
| 494 | - * @return \ASN1\Type\Primitive\CharacterString |
|
| 495 | - */ |
|
| 496 | - public function asCharacterString(): Primitive\CharacterString |
|
| 497 | - { |
|
| 498 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 499 | - throw new \UnexpectedValueException( |
|
| 500 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 501 | - } |
|
| 502 | - return $this->_element; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Get the wrapped element as a BMP string type. |
|
| 507 | - * |
|
| 508 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 509 | - * @return \ASN1\Type\Primitive\BMPString |
|
| 510 | - */ |
|
| 511 | - public function asBMPString(): Primitive\BMPString |
|
| 512 | - { |
|
| 513 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 514 | - throw new \UnexpectedValueException( |
|
| 515 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 516 | - } |
|
| 517 | - return $this->_element; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Get the wrapped element as any string type. |
|
| 522 | - * |
|
| 523 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 524 | - * @return StringType |
|
| 525 | - */ |
|
| 526 | - public function asString(): StringType |
|
| 527 | - { |
|
| 528 | - if (!$this->_element instanceof StringType) { |
|
| 529 | - throw new \UnexpectedValueException( |
|
| 530 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 531 | - } |
|
| 532 | - return $this->_element; |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * Get the wrapped element as any time type. |
|
| 537 | - * |
|
| 538 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 539 | - * @return TimeType |
|
| 540 | - */ |
|
| 541 | - public function asTime(): TimeType |
|
| 542 | - { |
|
| 543 | - if (!$this->_element instanceof TimeType) { |
|
| 544 | - throw new \UnexpectedValueException( |
|
| 545 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 546 | - } |
|
| 547 | - return $this->_element; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 552 | - * |
|
| 553 | - * @param int $tag Type tag of the expected element |
|
| 554 | - * @return string |
|
| 555 | - */ |
|
| 556 | - private function _generateExceptionMessage(int $tag): string |
|
| 557 | - { |
|
| 558 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 559 | - $this->_typeDescriptorString()); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 564 | - * |
|
| 565 | - * @return string |
|
| 566 | - */ |
|
| 567 | - private function _typeDescriptorString(): string |
|
| 568 | - { |
|
| 569 | - $type_cls = $this->_element->typeClass(); |
|
| 570 | - $tag = $this->_element->tag(); |
|
| 571 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 572 | - return Element::tagToName($tag); |
|
| 573 | - } |
|
| 574 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * |
|
| 579 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 580 | - * @return string |
|
| 581 | - */ |
|
| 582 | - public function toDER(): string |
|
| 583 | - { |
|
| 584 | - return $this->_element->toDER(); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * |
|
| 589 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 590 | - * @return int |
|
| 591 | - */ |
|
| 592 | - public function typeClass(): int |
|
| 593 | - { |
|
| 594 | - return $this->_element->typeClass(); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * |
|
| 599 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 600 | - * @return bool |
|
| 601 | - */ |
|
| 602 | - public function isConstructed(): bool |
|
| 603 | - { |
|
| 604 | - return $this->_element->isConstructed(); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * |
|
| 609 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 610 | - * @return int |
|
| 611 | - */ |
|
| 612 | - public function tag(): int |
|
| 613 | - { |
|
| 614 | - return $this->_element->tag(); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * |
|
| 619 | - * {@inheritdoc} |
|
| 620 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 621 | - * @return bool |
|
| 622 | - */ |
|
| 623 | - public function isType(int $tag): bool |
|
| 624 | - { |
|
| 625 | - return $this->_element->isType($tag); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * |
|
| 630 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 631 | - * type strictness. |
|
| 632 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 633 | - * @return ElementBase |
|
| 634 | - */ |
|
| 635 | - public function expectType(int $tag): ElementBase |
|
| 636 | - { |
|
| 637 | - return $this->_element->expectType($tag); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * |
|
| 642 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 643 | - * @return bool |
|
| 644 | - */ |
|
| 645 | - public function isTagged(): bool |
|
| 646 | - { |
|
| 647 | - return $this->_element->isTagged(); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * |
|
| 652 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 653 | - * type strictness. |
|
| 654 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 655 | - * @return TaggedType |
|
| 656 | - */ |
|
| 657 | - public function expectTagged($tag = null): TaggedType |
|
| 658 | - { |
|
| 659 | - return $this->_element->expectTagged($tag); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * |
|
| 664 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 665 | - * @return Element |
|
| 666 | - */ |
|
| 667 | - public function asElement(): Element |
|
| 668 | - { |
|
| 669 | - return $this->_element; |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * |
|
| 674 | - * {@inheritdoc} |
|
| 675 | - * @return UnspecifiedType |
|
| 676 | - */ |
|
| 677 | - public function asUnspecified(): UnspecifiedType |
|
| 678 | - { |
|
| 679 | - return $this; |
|
| 680 | - } |
|
| 20 | + /** |
|
| 21 | + * The wrapped element. |
|
| 22 | + * |
|
| 23 | + * @var Element |
|
| 24 | + */ |
|
| 25 | + private $_element; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param Element $el |
|
| 31 | + */ |
|
| 32 | + public function __construct(Element $el) |
|
| 33 | + { |
|
| 34 | + $this->_element = $el; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Initialize from DER data. |
|
| 39 | + * |
|
| 40 | + * @param string $data DER encoded data |
|
| 41 | + * @return self |
|
| 42 | + */ |
|
| 43 | + public static function fromDER(string $data): self |
|
| 44 | + { |
|
| 45 | + return Element::fromDER($data)->asUnspecified(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Initialize from ElementBase interface. |
|
| 50 | + * |
|
| 51 | + * @param ElementBase $el |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public static function fromElementBase(ElementBase $el): self |
|
| 55 | + { |
|
| 56 | + // if element is already wrapped |
|
| 57 | + if ($el instanceof self) { |
|
| 58 | + return $el; |
|
| 59 | + } |
|
| 60 | + return new self($el->asElement()); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Compatibility method to dispatch calls to the wrapped element. |
|
| 65 | + * |
|
| 66 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 67 | + * @param string $mtd Method name |
|
| 68 | + * @param array $args Arguments |
|
| 69 | + * @return mixed |
|
| 70 | + */ |
|
| 71 | + public function __call($mtd, array $args) |
|
| 72 | + { |
|
| 73 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Get the wrapped element as a context specific tagged type. |
|
| 78 | + * |
|
| 79 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 80 | + * @return TaggedType |
|
| 81 | + */ |
|
| 82 | + public function asTagged(): TaggedType |
|
| 83 | + { |
|
| 84 | + if (!$this->_element instanceof TaggedType) { |
|
| 85 | + throw new \UnexpectedValueException( |
|
| 86 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 87 | + } |
|
| 88 | + return $this->_element; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the wrapped element as an application specific type. |
|
| 93 | + * |
|
| 94 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 95 | + * @return \ASN1\Type\Tagged\ApplicationType |
|
| 96 | + */ |
|
| 97 | + public function asApplication(): Tagged\ApplicationType |
|
| 98 | + { |
|
| 99 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 100 | + throw new \UnexpectedValueException( |
|
| 101 | + "Application type expected, got " . |
|
| 102 | + $this->_typeDescriptorString()); |
|
| 103 | + } |
|
| 104 | + return $this->_element; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the wrapped element as a private tagged type. |
|
| 109 | + * |
|
| 110 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
| 111 | + * @return \ASN1\Type\Tagged\PrivateType |
|
| 112 | + */ |
|
| 113 | + public function asPrivate(): Tagged\PrivateType |
|
| 114 | + { |
|
| 115 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 116 | + throw new \UnexpectedValueException( |
|
| 117 | + "Private type expected, got " . $this->_typeDescriptorString()); |
|
| 118 | + } |
|
| 119 | + return $this->_element; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Get the wrapped element as a boolean type. |
|
| 124 | + * |
|
| 125 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 126 | + * @return \ASN1\Type\Primitive\Boolean |
|
| 127 | + */ |
|
| 128 | + public function asBoolean(): Primitive\Boolean |
|
| 129 | + { |
|
| 130 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 131 | + throw new \UnexpectedValueException( |
|
| 132 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 133 | + } |
|
| 134 | + return $this->_element; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Get the wrapped element as an integer type. |
|
| 139 | + * |
|
| 140 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 141 | + * @return \ASN1\Type\Primitive\Integer |
|
| 142 | + */ |
|
| 143 | + public function asInteger(): Primitive\Integer |
|
| 144 | + { |
|
| 145 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 146 | + throw new \UnexpectedValueException( |
|
| 147 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 148 | + } |
|
| 149 | + return $this->_element; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get the wrapped element as a bit string type. |
|
| 154 | + * |
|
| 155 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 156 | + * @return \ASN1\Type\Primitive\BitString |
|
| 157 | + */ |
|
| 158 | + public function asBitString(): Primitive\BitString |
|
| 159 | + { |
|
| 160 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 161 | + throw new \UnexpectedValueException( |
|
| 162 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 163 | + } |
|
| 164 | + return $this->_element; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get the wrapped element as an octet string type. |
|
| 169 | + * |
|
| 170 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 171 | + * @return \ASN1\Type\Primitive\OctetString |
|
| 172 | + */ |
|
| 173 | + public function asOctetString(): Primitive\OctetString |
|
| 174 | + { |
|
| 175 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 176 | + throw new \UnexpectedValueException( |
|
| 177 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 178 | + } |
|
| 179 | + return $this->_element; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the wrapped element as a null type. |
|
| 184 | + * |
|
| 185 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 186 | + * @return \ASN1\Type\Primitive\NullType |
|
| 187 | + */ |
|
| 188 | + public function asNull(): Primitive\NullType |
|
| 189 | + { |
|
| 190 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 191 | + throw new \UnexpectedValueException( |
|
| 192 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 193 | + } |
|
| 194 | + return $this->_element; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Get the wrapped element as an object identifier type. |
|
| 199 | + * |
|
| 200 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 201 | + * identifier |
|
| 202 | + * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
| 203 | + */ |
|
| 204 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 205 | + { |
|
| 206 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 207 | + throw new \UnexpectedValueException( |
|
| 208 | + $this->_generateExceptionMessage( |
|
| 209 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 210 | + } |
|
| 211 | + return $this->_element; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get the wrapped element as an object descriptor type. |
|
| 216 | + * |
|
| 217 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 218 | + * descriptor |
|
| 219 | + * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
| 220 | + */ |
|
| 221 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 222 | + { |
|
| 223 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 224 | + throw new \UnexpectedValueException( |
|
| 225 | + $this->_generateExceptionMessage( |
|
| 226 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 227 | + } |
|
| 228 | + return $this->_element; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Get the wrapped element as a real type. |
|
| 233 | + * |
|
| 234 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 235 | + * @return \ASN1\Type\Primitive\Real |
|
| 236 | + */ |
|
| 237 | + public function asReal(): Primitive\Real |
|
| 238 | + { |
|
| 239 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 240 | + throw new \UnexpectedValueException( |
|
| 241 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 242 | + } |
|
| 243 | + return $this->_element; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the wrapped element as an enumerated type. |
|
| 248 | + * |
|
| 249 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 250 | + * @return \ASN1\Type\Primitive\Enumerated |
|
| 251 | + */ |
|
| 252 | + public function asEnumerated(): Primitive\Enumerated |
|
| 253 | + { |
|
| 254 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 255 | + throw new \UnexpectedValueException( |
|
| 256 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 257 | + } |
|
| 258 | + return $this->_element; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Get the wrapped element as a UTF8 string type. |
|
| 263 | + * |
|
| 264 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 265 | + * @return \ASN1\Type\Primitive\UTF8String |
|
| 266 | + */ |
|
| 267 | + public function asUTF8String(): Primitive\UTF8String |
|
| 268 | + { |
|
| 269 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 270 | + throw new \UnexpectedValueException( |
|
| 271 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 272 | + } |
|
| 273 | + return $this->_element; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Get the wrapped element as a relative OID type. |
|
| 278 | + * |
|
| 279 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 280 | + * @return \ASN1\Type\Primitive\RelativeOID |
|
| 281 | + */ |
|
| 282 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 283 | + { |
|
| 284 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 285 | + throw new \UnexpectedValueException( |
|
| 286 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 287 | + } |
|
| 288 | + return $this->_element; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Get the wrapped element as a sequence type. |
|
| 293 | + * |
|
| 294 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 295 | + * @return \ASN1\Type\Constructed\Sequence |
|
| 296 | + */ |
|
| 297 | + public function asSequence(): Constructed\Sequence |
|
| 298 | + { |
|
| 299 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 300 | + throw new \UnexpectedValueException( |
|
| 301 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 302 | + } |
|
| 303 | + return $this->_element; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Get the wrapped element as a set type. |
|
| 308 | + * |
|
| 309 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 310 | + * @return \ASN1\Type\Constructed\Set |
|
| 311 | + */ |
|
| 312 | + public function asSet(): Constructed\Set |
|
| 313 | + { |
|
| 314 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 315 | + throw new \UnexpectedValueException( |
|
| 316 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 317 | + } |
|
| 318 | + return $this->_element; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Get the wrapped element as a numeric string type. |
|
| 323 | + * |
|
| 324 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 325 | + * @return \ASN1\Type\Primitive\NumericString |
|
| 326 | + */ |
|
| 327 | + public function asNumericString(): Primitive\NumericString |
|
| 328 | + { |
|
| 329 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 330 | + throw new \UnexpectedValueException( |
|
| 331 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 332 | + } |
|
| 333 | + return $this->_element; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Get the wrapped element as a printable string type. |
|
| 338 | + * |
|
| 339 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 340 | + * string |
|
| 341 | + * @return \ASN1\Type\Primitive\PrintableString |
|
| 342 | + */ |
|
| 343 | + public function asPrintableString(): Primitive\PrintableString |
|
| 344 | + { |
|
| 345 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 346 | + throw new \UnexpectedValueException( |
|
| 347 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 348 | + } |
|
| 349 | + return $this->_element; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get the wrapped element as a T61 string type. |
|
| 354 | + * |
|
| 355 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 356 | + * @return \ASN1\Type\Primitive\T61String |
|
| 357 | + */ |
|
| 358 | + public function asT61String(): Primitive\T61String |
|
| 359 | + { |
|
| 360 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 361 | + throw new \UnexpectedValueException( |
|
| 362 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 363 | + } |
|
| 364 | + return $this->_element; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Get the wrapped element as a videotex string type. |
|
| 369 | + * |
|
| 370 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 371 | + * @return \ASN1\Type\Primitive\VideotexString |
|
| 372 | + */ |
|
| 373 | + public function asVideotexString(): Primitive\VideotexString |
|
| 374 | + { |
|
| 375 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 376 | + throw new \UnexpectedValueException( |
|
| 377 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 378 | + } |
|
| 379 | + return $this->_element; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Get the wrapped element as a IA5 string type. |
|
| 384 | + * |
|
| 385 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 386 | + * @return \ASN1\Type\Primitive\IA5String |
|
| 387 | + */ |
|
| 388 | + public function asIA5String(): Primitive\IA5String |
|
| 389 | + { |
|
| 390 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 391 | + throw new \UnexpectedValueException( |
|
| 392 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 393 | + } |
|
| 394 | + return $this->_element; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Get the wrapped element as an UTC time type. |
|
| 399 | + * |
|
| 400 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 401 | + * @return \ASN1\Type\Primitive\UTCTime |
|
| 402 | + */ |
|
| 403 | + public function asUTCTime(): Primitive\UTCTime |
|
| 404 | + { |
|
| 405 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 406 | + throw new \UnexpectedValueException( |
|
| 407 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 408 | + } |
|
| 409 | + return $this->_element; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get the wrapped element as a generalized time type. |
|
| 414 | + * |
|
| 415 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 416 | + * time |
|
| 417 | + * @return \ASN1\Type\Primitive\GeneralizedTime |
|
| 418 | + */ |
|
| 419 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 420 | + { |
|
| 421 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 422 | + throw new \UnexpectedValueException( |
|
| 423 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 424 | + } |
|
| 425 | + return $this->_element; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Get the wrapped element as a graphic string type. |
|
| 430 | + * |
|
| 431 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 432 | + * @return \ASN1\Type\Primitive\GraphicString |
|
| 433 | + */ |
|
| 434 | + public function asGraphicString(): Primitive\GraphicString |
|
| 435 | + { |
|
| 436 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 437 | + throw new \UnexpectedValueException( |
|
| 438 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 439 | + } |
|
| 440 | + return $this->_element; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Get the wrapped element as a visible string type. |
|
| 445 | + * |
|
| 446 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 447 | + * @return \ASN1\Type\Primitive\VisibleString |
|
| 448 | + */ |
|
| 449 | + public function asVisibleString(): Primitive\VisibleString |
|
| 450 | + { |
|
| 451 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 452 | + throw new \UnexpectedValueException( |
|
| 453 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 454 | + } |
|
| 455 | + return $this->_element; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Get the wrapped element as a general string type. |
|
| 460 | + * |
|
| 461 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 462 | + * @return \ASN1\Type\Primitive\GeneralString |
|
| 463 | + */ |
|
| 464 | + public function asGeneralString(): Primitive\GeneralString |
|
| 465 | + { |
|
| 466 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 467 | + throw new \UnexpectedValueException( |
|
| 468 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 469 | + } |
|
| 470 | + return $this->_element; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Get the wrapped element as a universal string type. |
|
| 475 | + * |
|
| 476 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 477 | + * string |
|
| 478 | + * @return \ASN1\Type\Primitive\UniversalString |
|
| 479 | + */ |
|
| 480 | + public function asUniversalString(): Primitive\UniversalString |
|
| 481 | + { |
|
| 482 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 483 | + throw new \UnexpectedValueException( |
|
| 484 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 485 | + } |
|
| 486 | + return $this->_element; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Get the wrapped element as a character string type. |
|
| 491 | + * |
|
| 492 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 493 | + * string |
|
| 494 | + * @return \ASN1\Type\Primitive\CharacterString |
|
| 495 | + */ |
|
| 496 | + public function asCharacterString(): Primitive\CharacterString |
|
| 497 | + { |
|
| 498 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 499 | + throw new \UnexpectedValueException( |
|
| 500 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 501 | + } |
|
| 502 | + return $this->_element; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Get the wrapped element as a BMP string type. |
|
| 507 | + * |
|
| 508 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 509 | + * @return \ASN1\Type\Primitive\BMPString |
|
| 510 | + */ |
|
| 511 | + public function asBMPString(): Primitive\BMPString |
|
| 512 | + { |
|
| 513 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 514 | + throw new \UnexpectedValueException( |
|
| 515 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 516 | + } |
|
| 517 | + return $this->_element; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Get the wrapped element as any string type. |
|
| 522 | + * |
|
| 523 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 524 | + * @return StringType |
|
| 525 | + */ |
|
| 526 | + public function asString(): StringType |
|
| 527 | + { |
|
| 528 | + if (!$this->_element instanceof StringType) { |
|
| 529 | + throw new \UnexpectedValueException( |
|
| 530 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 531 | + } |
|
| 532 | + return $this->_element; |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * Get the wrapped element as any time type. |
|
| 537 | + * |
|
| 538 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 539 | + * @return TimeType |
|
| 540 | + */ |
|
| 541 | + public function asTime(): TimeType |
|
| 542 | + { |
|
| 543 | + if (!$this->_element instanceof TimeType) { |
|
| 544 | + throw new \UnexpectedValueException( |
|
| 545 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 546 | + } |
|
| 547 | + return $this->_element; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 552 | + * |
|
| 553 | + * @param int $tag Type tag of the expected element |
|
| 554 | + * @return string |
|
| 555 | + */ |
|
| 556 | + private function _generateExceptionMessage(int $tag): string |
|
| 557 | + { |
|
| 558 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 559 | + $this->_typeDescriptorString()); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 564 | + * |
|
| 565 | + * @return string |
|
| 566 | + */ |
|
| 567 | + private function _typeDescriptorString(): string |
|
| 568 | + { |
|
| 569 | + $type_cls = $this->_element->typeClass(); |
|
| 570 | + $tag = $this->_element->tag(); |
|
| 571 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 572 | + return Element::tagToName($tag); |
|
| 573 | + } |
|
| 574 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * |
|
| 579 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 580 | + * @return string |
|
| 581 | + */ |
|
| 582 | + public function toDER(): string |
|
| 583 | + { |
|
| 584 | + return $this->_element->toDER(); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * |
|
| 589 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 590 | + * @return int |
|
| 591 | + */ |
|
| 592 | + public function typeClass(): int |
|
| 593 | + { |
|
| 594 | + return $this->_element->typeClass(); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * |
|
| 599 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 600 | + * @return bool |
|
| 601 | + */ |
|
| 602 | + public function isConstructed(): bool |
|
| 603 | + { |
|
| 604 | + return $this->_element->isConstructed(); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * |
|
| 609 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 610 | + * @return int |
|
| 611 | + */ |
|
| 612 | + public function tag(): int |
|
| 613 | + { |
|
| 614 | + return $this->_element->tag(); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * |
|
| 619 | + * {@inheritdoc} |
|
| 620 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 621 | + * @return bool |
|
| 622 | + */ |
|
| 623 | + public function isType(int $tag): bool |
|
| 624 | + { |
|
| 625 | + return $this->_element->isType($tag); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * |
|
| 630 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 631 | + * type strictness. |
|
| 632 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 633 | + * @return ElementBase |
|
| 634 | + */ |
|
| 635 | + public function expectType(int $tag): ElementBase |
|
| 636 | + { |
|
| 637 | + return $this->_element->expectType($tag); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * |
|
| 642 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 643 | + * @return bool |
|
| 644 | + */ |
|
| 645 | + public function isTagged(): bool |
|
| 646 | + { |
|
| 647 | + return $this->_element->isTagged(); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * |
|
| 652 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 653 | + * type strictness. |
|
| 654 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 655 | + * @return TaggedType |
|
| 656 | + */ |
|
| 657 | + public function expectTagged($tag = null): TaggedType |
|
| 658 | + { |
|
| 659 | + return $this->_element->expectTagged($tag); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * |
|
| 664 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 665 | + * @return Element |
|
| 666 | + */ |
|
| 667 | + public function asElement(): Element |
|
| 668 | + { |
|
| 669 | + return $this->_element; |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * |
|
| 674 | + * {@inheritdoc} |
|
| 675 | + * @return UnspecifiedType |
|
| 676 | + */ |
|
| 677 | + public function asUnspecified(): UnspecifiedType |
|
| 678 | + { |
|
| 679 | + return $this; |
|
| 680 | + } |
|
| 681 | 681 | } |
@@ -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 | |