@@ -22,477 +22,477 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | abstract class Element implements ElementBase |
| 24 | 24 | { |
| 25 | - // Universal type tags |
|
| 26 | - const TYPE_EOC = 0x00; |
|
| 27 | - const TYPE_BOOLEAN = 0x01; |
|
| 28 | - const TYPE_INTEGER = 0x02; |
|
| 29 | - const TYPE_BIT_STRING = 0x03; |
|
| 30 | - const TYPE_OCTET_STRING = 0x04; |
|
| 31 | - const TYPE_NULL = 0x05; |
|
| 32 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 33 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 34 | - const TYPE_EXTERNAL = 0x08; |
|
| 35 | - const TYPE_REAL = 0x09; |
|
| 36 | - const TYPE_ENUMERATED = 0x0a; |
|
| 37 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 38 | - const TYPE_UTF8_STRING = 0x0c; |
|
| 39 | - const TYPE_RELATIVE_OID = 0x0d; |
|
| 40 | - const TYPE_SEQUENCE = 0x10; |
|
| 41 | - const TYPE_SET = 0x11; |
|
| 42 | - const TYPE_NUMERIC_STRING = 0x12; |
|
| 43 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
| 44 | - const TYPE_T61_STRING = 0x14; |
|
| 45 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 46 | - const TYPE_IA5_STRING = 0x16; |
|
| 47 | - const TYPE_UTC_TIME = 0x17; |
|
| 48 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
| 49 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
| 50 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
| 51 | - const TYPE_GENERAL_STRING = 0x1b; |
|
| 52 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 53 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
| 54 | - const TYPE_BMP_STRING = 0x1e; |
|
| 25 | + // Universal type tags |
|
| 26 | + const TYPE_EOC = 0x00; |
|
| 27 | + const TYPE_BOOLEAN = 0x01; |
|
| 28 | + const TYPE_INTEGER = 0x02; |
|
| 29 | + const TYPE_BIT_STRING = 0x03; |
|
| 30 | + const TYPE_OCTET_STRING = 0x04; |
|
| 31 | + const TYPE_NULL = 0x05; |
|
| 32 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 33 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 34 | + const TYPE_EXTERNAL = 0x08; |
|
| 35 | + const TYPE_REAL = 0x09; |
|
| 36 | + const TYPE_ENUMERATED = 0x0a; |
|
| 37 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 38 | + const TYPE_UTF8_STRING = 0x0c; |
|
| 39 | + const TYPE_RELATIVE_OID = 0x0d; |
|
| 40 | + const TYPE_SEQUENCE = 0x10; |
|
| 41 | + const TYPE_SET = 0x11; |
|
| 42 | + const TYPE_NUMERIC_STRING = 0x12; |
|
| 43 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
| 44 | + const TYPE_T61_STRING = 0x14; |
|
| 45 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 46 | + const TYPE_IA5_STRING = 0x16; |
|
| 47 | + const TYPE_UTC_TIME = 0x17; |
|
| 48 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
| 49 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
| 50 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
| 51 | + const TYPE_GENERAL_STRING = 0x1b; |
|
| 52 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 53 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
| 54 | + const TYPE_BMP_STRING = 0x1e; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Mapping from universal type tag to implementation class name. |
|
| 58 | - * |
|
| 59 | - * @internal |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
| 64 | - self::TYPE_EOC => Primitive\EOC::class, |
|
| 65 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 66 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 67 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 68 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 69 | - self::TYPE_NULL => Primitive\NullType::class, |
|
| 70 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 71 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 72 | - self::TYPE_REAL => Primitive\Real::class, |
|
| 73 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 74 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 75 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 76 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 77 | - self::TYPE_SET => Constructed\Set::class, |
|
| 78 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 79 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 80 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 81 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 82 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 83 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 84 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 85 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 86 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 87 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 88 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 89 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 90 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 91 | - /* @formatter:on */ |
|
| 92 | - ]; |
|
| 56 | + /** |
|
| 57 | + * Mapping from universal type tag to implementation class name. |
|
| 58 | + * |
|
| 59 | + * @internal |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
| 64 | + self::TYPE_EOC => Primitive\EOC::class, |
|
| 65 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 66 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 67 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 68 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 69 | + self::TYPE_NULL => Primitive\NullType::class, |
|
| 70 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 71 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 72 | + self::TYPE_REAL => Primitive\Real::class, |
|
| 73 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 74 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 75 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 76 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 77 | + self::TYPE_SET => Constructed\Set::class, |
|
| 78 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 79 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 80 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 81 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 82 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 83 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 84 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 85 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 86 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 87 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 88 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 89 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 90 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 91 | + /* @formatter:on */ |
|
| 92 | + ]; |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Pseudotype for all string types. |
|
| 96 | - * |
|
| 97 | - * May be used as an expectation parameter. |
|
| 98 | - * |
|
| 99 | - * @var int |
|
| 100 | - */ |
|
| 101 | - const TYPE_STRING = -1; |
|
| 94 | + /** |
|
| 95 | + * Pseudotype for all string types. |
|
| 96 | + * |
|
| 97 | + * May be used as an expectation parameter. |
|
| 98 | + * |
|
| 99 | + * @var int |
|
| 100 | + */ |
|
| 101 | + const TYPE_STRING = -1; |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Pseudotype for all time types. |
|
| 105 | - * |
|
| 106 | - * May be used as an expectation parameter. |
|
| 107 | - * |
|
| 108 | - * @var int |
|
| 109 | - */ |
|
| 110 | - const TYPE_TIME = -2; |
|
| 103 | + /** |
|
| 104 | + * Pseudotype for all time types. |
|
| 105 | + * |
|
| 106 | + * May be used as an expectation parameter. |
|
| 107 | + * |
|
| 108 | + * @var int |
|
| 109 | + */ |
|
| 110 | + const TYPE_TIME = -2; |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Mapping from universal type tag to human readable name. |
|
| 114 | - * |
|
| 115 | - * @internal |
|
| 116 | - * |
|
| 117 | - * @var array |
|
| 118 | - */ |
|
| 119 | - const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
| 120 | - self::TYPE_EOC => "EOC", |
|
| 121 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 122 | - self::TYPE_INTEGER => "INTEGER", |
|
| 123 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
| 124 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 125 | - self::TYPE_NULL => "NULL", |
|
| 126 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 127 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 128 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 129 | - self::TYPE_REAL => "REAL", |
|
| 130 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 131 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 132 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
| 133 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 134 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 135 | - self::TYPE_SET => "SET", |
|
| 136 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 137 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 138 | - self::TYPE_T61_STRING => "T61String", |
|
| 139 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 140 | - self::TYPE_IA5_STRING => "IA5String", |
|
| 141 | - self::TYPE_UTC_TIME => "UTCTime", |
|
| 142 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 143 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 144 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 145 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 146 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 147 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 148 | - self::TYPE_BMP_STRING => "BMPString", |
|
| 149 | - self::TYPE_STRING => "Any String", |
|
| 150 | - self::TYPE_TIME => "Any Time" |
|
| 151 | - /* @formatter:on */ |
|
| 152 | - ]; |
|
| 112 | + /** |
|
| 113 | + * Mapping from universal type tag to human readable name. |
|
| 114 | + * |
|
| 115 | + * @internal |
|
| 116 | + * |
|
| 117 | + * @var array |
|
| 118 | + */ |
|
| 119 | + const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
| 120 | + self::TYPE_EOC => "EOC", |
|
| 121 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 122 | + self::TYPE_INTEGER => "INTEGER", |
|
| 123 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
| 124 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 125 | + self::TYPE_NULL => "NULL", |
|
| 126 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 127 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 128 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 129 | + self::TYPE_REAL => "REAL", |
|
| 130 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 131 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 132 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
| 133 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 134 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 135 | + self::TYPE_SET => "SET", |
|
| 136 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 137 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 138 | + self::TYPE_T61_STRING => "T61String", |
|
| 139 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 140 | + self::TYPE_IA5_STRING => "IA5String", |
|
| 141 | + self::TYPE_UTC_TIME => "UTCTime", |
|
| 142 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 143 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 144 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 145 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 146 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 147 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 148 | + self::TYPE_BMP_STRING => "BMPString", |
|
| 149 | + self::TYPE_STRING => "Any String", |
|
| 150 | + self::TYPE_TIME => "Any Time" |
|
| 151 | + /* @formatter:on */ |
|
| 152 | + ]; |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Element's type tag. |
|
| 156 | - * |
|
| 157 | - * @var int |
|
| 158 | - */ |
|
| 159 | - protected $_typeTag; |
|
| 154 | + /** |
|
| 155 | + * Element's type tag. |
|
| 156 | + * |
|
| 157 | + * @var int |
|
| 158 | + */ |
|
| 159 | + protected $_typeTag; |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Whether type shall be encoded with indefinite length. |
|
| 163 | - * |
|
| 164 | - * @var bool |
|
| 165 | - */ |
|
| 166 | - protected $_indefiniteLength = false; |
|
| 161 | + /** |
|
| 162 | + * Whether type shall be encoded with indefinite length. |
|
| 163 | + * |
|
| 164 | + * @var bool |
|
| 165 | + */ |
|
| 166 | + protected $_indefiniteLength = false; |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * |
|
| 170 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 171 | - * @return int |
|
| 172 | - */ |
|
| 173 | - abstract public function typeClass(): int; |
|
| 168 | + /** |
|
| 169 | + * |
|
| 170 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 171 | + * @return int |
|
| 172 | + */ |
|
| 173 | + abstract public function typeClass(): int; |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * |
|
| 177 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - abstract public function isConstructed(): bool; |
|
| 175 | + /** |
|
| 176 | + * |
|
| 177 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + abstract public function isConstructed(): bool; |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get the content encoded in DER. |
|
| 184 | - * |
|
| 185 | - * Returns the DER encoded content without identifier and length header |
|
| 186 | - * octets. |
|
| 187 | - * |
|
| 188 | - * @return string |
|
| 189 | - */ |
|
| 190 | - abstract protected function _encodedContentDER(): string; |
|
| 182 | + /** |
|
| 183 | + * Get the content encoded in DER. |
|
| 184 | + * |
|
| 185 | + * Returns the DER encoded content without identifier and length header |
|
| 186 | + * octets. |
|
| 187 | + * |
|
| 188 | + * @return string |
|
| 189 | + */ |
|
| 190 | + abstract protected function _encodedContentDER(): string; |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Decode type-specific element from DER. |
|
| 194 | - * |
|
| 195 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 196 | - * @param string $data DER data |
|
| 197 | - * @param int $offset Offset in data to the next byte after identifier |
|
| 198 | - * @throws DecodeException If decoding fails |
|
| 199 | - * @return ElementBase |
|
| 200 | - */ |
|
| 201 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 202 | - string $data, int &$offset): ElementBase |
|
| 203 | - { |
|
| 204 | - throw new \BadMethodCallException( |
|
| 205 | - __METHOD__ . " must be implemented in derived class."); |
|
| 206 | - } |
|
| 192 | + /** |
|
| 193 | + * Decode type-specific element from DER. |
|
| 194 | + * |
|
| 195 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 196 | + * @param string $data DER data |
|
| 197 | + * @param int $offset Offset in data to the next byte after identifier |
|
| 198 | + * @throws DecodeException If decoding fails |
|
| 199 | + * @return ElementBase |
|
| 200 | + */ |
|
| 201 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 202 | + string $data, int &$offset): ElementBase |
|
| 203 | + { |
|
| 204 | + throw new \BadMethodCallException( |
|
| 205 | + __METHOD__ . " must be implemented in derived class."); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Decode element from DER data. |
|
| 210 | - * |
|
| 211 | - * @param string $data DER encoded data |
|
| 212 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 213 | - * into the data where to start parsing. Variable is updated to |
|
| 214 | - * the offset next to the parsed element. If null, start from offset |
|
| 215 | - * 0. |
|
| 216 | - * @throws DecodeException If decoding fails |
|
| 217 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
| 218 | - * type, but decoding yields another type |
|
| 219 | - * @return ElementBase |
|
| 220 | - */ |
|
| 221 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 222 | - { |
|
| 223 | - $idx = $offset ?? 0; |
|
| 224 | - // decode identifier |
|
| 225 | - $identifier = Identifier::fromDER($data, $idx); |
|
| 226 | - // determine class that implements type specific decoding |
|
| 227 | - $cls = self::_determineImplClass($identifier); |
|
| 228 | - try { |
|
| 229 | - // decode remaining element |
|
| 230 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 231 | - } catch (\LogicException $e) { |
|
| 232 | - // rethrow as a RuntimeException for unified exception handling |
|
| 233 | - throw new DecodeException( |
|
| 234 | - sprintf("Error while decoding %s.", |
|
| 235 | - self::tagToName($identifier->intTag())), 0, $e); |
|
| 236 | - } |
|
| 237 | - // if called in the context of a concrete class, check |
|
| 238 | - // that decoded type matches the type of a calling class |
|
| 239 | - $called_class = get_called_class(); |
|
| 240 | - if (self::class != $called_class) { |
|
| 241 | - if (!$element instanceof $called_class) { |
|
| 242 | - throw new \UnexpectedValueException( |
|
| 243 | - sprintf("%s expected, got %s.", $called_class, |
|
| 244 | - get_class($element))); |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - // update offset for the caller |
|
| 248 | - if (isset($offset)) { |
|
| 249 | - $offset = $idx; |
|
| 250 | - } |
|
| 251 | - return $element; |
|
| 252 | - } |
|
| 208 | + /** |
|
| 209 | + * Decode element from DER data. |
|
| 210 | + * |
|
| 211 | + * @param string $data DER encoded data |
|
| 212 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 213 | + * into the data where to start parsing. Variable is updated to |
|
| 214 | + * the offset next to the parsed element. If null, start from offset |
|
| 215 | + * 0. |
|
| 216 | + * @throws DecodeException If decoding fails |
|
| 217 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
| 218 | + * type, but decoding yields another type |
|
| 219 | + * @return ElementBase |
|
| 220 | + */ |
|
| 221 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 222 | + { |
|
| 223 | + $idx = $offset ?? 0; |
|
| 224 | + // decode identifier |
|
| 225 | + $identifier = Identifier::fromDER($data, $idx); |
|
| 226 | + // determine class that implements type specific decoding |
|
| 227 | + $cls = self::_determineImplClass($identifier); |
|
| 228 | + try { |
|
| 229 | + // decode remaining element |
|
| 230 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 231 | + } catch (\LogicException $e) { |
|
| 232 | + // rethrow as a RuntimeException for unified exception handling |
|
| 233 | + throw new DecodeException( |
|
| 234 | + sprintf("Error while decoding %s.", |
|
| 235 | + self::tagToName($identifier->intTag())), 0, $e); |
|
| 236 | + } |
|
| 237 | + // if called in the context of a concrete class, check |
|
| 238 | + // that decoded type matches the type of a calling class |
|
| 239 | + $called_class = get_called_class(); |
|
| 240 | + if (self::class != $called_class) { |
|
| 241 | + if (!$element instanceof $called_class) { |
|
| 242 | + throw new \UnexpectedValueException( |
|
| 243 | + sprintf("%s expected, got %s.", $called_class, |
|
| 244 | + get_class($element))); |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + // update offset for the caller |
|
| 248 | + if (isset($offset)) { |
|
| 249 | + $offset = $idx; |
|
| 250 | + } |
|
| 251 | + return $element; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * |
|
| 256 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 257 | - * @return string |
|
| 258 | - */ |
|
| 259 | - public function toDER(): string |
|
| 260 | - { |
|
| 261 | - $identifier = new Identifier($this->typeClass(), |
|
| 262 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 263 | - $this->_typeTag); |
|
| 264 | - $content = $this->_encodedContentDER(); |
|
| 265 | - if ($this->_indefiniteLength) { |
|
| 266 | - $length = new Length(0, true); |
|
| 267 | - $eoc = new Primitive\EOC(); |
|
| 268 | - return $identifier->toDER() . $length->toDER() . $content . |
|
| 269 | - $eoc->toDER(); |
|
| 270 | - } else { |
|
| 271 | - $length = new Length(strlen($content)); |
|
| 272 | - return $identifier->toDER() . $length->toDER() . $content; |
|
| 273 | - } |
|
| 274 | - } |
|
| 254 | + /** |
|
| 255 | + * |
|
| 256 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 257 | + * @return string |
|
| 258 | + */ |
|
| 259 | + public function toDER(): string |
|
| 260 | + { |
|
| 261 | + $identifier = new Identifier($this->typeClass(), |
|
| 262 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 263 | + $this->_typeTag); |
|
| 264 | + $content = $this->_encodedContentDER(); |
|
| 265 | + if ($this->_indefiniteLength) { |
|
| 266 | + $length = new Length(0, true); |
|
| 267 | + $eoc = new Primitive\EOC(); |
|
| 268 | + return $identifier->toDER() . $length->toDER() . $content . |
|
| 269 | + $eoc->toDER(); |
|
| 270 | + } else { |
|
| 271 | + $length = new Length(strlen($content)); |
|
| 272 | + return $identifier->toDER() . $length->toDER() . $content; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - /** |
|
| 277 | - * |
|
| 278 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 279 | - * @return int |
|
| 280 | - */ |
|
| 281 | - public function tag(): int |
|
| 282 | - { |
|
| 283 | - return $this->_typeTag; |
|
| 284 | - } |
|
| 276 | + /** |
|
| 277 | + * |
|
| 278 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 279 | + * @return int |
|
| 280 | + */ |
|
| 281 | + public function tag(): int |
|
| 282 | + { |
|
| 283 | + return $this->_typeTag; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * |
|
| 288 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 289 | - * @return bool |
|
| 290 | - */ |
|
| 291 | - public function isType(int $tag): bool |
|
| 292 | - { |
|
| 293 | - // if element is context specific |
|
| 294 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 295 | - return false; |
|
| 296 | - } |
|
| 297 | - // negative tags identify an abstract pseudotype |
|
| 298 | - if ($tag < 0) { |
|
| 299 | - return $this->_isPseudoType($tag); |
|
| 300 | - } |
|
| 301 | - return $this->_isConcreteType($tag); |
|
| 302 | - } |
|
| 286 | + /** |
|
| 287 | + * |
|
| 288 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 289 | + * @return bool |
|
| 290 | + */ |
|
| 291 | + public function isType(int $tag): bool |
|
| 292 | + { |
|
| 293 | + // if element is context specific |
|
| 294 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 295 | + return false; |
|
| 296 | + } |
|
| 297 | + // negative tags identify an abstract pseudotype |
|
| 298 | + if ($tag < 0) { |
|
| 299 | + return $this->_isPseudoType($tag); |
|
| 300 | + } |
|
| 301 | + return $this->_isConcreteType($tag); |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - /** |
|
| 305 | - * |
|
| 306 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 307 | - * @return ElementBase |
|
| 308 | - */ |
|
| 309 | - public function expectType(int $tag): ElementBase |
|
| 310 | - { |
|
| 311 | - if (!$this->isType($tag)) { |
|
| 312 | - throw new \UnexpectedValueException( |
|
| 313 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 314 | - $this->_typeDescriptorString())); |
|
| 315 | - } |
|
| 316 | - return $this; |
|
| 317 | - } |
|
| 304 | + /** |
|
| 305 | + * |
|
| 306 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 307 | + * @return ElementBase |
|
| 308 | + */ |
|
| 309 | + public function expectType(int $tag): ElementBase |
|
| 310 | + { |
|
| 311 | + if (!$this->isType($tag)) { |
|
| 312 | + throw new \UnexpectedValueException( |
|
| 313 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 314 | + $this->_typeDescriptorString())); |
|
| 315 | + } |
|
| 316 | + return $this; |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - /** |
|
| 320 | - * Check whether the element is a concrete type of a given tag. |
|
| 321 | - * |
|
| 322 | - * @param int $tag |
|
| 323 | - * @return bool |
|
| 324 | - */ |
|
| 325 | - private function _isConcreteType(int $tag): bool |
|
| 326 | - { |
|
| 327 | - // if tag doesn't match |
|
| 328 | - if ($this->tag() != $tag) { |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - // if type is universal check that instance is of a correct class |
|
| 332 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 333 | - $cls = self::_determineUniversalImplClass($tag); |
|
| 334 | - if (!$this instanceof $cls) { |
|
| 335 | - return false; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - return true; |
|
| 339 | - } |
|
| 319 | + /** |
|
| 320 | + * Check whether the element is a concrete type of a given tag. |
|
| 321 | + * |
|
| 322 | + * @param int $tag |
|
| 323 | + * @return bool |
|
| 324 | + */ |
|
| 325 | + private function _isConcreteType(int $tag): bool |
|
| 326 | + { |
|
| 327 | + // if tag doesn't match |
|
| 328 | + if ($this->tag() != $tag) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + // if type is universal check that instance is of a correct class |
|
| 332 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 333 | + $cls = self::_determineUniversalImplClass($tag); |
|
| 334 | + if (!$this instanceof $cls) { |
|
| 335 | + return false; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + return true; |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | - /** |
|
| 342 | - * Check whether the element is a pseudotype. |
|
| 343 | - * |
|
| 344 | - * @param int $tag |
|
| 345 | - * @return bool |
|
| 346 | - */ |
|
| 347 | - private function _isPseudoType(int $tag): bool |
|
| 348 | - { |
|
| 349 | - switch ($tag) { |
|
| 350 | - case self::TYPE_STRING: |
|
| 351 | - return $this instanceof StringType; |
|
| 352 | - case self::TYPE_TIME: |
|
| 353 | - return $this instanceof TimeType; |
|
| 354 | - } |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 341 | + /** |
|
| 342 | + * Check whether the element is a pseudotype. |
|
| 343 | + * |
|
| 344 | + * @param int $tag |
|
| 345 | + * @return bool |
|
| 346 | + */ |
|
| 347 | + private function _isPseudoType(int $tag): bool |
|
| 348 | + { |
|
| 349 | + switch ($tag) { |
|
| 350 | + case self::TYPE_STRING: |
|
| 351 | + return $this instanceof StringType; |
|
| 352 | + case self::TYPE_TIME: |
|
| 353 | + return $this instanceof TimeType; |
|
| 354 | + } |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * |
|
| 360 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 361 | - * @return bool |
|
| 362 | - */ |
|
| 363 | - public function isTagged(): bool |
|
| 364 | - { |
|
| 365 | - return $this instanceof TaggedType; |
|
| 366 | - } |
|
| 358 | + /** |
|
| 359 | + * |
|
| 360 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 361 | + * @return bool |
|
| 362 | + */ |
|
| 363 | + public function isTagged(): bool |
|
| 364 | + { |
|
| 365 | + return $this instanceof TaggedType; |
|
| 366 | + } |
|
| 367 | 367 | |
| 368 | - /** |
|
| 369 | - * |
|
| 370 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 371 | - * @return TaggedType |
|
| 372 | - */ |
|
| 373 | - public function expectTagged($tag = null): TaggedType |
|
| 374 | - { |
|
| 375 | - if (!$this->isTagged()) { |
|
| 376 | - throw new \UnexpectedValueException( |
|
| 377 | - sprintf("Context specific element expected, got %s.", |
|
| 378 | - Identifier::classToName($this->typeClass()))); |
|
| 379 | - } |
|
| 380 | - if (isset($tag) && $this->tag() != $tag) { |
|
| 381 | - throw new \UnexpectedValueException( |
|
| 382 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 383 | - } |
|
| 384 | - return $this; |
|
| 385 | - } |
|
| 368 | + /** |
|
| 369 | + * |
|
| 370 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 371 | + * @return TaggedType |
|
| 372 | + */ |
|
| 373 | + public function expectTagged($tag = null): TaggedType |
|
| 374 | + { |
|
| 375 | + if (!$this->isTagged()) { |
|
| 376 | + throw new \UnexpectedValueException( |
|
| 377 | + sprintf("Context specific element expected, got %s.", |
|
| 378 | + Identifier::classToName($this->typeClass()))); |
|
| 379 | + } |
|
| 380 | + if (isset($tag) && $this->tag() != $tag) { |
|
| 381 | + throw new \UnexpectedValueException( |
|
| 382 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 383 | + } |
|
| 384 | + return $this; |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | - /** |
|
| 388 | - * Whether element has indefinite length. |
|
| 389 | - * |
|
| 390 | - * @return bool |
|
| 391 | - */ |
|
| 392 | - public function hasIndefiniteLength(): bool |
|
| 393 | - { |
|
| 394 | - return $this->_indefiniteLength; |
|
| 395 | - } |
|
| 387 | + /** |
|
| 388 | + * Whether element has indefinite length. |
|
| 389 | + * |
|
| 390 | + * @return bool |
|
| 391 | + */ |
|
| 392 | + public function hasIndefiniteLength(): bool |
|
| 393 | + { |
|
| 394 | + return $this->_indefiniteLength; |
|
| 395 | + } |
|
| 396 | 396 | |
| 397 | - /** |
|
| 398 | - * Get self with indefinite length encoding set. |
|
| 399 | - * |
|
| 400 | - * @param bool $indefinite True for indefinite length, false for definite |
|
| 401 | - * length |
|
| 402 | - * @return self |
|
| 403 | - */ |
|
| 404 | - public function withIndefiniteLength(bool $indefinite = true): self |
|
| 405 | - { |
|
| 406 | - $obj = clone $this; |
|
| 407 | - $obj->_indefiniteLength = $indefinite; |
|
| 408 | - return $obj; |
|
| 409 | - } |
|
| 397 | + /** |
|
| 398 | + * Get self with indefinite length encoding set. |
|
| 399 | + * |
|
| 400 | + * @param bool $indefinite True for indefinite length, false for definite |
|
| 401 | + * length |
|
| 402 | + * @return self |
|
| 403 | + */ |
|
| 404 | + public function withIndefiniteLength(bool $indefinite = true): self |
|
| 405 | + { |
|
| 406 | + $obj = clone $this; |
|
| 407 | + $obj->_indefiniteLength = $indefinite; |
|
| 408 | + return $obj; |
|
| 409 | + } |
|
| 410 | 410 | |
| 411 | - /** |
|
| 412 | - * |
|
| 413 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 414 | - * @return Element |
|
| 415 | - */ |
|
| 416 | - final public function asElement(): Element |
|
| 417 | - { |
|
| 418 | - return $this; |
|
| 419 | - } |
|
| 411 | + /** |
|
| 412 | + * |
|
| 413 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 414 | + * @return Element |
|
| 415 | + */ |
|
| 416 | + final public function asElement(): Element |
|
| 417 | + { |
|
| 418 | + return $this; |
|
| 419 | + } |
|
| 420 | 420 | |
| 421 | - /** |
|
| 422 | - * Get element decorated with UnspecifiedType object. |
|
| 423 | - * |
|
| 424 | - * @return UnspecifiedType |
|
| 425 | - */ |
|
| 426 | - public function asUnspecified(): UnspecifiedType |
|
| 427 | - { |
|
| 428 | - return new UnspecifiedType($this); |
|
| 429 | - } |
|
| 421 | + /** |
|
| 422 | + * Get element decorated with UnspecifiedType object. |
|
| 423 | + * |
|
| 424 | + * @return UnspecifiedType |
|
| 425 | + */ |
|
| 426 | + public function asUnspecified(): UnspecifiedType |
|
| 427 | + { |
|
| 428 | + return new UnspecifiedType($this); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - /** |
|
| 432 | - * Determine the class that implements the type. |
|
| 433 | - * |
|
| 434 | - * @param Identifier $identifier |
|
| 435 | - * @return string Class name |
|
| 436 | - */ |
|
| 437 | - protected static function _determineImplClass(Identifier $identifier): string |
|
| 438 | - { |
|
| 439 | - switch ($identifier->typeClass()) { |
|
| 440 | - case Identifier::CLASS_UNIVERSAL: |
|
| 441 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 442 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 443 | - return ContextSpecificType::class; |
|
| 444 | - case Identifier::CLASS_APPLICATION: |
|
| 445 | - return ApplicationType::class; |
|
| 446 | - case Identifier::CLASS_PRIVATE: |
|
| 447 | - return PrivateType::class; |
|
| 448 | - } |
|
| 449 | - throw new \UnexpectedValueException( |
|
| 450 | - sprintf("%s %d not implemented.", |
|
| 451 | - Identifier::classToName($identifier->typeClass()), |
|
| 452 | - $identifier->tag())); |
|
| 453 | - } |
|
| 431 | + /** |
|
| 432 | + * Determine the class that implements the type. |
|
| 433 | + * |
|
| 434 | + * @param Identifier $identifier |
|
| 435 | + * @return string Class name |
|
| 436 | + */ |
|
| 437 | + protected static function _determineImplClass(Identifier $identifier): string |
|
| 438 | + { |
|
| 439 | + switch ($identifier->typeClass()) { |
|
| 440 | + case Identifier::CLASS_UNIVERSAL: |
|
| 441 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 442 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 443 | + return ContextSpecificType::class; |
|
| 444 | + case Identifier::CLASS_APPLICATION: |
|
| 445 | + return ApplicationType::class; |
|
| 446 | + case Identifier::CLASS_PRIVATE: |
|
| 447 | + return PrivateType::class; |
|
| 448 | + } |
|
| 449 | + throw new \UnexpectedValueException( |
|
| 450 | + sprintf("%s %d not implemented.", |
|
| 451 | + Identifier::classToName($identifier->typeClass()), |
|
| 452 | + $identifier->tag())); |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | - /** |
|
| 456 | - * Determine the class that implements an universal type of the given tag. |
|
| 457 | - * |
|
| 458 | - * @param int $tag |
|
| 459 | - * @throws \UnexpectedValueException |
|
| 460 | - * @return string Class name |
|
| 461 | - */ |
|
| 462 | - protected static function _determineUniversalImplClass(int $tag): string |
|
| 463 | - { |
|
| 464 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 465 | - throw new \UnexpectedValueException( |
|
| 466 | - "Universal tag $tag not implemented."); |
|
| 467 | - } |
|
| 468 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 469 | - } |
|
| 455 | + /** |
|
| 456 | + * Determine the class that implements an universal type of the given tag. |
|
| 457 | + * |
|
| 458 | + * @param int $tag |
|
| 459 | + * @throws \UnexpectedValueException |
|
| 460 | + * @return string Class name |
|
| 461 | + */ |
|
| 462 | + protected static function _determineUniversalImplClass(int $tag): string |
|
| 463 | + { |
|
| 464 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 465 | + throw new \UnexpectedValueException( |
|
| 466 | + "Universal tag $tag not implemented."); |
|
| 467 | + } |
|
| 468 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | - /** |
|
| 472 | - * Get textual description of the type for debugging purposes. |
|
| 473 | - * |
|
| 474 | - * @return string |
|
| 475 | - */ |
|
| 476 | - protected function _typeDescriptorString(): string |
|
| 477 | - { |
|
| 478 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 479 | - return self::tagToName($this->_typeTag); |
|
| 480 | - } |
|
| 481 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 482 | - $this->_typeTag); |
|
| 483 | - } |
|
| 471 | + /** |
|
| 472 | + * Get textual description of the type for debugging purposes. |
|
| 473 | + * |
|
| 474 | + * @return string |
|
| 475 | + */ |
|
| 476 | + protected function _typeDescriptorString(): string |
|
| 477 | + { |
|
| 478 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 479 | + return self::tagToName($this->_typeTag); |
|
| 480 | + } |
|
| 481 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 482 | + $this->_typeTag); |
|
| 483 | + } |
|
| 484 | 484 | |
| 485 | - /** |
|
| 486 | - * Get human readable name for an universal tag. |
|
| 487 | - * |
|
| 488 | - * @param int $tag |
|
| 489 | - * @return string |
|
| 490 | - */ |
|
| 491 | - public static function tagToName(int $tag): string |
|
| 492 | - { |
|
| 493 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 494 | - return "TAG $tag"; |
|
| 495 | - } |
|
| 496 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 497 | - } |
|
| 485 | + /** |
|
| 486 | + * Get human readable name for an universal tag. |
|
| 487 | + * |
|
| 488 | + * @param int $tag |
|
| 489 | + * @return string |
|
| 490 | + */ |
|
| 491 | + public static function tagToName(int $tag): string |
|
| 492 | + { |
|
| 493 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 494 | + return "TAG $tag"; |
|
| 495 | + } |
|
| 496 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 497 | + } |
|
| 498 | 498 | } |
@@ -347,10 +347,10 @@ discard block |
||
| 347 | 347 | private function _isPseudoType(int $tag): bool |
| 348 | 348 | { |
| 349 | 349 | switch ($tag) { |
| 350 | - case self::TYPE_STRING: |
|
| 351 | - return $this instanceof StringType; |
|
| 352 | - case self::TYPE_TIME: |
|
| 353 | - return $this instanceof TimeType; |
|
| 350 | + case self::TYPE_STRING: |
|
| 351 | + return $this instanceof StringType; |
|
| 352 | + case self::TYPE_TIME: |
|
| 353 | + return $this instanceof TimeType; |
|
| 354 | 354 | } |
| 355 | 355 | return false; |
| 356 | 356 | } |
@@ -437,14 +437,14 @@ discard block |
||
| 437 | 437 | protected static function _determineImplClass(Identifier $identifier): string |
| 438 | 438 | { |
| 439 | 439 | switch ($identifier->typeClass()) { |
| 440 | - case Identifier::CLASS_UNIVERSAL: |
|
| 441 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 442 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 443 | - return ContextSpecificType::class; |
|
| 444 | - case Identifier::CLASS_APPLICATION: |
|
| 445 | - return ApplicationType::class; |
|
| 446 | - case Identifier::CLASS_PRIVATE: |
|
| 447 | - return PrivateType::class; |
|
| 440 | + case Identifier::CLASS_UNIVERSAL: |
|
| 441 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 442 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 443 | + return ContextSpecificType::class; |
|
| 444 | + case Identifier::CLASS_APPLICATION: |
|
| 445 | + return ApplicationType::class; |
|
| 446 | + case Identifier::CLASS_PRIVATE: |
|
| 447 | + return PrivateType::class; |
|
| 448 | 448 | } |
| 449 | 449 | throw new \UnexpectedValueException( |
| 450 | 450 | sprintf("%s %d not implemented.", |
@@ -13,71 +13,71 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class TaggedType extends Element |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Check whether element supports explicit tagging. |
|
| 18 | - * |
|
| 19 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 20 | - * @throws \UnexpectedValueException If expectation fails |
|
| 21 | - * @return ExplicitTagging |
|
| 22 | - */ |
|
| 23 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
| 24 | - { |
|
| 25 | - $el = $this; |
|
| 26 | - if (!$el instanceof ExplicitTagging) { |
|
| 27 | - throw new \UnexpectedValueException( |
|
| 28 | - "Element doesn't implement explicit tagging."); |
|
| 29 | - } |
|
| 30 | - if (isset($expectedTag)) { |
|
| 31 | - $el->expectTagged($expectedTag); |
|
| 32 | - } |
|
| 33 | - return $el; |
|
| 34 | - } |
|
| 16 | + /** |
|
| 17 | + * Check whether element supports explicit tagging. |
|
| 18 | + * |
|
| 19 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 20 | + * @throws \UnexpectedValueException If expectation fails |
|
| 21 | + * @return ExplicitTagging |
|
| 22 | + */ |
|
| 23 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
| 24 | + { |
|
| 25 | + $el = $this; |
|
| 26 | + if (!$el instanceof ExplicitTagging) { |
|
| 27 | + throw new \UnexpectedValueException( |
|
| 28 | + "Element doesn't implement explicit tagging."); |
|
| 29 | + } |
|
| 30 | + if (isset($expectedTag)) { |
|
| 31 | + $el->expectTagged($expectedTag); |
|
| 32 | + } |
|
| 33 | + return $el; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get the wrapped inner element employing explicit tagging. |
|
| 38 | - * |
|
| 39 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 40 | - * @throws \UnexpectedValueException If expectation fails |
|
| 41 | - * @return UnspecifiedType |
|
| 42 | - */ |
|
| 43 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
| 44 | - { |
|
| 45 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
| 46 | - } |
|
| 36 | + /** |
|
| 37 | + * Get the wrapped inner element employing explicit tagging. |
|
| 38 | + * |
|
| 39 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 40 | + * @throws \UnexpectedValueException If expectation fails |
|
| 41 | + * @return UnspecifiedType |
|
| 42 | + */ |
|
| 43 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
| 44 | + { |
|
| 45 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Check whether element supports implicit tagging. |
|
| 50 | - * |
|
| 51 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 52 | - * @throws \UnexpectedValueException If expectation fails |
|
| 53 | - * @return ImplicitTagging |
|
| 54 | - */ |
|
| 55 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
| 56 | - { |
|
| 57 | - $el = $this; |
|
| 58 | - if (!$el instanceof ImplicitTagging) { |
|
| 59 | - throw new \UnexpectedValueException( |
|
| 60 | - "Element doesn't implement implicit tagging."); |
|
| 61 | - } |
|
| 62 | - if (isset($expectedTag)) { |
|
| 63 | - $el->expectTagged($expectedTag); |
|
| 64 | - } |
|
| 65 | - return $el; |
|
| 66 | - } |
|
| 48 | + /** |
|
| 49 | + * Check whether element supports implicit tagging. |
|
| 50 | + * |
|
| 51 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 52 | + * @throws \UnexpectedValueException If expectation fails |
|
| 53 | + * @return ImplicitTagging |
|
| 54 | + */ |
|
| 55 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
| 56 | + { |
|
| 57 | + $el = $this; |
|
| 58 | + if (!$el instanceof ImplicitTagging) { |
|
| 59 | + throw new \UnexpectedValueException( |
|
| 60 | + "Element doesn't implement implicit tagging."); |
|
| 61 | + } |
|
| 62 | + if (isset($expectedTag)) { |
|
| 63 | + $el->expectTagged($expectedTag); |
|
| 64 | + } |
|
| 65 | + return $el; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get the wrapped inner element employing implicit tagging. |
|
| 70 | - * |
|
| 71 | - * @param int $tag Type tag of the inner element |
|
| 72 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 73 | - * @param int $expectedClass Optional inner type class expectation |
|
| 74 | - * @throws \UnexpectedValueException If expectation fails |
|
| 75 | - * @return UnspecifiedType |
|
| 76 | - */ |
|
| 77 | - public function asImplicit(int $tag, $expectedTag = null, |
|
| 78 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 79 | - { |
|
| 80 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 81 | - $expectedClass); |
|
| 82 | - } |
|
| 68 | + /** |
|
| 69 | + * Get the wrapped inner element employing implicit tagging. |
|
| 70 | + * |
|
| 71 | + * @param int $tag Type tag of the inner element |
|
| 72 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 73 | + * @param int $expectedClass Optional inner type class expectation |
|
| 74 | + * @throws \UnexpectedValueException If expectation fails |
|
| 75 | + * @return UnspecifiedType |
|
| 76 | + */ |
|
| 77 | + public function asImplicit(int $tag, $expectedTag = null, |
|
| 78 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 79 | + { |
|
| 80 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 81 | + $expectedClass); |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Type\Tagged; |
| 6 | 6 | |