@@ -57,11 +57,11 @@ |
||
| 57 | 57 | $num = gmp_init($this->_number, 10); |
| 58 | 58 | switch (gmp_sign($num)) { |
| 59 | 59 | // positive |
| 60 | - case 1: |
|
| 61 | - return self::_encodePositiveInteger($num); |
|
| 62 | - // negative |
|
| 63 | - case -1: |
|
| 64 | - return self::_encodeNegativeInteger($num); |
|
| 60 | + case 1: |
|
| 61 | + return self::_encodePositiveInteger($num); |
|
| 62 | + // negative |
|
| 63 | + case -1: |
|
| 64 | + return self::_encodeNegativeInteger($num); |
|
| 65 | 65 | } |
| 66 | 66 | // zero |
| 67 | 67 | return "\0"; |
@@ -16,156 +16,156 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Integer extends Element |
| 18 | 18 | { |
| 19 | - use UniversalClass; |
|
| 20 | - use PrimitiveType; |
|
| 19 | + use UniversalClass; |
|
| 20 | + use PrimitiveType; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Number as a base 10. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - private $_number; |
|
| 22 | + /** |
|
| 23 | + * Number as a base 10. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + private $_number; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param int|string $number Base 10 integer |
|
| 33 | - */ |
|
| 34 | - public function __construct($number) |
|
| 35 | - { |
|
| 36 | - $this->_typeTag = self::TYPE_INTEGER; |
|
| 37 | - if (!self::_validateNumber($number)) { |
|
| 38 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 39 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
| 40 | - } |
|
| 41 | - $this->_number = strval($number); |
|
| 42 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param int|string $number Base 10 integer |
|
| 33 | + */ |
|
| 34 | + public function __construct($number) |
|
| 35 | + { |
|
| 36 | + $this->_typeTag = self::TYPE_INTEGER; |
|
| 37 | + if (!self::_validateNumber($number)) { |
|
| 38 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 39 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
| 40 | + } |
|
| 41 | + $this->_number = strval($number); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get the number as a base 10. |
|
| 46 | - * |
|
| 47 | - * @return string Integer as a string |
|
| 48 | - */ |
|
| 49 | - public function number(): string |
|
| 50 | - { |
|
| 51 | - return $this->_number; |
|
| 52 | - } |
|
| 44 | + /** |
|
| 45 | + * Get the number as a base 10. |
|
| 46 | + * |
|
| 47 | + * @return string Integer as a string |
|
| 48 | + */ |
|
| 49 | + public function number(): string |
|
| 50 | + { |
|
| 51 | + return $this->_number; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * |
|
| 56 | - * {@inheritdoc} |
|
| 57 | - */ |
|
| 58 | - protected function _encodedContentDER(): string |
|
| 59 | - { |
|
| 60 | - $num = gmp_init($this->_number, 10); |
|
| 61 | - switch (gmp_sign($num)) { |
|
| 62 | - // positive |
|
| 63 | - case 1: |
|
| 64 | - return self::_encodePositiveInteger($num); |
|
| 65 | - // negative |
|
| 66 | - case -1: |
|
| 67 | - return self::_encodeNegativeInteger($num); |
|
| 68 | - } |
|
| 69 | - // zero |
|
| 70 | - return "\0"; |
|
| 71 | - } |
|
| 54 | + /** |
|
| 55 | + * |
|
| 56 | + * {@inheritdoc} |
|
| 57 | + */ |
|
| 58 | + protected function _encodedContentDER(): string |
|
| 59 | + { |
|
| 60 | + $num = gmp_init($this->_number, 10); |
|
| 61 | + switch (gmp_sign($num)) { |
|
| 62 | + // positive |
|
| 63 | + case 1: |
|
| 64 | + return self::_encodePositiveInteger($num); |
|
| 65 | + // negative |
|
| 66 | + case -1: |
|
| 67 | + return self::_encodeNegativeInteger($num); |
|
| 68 | + } |
|
| 69 | + // zero |
|
| 70 | + return "\0"; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Encode positive integer to DER content. |
|
| 75 | - * |
|
| 76 | - * @param \GMP|resource $num |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - private static function _encodePositiveInteger(\GMP $num): string |
|
| 80 | - { |
|
| 81 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 82 | - // if first bit is 1, prepend full zero byte |
|
| 83 | - // to represent positive two's complement |
|
| 84 | - if (ord($bin[0]) & 0x80) { |
|
| 85 | - $bin = chr(0x00) . $bin; |
|
| 86 | - } |
|
| 87 | - return $bin; |
|
| 88 | - } |
|
| 73 | + /** |
|
| 74 | + * Encode positive integer to DER content. |
|
| 75 | + * |
|
| 76 | + * @param \GMP|resource $num |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + private static function _encodePositiveInteger(\GMP $num): string |
|
| 80 | + { |
|
| 81 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 82 | + // if first bit is 1, prepend full zero byte |
|
| 83 | + // to represent positive two's complement |
|
| 84 | + if (ord($bin[0]) & 0x80) { |
|
| 85 | + $bin = chr(0x00) . $bin; |
|
| 86 | + } |
|
| 87 | + return $bin; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Encode negative integer to DER content. |
|
| 92 | - * |
|
| 93 | - * @param \GMP|resource $num |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
| 97 | - { |
|
| 98 | - $num = gmp_abs($num); |
|
| 99 | - // compute number of bytes required |
|
| 100 | - $width = 1; |
|
| 101 | - if ($num > 128) { |
|
| 102 | - $tmp = $num; |
|
| 103 | - do { |
|
| 104 | - $width++; |
|
| 105 | - $tmp >>= 8; |
|
| 106 | - } while ($tmp > 128); |
|
| 107 | - } |
|
| 108 | - // compute two's complement 2^n - x |
|
| 109 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
| 110 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 111 | - // if first bit is 0, prepend full inverted byte |
|
| 112 | - // to represent negative two's complement |
|
| 113 | - if (!(ord($bin[0]) & 0x80)) { |
|
| 114 | - $bin = chr(0xff) . $bin; |
|
| 115 | - } |
|
| 116 | - return $bin; |
|
| 117 | - } |
|
| 90 | + /** |
|
| 91 | + * Encode negative integer to DER content. |
|
| 92 | + * |
|
| 93 | + * @param \GMP|resource $num |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
| 97 | + { |
|
| 98 | + $num = gmp_abs($num); |
|
| 99 | + // compute number of bytes required |
|
| 100 | + $width = 1; |
|
| 101 | + if ($num > 128) { |
|
| 102 | + $tmp = $num; |
|
| 103 | + do { |
|
| 104 | + $width++; |
|
| 105 | + $tmp >>= 8; |
|
| 106 | + } while ($tmp > 128); |
|
| 107 | + } |
|
| 108 | + // compute two's complement 2^n - x |
|
| 109 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
| 110 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 111 | + // if first bit is 0, prepend full inverted byte |
|
| 112 | + // to represent negative two's complement |
|
| 113 | + if (!(ord($bin[0]) & 0x80)) { |
|
| 114 | + $bin = chr(0xff) . $bin; |
|
| 115 | + } |
|
| 116 | + return $bin; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * |
|
| 121 | - * {@inheritdoc} |
|
| 122 | - * @return self |
|
| 123 | - */ |
|
| 124 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 125 | - int &$offset): ElementBase |
|
| 126 | - { |
|
| 127 | - $idx = $offset; |
|
| 128 | - $length = Length::expectFromDER($data, $idx); |
|
| 129 | - if (gmp_cmp(gmp_init($length->length(), 10), gmp_init(PHP_INT_MAX, 10)) >= |
|
| 130 | - 0) { |
|
| 131 | - throw new \RuntimeException("Integer length too large"); |
|
| 132 | - } |
|
| 119 | + /** |
|
| 120 | + * |
|
| 121 | + * {@inheritdoc} |
|
| 122 | + * @return self |
|
| 123 | + */ |
|
| 124 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 125 | + int &$offset): ElementBase |
|
| 126 | + { |
|
| 127 | + $idx = $offset; |
|
| 128 | + $length = Length::expectFromDER($data, $idx); |
|
| 129 | + if (gmp_cmp(gmp_init($length->length(), 10), gmp_init(PHP_INT_MAX, 10)) >= |
|
| 130 | + 0) { |
|
| 131 | + throw new \RuntimeException("Integer length too large"); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - $bytes = substr($data, $idx, (int) $length->length()); |
|
| 135 | - $idx += $length->length(); |
|
| 136 | - $neg = ord($bytes[0]) & 0x80; |
|
| 137 | - // negative, apply inversion of two's complement |
|
| 138 | - if ($neg) { |
|
| 139 | - $len = strlen($bytes); |
|
| 140 | - for ($i = 0; $i < $len; $i++) { |
|
| 141 | - $bytes[$i] = ~$bytes[$i]; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - $num = gmp_init(bin2hex($bytes), 16); |
|
| 145 | - // negative, apply addition of two's complement |
|
| 146 | - // and produce negative result |
|
| 147 | - if ($neg) { |
|
| 148 | - $num = gmp_neg($num + 1); |
|
| 149 | - } |
|
| 150 | - $offset = $idx; |
|
| 151 | - // late static binding since enumerated extends integer type |
|
| 152 | - return new static(gmp_strval($num, 10)); |
|
| 153 | - } |
|
| 134 | + $bytes = substr($data, $idx, (int) $length->length()); |
|
| 135 | + $idx += $length->length(); |
|
| 136 | + $neg = ord($bytes[0]) & 0x80; |
|
| 137 | + // negative, apply inversion of two's complement |
|
| 138 | + if ($neg) { |
|
| 139 | + $len = strlen($bytes); |
|
| 140 | + for ($i = 0; $i < $len; $i++) { |
|
| 141 | + $bytes[$i] = ~$bytes[$i]; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + $num = gmp_init(bin2hex($bytes), 16); |
|
| 145 | + // negative, apply addition of two's complement |
|
| 146 | + // and produce negative result |
|
| 147 | + if ($neg) { |
|
| 148 | + $num = gmp_neg($num + 1); |
|
| 149 | + } |
|
| 150 | + $offset = $idx; |
|
| 151 | + // late static binding since enumerated extends integer type |
|
| 152 | + return new static(gmp_strval($num, 10)); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Test that number is valid for this context. |
|
| 157 | - * |
|
| 158 | - * @param mixed $num |
|
| 159 | - * @return boolean |
|
| 160 | - */ |
|
| 161 | - private static function _validateNumber($num): bool |
|
| 162 | - { |
|
| 163 | - if (is_int($num)) { |
|
| 164 | - return true; |
|
| 165 | - } |
|
| 166 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 167 | - return true; |
|
| 168 | - } |
|
| 169 | - return false; |
|
| 170 | - } |
|
| 155 | + /** |
|
| 156 | + * Test that number is valid for this context. |
|
| 157 | + * |
|
| 158 | + * @param mixed $num |
|
| 159 | + * @return boolean |
|
| 160 | + */ |
|
| 161 | + private static function _validateNumber($num): bool |
|
| 162 | + { |
|
| 163 | + if (is_int($num)) { |
|
| 164 | + return true; |
|
| 165 | + } |
|
| 166 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 167 | + return true; |
|
| 168 | + } |
|
| 169 | + return false; |
|
| 170 | + } |
|
| 171 | 171 | } |
@@ -122,7 +122,7 @@ |
||
| 122 | 122 | * @return self |
| 123 | 123 | */ |
| 124 | 124 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 125 | - int &$offset): ElementBase |
|
| 125 | + int & $offset): ElementBase |
|
| 126 | 126 | { |
| 127 | 127 | $idx = $offset; |
| 128 | 128 | $length = Length::expectFromDER($data, $idx); |
@@ -7,14 +7,14 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Enumerated extends Integer |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Constructor. |
|
| 12 | - * |
|
| 13 | - * @param int|string $number |
|
| 14 | - */ |
|
| 15 | - public function __construct($number) |
|
| 16 | - { |
|
| 17 | - parent::__construct($number); |
|
| 18 | - $this->_typeTag = self::TYPE_ENUMERATED; |
|
| 19 | - } |
|
| 10 | + /** |
|
| 11 | + * Constructor. |
|
| 12 | + * |
|
| 13 | + * @param int|string $number |
|
| 14 | + */ |
|
| 15 | + public function __construct($number) |
|
| 16 | + { |
|
| 17 | + parent::__construct($number); |
|
| 18 | + $this->_typeTag = self::TYPE_ENUMERATED; |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | { |
| 32 | 32 | $obj = clone $this; |
| 33 | 33 | usort($obj->_elements, |
| 34 | - function (Element $a, Element $b) { |
|
| 34 | + function(Element $a, Element $b) { |
|
| 35 | 35 | if ($a->typeClass() != $b->typeClass()) { |
| 36 | 36 | return $a->typeClass() < $b->typeClass() ? -1 : 1; |
| 37 | 37 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | { |
| 54 | 54 | $obj = clone $this; |
| 55 | 55 | usort($obj->_elements, |
| 56 | - function (Element $a, Element $b) { |
|
| 56 | + function(Element $a, Element $b) { |
|
| 57 | 57 | $a_der = $a->toDER(); |
| 58 | 58 | $b_der = $b->toDER(); |
| 59 | 59 | return strcmp($a_der, $b_der); |
@@ -12,54 +12,54 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Set extends Structure |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Constructor |
|
| 17 | - * |
|
| 18 | - * @param Element[] $elements Any number of elements |
|
| 19 | - */ |
|
| 20 | - public function __construct(Element ...$elements) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_SET; |
|
| 23 | - parent::__construct(...$elements); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor |
|
| 17 | + * |
|
| 18 | + * @param Element[] $elements Any number of elements |
|
| 19 | + */ |
|
| 20 | + public function __construct(Element ...$elements) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_SET; |
|
| 23 | + parent::__construct(...$elements); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Sort by canonical ascending order. |
|
| 28 | - * Used for DER encoding of SET type. |
|
| 29 | - * |
|
| 30 | - * @return self |
|
| 31 | - */ |
|
| 32 | - public function sortedSet(): self |
|
| 33 | - { |
|
| 34 | - $obj = clone $this; |
|
| 35 | - usort($obj->_elements, |
|
| 36 | - function (Element $a, Element $b) { |
|
| 37 | - if ($a->typeClass() != $b->typeClass()) { |
|
| 38 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
| 39 | - } |
|
| 40 | - if ($a->tag() == $b->tag()) { |
|
| 41 | - return 0; |
|
| 42 | - } |
|
| 43 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
| 44 | - }); |
|
| 45 | - return $obj; |
|
| 46 | - } |
|
| 26 | + /** |
|
| 27 | + * Sort by canonical ascending order. |
|
| 28 | + * Used for DER encoding of SET type. |
|
| 29 | + * |
|
| 30 | + * @return self |
|
| 31 | + */ |
|
| 32 | + public function sortedSet(): self |
|
| 33 | + { |
|
| 34 | + $obj = clone $this; |
|
| 35 | + usort($obj->_elements, |
|
| 36 | + function (Element $a, Element $b) { |
|
| 37 | + if ($a->typeClass() != $b->typeClass()) { |
|
| 38 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
| 39 | + } |
|
| 40 | + if ($a->tag() == $b->tag()) { |
|
| 41 | + return 0; |
|
| 42 | + } |
|
| 43 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
| 44 | + }); |
|
| 45 | + return $obj; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Sort by encoding ascending order. |
|
| 50 | - * Used for DER encoding of SET OF type. |
|
| 51 | - * |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public function sortedSetOf(): self |
|
| 55 | - { |
|
| 56 | - $obj = clone $this; |
|
| 57 | - usort($obj->_elements, |
|
| 58 | - function (Element $a, Element $b) { |
|
| 59 | - $a_der = $a->toDER(); |
|
| 60 | - $b_der = $b->toDER(); |
|
| 61 | - return strcmp($a_der, $b_der); |
|
| 62 | - }); |
|
| 63 | - return $obj; |
|
| 64 | - } |
|
| 48 | + /** |
|
| 49 | + * Sort by encoding ascending order. |
|
| 50 | + * Used for DER encoding of SET OF type. |
|
| 51 | + * |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public function sortedSetOf(): self |
|
| 55 | + { |
|
| 56 | + $obj = clone $this; |
|
| 57 | + usort($obj->_elements, |
|
| 58 | + function (Element $a, Element $b) { |
|
| 59 | + $a_der = $a->toDER(); |
|
| 60 | + $b_der = $b->toDER(); |
|
| 61 | + return strcmp($a_der, $b_der); |
|
| 62 | + }); |
|
| 63 | + return $obj; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -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 |
@@ -17,271 +17,271 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Real extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 23 | + /** |
|
| 24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Regex pattern to parse PHP exponent number format. |
|
| 32 | - * |
|
| 33 | - * @link http://php.net/manual/en/language.types.float.php |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | - '([+\-]?'. // sign |
|
| 38 | - '(?:'. |
|
| 39 | - '\d+'. // LNUM |
|
| 40 | - '|'. |
|
| 41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | - '))[eE]'. |
|
| 43 | - '([+\-]?\d+)'. // exponent |
|
| 44 | - '$/'; /* @formatter:on */ |
|
| 30 | + /** |
|
| 31 | + * Regex pattern to parse PHP exponent number format. |
|
| 32 | + * |
|
| 33 | + * @link http://php.net/manual/en/language.types.float.php |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | + '([+\-]?'. // sign |
|
| 38 | + '(?:'. |
|
| 39 | + '\d+'. // LNUM |
|
| 40 | + '|'. |
|
| 41 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | + '))[eE]'. |
|
| 43 | + '([+\-]?\d+)'. // exponent |
|
| 44 | + '$/'; /* @formatter:on */ |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Number zero represented in NR3 form. |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - const NR3_ZERO = ".E+0"; |
|
| 46 | + /** |
|
| 47 | + * Number zero represented in NR3 form. |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + const NR3_ZERO = ".E+0"; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Number in NR3 form. |
|
| 55 | - * |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $_number; |
|
| 53 | + /** |
|
| 54 | + * Number in NR3 form. |
|
| 55 | + * |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $_number; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Constructor. |
|
| 62 | - * |
|
| 63 | - * @param string $number Number in NR3 form. |
|
| 64 | - */ |
|
| 65 | - public function __construct(string $number) |
|
| 66 | - { |
|
| 67 | - $this->_typeTag = self::TYPE_REAL; |
|
| 68 | - if (!self::_validateNumber($number)) { |
|
| 69 | - throw new \InvalidArgumentException( |
|
| 70 | - "'$number' is not a valid NR3 form real."); |
|
| 71 | - } |
|
| 72 | - $this->_number = $number; |
|
| 73 | - } |
|
| 60 | + /** |
|
| 61 | + * Constructor. |
|
| 62 | + * |
|
| 63 | + * @param string $number Number in NR3 form. |
|
| 64 | + */ |
|
| 65 | + public function __construct(string $number) |
|
| 66 | + { |
|
| 67 | + $this->_typeTag = self::TYPE_REAL; |
|
| 68 | + if (!self::_validateNumber($number)) { |
|
| 69 | + throw new \InvalidArgumentException( |
|
| 70 | + "'$number' is not a valid NR3 form real."); |
|
| 71 | + } |
|
| 72 | + $this->_number = $number; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Initialize from float. |
|
| 77 | - * |
|
| 78 | - * @param float $number |
|
| 79 | - * @return self |
|
| 80 | - */ |
|
| 81 | - public static function fromFloat(float $number): self |
|
| 82 | - { |
|
| 83 | - return new self(self::_decimalToNR3(strval($number))); |
|
| 84 | - } |
|
| 75 | + /** |
|
| 76 | + * Initialize from float. |
|
| 77 | + * |
|
| 78 | + * @param float $number |
|
| 79 | + * @return self |
|
| 80 | + */ |
|
| 81 | + public static function fromFloat(float $number): self |
|
| 82 | + { |
|
| 83 | + return new self(self::_decimalToNR3(strval($number))); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get number as a float. |
|
| 88 | - * |
|
| 89 | - * @return float |
|
| 90 | - */ |
|
| 91 | - public function float(): float |
|
| 92 | - { |
|
| 93 | - return self::_nr3ToDecimal($this->_number); |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Get number as a float. |
|
| 88 | + * |
|
| 89 | + * @return float |
|
| 90 | + */ |
|
| 91 | + public function float(): float |
|
| 92 | + { |
|
| 93 | + return self::_nr3ToDecimal($this->_number); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * |
|
| 98 | - * {@inheritdoc} |
|
| 99 | - */ |
|
| 100 | - protected function _encodedContentDER(): string |
|
| 101 | - { |
|
| 102 | - /* if the real value is the value zero, there shall be no contents |
|
| 96 | + /** |
|
| 97 | + * |
|
| 98 | + * {@inheritdoc} |
|
| 99 | + */ |
|
| 100 | + protected function _encodedContentDER(): string |
|
| 101 | + { |
|
| 102 | + /* if the real value is the value zero, there shall be no contents |
|
| 103 | 103 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
| 104 | - if (self::NR3_ZERO == $this->_number) { |
|
| 105 | - return ""; |
|
| 106 | - } |
|
| 107 | - // encode in NR3 decimal encoding |
|
| 108 | - $data = chr(0x03) . $this->_number; |
|
| 109 | - return $data; |
|
| 110 | - } |
|
| 104 | + if (self::NR3_ZERO == $this->_number) { |
|
| 105 | + return ""; |
|
| 106 | + } |
|
| 107 | + // encode in NR3 decimal encoding |
|
| 108 | + $data = chr(0x03) . $this->_number; |
|
| 109 | + return $data; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 118 | - int &$offset): ElementBase |
|
| 119 | - { |
|
| 120 | - $idx = $offset; |
|
| 121 | - $length = Length::expectFromDER($data, $idx); |
|
| 122 | - // if length is zero, value is zero (spec 8.5.2) |
|
| 123 | - if (!$length->length()) { |
|
| 124 | - $obj = new self(self::NR3_ZERO); |
|
| 125 | - } else { |
|
| 126 | - $bytes = substr($data, $idx, $length->length()); |
|
| 127 | - $byte = ord($bytes[0]); |
|
| 128 | - if (0x80 & $byte) { // bit 8 = 1 |
|
| 129 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
| 130 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 131 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
| 132 | - } else { // bit 8 = 0, bit 7 = 1 |
|
| 133 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - $offset = $idx + $length->length(); |
|
| 137 | - return $obj; |
|
| 138 | - } |
|
| 112 | + /** |
|
| 113 | + * |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 118 | + int &$offset): ElementBase |
|
| 119 | + { |
|
| 120 | + $idx = $offset; |
|
| 121 | + $length = Length::expectFromDER($data, $idx); |
|
| 122 | + // if length is zero, value is zero (spec 8.5.2) |
|
| 123 | + if (!$length->length()) { |
|
| 124 | + $obj = new self(self::NR3_ZERO); |
|
| 125 | + } else { |
|
| 126 | + $bytes = substr($data, $idx, $length->length()); |
|
| 127 | + $byte = ord($bytes[0]); |
|
| 128 | + if (0x80 & $byte) { // bit 8 = 1 |
|
| 129 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
| 130 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 131 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
| 132 | + } else { // bit 8 = 0, bit 7 = 1 |
|
| 133 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + $offset = $idx + $length->length(); |
|
| 137 | + return $obj; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * |
|
| 142 | - * @todo Implement |
|
| 143 | - * @param string $data |
|
| 144 | - */ |
|
| 145 | - protected static function _decodeBinaryEncoding(string $data) |
|
| 146 | - { |
|
| 147 | - throw new \RuntimeException( |
|
| 148 | - "Binary encoding of REAL is not implemented."); |
|
| 149 | - } |
|
| 140 | + /** |
|
| 141 | + * |
|
| 142 | + * @todo Implement |
|
| 143 | + * @param string $data |
|
| 144 | + */ |
|
| 145 | + protected static function _decodeBinaryEncoding(string $data) |
|
| 146 | + { |
|
| 147 | + throw new \RuntimeException( |
|
| 148 | + "Binary encoding of REAL is not implemented."); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * |
|
| 153 | - * @param string $data |
|
| 154 | - * @throws \RuntimeException |
|
| 155 | - * @return \ASN1\Type\Primitive\Real |
|
| 156 | - */ |
|
| 157 | - protected static function _decodeDecimalEncoding(string $data): Real |
|
| 158 | - { |
|
| 159 | - $nr = ord($data[0]) & 0x03; |
|
| 160 | - if ($nr != 0x03) { |
|
| 161 | - throw new \RuntimeException("Only NR3 form supported."); |
|
| 162 | - } |
|
| 163 | - $str = substr($data, 1); |
|
| 164 | - return new self($str); |
|
| 165 | - } |
|
| 151 | + /** |
|
| 152 | + * |
|
| 153 | + * @param string $data |
|
| 154 | + * @throws \RuntimeException |
|
| 155 | + * @return \ASN1\Type\Primitive\Real |
|
| 156 | + */ |
|
| 157 | + protected static function _decodeDecimalEncoding(string $data): Real |
|
| 158 | + { |
|
| 159 | + $nr = ord($data[0]) & 0x03; |
|
| 160 | + if ($nr != 0x03) { |
|
| 161 | + throw new \RuntimeException("Only NR3 form supported."); |
|
| 162 | + } |
|
| 163 | + $str = substr($data, 1); |
|
| 164 | + return new self($str); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * |
|
| 169 | - * @todo Implement |
|
| 170 | - * @param string $data |
|
| 171 | - */ |
|
| 172 | - protected static function _decodeSpecialRealValue(string $data) |
|
| 173 | - { |
|
| 174 | - if (strlen($data) != 1) { |
|
| 175 | - throw new DecodeException( |
|
| 176 | - "SpecialRealValue must have one content octet."); |
|
| 177 | - } |
|
| 178 | - $byte = ord($data[0]); |
|
| 179 | - if ($byte == 0x40) { // positive infinity |
|
| 180 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 181 | - } else if ($byte == 0x41) { // negative infinity |
|
| 182 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 183 | - } else { |
|
| 184 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 185 | - } |
|
| 186 | - } |
|
| 167 | + /** |
|
| 168 | + * |
|
| 169 | + * @todo Implement |
|
| 170 | + * @param string $data |
|
| 171 | + */ |
|
| 172 | + protected static function _decodeSpecialRealValue(string $data) |
|
| 173 | + { |
|
| 174 | + if (strlen($data) != 1) { |
|
| 175 | + throw new DecodeException( |
|
| 176 | + "SpecialRealValue must have one content octet."); |
|
| 177 | + } |
|
| 178 | + $byte = ord($data[0]); |
|
| 179 | + if ($byte == 0x40) { // positive infinity |
|
| 180 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 181 | + } else if ($byte == 0x41) { // negative infinity |
|
| 182 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 183 | + } else { |
|
| 184 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Convert decimal number string to NR3 form. |
|
| 190 | - * |
|
| 191 | - * @param string $str |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - private static function _decimalToNR3(string $str): string |
|
| 195 | - { |
|
| 196 | - // if number is in exponent form |
|
| 197 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 198 | - $parts = explode(".", $match[1]); |
|
| 199 | - $m = ltrim($parts[0], "0"); |
|
| 200 | - $e = intval($match[2]); |
|
| 201 | - // if mantissa had decimals |
|
| 202 | - if (count($parts) == 2) { |
|
| 203 | - $d = rtrim($parts[1], "0"); |
|
| 204 | - $e -= strlen($d); |
|
| 205 | - $m .= $d; |
|
| 206 | - } |
|
| 207 | - } else { |
|
| 208 | - // explode from decimal |
|
| 209 | - $parts = explode(".", $str); |
|
| 210 | - $m = ltrim($parts[0], "0"); |
|
| 211 | - // if number had decimals |
|
| 212 | - if (count($parts) == 2) { |
|
| 213 | - // exponent is negative number of the decimals |
|
| 214 | - $e = -strlen($parts[1]); |
|
| 215 | - // append decimals to the mantissa |
|
| 216 | - $m .= $parts[1]; |
|
| 217 | - } else { |
|
| 218 | - $e = 0; |
|
| 219 | - } |
|
| 220 | - // shift trailing zeroes from the mantissa to the exponent |
|
| 221 | - while (substr($m, -1) === "0") { |
|
| 222 | - $e++; |
|
| 223 | - $m = substr($m, 0, -1); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 188 | + /** |
|
| 189 | + * Convert decimal number string to NR3 form. |
|
| 190 | + * |
|
| 191 | + * @param string $str |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + private static function _decimalToNR3(string $str): string |
|
| 195 | + { |
|
| 196 | + // if number is in exponent form |
|
| 197 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 198 | + $parts = explode(".", $match[1]); |
|
| 199 | + $m = ltrim($parts[0], "0"); |
|
| 200 | + $e = intval($match[2]); |
|
| 201 | + // if mantissa had decimals |
|
| 202 | + if (count($parts) == 2) { |
|
| 203 | + $d = rtrim($parts[1], "0"); |
|
| 204 | + $e -= strlen($d); |
|
| 205 | + $m .= $d; |
|
| 206 | + } |
|
| 207 | + } else { |
|
| 208 | + // explode from decimal |
|
| 209 | + $parts = explode(".", $str); |
|
| 210 | + $m = ltrim($parts[0], "0"); |
|
| 211 | + // if number had decimals |
|
| 212 | + if (count($parts) == 2) { |
|
| 213 | + // exponent is negative number of the decimals |
|
| 214 | + $e = -strlen($parts[1]); |
|
| 215 | + // append decimals to the mantissa |
|
| 216 | + $m .= $parts[1]; |
|
| 217 | + } else { |
|
| 218 | + $e = 0; |
|
| 219 | + } |
|
| 220 | + // shift trailing zeroes from the mantissa to the exponent |
|
| 221 | + while (substr($m, -1) === "0") { |
|
| 222 | + $e++; |
|
| 223 | + $m = substr($m, 0, -1); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 227 | 227 | (X.690 07-2002, section 11.3.2.6) */ |
| 228 | - if (0 == $e) { |
|
| 229 | - $es = "+"; |
|
| 230 | - } else { |
|
| 231 | - $es = $e < 0 ? "-" : ""; |
|
| 232 | - } |
|
| 233 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 234 | - } |
|
| 228 | + if (0 == $e) { |
|
| 229 | + $es = "+"; |
|
| 230 | + } else { |
|
| 231 | + $es = $e < 0 ? "-" : ""; |
|
| 232 | + } |
|
| 233 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Convert NR3 form number to decimal. |
|
| 238 | - * |
|
| 239 | - * @param string $str |
|
| 240 | - * @throws \UnexpectedValueException |
|
| 241 | - * @return float |
|
| 242 | - */ |
|
| 243 | - private static function _nr3ToDecimal(string $str): float |
|
| 244 | - { |
|
| 245 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 246 | - throw new \UnexpectedValueException( |
|
| 247 | - "'$str' is not a valid NR3 form real."); |
|
| 248 | - } |
|
| 249 | - $m = $match[2]; |
|
| 250 | - // if number started with minus sign |
|
| 251 | - $inv = $match[1] == "-"; |
|
| 252 | - $e = intval($match[3]); |
|
| 253 | - // positive exponent |
|
| 254 | - if ($e > 0) { |
|
| 255 | - // pad with trailing zeroes |
|
| 256 | - $num = $m . str_repeat("0", $e); |
|
| 257 | - } else if ($e < 0) { |
|
| 258 | - // pad with leading zeroes |
|
| 259 | - if (strlen($m) < abs($e)) { |
|
| 260 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 261 | - } |
|
| 262 | - // insert decimal point |
|
| 263 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 264 | - } else { |
|
| 265 | - $num = empty($m) ? "0" : $m; |
|
| 266 | - } |
|
| 267 | - // if number is negative |
|
| 268 | - if ($inv) { |
|
| 269 | - $num = "-$num"; |
|
| 270 | - } |
|
| 271 | - return floatval($num); |
|
| 272 | - } |
|
| 236 | + /** |
|
| 237 | + * Convert NR3 form number to decimal. |
|
| 238 | + * |
|
| 239 | + * @param string $str |
|
| 240 | + * @throws \UnexpectedValueException |
|
| 241 | + * @return float |
|
| 242 | + */ |
|
| 243 | + private static function _nr3ToDecimal(string $str): float |
|
| 244 | + { |
|
| 245 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 246 | + throw new \UnexpectedValueException( |
|
| 247 | + "'$str' is not a valid NR3 form real."); |
|
| 248 | + } |
|
| 249 | + $m = $match[2]; |
|
| 250 | + // if number started with minus sign |
|
| 251 | + $inv = $match[1] == "-"; |
|
| 252 | + $e = intval($match[3]); |
|
| 253 | + // positive exponent |
|
| 254 | + if ($e > 0) { |
|
| 255 | + // pad with trailing zeroes |
|
| 256 | + $num = $m . str_repeat("0", $e); |
|
| 257 | + } else if ($e < 0) { |
|
| 258 | + // pad with leading zeroes |
|
| 259 | + if (strlen($m) < abs($e)) { |
|
| 260 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 261 | + } |
|
| 262 | + // insert decimal point |
|
| 263 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 264 | + } else { |
|
| 265 | + $num = empty($m) ? "0" : $m; |
|
| 266 | + } |
|
| 267 | + // if number is negative |
|
| 268 | + if ($inv) { |
|
| 269 | + $num = "-$num"; |
|
| 270 | + } |
|
| 271 | + return floatval($num); |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Test that number is valid for this context. |
|
| 276 | - * |
|
| 277 | - * @param mixed $num |
|
| 278 | - * @return boolean |
|
| 279 | - */ |
|
| 280 | - private static function _validateNumber($num): bool |
|
| 281 | - { |
|
| 282 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 283 | - return false; |
|
| 284 | - } |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 274 | + /** |
|
| 275 | + * Test that number is valid for this context. |
|
| 276 | + * |
|
| 277 | + * @param mixed $num |
|
| 278 | + * @return boolean |
|
| 279 | + */ |
|
| 280 | + private static function _validateNumber($num): bool |
|
| 281 | + { |
|
| 282 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | * @link http://php.net/manual/en/language.types.float.php |
| 34 | 34 | * @var string |
| 35 | 35 | */ |
| 36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | - '([+\-]?'. // sign |
|
| 38 | - '(?:'. |
|
| 39 | - '\d+'. // LNUM |
|
| 40 | - '|'. |
|
| 41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | - '))[eE]'. |
|
| 43 | - '([+\-]?\d+)'. // exponent |
|
| 36 | + const PHP_EXPONENT_DNUM = '/^' . /* @formatter:off */ |
|
| 37 | + '([+\-]?' . // sign |
|
| 38 | + '(?:' . |
|
| 39 | + '\d+' . // LNUM |
|
| 40 | + '|' . |
|
| 41 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
| 42 | + '))[eE]' . |
|
| 43 | + '([+\-]?\d+)' . // exponent |
|
| 44 | 44 | '$/'; /* @formatter:on */ |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | * @return self |
| 116 | 116 | */ |
| 117 | 117 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 118 | - int &$offset): ElementBase |
|
| 118 | + int & $offset): ElementBase |
|
| 119 | 119 | { |
| 120 | 120 | $idx = $offset; |
| 121 | 121 | $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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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(): string; |
|
| 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(): string; |
|
| 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): ElementBase |
|
| 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): ElementBase |
|
| 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): ElementBase |
|
| 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): ElementBase |
|
| 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(): int |
|
| 267 | - { |
|
| 268 | - return $this->_typeTag; |
|
| 269 | - } |
|
| 261 | + /** |
|
| 262 | + * |
|
| 263 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 264 | + * @return int |
|
| 265 | + */ |
|
| 266 | + public function tag(): int |
|
| 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 | } |
@@ -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): ElementBase |
|
| 194 | + int & $offset): ElementBase |
|
| 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): ElementBase |
|
| 213 | + public static function fromDER(string $data, int & $offset = null): ElementBase |
|
| 214 | 214 | { |
| 215 | 215 | // decode identifier |
| 216 | 216 | $idx = $offset ? $offset : 0; |
@@ -11,13 +11,13 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | trait UniversalClass |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * |
|
| 16 | - * @see \ASN1\Element::typeClass() |
|
| 17 | - * @return int |
|
| 18 | - */ |
|
| 19 | - public function typeClass(): int |
|
| 20 | - { |
|
| 21 | - return Identifier::CLASS_UNIVERSAL; |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * |
|
| 16 | + * @see \ASN1\Element::typeClass() |
|
| 17 | + * @return int |
|
| 18 | + */ |
|
| 19 | + public function typeClass(): int |
|
| 20 | + { |
|
| 21 | + return Identifier::CLASS_UNIVERSAL; |
|
| 22 | + } |
|
| 23 | 23 | } |
@@ -12,14 +12,14 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Sequence extends Structure |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Constructor |
|
| 17 | - * |
|
| 18 | - * @param Element[] $elements Any number of elements |
|
| 19 | - */ |
|
| 20 | - public function __construct(Element ...$elements) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_SEQUENCE; |
|
| 23 | - parent::__construct(...$elements); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor |
|
| 17 | + * |
|
| 18 | + * @param Element[] $elements Any number of elements |
|
| 19 | + */ |
|
| 20 | + public function __construct(Element ...$elements) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_SEQUENCE; |
|
| 23 | + parent::__construct(...$elements); |
|
| 24 | + } |
|
| 25 | 25 | } |