@@ -273,7 +273,7 @@ |
||
| 273 | 273 | /** |
| 274 | 274 | * Test that number is valid for this context. |
| 275 | 275 | * |
| 276 | - * @param mixed $num |
|
| 276 | + * @param string $num |
|
| 277 | 277 | * @return boolean |
| 278 | 278 | */ |
| 279 | 279 | private static function _validateNumber($num): bool |
@@ -16,271 +16,271 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Real extends Element |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 22 | + /** |
|
| 23 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Regex pattern to parse PHP exponent number format. |
|
| 31 | - * |
|
| 32 | - * @link http://php.net/manual/en/language.types.float.php |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 36 | - '([+\-]?'. // sign |
|
| 37 | - '(?:'. |
|
| 38 | - '\d+'. // LNUM |
|
| 39 | - '|'. |
|
| 40 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 41 | - '))[eE]'. |
|
| 42 | - '([+\-]?\d+)'. // exponent |
|
| 43 | - '$/'; /* @formatter:on */ |
|
| 29 | + /** |
|
| 30 | + * Regex pattern to parse PHP exponent number format. |
|
| 31 | + * |
|
| 32 | + * @link http://php.net/manual/en/language.types.float.php |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 36 | + '([+\-]?'. // sign |
|
| 37 | + '(?:'. |
|
| 38 | + '\d+'. // LNUM |
|
| 39 | + '|'. |
|
| 40 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 41 | + '))[eE]'. |
|
| 42 | + '([+\-]?\d+)'. // exponent |
|
| 43 | + '$/'; /* @formatter:on */ |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Number zero represented in NR3 form. |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - const NR3_ZERO = ".E+0"; |
|
| 45 | + /** |
|
| 46 | + * Number zero represented in NR3 form. |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + const NR3_ZERO = ".E+0"; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Number in NR3 form. |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $_number; |
|
| 52 | + /** |
|
| 53 | + * Number in NR3 form. |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $_number; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Constructor. |
|
| 61 | - * |
|
| 62 | - * @param string $number Number in NR3 form. |
|
| 63 | - */ |
|
| 64 | - public function __construct(string $number) |
|
| 65 | - { |
|
| 66 | - $this->_typeTag = self::TYPE_REAL; |
|
| 67 | - if (!self::_validateNumber($number)) { |
|
| 68 | - throw new \InvalidArgumentException( |
|
| 69 | - "'$number' is not a valid NR3 form real."); |
|
| 70 | - } |
|
| 71 | - $this->_number = $number; |
|
| 72 | - } |
|
| 59 | + /** |
|
| 60 | + * Constructor. |
|
| 61 | + * |
|
| 62 | + * @param string $number Number in NR3 form. |
|
| 63 | + */ |
|
| 64 | + public function __construct(string $number) |
|
| 65 | + { |
|
| 66 | + $this->_typeTag = self::TYPE_REAL; |
|
| 67 | + if (!self::_validateNumber($number)) { |
|
| 68 | + throw new \InvalidArgumentException( |
|
| 69 | + "'$number' is not a valid NR3 form real."); |
|
| 70 | + } |
|
| 71 | + $this->_number = $number; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Initialize from float. |
|
| 76 | - * |
|
| 77 | - * @param float $number |
|
| 78 | - * @return self |
|
| 79 | - */ |
|
| 80 | - public static function fromFloat(float $number) |
|
| 81 | - { |
|
| 82 | - return new self(self::_decimalToNR3(strval($number))); |
|
| 83 | - } |
|
| 74 | + /** |
|
| 75 | + * Initialize from float. |
|
| 76 | + * |
|
| 77 | + * @param float $number |
|
| 78 | + * @return self |
|
| 79 | + */ |
|
| 80 | + public static function fromFloat(float $number) |
|
| 81 | + { |
|
| 82 | + return new self(self::_decimalToNR3(strval($number))); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Get number as a float. |
|
| 87 | - * |
|
| 88 | - * @return float |
|
| 89 | - */ |
|
| 90 | - public function float(): float |
|
| 91 | - { |
|
| 92 | - return self::_nr3ToDecimal($this->_number); |
|
| 93 | - } |
|
| 85 | + /** |
|
| 86 | + * Get number as a float. |
|
| 87 | + * |
|
| 88 | + * @return float |
|
| 89 | + */ |
|
| 90 | + public function float(): float |
|
| 91 | + { |
|
| 92 | + return self::_nr3ToDecimal($this->_number); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * |
|
| 97 | - * {@inheritdoc} |
|
| 98 | - */ |
|
| 99 | - protected function _encodedContentDER(): string |
|
| 100 | - { |
|
| 101 | - /* if the real value is the value zero, there shall be no contents |
|
| 95 | + /** |
|
| 96 | + * |
|
| 97 | + * {@inheritdoc} |
|
| 98 | + */ |
|
| 99 | + protected function _encodedContentDER(): string |
|
| 100 | + { |
|
| 101 | + /* if the real value is the value zero, there shall be no contents |
|
| 102 | 102 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
| 103 | - if (self::NR3_ZERO == $this->_number) { |
|
| 104 | - return ""; |
|
| 105 | - } |
|
| 106 | - // encode in NR3 decimal encoding |
|
| 107 | - $data = chr(0x03) . $this->_number; |
|
| 108 | - return $data; |
|
| 109 | - } |
|
| 103 | + if (self::NR3_ZERO == $this->_number) { |
|
| 104 | + return ""; |
|
| 105 | + } |
|
| 106 | + // encode in NR3 decimal encoding |
|
| 107 | + $data = chr(0x03) . $this->_number; |
|
| 108 | + return $data; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - * @return self |
|
| 115 | - */ |
|
| 116 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 117 | - int &$offset) |
|
| 118 | - { |
|
| 119 | - $idx = $offset; |
|
| 120 | - $length = Length::expectFromDER($data, $idx); |
|
| 121 | - // if length is zero, value is zero (spec 8.5.2) |
|
| 122 | - if (!$length->length()) { |
|
| 123 | - $obj = new self(self::NR3_ZERO); |
|
| 124 | - } else { |
|
| 125 | - $bytes = substr($data, $idx, $length->length()); |
|
| 126 | - $byte = ord($bytes[0]); |
|
| 127 | - if (0x80 & $byte) { // bit 8 = 1 |
|
| 128 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
| 129 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 130 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
| 131 | - } else { // bit 8 = 0, bit 7 = 1 |
|
| 132 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - $offset = $idx + $length->length(); |
|
| 136 | - return $obj; |
|
| 137 | - } |
|
| 111 | + /** |
|
| 112 | + * |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + * @return self |
|
| 115 | + */ |
|
| 116 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 117 | + int &$offset) |
|
| 118 | + { |
|
| 119 | + $idx = $offset; |
|
| 120 | + $length = Length::expectFromDER($data, $idx); |
|
| 121 | + // if length is zero, value is zero (spec 8.5.2) |
|
| 122 | + if (!$length->length()) { |
|
| 123 | + $obj = new self(self::NR3_ZERO); |
|
| 124 | + } else { |
|
| 125 | + $bytes = substr($data, $idx, $length->length()); |
|
| 126 | + $byte = ord($bytes[0]); |
|
| 127 | + if (0x80 & $byte) { // bit 8 = 1 |
|
| 128 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
| 129 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 130 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
| 131 | + } else { // bit 8 = 0, bit 7 = 1 |
|
| 132 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + $offset = $idx + $length->length(); |
|
| 136 | + return $obj; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * |
|
| 141 | - * @todo Implement |
|
| 142 | - * @param string $data |
|
| 143 | - */ |
|
| 144 | - protected static function _decodeBinaryEncoding(string $data) |
|
| 145 | - { |
|
| 146 | - throw new \RuntimeException( |
|
| 147 | - "Binary encoding of REAL is not implemented."); |
|
| 148 | - } |
|
| 139 | + /** |
|
| 140 | + * |
|
| 141 | + * @todo Implement |
|
| 142 | + * @param string $data |
|
| 143 | + */ |
|
| 144 | + protected static function _decodeBinaryEncoding(string $data) |
|
| 145 | + { |
|
| 146 | + throw new \RuntimeException( |
|
| 147 | + "Binary encoding of REAL is not implemented."); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * |
|
| 152 | - * @param string $data |
|
| 153 | - * @throws \RuntimeException |
|
| 154 | - * @return \ASN1\Type\Primitive\Real |
|
| 155 | - */ |
|
| 156 | - protected static function _decodeDecimalEncoding(string $data): Real |
|
| 157 | - { |
|
| 158 | - $nr = ord($data[0]) & 0x03; |
|
| 159 | - if ($nr != 0x03) { |
|
| 160 | - throw new \RuntimeException("Only NR3 form supported."); |
|
| 161 | - } |
|
| 162 | - $str = substr($data, 1); |
|
| 163 | - return new self($str); |
|
| 164 | - } |
|
| 150 | + /** |
|
| 151 | + * |
|
| 152 | + * @param string $data |
|
| 153 | + * @throws \RuntimeException |
|
| 154 | + * @return \ASN1\Type\Primitive\Real |
|
| 155 | + */ |
|
| 156 | + protected static function _decodeDecimalEncoding(string $data): Real |
|
| 157 | + { |
|
| 158 | + $nr = ord($data[0]) & 0x03; |
|
| 159 | + if ($nr != 0x03) { |
|
| 160 | + throw new \RuntimeException("Only NR3 form supported."); |
|
| 161 | + } |
|
| 162 | + $str = substr($data, 1); |
|
| 163 | + return new self($str); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * |
|
| 168 | - * @todo Implement |
|
| 169 | - * @param string $data |
|
| 170 | - */ |
|
| 171 | - protected static function _decodeSpecialRealValue(string $data) |
|
| 172 | - { |
|
| 173 | - if (strlen($data) != 1) { |
|
| 174 | - throw new DecodeException( |
|
| 175 | - "SpecialRealValue must have one content octet."); |
|
| 176 | - } |
|
| 177 | - $byte = ord($data[0]); |
|
| 178 | - if ($byte == 0x40) { // positive infinity |
|
| 179 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 180 | - } else if ($byte == 0x41) { // negative infinity |
|
| 181 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 182 | - } else { |
|
| 183 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 184 | - } |
|
| 185 | - } |
|
| 166 | + /** |
|
| 167 | + * |
|
| 168 | + * @todo Implement |
|
| 169 | + * @param string $data |
|
| 170 | + */ |
|
| 171 | + protected static function _decodeSpecialRealValue(string $data) |
|
| 172 | + { |
|
| 173 | + if (strlen($data) != 1) { |
|
| 174 | + throw new DecodeException( |
|
| 175 | + "SpecialRealValue must have one content octet."); |
|
| 176 | + } |
|
| 177 | + $byte = ord($data[0]); |
|
| 178 | + if ($byte == 0x40) { // positive infinity |
|
| 179 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 180 | + } else if ($byte == 0x41) { // negative infinity |
|
| 181 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 182 | + } else { |
|
| 183 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Convert decimal number string to NR3 form. |
|
| 189 | - * |
|
| 190 | - * @param string $str |
|
| 191 | - * @return string |
|
| 192 | - */ |
|
| 193 | - private static function _decimalToNR3(string $str): string |
|
| 194 | - { |
|
| 195 | - // if number is in exponent form |
|
| 196 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 197 | - $parts = explode(".", $match[1]); |
|
| 198 | - $m = ltrim($parts[0], "0"); |
|
| 199 | - $e = intval($match[2]); |
|
| 200 | - // if mantissa had decimals |
|
| 201 | - if (count($parts) == 2) { |
|
| 202 | - $d = rtrim($parts[1], "0"); |
|
| 203 | - $e -= strlen($d); |
|
| 204 | - $m .= $d; |
|
| 205 | - } |
|
| 206 | - } else { |
|
| 207 | - // explode from decimal |
|
| 208 | - $parts = explode(".", $str); |
|
| 209 | - $m = ltrim($parts[0], "0"); |
|
| 210 | - // if number had decimals |
|
| 211 | - if (count($parts) == 2) { |
|
| 212 | - // exponent is negative number of the decimals |
|
| 213 | - $e = -strlen($parts[1]); |
|
| 214 | - // append decimals to the mantissa |
|
| 215 | - $m .= $parts[1]; |
|
| 216 | - } else { |
|
| 217 | - $e = 0; |
|
| 218 | - } |
|
| 219 | - // shift trailing zeroes from the mantissa to the exponent |
|
| 220 | - while (substr($m, -1) === "0") { |
|
| 221 | - $e++; |
|
| 222 | - $m = substr($m, 0, -1); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 187 | + /** |
|
| 188 | + * Convert decimal number string to NR3 form. |
|
| 189 | + * |
|
| 190 | + * @param string $str |
|
| 191 | + * @return string |
|
| 192 | + */ |
|
| 193 | + private static function _decimalToNR3(string $str): string |
|
| 194 | + { |
|
| 195 | + // if number is in exponent form |
|
| 196 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 197 | + $parts = explode(".", $match[1]); |
|
| 198 | + $m = ltrim($parts[0], "0"); |
|
| 199 | + $e = intval($match[2]); |
|
| 200 | + // if mantissa had decimals |
|
| 201 | + if (count($parts) == 2) { |
|
| 202 | + $d = rtrim($parts[1], "0"); |
|
| 203 | + $e -= strlen($d); |
|
| 204 | + $m .= $d; |
|
| 205 | + } |
|
| 206 | + } else { |
|
| 207 | + // explode from decimal |
|
| 208 | + $parts = explode(".", $str); |
|
| 209 | + $m = ltrim($parts[0], "0"); |
|
| 210 | + // if number had decimals |
|
| 211 | + if (count($parts) == 2) { |
|
| 212 | + // exponent is negative number of the decimals |
|
| 213 | + $e = -strlen($parts[1]); |
|
| 214 | + // append decimals to the mantissa |
|
| 215 | + $m .= $parts[1]; |
|
| 216 | + } else { |
|
| 217 | + $e = 0; |
|
| 218 | + } |
|
| 219 | + // shift trailing zeroes from the mantissa to the exponent |
|
| 220 | + while (substr($m, -1) === "0") { |
|
| 221 | + $e++; |
|
| 222 | + $m = substr($m, 0, -1); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 226 | 226 | (X.690 07-2002, section 11.3.2.6) */ |
| 227 | - if (0 == $e) { |
|
| 228 | - $es = "+"; |
|
| 229 | - } else { |
|
| 230 | - $es = $e < 0 ? "-" : ""; |
|
| 231 | - } |
|
| 232 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 233 | - } |
|
| 227 | + if (0 == $e) { |
|
| 228 | + $es = "+"; |
|
| 229 | + } else { |
|
| 230 | + $es = $e < 0 ? "-" : ""; |
|
| 231 | + } |
|
| 232 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * Convert NR3 form number to decimal. |
|
| 237 | - * |
|
| 238 | - * @param string $str |
|
| 239 | - * @throws \UnexpectedValueException |
|
| 240 | - * @return float |
|
| 241 | - */ |
|
| 242 | - private static function _nr3ToDecimal(string $str): float |
|
| 243 | - { |
|
| 244 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 245 | - throw new \UnexpectedValueException( |
|
| 246 | - "'$str' is not a valid NR3 form real."); |
|
| 247 | - } |
|
| 248 | - $m = $match[2]; |
|
| 249 | - // if number started with minus sign |
|
| 250 | - $inv = $match[1] == "-"; |
|
| 251 | - $e = intval($match[3]); |
|
| 252 | - // positive exponent |
|
| 253 | - if ($e > 0) { |
|
| 254 | - // pad with trailing zeroes |
|
| 255 | - $num = $m . str_repeat("0", $e); |
|
| 256 | - } else if ($e < 0) { |
|
| 257 | - // pad with leading zeroes |
|
| 258 | - if (strlen($m) < abs($e)) { |
|
| 259 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 260 | - } |
|
| 261 | - // insert decimal point |
|
| 262 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 263 | - } else { |
|
| 264 | - $num = empty($m) ? "0" : $m; |
|
| 265 | - } |
|
| 266 | - // if number is negative |
|
| 267 | - if ($inv) { |
|
| 268 | - $num = "-$num"; |
|
| 269 | - } |
|
| 270 | - return floatval($num); |
|
| 271 | - } |
|
| 235 | + /** |
|
| 236 | + * Convert NR3 form number to decimal. |
|
| 237 | + * |
|
| 238 | + * @param string $str |
|
| 239 | + * @throws \UnexpectedValueException |
|
| 240 | + * @return float |
|
| 241 | + */ |
|
| 242 | + private static function _nr3ToDecimal(string $str): float |
|
| 243 | + { |
|
| 244 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 245 | + throw new \UnexpectedValueException( |
|
| 246 | + "'$str' is not a valid NR3 form real."); |
|
| 247 | + } |
|
| 248 | + $m = $match[2]; |
|
| 249 | + // if number started with minus sign |
|
| 250 | + $inv = $match[1] == "-"; |
|
| 251 | + $e = intval($match[3]); |
|
| 252 | + // positive exponent |
|
| 253 | + if ($e > 0) { |
|
| 254 | + // pad with trailing zeroes |
|
| 255 | + $num = $m . str_repeat("0", $e); |
|
| 256 | + } else if ($e < 0) { |
|
| 257 | + // pad with leading zeroes |
|
| 258 | + if (strlen($m) < abs($e)) { |
|
| 259 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 260 | + } |
|
| 261 | + // insert decimal point |
|
| 262 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 263 | + } else { |
|
| 264 | + $num = empty($m) ? "0" : $m; |
|
| 265 | + } |
|
| 266 | + // if number is negative |
|
| 267 | + if ($inv) { |
|
| 268 | + $num = "-$num"; |
|
| 269 | + } |
|
| 270 | + return floatval($num); |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - /** |
|
| 274 | - * Test that number is valid for this context. |
|
| 275 | - * |
|
| 276 | - * @param mixed $num |
|
| 277 | - * @return boolean |
|
| 278 | - */ |
|
| 279 | - private static function _validateNumber($num): bool |
|
| 280 | - { |
|
| 281 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 282 | - return false; |
|
| 283 | - } |
|
| 284 | - return true; |
|
| 285 | - } |
|
| 273 | + /** |
|
| 274 | + * Test that number is valid for this context. |
|
| 275 | + * |
|
| 276 | + * @param mixed $num |
|
| 277 | + * @return boolean |
|
| 278 | + */ |
|
| 279 | + private static function _validateNumber($num): bool |
|
| 280 | + { |
|
| 281 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 282 | + return false; |
|
| 283 | + } |
|
| 284 | + return true; |
|
| 285 | + } |
|
| 286 | 286 | } |
@@ -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 | |
@@ -32,14 +32,14 @@ discard block |
||
| 32 | 32 | * @link http://php.net/manual/en/language.types.float.php |
| 33 | 33 | * @var string |
| 34 | 34 | */ |
| 35 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 36 | - '([+\-]?'. // sign |
|
| 37 | - '(?:'. |
|
| 38 | - '\d+'. // LNUM |
|
| 39 | - '|'. |
|
| 40 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 41 | - '))[eE]'. |
|
| 42 | - '([+\-]?\d+)'. // exponent |
|
| 35 | + const PHP_EXPONENT_DNUM = '/^' . /* @formatter:off */ |
|
| 36 | + '([+\-]?' . // sign |
|
| 37 | + '(?:' . |
|
| 38 | + '\d+' . // LNUM |
|
| 39 | + '|' . |
|
| 40 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
| 41 | + '))[eE]' . |
|
| 42 | + '([+\-]?\d+)' . // exponent |
|
| 43 | 43 | '$/'; /* @formatter:on */ |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | * @return self |
| 115 | 115 | */ |
| 116 | 116 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 117 | - int &$offset) |
|
| 117 | + int & $offset) |
|
| 118 | 118 | { |
| 119 | 119 | $idx = $offset; |
| 120 | 120 | $length = Length::expectFromDER($data, $idx); |
@@ -9,10 +9,10 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface Encodable |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Encode object to DER. |
|
| 14 | - * |
|
| 15 | - * @return string |
|
| 16 | - */ |
|
| 17 | - public function toDER(): string; |
|
| 12 | + /** |
|
| 13 | + * Encode object to DER. |
|
| 14 | + * |
|
| 15 | + * @return string |
|
| 16 | + */ |
|
| 17 | + public function toDER(): string; |
|
| 18 | 18 | } |
@@ -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\Feature; |
| 6 | 6 | |
@@ -11,79 +11,79 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | interface ElementBase extends Encodable |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Get the class of the ASN.1 type. |
|
| 16 | - * |
|
| 17 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
| 18 | - * |
|
| 19 | - * @return int |
|
| 20 | - */ |
|
| 21 | - public function typeClass(): int; |
|
| 14 | + /** |
|
| 15 | + * Get the class of the ASN.1 type. |
|
| 16 | + * |
|
| 17 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
| 18 | + * |
|
| 19 | + * @return int |
|
| 20 | + */ |
|
| 21 | + public function typeClass(): int; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Check whether the element is constructed. |
|
| 25 | - * |
|
| 26 | - * Otherwise it's primitive. |
|
| 27 | - * |
|
| 28 | - * @return bool |
|
| 29 | - */ |
|
| 30 | - public function isConstructed(): bool; |
|
| 23 | + /** |
|
| 24 | + * Check whether the element is constructed. |
|
| 25 | + * |
|
| 26 | + * Otherwise it's primitive. |
|
| 27 | + * |
|
| 28 | + * @return bool |
|
| 29 | + */ |
|
| 30 | + public function isConstructed(): bool; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Get the tag of the element. |
|
| 34 | - * |
|
| 35 | - * Interpretation of the tag depends on the context. For example it may |
|
| 36 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
| 37 | - * tagged type. |
|
| 38 | - * |
|
| 39 | - * @return int |
|
| 40 | - */ |
|
| 41 | - public function tag(); |
|
| 32 | + /** |
|
| 33 | + * Get the tag of the element. |
|
| 34 | + * |
|
| 35 | + * Interpretation of the tag depends on the context. For example it may |
|
| 36 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
| 37 | + * tagged type. |
|
| 38 | + * |
|
| 39 | + * @return int |
|
| 40 | + */ |
|
| 41 | + public function tag(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Check whether the element is a type of a given tag. |
|
| 45 | - * |
|
| 46 | - * @param int $tag Type tag |
|
| 47 | - * @return boolean |
|
| 48 | - */ |
|
| 49 | - public function isType($tag): bool; |
|
| 43 | + /** |
|
| 44 | + * Check whether the element is a type of a given tag. |
|
| 45 | + * |
|
| 46 | + * @param int $tag Type tag |
|
| 47 | + * @return boolean |
|
| 48 | + */ |
|
| 49 | + public function isType($tag): bool; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Check whether the element is a type of a given tag. |
|
| 53 | - * |
|
| 54 | - * Throws an exception if expectation fails. |
|
| 55 | - * |
|
| 56 | - * @param int $tag Type tag |
|
| 57 | - * @throws \UnexpectedValueException If the element type differs from the |
|
| 58 | - * expected |
|
| 59 | - * @return ElementBase |
|
| 60 | - */ |
|
| 61 | - public function expectType($tag): ElementBase; |
|
| 51 | + /** |
|
| 52 | + * Check whether the element is a type of a given tag. |
|
| 53 | + * |
|
| 54 | + * Throws an exception if expectation fails. |
|
| 55 | + * |
|
| 56 | + * @param int $tag Type tag |
|
| 57 | + * @throws \UnexpectedValueException If the element type differs from the |
|
| 58 | + * expected |
|
| 59 | + * @return ElementBase |
|
| 60 | + */ |
|
| 61 | + public function expectType($tag): ElementBase; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Check whether the element is tagged (context specific). |
|
| 65 | - * |
|
| 66 | - * @return bool |
|
| 67 | - */ |
|
| 68 | - public function isTagged(): bool; |
|
| 63 | + /** |
|
| 64 | + * Check whether the element is tagged (context specific). |
|
| 65 | + * |
|
| 66 | + * @return bool |
|
| 67 | + */ |
|
| 68 | + public function isTagged(): bool; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Check whether the element is tagged (context specific) and optionally has |
|
| 72 | - * a given tag. |
|
| 73 | - * |
|
| 74 | - * Throws an exception if the element is not tagged or tag differs from |
|
| 75 | - * the expected. |
|
| 76 | - * |
|
| 77 | - * @param int|null $tag Optional type tag |
|
| 78 | - * @throws \UnexpectedValueException If expectation fails |
|
| 79 | - * @return \ASN1\Type\TaggedType |
|
| 80 | - */ |
|
| 81 | - public function expectTagged($tag = null): TaggedType; |
|
| 70 | + /** |
|
| 71 | + * Check whether the element is tagged (context specific) and optionally has |
|
| 72 | + * a given tag. |
|
| 73 | + * |
|
| 74 | + * Throws an exception if the element is not tagged or tag differs from |
|
| 75 | + * the expected. |
|
| 76 | + * |
|
| 77 | + * @param int|null $tag Optional type tag |
|
| 78 | + * @throws \UnexpectedValueException If expectation fails |
|
| 79 | + * @return \ASN1\Type\TaggedType |
|
| 80 | + */ |
|
| 81 | + public function expectTagged($tag = null): TaggedType; |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Get the object as an abstract Element instance. |
|
| 85 | - * |
|
| 86 | - * @return \ASN1\Element |
|
| 87 | - */ |
|
| 88 | - public function asElement(): Element; |
|
| 83 | + /** |
|
| 84 | + * Get the object as an abstract Element instance. |
|
| 85 | + * |
|
| 86 | + * @return \ASN1\Element |
|
| 87 | + */ |
|
| 88 | + public function asElement(): Element; |
|
| 89 | 89 | } |
@@ -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\Feature; |
| 6 | 6 | use ASN1\Element; |
@@ -12,190 +12,190 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Length implements Encodable |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Length. |
|
| 17 | - * |
|
| 18 | - * @var int|string |
|
| 19 | - */ |
|
| 20 | - private $_length; |
|
| 15 | + /** |
|
| 16 | + * Length. |
|
| 17 | + * |
|
| 18 | + * @var int|string |
|
| 19 | + */ |
|
| 20 | + private $_length; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Whether length is indefinite. |
|
| 24 | - * |
|
| 25 | - * @var boolean |
|
| 26 | - */ |
|
| 27 | - private $_indefinite; |
|
| 22 | + /** |
|
| 23 | + * Whether length is indefinite. |
|
| 24 | + * |
|
| 25 | + * @var boolean |
|
| 26 | + */ |
|
| 27 | + private $_indefinite; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param int|string $length Length |
|
| 33 | - * @param boolean $indefinite Whether length is indefinite |
|
| 34 | - */ |
|
| 35 | - public function __construct($length, bool $indefinite = false) |
|
| 36 | - { |
|
| 37 | - $this->_length = $length; |
|
| 38 | - $this->_indefinite = $indefinite; |
|
| 39 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param int|string $length Length |
|
| 33 | + * @param boolean $indefinite Whether length is indefinite |
|
| 34 | + */ |
|
| 35 | + public function __construct($length, bool $indefinite = false) |
|
| 36 | + { |
|
| 37 | + $this->_length = $length; |
|
| 38 | + $this->_indefinite = $indefinite; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Decode length component from DER data. |
|
| 43 | - * |
|
| 44 | - * @param string $data DER encoded data |
|
| 45 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 46 | - * into the data where to start parsing. Variable is updated to |
|
| 47 | - * the offset next to the parsed length component. If null, start |
|
| 48 | - * from offset 0. |
|
| 49 | - * @throws DecodeException If decoding fails |
|
| 50 | - * @return self |
|
| 51 | - */ |
|
| 52 | - public static function fromDER(string $data, int &$offset = null): self |
|
| 53 | - { |
|
| 54 | - $idx = $offset ? $offset : 0; |
|
| 55 | - $datalen = strlen($data); |
|
| 56 | - if ($idx >= $datalen) { |
|
| 57 | - throw new DecodeException("Invalid offset."); |
|
| 58 | - } |
|
| 59 | - $indefinite = false; |
|
| 60 | - $byte = ord($data[$idx++]); |
|
| 61 | - // bits 7 to 1 |
|
| 62 | - $length = (0x7f & $byte); |
|
| 63 | - // long form |
|
| 64 | - if (0x80 & $byte) { |
|
| 65 | - if (!$length) { |
|
| 66 | - $indefinite = true; |
|
| 67 | - } else { |
|
| 68 | - if ($idx + $length > $datalen) { |
|
| 69 | - throw new DecodeException("Too many length octets."); |
|
| 70 | - } |
|
| 71 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - if (isset($offset)) { |
|
| 75 | - $offset = $idx; |
|
| 76 | - } |
|
| 77 | - return new self($length, $indefinite); |
|
| 78 | - } |
|
| 41 | + /** |
|
| 42 | + * Decode length component from DER data. |
|
| 43 | + * |
|
| 44 | + * @param string $data DER encoded data |
|
| 45 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 46 | + * into the data where to start parsing. Variable is updated to |
|
| 47 | + * the offset next to the parsed length component. If null, start |
|
| 48 | + * from offset 0. |
|
| 49 | + * @throws DecodeException If decoding fails |
|
| 50 | + * @return self |
|
| 51 | + */ |
|
| 52 | + public static function fromDER(string $data, int &$offset = null): self |
|
| 53 | + { |
|
| 54 | + $idx = $offset ? $offset : 0; |
|
| 55 | + $datalen = strlen($data); |
|
| 56 | + if ($idx >= $datalen) { |
|
| 57 | + throw new DecodeException("Invalid offset."); |
|
| 58 | + } |
|
| 59 | + $indefinite = false; |
|
| 60 | + $byte = ord($data[$idx++]); |
|
| 61 | + // bits 7 to 1 |
|
| 62 | + $length = (0x7f & $byte); |
|
| 63 | + // long form |
|
| 64 | + if (0x80 & $byte) { |
|
| 65 | + if (!$length) { |
|
| 66 | + $indefinite = true; |
|
| 67 | + } else { |
|
| 68 | + if ($idx + $length > $datalen) { |
|
| 69 | + throw new DecodeException("Too many length octets."); |
|
| 70 | + } |
|
| 71 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + if (isset($offset)) { |
|
| 75 | + $offset = $idx; |
|
| 76 | + } |
|
| 77 | + return new self($length, $indefinite); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Decode long form length. |
|
| 82 | - * |
|
| 83 | - * @param int $length Number of octets |
|
| 84 | - * @param string $data Data |
|
| 85 | - * @param int $offset Reference to the variable containing offset to the |
|
| 86 | - * data. |
|
| 87 | - * @throws DecodeException If decoding fails |
|
| 88 | - * @return int|string |
|
| 89 | - */ |
|
| 90 | - private static function _decodeLongFormLength(int $length, string $data, int &$offset) |
|
| 91 | - { |
|
| 92 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
| 93 | - if ($length == 127) { |
|
| 94 | - throw new DecodeException("Invalid number of length octets."); |
|
| 95 | - } |
|
| 96 | - $num = gmp_init(0, 10); |
|
| 97 | - while (--$length >= 0) { |
|
| 98 | - $byte = ord($data[$offset++]); |
|
| 99 | - $num <<= 8; |
|
| 100 | - $num |= $byte; |
|
| 101 | - } |
|
| 80 | + /** |
|
| 81 | + * Decode long form length. |
|
| 82 | + * |
|
| 83 | + * @param int $length Number of octets |
|
| 84 | + * @param string $data Data |
|
| 85 | + * @param int $offset Reference to the variable containing offset to the |
|
| 86 | + * data. |
|
| 87 | + * @throws DecodeException If decoding fails |
|
| 88 | + * @return int|string |
|
| 89 | + */ |
|
| 90 | + private static function _decodeLongFormLength(int $length, string $data, int &$offset) |
|
| 91 | + { |
|
| 92 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
| 93 | + if ($length == 127) { |
|
| 94 | + throw new DecodeException("Invalid number of length octets."); |
|
| 95 | + } |
|
| 96 | + $num = gmp_init(0, 10); |
|
| 97 | + while (--$length >= 0) { |
|
| 98 | + $byte = ord($data[$offset++]); |
|
| 99 | + $num <<= 8; |
|
| 100 | + $num |= $byte; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - return gmp_strval($num); |
|
| 104 | - } |
|
| 103 | + return gmp_strval($num); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Decode length from DER. |
|
| 108 | - * |
|
| 109 | - * Throws an exception if length doesn't match with expected or if data |
|
| 110 | - * doesn't contain enough bytes. |
|
| 111 | - * |
|
| 112 | - * @see self::fromDER |
|
| 113 | - * @param string $data DER data |
|
| 114 | - * @param int $offset Reference to the offset variable |
|
| 115 | - * @param int|null $expected Expected length, null to bypass checking |
|
| 116 | - * @throws DecodeException If decoding or expectation fails |
|
| 117 | - * @return self |
|
| 118 | - */ |
|
| 119 | - public static function expectFromDER(string $data, int &$offset, int $expected = null): self |
|
| 120 | - { |
|
| 121 | - $idx = $offset; |
|
| 122 | - $length = self::fromDER($data, $idx); |
|
| 123 | - // DER encoding must have definite length (spec 10.1) |
|
| 124 | - if ($length->isIndefinite()) { |
|
| 125 | - throw new DecodeException("DER encoding must have definite length."); |
|
| 126 | - } |
|
| 127 | - // if certain length was expected |
|
| 128 | - if (isset($expected) && $expected != $length->_length) { |
|
| 129 | - throw new DecodeException( |
|
| 130 | - sprintf("Expected length %d, got %d.", $expected, |
|
| 131 | - $length->_length)); |
|
| 132 | - } |
|
| 133 | - // check that enough data is available |
|
| 134 | - if (strlen($data) < $idx + $length->_length) { |
|
| 135 | - throw new DecodeException( |
|
| 136 | - sprintf("Length %d overflows data, %d bytes left.", |
|
| 137 | - $length->_length, strlen($data) - $idx)); |
|
| 138 | - } |
|
| 139 | - $offset = $idx; |
|
| 140 | - return $length; |
|
| 141 | - } |
|
| 106 | + /** |
|
| 107 | + * Decode length from DER. |
|
| 108 | + * |
|
| 109 | + * Throws an exception if length doesn't match with expected or if data |
|
| 110 | + * doesn't contain enough bytes. |
|
| 111 | + * |
|
| 112 | + * @see self::fromDER |
|
| 113 | + * @param string $data DER data |
|
| 114 | + * @param int $offset Reference to the offset variable |
|
| 115 | + * @param int|null $expected Expected length, null to bypass checking |
|
| 116 | + * @throws DecodeException If decoding or expectation fails |
|
| 117 | + * @return self |
|
| 118 | + */ |
|
| 119 | + public static function expectFromDER(string $data, int &$offset, int $expected = null): self |
|
| 120 | + { |
|
| 121 | + $idx = $offset; |
|
| 122 | + $length = self::fromDER($data, $idx); |
|
| 123 | + // DER encoding must have definite length (spec 10.1) |
|
| 124 | + if ($length->isIndefinite()) { |
|
| 125 | + throw new DecodeException("DER encoding must have definite length."); |
|
| 126 | + } |
|
| 127 | + // if certain length was expected |
|
| 128 | + if (isset($expected) && $expected != $length->_length) { |
|
| 129 | + throw new DecodeException( |
|
| 130 | + sprintf("Expected length %d, got %d.", $expected, |
|
| 131 | + $length->_length)); |
|
| 132 | + } |
|
| 133 | + // check that enough data is available |
|
| 134 | + if (strlen($data) < $idx + $length->_length) { |
|
| 135 | + throw new DecodeException( |
|
| 136 | + sprintf("Length %d overflows data, %d bytes left.", |
|
| 137 | + $length->_length, strlen($data) - $idx)); |
|
| 138 | + } |
|
| 139 | + $offset = $idx; |
|
| 140 | + return $length; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * |
|
| 145 | - * @see Encodable::toDER() |
|
| 146 | - * @throws \DomainException If length is too large to encode |
|
| 147 | - * @return string |
|
| 148 | - */ |
|
| 149 | - public function toDER(): string |
|
| 150 | - { |
|
| 151 | - $bytes = []; |
|
| 152 | - if ($this->_indefinite) { |
|
| 153 | - $bytes[] = 0x80; |
|
| 154 | - } else { |
|
| 155 | - $num = gmp_init($this->_length, 10); |
|
| 156 | - // long form |
|
| 157 | - if ($num > 127) { |
|
| 158 | - $octets = []; |
|
| 159 | - for (; $num > 0; $num >>= 8) { |
|
| 160 | - $octets[] = gmp_intval(0xff & $num); |
|
| 161 | - } |
|
| 162 | - $count = count($octets); |
|
| 163 | - // first octet must not be 0xff |
|
| 164 | - if ($count >= 127) { |
|
| 165 | - throw new \DomainException("Too many length octets."); |
|
| 166 | - } |
|
| 167 | - $bytes[] = 0x80 | $count; |
|
| 168 | - foreach (array_reverse($octets) as $octet) { |
|
| 169 | - $bytes[] = $octet; |
|
| 170 | - } |
|
| 171 | - } else { // short form |
|
| 172 | - $bytes[] = gmp_intval($num); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - return pack("C*", ...$bytes); |
|
| 176 | - } |
|
| 143 | + /** |
|
| 144 | + * |
|
| 145 | + * @see Encodable::toDER() |
|
| 146 | + * @throws \DomainException If length is too large to encode |
|
| 147 | + * @return string |
|
| 148 | + */ |
|
| 149 | + public function toDER(): string |
|
| 150 | + { |
|
| 151 | + $bytes = []; |
|
| 152 | + if ($this->_indefinite) { |
|
| 153 | + $bytes[] = 0x80; |
|
| 154 | + } else { |
|
| 155 | + $num = gmp_init($this->_length, 10); |
|
| 156 | + // long form |
|
| 157 | + if ($num > 127) { |
|
| 158 | + $octets = []; |
|
| 159 | + for (; $num > 0; $num >>= 8) { |
|
| 160 | + $octets[] = gmp_intval(0xff & $num); |
|
| 161 | + } |
|
| 162 | + $count = count($octets); |
|
| 163 | + // first octet must not be 0xff |
|
| 164 | + if ($count >= 127) { |
|
| 165 | + throw new \DomainException("Too many length octets."); |
|
| 166 | + } |
|
| 167 | + $bytes[] = 0x80 | $count; |
|
| 168 | + foreach (array_reverse($octets) as $octet) { |
|
| 169 | + $bytes[] = $octet; |
|
| 170 | + } |
|
| 171 | + } else { // short form |
|
| 172 | + $bytes[] = gmp_intval($num); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + return pack("C*", ...$bytes); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Get the length. |
|
| 180 | - * |
|
| 181 | - * @throws \LogicException If length is indefinite |
|
| 182 | - * @return int|string |
|
| 183 | - */ |
|
| 184 | - public function length() |
|
| 185 | - { |
|
| 186 | - if ($this->_indefinite) { |
|
| 187 | - throw new \LogicException("Length is indefinite."); |
|
| 188 | - } |
|
| 189 | - return $this->_length; |
|
| 190 | - } |
|
| 178 | + /** |
|
| 179 | + * Get the length. |
|
| 180 | + * |
|
| 181 | + * @throws \LogicException If length is indefinite |
|
| 182 | + * @return int|string |
|
| 183 | + */ |
|
| 184 | + public function length() |
|
| 185 | + { |
|
| 186 | + if ($this->_indefinite) { |
|
| 187 | + throw new \LogicException("Length is indefinite."); |
|
| 188 | + } |
|
| 189 | + return $this->_length; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Whether length is indefinite. |
|
| 194 | - * |
|
| 195 | - * @return boolean |
|
| 196 | - */ |
|
| 197 | - public function isIndefinite(): bool |
|
| 198 | - { |
|
| 199 | - return $this->_indefinite; |
|
| 200 | - } |
|
| 192 | + /** |
|
| 193 | + * Whether length is indefinite. |
|
| 194 | + * |
|
| 195 | + * @return boolean |
|
| 196 | + */ |
|
| 197 | + public function isIndefinite(): bool |
|
| 198 | + { |
|
| 199 | + return $this->_indefinite; |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -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\Component; |
| 6 | 6 | |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * @throws DecodeException If decoding fails |
| 50 | 50 | * @return self |
| 51 | 51 | */ |
| 52 | - public static function fromDER(string $data, int &$offset = null): self |
|
| 52 | + public static function fromDER(string $data, int & $offset = null): self |
|
| 53 | 53 | { |
| 54 | 54 | $idx = $offset ? $offset : 0; |
| 55 | 55 | $datalen = strlen($data); |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @throws DecodeException If decoding fails |
| 88 | 88 | * @return int|string |
| 89 | 89 | */ |
| 90 | - private static function _decodeLongFormLength(int $length, string $data, int &$offset) |
|
| 90 | + private static function _decodeLongFormLength(int $length, string $data, int & $offset) |
|
| 91 | 91 | { |
| 92 | 92 | // first octet must not be 0xff (spec 8.1.3.5c) |
| 93 | 93 | if ($length == 127) { |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * @throws DecodeException If decoding or expectation fails |
| 117 | 117 | * @return self |
| 118 | 118 | */ |
| 119 | - public static function expectFromDER(string $data, int &$offset, int $expected = null): self |
|
| 119 | + public static function expectFromDER(string $data, int & $offset, int $expected = null): self |
|
| 120 | 120 | { |
| 121 | 121 | $idx = $offset; |
| 122 | 122 | $length = self::fromDER($data, $idx); |
@@ -12,286 +12,286 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Identifier implements Encodable |
| 14 | 14 | { |
| 15 | - // Type class enumerations |
|
| 16 | - const CLASS_UNIVERSAL = 0b00; |
|
| 17 | - const CLASS_APPLICATION = 0b01; |
|
| 18 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
| 19 | - const CLASS_PRIVATE = 0b11; |
|
| 15 | + // Type class enumerations |
|
| 16 | + const CLASS_UNIVERSAL = 0b00; |
|
| 17 | + const CLASS_APPLICATION = 0b01; |
|
| 18 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
| 19 | + const CLASS_PRIVATE = 0b11; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Mapping from type class to human readable name. |
|
| 23 | - * |
|
| 24 | - * @internal |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - const MAP_CLASS_TO_NAME = [ |
|
| 29 | - /* @formatter:off */ |
|
| 30 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
| 31 | - self::CLASS_APPLICATION => "APPLICATION", |
|
| 32 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
| 33 | - self::CLASS_PRIVATE => "PRIVATE", |
|
| 34 | - /* @formatter:on */ |
|
| 35 | - ]; |
|
| 21 | + /** |
|
| 22 | + * Mapping from type class to human readable name. |
|
| 23 | + * |
|
| 24 | + * @internal |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + const MAP_CLASS_TO_NAME = [ |
|
| 29 | + /* @formatter:off */ |
|
| 30 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
| 31 | + self::CLASS_APPLICATION => "APPLICATION", |
|
| 32 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
| 33 | + self::CLASS_PRIVATE => "PRIVATE", |
|
| 34 | + /* @formatter:on */ |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - // P/C enumerations |
|
| 38 | - const PRIMITIVE = 0b0; |
|
| 39 | - const CONSTRUCTED = 0b1; |
|
| 37 | + // P/C enumerations |
|
| 38 | + const PRIMITIVE = 0b0; |
|
| 39 | + const CONSTRUCTED = 0b1; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Type class. |
|
| 43 | - * |
|
| 44 | - * @var int |
|
| 45 | - */ |
|
| 46 | - private $_class; |
|
| 41 | + /** |
|
| 42 | + * Type class. |
|
| 43 | + * |
|
| 44 | + * @var int |
|
| 45 | + */ |
|
| 46 | + private $_class; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Primitive or Constructed. |
|
| 50 | - * |
|
| 51 | - * @var int |
|
| 52 | - */ |
|
| 53 | - private $_pc; |
|
| 48 | + /** |
|
| 49 | + * Primitive or Constructed. |
|
| 50 | + * |
|
| 51 | + * @var int |
|
| 52 | + */ |
|
| 53 | + private $_pc; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Content type tag. |
|
| 57 | - * |
|
| 58 | - * @var int|string |
|
| 59 | - */ |
|
| 60 | - private $_tag; |
|
| 55 | + /** |
|
| 56 | + * Content type tag. |
|
| 57 | + * |
|
| 58 | + * @var int|string |
|
| 59 | + */ |
|
| 60 | + private $_tag; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Constructor. |
|
| 64 | - * |
|
| 65 | - * @param int $class Type class |
|
| 66 | - * @param int $pc Primitive / Constructed |
|
| 67 | - * @param int|string $tag Type tag number |
|
| 68 | - */ |
|
| 69 | - public function __construct(int $class, int $pc, $tag) |
|
| 70 | - { |
|
| 71 | - $this->_class = 0b11 & $class; |
|
| 72 | - $this->_pc = 0b1 & $pc; |
|
| 73 | - $this->_tag = $tag; |
|
| 74 | - } |
|
| 62 | + /** |
|
| 63 | + * Constructor. |
|
| 64 | + * |
|
| 65 | + * @param int $class Type class |
|
| 66 | + * @param int $pc Primitive / Constructed |
|
| 67 | + * @param int|string $tag Type tag number |
|
| 68 | + */ |
|
| 69 | + public function __construct(int $class, int $pc, $tag) |
|
| 70 | + { |
|
| 71 | + $this->_class = 0b11 & $class; |
|
| 72 | + $this->_pc = 0b1 & $pc; |
|
| 73 | + $this->_tag = $tag; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Decode identifier component from DER data. |
|
| 78 | - * |
|
| 79 | - * @param string $data DER encoded data |
|
| 80 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 81 | - * into the data where to start parsing. Variable is updated to |
|
| 82 | - * the offset next to the parsed identifier. If null, start from |
|
| 83 | - * offset 0. |
|
| 84 | - * @throws DecodeException If decoding fails |
|
| 85 | - * @return self |
|
| 86 | - */ |
|
| 87 | - public static function fromDER(string $data, int &$offset = null): self |
|
| 88 | - { |
|
| 89 | - $idx = $offset ? $offset : 0; |
|
| 90 | - $datalen = strlen($data); |
|
| 91 | - if ($idx >= $datalen) { |
|
| 92 | - throw new DecodeException("Invalid offset."); |
|
| 93 | - } |
|
| 94 | - $byte = ord($data[$idx++]); |
|
| 95 | - // bits 8 and 7 (class) |
|
| 96 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 97 | - $class = (0b11000000 & $byte) >> 6; |
|
| 98 | - // bit 6 (0 = primitive / 1 = constructed) |
|
| 99 | - $pc = (0b00100000 & $byte) >> 5; |
|
| 100 | - // bits 5 to 1 (tag number) |
|
| 101 | - $tag = (0b00011111 & $byte); |
|
| 102 | - // long-form identifier |
|
| 103 | - if (0x1f == $tag) { |
|
| 104 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
| 105 | - } |
|
| 106 | - if (isset($offset)) { |
|
| 107 | - $offset = $idx; |
|
| 108 | - } |
|
| 109 | - return new self($class, $pc, $tag); |
|
| 110 | - } |
|
| 76 | + /** |
|
| 77 | + * Decode identifier component from DER data. |
|
| 78 | + * |
|
| 79 | + * @param string $data DER encoded data |
|
| 80 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 81 | + * into the data where to start parsing. Variable is updated to |
|
| 82 | + * the offset next to the parsed identifier. If null, start from |
|
| 83 | + * offset 0. |
|
| 84 | + * @throws DecodeException If decoding fails |
|
| 85 | + * @return self |
|
| 86 | + */ |
|
| 87 | + public static function fromDER(string $data, int &$offset = null): self |
|
| 88 | + { |
|
| 89 | + $idx = $offset ? $offset : 0; |
|
| 90 | + $datalen = strlen($data); |
|
| 91 | + if ($idx >= $datalen) { |
|
| 92 | + throw new DecodeException("Invalid offset."); |
|
| 93 | + } |
|
| 94 | + $byte = ord($data[$idx++]); |
|
| 95 | + // bits 8 and 7 (class) |
|
| 96 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 97 | + $class = (0b11000000 & $byte) >> 6; |
|
| 98 | + // bit 6 (0 = primitive / 1 = constructed) |
|
| 99 | + $pc = (0b00100000 & $byte) >> 5; |
|
| 100 | + // bits 5 to 1 (tag number) |
|
| 101 | + $tag = (0b00011111 & $byte); |
|
| 102 | + // long-form identifier |
|
| 103 | + if (0x1f == $tag) { |
|
| 104 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
| 105 | + } |
|
| 106 | + if (isset($offset)) { |
|
| 107 | + $offset = $idx; |
|
| 108 | + } |
|
| 109 | + return new self($class, $pc, $tag); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Parse long form tag. |
|
| 114 | - * |
|
| 115 | - * @param string $data DER data |
|
| 116 | - * @param int $offset Reference to the variable containing offset to data |
|
| 117 | - * @throws DecodeException If decoding fails |
|
| 118 | - * @return string Tag number |
|
| 119 | - */ |
|
| 120 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 121 | - { |
|
| 122 | - $datalen = strlen($data); |
|
| 123 | - $tag = gmp_init(0, 10); |
|
| 124 | - while (true) { |
|
| 125 | - if ($offset >= $datalen) { |
|
| 126 | - throw new DecodeException( |
|
| 127 | - "Unexpected end of data while decoding" . |
|
| 128 | - " long form identifier."); |
|
| 129 | - } |
|
| 130 | - $byte = ord($data[$offset++]); |
|
| 131 | - $tag <<= 7; |
|
| 132 | - $tag |= 0x7f & $byte; |
|
| 133 | - // last byte has bit 8 set to zero |
|
| 134 | - if (!(0x80 & $byte)) { |
|
| 135 | - break; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - return gmp_strval($tag, 10); |
|
| 139 | - } |
|
| 112 | + /** |
|
| 113 | + * Parse long form tag. |
|
| 114 | + * |
|
| 115 | + * @param string $data DER data |
|
| 116 | + * @param int $offset Reference to the variable containing offset to data |
|
| 117 | + * @throws DecodeException If decoding fails |
|
| 118 | + * @return string Tag number |
|
| 119 | + */ |
|
| 120 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 121 | + { |
|
| 122 | + $datalen = strlen($data); |
|
| 123 | + $tag = gmp_init(0, 10); |
|
| 124 | + while (true) { |
|
| 125 | + if ($offset >= $datalen) { |
|
| 126 | + throw new DecodeException( |
|
| 127 | + "Unexpected end of data while decoding" . |
|
| 128 | + " long form identifier."); |
|
| 129 | + } |
|
| 130 | + $byte = ord($data[$offset++]); |
|
| 131 | + $tag <<= 7; |
|
| 132 | + $tag |= 0x7f & $byte; |
|
| 133 | + // last byte has bit 8 set to zero |
|
| 134 | + if (!(0x80 & $byte)) { |
|
| 135 | + break; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + return gmp_strval($tag, 10); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * |
|
| 143 | - * @see Encodable::toDER() |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - public function toDER(): string |
|
| 147 | - { |
|
| 148 | - $bytes = []; |
|
| 149 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 150 | - $tag = gmp_init($this->_tag, 10); |
|
| 151 | - if ($tag < 0x1f) { |
|
| 152 | - $bytes[] = $byte | $tag; |
|
| 153 | - } else { // long-form identifier |
|
| 154 | - $bytes[] = $byte | 0x1f; |
|
| 155 | - $octets = []; |
|
| 156 | - for (; $tag > 0; $tag >>= 7) { |
|
| 157 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 158 | - } |
|
| 159 | - // last octet has bit 8 set to zero |
|
| 160 | - $octets[0] &= 0x7f; |
|
| 161 | - foreach (array_reverse($octets) as $octet) { |
|
| 162 | - $bytes[] = $octet; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - return pack("C*", ...$bytes); |
|
| 166 | - } |
|
| 141 | + /** |
|
| 142 | + * |
|
| 143 | + * @see Encodable::toDER() |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + public function toDER(): string |
|
| 147 | + { |
|
| 148 | + $bytes = []; |
|
| 149 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 150 | + $tag = gmp_init($this->_tag, 10); |
|
| 151 | + if ($tag < 0x1f) { |
|
| 152 | + $bytes[] = $byte | $tag; |
|
| 153 | + } else { // long-form identifier |
|
| 154 | + $bytes[] = $byte | 0x1f; |
|
| 155 | + $octets = []; |
|
| 156 | + for (; $tag > 0; $tag >>= 7) { |
|
| 157 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 158 | + } |
|
| 159 | + // last octet has bit 8 set to zero |
|
| 160 | + $octets[0] &= 0x7f; |
|
| 161 | + foreach (array_reverse($octets) as $octet) { |
|
| 162 | + $bytes[] = $octet; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + return pack("C*", ...$bytes); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Get class of the type. |
|
| 170 | - * |
|
| 171 | - * @return int |
|
| 172 | - */ |
|
| 173 | - public function typeClass(): int |
|
| 174 | - { |
|
| 175 | - return $this->_class; |
|
| 176 | - } |
|
| 168 | + /** |
|
| 169 | + * Get class of the type. |
|
| 170 | + * |
|
| 171 | + * @return int |
|
| 172 | + */ |
|
| 173 | + public function typeClass(): int |
|
| 174 | + { |
|
| 175 | + return $this->_class; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Get P/C. |
|
| 180 | - * |
|
| 181 | - * @return int |
|
| 182 | - */ |
|
| 183 | - public function pc(): int |
|
| 184 | - { |
|
| 185 | - return $this->_pc; |
|
| 186 | - } |
|
| 178 | + /** |
|
| 179 | + * Get P/C. |
|
| 180 | + * |
|
| 181 | + * @return int |
|
| 182 | + */ |
|
| 183 | + public function pc(): int |
|
| 184 | + { |
|
| 185 | + return $this->_pc; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Get the tag number. |
|
| 190 | - * |
|
| 191 | - * @return int|string |
|
| 192 | - */ |
|
| 193 | - public function tag() |
|
| 194 | - { |
|
| 195 | - return $this->_tag; |
|
| 196 | - } |
|
| 188 | + /** |
|
| 189 | + * Get the tag number. |
|
| 190 | + * |
|
| 191 | + * @return int|string |
|
| 192 | + */ |
|
| 193 | + public function tag() |
|
| 194 | + { |
|
| 195 | + return $this->_tag; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Check whether type is of an universal class. |
|
| 200 | - * |
|
| 201 | - * @return boolean |
|
| 202 | - */ |
|
| 203 | - public function isUniversal(): bool |
|
| 204 | - { |
|
| 205 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
| 206 | - } |
|
| 198 | + /** |
|
| 199 | + * Check whether type is of an universal class. |
|
| 200 | + * |
|
| 201 | + * @return boolean |
|
| 202 | + */ |
|
| 203 | + public function isUniversal(): bool |
|
| 204 | + { |
|
| 205 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Check whether type is of an application class. |
|
| 210 | - * |
|
| 211 | - * @return boolean |
|
| 212 | - */ |
|
| 213 | - public function isApplication(): bool |
|
| 214 | - { |
|
| 215 | - return self::CLASS_APPLICATION == $this->_class; |
|
| 216 | - } |
|
| 208 | + /** |
|
| 209 | + * Check whether type is of an application class. |
|
| 210 | + * |
|
| 211 | + * @return boolean |
|
| 212 | + */ |
|
| 213 | + public function isApplication(): bool |
|
| 214 | + { |
|
| 215 | + return self::CLASS_APPLICATION == $this->_class; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * Check whether type is of a context specific class. |
|
| 220 | - * |
|
| 221 | - * @return boolean |
|
| 222 | - */ |
|
| 223 | - public function isContextSpecific(): bool |
|
| 224 | - { |
|
| 225 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
| 226 | - } |
|
| 218 | + /** |
|
| 219 | + * Check whether type is of a context specific class. |
|
| 220 | + * |
|
| 221 | + * @return boolean |
|
| 222 | + */ |
|
| 223 | + public function isContextSpecific(): bool |
|
| 224 | + { |
|
| 225 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Check whether type is of a private class. |
|
| 230 | - * |
|
| 231 | - * @return boolean |
|
| 232 | - */ |
|
| 233 | - public function isPrivate(): bool |
|
| 234 | - { |
|
| 235 | - return self::CLASS_PRIVATE == $this->_class; |
|
| 236 | - } |
|
| 228 | + /** |
|
| 229 | + * Check whether type is of a private class. |
|
| 230 | + * |
|
| 231 | + * @return boolean |
|
| 232 | + */ |
|
| 233 | + public function isPrivate(): bool |
|
| 234 | + { |
|
| 235 | + return self::CLASS_PRIVATE == $this->_class; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * Check whether content is primitive type. |
|
| 240 | - * |
|
| 241 | - * @return boolean |
|
| 242 | - */ |
|
| 243 | - public function isPrimitive(): bool |
|
| 244 | - { |
|
| 245 | - return self::PRIMITIVE == $this->_pc; |
|
| 246 | - } |
|
| 238 | + /** |
|
| 239 | + * Check whether content is primitive type. |
|
| 240 | + * |
|
| 241 | + * @return boolean |
|
| 242 | + */ |
|
| 243 | + public function isPrimitive(): bool |
|
| 244 | + { |
|
| 245 | + return self::PRIMITIVE == $this->_pc; |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * Check hether content is constructed type. |
|
| 250 | - * |
|
| 251 | - * @return boolean |
|
| 252 | - */ |
|
| 253 | - public function isConstructed(): bool |
|
| 254 | - { |
|
| 255 | - return self::CONSTRUCTED == $this->_pc; |
|
| 256 | - } |
|
| 248 | + /** |
|
| 249 | + * Check hether content is constructed type. |
|
| 250 | + * |
|
| 251 | + * @return boolean |
|
| 252 | + */ |
|
| 253 | + public function isConstructed(): bool |
|
| 254 | + { |
|
| 255 | + return self::CONSTRUCTED == $this->_pc; |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * Get self with given type class. |
|
| 260 | - * |
|
| 261 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 262 | - * @return self |
|
| 263 | - */ |
|
| 264 | - public function withClass(int $class): self |
|
| 265 | - { |
|
| 266 | - $obj = clone $this; |
|
| 267 | - $obj->_class = $class; |
|
| 268 | - return $obj; |
|
| 269 | - } |
|
| 258 | + /** |
|
| 259 | + * Get self with given type class. |
|
| 260 | + * |
|
| 261 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 262 | + * @return self |
|
| 263 | + */ |
|
| 264 | + public function withClass(int $class): self |
|
| 265 | + { |
|
| 266 | + $obj = clone $this; |
|
| 267 | + $obj->_class = $class; |
|
| 268 | + return $obj; |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Get self with given type tag. |
|
| 273 | - * |
|
| 274 | - * @param int|string $tag Tag number |
|
| 275 | - * @return self |
|
| 276 | - */ |
|
| 277 | - public function withTag(int $tag): self |
|
| 278 | - { |
|
| 279 | - $obj = clone $this; |
|
| 280 | - $obj->_tag = $tag; |
|
| 281 | - return $obj; |
|
| 282 | - } |
|
| 271 | + /** |
|
| 272 | + * Get self with given type tag. |
|
| 273 | + * |
|
| 274 | + * @param int|string $tag Tag number |
|
| 275 | + * @return self |
|
| 276 | + */ |
|
| 277 | + public function withTag(int $tag): self |
|
| 278 | + { |
|
| 279 | + $obj = clone $this; |
|
| 280 | + $obj->_tag = $tag; |
|
| 281 | + return $obj; |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | - /** |
|
| 285 | - * Get human readable name of the type class. |
|
| 286 | - * |
|
| 287 | - * @param int $class |
|
| 288 | - * @return string |
|
| 289 | - */ |
|
| 290 | - public static function classToName(int $class): string |
|
| 291 | - { |
|
| 292 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 293 | - return "CLASS $class"; |
|
| 294 | - } |
|
| 295 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
| 296 | - } |
|
| 284 | + /** |
|
| 285 | + * Get human readable name of the type class. |
|
| 286 | + * |
|
| 287 | + * @param int $class |
|
| 288 | + * @return string |
|
| 289 | + */ |
|
| 290 | + public static function classToName(int $class): string |
|
| 291 | + { |
|
| 292 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 293 | + return "CLASS $class"; |
|
| 294 | + } |
|
| 295 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
| 296 | + } |
|
| 297 | 297 | } |
@@ -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\Component; |
| 6 | 6 | |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * @throws DecodeException If decoding fails |
| 85 | 85 | * @return self |
| 86 | 86 | */ |
| 87 | - public static function fromDER(string $data, int &$offset = null): self |
|
| 87 | + public static function fromDER(string $data, int & $offset = null): self |
|
| 88 | 88 | { |
| 89 | 89 | $idx = $offset ? $offset : 0; |
| 90 | 90 | $datalen = strlen($data); |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * @throws DecodeException If decoding fails |
| 118 | 118 | * @return string Tag number |
| 119 | 119 | */ |
| 120 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 120 | + private static function _decodeLongFormTag(string $data, int & $offset): string |
|
| 121 | 121 | { |
| 122 | 122 | $datalen = strlen($data); |
| 123 | 123 | $tag = gmp_init(0, 10); |
@@ -14,82 +14,82 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class DERData extends Element |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * DER encoded data. |
|
| 19 | - * |
|
| 20 | - * @var string $_der |
|
| 21 | - */ |
|
| 22 | - protected $_der; |
|
| 17 | + /** |
|
| 18 | + * DER encoded data. |
|
| 19 | + * |
|
| 20 | + * @var string $_der |
|
| 21 | + */ |
|
| 22 | + protected $_der; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Identifier of the underlying type. |
|
| 26 | - * |
|
| 27 | - * @var Identifier $_identifier |
|
| 28 | - */ |
|
| 29 | - protected $_identifier; |
|
| 24 | + /** |
|
| 25 | + * Identifier of the underlying type. |
|
| 26 | + * |
|
| 27 | + * @var Identifier $_identifier |
|
| 28 | + */ |
|
| 29 | + protected $_identifier; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Offset to the content in DER data. |
|
| 33 | - * |
|
| 34 | - * @var int $_contentOffset |
|
| 35 | - */ |
|
| 36 | - protected $_contentOffset = 0; |
|
| 31 | + /** |
|
| 32 | + * Offset to the content in DER data. |
|
| 33 | + * |
|
| 34 | + * @var int $_contentOffset |
|
| 35 | + */ |
|
| 36 | + protected $_contentOffset = 0; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $data DER encoded data |
|
| 42 | - * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 43 | - */ |
|
| 44 | - public function __construct(string $data) |
|
| 45 | - { |
|
| 46 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 47 | - Length::expectFromDER($data, $this->_contentOffset); |
|
| 48 | - $this->_der = $data; |
|
| 49 | - $this->_typeTag = intval($this->_identifier->tag()); |
|
| 50 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $data DER encoded data |
|
| 42 | + * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 43 | + */ |
|
| 44 | + public function __construct(string $data) |
|
| 45 | + { |
|
| 46 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 47 | + Length::expectFromDER($data, $this->_contentOffset); |
|
| 48 | + $this->_der = $data; |
|
| 49 | + $this->_typeTag = intval($this->_identifier->tag()); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * |
|
| 54 | - * @see \ASN1\Element::typeClass() |
|
| 55 | - * @return int |
|
| 56 | - */ |
|
| 57 | - public function typeClass(): int |
|
| 58 | - { |
|
| 59 | - return $this->_identifier->typeClass(); |
|
| 60 | - } |
|
| 52 | + /** |
|
| 53 | + * |
|
| 54 | + * @see \ASN1\Element::typeClass() |
|
| 55 | + * @return int |
|
| 56 | + */ |
|
| 57 | + public function typeClass(): int |
|
| 58 | + { |
|
| 59 | + return $this->_identifier->typeClass(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * |
|
| 64 | - * @see \ASN1\Element::isConstructed() |
|
| 65 | - * @return bool |
|
| 66 | - */ |
|
| 67 | - public function isConstructed(): bool |
|
| 68 | - { |
|
| 69 | - return $this->_identifier->isConstructed(); |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * |
|
| 64 | + * @see \ASN1\Element::isConstructed() |
|
| 65 | + * @return bool |
|
| 66 | + */ |
|
| 67 | + public function isConstructed(): bool |
|
| 68 | + { |
|
| 69 | + return $this->_identifier->isConstructed(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * |
|
| 74 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - protected function _encodedContentDER(): string |
|
| 78 | - { |
|
| 79 | - // if there's no content payload |
|
| 80 | - if (strlen($this->_der) == $this->_contentOffset) { |
|
| 81 | - return ""; |
|
| 82 | - } |
|
| 83 | - return substr($this->_der, $this->_contentOffset); |
|
| 84 | - } |
|
| 72 | + /** |
|
| 73 | + * |
|
| 74 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + protected function _encodedContentDER(): string |
|
| 78 | + { |
|
| 79 | + // if there's no content payload |
|
| 80 | + if (strlen($this->_der) == $this->_contentOffset) { |
|
| 81 | + return ""; |
|
| 82 | + } |
|
| 83 | + return substr($this->_der, $this->_contentOffset); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * |
|
| 88 | - * @see \ASN1\Element::toDER() |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function toDER(): string |
|
| 92 | - { |
|
| 93 | - return $this->_der; |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * |
|
| 88 | + * @see \ASN1\Element::toDER() |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function toDER(): string |
|
| 92 | + { |
|
| 93 | + return $this->_der; |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -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; |
| 6 | 6 | |
@@ -20,439 +20,439 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | abstract class Element implements ElementBase |
| 22 | 22 | { |
| 23 | - // Universal type tags |
|
| 24 | - const TYPE_EOC = 0x00; |
|
| 25 | - const TYPE_BOOLEAN = 0x01; |
|
| 26 | - const TYPE_INTEGER = 0x02; |
|
| 27 | - const TYPE_BIT_STRING = 0x03; |
|
| 28 | - const TYPE_OCTET_STRING = 0x04; |
|
| 29 | - const TYPE_NULL = 0x05; |
|
| 30 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 31 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 32 | - const TYPE_EXTERNAL = 0x08; |
|
| 33 | - const TYPE_REAL = 0x09; |
|
| 34 | - const TYPE_ENUMERATED = 0x0a; |
|
| 35 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 36 | - const TYPE_UTF8_STRING = 0x0c; |
|
| 37 | - const TYPE_RELATIVE_OID = 0x0d; |
|
| 38 | - const TYPE_SEQUENCE = 0x10; |
|
| 39 | - const TYPE_SET = 0x11; |
|
| 40 | - const TYPE_NUMERIC_STRING = 0x12; |
|
| 41 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
| 42 | - const TYPE_T61_STRING = 0x14; |
|
| 43 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 44 | - const TYPE_IA5_STRING = 0x16; |
|
| 45 | - const TYPE_UTC_TIME = 0x17; |
|
| 46 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
| 47 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
| 48 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
| 49 | - const TYPE_GENERAL_STRING = 0x1b; |
|
| 50 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 51 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
| 52 | - const TYPE_BMP_STRING = 0x1e; |
|
| 23 | + // Universal type tags |
|
| 24 | + const TYPE_EOC = 0x00; |
|
| 25 | + const TYPE_BOOLEAN = 0x01; |
|
| 26 | + const TYPE_INTEGER = 0x02; |
|
| 27 | + const TYPE_BIT_STRING = 0x03; |
|
| 28 | + const TYPE_OCTET_STRING = 0x04; |
|
| 29 | + const TYPE_NULL = 0x05; |
|
| 30 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 31 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 32 | + const TYPE_EXTERNAL = 0x08; |
|
| 33 | + const TYPE_REAL = 0x09; |
|
| 34 | + const TYPE_ENUMERATED = 0x0a; |
|
| 35 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 36 | + const TYPE_UTF8_STRING = 0x0c; |
|
| 37 | + const TYPE_RELATIVE_OID = 0x0d; |
|
| 38 | + const TYPE_SEQUENCE = 0x10; |
|
| 39 | + const TYPE_SET = 0x11; |
|
| 40 | + const TYPE_NUMERIC_STRING = 0x12; |
|
| 41 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
| 42 | + const TYPE_T61_STRING = 0x14; |
|
| 43 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 44 | + const TYPE_IA5_STRING = 0x16; |
|
| 45 | + const TYPE_UTC_TIME = 0x17; |
|
| 46 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
| 47 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
| 48 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
| 49 | + const TYPE_GENERAL_STRING = 0x1b; |
|
| 50 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 51 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
| 52 | + const TYPE_BMP_STRING = 0x1e; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Mapping from universal type tag to implementation class name. |
|
| 56 | - * |
|
| 57 | - * @internal |
|
| 58 | - * |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - const MAP_TAG_TO_CLASS = [ |
|
| 62 | - /* @formatter:off */ |
|
| 63 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 64 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 65 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 66 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 67 | - self::TYPE_NULL => Primitive\NullType::class, |
|
| 68 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 69 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 70 | - self::TYPE_REAL => Primitive\Real::class, |
|
| 71 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 72 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 73 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 74 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 75 | - self::TYPE_SET => Constructed\Set::class, |
|
| 76 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 77 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 78 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 79 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 80 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 81 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 82 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 83 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 84 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 85 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 86 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 87 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 88 | - self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
| 89 | - /* @formatter:on */ |
|
| 90 | - ]; |
|
| 54 | + /** |
|
| 55 | + * Mapping from universal type tag to implementation class name. |
|
| 56 | + * |
|
| 57 | + * @internal |
|
| 58 | + * |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + const MAP_TAG_TO_CLASS = [ |
|
| 62 | + /* @formatter:off */ |
|
| 63 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 64 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 65 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 66 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 67 | + self::TYPE_NULL => Primitive\NullType::class, |
|
| 68 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 69 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 70 | + self::TYPE_REAL => Primitive\Real::class, |
|
| 71 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 72 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 73 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 74 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 75 | + self::TYPE_SET => Constructed\Set::class, |
|
| 76 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 77 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 78 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 79 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 80 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 81 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 82 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 83 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 84 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 85 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 86 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 87 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 88 | + self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
| 89 | + /* @formatter:on */ |
|
| 90 | + ]; |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Pseudotype for all string types. |
|
| 94 | - * |
|
| 95 | - * May be used as an expectation parameter. |
|
| 96 | - * |
|
| 97 | - * @var int |
|
| 98 | - */ |
|
| 99 | - const TYPE_STRING = -1; |
|
| 92 | + /** |
|
| 93 | + * Pseudotype for all string types. |
|
| 94 | + * |
|
| 95 | + * May be used as an expectation parameter. |
|
| 96 | + * |
|
| 97 | + * @var int |
|
| 98 | + */ |
|
| 99 | + const TYPE_STRING = -1; |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Pseudotype for all time types. |
|
| 103 | - * |
|
| 104 | - * May be used as an expectation parameter. |
|
| 105 | - * |
|
| 106 | - * @var int |
|
| 107 | - */ |
|
| 108 | - const TYPE_TIME = -2; |
|
| 101 | + /** |
|
| 102 | + * Pseudotype for all time types. |
|
| 103 | + * |
|
| 104 | + * May be used as an expectation parameter. |
|
| 105 | + * |
|
| 106 | + * @var int |
|
| 107 | + */ |
|
| 108 | + const TYPE_TIME = -2; |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Mapping from universal type tag to human readable name. |
|
| 112 | - * |
|
| 113 | - * @internal |
|
| 114 | - * |
|
| 115 | - * @var array |
|
| 116 | - */ |
|
| 117 | - const MAP_TYPE_TO_NAME = array( |
|
| 118 | - /* @formatter:off */ |
|
| 119 | - self::TYPE_EOC => "EOC", |
|
| 120 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 121 | - self::TYPE_INTEGER => "INTEGER", |
|
| 122 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
| 123 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 124 | - self::TYPE_NULL => "NULL", |
|
| 125 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 126 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 127 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 128 | - self::TYPE_REAL => "REAL", |
|
| 129 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 130 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 131 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
| 132 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 133 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 134 | - self::TYPE_SET => "SET", |
|
| 135 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 136 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 137 | - self::TYPE_T61_STRING => "T61String", |
|
| 138 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 139 | - self::TYPE_IA5_STRING => "IA5String", |
|
| 140 | - self::TYPE_UTC_TIME => "UTCTime", |
|
| 141 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 142 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 143 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 144 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 145 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 146 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 147 | - self::TYPE_BMP_STRING => "BMPString", |
|
| 148 | - self::TYPE_STRING => "Any String", |
|
| 149 | - self::TYPE_TIME => "Any Time" |
|
| 150 | - /* @formatter:on */ |
|
| 151 | - ); |
|
| 110 | + /** |
|
| 111 | + * Mapping from universal type tag to human readable name. |
|
| 112 | + * |
|
| 113 | + * @internal |
|
| 114 | + * |
|
| 115 | + * @var array |
|
| 116 | + */ |
|
| 117 | + const MAP_TYPE_TO_NAME = array( |
|
| 118 | + /* @formatter:off */ |
|
| 119 | + self::TYPE_EOC => "EOC", |
|
| 120 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 121 | + self::TYPE_INTEGER => "INTEGER", |
|
| 122 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
| 123 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 124 | + self::TYPE_NULL => "NULL", |
|
| 125 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 126 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 127 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 128 | + self::TYPE_REAL => "REAL", |
|
| 129 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 130 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 131 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
| 132 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 133 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 134 | + self::TYPE_SET => "SET", |
|
| 135 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 136 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 137 | + self::TYPE_T61_STRING => "T61String", |
|
| 138 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 139 | + self::TYPE_IA5_STRING => "IA5String", |
|
| 140 | + self::TYPE_UTC_TIME => "UTCTime", |
|
| 141 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 142 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 143 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 144 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 145 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 146 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 147 | + self::TYPE_BMP_STRING => "BMPString", |
|
| 148 | + self::TYPE_STRING => "Any String", |
|
| 149 | + self::TYPE_TIME => "Any Time" |
|
| 150 | + /* @formatter:on */ |
|
| 151 | + ); |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Element's type tag. |
|
| 155 | - * |
|
| 156 | - * @var int |
|
| 157 | - */ |
|
| 158 | - protected $_typeTag; |
|
| 153 | + /** |
|
| 154 | + * Element's type tag. |
|
| 155 | + * |
|
| 156 | + * @var int |
|
| 157 | + */ |
|
| 158 | + protected $_typeTag; |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * |
|
| 162 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 163 | - * @return int |
|
| 164 | - */ |
|
| 165 | - abstract public function typeClass(): int; |
|
| 160 | + /** |
|
| 161 | + * |
|
| 162 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 163 | + * @return int |
|
| 164 | + */ |
|
| 165 | + abstract public function typeClass(): int; |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * |
|
| 169 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 170 | - * @return bool |
|
| 171 | - */ |
|
| 172 | - abstract public function isConstructed(): bool; |
|
| 167 | + /** |
|
| 168 | + * |
|
| 169 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 170 | + * @return bool |
|
| 171 | + */ |
|
| 172 | + abstract public function isConstructed(): bool; |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Get the content encoded in DER. |
|
| 176 | - * |
|
| 177 | - * Returns the DER encoded content without identifier and length header |
|
| 178 | - * octets. |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - */ |
|
| 182 | - abstract protected function _encodedContentDER(); |
|
| 174 | + /** |
|
| 175 | + * Get the content encoded in DER. |
|
| 176 | + * |
|
| 177 | + * Returns the DER encoded content without identifier and length header |
|
| 178 | + * octets. |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + */ |
|
| 182 | + abstract protected function _encodedContentDER(); |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Decode type-specific element from DER. |
|
| 186 | - * |
|
| 187 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 188 | - * @param string $data DER data |
|
| 189 | - * @param int $offset Offset in data to the next byte after identifier |
|
| 190 | - * @throws DecodeException If decoding fails |
|
| 191 | - * @return self |
|
| 192 | - */ |
|
| 193 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 194 | - int &$offset) |
|
| 195 | - { |
|
| 196 | - throw new \BadMethodCallException( |
|
| 197 | - __METHOD__ . " must be implemented in derived class."); |
|
| 198 | - } |
|
| 184 | + /** |
|
| 185 | + * Decode type-specific element from DER. |
|
| 186 | + * |
|
| 187 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 188 | + * @param string $data DER data |
|
| 189 | + * @param int $offset Offset in data to the next byte after identifier |
|
| 190 | + * @throws DecodeException If decoding fails |
|
| 191 | + * @return self |
|
| 192 | + */ |
|
| 193 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 194 | + int &$offset) |
|
| 195 | + { |
|
| 196 | + throw new \BadMethodCallException( |
|
| 197 | + __METHOD__ . " must be implemented in derived class."); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Decode element from DER data. |
|
| 202 | - * |
|
| 203 | - * @param string $data DER encoded data |
|
| 204 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 205 | - * into the data where to start parsing. Variable is updated to |
|
| 206 | - * the offset next to the parsed element. If null, start from offset |
|
| 207 | - * 0. |
|
| 208 | - * @throws DecodeException If decoding fails |
|
| 209 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
| 210 | - * type, but decoding yields another type |
|
| 211 | - * @return self |
|
| 212 | - */ |
|
| 213 | - public static function fromDER(string $data, int &$offset = null) |
|
| 214 | - { |
|
| 215 | - // decode identifier |
|
| 216 | - $idx = $offset ? $offset : 0; |
|
| 217 | - $identifier = Identifier::fromDER($data, $idx); |
|
| 218 | - // determine class that implements type specific decoding |
|
| 219 | - $cls = self::_determineImplClass($identifier); |
|
| 220 | - try { |
|
| 221 | - // decode remaining element |
|
| 222 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 223 | - } catch (\LogicException $e) { |
|
| 224 | - // rethrow as a RuntimeException for unified exception handling |
|
| 225 | - throw new DecodeException( |
|
| 226 | - sprintf("Error while decoding %s.", |
|
| 227 | - self::tagToName($identifier->tag())), 0, $e); |
|
| 228 | - } |
|
| 229 | - // if called in the context of a concrete class, check |
|
| 230 | - // that decoded type matches the type of a calling class |
|
| 231 | - $called_class = get_called_class(); |
|
| 232 | - if (__CLASS__ != $called_class) { |
|
| 233 | - if (!$element instanceof $called_class) { |
|
| 234 | - throw new \UnexpectedValueException( |
|
| 235 | - sprintf("%s expected, got %s.", $called_class, |
|
| 236 | - get_class($element))); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - // update offset for the caller |
|
| 240 | - if (isset($offset)) { |
|
| 241 | - $offset = $idx; |
|
| 242 | - } |
|
| 243 | - return $element; |
|
| 244 | - } |
|
| 200 | + /** |
|
| 201 | + * Decode element from DER data. |
|
| 202 | + * |
|
| 203 | + * @param string $data DER encoded data |
|
| 204 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 205 | + * into the data where to start parsing. Variable is updated to |
|
| 206 | + * the offset next to the parsed element. If null, start from offset |
|
| 207 | + * 0. |
|
| 208 | + * @throws DecodeException If decoding fails |
|
| 209 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
| 210 | + * type, but decoding yields another type |
|
| 211 | + * @return self |
|
| 212 | + */ |
|
| 213 | + public static function fromDER(string $data, int &$offset = null) |
|
| 214 | + { |
|
| 215 | + // decode identifier |
|
| 216 | + $idx = $offset ? $offset : 0; |
|
| 217 | + $identifier = Identifier::fromDER($data, $idx); |
|
| 218 | + // determine class that implements type specific decoding |
|
| 219 | + $cls = self::_determineImplClass($identifier); |
|
| 220 | + try { |
|
| 221 | + // decode remaining element |
|
| 222 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 223 | + } catch (\LogicException $e) { |
|
| 224 | + // rethrow as a RuntimeException for unified exception handling |
|
| 225 | + throw new DecodeException( |
|
| 226 | + sprintf("Error while decoding %s.", |
|
| 227 | + self::tagToName($identifier->tag())), 0, $e); |
|
| 228 | + } |
|
| 229 | + // if called in the context of a concrete class, check |
|
| 230 | + // that decoded type matches the type of a calling class |
|
| 231 | + $called_class = get_called_class(); |
|
| 232 | + if (__CLASS__ != $called_class) { |
|
| 233 | + if (!$element instanceof $called_class) { |
|
| 234 | + throw new \UnexpectedValueException( |
|
| 235 | + sprintf("%s expected, got %s.", $called_class, |
|
| 236 | + get_class($element))); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + // update offset for the caller |
|
| 240 | + if (isset($offset)) { |
|
| 241 | + $offset = $idx; |
|
| 242 | + } |
|
| 243 | + return $element; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * |
|
| 248 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 249 | - * @return string |
|
| 250 | - */ |
|
| 251 | - public function toDER(): string |
|
| 252 | - { |
|
| 253 | - $identifier = new Identifier($this->typeClass(), |
|
| 254 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 255 | - $this->_typeTag); |
|
| 256 | - $content = $this->_encodedContentDER(); |
|
| 257 | - $length = new Length(strlen($content)); |
|
| 258 | - return $identifier->toDER() . $length->toDER() . $content; |
|
| 259 | - } |
|
| 246 | + /** |
|
| 247 | + * |
|
| 248 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 249 | + * @return string |
|
| 250 | + */ |
|
| 251 | + public function toDER(): string |
|
| 252 | + { |
|
| 253 | + $identifier = new Identifier($this->typeClass(), |
|
| 254 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 255 | + $this->_typeTag); |
|
| 256 | + $content = $this->_encodedContentDER(); |
|
| 257 | + $length = new Length(strlen($content)); |
|
| 258 | + return $identifier->toDER() . $length->toDER() . $content; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * |
|
| 263 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 264 | - * @return int |
|
| 265 | - */ |
|
| 266 | - public function tag() |
|
| 267 | - { |
|
| 268 | - return $this->_typeTag; |
|
| 269 | - } |
|
| 261 | + /** |
|
| 262 | + * |
|
| 263 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 264 | + * @return int |
|
| 265 | + */ |
|
| 266 | + public function tag() |
|
| 267 | + { |
|
| 268 | + return $this->_typeTag; |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * |
|
| 273 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 274 | - * @return bool |
|
| 275 | - */ |
|
| 276 | - public function isType($tag): bool |
|
| 277 | - { |
|
| 278 | - // if element is context specific |
|
| 279 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - // negative tags identify an abstract pseudotype |
|
| 283 | - if ($tag < 0) { |
|
| 284 | - return $this->_isPseudoType($tag); |
|
| 285 | - } |
|
| 286 | - return $this->_isConcreteType($tag); |
|
| 287 | - } |
|
| 271 | + /** |
|
| 272 | + * |
|
| 273 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 274 | + * @return bool |
|
| 275 | + */ |
|
| 276 | + public function isType($tag): bool |
|
| 277 | + { |
|
| 278 | + // if element is context specific |
|
| 279 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + // negative tags identify an abstract pseudotype |
|
| 283 | + if ($tag < 0) { |
|
| 284 | + return $this->_isPseudoType($tag); |
|
| 285 | + } |
|
| 286 | + return $this->_isConcreteType($tag); |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * |
|
| 291 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 292 | - * @return ElementBase |
|
| 293 | - */ |
|
| 294 | - public function expectType($tag): ElementBase |
|
| 295 | - { |
|
| 296 | - if (!$this->isType($tag)) { |
|
| 297 | - throw new \UnexpectedValueException( |
|
| 298 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 299 | - $this->_typeDescriptorString())); |
|
| 300 | - } |
|
| 301 | - return $this; |
|
| 302 | - } |
|
| 289 | + /** |
|
| 290 | + * |
|
| 291 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 292 | + * @return ElementBase |
|
| 293 | + */ |
|
| 294 | + public function expectType($tag): ElementBase |
|
| 295 | + { |
|
| 296 | + if (!$this->isType($tag)) { |
|
| 297 | + throw new \UnexpectedValueException( |
|
| 298 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 299 | + $this->_typeDescriptorString())); |
|
| 300 | + } |
|
| 301 | + return $this; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - /** |
|
| 305 | - * Check whether the element is a concrete type of a given tag. |
|
| 306 | - * |
|
| 307 | - * @param int $tag |
|
| 308 | - * @return bool |
|
| 309 | - */ |
|
| 310 | - private function _isConcreteType($tag): bool |
|
| 311 | - { |
|
| 312 | - // if tag doesn't match |
|
| 313 | - if ($this->tag() != $tag) { |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - // if type is universal check that instance is of a correct class |
|
| 317 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 318 | - $cls = self::_determineUniversalImplClass($tag); |
|
| 319 | - if (!$this instanceof $cls) { |
|
| 320 | - return false; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - return true; |
|
| 324 | - } |
|
| 304 | + /** |
|
| 305 | + * Check whether the element is a concrete type of a given tag. |
|
| 306 | + * |
|
| 307 | + * @param int $tag |
|
| 308 | + * @return bool |
|
| 309 | + */ |
|
| 310 | + private function _isConcreteType($tag): bool |
|
| 311 | + { |
|
| 312 | + // if tag doesn't match |
|
| 313 | + if ($this->tag() != $tag) { |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + // if type is universal check that instance is of a correct class |
|
| 317 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 318 | + $cls = self::_determineUniversalImplClass($tag); |
|
| 319 | + if (!$this instanceof $cls) { |
|
| 320 | + return false; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + return true; |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | - /** |
|
| 327 | - * Check whether the element is a pseudotype. |
|
| 328 | - * |
|
| 329 | - * @param int $tag |
|
| 330 | - * @return bool |
|
| 331 | - */ |
|
| 332 | - private function _isPseudoType($tag): bool |
|
| 333 | - { |
|
| 334 | - switch ($tag) { |
|
| 335 | - case self::TYPE_STRING: |
|
| 336 | - return $this instanceof StringType; |
|
| 337 | - case self::TYPE_TIME: |
|
| 338 | - return $this instanceof TimeType; |
|
| 339 | - } |
|
| 340 | - return false; |
|
| 341 | - } |
|
| 326 | + /** |
|
| 327 | + * Check whether the element is a pseudotype. |
|
| 328 | + * |
|
| 329 | + * @param int $tag |
|
| 330 | + * @return bool |
|
| 331 | + */ |
|
| 332 | + private function _isPseudoType($tag): bool |
|
| 333 | + { |
|
| 334 | + switch ($tag) { |
|
| 335 | + case self::TYPE_STRING: |
|
| 336 | + return $this instanceof StringType; |
|
| 337 | + case self::TYPE_TIME: |
|
| 338 | + return $this instanceof TimeType; |
|
| 339 | + } |
|
| 340 | + return false; |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * |
|
| 345 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 346 | - * @return bool |
|
| 347 | - */ |
|
| 348 | - public function isTagged(): bool |
|
| 349 | - { |
|
| 350 | - return $this instanceof TaggedType; |
|
| 351 | - } |
|
| 343 | + /** |
|
| 344 | + * |
|
| 345 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 346 | + * @return bool |
|
| 347 | + */ |
|
| 348 | + public function isTagged(): bool |
|
| 349 | + { |
|
| 350 | + return $this instanceof TaggedType; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * |
|
| 355 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 356 | - * @return TaggedType |
|
| 357 | - */ |
|
| 358 | - public function expectTagged($tag = null): TaggedType |
|
| 359 | - { |
|
| 360 | - if (!$this->isTagged()) { |
|
| 361 | - throw new \UnexpectedValueException( |
|
| 362 | - sprintf("Context specific element expected, got %s.", |
|
| 363 | - Identifier::classToName($this->typeClass()))); |
|
| 364 | - } |
|
| 365 | - if (isset($tag) && $this->tag() != $tag) { |
|
| 366 | - throw new \UnexpectedValueException( |
|
| 367 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 368 | - } |
|
| 369 | - return $this; |
|
| 370 | - } |
|
| 353 | + /** |
|
| 354 | + * |
|
| 355 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 356 | + * @return TaggedType |
|
| 357 | + */ |
|
| 358 | + public function expectTagged($tag = null): TaggedType |
|
| 359 | + { |
|
| 360 | + if (!$this->isTagged()) { |
|
| 361 | + throw new \UnexpectedValueException( |
|
| 362 | + sprintf("Context specific element expected, got %s.", |
|
| 363 | + Identifier::classToName($this->typeClass()))); |
|
| 364 | + } |
|
| 365 | + if (isset($tag) && $this->tag() != $tag) { |
|
| 366 | + throw new \UnexpectedValueException( |
|
| 367 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 368 | + } |
|
| 369 | + return $this; |
|
| 370 | + } |
|
| 371 | 371 | |
| 372 | - /** |
|
| 373 | - * |
|
| 374 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 375 | - * @return Element |
|
| 376 | - */ |
|
| 377 | - final public function asElement(): Element |
|
| 378 | - { |
|
| 379 | - return $this; |
|
| 380 | - } |
|
| 372 | + /** |
|
| 373 | + * |
|
| 374 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 375 | + * @return Element |
|
| 376 | + */ |
|
| 377 | + final public function asElement(): Element |
|
| 378 | + { |
|
| 379 | + return $this; |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | - /** |
|
| 383 | - * Get element decorated with UnspecifiedType object. |
|
| 384 | - * |
|
| 385 | - * @return UnspecifiedType |
|
| 386 | - */ |
|
| 387 | - public function asUnspecified(): UnspecifiedType |
|
| 388 | - { |
|
| 389 | - return new UnspecifiedType($this); |
|
| 390 | - } |
|
| 382 | + /** |
|
| 383 | + * Get element decorated with UnspecifiedType object. |
|
| 384 | + * |
|
| 385 | + * @return UnspecifiedType |
|
| 386 | + */ |
|
| 387 | + public function asUnspecified(): UnspecifiedType |
|
| 388 | + { |
|
| 389 | + return new UnspecifiedType($this); |
|
| 390 | + } |
|
| 391 | 391 | |
| 392 | - /** |
|
| 393 | - * Determine the class that implements the type. |
|
| 394 | - * |
|
| 395 | - * @param Identifier $identifier |
|
| 396 | - * @return string Class name |
|
| 397 | - */ |
|
| 398 | - protected static function _determineImplClass(Identifier $identifier): string |
|
| 399 | - { |
|
| 400 | - // tagged type |
|
| 401 | - if ($identifier->isContextSpecific()) { |
|
| 402 | - return TaggedType::class; |
|
| 403 | - } |
|
| 404 | - // universal class |
|
| 405 | - if ($identifier->isUniversal()) { |
|
| 406 | - return self::_determineUniversalImplClass( |
|
| 407 | - intval($identifier->tag())); |
|
| 408 | - } |
|
| 409 | - throw new \UnexpectedValueException( |
|
| 410 | - sprintf("%s %d not implemented.", |
|
| 411 | - Identifier::classToName($identifier->typeClass()), |
|
| 412 | - $identifier->tag())); |
|
| 413 | - } |
|
| 392 | + /** |
|
| 393 | + * Determine the class that implements the type. |
|
| 394 | + * |
|
| 395 | + * @param Identifier $identifier |
|
| 396 | + * @return string Class name |
|
| 397 | + */ |
|
| 398 | + protected static function _determineImplClass(Identifier $identifier): string |
|
| 399 | + { |
|
| 400 | + // tagged type |
|
| 401 | + if ($identifier->isContextSpecific()) { |
|
| 402 | + return TaggedType::class; |
|
| 403 | + } |
|
| 404 | + // universal class |
|
| 405 | + if ($identifier->isUniversal()) { |
|
| 406 | + return self::_determineUniversalImplClass( |
|
| 407 | + intval($identifier->tag())); |
|
| 408 | + } |
|
| 409 | + throw new \UnexpectedValueException( |
|
| 410 | + sprintf("%s %d not implemented.", |
|
| 411 | + Identifier::classToName($identifier->typeClass()), |
|
| 412 | + $identifier->tag())); |
|
| 413 | + } |
|
| 414 | 414 | |
| 415 | - /** |
|
| 416 | - * Determine the class that implements an universal type of the given tag. |
|
| 417 | - * |
|
| 418 | - * @param int $tag |
|
| 419 | - * @throws \UnexpectedValueException |
|
| 420 | - * @return string Class name |
|
| 421 | - */ |
|
| 422 | - protected static function _determineUniversalImplClass($tag): string |
|
| 423 | - { |
|
| 424 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 425 | - throw new \UnexpectedValueException( |
|
| 426 | - "Universal tag $tag not implemented."); |
|
| 427 | - } |
|
| 428 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 429 | - } |
|
| 415 | + /** |
|
| 416 | + * Determine the class that implements an universal type of the given tag. |
|
| 417 | + * |
|
| 418 | + * @param int $tag |
|
| 419 | + * @throws \UnexpectedValueException |
|
| 420 | + * @return string Class name |
|
| 421 | + */ |
|
| 422 | + protected static function _determineUniversalImplClass($tag): string |
|
| 423 | + { |
|
| 424 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 425 | + throw new \UnexpectedValueException( |
|
| 426 | + "Universal tag $tag not implemented."); |
|
| 427 | + } |
|
| 428 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - /** |
|
| 432 | - * Get textual description of the type for debugging purposes. |
|
| 433 | - * |
|
| 434 | - * @return string |
|
| 435 | - */ |
|
| 436 | - protected function _typeDescriptorString(): string |
|
| 437 | - { |
|
| 438 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 439 | - return self::tagToName($this->_typeTag); |
|
| 440 | - } |
|
| 441 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 442 | - $this->_typeTag); |
|
| 443 | - } |
|
| 431 | + /** |
|
| 432 | + * Get textual description of the type for debugging purposes. |
|
| 433 | + * |
|
| 434 | + * @return string |
|
| 435 | + */ |
|
| 436 | + protected function _typeDescriptorString(): string |
|
| 437 | + { |
|
| 438 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 439 | + return self::tagToName($this->_typeTag); |
|
| 440 | + } |
|
| 441 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 442 | + $this->_typeTag); |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - /** |
|
| 446 | - * Get human readable name for an universal tag. |
|
| 447 | - * |
|
| 448 | - * @param int $tag |
|
| 449 | - * @return string |
|
| 450 | - */ |
|
| 451 | - public static function tagToName($tag): string |
|
| 452 | - { |
|
| 453 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 454 | - return "TAG $tag"; |
|
| 455 | - } |
|
| 456 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 457 | - } |
|
| 445 | + /** |
|
| 446 | + * Get human readable name for an universal tag. |
|
| 447 | + * |
|
| 448 | + * @param int $tag |
|
| 449 | + * @return string |
|
| 450 | + */ |
|
| 451 | + public static function tagToName($tag): string |
|
| 452 | + { |
|
| 453 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 454 | + return "TAG $tag"; |
|
| 455 | + } |
|
| 456 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 457 | + } |
|
| 458 | 458 | } |
@@ -332,10 +332,10 @@ |
||
| 332 | 332 | private function _isPseudoType($tag): bool |
| 333 | 333 | { |
| 334 | 334 | switch ($tag) { |
| 335 | - case self::TYPE_STRING: |
|
| 336 | - return $this instanceof StringType; |
|
| 337 | - case self::TYPE_TIME: |
|
| 338 | - return $this instanceof TimeType; |
|
| 335 | + case self::TYPE_STRING: |
|
| 336 | + return $this instanceof StringType; |
|
| 337 | + case self::TYPE_TIME: |
|
| 338 | + return $this instanceof TimeType; |
|
| 339 | 339 | } |
| 340 | 340 | return false; |
| 341 | 341 | } |
@@ -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; |
| 6 | 6 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | * @return self |
| 192 | 192 | */ |
| 193 | 193 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 194 | - int &$offset) |
|
| 194 | + int & $offset) |
|
| 195 | 195 | { |
| 196 | 196 | throw new \BadMethodCallException( |
| 197 | 197 | __METHOD__ . " must be implemented in derived class."); |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | * type, but decoding yields another type |
| 211 | 211 | * @return self |
| 212 | 212 | */ |
| 213 | - public static function fromDER(string $data, int &$offset = null) |
|
| 213 | + public static function fromDER(string $data, int & $offset = null) |
|
| 214 | 214 | { |
| 215 | 215 | // decode identifier |
| 216 | 216 | $idx = $offset ? $offset : 0; |
@@ -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\Exception; |
| 6 | 6 | |
@@ -11,130 +11,130 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Flags |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Flag octets. |
|
| 16 | - * |
|
| 17 | - * @var string $_flags |
|
| 18 | - */ |
|
| 19 | - protected $_flags; |
|
| 14 | + /** |
|
| 15 | + * Flag octets. |
|
| 16 | + * |
|
| 17 | + * @var string $_flags |
|
| 18 | + */ |
|
| 19 | + protected $_flags; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Number of flags. |
|
| 23 | - * |
|
| 24 | - * @var int $_width |
|
| 25 | - */ |
|
| 26 | - protected $_width; |
|
| 21 | + /** |
|
| 22 | + * Number of flags. |
|
| 23 | + * |
|
| 24 | + * @var int $_width |
|
| 25 | + */ |
|
| 26 | + protected $_width; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param number $flags Flags |
|
| 32 | - * @param int $width The number of flags. If width is larger than number of |
|
| 33 | - * bits in $flags, zeroes are prepended to flag field. |
|
| 34 | - */ |
|
| 35 | - public function __construct($flags, int $width) |
|
| 36 | - { |
|
| 37 | - if (!$width) { |
|
| 38 | - $this->_flags = ""; |
|
| 39 | - } else { |
|
| 40 | - // calculate number of unused bits in last octet |
|
| 41 | - $last_octet_bits = $width % 8; |
|
| 42 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 43 | - $num = gmp_init($flags); |
|
| 44 | - // mask bits outside bitfield width |
|
| 45 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
| 46 | - $num &= $mask; |
|
| 47 | - // shift towards MSB if needed |
|
| 48 | - $data = gmp_export($num << $unused_bits, 1, |
|
| 49 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 50 | - $octets = unpack("C*", $data); |
|
| 51 | - $bits = count($octets) * 8; |
|
| 52 | - // pad with zeroes |
|
| 53 | - while ($bits < $width) { |
|
| 54 | - array_unshift($octets, 0); |
|
| 55 | - $bits += 8; |
|
| 56 | - } |
|
| 57 | - $this->_flags = pack("C*", ...$octets); |
|
| 58 | - } |
|
| 59 | - $this->_width = $width; |
|
| 60 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param number $flags Flags |
|
| 32 | + * @param int $width The number of flags. If width is larger than number of |
|
| 33 | + * bits in $flags, zeroes are prepended to flag field. |
|
| 34 | + */ |
|
| 35 | + public function __construct($flags, int $width) |
|
| 36 | + { |
|
| 37 | + if (!$width) { |
|
| 38 | + $this->_flags = ""; |
|
| 39 | + } else { |
|
| 40 | + // calculate number of unused bits in last octet |
|
| 41 | + $last_octet_bits = $width % 8; |
|
| 42 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 43 | + $num = gmp_init($flags); |
|
| 44 | + // mask bits outside bitfield width |
|
| 45 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
| 46 | + $num &= $mask; |
|
| 47 | + // shift towards MSB if needed |
|
| 48 | + $data = gmp_export($num << $unused_bits, 1, |
|
| 49 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 50 | + $octets = unpack("C*", $data); |
|
| 51 | + $bits = count($octets) * 8; |
|
| 52 | + // pad with zeroes |
|
| 53 | + while ($bits < $width) { |
|
| 54 | + array_unshift($octets, 0); |
|
| 55 | + $bits += 8; |
|
| 56 | + } |
|
| 57 | + $this->_flags = pack("C*", ...$octets); |
|
| 58 | + } |
|
| 59 | + $this->_width = $width; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Initialize from BitString. |
|
| 64 | - * |
|
| 65 | - * @param BitString $bs |
|
| 66 | - * @param int $width |
|
| 67 | - * @return self |
|
| 68 | - */ |
|
| 69 | - public static function fromBitString(BitString $bs, int $width) |
|
| 70 | - { |
|
| 71 | - $num_bits = $bs->numBits(); |
|
| 72 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 73 | - $num >>= $bs->unusedBits(); |
|
| 74 | - if ($num_bits < $width) { |
|
| 75 | - $num <<= ($width - $num_bits); |
|
| 76 | - } |
|
| 77 | - return new self(gmp_strval($num, 10), $width); |
|
| 78 | - } |
|
| 62 | + /** |
|
| 63 | + * Initialize from BitString. |
|
| 64 | + * |
|
| 65 | + * @param BitString $bs |
|
| 66 | + * @param int $width |
|
| 67 | + * @return self |
|
| 68 | + */ |
|
| 69 | + public static function fromBitString(BitString $bs, int $width) |
|
| 70 | + { |
|
| 71 | + $num_bits = $bs->numBits(); |
|
| 72 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 73 | + $num >>= $bs->unusedBits(); |
|
| 74 | + if ($num_bits < $width) { |
|
| 75 | + $num <<= ($width - $num_bits); |
|
| 76 | + } |
|
| 77 | + return new self(gmp_strval($num, 10), $width); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Check whether a bit at given index is set. |
|
| 82 | - * Index 0 is the leftmost bit. |
|
| 83 | - * |
|
| 84 | - * @param int $idx |
|
| 85 | - * @throws \OutOfBoundsException |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - public function test(int $idx) |
|
| 89 | - { |
|
| 90 | - if ($idx >= $this->_width) { |
|
| 91 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 92 | - } |
|
| 93 | - // octet index |
|
| 94 | - $oi = (int) floor($idx / 8); |
|
| 95 | - $byte = $this->_flags[$oi]; |
|
| 96 | - // bit index |
|
| 97 | - $bi = $idx % 8; |
|
| 98 | - // index 0 is the most significant bit in byte |
|
| 99 | - $mask = 0x01 << (7 - $bi); |
|
| 100 | - return (ord($byte) & $mask) > 0; |
|
| 101 | - } |
|
| 80 | + /** |
|
| 81 | + * Check whether a bit at given index is set. |
|
| 82 | + * Index 0 is the leftmost bit. |
|
| 83 | + * |
|
| 84 | + * @param int $idx |
|
| 85 | + * @throws \OutOfBoundsException |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + public function test(int $idx) |
|
| 89 | + { |
|
| 90 | + if ($idx >= $this->_width) { |
|
| 91 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 92 | + } |
|
| 93 | + // octet index |
|
| 94 | + $oi = (int) floor($idx / 8); |
|
| 95 | + $byte = $this->_flags[$oi]; |
|
| 96 | + // bit index |
|
| 97 | + $bi = $idx % 8; |
|
| 98 | + // index 0 is the most significant bit in byte |
|
| 99 | + $mask = 0x01 << (7 - $bi); |
|
| 100 | + return (ord($byte) & $mask) > 0; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Get flags as an octet string. |
|
| 105 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function string(): string |
|
| 110 | - { |
|
| 111 | - return $this->_flags; |
|
| 112 | - } |
|
| 103 | + /** |
|
| 104 | + * Get flags as an octet string. |
|
| 105 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function string(): string |
|
| 110 | + { |
|
| 111 | + return $this->_flags; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Get flags as a base 10 integer. |
|
| 116 | - * |
|
| 117 | - * @return int|string |
|
| 118 | - */ |
|
| 119 | - public function number() |
|
| 120 | - { |
|
| 121 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | - $last_octet_bits = $this->_width % 8; |
|
| 123 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 124 | - $num >>= $unused_bits; |
|
| 125 | - return gmp_strval($num, 10); |
|
| 126 | - } |
|
| 114 | + /** |
|
| 115 | + * Get flags as a base 10 integer. |
|
| 116 | + * |
|
| 117 | + * @return int|string |
|
| 118 | + */ |
|
| 119 | + public function number() |
|
| 120 | + { |
|
| 121 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | + $last_octet_bits = $this->_width % 8; |
|
| 123 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 124 | + $num >>= $unused_bits; |
|
| 125 | + return gmp_strval($num, 10); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get flags as a BitString. |
|
| 130 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
| 131 | - * |
|
| 132 | - * @return BitString |
|
| 133 | - */ |
|
| 134 | - public function bitString(): BitString |
|
| 135 | - { |
|
| 136 | - $last_octet_bits = $this->_width % 8; |
|
| 137 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 138 | - return new BitString($this->_flags, $unused_bits); |
|
| 139 | - } |
|
| 128 | + /** |
|
| 129 | + * Get flags as a BitString. |
|
| 130 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
| 131 | + * |
|
| 132 | + * @return BitString |
|
| 133 | + */ |
|
| 134 | + public function bitString(): BitString |
|
| 135 | + { |
|
| 136 | + $last_octet_bits = $this->_width % 8; |
|
| 137 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 138 | + return new BitString($this->_flags, $unused_bits); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -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\Util; |
| 6 | 6 | |