@@ -13,295 +13,295 @@ |
||
| 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 = [ /* @formatter:off */ |
|
| 30 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
| 31 | - self::CLASS_APPLICATION => "APPLICATION", |
|
| 32 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
| 33 | - self::CLASS_PRIVATE => "PRIVATE", |
|
| 34 | - /* @formatter:on */ |
|
| 35 | - ]; |
|
| 22 | + /** |
|
| 23 | + * Mapping from type class to human readable name. |
|
| 24 | + * |
|
| 25 | + * @internal |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
| 30 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
| 31 | + self::CLASS_APPLICATION => "APPLICATION", |
|
| 32 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
| 33 | + self::CLASS_PRIVATE => "PRIVATE", |
|
| 34 | + /* @formatter:on */ |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - // P/C enumerations |
|
| 38 | - const PRIMITIVE = 0b0; |
|
| 39 | - const CONSTRUCTED = 0b1; |
|
| 37 | + // P/C enumerations |
|
| 38 | + const PRIMITIVE = 0b0; |
|
| 39 | + const CONSTRUCTED = 0b1; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Type class. |
|
| 43 | - * |
|
| 44 | - * @var int |
|
| 45 | - */ |
|
| 46 | - private $_class; |
|
| 41 | + /** |
|
| 42 | + * Type class. |
|
| 43 | + * |
|
| 44 | + * @var int |
|
| 45 | + */ |
|
| 46 | + private $_class; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Primitive or Constructed. |
|
| 50 | - * |
|
| 51 | - * @var int |
|
| 52 | - */ |
|
| 53 | - private $_pc; |
|
| 48 | + /** |
|
| 49 | + * Primitive or Constructed. |
|
| 50 | + * |
|
| 51 | + * @var int |
|
| 52 | + */ |
|
| 53 | + private $_pc; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Content type tag. |
|
| 57 | - * |
|
| 58 | - * @var BigInt |
|
| 59 | - */ |
|
| 60 | - private $_tag; |
|
| 55 | + /** |
|
| 56 | + * Content type tag. |
|
| 57 | + * |
|
| 58 | + * @var BigInt |
|
| 59 | + */ |
|
| 60 | + private $_tag; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Constructor. |
|
| 64 | - * |
|
| 65 | - * @param int $class Type class |
|
| 66 | - * @param int $pc Primitive / Constructed |
|
| 67 | - * @param int|string $tag Type tag number |
|
| 68 | - */ |
|
| 69 | - public function __construct(int $class, int $pc, $tag) |
|
| 70 | - { |
|
| 71 | - $this->_class = 0b11 & $class; |
|
| 72 | - $this->_pc = 0b1 & $pc; |
|
| 73 | - $this->_tag = new BigInt($tag); |
|
| 74 | - } |
|
| 62 | + /** |
|
| 63 | + * Constructor. |
|
| 64 | + * |
|
| 65 | + * @param int $class Type class |
|
| 66 | + * @param int $pc Primitive / Constructed |
|
| 67 | + * @param int|string $tag Type tag number |
|
| 68 | + */ |
|
| 69 | + public function __construct(int $class, int $pc, $tag) |
|
| 70 | + { |
|
| 71 | + $this->_class = 0b11 & $class; |
|
| 72 | + $this->_pc = 0b1 & $pc; |
|
| 73 | + $this->_tag = new BigInt($tag); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Decode identifier component from DER data. |
|
| 78 | - * |
|
| 79 | - * @param string $data DER encoded data |
|
| 80 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 81 | - * into the data where to start parsing. Variable is updated to |
|
| 82 | - * the offset next to the parsed identifier. If null, start from |
|
| 83 | - * offset 0. |
|
| 84 | - * @throws DecodeException If decoding fails |
|
| 85 | - * @return self |
|
| 86 | - */ |
|
| 87 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 88 | - { |
|
| 89 | - $idx = $offset ? $offset : 0; |
|
| 90 | - $datalen = strlen($data); |
|
| 91 | - if ($idx >= $datalen) { |
|
| 92 | - throw new DecodeException("Invalid offset."); |
|
| 93 | - } |
|
| 94 | - $byte = ord($data[$idx++]); |
|
| 95 | - // bits 8 and 7 (class) |
|
| 96 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 97 | - $class = (0b11000000 & $byte) >> 6; |
|
| 98 | - // bit 6 (0 = primitive / 1 = constructed) |
|
| 99 | - $pc = (0b00100000 & $byte) >> 5; |
|
| 100 | - // bits 5 to 1 (tag number) |
|
| 101 | - $tag = (0b00011111 & $byte); |
|
| 102 | - // long-form identifier |
|
| 103 | - if (0x1f == $tag) { |
|
| 104 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
| 105 | - } |
|
| 106 | - if (isset($offset)) { |
|
| 107 | - $offset = $idx; |
|
| 108 | - } |
|
| 109 | - return new self($class, $pc, $tag); |
|
| 110 | - } |
|
| 76 | + /** |
|
| 77 | + * Decode identifier component from DER data. |
|
| 78 | + * |
|
| 79 | + * @param string $data DER encoded data |
|
| 80 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 81 | + * into the data where to start parsing. Variable is updated to |
|
| 82 | + * the offset next to the parsed identifier. If null, start from |
|
| 83 | + * offset 0. |
|
| 84 | + * @throws DecodeException If decoding fails |
|
| 85 | + * @return self |
|
| 86 | + */ |
|
| 87 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 88 | + { |
|
| 89 | + $idx = $offset ? $offset : 0; |
|
| 90 | + $datalen = strlen($data); |
|
| 91 | + if ($idx >= $datalen) { |
|
| 92 | + throw new DecodeException("Invalid offset."); |
|
| 93 | + } |
|
| 94 | + $byte = ord($data[$idx++]); |
|
| 95 | + // bits 8 and 7 (class) |
|
| 96 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 97 | + $class = (0b11000000 & $byte) >> 6; |
|
| 98 | + // bit 6 (0 = primitive / 1 = constructed) |
|
| 99 | + $pc = (0b00100000 & $byte) >> 5; |
|
| 100 | + // bits 5 to 1 (tag number) |
|
| 101 | + $tag = (0b00011111 & $byte); |
|
| 102 | + // long-form identifier |
|
| 103 | + if (0x1f == $tag) { |
|
| 104 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
| 105 | + } |
|
| 106 | + if (isset($offset)) { |
|
| 107 | + $offset = $idx; |
|
| 108 | + } |
|
| 109 | + return new self($class, $pc, $tag); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Parse long form tag. |
|
| 114 | - * |
|
| 115 | - * @param string $data DER data |
|
| 116 | - * @param int $offset Reference to the variable containing offset to data |
|
| 117 | - * @throws DecodeException If decoding fails |
|
| 118 | - * @return string Tag number |
|
| 119 | - */ |
|
| 120 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 121 | - { |
|
| 122 | - $datalen = strlen($data); |
|
| 123 | - $tag = gmp_init(0, 10); |
|
| 124 | - while (true) { |
|
| 125 | - if ($offset >= $datalen) { |
|
| 126 | - throw new DecodeException( |
|
| 127 | - "Unexpected end of data while decoding" . |
|
| 128 | - " long form identifier."); |
|
| 129 | - } |
|
| 130 | - $byte = ord($data[$offset++]); |
|
| 131 | - $tag <<= 7; |
|
| 132 | - $tag |= 0x7f & $byte; |
|
| 133 | - // last byte has bit 8 set to zero |
|
| 134 | - if (!(0x80 & $byte)) { |
|
| 135 | - break; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - return gmp_strval($tag, 10); |
|
| 139 | - } |
|
| 112 | + /** |
|
| 113 | + * Parse long form tag. |
|
| 114 | + * |
|
| 115 | + * @param string $data DER data |
|
| 116 | + * @param int $offset Reference to the variable containing offset to data |
|
| 117 | + * @throws DecodeException If decoding fails |
|
| 118 | + * @return string Tag number |
|
| 119 | + */ |
|
| 120 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 121 | + { |
|
| 122 | + $datalen = strlen($data); |
|
| 123 | + $tag = gmp_init(0, 10); |
|
| 124 | + while (true) { |
|
| 125 | + if ($offset >= $datalen) { |
|
| 126 | + throw new DecodeException( |
|
| 127 | + "Unexpected end of data while decoding" . |
|
| 128 | + " long form identifier."); |
|
| 129 | + } |
|
| 130 | + $byte = ord($data[$offset++]); |
|
| 131 | + $tag <<= 7; |
|
| 132 | + $tag |= 0x7f & $byte; |
|
| 133 | + // last byte has bit 8 set to zero |
|
| 134 | + if (!(0x80 & $byte)) { |
|
| 135 | + break; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + return gmp_strval($tag, 10); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * |
|
| 143 | - * @see Encodable::toDER() |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - public function toDER(): string |
|
| 147 | - { |
|
| 148 | - $bytes = []; |
|
| 149 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 150 | - $tag = $this->_tag->gmpObj(); |
|
| 151 | - if ($tag < 0x1f) { |
|
| 152 | - $bytes[] = $byte | $tag; |
|
| 153 | - } else { // long-form identifier |
|
| 154 | - $bytes[] = $byte | 0x1f; |
|
| 155 | - $octets = []; |
|
| 156 | - for (; $tag > 0; $tag >>= 7) { |
|
| 157 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 158 | - } |
|
| 159 | - // last octet has bit 8 set to zero |
|
| 160 | - $octets[0] &= 0x7f; |
|
| 161 | - foreach (array_reverse($octets) as $octet) { |
|
| 162 | - $bytes[] = $octet; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - return pack("C*", ...$bytes); |
|
| 166 | - } |
|
| 141 | + /** |
|
| 142 | + * |
|
| 143 | + * @see Encodable::toDER() |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + public function toDER(): string |
|
| 147 | + { |
|
| 148 | + $bytes = []; |
|
| 149 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 150 | + $tag = $this->_tag->gmpObj(); |
|
| 151 | + if ($tag < 0x1f) { |
|
| 152 | + $bytes[] = $byte | $tag; |
|
| 153 | + } else { // long-form identifier |
|
| 154 | + $bytes[] = $byte | 0x1f; |
|
| 155 | + $octets = []; |
|
| 156 | + for (; $tag > 0; $tag >>= 7) { |
|
| 157 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 158 | + } |
|
| 159 | + // last octet has bit 8 set to zero |
|
| 160 | + $octets[0] &= 0x7f; |
|
| 161 | + foreach (array_reverse($octets) as $octet) { |
|
| 162 | + $bytes[] = $octet; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + return pack("C*", ...$bytes); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Get class of the type. |
|
| 170 | - * |
|
| 171 | - * @return int |
|
| 172 | - */ |
|
| 173 | - public function typeClass(): int |
|
| 174 | - { |
|
| 175 | - return $this->_class; |
|
| 176 | - } |
|
| 168 | + /** |
|
| 169 | + * Get class of the type. |
|
| 170 | + * |
|
| 171 | + * @return int |
|
| 172 | + */ |
|
| 173 | + public function typeClass(): int |
|
| 174 | + { |
|
| 175 | + return $this->_class; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Get P/C. |
|
| 180 | - * |
|
| 181 | - * @return int |
|
| 182 | - */ |
|
| 183 | - public function pc(): int |
|
| 184 | - { |
|
| 185 | - return $this->_pc; |
|
| 186 | - } |
|
| 178 | + /** |
|
| 179 | + * Get P/C. |
|
| 180 | + * |
|
| 181 | + * @return int |
|
| 182 | + */ |
|
| 183 | + public function pc(): int |
|
| 184 | + { |
|
| 185 | + return $this->_pc; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Get the tag number. |
|
| 190 | - * |
|
| 191 | - * @return string Base 10 integer string |
|
| 192 | - */ |
|
| 193 | - public function tag(): string |
|
| 194 | - { |
|
| 195 | - return $this->_tag->base10(); |
|
| 196 | - } |
|
| 188 | + /** |
|
| 189 | + * Get the tag number. |
|
| 190 | + * |
|
| 191 | + * @return string Base 10 integer string |
|
| 192 | + */ |
|
| 193 | + public function tag(): string |
|
| 194 | + { |
|
| 195 | + return $this->_tag->base10(); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Get the tag as an integer. |
|
| 200 | - * |
|
| 201 | - * @return int |
|
| 202 | - */ |
|
| 203 | - public function intTag(): int |
|
| 204 | - { |
|
| 205 | - return $this->_tag->intVal(); |
|
| 206 | - } |
|
| 198 | + /** |
|
| 199 | + * Get the tag as an integer. |
|
| 200 | + * |
|
| 201 | + * @return int |
|
| 202 | + */ |
|
| 203 | + public function intTag(): int |
|
| 204 | + { |
|
| 205 | + return $this->_tag->intVal(); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Check whether type is of an universal class. |
|
| 210 | - * |
|
| 211 | - * @return boolean |
|
| 212 | - */ |
|
| 213 | - public function isUniversal(): bool |
|
| 214 | - { |
|
| 215 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
| 216 | - } |
|
| 208 | + /** |
|
| 209 | + * Check whether type is of an universal class. |
|
| 210 | + * |
|
| 211 | + * @return boolean |
|
| 212 | + */ |
|
| 213 | + public function isUniversal(): bool |
|
| 214 | + { |
|
| 215 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * Check whether type is of an application class. |
|
| 220 | - * |
|
| 221 | - * @return boolean |
|
| 222 | - */ |
|
| 223 | - public function isApplication(): bool |
|
| 224 | - { |
|
| 225 | - return self::CLASS_APPLICATION == $this->_class; |
|
| 226 | - } |
|
| 218 | + /** |
|
| 219 | + * Check whether type is of an application class. |
|
| 220 | + * |
|
| 221 | + * @return boolean |
|
| 222 | + */ |
|
| 223 | + public function isApplication(): bool |
|
| 224 | + { |
|
| 225 | + return self::CLASS_APPLICATION == $this->_class; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Check whether type is of a context specific class. |
|
| 230 | - * |
|
| 231 | - * @return boolean |
|
| 232 | - */ |
|
| 233 | - public function isContextSpecific(): bool |
|
| 234 | - { |
|
| 235 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
| 236 | - } |
|
| 228 | + /** |
|
| 229 | + * Check whether type is of a context specific class. |
|
| 230 | + * |
|
| 231 | + * @return boolean |
|
| 232 | + */ |
|
| 233 | + public function isContextSpecific(): bool |
|
| 234 | + { |
|
| 235 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * Check whether type is of a private class. |
|
| 240 | - * |
|
| 241 | - * @return boolean |
|
| 242 | - */ |
|
| 243 | - public function isPrivate(): bool |
|
| 244 | - { |
|
| 245 | - return self::CLASS_PRIVATE == $this->_class; |
|
| 246 | - } |
|
| 238 | + /** |
|
| 239 | + * Check whether type is of a private class. |
|
| 240 | + * |
|
| 241 | + * @return boolean |
|
| 242 | + */ |
|
| 243 | + public function isPrivate(): bool |
|
| 244 | + { |
|
| 245 | + return self::CLASS_PRIVATE == $this->_class; |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * Check whether content is primitive type. |
|
| 250 | - * |
|
| 251 | - * @return boolean |
|
| 252 | - */ |
|
| 253 | - public function isPrimitive(): bool |
|
| 254 | - { |
|
| 255 | - return self::PRIMITIVE == $this->_pc; |
|
| 256 | - } |
|
| 248 | + /** |
|
| 249 | + * Check whether content is primitive type. |
|
| 250 | + * |
|
| 251 | + * @return boolean |
|
| 252 | + */ |
|
| 253 | + public function isPrimitive(): bool |
|
| 254 | + { |
|
| 255 | + return self::PRIMITIVE == $this->_pc; |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * Check hether content is constructed type. |
|
| 260 | - * |
|
| 261 | - * @return boolean |
|
| 262 | - */ |
|
| 263 | - public function isConstructed(): bool |
|
| 264 | - { |
|
| 265 | - return self::CONSTRUCTED == $this->_pc; |
|
| 266 | - } |
|
| 258 | + /** |
|
| 259 | + * Check hether content is constructed type. |
|
| 260 | + * |
|
| 261 | + * @return boolean |
|
| 262 | + */ |
|
| 263 | + public function isConstructed(): bool |
|
| 264 | + { |
|
| 265 | + return self::CONSTRUCTED == $this->_pc; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Get self with given type class. |
|
| 270 | - * |
|
| 271 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 272 | - * @return self |
|
| 273 | - */ |
|
| 274 | - public function withClass(int $class): Identifier |
|
| 275 | - { |
|
| 276 | - $obj = clone $this; |
|
| 277 | - $obj->_class = $class; |
|
| 278 | - return $obj; |
|
| 279 | - } |
|
| 268 | + /** |
|
| 269 | + * Get self with given type class. |
|
| 270 | + * |
|
| 271 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 272 | + * @return self |
|
| 273 | + */ |
|
| 274 | + public function withClass(int $class): Identifier |
|
| 275 | + { |
|
| 276 | + $obj = clone $this; |
|
| 277 | + $obj->_class = $class; |
|
| 278 | + return $obj; |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - /** |
|
| 282 | - * Get self with given type tag. |
|
| 283 | - * |
|
| 284 | - * @param int|string $tag Tag number |
|
| 285 | - * @return self |
|
| 286 | - */ |
|
| 287 | - public function withTag($tag): Identifier |
|
| 288 | - { |
|
| 289 | - $obj = clone $this; |
|
| 290 | - $obj->_tag = new BigInt($tag); |
|
| 291 | - return $obj; |
|
| 292 | - } |
|
| 281 | + /** |
|
| 282 | + * Get self with given type tag. |
|
| 283 | + * |
|
| 284 | + * @param int|string $tag Tag number |
|
| 285 | + * @return self |
|
| 286 | + */ |
|
| 287 | + public function withTag($tag): Identifier |
|
| 288 | + { |
|
| 289 | + $obj = clone $this; |
|
| 290 | + $obj->_tag = new BigInt($tag); |
|
| 291 | + return $obj; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Get human readable name of the type class. |
|
| 296 | - * |
|
| 297 | - * @param int $class |
|
| 298 | - * @return string |
|
| 299 | - */ |
|
| 300 | - public static function classToName(int $class): string |
|
| 301 | - { |
|
| 302 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 303 | - return "CLASS $class"; |
|
| 304 | - } |
|
| 305 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
| 306 | - } |
|
| 294 | + /** |
|
| 295 | + * Get human readable name of the type class. |
|
| 296 | + * |
|
| 297 | + * @param int $class |
|
| 298 | + * @return string |
|
| 299 | + */ |
|
| 300 | + public static function classToName(int $class): string |
|
| 301 | + { |
|
| 302 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 303 | + return "CLASS $class"; |
|
| 304 | + } |
|
| 305 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
| 306 | + } |
|
| 307 | 307 | } |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @var array |
| 28 | 28 | */ |
| 29 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
| 29 | + const MAP_CLASS_TO_NAME = [/* @formatter:off */ |
|
| 30 | 30 | self::CLASS_UNIVERSAL => "UNIVERSAL", |
| 31 | 31 | self::CLASS_APPLICATION => "APPLICATION", |
| 32 | 32 | self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * @throws DecodeException If decoding fails |
| 85 | 85 | * @return self |
| 86 | 86 | */ |
| 87 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 87 | + public static function fromDER(string $data, int & $offset = null): Identifier |
|
| 88 | 88 | { |
| 89 | 89 | $idx = $offset ? $offset : 0; |
| 90 | 90 | $datalen = strlen($data); |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * @throws DecodeException If decoding fails |
| 118 | 118 | * @return string Tag number |
| 119 | 119 | */ |
| 120 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 120 | + private static function _decodeLongFormTag(string $data, int & $offset): string |
|
| 121 | 121 | { |
| 122 | 122 | $datalen = strlen($data); |
| 123 | 123 | $tag = gmp_init(0, 10); |
@@ -20,106 +20,106 @@ |
||
| 20 | 20 | * May be encoded back to complete DER encoding. |
| 21 | 21 | */ |
| 22 | 22 | class DERTaggedType extends TaggedType implements |
| 23 | - ExplicitTagging, |
|
| 24 | - ImplicitTagging |
|
| 23 | + ExplicitTagging, |
|
| 24 | + ImplicitTagging |
|
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Identifier. |
|
| 28 | - * |
|
| 29 | - * @var Identifier |
|
| 30 | - */ |
|
| 31 | - private $_identifier; |
|
| 26 | + /** |
|
| 27 | + * Identifier. |
|
| 28 | + * |
|
| 29 | + * @var Identifier |
|
| 30 | + */ |
|
| 31 | + private $_identifier; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * DER data. |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $_data; |
|
| 33 | + /** |
|
| 34 | + * DER data. |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $_data; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Offset to data. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_offset; |
|
| 40 | + /** |
|
| 41 | + * Offset to data. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_offset; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor. |
|
| 49 | - * |
|
| 50 | - * @param Identifier $identifier |
|
| 51 | - * @param string $data |
|
| 52 | - * @param int $offset Offset to next byte after identifier |
|
| 53 | - */ |
|
| 54 | - public function __construct(Identifier $identifier, string $data, int $offset) |
|
| 55 | - { |
|
| 56 | - $this->_identifier = $identifier; |
|
| 57 | - $this->_data = $data; |
|
| 58 | - $this->_offset = $offset; |
|
| 59 | - $this->_typeTag = $identifier->intTag(); |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Constructor. |
|
| 49 | + * |
|
| 50 | + * @param Identifier $identifier |
|
| 51 | + * @param string $data |
|
| 52 | + * @param int $offset Offset to next byte after identifier |
|
| 53 | + */ |
|
| 54 | + public function __construct(Identifier $identifier, string $data, int $offset) |
|
| 55 | + { |
|
| 56 | + $this->_identifier = $identifier; |
|
| 57 | + $this->_data = $data; |
|
| 58 | + $this->_offset = $offset; |
|
| 59 | + $this->_typeTag = $identifier->intTag(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * |
|
| 64 | - * @see \ASN1\Element::typeClass() |
|
| 65 | - * @return int |
|
| 66 | - */ |
|
| 67 | - public function typeClass(): int |
|
| 68 | - { |
|
| 69 | - return $this->_identifier->typeClass(); |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * |
|
| 64 | + * @see \ASN1\Element::typeClass() |
|
| 65 | + * @return int |
|
| 66 | + */ |
|
| 67 | + public function typeClass(): int |
|
| 68 | + { |
|
| 69 | + return $this->_identifier->typeClass(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * |
|
| 74 | - * @see \ASN1\Element::isConstructed() |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function isConstructed(): bool |
|
| 78 | - { |
|
| 79 | - return $this->_identifier->isConstructed(); |
|
| 80 | - } |
|
| 72 | + /** |
|
| 73 | + * |
|
| 74 | + * @see \ASN1\Element::isConstructed() |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function isConstructed(): bool |
|
| 78 | + { |
|
| 79 | + return $this->_identifier->isConstructed(); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * |
|
| 84 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - protected function _encodedContentDER(): string |
|
| 88 | - { |
|
| 89 | - $idx = $this->_offset; |
|
| 90 | - $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
| 91 | - return substr($this->_data, $idx, $length); |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * |
|
| 84 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + protected function _encodedContentDER(): string |
|
| 88 | + { |
|
| 89 | + $idx = $this->_offset; |
|
| 90 | + $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
| 91 | + return substr($this->_data, $idx, $length); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * |
|
| 96 | - * {@inheritdoc} |
|
| 97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | - * @return UnspecifiedType |
|
| 99 | - */ |
|
| 100 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | - { |
|
| 102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | - $cls = self::_determineImplClass($identifier); |
|
| 104 | - $idx = $this->_offset; |
|
| 105 | - /** @var \ASN1\Feature\ElementBase $element */ |
|
| 106 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 107 | - return $element->asUnspecified(); |
|
| 108 | - } |
|
| 94 | + /** |
|
| 95 | + * |
|
| 96 | + * {@inheritdoc} |
|
| 97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | + * @return UnspecifiedType |
|
| 99 | + */ |
|
| 100 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | + { |
|
| 102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | + $cls = self::_determineImplClass($identifier); |
|
| 104 | + $idx = $this->_offset; |
|
| 105 | + /** @var \ASN1\Feature\ElementBase $element */ |
|
| 106 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 107 | + return $element->asUnspecified(); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * |
|
| 112 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 113 | - * @return UnspecifiedType |
|
| 114 | - */ |
|
| 115 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 116 | - { |
|
| 117 | - $idx = $this->_offset; |
|
| 118 | - Length::expectFromDER($this->_data, $idx); |
|
| 119 | - $element = Element::fromDER($this->_data, $idx); |
|
| 120 | - if (isset($expectedTag)) { |
|
| 121 | - $element->expectType($expectedTag); |
|
| 122 | - } |
|
| 123 | - return $element->asUnspecified(); |
|
| 124 | - } |
|
| 110 | + /** |
|
| 111 | + * |
|
| 112 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 113 | + * @return UnspecifiedType |
|
| 114 | + */ |
|
| 115 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 116 | + { |
|
| 117 | + $idx = $this->_offset; |
|
| 118 | + Length::expectFromDER($this->_data, $idx); |
|
| 119 | + $element = Element::fromDER($this->_data, $idx); |
|
| 120 | + if (isset($expectedTag)) { |
|
| 121 | + $element->expectType($expectedTag); |
|
| 122 | + } |
|
| 123 | + return $element->asUnspecified(); |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -14,52 +14,52 @@ |
||
| 14 | 14 | * is not changed. |
| 15 | 15 | */ |
| 16 | 16 | class ExplicitlyTaggedType extends ContextSpecificTaggedType implements |
| 17 | - ExplicitTagging |
|
| 17 | + ExplicitTagging |
|
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param int $tag Tag number |
|
| 23 | - * @param Element $element Wrapped element |
|
| 24 | - */ |
|
| 25 | - public function __construct(int $tag, Element $element) |
|
| 26 | - { |
|
| 27 | - $this->_typeTag = $tag; |
|
| 28 | - $this->_element = $element; |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param int $tag Tag number |
|
| 23 | + * @param Element $element Wrapped element |
|
| 24 | + */ |
|
| 25 | + public function __construct(int $tag, Element $element) |
|
| 26 | + { |
|
| 27 | + $this->_typeTag = $tag; |
|
| 28 | + $this->_element = $element; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * |
|
| 33 | - * @see \ASN1\Element::isConstructed() |
|
| 34 | - * @return bool |
|
| 35 | - */ |
|
| 36 | - public function isConstructed(): bool |
|
| 37 | - { |
|
| 38 | - return true; |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * |
|
| 33 | + * @see \ASN1\Element::isConstructed() |
|
| 34 | + * @return bool |
|
| 35 | + */ |
|
| 36 | + public function isConstructed(): bool |
|
| 37 | + { |
|
| 38 | + return true; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * |
|
| 43 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - protected function _encodedContentDER(): string |
|
| 47 | - { |
|
| 48 | - // get the full encoding of the wrapped element |
|
| 49 | - return $this->_element->toDER(); |
|
| 50 | - } |
|
| 41 | + /** |
|
| 42 | + * |
|
| 43 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + protected function _encodedContentDER(): string |
|
| 47 | + { |
|
| 48 | + // get the full encoding of the wrapped element |
|
| 49 | + return $this->_element->toDER(); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * |
|
| 54 | - * {@inheritdoc} |
|
| 55 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 56 | - * @return UnspecifiedType |
|
| 57 | - */ |
|
| 58 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 59 | - { |
|
| 60 | - if (isset($expectedTag)) { |
|
| 61 | - $this->_element->expectType($expectedTag); |
|
| 62 | - } |
|
| 63 | - return $this->_element->asUnspecified(); |
|
| 64 | - } |
|
| 52 | + /** |
|
| 53 | + * |
|
| 54 | + * {@inheritdoc} |
|
| 55 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 56 | + * @return UnspecifiedType |
|
| 57 | + */ |
|
| 58 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 59 | + { |
|
| 60 | + if (isset($expectedTag)) { |
|
| 61 | + $this->_element->expectType($expectedTag); |
|
| 62 | + } |
|
| 63 | + return $this->_element->asUnspecified(); |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -16,57 +16,57 @@ |
||
| 16 | 16 | * decoding the data. |
| 17 | 17 | */ |
| 18 | 18 | class ImplicitlyTaggedType extends ContextSpecificTaggedType implements |
| 19 | - ImplicitTagging |
|
| 19 | + ImplicitTagging |
|
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param int $tag |
|
| 25 | - * @param Element $element |
|
| 26 | - */ |
|
| 27 | - public function __construct(int $tag, Element $element) |
|
| 28 | - { |
|
| 29 | - $this->_typeTag = $tag; |
|
| 30 | - $this->_element = $element; |
|
| 31 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param int $tag |
|
| 25 | + * @param Element $element |
|
| 26 | + */ |
|
| 27 | + public function __construct(int $tag, Element $element) |
|
| 28 | + { |
|
| 29 | + $this->_typeTag = $tag; |
|
| 30 | + $this->_element = $element; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * |
|
| 35 | - * @see \ASN1\Element::isConstructed() |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function isConstructed(): bool |
|
| 39 | - { |
|
| 40 | - // depends on the underlying type |
|
| 41 | - return $this->_element->isConstructed(); |
|
| 42 | - } |
|
| 33 | + /** |
|
| 34 | + * |
|
| 35 | + * @see \ASN1\Element::isConstructed() |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function isConstructed(): bool |
|
| 39 | + { |
|
| 40 | + // depends on the underlying type |
|
| 41 | + return $this->_element->isConstructed(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * |
|
| 46 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - protected function _encodedContentDER(): string |
|
| 50 | - { |
|
| 51 | - // get only the content of the wrapped element. |
|
| 52 | - return $this->_element->_encodedContentDER(); |
|
| 53 | - } |
|
| 44 | + /** |
|
| 45 | + * |
|
| 46 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + protected function _encodedContentDER(): string |
|
| 50 | + { |
|
| 51 | + // get only the content of the wrapped element. |
|
| 52 | + return $this->_element->_encodedContentDER(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 59 | - * @return UnspecifiedType |
|
| 60 | - */ |
|
| 61 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 62 | - { |
|
| 63 | - $this->_element->expectType($tag); |
|
| 64 | - if ($this->_element->typeClass() != $class) { |
|
| 65 | - throw new \UnexpectedValueException( |
|
| 66 | - sprintf("Type class %s expected, got %s.", |
|
| 67 | - Identifier::classToName($class), |
|
| 68 | - Identifier::classToName($this->_element->typeClass()))); |
|
| 69 | - } |
|
| 70 | - return $this->_element->asUnspecified(); |
|
| 71 | - } |
|
| 55 | + /** |
|
| 56 | + * |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 59 | + * @return UnspecifiedType |
|
| 60 | + */ |
|
| 61 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 62 | + { |
|
| 63 | + $this->_element->expectType($tag); |
|
| 64 | + if ($this->_element->typeClass() != $class) { |
|
| 65 | + throw new \UnexpectedValueException( |
|
| 66 | + sprintf("Type class %s expected, got %s.", |
|
| 67 | + Identifier::classToName($class), |
|
| 68 | + Identifier::classToName($this->_element->typeClass()))); |
|
| 69 | + } |
|
| 70 | + return $this->_element->asUnspecified(); |
|
| 71 | + } |
|
| 72 | 72 | } |