@@ -14,28 +14,28 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class UniversalString extends PrimitiveString |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string |
|
| 23 | - */ |
|
| 24 | - public function __construct(string $string) |
|
| 25 | - { |
|
| 26 | - $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | - parent::__construct($string); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string |
|
| 23 | + */ |
|
| 24 | + public function __construct(string $string) |
|
| 25 | + { |
|
| 26 | + $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | + parent::__construct($string); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - protected function _validateString(string $string): bool |
|
| 34 | - { |
|
| 35 | - // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 36 | - if (0 !== strlen($string) % 4) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 30 | + /** |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + protected function _validateString(string $string): bool |
|
| 34 | + { |
|
| 35 | + // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 36 | + if (0 !== strlen($string) % 4) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -17,194 +17,194 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class BitString extends StringType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Number of unused bits in the last octet. |
|
| 25 | - * |
|
| 26 | - * @var int |
|
| 27 | - */ |
|
| 28 | - protected $_unusedBits; |
|
| 23 | + /** |
|
| 24 | + * Number of unused bits in the last octet. |
|
| 25 | + * |
|
| 26 | + * @var int |
|
| 27 | + */ |
|
| 28 | + protected $_unusedBits; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $string Content octets |
|
| 34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | - */ |
|
| 36 | - public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | - { |
|
| 38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | - parent::__construct($string); |
|
| 40 | - $this->_unusedBits = $unused_bits; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $string Content octets |
|
| 34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | + */ |
|
| 36 | + public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | + { |
|
| 38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | + parent::__construct($string); |
|
| 40 | + $this->_unusedBits = $unused_bits; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the number of bits in the string. |
|
| 45 | - * |
|
| 46 | - * @return int |
|
| 47 | - */ |
|
| 48 | - public function numBits(): int |
|
| 49 | - { |
|
| 50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Get the number of bits in the string. |
|
| 45 | + * |
|
| 46 | + * @return int |
|
| 47 | + */ |
|
| 48 | + public function numBits(): int |
|
| 49 | + { |
|
| 50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Get the number of unused bits in the last octet of the string. |
|
| 55 | - * |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public function unusedBits(): int |
|
| 59 | - { |
|
| 60 | - return $this->_unusedBits; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Get the number of unused bits in the last octet of the string. |
|
| 55 | + * |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public function unusedBits(): int |
|
| 59 | + { |
|
| 60 | + return $this->_unusedBits; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Test whether bit is set. |
|
| 65 | - * |
|
| 66 | - * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
| 67 | - * |
|
| 68 | - * @return bool |
|
| 69 | - */ |
|
| 70 | - public function testBit(int $idx): bool |
|
| 71 | - { |
|
| 72 | - // octet index |
|
| 73 | - $oi = (int) floor($idx / 8); |
|
| 74 | - // if octet is outside range |
|
| 75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
| 77 | - } |
|
| 78 | - // bit index |
|
| 79 | - $bi = $idx % 8; |
|
| 80 | - // if tested bit is last octet's unused bit |
|
| 81 | - if ($oi === strlen($this->_string) - 1) { |
|
| 82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | - throw new \OutOfBoundsException( |
|
| 84 | - 'Index refers to an unused bit.'); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - $byte = $this->_string[$oi]; |
|
| 88 | - // index 0 is the most significant bit in byte |
|
| 89 | - $mask = 0x01 << (7 - $bi); |
|
| 90 | - return (ord($byte) & $mask) > 0; |
|
| 91 | - } |
|
| 63 | + /** |
|
| 64 | + * Test whether bit is set. |
|
| 65 | + * |
|
| 66 | + * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
| 67 | + * |
|
| 68 | + * @return bool |
|
| 69 | + */ |
|
| 70 | + public function testBit(int $idx): bool |
|
| 71 | + { |
|
| 72 | + // octet index |
|
| 73 | + $oi = (int) floor($idx / 8); |
|
| 74 | + // if octet is outside range |
|
| 75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
| 77 | + } |
|
| 78 | + // bit index |
|
| 79 | + $bi = $idx % 8; |
|
| 80 | + // if tested bit is last octet's unused bit |
|
| 81 | + if ($oi === strlen($this->_string) - 1) { |
|
| 82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | + throw new \OutOfBoundsException( |
|
| 84 | + 'Index refers to an unused bit.'); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + $byte = $this->_string[$oi]; |
|
| 88 | + // index 0 is the most significant bit in byte |
|
| 89 | + $mask = 0x01 << (7 - $bi); |
|
| 90 | + return (ord($byte) & $mask) > 0; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get range of bits. |
|
| 95 | - * |
|
| 96 | - * @param int $start Index of first bit |
|
| 97 | - * @param int $length Number of bits in range |
|
| 98 | - * |
|
| 99 | - * @throws \OutOfBoundsException |
|
| 100 | - * |
|
| 101 | - * @return string Integer of $length bits |
|
| 102 | - */ |
|
| 103 | - public function range(int $start, int $length): string |
|
| 104 | - { |
|
| 105 | - if (!$length) { |
|
| 106 | - return '0'; |
|
| 107 | - } |
|
| 108 | - if ($start + $length > $this->numBits()) { |
|
| 109 | - throw new \OutOfBoundsException('Not enough bits.'); |
|
| 110 | - } |
|
| 111 | - $bits = gmp_init(0); |
|
| 112 | - $idx = $start; |
|
| 113 | - $end = $start + $length; |
|
| 114 | - while (true) { |
|
| 115 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
| 116 | - $bits |= $bit; |
|
| 117 | - if (++$idx >= $end) { |
|
| 118 | - break; |
|
| 119 | - } |
|
| 120 | - $bits <<= 1; |
|
| 121 | - } |
|
| 122 | - return gmp_strval($bits, 10); |
|
| 123 | - } |
|
| 93 | + /** |
|
| 94 | + * Get range of bits. |
|
| 95 | + * |
|
| 96 | + * @param int $start Index of first bit |
|
| 97 | + * @param int $length Number of bits in range |
|
| 98 | + * |
|
| 99 | + * @throws \OutOfBoundsException |
|
| 100 | + * |
|
| 101 | + * @return string Integer of $length bits |
|
| 102 | + */ |
|
| 103 | + public function range(int $start, int $length): string |
|
| 104 | + { |
|
| 105 | + if (!$length) { |
|
| 106 | + return '0'; |
|
| 107 | + } |
|
| 108 | + if ($start + $length > $this->numBits()) { |
|
| 109 | + throw new \OutOfBoundsException('Not enough bits.'); |
|
| 110 | + } |
|
| 111 | + $bits = gmp_init(0); |
|
| 112 | + $idx = $start; |
|
| 113 | + $end = $start + $length; |
|
| 114 | + while (true) { |
|
| 115 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
| 116 | + $bits |= $bit; |
|
| 117 | + if (++$idx >= $end) { |
|
| 118 | + break; |
|
| 119 | + } |
|
| 120 | + $bits <<= 1; |
|
| 121 | + } |
|
| 122 | + return gmp_strval($bits, 10); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Get a copy of the bit string with trailing zeroes removed. |
|
| 127 | - * |
|
| 128 | - * @return self |
|
| 129 | - */ |
|
| 130 | - public function withoutTrailingZeroes(): self |
|
| 131 | - { |
|
| 132 | - // if bit string was empty |
|
| 133 | - if (!strlen($this->_string)) { |
|
| 134 | - return new self(''); |
|
| 135 | - } |
|
| 136 | - $bits = $this->_string; |
|
| 137 | - // count number of empty trailing octets |
|
| 138 | - $unused_octets = 0; |
|
| 139 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 140 | - if ("\x0" !== $bits[$idx]) { |
|
| 141 | - break; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - // strip trailing octets |
|
| 145 | - if ($unused_octets) { |
|
| 146 | - $bits = substr($bits, 0, -$unused_octets); |
|
| 147 | - } |
|
| 148 | - // if bit string was full of zeroes |
|
| 149 | - if (!strlen($bits)) { |
|
| 150 | - return new self(''); |
|
| 151 | - } |
|
| 152 | - // count number of trailing zeroes in the last octet |
|
| 153 | - $unused_bits = 0; |
|
| 154 | - $byte = ord($bits[strlen($bits) - 1]); |
|
| 155 | - while (!($byte & 0x01)) { |
|
| 156 | - ++$unused_bits; |
|
| 157 | - $byte >>= 1; |
|
| 158 | - } |
|
| 159 | - return new self($bits, $unused_bits); |
|
| 160 | - } |
|
| 125 | + /** |
|
| 126 | + * Get a copy of the bit string with trailing zeroes removed. |
|
| 127 | + * |
|
| 128 | + * @return self |
|
| 129 | + */ |
|
| 130 | + public function withoutTrailingZeroes(): self |
|
| 131 | + { |
|
| 132 | + // if bit string was empty |
|
| 133 | + if (!strlen($this->_string)) { |
|
| 134 | + return new self(''); |
|
| 135 | + } |
|
| 136 | + $bits = $this->_string; |
|
| 137 | + // count number of empty trailing octets |
|
| 138 | + $unused_octets = 0; |
|
| 139 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 140 | + if ("\x0" !== $bits[$idx]) { |
|
| 141 | + break; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + // strip trailing octets |
|
| 145 | + if ($unused_octets) { |
|
| 146 | + $bits = substr($bits, 0, -$unused_octets); |
|
| 147 | + } |
|
| 148 | + // if bit string was full of zeroes |
|
| 149 | + if (!strlen($bits)) { |
|
| 150 | + return new self(''); |
|
| 151 | + } |
|
| 152 | + // count number of trailing zeroes in the last octet |
|
| 153 | + $unused_bits = 0; |
|
| 154 | + $byte = ord($bits[strlen($bits) - 1]); |
|
| 155 | + while (!($byte & 0x01)) { |
|
| 156 | + ++$unused_bits; |
|
| 157 | + $byte >>= 1; |
|
| 158 | + } |
|
| 159 | + return new self($bits, $unused_bits); |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * {@inheritdoc} |
|
| 164 | - */ |
|
| 165 | - protected function _encodedContentDER(): string |
|
| 166 | - { |
|
| 167 | - $der = chr($this->_unusedBits); |
|
| 168 | - $der .= $this->_string; |
|
| 169 | - if ($this->_unusedBits) { |
|
| 170 | - $octet = $der[strlen($der) - 1]; |
|
| 171 | - // set unused bits to zero |
|
| 172 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 173 | - $der[strlen($der) - 1] = $octet; |
|
| 174 | - } |
|
| 175 | - return $der; |
|
| 176 | - } |
|
| 162 | + /** |
|
| 163 | + * {@inheritdoc} |
|
| 164 | + */ |
|
| 165 | + protected function _encodedContentDER(): string |
|
| 166 | + { |
|
| 167 | + $der = chr($this->_unusedBits); |
|
| 168 | + $der .= $this->_string; |
|
| 169 | + if ($this->_unusedBits) { |
|
| 170 | + $octet = $der[strlen($der) - 1]; |
|
| 171 | + // set unused bits to zero |
|
| 172 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 173 | + $der[strlen($der) - 1] = $octet; |
|
| 174 | + } |
|
| 175 | + return $der; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * {@inheritdoc} |
|
| 180 | - */ |
|
| 181 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 182 | - string $data, int &$offset): ElementBase |
|
| 183 | - { |
|
| 184 | - $idx = $offset; |
|
| 185 | - $length = Length::expectFromDER($data, $idx); |
|
| 186 | - if ($length->intLength() < 1) { |
|
| 187 | - throw new DecodeException('Bit string length must be at least 1.'); |
|
| 188 | - } |
|
| 189 | - $unused_bits = ord($data[$idx++]); |
|
| 190 | - if ($unused_bits > 7) { |
|
| 191 | - throw new DecodeException( |
|
| 192 | - 'Unused bits in a bit string must be less than 8.'); |
|
| 193 | - } |
|
| 194 | - $str_len = $length->intLength() - 1; |
|
| 195 | - if ($str_len) { |
|
| 196 | - $str = substr($data, $idx, $str_len); |
|
| 197 | - if ($unused_bits) { |
|
| 198 | - $mask = (1 << $unused_bits) - 1; |
|
| 199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | - throw new DecodeException( |
|
| 201 | - 'DER encoded bit string must have zero padding.'); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - } else { |
|
| 205 | - $str = ''; |
|
| 206 | - } |
|
| 207 | - $offset = $idx + $str_len; |
|
| 208 | - return new self($str, $unused_bits); |
|
| 209 | - } |
|
| 178 | + /** |
|
| 179 | + * {@inheritdoc} |
|
| 180 | + */ |
|
| 181 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 182 | + string $data, int &$offset): ElementBase |
|
| 183 | + { |
|
| 184 | + $idx = $offset; |
|
| 185 | + $length = Length::expectFromDER($data, $idx); |
|
| 186 | + if ($length->intLength() < 1) { |
|
| 187 | + throw new DecodeException('Bit string length must be at least 1.'); |
|
| 188 | + } |
|
| 189 | + $unused_bits = ord($data[$idx++]); |
|
| 190 | + if ($unused_bits > 7) { |
|
| 191 | + throw new DecodeException( |
|
| 192 | + 'Unused bits in a bit string must be less than 8.'); |
|
| 193 | + } |
|
| 194 | + $str_len = $length->intLength() - 1; |
|
| 195 | + if ($str_len) { |
|
| 196 | + $str = substr($data, $idx, $str_len); |
|
| 197 | + if ($unused_bits) { |
|
| 198 | + $mask = (1 << $unused_bits) - 1; |
|
| 199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | + throw new DecodeException( |
|
| 201 | + 'DER encoded bit string must have zero padding.'); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + } else { |
|
| 205 | + $str = ''; |
|
| 206 | + } |
|
| 207 | + $offset = $idx + $str_len; |
|
| 208 | + return new self($str, $unused_bits); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -12,60 +12,60 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class StringType extends Element implements Stringable |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * String value. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $_string; |
|
| 15 | + /** |
|
| 16 | + * String value. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $_string; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Constructor. |
|
| 24 | - * |
|
| 25 | - * @param string $string |
|
| 26 | - * |
|
| 27 | - * @throws \InvalidArgumentException |
|
| 28 | - */ |
|
| 29 | - public function __construct(string $string) |
|
| 30 | - { |
|
| 31 | - if (!$this->_validateString($string)) { |
|
| 32 | - throw new \InvalidArgumentException( |
|
| 33 | - sprintf('Not a valid %s string.', |
|
| 34 | - self::tagToName($this->_typeTag))); |
|
| 35 | - } |
|
| 36 | - $this->_string = $string; |
|
| 37 | - } |
|
| 22 | + /** |
|
| 23 | + * Constructor. |
|
| 24 | + * |
|
| 25 | + * @param string $string |
|
| 26 | + * |
|
| 27 | + * @throws \InvalidArgumentException |
|
| 28 | + */ |
|
| 29 | + public function __construct(string $string) |
|
| 30 | + { |
|
| 31 | + if (!$this->_validateString($string)) { |
|
| 32 | + throw new \InvalidArgumentException( |
|
| 33 | + sprintf('Not a valid %s string.', |
|
| 34 | + self::tagToName($this->_typeTag))); |
|
| 35 | + } |
|
| 36 | + $this->_string = $string; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * {@inheritdoc} |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function __toString(): string |
|
| 45 | - { |
|
| 46 | - return $this->string(); |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * {@inheritdoc} |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function __toString(): string |
|
| 45 | + { |
|
| 46 | + return $this->string(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Get the string value. |
|
| 51 | - * |
|
| 52 | - * @return string |
|
| 53 | - */ |
|
| 54 | - public function string(): string |
|
| 55 | - { |
|
| 56 | - return $this->_string; |
|
| 57 | - } |
|
| 49 | + /** |
|
| 50 | + * Get the string value. |
|
| 51 | + * |
|
| 52 | + * @return string |
|
| 53 | + */ |
|
| 54 | + public function string(): string |
|
| 55 | + { |
|
| 56 | + return $this->_string; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Check whether string is valid for the concrete type. |
|
| 61 | - * |
|
| 62 | - * @param string $string |
|
| 63 | - * |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - protected function _validateString(string $string): bool |
|
| 67 | - { |
|
| 68 | - // Override in derived classes |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Check whether string is valid for the concrete type. |
|
| 61 | + * |
|
| 62 | + * @param string $string |
|
| 63 | + * |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + protected function _validateString(string $string): bool |
|
| 67 | + { |
|
| 68 | + // Override in derived classes |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -14,76 +14,76 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class DERData extends Element |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * DER encoded data. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $_der; |
|
| 17 | + /** |
|
| 18 | + * DER encoded data. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $_der; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Identifier of the underlying type. |
|
| 26 | - * |
|
| 27 | - * @var Identifier |
|
| 28 | - */ |
|
| 29 | - protected $_identifier; |
|
| 24 | + /** |
|
| 25 | + * Identifier of the underlying type. |
|
| 26 | + * |
|
| 27 | + * @var Identifier |
|
| 28 | + */ |
|
| 29 | + protected $_identifier; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Offset to the content in DER data. |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - protected $_contentOffset = 0; |
|
| 31 | + /** |
|
| 32 | + * Offset to the content in DER data. |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + protected $_contentOffset = 0; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $data DER encoded data |
|
| 42 | - * |
|
| 43 | - * @throws \Sop\ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 44 | - */ |
|
| 45 | - public function __construct(string $data) |
|
| 46 | - { |
|
| 47 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 48 | - // check that length encoding is valid |
|
| 49 | - Length::expectFromDER($data, $this->_contentOffset); |
|
| 50 | - $this->_der = $data; |
|
| 51 | - $this->_typeTag = $this->_identifier->intTag(); |
|
| 52 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $data DER encoded data |
|
| 42 | + * |
|
| 43 | + * @throws \Sop\ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 44 | + */ |
|
| 45 | + public function __construct(string $data) |
|
| 46 | + { |
|
| 47 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 48 | + // check that length encoding is valid |
|
| 49 | + Length::expectFromDER($data, $this->_contentOffset); |
|
| 50 | + $this->_der = $data; |
|
| 51 | + $this->_typeTag = $this->_identifier->intTag(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * {@inheritdoc} |
|
| 56 | - */ |
|
| 57 | - public function typeClass(): int |
|
| 58 | - { |
|
| 59 | - return $this->_identifier->typeClass(); |
|
| 60 | - } |
|
| 54 | + /** |
|
| 55 | + * {@inheritdoc} |
|
| 56 | + */ |
|
| 57 | + public function typeClass(): int |
|
| 58 | + { |
|
| 59 | + return $this->_identifier->typeClass(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - */ |
|
| 65 | - public function isConstructed(): bool |
|
| 66 | - { |
|
| 67 | - return $this->_identifier->isConstructed(); |
|
| 68 | - } |
|
| 62 | + /** |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + */ |
|
| 65 | + public function isConstructed(): bool |
|
| 66 | + { |
|
| 67 | + return $this->_identifier->isConstructed(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * {@inheritdoc} |
|
| 72 | - */ |
|
| 73 | - public function toDER(): string |
|
| 74 | - { |
|
| 75 | - return $this->_der; |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * {@inheritdoc} |
|
| 72 | + */ |
|
| 73 | + public function toDER(): string |
|
| 74 | + { |
|
| 75 | + return $this->_der; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * {@inheritdoc} |
|
| 80 | - */ |
|
| 81 | - protected function _encodedContentDER(): string |
|
| 82 | - { |
|
| 83 | - // if there's no content payload |
|
| 84 | - if (strlen($this->_der) === $this->_contentOffset) { |
|
| 85 | - return ''; |
|
| 86 | - } |
|
| 87 | - return substr($this->_der, $this->_contentOffset); |
|
| 88 | - } |
|
| 78 | + /** |
|
| 79 | + * {@inheritdoc} |
|
| 80 | + */ |
|
| 81 | + protected function _encodedContentDER(): string |
|
| 82 | + { |
|
| 83 | + // if there's no content payload |
|
| 84 | + if (strlen($this->_der) === $this->_contentOffset) { |
|
| 85 | + return ''; |
|
| 86 | + } |
|
| 87 | + return substr($this->_der, $this->_contentOffset); |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -9,107 +9,107 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class BigInt |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Number as a base10 integer string. |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - private $_num; |
|
| 12 | + /** |
|
| 13 | + * Number as a base10 integer string. |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + private $_num; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Number as an integer type. |
|
| 21 | - * |
|
| 22 | - * @internal Lazily initialized |
|
| 23 | - * |
|
| 24 | - * @var null|int |
|
| 25 | - */ |
|
| 26 | - private $_intNum; |
|
| 19 | + /** |
|
| 20 | + * Number as an integer type. |
|
| 21 | + * |
|
| 22 | + * @internal Lazily initialized |
|
| 23 | + * |
|
| 24 | + * @var null|int |
|
| 25 | + */ |
|
| 26 | + private $_intNum; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param int|string $num |
|
| 32 | - */ |
|
| 33 | - public function __construct($num) |
|
| 34 | - { |
|
| 35 | - $this->_num = strval($num); |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param int|string $num |
|
| 32 | + */ |
|
| 33 | + public function __construct($num) |
|
| 34 | + { |
|
| 35 | + $this->_num = strval($num); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function __toString() |
|
| 42 | - { |
|
| 43 | - return $this->base10(); |
|
| 44 | - } |
|
| 38 | + /** |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function __toString() |
|
| 42 | + { |
|
| 43 | + return $this->base10(); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get the number as a base10 integer string. |
|
| 48 | - * |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function base10(): string |
|
| 52 | - { |
|
| 53 | - return $this->_num; |
|
| 54 | - } |
|
| 46 | + /** |
|
| 47 | + * Get the number as a base10 integer string. |
|
| 48 | + * |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function base10(): string |
|
| 52 | + { |
|
| 53 | + return $this->_num; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get the number as an integer. |
|
| 58 | - * |
|
| 59 | - * @throws \RuntimeException If number overflows integer size |
|
| 60 | - * |
|
| 61 | - * @return int |
|
| 62 | - */ |
|
| 63 | - public function intVal(): int |
|
| 64 | - { |
|
| 65 | - if (!isset($this->_intNum)) { |
|
| 66 | - $num = gmp_init($this->_num, 10); |
|
| 67 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
| 68 | - throw new \RuntimeException('Integer overflow.'); |
|
| 69 | - } |
|
| 70 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
| 71 | - throw new \RuntimeException('Integer underflow.'); |
|
| 72 | - } |
|
| 73 | - $this->_intNum = gmp_intval($num); |
|
| 74 | - } |
|
| 75 | - return $this->_intNum; |
|
| 76 | - } |
|
| 56 | + /** |
|
| 57 | + * Get the number as an integer. |
|
| 58 | + * |
|
| 59 | + * @throws \RuntimeException If number overflows integer size |
|
| 60 | + * |
|
| 61 | + * @return int |
|
| 62 | + */ |
|
| 63 | + public function intVal(): int |
|
| 64 | + { |
|
| 65 | + if (!isset($this->_intNum)) { |
|
| 66 | + $num = gmp_init($this->_num, 10); |
|
| 67 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
| 68 | + throw new \RuntimeException('Integer overflow.'); |
|
| 69 | + } |
|
| 70 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
| 71 | + throw new \RuntimeException('Integer underflow.'); |
|
| 72 | + } |
|
| 73 | + $this->_intNum = gmp_intval($num); |
|
| 74 | + } |
|
| 75 | + return $this->_intNum; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get the number as a GMP object. |
|
| 80 | - * |
|
| 81 | - * @return \GMP |
|
| 82 | - */ |
|
| 83 | - public function gmpObj(): \GMP |
|
| 84 | - { |
|
| 85 | - return gmp_init($this->_num, 10); |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * Get the number as a GMP object. |
|
| 80 | + * |
|
| 81 | + * @return \GMP |
|
| 82 | + */ |
|
| 83 | + public function gmpObj(): \GMP |
|
| 84 | + { |
|
| 85 | + return gmp_init($this->_num, 10); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get the maximum integer value. |
|
| 90 | - * |
|
| 91 | - * @return \GMP |
|
| 92 | - */ |
|
| 93 | - private function _intMaxGmp(): \GMP |
|
| 94 | - { |
|
| 95 | - static $gmp; |
|
| 96 | - if (!isset($gmp)) { |
|
| 97 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
| 98 | - } |
|
| 99 | - return $gmp; |
|
| 100 | - } |
|
| 88 | + /** |
|
| 89 | + * Get the maximum integer value. |
|
| 90 | + * |
|
| 91 | + * @return \GMP |
|
| 92 | + */ |
|
| 93 | + private function _intMaxGmp(): \GMP |
|
| 94 | + { |
|
| 95 | + static $gmp; |
|
| 96 | + if (!isset($gmp)) { |
|
| 97 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
| 98 | + } |
|
| 99 | + return $gmp; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Get the minimum integer value. |
|
| 104 | - * |
|
| 105 | - * @return \GMP |
|
| 106 | - */ |
|
| 107 | - private function _intMinGmp(): \GMP |
|
| 108 | - { |
|
| 109 | - static $gmp; |
|
| 110 | - if (!isset($gmp)) { |
|
| 111 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
| 112 | - } |
|
| 113 | - return $gmp; |
|
| 114 | - } |
|
| 102 | + /** |
|
| 103 | + * Get the minimum integer value. |
|
| 104 | + * |
|
| 105 | + * @return \GMP |
|
| 106 | + */ |
|
| 107 | + private function _intMinGmp(): \GMP |
|
| 108 | + { |
|
| 109 | + static $gmp; |
|
| 110 | + if (!isset($gmp)) { |
|
| 111 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
| 112 | + } |
|
| 113 | + return $gmp; |
|
| 114 | + } |
|
| 115 | 115 | } |
@@ -13,301 +13,301 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Identifier implements Encodable |
| 15 | 15 | { |
| 16 | - // Type class enumerations |
|
| 17 | - const CLASS_UNIVERSAL = 0b00; |
|
| 18 | - const CLASS_APPLICATION = 0b01; |
|
| 19 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
| 20 | - const CLASS_PRIVATE = 0b11; |
|
| 16 | + // Type class enumerations |
|
| 17 | + const CLASS_UNIVERSAL = 0b00; |
|
| 18 | + const CLASS_APPLICATION = 0b01; |
|
| 19 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
| 20 | + const CLASS_PRIVATE = 0b11; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Mapping from type class to human readable name. |
|
| 24 | - * |
|
| 25 | - * @internal |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - const MAP_CLASS_TO_NAME = [ |
|
| 30 | - self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
| 31 | - self::CLASS_APPLICATION => 'APPLICATION', |
|
| 32 | - self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
| 33 | - self::CLASS_PRIVATE => 'PRIVATE', |
|
| 34 | - ]; |
|
| 22 | + /** |
|
| 23 | + * Mapping from type class to human readable name. |
|
| 24 | + * |
|
| 25 | + * @internal |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + const MAP_CLASS_TO_NAME = [ |
|
| 30 | + self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
| 31 | + self::CLASS_APPLICATION => 'APPLICATION', |
|
| 32 | + self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
| 33 | + self::CLASS_PRIVATE => 'PRIVATE', |
|
| 34 | + ]; |
|
| 35 | 35 | |
| 36 | - // P/C enumerations |
|
| 37 | - const PRIMITIVE = 0b0; |
|
| 38 | - const CONSTRUCTED = 0b1; |
|
| 36 | + // P/C enumerations |
|
| 37 | + const PRIMITIVE = 0b0; |
|
| 38 | + const CONSTRUCTED = 0b1; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Type class. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_class; |
|
| 40 | + /** |
|
| 41 | + * Type class. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_class; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Primitive or Constructed. |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - private $_pc; |
|
| 47 | + /** |
|
| 48 | + * Primitive or Constructed. |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + private $_pc; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Content type tag. |
|
| 56 | - * |
|
| 57 | - * @var BigInt |
|
| 58 | - */ |
|
| 59 | - private $_tag; |
|
| 54 | + /** |
|
| 55 | + * Content type tag. |
|
| 56 | + * |
|
| 57 | + * @var BigInt |
|
| 58 | + */ |
|
| 59 | + private $_tag; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Constructor. |
|
| 63 | - * |
|
| 64 | - * @param int $class Type class |
|
| 65 | - * @param int $pc Primitive / Constructed |
|
| 66 | - * @param int|string $tag Type tag number |
|
| 67 | - */ |
|
| 68 | - public function __construct(int $class, int $pc, $tag) |
|
| 69 | - { |
|
| 70 | - $this->_class = 0b11 & $class; |
|
| 71 | - $this->_pc = 0b1 & $pc; |
|
| 72 | - $this->_tag = new BigInt($tag); |
|
| 73 | - } |
|
| 61 | + /** |
|
| 62 | + * Constructor. |
|
| 63 | + * |
|
| 64 | + * @param int $class Type class |
|
| 65 | + * @param int $pc Primitive / Constructed |
|
| 66 | + * @param int|string $tag Type tag number |
|
| 67 | + */ |
|
| 68 | + public function __construct(int $class, int $pc, $tag) |
|
| 69 | + { |
|
| 70 | + $this->_class = 0b11 & $class; |
|
| 71 | + $this->_pc = 0b1 & $pc; |
|
| 72 | + $this->_tag = new BigInt($tag); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Decode identifier component from DER data. |
|
| 77 | - * |
|
| 78 | - * @param string $data DER encoded data |
|
| 79 | - * @param null|int $offset Reference to the variable that contains offset |
|
| 80 | - * into the data where to start parsing. |
|
| 81 | - * Variable is updated to the offset next to the |
|
| 82 | - * parsed identifier. If null, start from offset 0. |
|
| 83 | - * |
|
| 84 | - * @throws DecodeException If decoding fails |
|
| 85 | - * |
|
| 86 | - * @return self |
|
| 87 | - */ |
|
| 88 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 89 | - { |
|
| 90 | - $idx = $offset ?? 0; |
|
| 91 | - $datalen = strlen($data); |
|
| 92 | - if ($idx >= $datalen) { |
|
| 93 | - throw new DecodeException('Invalid offset.'); |
|
| 94 | - } |
|
| 95 | - $byte = ord($data[$idx++]); |
|
| 96 | - // bits 8 and 7 (class) |
|
| 97 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 98 | - $class = (0b11000000 & $byte) >> 6; |
|
| 99 | - // bit 6 (0 = primitive / 1 = constructed) |
|
| 100 | - $pc = (0b00100000 & $byte) >> 5; |
|
| 101 | - // bits 5 to 1 (tag number) |
|
| 102 | - $tag = (0b00011111 & $byte); |
|
| 103 | - // long-form identifier |
|
| 104 | - if (0x1f === $tag) { |
|
| 105 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
| 106 | - } |
|
| 107 | - if (isset($offset)) { |
|
| 108 | - $offset = $idx; |
|
| 109 | - } |
|
| 110 | - return new self($class, $pc, $tag); |
|
| 111 | - } |
|
| 75 | + /** |
|
| 76 | + * Decode identifier component from DER data. |
|
| 77 | + * |
|
| 78 | + * @param string $data DER encoded data |
|
| 79 | + * @param null|int $offset Reference to the variable that contains offset |
|
| 80 | + * into the data where to start parsing. |
|
| 81 | + * Variable is updated to the offset next to the |
|
| 82 | + * parsed identifier. If null, start from offset 0. |
|
| 83 | + * |
|
| 84 | + * @throws DecodeException If decoding fails |
|
| 85 | + * |
|
| 86 | + * @return self |
|
| 87 | + */ |
|
| 88 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 89 | + { |
|
| 90 | + $idx = $offset ?? 0; |
|
| 91 | + $datalen = strlen($data); |
|
| 92 | + if ($idx >= $datalen) { |
|
| 93 | + throw new DecodeException('Invalid offset.'); |
|
| 94 | + } |
|
| 95 | + $byte = ord($data[$idx++]); |
|
| 96 | + // bits 8 and 7 (class) |
|
| 97 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 98 | + $class = (0b11000000 & $byte) >> 6; |
|
| 99 | + // bit 6 (0 = primitive / 1 = constructed) |
|
| 100 | + $pc = (0b00100000 & $byte) >> 5; |
|
| 101 | + // bits 5 to 1 (tag number) |
|
| 102 | + $tag = (0b00011111 & $byte); |
|
| 103 | + // long-form identifier |
|
| 104 | + if (0x1f === $tag) { |
|
| 105 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
| 106 | + } |
|
| 107 | + if (isset($offset)) { |
|
| 108 | + $offset = $idx; |
|
| 109 | + } |
|
| 110 | + return new self($class, $pc, $tag); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - */ |
|
| 116 | - public function toDER(): string |
|
| 117 | - { |
|
| 118 | - $bytes = []; |
|
| 119 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 120 | - $tag = $this->_tag->gmpObj(); |
|
| 121 | - if ($tag < 0x1f) { |
|
| 122 | - $bytes[] = $byte | $tag; |
|
| 123 | - } |
|
| 124 | - // long-form identifier |
|
| 125 | - else { |
|
| 126 | - $bytes[] = $byte | 0x1f; |
|
| 127 | - $octets = []; |
|
| 128 | - for (; $tag > 0; $tag >>= 7) { |
|
| 129 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 130 | - } |
|
| 131 | - // last octet has bit 8 set to zero |
|
| 132 | - $octets[0] &= 0x7f; |
|
| 133 | - foreach (array_reverse($octets) as $octet) { |
|
| 134 | - $bytes[] = $octet; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - return pack('C*', ...$bytes); |
|
| 138 | - } |
|
| 113 | + /** |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + */ |
|
| 116 | + public function toDER(): string |
|
| 117 | + { |
|
| 118 | + $bytes = []; |
|
| 119 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 120 | + $tag = $this->_tag->gmpObj(); |
|
| 121 | + if ($tag < 0x1f) { |
|
| 122 | + $bytes[] = $byte | $tag; |
|
| 123 | + } |
|
| 124 | + // long-form identifier |
|
| 125 | + else { |
|
| 126 | + $bytes[] = $byte | 0x1f; |
|
| 127 | + $octets = []; |
|
| 128 | + for (; $tag > 0; $tag >>= 7) { |
|
| 129 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 130 | + } |
|
| 131 | + // last octet has bit 8 set to zero |
|
| 132 | + $octets[0] &= 0x7f; |
|
| 133 | + foreach (array_reverse($octets) as $octet) { |
|
| 134 | + $bytes[] = $octet; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + return pack('C*', ...$bytes); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Get class of the type. |
|
| 142 | - * |
|
| 143 | - * @return int |
|
| 144 | - */ |
|
| 145 | - public function typeClass(): int |
|
| 146 | - { |
|
| 147 | - return $this->_class; |
|
| 148 | - } |
|
| 140 | + /** |
|
| 141 | + * Get class of the type. |
|
| 142 | + * |
|
| 143 | + * @return int |
|
| 144 | + */ |
|
| 145 | + public function typeClass(): int |
|
| 146 | + { |
|
| 147 | + return $this->_class; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Get P/C. |
|
| 152 | - * |
|
| 153 | - * @return int |
|
| 154 | - */ |
|
| 155 | - public function pc(): int |
|
| 156 | - { |
|
| 157 | - return $this->_pc; |
|
| 158 | - } |
|
| 150 | + /** |
|
| 151 | + * Get P/C. |
|
| 152 | + * |
|
| 153 | + * @return int |
|
| 154 | + */ |
|
| 155 | + public function pc(): int |
|
| 156 | + { |
|
| 157 | + return $this->_pc; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Get the tag number. |
|
| 162 | - * |
|
| 163 | - * @return string Base 10 integer string |
|
| 164 | - */ |
|
| 165 | - public function tag(): string |
|
| 166 | - { |
|
| 167 | - return $this->_tag->base10(); |
|
| 168 | - } |
|
| 160 | + /** |
|
| 161 | + * Get the tag number. |
|
| 162 | + * |
|
| 163 | + * @return string Base 10 integer string |
|
| 164 | + */ |
|
| 165 | + public function tag(): string |
|
| 166 | + { |
|
| 167 | + return $this->_tag->base10(); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Get the tag as an integer. |
|
| 172 | - * |
|
| 173 | - * @return int |
|
| 174 | - */ |
|
| 175 | - public function intTag(): int |
|
| 176 | - { |
|
| 177 | - return $this->_tag->intVal(); |
|
| 178 | - } |
|
| 170 | + /** |
|
| 171 | + * Get the tag as an integer. |
|
| 172 | + * |
|
| 173 | + * @return int |
|
| 174 | + */ |
|
| 175 | + public function intTag(): int |
|
| 176 | + { |
|
| 177 | + return $this->_tag->intVal(); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Check whether type is of an universal class. |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - public function isUniversal(): bool |
|
| 186 | - { |
|
| 187 | - return self::CLASS_UNIVERSAL === $this->_class; |
|
| 188 | - } |
|
| 180 | + /** |
|
| 181 | + * Check whether type is of an universal class. |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + public function isUniversal(): bool |
|
| 186 | + { |
|
| 187 | + return self::CLASS_UNIVERSAL === $this->_class; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Check whether type is of an application class. |
|
| 192 | - * |
|
| 193 | - * @return bool |
|
| 194 | - */ |
|
| 195 | - public function isApplication(): bool |
|
| 196 | - { |
|
| 197 | - return self::CLASS_APPLICATION === $this->_class; |
|
| 198 | - } |
|
| 190 | + /** |
|
| 191 | + * Check whether type is of an application class. |
|
| 192 | + * |
|
| 193 | + * @return bool |
|
| 194 | + */ |
|
| 195 | + public function isApplication(): bool |
|
| 196 | + { |
|
| 197 | + return self::CLASS_APPLICATION === $this->_class; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Check whether type is of a context specific class. |
|
| 202 | - * |
|
| 203 | - * @return bool |
|
| 204 | - */ |
|
| 205 | - public function isContextSpecific(): bool |
|
| 206 | - { |
|
| 207 | - return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
| 208 | - } |
|
| 200 | + /** |
|
| 201 | + * Check whether type is of a context specific class. |
|
| 202 | + * |
|
| 203 | + * @return bool |
|
| 204 | + */ |
|
| 205 | + public function isContextSpecific(): bool |
|
| 206 | + { |
|
| 207 | + return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Check whether type is of a private class. |
|
| 212 | - * |
|
| 213 | - * @return bool |
|
| 214 | - */ |
|
| 215 | - public function isPrivate(): bool |
|
| 216 | - { |
|
| 217 | - return self::CLASS_PRIVATE === $this->_class; |
|
| 218 | - } |
|
| 210 | + /** |
|
| 211 | + * Check whether type is of a private class. |
|
| 212 | + * |
|
| 213 | + * @return bool |
|
| 214 | + */ |
|
| 215 | + public function isPrivate(): bool |
|
| 216 | + { |
|
| 217 | + return self::CLASS_PRIVATE === $this->_class; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Check whether content is primitive type. |
|
| 222 | - * |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - public function isPrimitive(): bool |
|
| 226 | - { |
|
| 227 | - return self::PRIMITIVE === $this->_pc; |
|
| 228 | - } |
|
| 220 | + /** |
|
| 221 | + * Check whether content is primitive type. |
|
| 222 | + * |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + public function isPrimitive(): bool |
|
| 226 | + { |
|
| 227 | + return self::PRIMITIVE === $this->_pc; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Check hether content is constructed type. |
|
| 232 | - * |
|
| 233 | - * @return bool |
|
| 234 | - */ |
|
| 235 | - public function isConstructed(): bool |
|
| 236 | - { |
|
| 237 | - return self::CONSTRUCTED === $this->_pc; |
|
| 238 | - } |
|
| 230 | + /** |
|
| 231 | + * Check hether content is constructed type. |
|
| 232 | + * |
|
| 233 | + * @return bool |
|
| 234 | + */ |
|
| 235 | + public function isConstructed(): bool |
|
| 236 | + { |
|
| 237 | + return self::CONSTRUCTED === $this->_pc; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Get self with given type class. |
|
| 242 | - * |
|
| 243 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 244 | - * |
|
| 245 | - * @return self |
|
| 246 | - */ |
|
| 247 | - public function withClass(int $class): Identifier |
|
| 248 | - { |
|
| 249 | - $obj = clone $this; |
|
| 250 | - $obj->_class = 0b11 & $class; |
|
| 251 | - return $obj; |
|
| 252 | - } |
|
| 240 | + /** |
|
| 241 | + * Get self with given type class. |
|
| 242 | + * |
|
| 243 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 244 | + * |
|
| 245 | + * @return self |
|
| 246 | + */ |
|
| 247 | + public function withClass(int $class): Identifier |
|
| 248 | + { |
|
| 249 | + $obj = clone $this; |
|
| 250 | + $obj->_class = 0b11 & $class; |
|
| 251 | + return $obj; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Get self with given type tag. |
|
| 256 | - * |
|
| 257 | - * @param int|string $tag Tag number |
|
| 258 | - * |
|
| 259 | - * @return self |
|
| 260 | - */ |
|
| 261 | - public function withTag($tag): Identifier |
|
| 262 | - { |
|
| 263 | - $obj = clone $this; |
|
| 264 | - $obj->_tag = new BigInt($tag); |
|
| 265 | - return $obj; |
|
| 266 | - } |
|
| 254 | + /** |
|
| 255 | + * Get self with given type tag. |
|
| 256 | + * |
|
| 257 | + * @param int|string $tag Tag number |
|
| 258 | + * |
|
| 259 | + * @return self |
|
| 260 | + */ |
|
| 261 | + public function withTag($tag): Identifier |
|
| 262 | + { |
|
| 263 | + $obj = clone $this; |
|
| 264 | + $obj->_tag = new BigInt($tag); |
|
| 265 | + return $obj; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Get human readable name of the type class. |
|
| 270 | - * |
|
| 271 | - * @param int $class |
|
| 272 | - * |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - public static function classToName(int $class): string |
|
| 276 | - { |
|
| 277 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 278 | - return "CLASS {$class}"; |
|
| 279 | - } |
|
| 280 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
| 281 | - } |
|
| 268 | + /** |
|
| 269 | + * Get human readable name of the type class. |
|
| 270 | + * |
|
| 271 | + * @param int $class |
|
| 272 | + * |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + public static function classToName(int $class): string |
|
| 276 | + { |
|
| 277 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 278 | + return "CLASS {$class}"; |
|
| 279 | + } |
|
| 280 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - /** |
|
| 284 | - * Parse long form tag. |
|
| 285 | - * |
|
| 286 | - * @param string $data DER data |
|
| 287 | - * @param int $offset Reference to the variable containing offset to data |
|
| 288 | - * |
|
| 289 | - * @throws DecodeException If decoding fails |
|
| 290 | - * |
|
| 291 | - * @return string Tag number |
|
| 292 | - */ |
|
| 293 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 294 | - { |
|
| 295 | - $datalen = strlen($data); |
|
| 296 | - $tag = gmp_init(0, 10); |
|
| 297 | - while (true) { |
|
| 298 | - if ($offset >= $datalen) { |
|
| 299 | - throw new DecodeException( |
|
| 300 | - 'Unexpected end of data while decoding' . |
|
| 301 | - ' long form identifier.'); |
|
| 302 | - } |
|
| 303 | - $byte = ord($data[$offset++]); |
|
| 304 | - $tag <<= 7; |
|
| 305 | - $tag |= 0x7f & $byte; |
|
| 306 | - // last byte has bit 8 set to zero |
|
| 307 | - if (!(0x80 & $byte)) { |
|
| 308 | - break; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - return gmp_strval($tag, 10); |
|
| 312 | - } |
|
| 283 | + /** |
|
| 284 | + * Parse long form tag. |
|
| 285 | + * |
|
| 286 | + * @param string $data DER data |
|
| 287 | + * @param int $offset Reference to the variable containing offset to data |
|
| 288 | + * |
|
| 289 | + * @throws DecodeException If decoding fails |
|
| 290 | + * |
|
| 291 | + * @return string Tag number |
|
| 292 | + */ |
|
| 293 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 294 | + { |
|
| 295 | + $datalen = strlen($data); |
|
| 296 | + $tag = gmp_init(0, 10); |
|
| 297 | + while (true) { |
|
| 298 | + if ($offset >= $datalen) { |
|
| 299 | + throw new DecodeException( |
|
| 300 | + 'Unexpected end of data while decoding' . |
|
| 301 | + ' long form identifier.'); |
|
| 302 | + } |
|
| 303 | + $byte = ord($data[$offset++]); |
|
| 304 | + $tag <<= 7; |
|
| 305 | + $tag |= 0x7f & $byte; |
|
| 306 | + // last byte has bit 8 set to zero |
|
| 307 | + if (!(0x80 & $byte)) { |
|
| 308 | + break; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + return gmp_strval($tag, 10); |
|
| 312 | + } |
|
| 313 | 313 | } |
@@ -13,225 +13,225 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Length implements Encodable |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Length. |
|
| 18 | - * |
|
| 19 | - * @var BigInt |
|
| 20 | - */ |
|
| 21 | - private $_length; |
|
| 16 | + /** |
|
| 17 | + * Length. |
|
| 18 | + * |
|
| 19 | + * @var BigInt |
|
| 20 | + */ |
|
| 21 | + private $_length; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Whether length is indefinite. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - private $_indefinite; |
|
| 23 | + /** |
|
| 24 | + * Whether length is indefinite. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + private $_indefinite; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param int|string $length Length |
|
| 34 | - * @param bool $indefinite Whether length is indefinite |
|
| 35 | - */ |
|
| 36 | - public function __construct($length, bool $indefinite = false) |
|
| 37 | - { |
|
| 38 | - $this->_length = new BigInt($length); |
|
| 39 | - $this->_indefinite = $indefinite; |
|
| 40 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param int|string $length Length |
|
| 34 | + * @param bool $indefinite Whether length is indefinite |
|
| 35 | + */ |
|
| 36 | + public function __construct($length, bool $indefinite = false) |
|
| 37 | + { |
|
| 38 | + $this->_length = new BigInt($length); |
|
| 39 | + $this->_indefinite = $indefinite; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Decode length component from DER data. |
|
| 44 | - * |
|
| 45 | - * @param string $data DER encoded data |
|
| 46 | - * @param null|int $offset Reference to the variable that contains offset |
|
| 47 | - * into the data where to start parsing. |
|
| 48 | - * Variable is updated to the offset next to the |
|
| 49 | - * parsed length component. If null, start from offset 0. |
|
| 50 | - * |
|
| 51 | - * @throws DecodeException If decoding fails |
|
| 52 | - * |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromDER(string $data, int &$offset = null): self |
|
| 56 | - { |
|
| 57 | - $idx = $offset ?? 0; |
|
| 58 | - $datalen = strlen($data); |
|
| 59 | - if ($idx >= $datalen) { |
|
| 60 | - throw new DecodeException( |
|
| 61 | - 'Unexpected end of data while decoding length.'); |
|
| 62 | - } |
|
| 63 | - $indefinite = false; |
|
| 64 | - $byte = ord($data[$idx++]); |
|
| 65 | - // bits 7 to 1 |
|
| 66 | - $length = (0x7f & $byte); |
|
| 67 | - // long form |
|
| 68 | - if (0x80 & $byte) { |
|
| 69 | - if (!$length) { |
|
| 70 | - $indefinite = true; |
|
| 71 | - } else { |
|
| 72 | - if ($idx + $length > $datalen) { |
|
| 73 | - throw new DecodeException( |
|
| 74 | - 'Unexpected end of data while decoding long form length.'); |
|
| 75 | - } |
|
| 76 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - if (isset($offset)) { |
|
| 80 | - $offset = $idx; |
|
| 81 | - } |
|
| 82 | - return new self($length, $indefinite); |
|
| 83 | - } |
|
| 42 | + /** |
|
| 43 | + * Decode length component from DER data. |
|
| 44 | + * |
|
| 45 | + * @param string $data DER encoded data |
|
| 46 | + * @param null|int $offset Reference to the variable that contains offset |
|
| 47 | + * into the data where to start parsing. |
|
| 48 | + * Variable is updated to the offset next to the |
|
| 49 | + * parsed length component. If null, start from offset 0. |
|
| 50 | + * |
|
| 51 | + * @throws DecodeException If decoding fails |
|
| 52 | + * |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromDER(string $data, int &$offset = null): self |
|
| 56 | + { |
|
| 57 | + $idx = $offset ?? 0; |
|
| 58 | + $datalen = strlen($data); |
|
| 59 | + if ($idx >= $datalen) { |
|
| 60 | + throw new DecodeException( |
|
| 61 | + 'Unexpected end of data while decoding length.'); |
|
| 62 | + } |
|
| 63 | + $indefinite = false; |
|
| 64 | + $byte = ord($data[$idx++]); |
|
| 65 | + // bits 7 to 1 |
|
| 66 | + $length = (0x7f & $byte); |
|
| 67 | + // long form |
|
| 68 | + if (0x80 & $byte) { |
|
| 69 | + if (!$length) { |
|
| 70 | + $indefinite = true; |
|
| 71 | + } else { |
|
| 72 | + if ($idx + $length > $datalen) { |
|
| 73 | + throw new DecodeException( |
|
| 74 | + 'Unexpected end of data while decoding long form length.'); |
|
| 75 | + } |
|
| 76 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + if (isset($offset)) { |
|
| 80 | + $offset = $idx; |
|
| 81 | + } |
|
| 82 | + return new self($length, $indefinite); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Decode length from DER. |
|
| 87 | - * |
|
| 88 | - * Throws an exception if length doesn't match with expected or if data |
|
| 89 | - * doesn't contain enough bytes. |
|
| 90 | - * |
|
| 91 | - * Requirement of definite length is relaxed contrary to the specification |
|
| 92 | - * (sect. 10.1). |
|
| 93 | - * |
|
| 94 | - * @see self::fromDER |
|
| 95 | - * |
|
| 96 | - * @param string $data DER data |
|
| 97 | - * @param int $offset Reference to the offset variable |
|
| 98 | - * @param null|int $expected Expected length, null to bypass checking |
|
| 99 | - * |
|
| 100 | - * @throws DecodeException If decoding or expectation fails |
|
| 101 | - * |
|
| 102 | - * @return self |
|
| 103 | - */ |
|
| 104 | - public static function expectFromDER(string $data, int &$offset, |
|
| 105 | - int $expected = null): self |
|
| 106 | - { |
|
| 107 | - $idx = $offset; |
|
| 108 | - $length = self::fromDER($data, $idx); |
|
| 109 | - // if certain length was expected |
|
| 110 | - if (isset($expected)) { |
|
| 111 | - if ($length->isIndefinite()) { |
|
| 112 | - throw new DecodeException('Expected length %d, got indefinite.', |
|
| 113 | - $expected); |
|
| 114 | - } |
|
| 115 | - if ($expected !== $length->intLength()) { |
|
| 116 | - throw new DecodeException( |
|
| 117 | - sprintf('Expected length %d, got %d.', $expected, |
|
| 118 | - $length->intLength())); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - // check that enough data is available |
|
| 122 | - if (!$length->isIndefinite() && |
|
| 123 | - strlen($data) < $idx + $length->intLength()) { |
|
| 124 | - throw new DecodeException( |
|
| 125 | - sprintf('Length %d overflows data, %d bytes left.', |
|
| 126 | - $length->intLength(), strlen($data) - $idx)); |
|
| 127 | - } |
|
| 128 | - $offset = $idx; |
|
| 129 | - return $length; |
|
| 130 | - } |
|
| 85 | + /** |
|
| 86 | + * Decode length from DER. |
|
| 87 | + * |
|
| 88 | + * Throws an exception if length doesn't match with expected or if data |
|
| 89 | + * doesn't contain enough bytes. |
|
| 90 | + * |
|
| 91 | + * Requirement of definite length is relaxed contrary to the specification |
|
| 92 | + * (sect. 10.1). |
|
| 93 | + * |
|
| 94 | + * @see self::fromDER |
|
| 95 | + * |
|
| 96 | + * @param string $data DER data |
|
| 97 | + * @param int $offset Reference to the offset variable |
|
| 98 | + * @param null|int $expected Expected length, null to bypass checking |
|
| 99 | + * |
|
| 100 | + * @throws DecodeException If decoding or expectation fails |
|
| 101 | + * |
|
| 102 | + * @return self |
|
| 103 | + */ |
|
| 104 | + public static function expectFromDER(string $data, int &$offset, |
|
| 105 | + int $expected = null): self |
|
| 106 | + { |
|
| 107 | + $idx = $offset; |
|
| 108 | + $length = self::fromDER($data, $idx); |
|
| 109 | + // if certain length was expected |
|
| 110 | + if (isset($expected)) { |
|
| 111 | + if ($length->isIndefinite()) { |
|
| 112 | + throw new DecodeException('Expected length %d, got indefinite.', |
|
| 113 | + $expected); |
|
| 114 | + } |
|
| 115 | + if ($expected !== $length->intLength()) { |
|
| 116 | + throw new DecodeException( |
|
| 117 | + sprintf('Expected length %d, got %d.', $expected, |
|
| 118 | + $length->intLength())); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + // check that enough data is available |
|
| 122 | + if (!$length->isIndefinite() && |
|
| 123 | + strlen($data) < $idx + $length->intLength()) { |
|
| 124 | + throw new DecodeException( |
|
| 125 | + sprintf('Length %d overflows data, %d bytes left.', |
|
| 126 | + $length->intLength(), strlen($data) - $idx)); |
|
| 127 | + } |
|
| 128 | + $offset = $idx; |
|
| 129 | + return $length; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * @see Encodable::toDER() |
|
| 134 | - * |
|
| 135 | - * @throws \DomainException If length is too large to encode |
|
| 136 | - * |
|
| 137 | - * @return string |
|
| 138 | - */ |
|
| 139 | - public function toDER(): string |
|
| 140 | - { |
|
| 141 | - $bytes = []; |
|
| 142 | - if ($this->_indefinite) { |
|
| 143 | - $bytes[] = 0x80; |
|
| 144 | - } else { |
|
| 145 | - $num = $this->_length->gmpObj(); |
|
| 146 | - // long form |
|
| 147 | - if ($num > 127) { |
|
| 148 | - $octets = []; |
|
| 149 | - for (; $num > 0; $num >>= 8) { |
|
| 150 | - $octets[] = gmp_intval(0xff & $num); |
|
| 151 | - } |
|
| 152 | - $count = count($octets); |
|
| 153 | - // first octet must not be 0xff |
|
| 154 | - if ($count >= 127) { |
|
| 155 | - throw new \DomainException('Too many length octets.'); |
|
| 156 | - } |
|
| 157 | - $bytes[] = 0x80 | $count; |
|
| 158 | - foreach (array_reverse($octets) as $octet) { |
|
| 159 | - $bytes[] = $octet; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - // short form |
|
| 163 | - else { |
|
| 164 | - $bytes[] = gmp_intval($num); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - return pack('C*', ...$bytes); |
|
| 168 | - } |
|
| 132 | + /** |
|
| 133 | + * @see Encodable::toDER() |
|
| 134 | + * |
|
| 135 | + * @throws \DomainException If length is too large to encode |
|
| 136 | + * |
|
| 137 | + * @return string |
|
| 138 | + */ |
|
| 139 | + public function toDER(): string |
|
| 140 | + { |
|
| 141 | + $bytes = []; |
|
| 142 | + if ($this->_indefinite) { |
|
| 143 | + $bytes[] = 0x80; |
|
| 144 | + } else { |
|
| 145 | + $num = $this->_length->gmpObj(); |
|
| 146 | + // long form |
|
| 147 | + if ($num > 127) { |
|
| 148 | + $octets = []; |
|
| 149 | + for (; $num > 0; $num >>= 8) { |
|
| 150 | + $octets[] = gmp_intval(0xff & $num); |
|
| 151 | + } |
|
| 152 | + $count = count($octets); |
|
| 153 | + // first octet must not be 0xff |
|
| 154 | + if ($count >= 127) { |
|
| 155 | + throw new \DomainException('Too many length octets.'); |
|
| 156 | + } |
|
| 157 | + $bytes[] = 0x80 | $count; |
|
| 158 | + foreach (array_reverse($octets) as $octet) { |
|
| 159 | + $bytes[] = $octet; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + // short form |
|
| 163 | + else { |
|
| 164 | + $bytes[] = gmp_intval($num); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + return pack('C*', ...$bytes); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Get the length. |
|
| 172 | - * |
|
| 173 | - * @throws \LogicException If length is indefinite |
|
| 174 | - * |
|
| 175 | - * @return string Length as an integer string |
|
| 176 | - */ |
|
| 177 | - public function length(): string |
|
| 178 | - { |
|
| 179 | - if ($this->_indefinite) { |
|
| 180 | - throw new \LogicException('Length is indefinite.'); |
|
| 181 | - } |
|
| 182 | - return $this->_length->base10(); |
|
| 183 | - } |
|
| 170 | + /** |
|
| 171 | + * Get the length. |
|
| 172 | + * |
|
| 173 | + * @throws \LogicException If length is indefinite |
|
| 174 | + * |
|
| 175 | + * @return string Length as an integer string |
|
| 176 | + */ |
|
| 177 | + public function length(): string |
|
| 178 | + { |
|
| 179 | + if ($this->_indefinite) { |
|
| 180 | + throw new \LogicException('Length is indefinite.'); |
|
| 181 | + } |
|
| 182 | + return $this->_length->base10(); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Get the length as an integer. |
|
| 187 | - * |
|
| 188 | - * @throws \LogicException If length is indefinite |
|
| 189 | - * @throws \RuntimeException If length overflows integer size |
|
| 190 | - * |
|
| 191 | - * @return int |
|
| 192 | - */ |
|
| 193 | - public function intLength(): int |
|
| 194 | - { |
|
| 195 | - if ($this->_indefinite) { |
|
| 196 | - throw new \LogicException('Length is indefinite.'); |
|
| 197 | - } |
|
| 198 | - return $this->_length->intVal(); |
|
| 199 | - } |
|
| 185 | + /** |
|
| 186 | + * Get the length as an integer. |
|
| 187 | + * |
|
| 188 | + * @throws \LogicException If length is indefinite |
|
| 189 | + * @throws \RuntimeException If length overflows integer size |
|
| 190 | + * |
|
| 191 | + * @return int |
|
| 192 | + */ |
|
| 193 | + public function intLength(): int |
|
| 194 | + { |
|
| 195 | + if ($this->_indefinite) { |
|
| 196 | + throw new \LogicException('Length is indefinite.'); |
|
| 197 | + } |
|
| 198 | + return $this->_length->intVal(); |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * Whether length is indefinite. |
|
| 203 | - * |
|
| 204 | - * @return bool |
|
| 205 | - */ |
|
| 206 | - public function isIndefinite(): bool |
|
| 207 | - { |
|
| 208 | - return $this->_indefinite; |
|
| 209 | - } |
|
| 201 | + /** |
|
| 202 | + * Whether length is indefinite. |
|
| 203 | + * |
|
| 204 | + * @return bool |
|
| 205 | + */ |
|
| 206 | + public function isIndefinite(): bool |
|
| 207 | + { |
|
| 208 | + return $this->_indefinite; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Decode long form length. |
|
| 213 | - * |
|
| 214 | - * @param int $length Number of octets |
|
| 215 | - * @param string $data Data |
|
| 216 | - * @param int $offset reference to the variable containing offset to the data |
|
| 217 | - * |
|
| 218 | - * @throws DecodeException If decoding fails |
|
| 219 | - * |
|
| 220 | - * @return string Integer as a string |
|
| 221 | - */ |
|
| 222 | - private static function _decodeLongFormLength(int $length, string $data, |
|
| 223 | - int &$offset): string |
|
| 224 | - { |
|
| 225 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
| 226 | - if (127 === $length) { |
|
| 227 | - throw new DecodeException('Invalid number of length octets.'); |
|
| 228 | - } |
|
| 229 | - $num = gmp_init(0, 10); |
|
| 230 | - while (--$length >= 0) { |
|
| 231 | - $byte = ord($data[$offset++]); |
|
| 232 | - $num <<= 8; |
|
| 233 | - $num |= $byte; |
|
| 234 | - } |
|
| 235 | - return gmp_strval($num); |
|
| 236 | - } |
|
| 211 | + /** |
|
| 212 | + * Decode long form length. |
|
| 213 | + * |
|
| 214 | + * @param int $length Number of octets |
|
| 215 | + * @param string $data Data |
|
| 216 | + * @param int $offset reference to the variable containing offset to the data |
|
| 217 | + * |
|
| 218 | + * @throws DecodeException If decoding fails |
|
| 219 | + * |
|
| 220 | + * @return string Integer as a string |
|
| 221 | + */ |
|
| 222 | + private static function _decodeLongFormLength(int $length, string $data, |
|
| 223 | + int &$offset): string |
|
| 224 | + { |
|
| 225 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
| 226 | + if (127 === $length) { |
|
| 227 | + throw new DecodeException('Invalid number of length octets.'); |
|
| 228 | + } |
|
| 229 | + $num = gmp_init(0, 10); |
|
| 230 | + while (--$length >= 0) { |
|
| 231 | + $byte = ord($data[$offset++]); |
|
| 232 | + $num <<= 8; |
|
| 233 | + $num |= $byte; |
|
| 234 | + } |
|
| 235 | + return gmp_strval($num); |
|
| 236 | + } |
|
| 237 | 237 | } |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface Stringable |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Convert object to string. |
|
| 14 | - * |
|
| 15 | - * @return string |
|
| 16 | - */ |
|
| 17 | - public function __toString(): string; |
|
| 12 | + /** |
|
| 13 | + * Convert object to string. |
|
| 14 | + * |
|
| 15 | + * @return string |
|
| 16 | + */ |
|
| 17 | + public function __toString(): string; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Get the string representation of the type. |
|
| 21 | - * |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - public function string(): string; |
|
| 19 | + /** |
|
| 20 | + * Get the string representation of the type. |
|
| 21 | + * |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + public function string(): string; |
|
| 25 | 25 | } |
@@ -13,90 +13,90 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface ElementBase extends Encodable |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Get the class of the ASN.1 type. |
|
| 18 | - * |
|
| 19 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
| 20 | - * |
|
| 21 | - * @return int |
|
| 22 | - */ |
|
| 23 | - public function typeClass(): int; |
|
| 16 | + /** |
|
| 17 | + * Get the class of the ASN.1 type. |
|
| 18 | + * |
|
| 19 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
| 20 | + * |
|
| 21 | + * @return int |
|
| 22 | + */ |
|
| 23 | + public function typeClass(): int; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Check whether the element is constructed. |
|
| 27 | - * |
|
| 28 | - * Otherwise it's primitive. |
|
| 29 | - * |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - public function isConstructed(): bool; |
|
| 25 | + /** |
|
| 26 | + * Check whether the element is constructed. |
|
| 27 | + * |
|
| 28 | + * Otherwise it's primitive. |
|
| 29 | + * |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + public function isConstructed(): bool; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the tag of the element. |
|
| 36 | - * |
|
| 37 | - * Interpretation of the tag depends on the context. For example it may |
|
| 38 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
| 39 | - * tagged type. |
|
| 40 | - * |
|
| 41 | - * @return int |
|
| 42 | - */ |
|
| 43 | - public function tag(): int; |
|
| 34 | + /** |
|
| 35 | + * Get the tag of the element. |
|
| 36 | + * |
|
| 37 | + * Interpretation of the tag depends on the context. For example it may |
|
| 38 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
| 39 | + * tagged type. |
|
| 40 | + * |
|
| 41 | + * @return int |
|
| 42 | + */ |
|
| 43 | + public function tag(): int; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Check whether the element is a type of a given tag. |
|
| 47 | - * |
|
| 48 | - * @param int $tag Type tag |
|
| 49 | - * |
|
| 50 | - * @return bool |
|
| 51 | - */ |
|
| 52 | - public function isType(int $tag): bool; |
|
| 45 | + /** |
|
| 46 | + * Check whether the element is a type of a given tag. |
|
| 47 | + * |
|
| 48 | + * @param int $tag Type tag |
|
| 49 | + * |
|
| 50 | + * @return bool |
|
| 51 | + */ |
|
| 52 | + public function isType(int $tag): bool; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Check whether the element is a type of a given tag. |
|
| 56 | - * |
|
| 57 | - * Throws an exception if expectation fails. |
|
| 58 | - * |
|
| 59 | - * @param int $tag Type tag |
|
| 60 | - * |
|
| 61 | - * @throws \UnexpectedValueException If the element type differs from the expected |
|
| 62 | - * |
|
| 63 | - * @return ElementBase |
|
| 64 | - */ |
|
| 65 | - public function expectType(int $tag): ElementBase; |
|
| 54 | + /** |
|
| 55 | + * Check whether the element is a type of a given tag. |
|
| 56 | + * |
|
| 57 | + * Throws an exception if expectation fails. |
|
| 58 | + * |
|
| 59 | + * @param int $tag Type tag |
|
| 60 | + * |
|
| 61 | + * @throws \UnexpectedValueException If the element type differs from the expected |
|
| 62 | + * |
|
| 63 | + * @return ElementBase |
|
| 64 | + */ |
|
| 65 | + public function expectType(int $tag): ElementBase; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Check whether the element is tagged (context specific). |
|
| 69 | - * |
|
| 70 | - * @return bool |
|
| 71 | - */ |
|
| 72 | - public function isTagged(): bool; |
|
| 67 | + /** |
|
| 68 | + * Check whether the element is tagged (context specific). |
|
| 69 | + * |
|
| 70 | + * @return bool |
|
| 71 | + */ |
|
| 72 | + public function isTagged(): bool; |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Check whether the element is tagged (context specific) and optionally has |
|
| 76 | - * a given tag. |
|
| 77 | - * |
|
| 78 | - * Throws an exception if the element is not tagged or tag differs from |
|
| 79 | - * the expected. |
|
| 80 | - * |
|
| 81 | - * @param null|int $tag Optional type tag |
|
| 82 | - * |
|
| 83 | - * @throws \UnexpectedValueException If expectation fails |
|
| 84 | - * |
|
| 85 | - * @return TaggedType |
|
| 86 | - */ |
|
| 87 | - public function expectTagged(?int $tag = null): TaggedType; |
|
| 74 | + /** |
|
| 75 | + * Check whether the element is tagged (context specific) and optionally has |
|
| 76 | + * a given tag. |
|
| 77 | + * |
|
| 78 | + * Throws an exception if the element is not tagged or tag differs from |
|
| 79 | + * the expected. |
|
| 80 | + * |
|
| 81 | + * @param null|int $tag Optional type tag |
|
| 82 | + * |
|
| 83 | + * @throws \UnexpectedValueException If expectation fails |
|
| 84 | + * |
|
| 85 | + * @return TaggedType |
|
| 86 | + */ |
|
| 87 | + public function expectTagged(?int $tag = null): TaggedType; |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Get the object as an abstract Element instance. |
|
| 91 | - * |
|
| 92 | - * @return Element |
|
| 93 | - */ |
|
| 94 | - public function asElement(): Element; |
|
| 89 | + /** |
|
| 90 | + * Get the object as an abstract Element instance. |
|
| 91 | + * |
|
| 92 | + * @return Element |
|
| 93 | + */ |
|
| 94 | + public function asElement(): Element; |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Get the object as an UnspecifiedType instance. |
|
| 98 | - * |
|
| 99 | - * @return UnspecifiedType |
|
| 100 | - */ |
|
| 101 | - public function asUnspecified(): UnspecifiedType; |
|
| 96 | + /** |
|
| 97 | + * Get the object as an UnspecifiedType instance. |
|
| 98 | + * |
|
| 99 | + * @return UnspecifiedType |
|
| 100 | + */ |
|
| 101 | + public function asUnspecified(): UnspecifiedType; |
|
| 102 | 102 | } |