| @@ -16,193 +16,193 @@ | ||
| 16 | 16 | */ | 
| 17 | 17 | class ObjectIdentifier extends Element | 
| 18 | 18 |  { | 
| 19 | - use UniversalClass; | |
| 20 | - use PrimitiveType; | |
| 19 | + use UniversalClass; | |
| 20 | + use PrimitiveType; | |
| 21 | 21 | |
| 22 | - /** | |
| 23 | - * Object identifier in dotted format. | |
| 24 | - * | |
| 25 | - * @var string | |
| 26 | - */ | |
| 27 | - protected $_oid; | |
| 22 | + /** | |
| 23 | + * Object identifier in dotted format. | |
| 24 | + * | |
| 25 | + * @var string | |
| 26 | + */ | |
| 27 | + protected $_oid; | |
| 28 | 28 | |
| 29 | - /** | |
| 30 | - * Object identifier split to sub ID's. | |
| 31 | - * | |
| 32 | - * @var \GMP[] | |
| 33 | - */ | |
| 34 | - protected $_subids; | |
| 29 | + /** | |
| 30 | + * Object identifier split to sub ID's. | |
| 31 | + * | |
| 32 | + * @var \GMP[] | |
| 33 | + */ | |
| 34 | + protected $_subids; | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * Constructor. | |
| 38 | - * | |
| 39 | - * @param string $oid OID in dotted format | |
| 40 | - */ | |
| 41 | - public function __construct(string $oid) | |
| 42 | -    { | |
| 43 | - $this->_oid = $oid; | |
| 44 | - $this->_subids = self::_explodeDottedOID($oid); | |
| 45 | -        if (count($this->_subids) > 0) { | |
| 46 | -            if (count($this->_subids) < 2) { | |
| 47 | - throw new \UnexpectedValueException( | |
| 48 | - "OID must have at least two arcs."); | |
| 49 | - } | |
| 50 | -            if ($this->_subids[0] > 2) { | |
| 51 | - throw new \UnexpectedValueException( | |
| 52 | - "First arc must be less or equal to 2."); | |
| 53 | - } | |
| 54 | -            if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) { | |
| 55 | - throw new \UnexpectedValueException( | |
| 56 | - "Second arc must be less than 40 for root arcs 0 and 1."); | |
| 57 | - } | |
| 58 | - } | |
| 59 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; | |
| 60 | - } | |
| 36 | + /** | |
| 37 | + * Constructor. | |
| 38 | + * | |
| 39 | + * @param string $oid OID in dotted format | |
| 40 | + */ | |
| 41 | + public function __construct(string $oid) | |
| 42 | +	{ | |
| 43 | + $this->_oid = $oid; | |
| 44 | + $this->_subids = self::_explodeDottedOID($oid); | |
| 45 | +		if (count($this->_subids) > 0) { | |
| 46 | +			if (count($this->_subids) < 2) { | |
| 47 | + throw new \UnexpectedValueException( | |
| 48 | + "OID must have at least two arcs."); | |
| 49 | + } | |
| 50 | +			if ($this->_subids[0] > 2) { | |
| 51 | + throw new \UnexpectedValueException( | |
| 52 | + "First arc must be less or equal to 2."); | |
| 53 | + } | |
| 54 | +			if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) { | |
| 55 | + throw new \UnexpectedValueException( | |
| 56 | + "Second arc must be less than 40 for root arcs 0 and 1."); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; | |
| 60 | + } | |
| 61 | 61 | |
| 62 | - /** | |
| 63 | - * Get OID in dotted format. | |
| 64 | - * | |
| 65 | - * @return string | |
| 66 | - */ | |
| 67 | - public function oid(): string | |
| 68 | -    { | |
| 69 | - return $this->_oid; | |
| 70 | - } | |
| 62 | + /** | |
| 63 | + * Get OID in dotted format. | |
| 64 | + * | |
| 65 | + * @return string | |
| 66 | + */ | |
| 67 | + public function oid(): string | |
| 68 | +	{ | |
| 69 | + return $this->_oid; | |
| 70 | + } | |
| 71 | 71 | |
| 72 | - /** | |
| 73 | - * | |
| 74 | -     * {@inheritdoc} | |
| 75 | - */ | |
| 76 | - protected function _encodedContentDER(): string | |
| 77 | -    { | |
| 78 | - $subids = $this->_subids; | |
| 79 | - // encode first two subids to one according to spec section 8.19.4 | |
| 80 | -        if (count($subids) >= 2) { | |
| 81 | - $num = ($subids[0] * 40) + $subids[1]; | |
| 82 | - array_splice($subids, 0, 2, array($num)); | |
| 83 | - } | |
| 84 | - return self::_encodeSubIDs(...$subids); | |
| 85 | - } | |
| 72 | + /** | |
| 73 | + * | |
| 74 | +	 * {@inheritdoc} | |
| 75 | + */ | |
| 76 | + protected function _encodedContentDER(): string | |
| 77 | +	{ | |
| 78 | + $subids = $this->_subids; | |
| 79 | + // encode first two subids to one according to spec section 8.19.4 | |
| 80 | +		if (count($subids) >= 2) { | |
| 81 | + $num = ($subids[0] * 40) + $subids[1]; | |
| 82 | + array_splice($subids, 0, 2, array($num)); | |
| 83 | + } | |
| 84 | + return self::_encodeSubIDs(...$subids); | |
| 85 | + } | |
| 86 | 86 | |
| 87 | - /** | |
| 88 | - * | |
| 89 | -     * {@inheritdoc} | |
| 90 | - * @return self | |
| 91 | - */ | |
| 92 | - protected static function _decodeFromDER(Identifier $identifier, | |
| 93 | - string $data, int &$offset): ElementBase | |
| 94 | -    { | |
| 95 | - $idx = $offset; | |
| 96 | - $len = Length::expectFromDER($data, $idx)->intLength(); | |
| 97 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); | |
| 98 | - $idx += $len; | |
| 99 | - // decode first subidentifier according to spec section 8.19.4 | |
| 100 | -        if (isset($subids[0])) { | |
| 101 | -            if ($subids[0] < 80) { | |
| 102 | - list($x, $y) = gmp_div_qr($subids[0], "40"); | |
| 103 | -            } else { | |
| 104 | - $x = gmp_init(2, 10); | |
| 105 | - $y = $subids[0] - 80; | |
| 106 | - } | |
| 107 | - array_splice($subids, 0, 1, array($x, $y)); | |
| 108 | - } | |
| 109 | - $offset = $idx; | |
| 110 | - return new self(self::_implodeSubIDs(...$subids)); | |
| 111 | - } | |
| 87 | + /** | |
| 88 | + * | |
| 89 | +	 * {@inheritdoc} | |
| 90 | + * @return self | |
| 91 | + */ | |
| 92 | + protected static function _decodeFromDER(Identifier $identifier, | |
| 93 | + string $data, int &$offset): ElementBase | |
| 94 | +	{ | |
| 95 | + $idx = $offset; | |
| 96 | + $len = Length::expectFromDER($data, $idx)->intLength(); | |
| 97 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); | |
| 98 | + $idx += $len; | |
| 99 | + // decode first subidentifier according to spec section 8.19.4 | |
| 100 | +		if (isset($subids[0])) { | |
| 101 | +			if ($subids[0] < 80) { | |
| 102 | + list($x, $y) = gmp_div_qr($subids[0], "40"); | |
| 103 | +			} else { | |
| 104 | + $x = gmp_init(2, 10); | |
| 105 | + $y = $subids[0] - 80; | |
| 106 | + } | |
| 107 | + array_splice($subids, 0, 1, array($x, $y)); | |
| 108 | + } | |
| 109 | + $offset = $idx; | |
| 110 | + return new self(self::_implodeSubIDs(...$subids)); | |
| 111 | + } | |
| 112 | 112 | |
| 113 | - /** | |
| 114 | - * Explode dotted OID to an array of sub ID's. | |
| 115 | - * | |
| 116 | - * @param string $oid OID in dotted format | |
| 117 | - * @return \GMP[] Array of GMP numbers | |
| 118 | - */ | |
| 119 | - protected static function _explodeDottedOID(string $oid): array | |
| 120 | -    { | |
| 121 | - $subids = []; | |
| 122 | -        if (strlen($oid)) { | |
| 123 | -            foreach (explode(".", $oid) as $subid) { | |
| 124 | - $n = @gmp_init($subid, 10); | |
| 125 | -                if (false === $n) { | |
| 126 | - throw new \UnexpectedValueException( | |
| 127 | - "'$subid' is not a number."); | |
| 128 | - } | |
| 129 | - $subids[] = $n; | |
| 130 | - } | |
| 131 | - } | |
| 132 | - return $subids; | |
| 133 | - } | |
| 113 | + /** | |
| 114 | + * Explode dotted OID to an array of sub ID's. | |
| 115 | + * | |
| 116 | + * @param string $oid OID in dotted format | |
| 117 | + * @return \GMP[] Array of GMP numbers | |
| 118 | + */ | |
| 119 | + protected static function _explodeDottedOID(string $oid): array | |
| 120 | +	{ | |
| 121 | + $subids = []; | |
| 122 | +		if (strlen($oid)) { | |
| 123 | +			foreach (explode(".", $oid) as $subid) { | |
| 124 | + $n = @gmp_init($subid, 10); | |
| 125 | +				if (false === $n) { | |
| 126 | + throw new \UnexpectedValueException( | |
| 127 | + "'$subid' is not a number."); | |
| 128 | + } | |
| 129 | + $subids[] = $n; | |
| 130 | + } | |
| 131 | + } | |
| 132 | + return $subids; | |
| 133 | + } | |
| 134 | 134 | |
| 135 | - /** | |
| 136 | - * Implode an array of sub IDs to dotted OID format. | |
| 137 | - * | |
| 138 | - * @param \GMP ...$subids | |
| 139 | - * @return string | |
| 140 | - */ | |
| 141 | - protected static function _implodeSubIDs(\GMP ...$subids): string | |
| 142 | -    { | |
| 143 | -        return implode(".", | |
| 144 | -            array_map(function ($num) { | |
| 145 | - return gmp_strval($num, 10); | |
| 146 | - }, $subids)); | |
| 147 | - } | |
| 135 | + /** | |
| 136 | + * Implode an array of sub IDs to dotted OID format. | |
| 137 | + * | |
| 138 | + * @param \GMP ...$subids | |
| 139 | + * @return string | |
| 140 | + */ | |
| 141 | + protected static function _implodeSubIDs(\GMP ...$subids): string | |
| 142 | +	{ | |
| 143 | +		return implode(".", | |
| 144 | +			array_map(function ($num) { | |
| 145 | + return gmp_strval($num, 10); | |
| 146 | + }, $subids)); | |
| 147 | + } | |
| 148 | 148 | |
| 149 | - /** | |
| 150 | - * Encode sub ID's to DER. | |
| 151 | - * | |
| 152 | - * @param \GMP ...$subids | |
| 153 | - * @return string | |
| 154 | - */ | |
| 155 | - protected static function _encodeSubIDs(\GMP ...$subids): string | |
| 156 | -    { | |
| 157 | - $data = ""; | |
| 158 | -        foreach ($subids as $subid) { | |
| 159 | - // if number fits to one base 128 byte | |
| 160 | -            if ($subid < 128) { | |
| 161 | - $data .= chr(intval($subid)); | |
| 162 | -            } else { // encode to multiple bytes | |
| 163 | - $bytes = []; | |
| 164 | -                do { | |
| 165 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); | |
| 166 | - $subid >>= 7; | |
| 167 | - } while ($subid > 0); | |
| 168 | - // all bytes except last must have bit 8 set to one | |
| 169 | -                foreach (array_splice($bytes, 0, -1) as $byte) { | |
| 170 | - $data .= chr(0x80 | $byte); | |
| 171 | - } | |
| 172 | - $data .= chr(reset($bytes)); | |
| 173 | - } | |
| 174 | - } | |
| 175 | - return $data; | |
| 176 | - } | |
| 149 | + /** | |
| 150 | + * Encode sub ID's to DER. | |
| 151 | + * | |
| 152 | + * @param \GMP ...$subids | |
| 153 | + * @return string | |
| 154 | + */ | |
| 155 | + protected static function _encodeSubIDs(\GMP ...$subids): string | |
| 156 | +	{ | |
| 157 | + $data = ""; | |
| 158 | +		foreach ($subids as $subid) { | |
| 159 | + // if number fits to one base 128 byte | |
| 160 | +			if ($subid < 128) { | |
| 161 | + $data .= chr(intval($subid)); | |
| 162 | +			} else { // encode to multiple bytes | |
| 163 | + $bytes = []; | |
| 164 | +				do { | |
| 165 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); | |
| 166 | + $subid >>= 7; | |
| 167 | + } while ($subid > 0); | |
| 168 | + // all bytes except last must have bit 8 set to one | |
| 169 | +				foreach (array_splice($bytes, 0, -1) as $byte) { | |
| 170 | + $data .= chr(0x80 | $byte); | |
| 171 | + } | |
| 172 | + $data .= chr(reset($bytes)); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + return $data; | |
| 176 | + } | |
| 177 | 177 | |
| 178 | - /** | |
| 179 | - * Decode sub ID's from DER data. | |
| 180 | - * | |
| 181 | - * @param string $data | |
| 182 | - * @throws DecodeException | |
| 183 | - * @return \GMP[] Array of GMP numbers | |
| 184 | - */ | |
| 185 | - protected static function _decodeSubIDs(string $data): array | |
| 186 | -    { | |
| 187 | - $subids = []; | |
| 188 | - $idx = 0; | |
| 189 | - $end = strlen($data); | |
| 190 | -        while ($idx < $end) { | |
| 191 | -            $num = gmp_init("0", 10); | |
| 192 | -            while (true) { | |
| 193 | -                if ($idx >= $end) { | |
| 194 | -                    throw new DecodeException("Unexpected end of data."); | |
| 195 | - } | |
| 196 | - $byte = ord($data[$idx++]); | |
| 197 | - $num |= $byte & 0x7f; | |
| 198 | - // bit 8 of the last octet is zero | |
| 199 | -                if (!($byte & 0x80)) { | |
| 200 | - break; | |
| 201 | - } | |
| 202 | - $num <<= 7; | |
| 203 | - } | |
| 204 | - $subids[] = $num; | |
| 205 | - } | |
| 206 | - return $subids; | |
| 207 | - } | |
| 178 | + /** | |
| 179 | + * Decode sub ID's from DER data. | |
| 180 | + * | |
| 181 | + * @param string $data | |
| 182 | + * @throws DecodeException | |
| 183 | + * @return \GMP[] Array of GMP numbers | |
| 184 | + */ | |
| 185 | + protected static function _decodeSubIDs(string $data): array | |
| 186 | +	{ | |
| 187 | + $subids = []; | |
| 188 | + $idx = 0; | |
| 189 | + $end = strlen($data); | |
| 190 | +		while ($idx < $end) { | |
| 191 | +			$num = gmp_init("0", 10); | |
| 192 | +			while (true) { | |
| 193 | +				if ($idx >= $end) { | |
| 194 | +					throw new DecodeException("Unexpected end of data."); | |
| 195 | + } | |
| 196 | + $byte = ord($data[$idx++]); | |
| 197 | + $num |= $byte & 0x7f; | |
| 198 | + // bit 8 of the last octet is zero | |
| 199 | +				if (!($byte & 0x80)) { | |
| 200 | + break; | |
| 201 | + } | |
| 202 | + $num <<= 7; | |
| 203 | + } | |
| 204 | + $subids[] = $num; | |
| 205 | + } | |
| 206 | + return $subids; | |
| 207 | + } | |
| 208 | 208 | } | 
| @@ -1,5 +1,5 @@ discard block | ||
| 1 | 1 | <?php | 
| 2 | -declare(strict_types = 1); | |
| 2 | +declare(strict_types=1); | |
| 3 | 3 | |
| 4 | 4 | namespace ASN1\Type\Primitive; | 
| 5 | 5 | |
| @@ -141,7 +141,7 @@ discard block | ||
| 141 | 141 | protected static function _implodeSubIDs(\GMP ...$subids): string | 
| 142 | 142 |      { | 
| 143 | 143 |          return implode(".", | 
| 144 | -            array_map(function ($num) { | |
| 144 | +            array_map(function($num) { | |
| 145 | 145 | return gmp_strval($num, 10); | 
| 146 | 146 | }, $subids)); | 
| 147 | 147 | } | 
| @@ -12,39 +12,39 @@ | ||
| 12 | 12 | */ | 
| 13 | 13 | class RelativeOID extends ObjectIdentifier | 
| 14 | 14 |  { | 
| 15 | - /** | |
| 16 | - * Constructor. | |
| 17 | - * | |
| 18 | - * @param string $oid OID in dotted format | |
| 19 | - */ | |
| 20 | - public function __construct(string $oid) | |
| 21 | -    { | |
| 22 | - $this->_oid = $oid; | |
| 23 | - $this->_subids = self::_explodeDottedOID($oid); | |
| 24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; | |
| 25 | - } | |
| 15 | + /** | |
| 16 | + * Constructor. | |
| 17 | + * | |
| 18 | + * @param string $oid OID in dotted format | |
| 19 | + */ | |
| 20 | + public function __construct(string $oid) | |
| 21 | +	{ | |
| 22 | + $this->_oid = $oid; | |
| 23 | + $this->_subids = self::_explodeDottedOID($oid); | |
| 24 | + $this->_typeTag = self::TYPE_RELATIVE_OID; | |
| 25 | + } | |
| 26 | 26 | |
| 27 | - /** | |
| 28 | - * | |
| 29 | -     * {@inheritdoc} | |
| 30 | - */ | |
| 31 | - protected function _encodedContentDER(): string | |
| 32 | -    { | |
| 33 | - return self::_encodeSubIDs(...$this->_subids); | |
| 34 | - } | |
| 27 | + /** | |
| 28 | + * | |
| 29 | +	 * {@inheritdoc} | |
| 30 | + */ | |
| 31 | + protected function _encodedContentDER(): string | |
| 32 | +	{ | |
| 33 | + return self::_encodeSubIDs(...$this->_subids); | |
| 34 | + } | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * | |
| 38 | -     * {@inheritdoc} | |
| 39 | - * @return self | |
| 40 | - */ | |
| 41 | - protected static function _decodeFromDER(Identifier $identifier, | |
| 42 | - string $data, int &$offset): ElementBase | |
| 43 | -    { | |
| 44 | - $idx = $offset; | |
| 45 | - $len = Length::expectFromDER($data, $idx)->intLength(); | |
| 46 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); | |
| 47 | - $offset = $idx + $len; | |
| 48 | - return new self(self::_implodeSubIDs(...$subids)); | |
| 49 | - } | |
| 36 | + /** | |
| 37 | + * | |
| 38 | +	 * {@inheritdoc} | |
| 39 | + * @return self | |
| 40 | + */ | |
| 41 | + protected static function _decodeFromDER(Identifier $identifier, | |
| 42 | + string $data, int &$offset): ElementBase | |
| 43 | +	{ | |
| 44 | + $idx = $offset; | |
| 45 | + $len = Length::expectFromDER($data, $idx)->intLength(); | |
| 46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); | |
| 47 | + $offset = $idx + $len; | |
| 48 | + return new self(self::_implodeSubIDs(...$subids)); | |
| 49 | + } | |
| 50 | 50 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Primitive; | 
| 6 | 6 | |