@@ -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 `Identifier::CLASS_*` 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 `Identifier::CLASS_*` 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 | } |
@@ -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 `CLASS_*` 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 `CLASS_*` 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 | } |
@@ -17,674 +17,674 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UnspecifiedType implements ElementBase |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * The wrapped element. |
|
| 22 | - * |
|
| 23 | - * @var Element |
|
| 24 | - */ |
|
| 25 | - private $_element; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param Element $el |
|
| 31 | - */ |
|
| 32 | - public function __construct(Element $el) |
|
| 33 | - { |
|
| 34 | - $this->_element = $el; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Initialize from DER data. |
|
| 39 | - * |
|
| 40 | - * @param string $data DER encoded data |
|
| 41 | - * |
|
| 42 | - * @return self |
|
| 43 | - */ |
|
| 44 | - public static function fromDER(string $data): self |
|
| 45 | - { |
|
| 46 | - return Element::fromDER($data)->asUnspecified(); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Initialize from ElementBase interface. |
|
| 51 | - * |
|
| 52 | - * @param ElementBase $el |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromElementBase(ElementBase $el): self |
|
| 57 | - { |
|
| 58 | - // if element is already wrapped |
|
| 59 | - if ($el instanceof self) { |
|
| 60 | - return $el; |
|
| 61 | - } |
|
| 62 | - return new self($el->asElement()); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the wrapped element as a context specific tagged type. |
|
| 67 | - * |
|
| 68 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 69 | - * |
|
| 70 | - * @return TaggedType |
|
| 71 | - */ |
|
| 72 | - public function asTagged(): TaggedType |
|
| 73 | - { |
|
| 74 | - if (!$this->_element instanceof TaggedType) { |
|
| 75 | - throw new \UnexpectedValueException( |
|
| 76 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
| 77 | - } |
|
| 78 | - return $this->_element; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the wrapped element as an application specific type. |
|
| 83 | - * |
|
| 84 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 85 | - * |
|
| 86 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
| 87 | - */ |
|
| 88 | - public function asApplication(): Tagged\ApplicationType |
|
| 89 | - { |
|
| 90 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 91 | - throw new \UnexpectedValueException( |
|
| 92 | - 'Application type expected, got ' . $this->_typeDescriptorString()); |
|
| 93 | - } |
|
| 94 | - return $this->_element; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get the wrapped element as a private tagged type. |
|
| 99 | - * |
|
| 100 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
| 101 | - * |
|
| 102 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
| 103 | - */ |
|
| 104 | - public function asPrivate(): Tagged\PrivateType |
|
| 105 | - { |
|
| 106 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 107 | - throw new \UnexpectedValueException( |
|
| 108 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
| 109 | - } |
|
| 110 | - return $this->_element; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the wrapped element as a boolean type. |
|
| 115 | - * |
|
| 116 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 117 | - * |
|
| 118 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
| 119 | - */ |
|
| 120 | - public function asBoolean(): Primitive\Boolean |
|
| 121 | - { |
|
| 122 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 123 | - throw new \UnexpectedValueException( |
|
| 124 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 125 | - } |
|
| 126 | - return $this->_element; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get the wrapped element as an integer type. |
|
| 131 | - * |
|
| 132 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 133 | - * |
|
| 134 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
| 135 | - */ |
|
| 136 | - public function asInteger(): Primitive\Integer |
|
| 137 | - { |
|
| 138 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 139 | - throw new \UnexpectedValueException( |
|
| 140 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 141 | - } |
|
| 142 | - return $this->_element; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Get the wrapped element as a bit string type. |
|
| 147 | - * |
|
| 148 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 149 | - * |
|
| 150 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
| 151 | - */ |
|
| 152 | - public function asBitString(): Primitive\BitString |
|
| 153 | - { |
|
| 154 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 155 | - throw new \UnexpectedValueException( |
|
| 156 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 157 | - } |
|
| 158 | - return $this->_element; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get the wrapped element as an octet string type. |
|
| 163 | - * |
|
| 164 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 165 | - * |
|
| 166 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
| 167 | - */ |
|
| 168 | - public function asOctetString(): Primitive\OctetString |
|
| 169 | - { |
|
| 170 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 171 | - throw new \UnexpectedValueException( |
|
| 172 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 173 | - } |
|
| 174 | - return $this->_element; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Get the wrapped element as a null type. |
|
| 179 | - * |
|
| 180 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 181 | - * |
|
| 182 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
| 183 | - */ |
|
| 184 | - public function asNull(): Primitive\NullType |
|
| 185 | - { |
|
| 186 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 187 | - throw new \UnexpectedValueException( |
|
| 188 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 189 | - } |
|
| 190 | - return $this->_element; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get the wrapped element as an object identifier type. |
|
| 195 | - * |
|
| 196 | - * @throws \UnexpectedValueException If the element is not an object identifier |
|
| 197 | - * |
|
| 198 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
| 199 | - */ |
|
| 200 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 201 | - { |
|
| 202 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 203 | - throw new \UnexpectedValueException( |
|
| 204 | - $this->_generateExceptionMessage(Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 205 | - } |
|
| 206 | - return $this->_element; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the wrapped element as an object descriptor type. |
|
| 211 | - * |
|
| 212 | - * @throws \UnexpectedValueException If the element is not an object descriptor |
|
| 213 | - * |
|
| 214 | - * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
| 215 | - */ |
|
| 216 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 217 | - { |
|
| 218 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 219 | - throw new \UnexpectedValueException( |
|
| 220 | - $this->_generateExceptionMessage(Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 221 | - } |
|
| 222 | - return $this->_element; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Get the wrapped element as a real type. |
|
| 227 | - * |
|
| 228 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 229 | - * |
|
| 230 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
| 231 | - */ |
|
| 232 | - public function asReal(): Primitive\Real |
|
| 233 | - { |
|
| 234 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 235 | - throw new \UnexpectedValueException( |
|
| 236 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 237 | - } |
|
| 238 | - return $this->_element; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Get the wrapped element as an enumerated type. |
|
| 243 | - * |
|
| 244 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 245 | - * |
|
| 246 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
| 247 | - */ |
|
| 248 | - public function asEnumerated(): Primitive\Enumerated |
|
| 249 | - { |
|
| 250 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 251 | - throw new \UnexpectedValueException( |
|
| 252 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 253 | - } |
|
| 254 | - return $this->_element; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Get the wrapped element as a UTF8 string type. |
|
| 259 | - * |
|
| 260 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 261 | - * |
|
| 262 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
| 263 | - */ |
|
| 264 | - public function asUTF8String(): Primitive\UTF8String |
|
| 265 | - { |
|
| 266 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 267 | - throw new \UnexpectedValueException( |
|
| 268 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 269 | - } |
|
| 270 | - return $this->_element; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the wrapped element as a relative OID type. |
|
| 275 | - * |
|
| 276 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 277 | - * |
|
| 278 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
| 279 | - */ |
|
| 280 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 281 | - { |
|
| 282 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 283 | - throw new \UnexpectedValueException( |
|
| 284 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 285 | - } |
|
| 286 | - return $this->_element; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Get the wrapped element as a sequence type. |
|
| 291 | - * |
|
| 292 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 293 | - * |
|
| 294 | - * @return \Sop\ASN1\Type\Constructed\Sequence |
|
| 295 | - */ |
|
| 296 | - public function asSequence(): Constructed\Sequence |
|
| 297 | - { |
|
| 298 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 299 | - throw new \UnexpectedValueException( |
|
| 300 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 301 | - } |
|
| 302 | - return $this->_element; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Get the wrapped element as a set type. |
|
| 307 | - * |
|
| 308 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 309 | - * |
|
| 310 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
| 311 | - */ |
|
| 312 | - public function asSet(): Constructed\Set |
|
| 313 | - { |
|
| 314 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 315 | - throw new \UnexpectedValueException( |
|
| 316 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 317 | - } |
|
| 318 | - return $this->_element; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Get the wrapped element as a numeric string type. |
|
| 323 | - * |
|
| 324 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 325 | - * |
|
| 326 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
| 327 | - */ |
|
| 328 | - public function asNumericString(): Primitive\NumericString |
|
| 329 | - { |
|
| 330 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 331 | - throw new \UnexpectedValueException( |
|
| 332 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 333 | - } |
|
| 334 | - return $this->_element; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Get the wrapped element as a printable string type. |
|
| 339 | - * |
|
| 340 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 341 | - * string |
|
| 342 | - * |
|
| 343 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
| 344 | - */ |
|
| 345 | - public function asPrintableString(): Primitive\PrintableString |
|
| 346 | - { |
|
| 347 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 348 | - throw new \UnexpectedValueException( |
|
| 349 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 350 | - } |
|
| 351 | - return $this->_element; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Get the wrapped element as a T61 string type. |
|
| 356 | - * |
|
| 357 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 358 | - * |
|
| 359 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
| 360 | - */ |
|
| 361 | - public function asT61String(): Primitive\T61String |
|
| 362 | - { |
|
| 363 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 364 | - throw new \UnexpectedValueException( |
|
| 365 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 366 | - } |
|
| 367 | - return $this->_element; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Get the wrapped element as a videotex string type. |
|
| 372 | - * |
|
| 373 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 374 | - * |
|
| 375 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
| 376 | - */ |
|
| 377 | - public function asVideotexString(): Primitive\VideotexString |
|
| 378 | - { |
|
| 379 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 380 | - throw new \UnexpectedValueException( |
|
| 381 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 382 | - } |
|
| 383 | - return $this->_element; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Get the wrapped element as a IA5 string type. |
|
| 388 | - * |
|
| 389 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 390 | - * |
|
| 391 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
| 392 | - */ |
|
| 393 | - public function asIA5String(): Primitive\IA5String |
|
| 394 | - { |
|
| 395 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 396 | - throw new \UnexpectedValueException( |
|
| 397 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 398 | - } |
|
| 399 | - return $this->_element; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Get the wrapped element as an UTC time type. |
|
| 404 | - * |
|
| 405 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 406 | - * |
|
| 407 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
| 408 | - */ |
|
| 409 | - public function asUTCTime(): Primitive\UTCTime |
|
| 410 | - { |
|
| 411 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 412 | - throw new \UnexpectedValueException( |
|
| 413 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 414 | - } |
|
| 415 | - return $this->_element; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Get the wrapped element as a generalized time type. |
|
| 420 | - * |
|
| 421 | - * @throws \UnexpectedValueException If the element is not a generalized time |
|
| 422 | - * |
|
| 423 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
| 424 | - */ |
|
| 425 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 426 | - { |
|
| 427 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 428 | - throw new \UnexpectedValueException( |
|
| 429 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 430 | - } |
|
| 431 | - return $this->_element; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Get the wrapped element as a graphic string type. |
|
| 436 | - * |
|
| 437 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 438 | - * |
|
| 439 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
| 440 | - */ |
|
| 441 | - public function asGraphicString(): Primitive\GraphicString |
|
| 442 | - { |
|
| 443 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 444 | - throw new \UnexpectedValueException( |
|
| 445 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 446 | - } |
|
| 447 | - return $this->_element; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Get the wrapped element as a visible string type. |
|
| 452 | - * |
|
| 453 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 454 | - * |
|
| 455 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
| 456 | - */ |
|
| 457 | - public function asVisibleString(): Primitive\VisibleString |
|
| 458 | - { |
|
| 459 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 460 | - throw new \UnexpectedValueException( |
|
| 461 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 462 | - } |
|
| 463 | - return $this->_element; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Get the wrapped element as a general string type. |
|
| 468 | - * |
|
| 469 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 470 | - * |
|
| 471 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
| 472 | - */ |
|
| 473 | - public function asGeneralString(): Primitive\GeneralString |
|
| 474 | - { |
|
| 475 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 476 | - throw new \UnexpectedValueException( |
|
| 477 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 478 | - } |
|
| 479 | - return $this->_element; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Get the wrapped element as a universal string type. |
|
| 484 | - * |
|
| 485 | - * @throws \UnexpectedValueException If the element is not a universal string |
|
| 486 | - * |
|
| 487 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
| 488 | - */ |
|
| 489 | - public function asUniversalString(): Primitive\UniversalString |
|
| 490 | - { |
|
| 491 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 492 | - throw new \UnexpectedValueException( |
|
| 493 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 494 | - } |
|
| 495 | - return $this->_element; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Get the wrapped element as a character string type. |
|
| 500 | - * |
|
| 501 | - * @throws \UnexpectedValueException If the element is not a character string |
|
| 502 | - * |
|
| 503 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
| 504 | - */ |
|
| 505 | - public function asCharacterString(): Primitive\CharacterString |
|
| 506 | - { |
|
| 507 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 508 | - throw new \UnexpectedValueException( |
|
| 509 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 510 | - } |
|
| 511 | - return $this->_element; |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Get the wrapped element as a BMP string type. |
|
| 516 | - * |
|
| 517 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 518 | - * |
|
| 519 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
| 520 | - */ |
|
| 521 | - public function asBMPString(): Primitive\BMPString |
|
| 522 | - { |
|
| 523 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 524 | - throw new \UnexpectedValueException( |
|
| 525 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 526 | - } |
|
| 527 | - return $this->_element; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Get the wrapped element as a constructed string type. |
|
| 532 | - * |
|
| 533 | - * @throws \UnexpectedValueException If the element is not a constructed string |
|
| 534 | - * |
|
| 535 | - * @return \Sop\ASN1\Type\Constructed\ConstructedString |
|
| 536 | - */ |
|
| 537 | - public function asConstructedString(): Constructed\ConstructedString |
|
| 538 | - { |
|
| 539 | - if (!$this->_element instanceof Constructed\ConstructedString) { |
|
| 540 | - throw new \UnexpectedValueException( |
|
| 541 | - $this->_generateExceptionMessage(Element::TYPE_CONSTRUCTED_STRING)); |
|
| 542 | - } |
|
| 543 | - return $this->_element; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Get the wrapped element as any string type. |
|
| 548 | - * |
|
| 549 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 550 | - * |
|
| 551 | - * @return StringType |
|
| 552 | - */ |
|
| 553 | - public function asString(): StringType |
|
| 554 | - { |
|
| 555 | - if (!$this->_element instanceof StringType) { |
|
| 556 | - throw new \UnexpectedValueException( |
|
| 557 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 558 | - } |
|
| 559 | - return $this->_element; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Get the wrapped element as any time type. |
|
| 564 | - * |
|
| 565 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 566 | - * |
|
| 567 | - * @return TimeType |
|
| 568 | - */ |
|
| 569 | - public function asTime(): TimeType |
|
| 570 | - { |
|
| 571 | - if (!$this->_element instanceof TimeType) { |
|
| 572 | - throw new \UnexpectedValueException( |
|
| 573 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 574 | - } |
|
| 575 | - return $this->_element; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * {@inheritdoc} |
|
| 580 | - */ |
|
| 581 | - public function toDER(): string |
|
| 582 | - { |
|
| 583 | - return $this->_element->toDER(); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * {@inheritdoc} |
|
| 588 | - */ |
|
| 589 | - public function typeClass(): int |
|
| 590 | - { |
|
| 591 | - return $this->_element->typeClass(); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * {@inheritdoc} |
|
| 596 | - */ |
|
| 597 | - public function isConstructed(): bool |
|
| 598 | - { |
|
| 599 | - return $this->_element->isConstructed(); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * {@inheritdoc} |
|
| 604 | - */ |
|
| 605 | - public function tag(): int |
|
| 606 | - { |
|
| 607 | - return $this->_element->tag(); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * {@inheritdoc} |
|
| 612 | - */ |
|
| 613 | - public function isType(int $tag): bool |
|
| 614 | - { |
|
| 615 | - return $this->_element->isType($tag); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * {@inheritdoc} |
|
| 620 | - * |
|
| 621 | - * Consider using any of the `as*` accessor methods instead. |
|
| 622 | - */ |
|
| 623 | - public function expectType(int $tag): ElementBase |
|
| 624 | - { |
|
| 625 | - return $this->_element->expectType($tag); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * {@inheritdoc} |
|
| 630 | - */ |
|
| 631 | - public function isTagged(): bool |
|
| 632 | - { |
|
| 633 | - return $this->_element->isTagged(); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * {@inheritdoc} |
|
| 638 | - * |
|
| 639 | - * Consider using `asTagged()` method instead and chaining |
|
| 640 | - * with `TaggedType::asExplicit()` or `TaggedType::asImplicit()`. |
|
| 641 | - */ |
|
| 642 | - public function expectTagged(?int $tag = null): TaggedType |
|
| 643 | - { |
|
| 644 | - return $this->_element->expectTagged($tag); |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * {@inheritdoc} |
|
| 649 | - */ |
|
| 650 | - public function asElement(): Element |
|
| 651 | - { |
|
| 652 | - return $this->_element; |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * {@inheritdoc} |
|
| 657 | - */ |
|
| 658 | - public function asUnspecified(): UnspecifiedType |
|
| 659 | - { |
|
| 660 | - return $this; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Generate message for exceptions thrown by `as*` methods. |
|
| 665 | - * |
|
| 666 | - * @param int $tag Type tag of the expected element |
|
| 667 | - * |
|
| 668 | - * @return string |
|
| 669 | - */ |
|
| 670 | - private function _generateExceptionMessage(int $tag): string |
|
| 671 | - { |
|
| 672 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
| 673 | - $this->_typeDescriptorString()); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 678 | - * |
|
| 679 | - * @return string |
|
| 680 | - */ |
|
| 681 | - private function _typeDescriptorString(): string |
|
| 682 | - { |
|
| 683 | - $type_cls = $this->_element->typeClass(); |
|
| 684 | - $tag = $this->_element->tag(); |
|
| 685 | - if (Identifier::CLASS_UNIVERSAL === $type_cls) { |
|
| 686 | - return Element::tagToName($tag); |
|
| 687 | - } |
|
| 688 | - return Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
| 689 | - } |
|
| 20 | + /** |
|
| 21 | + * The wrapped element. |
|
| 22 | + * |
|
| 23 | + * @var Element |
|
| 24 | + */ |
|
| 25 | + private $_element; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param Element $el |
|
| 31 | + */ |
|
| 32 | + public function __construct(Element $el) |
|
| 33 | + { |
|
| 34 | + $this->_element = $el; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Initialize from DER data. |
|
| 39 | + * |
|
| 40 | + * @param string $data DER encoded data |
|
| 41 | + * |
|
| 42 | + * @return self |
|
| 43 | + */ |
|
| 44 | + public static function fromDER(string $data): self |
|
| 45 | + { |
|
| 46 | + return Element::fromDER($data)->asUnspecified(); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Initialize from ElementBase interface. |
|
| 51 | + * |
|
| 52 | + * @param ElementBase $el |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromElementBase(ElementBase $el): self |
|
| 57 | + { |
|
| 58 | + // if element is already wrapped |
|
| 59 | + if ($el instanceof self) { |
|
| 60 | + return $el; |
|
| 61 | + } |
|
| 62 | + return new self($el->asElement()); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the wrapped element as a context specific tagged type. |
|
| 67 | + * |
|
| 68 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 69 | + * |
|
| 70 | + * @return TaggedType |
|
| 71 | + */ |
|
| 72 | + public function asTagged(): TaggedType |
|
| 73 | + { |
|
| 74 | + if (!$this->_element instanceof TaggedType) { |
|
| 75 | + throw new \UnexpectedValueException( |
|
| 76 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
| 77 | + } |
|
| 78 | + return $this->_element; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the wrapped element as an application specific type. |
|
| 83 | + * |
|
| 84 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 85 | + * |
|
| 86 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
| 87 | + */ |
|
| 88 | + public function asApplication(): Tagged\ApplicationType |
|
| 89 | + { |
|
| 90 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 91 | + throw new \UnexpectedValueException( |
|
| 92 | + 'Application type expected, got ' . $this->_typeDescriptorString()); |
|
| 93 | + } |
|
| 94 | + return $this->_element; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get the wrapped element as a private tagged type. |
|
| 99 | + * |
|
| 100 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
| 101 | + * |
|
| 102 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
| 103 | + */ |
|
| 104 | + public function asPrivate(): Tagged\PrivateType |
|
| 105 | + { |
|
| 106 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 107 | + throw new \UnexpectedValueException( |
|
| 108 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
| 109 | + } |
|
| 110 | + return $this->_element; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the wrapped element as a boolean type. |
|
| 115 | + * |
|
| 116 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 117 | + * |
|
| 118 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
| 119 | + */ |
|
| 120 | + public function asBoolean(): Primitive\Boolean |
|
| 121 | + { |
|
| 122 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 123 | + throw new \UnexpectedValueException( |
|
| 124 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 125 | + } |
|
| 126 | + return $this->_element; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get the wrapped element as an integer type. |
|
| 131 | + * |
|
| 132 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 133 | + * |
|
| 134 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
| 135 | + */ |
|
| 136 | + public function asInteger(): Primitive\Integer |
|
| 137 | + { |
|
| 138 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 139 | + throw new \UnexpectedValueException( |
|
| 140 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 141 | + } |
|
| 142 | + return $this->_element; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Get the wrapped element as a bit string type. |
|
| 147 | + * |
|
| 148 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 149 | + * |
|
| 150 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
| 151 | + */ |
|
| 152 | + public function asBitString(): Primitive\BitString |
|
| 153 | + { |
|
| 154 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 155 | + throw new \UnexpectedValueException( |
|
| 156 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 157 | + } |
|
| 158 | + return $this->_element; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get the wrapped element as an octet string type. |
|
| 163 | + * |
|
| 164 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 165 | + * |
|
| 166 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
| 167 | + */ |
|
| 168 | + public function asOctetString(): Primitive\OctetString |
|
| 169 | + { |
|
| 170 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 171 | + throw new \UnexpectedValueException( |
|
| 172 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 173 | + } |
|
| 174 | + return $this->_element; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Get the wrapped element as a null type. |
|
| 179 | + * |
|
| 180 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 181 | + * |
|
| 182 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
| 183 | + */ |
|
| 184 | + public function asNull(): Primitive\NullType |
|
| 185 | + { |
|
| 186 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 187 | + throw new \UnexpectedValueException( |
|
| 188 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 189 | + } |
|
| 190 | + return $this->_element; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get the wrapped element as an object identifier type. |
|
| 195 | + * |
|
| 196 | + * @throws \UnexpectedValueException If the element is not an object identifier |
|
| 197 | + * |
|
| 198 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
| 199 | + */ |
|
| 200 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 201 | + { |
|
| 202 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 203 | + throw new \UnexpectedValueException( |
|
| 204 | + $this->_generateExceptionMessage(Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 205 | + } |
|
| 206 | + return $this->_element; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the wrapped element as an object descriptor type. |
|
| 211 | + * |
|
| 212 | + * @throws \UnexpectedValueException If the element is not an object descriptor |
|
| 213 | + * |
|
| 214 | + * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
| 215 | + */ |
|
| 216 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 217 | + { |
|
| 218 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 219 | + throw new \UnexpectedValueException( |
|
| 220 | + $this->_generateExceptionMessage(Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 221 | + } |
|
| 222 | + return $this->_element; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Get the wrapped element as a real type. |
|
| 227 | + * |
|
| 228 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 229 | + * |
|
| 230 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
| 231 | + */ |
|
| 232 | + public function asReal(): Primitive\Real |
|
| 233 | + { |
|
| 234 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 235 | + throw new \UnexpectedValueException( |
|
| 236 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 237 | + } |
|
| 238 | + return $this->_element; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Get the wrapped element as an enumerated type. |
|
| 243 | + * |
|
| 244 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 245 | + * |
|
| 246 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
| 247 | + */ |
|
| 248 | + public function asEnumerated(): Primitive\Enumerated |
|
| 249 | + { |
|
| 250 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 251 | + throw new \UnexpectedValueException( |
|
| 252 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 253 | + } |
|
| 254 | + return $this->_element; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Get the wrapped element as a UTF8 string type. |
|
| 259 | + * |
|
| 260 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 261 | + * |
|
| 262 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
| 263 | + */ |
|
| 264 | + public function asUTF8String(): Primitive\UTF8String |
|
| 265 | + { |
|
| 266 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 267 | + throw new \UnexpectedValueException( |
|
| 268 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 269 | + } |
|
| 270 | + return $this->_element; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the wrapped element as a relative OID type. |
|
| 275 | + * |
|
| 276 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 277 | + * |
|
| 278 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
| 279 | + */ |
|
| 280 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 281 | + { |
|
| 282 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 283 | + throw new \UnexpectedValueException( |
|
| 284 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 285 | + } |
|
| 286 | + return $this->_element; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Get the wrapped element as a sequence type. |
|
| 291 | + * |
|
| 292 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 293 | + * |
|
| 294 | + * @return \Sop\ASN1\Type\Constructed\Sequence |
|
| 295 | + */ |
|
| 296 | + public function asSequence(): Constructed\Sequence |
|
| 297 | + { |
|
| 298 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 299 | + throw new \UnexpectedValueException( |
|
| 300 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 301 | + } |
|
| 302 | + return $this->_element; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Get the wrapped element as a set type. |
|
| 307 | + * |
|
| 308 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 309 | + * |
|
| 310 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
| 311 | + */ |
|
| 312 | + public function asSet(): Constructed\Set |
|
| 313 | + { |
|
| 314 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 315 | + throw new \UnexpectedValueException( |
|
| 316 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 317 | + } |
|
| 318 | + return $this->_element; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Get the wrapped element as a numeric string type. |
|
| 323 | + * |
|
| 324 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 325 | + * |
|
| 326 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
| 327 | + */ |
|
| 328 | + public function asNumericString(): Primitive\NumericString |
|
| 329 | + { |
|
| 330 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 331 | + throw new \UnexpectedValueException( |
|
| 332 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 333 | + } |
|
| 334 | + return $this->_element; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Get the wrapped element as a printable string type. |
|
| 339 | + * |
|
| 340 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 341 | + * string |
|
| 342 | + * |
|
| 343 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
| 344 | + */ |
|
| 345 | + public function asPrintableString(): Primitive\PrintableString |
|
| 346 | + { |
|
| 347 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 348 | + throw new \UnexpectedValueException( |
|
| 349 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 350 | + } |
|
| 351 | + return $this->_element; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Get the wrapped element as a T61 string type. |
|
| 356 | + * |
|
| 357 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 358 | + * |
|
| 359 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
| 360 | + */ |
|
| 361 | + public function asT61String(): Primitive\T61String |
|
| 362 | + { |
|
| 363 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 364 | + throw new \UnexpectedValueException( |
|
| 365 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 366 | + } |
|
| 367 | + return $this->_element; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Get the wrapped element as a videotex string type. |
|
| 372 | + * |
|
| 373 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 374 | + * |
|
| 375 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
| 376 | + */ |
|
| 377 | + public function asVideotexString(): Primitive\VideotexString |
|
| 378 | + { |
|
| 379 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 380 | + throw new \UnexpectedValueException( |
|
| 381 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 382 | + } |
|
| 383 | + return $this->_element; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Get the wrapped element as a IA5 string type. |
|
| 388 | + * |
|
| 389 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 390 | + * |
|
| 391 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
| 392 | + */ |
|
| 393 | + public function asIA5String(): Primitive\IA5String |
|
| 394 | + { |
|
| 395 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 396 | + throw new \UnexpectedValueException( |
|
| 397 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 398 | + } |
|
| 399 | + return $this->_element; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Get the wrapped element as an UTC time type. |
|
| 404 | + * |
|
| 405 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 406 | + * |
|
| 407 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
| 408 | + */ |
|
| 409 | + public function asUTCTime(): Primitive\UTCTime |
|
| 410 | + { |
|
| 411 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 412 | + throw new \UnexpectedValueException( |
|
| 413 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 414 | + } |
|
| 415 | + return $this->_element; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Get the wrapped element as a generalized time type. |
|
| 420 | + * |
|
| 421 | + * @throws \UnexpectedValueException If the element is not a generalized time |
|
| 422 | + * |
|
| 423 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
| 424 | + */ |
|
| 425 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 426 | + { |
|
| 427 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 428 | + throw new \UnexpectedValueException( |
|
| 429 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 430 | + } |
|
| 431 | + return $this->_element; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Get the wrapped element as a graphic string type. |
|
| 436 | + * |
|
| 437 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 438 | + * |
|
| 439 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
| 440 | + */ |
|
| 441 | + public function asGraphicString(): Primitive\GraphicString |
|
| 442 | + { |
|
| 443 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 444 | + throw new \UnexpectedValueException( |
|
| 445 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 446 | + } |
|
| 447 | + return $this->_element; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Get the wrapped element as a visible string type. |
|
| 452 | + * |
|
| 453 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 454 | + * |
|
| 455 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
| 456 | + */ |
|
| 457 | + public function asVisibleString(): Primitive\VisibleString |
|
| 458 | + { |
|
| 459 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 460 | + throw new \UnexpectedValueException( |
|
| 461 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 462 | + } |
|
| 463 | + return $this->_element; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Get the wrapped element as a general string type. |
|
| 468 | + * |
|
| 469 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 470 | + * |
|
| 471 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
| 472 | + */ |
|
| 473 | + public function asGeneralString(): Primitive\GeneralString |
|
| 474 | + { |
|
| 475 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 476 | + throw new \UnexpectedValueException( |
|
| 477 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 478 | + } |
|
| 479 | + return $this->_element; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Get the wrapped element as a universal string type. |
|
| 484 | + * |
|
| 485 | + * @throws \UnexpectedValueException If the element is not a universal string |
|
| 486 | + * |
|
| 487 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
| 488 | + */ |
|
| 489 | + public function asUniversalString(): Primitive\UniversalString |
|
| 490 | + { |
|
| 491 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 492 | + throw new \UnexpectedValueException( |
|
| 493 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 494 | + } |
|
| 495 | + return $this->_element; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Get the wrapped element as a character string type. |
|
| 500 | + * |
|
| 501 | + * @throws \UnexpectedValueException If the element is not a character string |
|
| 502 | + * |
|
| 503 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
| 504 | + */ |
|
| 505 | + public function asCharacterString(): Primitive\CharacterString |
|
| 506 | + { |
|
| 507 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 508 | + throw new \UnexpectedValueException( |
|
| 509 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 510 | + } |
|
| 511 | + return $this->_element; |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Get the wrapped element as a BMP string type. |
|
| 516 | + * |
|
| 517 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 518 | + * |
|
| 519 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
| 520 | + */ |
|
| 521 | + public function asBMPString(): Primitive\BMPString |
|
| 522 | + { |
|
| 523 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 524 | + throw new \UnexpectedValueException( |
|
| 525 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 526 | + } |
|
| 527 | + return $this->_element; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Get the wrapped element as a constructed string type. |
|
| 532 | + * |
|
| 533 | + * @throws \UnexpectedValueException If the element is not a constructed string |
|
| 534 | + * |
|
| 535 | + * @return \Sop\ASN1\Type\Constructed\ConstructedString |
|
| 536 | + */ |
|
| 537 | + public function asConstructedString(): Constructed\ConstructedString |
|
| 538 | + { |
|
| 539 | + if (!$this->_element instanceof Constructed\ConstructedString) { |
|
| 540 | + throw new \UnexpectedValueException( |
|
| 541 | + $this->_generateExceptionMessage(Element::TYPE_CONSTRUCTED_STRING)); |
|
| 542 | + } |
|
| 543 | + return $this->_element; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Get the wrapped element as any string type. |
|
| 548 | + * |
|
| 549 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 550 | + * |
|
| 551 | + * @return StringType |
|
| 552 | + */ |
|
| 553 | + public function asString(): StringType |
|
| 554 | + { |
|
| 555 | + if (!$this->_element instanceof StringType) { |
|
| 556 | + throw new \UnexpectedValueException( |
|
| 557 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 558 | + } |
|
| 559 | + return $this->_element; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Get the wrapped element as any time type. |
|
| 564 | + * |
|
| 565 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 566 | + * |
|
| 567 | + * @return TimeType |
|
| 568 | + */ |
|
| 569 | + public function asTime(): TimeType |
|
| 570 | + { |
|
| 571 | + if (!$this->_element instanceof TimeType) { |
|
| 572 | + throw new \UnexpectedValueException( |
|
| 573 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 574 | + } |
|
| 575 | + return $this->_element; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * {@inheritdoc} |
|
| 580 | + */ |
|
| 581 | + public function toDER(): string |
|
| 582 | + { |
|
| 583 | + return $this->_element->toDER(); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * {@inheritdoc} |
|
| 588 | + */ |
|
| 589 | + public function typeClass(): int |
|
| 590 | + { |
|
| 591 | + return $this->_element->typeClass(); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * {@inheritdoc} |
|
| 596 | + */ |
|
| 597 | + public function isConstructed(): bool |
|
| 598 | + { |
|
| 599 | + return $this->_element->isConstructed(); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * {@inheritdoc} |
|
| 604 | + */ |
|
| 605 | + public function tag(): int |
|
| 606 | + { |
|
| 607 | + return $this->_element->tag(); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * {@inheritdoc} |
|
| 612 | + */ |
|
| 613 | + public function isType(int $tag): bool |
|
| 614 | + { |
|
| 615 | + return $this->_element->isType($tag); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * {@inheritdoc} |
|
| 620 | + * |
|
| 621 | + * Consider using any of the `as*` accessor methods instead. |
|
| 622 | + */ |
|
| 623 | + public function expectType(int $tag): ElementBase |
|
| 624 | + { |
|
| 625 | + return $this->_element->expectType($tag); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * {@inheritdoc} |
|
| 630 | + */ |
|
| 631 | + public function isTagged(): bool |
|
| 632 | + { |
|
| 633 | + return $this->_element->isTagged(); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * {@inheritdoc} |
|
| 638 | + * |
|
| 639 | + * Consider using `asTagged()` method instead and chaining |
|
| 640 | + * with `TaggedType::asExplicit()` or `TaggedType::asImplicit()`. |
|
| 641 | + */ |
|
| 642 | + public function expectTagged(?int $tag = null): TaggedType |
|
| 643 | + { |
|
| 644 | + return $this->_element->expectTagged($tag); |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * {@inheritdoc} |
|
| 649 | + */ |
|
| 650 | + public function asElement(): Element |
|
| 651 | + { |
|
| 652 | + return $this->_element; |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * {@inheritdoc} |
|
| 657 | + */ |
|
| 658 | + public function asUnspecified(): UnspecifiedType |
|
| 659 | + { |
|
| 660 | + return $this; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Generate message for exceptions thrown by `as*` methods. |
|
| 665 | + * |
|
| 666 | + * @param int $tag Type tag of the expected element |
|
| 667 | + * |
|
| 668 | + * @return string |
|
| 669 | + */ |
|
| 670 | + private function _generateExceptionMessage(int $tag): string |
|
| 671 | + { |
|
| 672 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
| 673 | + $this->_typeDescriptorString()); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 678 | + * |
|
| 679 | + * @return string |
|
| 680 | + */ |
|
| 681 | + private function _typeDescriptorString(): string |
|
| 682 | + { |
|
| 683 | + $type_cls = $this->_element->typeClass(); |
|
| 684 | + $tag = $this->_element->tag(); |
|
| 685 | + if (Identifier::CLASS_UNIVERSAL === $type_cls) { |
|
| 686 | + return Element::tagToName($tag); |
|
| 687 | + } |
|
| 688 | + return Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
| 689 | + } |
|
| 690 | 690 | } |