| @@ -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 | } | 
| @@ -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 | } | 
| @@ -12,16 +12,16 @@ | ||
| 12 | 12 | */ | 
| 13 | 13 | class OctetString extends PrimitiveString | 
| 14 | 14 |  { | 
| 15 | - use UniversalClass; | |
| 15 | + use UniversalClass; | |
| 16 | 16 | |
| 17 | - /** | |
| 18 | - * Constructor. | |
| 19 | - * | |
| 20 | - * @param string $string | |
| 21 | - */ | |
| 22 | - public function __construct(string $string) | |
| 23 | -    { | |
| 24 | - $this->_typeTag = self::TYPE_OCTET_STRING; | |
| 25 | - parent::__construct($string); | |
| 26 | - } | |
| 17 | + /** | |
| 18 | + * Constructor. | |
| 19 | + * | |
| 20 | + * @param string $string | |
| 21 | + */ | |
| 22 | + public function __construct(string $string) | |
| 23 | +	{ | |
| 24 | + $this->_typeTag = self::TYPE_OCTET_STRING; | |
| 25 | + parent::__construct($string); | |
| 26 | + } | |
| 27 | 27 | } | 
| @@ -9,16 +9,16 @@ | ||
| 9 | 9 | |
| 10 | 10 | class CharacterString extends PrimitiveString | 
| 11 | 11 |  { | 
| 12 | - use UniversalClass; | |
| 12 | + use UniversalClass; | |
| 13 | 13 | |
| 14 | - /** | |
| 15 | - * Constructor. | |
| 16 | - * | |
| 17 | - * @param string $string | |
| 18 | - */ | |
| 19 | - public function __construct(string $string) | |
| 20 | -    { | |
| 21 | - $this->_typeTag = self::TYPE_CHARACTER_STRING; | |
| 22 | - parent::__construct($string); | |
| 23 | - } | |
| 14 | + /** | |
| 15 | + * Constructor. | |
| 16 | + * | |
| 17 | + * @param string $string | |
| 18 | + */ | |
| 19 | + public function __construct(string $string) | |
| 20 | +	{ | |
| 21 | + $this->_typeTag = self::TYPE_CHARACTER_STRING; | |
| 22 | + parent::__construct($string); | |
| 23 | + } | |
| 24 | 24 | } | 
| @@ -12,26 +12,26 @@ | ||
| 12 | 12 | */ | 
| 13 | 13 | class ObjectDescriptor extends PrimitiveString | 
| 14 | 14 |  { | 
| 15 | - use UniversalClass; | |
| 15 | + use UniversalClass; | |
| 16 | 16 | |
| 17 | - /** | |
| 18 | - * Constructor. | |
| 19 | - * | |
| 20 | - * @param string $descriptor | |
| 21 | - */ | |
| 22 | - public function __construct(string $descriptor) | |
| 23 | -    { | |
| 24 | - $this->_string = $descriptor; | |
| 25 | - $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; | |
| 26 | - } | |
| 17 | + /** | |
| 18 | + * Constructor. | |
| 19 | + * | |
| 20 | + * @param string $descriptor | |
| 21 | + */ | |
| 22 | + public function __construct(string $descriptor) | |
| 23 | +	{ | |
| 24 | + $this->_string = $descriptor; | |
| 25 | + $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; | |
| 26 | + } | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | - * Get the object descriptor. | |
| 30 | - * | |
| 31 | - * @return string | |
| 32 | - */ | |
| 33 | - public function descriptor(): string | |
| 34 | -    { | |
| 35 | - return $this->_string; | |
| 36 | - } | |
| 28 | + /** | |
| 29 | + * Get the object descriptor. | |
| 30 | + * | |
| 31 | + * @return string | |
| 32 | + */ | |
| 33 | + public function descriptor(): string | |
| 34 | +	{ | |
| 35 | + return $this->_string; | |
| 36 | + } | |
| 37 | 37 | } | 
| @@ -71,11 +71,11 @@ | ||
| 71 | 71 | $num = $this->_number->gmpObj(); | 
| 72 | 72 |          switch (gmp_sign($num)) { | 
| 73 | 73 | // positive | 
| 74 | - case 1: | |
| 75 | - return self::_encodePositiveInteger($num); | |
| 76 | - // negative | |
| 77 | - case -1: | |
| 78 | - return self::_encodeNegativeInteger($num); | |
| 74 | + case 1: | |
| 75 | + return self::_encodePositiveInteger($num); | |
| 76 | + // negative | |
| 77 | + case -1: | |
| 78 | + return self::_encodeNegativeInteger($num); | |
| 79 | 79 | } | 
| 80 | 80 | // zero | 
| 81 | 81 | return "\0"; | 
| @@ -17,161 +17,161 @@ | ||
| 17 | 17 | */ | 
| 18 | 18 | class Integer extends Element | 
| 19 | 19 |  { | 
| 20 | - use UniversalClass; | |
| 21 | - use PrimitiveType; | |
| 20 | + use UniversalClass; | |
| 21 | + use PrimitiveType; | |
| 22 | 22 | |
| 23 | - /** | |
| 24 | - * The number. | |
| 25 | - * | |
| 26 | - * @var BigInt | |
| 27 | - */ | |
| 28 | - private $_number; | |
| 23 | + /** | |
| 24 | + * The number. | |
| 25 | + * | |
| 26 | + * @var BigInt | |
| 27 | + */ | |
| 28 | + private $_number; | |
| 29 | 29 | |
| 30 | - /** | |
| 31 | - * Constructor. | |
| 32 | - * | |
| 33 | - * @param int|string $number Base 10 integer | |
| 34 | - */ | |
| 35 | - public function __construct($number) | |
| 36 | -    { | |
| 37 | - $this->_typeTag = self::TYPE_INTEGER; | |
| 38 | -        if (!self::_validateNumber($number)) { | |
| 39 | - $var = is_scalar($number) ? strval($number) : gettype($number); | |
| 40 | -            throw new \InvalidArgumentException("'{$var}' is not a valid number."); | |
| 41 | - } | |
| 42 | - $this->_number = new BigInt($number); | |
| 43 | - } | |
| 30 | + /** | |
| 31 | + * Constructor. | |
| 32 | + * | |
| 33 | + * @param int|string $number Base 10 integer | |
| 34 | + */ | |
| 35 | + public function __construct($number) | |
| 36 | +	{ | |
| 37 | + $this->_typeTag = self::TYPE_INTEGER; | |
| 38 | +		if (!self::_validateNumber($number)) { | |
| 39 | + $var = is_scalar($number) ? strval($number) : gettype($number); | |
| 40 | +			throw new \InvalidArgumentException("'{$var}' is not a valid number."); | |
| 41 | + } | |
| 42 | + $this->_number = new BigInt($number); | |
| 43 | + } | |
| 44 | 44 | |
| 45 | - /** | |
| 46 | - * Get the number as a base 10. | |
| 47 | - * | |
| 48 | - * @return string Integer as a string | |
| 49 | - */ | |
| 50 | - public function number(): string | |
| 51 | -    { | |
| 52 | - return $this->_number->base10(); | |
| 53 | - } | |
| 45 | + /** | |
| 46 | + * Get the number as a base 10. | |
| 47 | + * | |
| 48 | + * @return string Integer as a string | |
| 49 | + */ | |
| 50 | + public function number(): string | |
| 51 | +	{ | |
| 52 | + return $this->_number->base10(); | |
| 53 | + } | |
| 54 | 54 | |
| 55 | - /** | |
| 56 | - * Get the number as an integer type. | |
| 57 | - * | |
| 58 | - * @return int | |
| 59 | - */ | |
| 60 | - public function intNumber(): int | |
| 61 | -    { | |
| 62 | - return $this->_number->intVal(); | |
| 63 | - } | |
| 55 | + /** | |
| 56 | + * Get the number as an integer type. | |
| 57 | + * | |
| 58 | + * @return int | |
| 59 | + */ | |
| 60 | + public function intNumber(): int | |
| 61 | +	{ | |
| 62 | + return $this->_number->intVal(); | |
| 63 | + } | |
| 64 | 64 | |
| 65 | - /** | |
| 66 | -     * {@inheritdoc} | |
| 67 | - */ | |
| 68 | - protected function _encodedContentDER(): string | |
| 69 | -    { | |
| 70 | - $num = $this->_number->gmpObj(); | |
| 71 | -        switch (gmp_sign($num)) { | |
| 72 | - // positive | |
| 73 | - case 1: | |
| 74 | - return self::_encodePositiveInteger($num); | |
| 75 | - // negative | |
| 76 | - case -1: | |
| 77 | - return self::_encodeNegativeInteger($num); | |
| 78 | - } | |
| 79 | - // zero | |
| 80 | - return "\0"; | |
| 81 | - } | |
| 65 | + /** | |
| 66 | +	 * {@inheritdoc} | |
| 67 | + */ | |
| 68 | + protected function _encodedContentDER(): string | |
| 69 | +	{ | |
| 70 | + $num = $this->_number->gmpObj(); | |
| 71 | +		switch (gmp_sign($num)) { | |
| 72 | + // positive | |
| 73 | + case 1: | |
| 74 | + return self::_encodePositiveInteger($num); | |
| 75 | + // negative | |
| 76 | + case -1: | |
| 77 | + return self::_encodeNegativeInteger($num); | |
| 78 | + } | |
| 79 | + // zero | |
| 80 | + return "\0"; | |
| 81 | + } | |
| 82 | 82 | |
| 83 | - /** | |
| 84 | -     * {@inheritdoc} | |
| 85 | - */ | |
| 86 | - protected static function _decodeFromDER(Identifier $identifier, | |
| 87 | - string $data, int &$offset): ElementBase | |
| 88 | -    { | |
| 89 | - $idx = $offset; | |
| 90 | - $length = Length::expectFromDER($data, $idx)->intLength(); | |
| 91 | - $bytes = substr($data, $idx, $length); | |
| 92 | - $idx += $length; | |
| 93 | - $neg = ord($bytes[0]) & 0x80; | |
| 94 | - // negative, apply inversion of two's complement | |
| 95 | -        if ($neg) { | |
| 96 | - $len = strlen($bytes); | |
| 97 | -            for ($i = 0; $i < $len; ++$i) { | |
| 98 | - $bytes[$i] = ~$bytes[$i]; | |
| 99 | - } | |
| 100 | - } | |
| 101 | - $num = gmp_init(bin2hex($bytes), 16); | |
| 102 | - // negative, apply addition of two's complement | |
| 103 | - // and produce negative result | |
| 104 | -        if ($neg) { | |
| 105 | - $num = gmp_neg($num + 1); | |
| 106 | - } | |
| 107 | - $offset = $idx; | |
| 108 | - // late static binding since enumerated extends integer type | |
| 109 | - return new static(gmp_strval($num, 10)); | |
| 110 | - } | |
| 83 | + /** | |
| 84 | +	 * {@inheritdoc} | |
| 85 | + */ | |
| 86 | + protected static function _decodeFromDER(Identifier $identifier, | |
| 87 | + string $data, int &$offset): ElementBase | |
| 88 | +	{ | |
| 89 | + $idx = $offset; | |
| 90 | + $length = Length::expectFromDER($data, $idx)->intLength(); | |
| 91 | + $bytes = substr($data, $idx, $length); | |
| 92 | + $idx += $length; | |
| 93 | + $neg = ord($bytes[0]) & 0x80; | |
| 94 | + // negative, apply inversion of two's complement | |
| 95 | +		if ($neg) { | |
| 96 | + $len = strlen($bytes); | |
| 97 | +			for ($i = 0; $i < $len; ++$i) { | |
| 98 | + $bytes[$i] = ~$bytes[$i]; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + $num = gmp_init(bin2hex($bytes), 16); | |
| 102 | + // negative, apply addition of two's complement | |
| 103 | + // and produce negative result | |
| 104 | +		if ($neg) { | |
| 105 | + $num = gmp_neg($num + 1); | |
| 106 | + } | |
| 107 | + $offset = $idx; | |
| 108 | + // late static binding since enumerated extends integer type | |
| 109 | + return new static(gmp_strval($num, 10)); | |
| 110 | + } | |
| 111 | 111 | |
| 112 | - /** | |
| 113 | - * Encode positive integer to DER content. | |
| 114 | - * | |
| 115 | - * @param \GMP $num | |
| 116 | - * | |
| 117 | - * @return string | |
| 118 | - */ | |
| 119 | - private static function _encodePositiveInteger(\GMP $num): string | |
| 120 | -    { | |
| 121 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 122 | - // if first bit is 1, prepend full zero byte | |
| 123 | - // to represent positive two's complement | |
| 124 | -        if (ord($bin[0]) & 0x80) { | |
| 125 | - $bin = chr(0x00) . $bin; | |
| 126 | - } | |
| 127 | - return $bin; | |
| 128 | - } | |
| 112 | + /** | |
| 113 | + * Encode positive integer to DER content. | |
| 114 | + * | |
| 115 | + * @param \GMP $num | |
| 116 | + * | |
| 117 | + * @return string | |
| 118 | + */ | |
| 119 | + private static function _encodePositiveInteger(\GMP $num): string | |
| 120 | +	{ | |
| 121 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 122 | + // if first bit is 1, prepend full zero byte | |
| 123 | + // to represent positive two's complement | |
| 124 | +		if (ord($bin[0]) & 0x80) { | |
| 125 | + $bin = chr(0x00) . $bin; | |
| 126 | + } | |
| 127 | + return $bin; | |
| 128 | + } | |
| 129 | 129 | |
| 130 | - /** | |
| 131 | - * Encode negative integer to DER content. | |
| 132 | - * | |
| 133 | - * @param \GMP $num | |
| 134 | - * | |
| 135 | - * @return string | |
| 136 | - */ | |
| 137 | - private static function _encodeNegativeInteger(\GMP $num): string | |
| 138 | -    { | |
| 139 | - $num = gmp_abs($num); | |
| 140 | - // compute number of bytes required | |
| 141 | - $width = 1; | |
| 142 | -        if ($num > 128) { | |
| 143 | - $tmp = $num; | |
| 144 | -            do { | |
| 145 | - ++$width; | |
| 146 | - $tmp >>= 8; | |
| 147 | - } while ($tmp > 128); | |
| 148 | - } | |
| 149 | - // compute two's complement 2^n - x | |
| 150 | -        $num = gmp_pow('2', 8 * $width) - $num; | |
| 151 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 152 | - // if first bit is 0, prepend full inverted byte | |
| 153 | - // to represent negative two's complement | |
| 154 | -        if (!(ord($bin[0]) & 0x80)) { | |
| 155 | - $bin = chr(0xff) . $bin; | |
| 156 | - } | |
| 157 | - return $bin; | |
| 158 | - } | |
| 130 | + /** | |
| 131 | + * Encode negative integer to DER content. | |
| 132 | + * | |
| 133 | + * @param \GMP $num | |
| 134 | + * | |
| 135 | + * @return string | |
| 136 | + */ | |
| 137 | + private static function _encodeNegativeInteger(\GMP $num): string | |
| 138 | +	{ | |
| 139 | + $num = gmp_abs($num); | |
| 140 | + // compute number of bytes required | |
| 141 | + $width = 1; | |
| 142 | +		if ($num > 128) { | |
| 143 | + $tmp = $num; | |
| 144 | +			do { | |
| 145 | + ++$width; | |
| 146 | + $tmp >>= 8; | |
| 147 | + } while ($tmp > 128); | |
| 148 | + } | |
| 149 | + // compute two's complement 2^n - x | |
| 150 | +		$num = gmp_pow('2', 8 * $width) - $num; | |
| 151 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 152 | + // if first bit is 0, prepend full inverted byte | |
| 153 | + // to represent negative two's complement | |
| 154 | +		if (!(ord($bin[0]) & 0x80)) { | |
| 155 | + $bin = chr(0xff) . $bin; | |
| 156 | + } | |
| 157 | + return $bin; | |
| 158 | + } | |
| 159 | 159 | |
| 160 | - /** | |
| 161 | - * Test that number is valid for this context. | |
| 162 | - * | |
| 163 | - * @param mixed $num | |
| 164 | - * | |
| 165 | - * @return bool | |
| 166 | - */ | |
| 167 | - private static function _validateNumber($num): bool | |
| 168 | -    { | |
| 169 | -        if (is_int($num)) { | |
| 170 | - return true; | |
| 171 | - } | |
| 172 | -        if (is_string($num) && preg_match('/-?\d+/', $num)) { | |
| 173 | - return true; | |
| 174 | - } | |
| 175 | - return false; | |
| 176 | - } | |
| 160 | + /** | |
| 161 | + * Test that number is valid for this context. | |
| 162 | + * | |
| 163 | + * @param mixed $num | |
| 164 | + * | |
| 165 | + * @return bool | |
| 166 | + */ | |
| 167 | + private static function _validateNumber($num): bool | |
| 168 | +	{ | |
| 169 | +		if (is_int($num)) { | |
| 170 | + return true; | |
| 171 | + } | |
| 172 | +		if (is_string($num) && preg_match('/-?\d+/', $num)) { | |
| 173 | + return true; | |
| 174 | + } | |
| 175 | + return false; | |
| 176 | + } | |
| 177 | 177 | } | 
| @@ -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 | } | 
| @@ -11,13 +11,13 @@ | ||
| 11 | 11 | */ | 
| 12 | 12 | trait UniversalClass | 
| 13 | 13 |  { | 
| 14 | - /** | |
| 15 | - * @see \Sop\ASN1\Feature\ElementBase::typeClass() | |
| 16 | - * | |
| 17 | - * @return int | |
| 18 | - */ | |
| 19 | - public function typeClass(): int | |
| 20 | -    { | |
| 21 | - return Identifier::CLASS_UNIVERSAL; | |
| 22 | - } | |
| 14 | + /** | |
| 15 | + * @see \Sop\ASN1\Feature\ElementBase::typeClass() | |
| 16 | + * | |
| 17 | + * @return int | |
| 18 | + */ | |
| 19 | + public function typeClass(): int | |
| 20 | +	{ | |
| 21 | + return Identifier::CLASS_UNIVERSAL; | |
| 22 | + } | |
| 23 | 23 | } | 
| @@ -14,36 +14,36 @@ | ||
| 14 | 14 | */ | 
| 15 | 15 | abstract class PrimitiveString extends StringType | 
| 16 | 16 |  { | 
| 17 | - use PrimitiveType; | |
| 17 | + use PrimitiveType; | |
| 18 | 18 | |
| 19 | - /** | |
| 20 | -     * {@inheritdoc} | |
| 21 | - */ | |
| 22 | - protected function _encodedContentDER(): string | |
| 23 | -    { | |
| 24 | - return $this->_string; | |
| 25 | - } | |
| 19 | + /** | |
| 20 | +	 * {@inheritdoc} | |
| 21 | + */ | |
| 22 | + protected function _encodedContentDER(): string | |
| 23 | +	{ | |
| 24 | + return $this->_string; | |
| 25 | + } | |
| 26 | 26 | |
| 27 | - /** | |
| 28 | -     * {@inheritdoc} | |
| 29 | - */ | |
| 30 | - protected static function _decodeFromDER(Identifier $identifier, | |
| 31 | - string $data, int &$offset): ElementBase | |
| 32 | -    { | |
| 33 | - $idx = $offset; | |
| 34 | -        if (!$identifier->isPrimitive()) { | |
| 35 | -            throw new DecodeException('DER encoded string must be primitive.'); | |
| 36 | - } | |
| 37 | - $length = Length::expectFromDER($data, $idx)->intLength(); | |
| 38 | - $str = $length ? substr($data, $idx, $length) : ''; | |
| 39 | - // substr should never return false, since length is | |
| 40 | - // checked by Length::expectFromDER. | |
| 41 | -        assert(is_string($str), new DecodeException('substr')); | |
| 42 | - $offset = $idx + $length; | |
| 43 | -        try { | |
| 44 | - return new static($str); | |
| 45 | -        } catch (\InvalidArgumentException $e) { | |
| 46 | - throw new DecodeException($e->getMessage(), 0, $e); | |
| 47 | - } | |
| 48 | - } | |
| 27 | + /** | |
| 28 | +	 * {@inheritdoc} | |
| 29 | + */ | |
| 30 | + protected static function _decodeFromDER(Identifier $identifier, | |
| 31 | + string $data, int &$offset): ElementBase | |
| 32 | +	{ | |
| 33 | + $idx = $offset; | |
| 34 | +		if (!$identifier->isPrimitive()) { | |
| 35 | +			throw new DecodeException('DER encoded string must be primitive.'); | |
| 36 | + } | |
| 37 | + $length = Length::expectFromDER($data, $idx)->intLength(); | |
| 38 | + $str = $length ? substr($data, $idx, $length) : ''; | |
| 39 | + // substr should never return false, since length is | |
| 40 | + // checked by Length::expectFromDER. | |
| 41 | +		assert(is_string($str), new DecodeException('substr')); | |
| 42 | + $offset = $idx + $length; | |
| 43 | +		try { | |
| 44 | + return new static($str); | |
| 45 | +		} catch (\InvalidArgumentException $e) { | |
| 46 | + throw new DecodeException($e->getMessage(), 0, $e); | |
| 47 | + } | |
| 48 | + } | |
| 49 | 49 | } |