@@ -21,440 +21,440 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class Element implements ElementBase |
| 23 | 23 | { |
| 24 | - // Universal type tags |
|
| 25 | - const TYPE_EOC = 0x00; |
|
| 26 | - const TYPE_BOOLEAN = 0x01; |
|
| 27 | - const TYPE_INTEGER = 0x02; |
|
| 28 | - const TYPE_BIT_STRING = 0x03; |
|
| 29 | - const TYPE_OCTET_STRING = 0x04; |
|
| 30 | - const TYPE_NULL = 0x05; |
|
| 31 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 32 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 33 | - const TYPE_EXTERNAL = 0x08; |
|
| 34 | - const TYPE_REAL = 0x09; |
|
| 35 | - const TYPE_ENUMERATED = 0x0a; |
|
| 36 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 37 | - const TYPE_UTF8_STRING = 0x0c; |
|
| 38 | - const TYPE_RELATIVE_OID = 0x0d; |
|
| 39 | - const TYPE_SEQUENCE = 0x10; |
|
| 40 | - const TYPE_SET = 0x11; |
|
| 41 | - const TYPE_NUMERIC_STRING = 0x12; |
|
| 42 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
| 43 | - const TYPE_T61_STRING = 0x14; |
|
| 44 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 45 | - const TYPE_IA5_STRING = 0x16; |
|
| 46 | - const TYPE_UTC_TIME = 0x17; |
|
| 47 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
| 48 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
| 49 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
| 50 | - const TYPE_GENERAL_STRING = 0x1b; |
|
| 51 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 52 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
| 53 | - const TYPE_BMP_STRING = 0x1e; |
|
| 24 | + // Universal type tags |
|
| 25 | + const TYPE_EOC = 0x00; |
|
| 26 | + const TYPE_BOOLEAN = 0x01; |
|
| 27 | + const TYPE_INTEGER = 0x02; |
|
| 28 | + const TYPE_BIT_STRING = 0x03; |
|
| 29 | + const TYPE_OCTET_STRING = 0x04; |
|
| 30 | + const TYPE_NULL = 0x05; |
|
| 31 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 32 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 33 | + const TYPE_EXTERNAL = 0x08; |
|
| 34 | + const TYPE_REAL = 0x09; |
|
| 35 | + const TYPE_ENUMERATED = 0x0a; |
|
| 36 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 37 | + const TYPE_UTF8_STRING = 0x0c; |
|
| 38 | + const TYPE_RELATIVE_OID = 0x0d; |
|
| 39 | + const TYPE_SEQUENCE = 0x10; |
|
| 40 | + const TYPE_SET = 0x11; |
|
| 41 | + const TYPE_NUMERIC_STRING = 0x12; |
|
| 42 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
| 43 | + const TYPE_T61_STRING = 0x14; |
|
| 44 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 45 | + const TYPE_IA5_STRING = 0x16; |
|
| 46 | + const TYPE_UTC_TIME = 0x17; |
|
| 47 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
| 48 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
| 49 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
| 50 | + const TYPE_GENERAL_STRING = 0x1b; |
|
| 51 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 52 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
| 53 | + const TYPE_BMP_STRING = 0x1e; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Mapping from universal type tag to implementation class name. |
|
| 57 | - * |
|
| 58 | - * @internal |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
| 63 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 64 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 65 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 66 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 67 | - self::TYPE_NULL => Primitive\NullType::class, |
|
| 68 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 69 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 70 | - self::TYPE_REAL => Primitive\Real::class, |
|
| 71 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 72 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 73 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 74 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 75 | - self::TYPE_SET => Constructed\Set::class, |
|
| 76 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 77 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 78 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 79 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 80 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 81 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 82 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 83 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 84 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 85 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 86 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 87 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 88 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 89 | - /* @formatter:on */ |
|
| 90 | - ]; |
|
| 55 | + /** |
|
| 56 | + * Mapping from universal type tag to implementation class name. |
|
| 57 | + * |
|
| 58 | + * @internal |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
| 63 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 64 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 65 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 66 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 67 | + self::TYPE_NULL => Primitive\NullType::class, |
|
| 68 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 69 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 70 | + self::TYPE_REAL => Primitive\Real::class, |
|
| 71 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 72 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 73 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 74 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 75 | + self::TYPE_SET => Constructed\Set::class, |
|
| 76 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 77 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 78 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 79 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 80 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 81 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 82 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 83 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 84 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 85 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 86 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 87 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 88 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 89 | + /* @formatter:on */ |
|
| 90 | + ]; |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Pseudotype for all string types. |
|
| 94 | - * |
|
| 95 | - * May be used as an expectation parameter. |
|
| 96 | - * |
|
| 97 | - * @var int |
|
| 98 | - */ |
|
| 99 | - const TYPE_STRING = -1; |
|
| 92 | + /** |
|
| 93 | + * Pseudotype for all string types. |
|
| 94 | + * |
|
| 95 | + * May be used as an expectation parameter. |
|
| 96 | + * |
|
| 97 | + * @var int |
|
| 98 | + */ |
|
| 99 | + const TYPE_STRING = -1; |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Pseudotype for all time types. |
|
| 103 | - * |
|
| 104 | - * May be used as an expectation parameter. |
|
| 105 | - * |
|
| 106 | - * @var int |
|
| 107 | - */ |
|
| 108 | - const TYPE_TIME = -2; |
|
| 101 | + /** |
|
| 102 | + * Pseudotype for all time types. |
|
| 103 | + * |
|
| 104 | + * May be used as an expectation parameter. |
|
| 105 | + * |
|
| 106 | + * @var int |
|
| 107 | + */ |
|
| 108 | + const TYPE_TIME = -2; |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Mapping from universal type tag to human readable name. |
|
| 112 | - * |
|
| 113 | - * @internal |
|
| 114 | - * |
|
| 115 | - * @var array |
|
| 116 | - */ |
|
| 117 | - const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
| 118 | - self::TYPE_EOC => "EOC", |
|
| 119 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 120 | - self::TYPE_INTEGER => "INTEGER", |
|
| 121 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
| 122 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 123 | - self::TYPE_NULL => "NULL", |
|
| 124 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 125 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 126 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 127 | - self::TYPE_REAL => "REAL", |
|
| 128 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 129 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 130 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
| 131 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 132 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 133 | - self::TYPE_SET => "SET", |
|
| 134 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 135 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 136 | - self::TYPE_T61_STRING => "T61String", |
|
| 137 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 138 | - self::TYPE_IA5_STRING => "IA5String", |
|
| 139 | - self::TYPE_UTC_TIME => "UTCTime", |
|
| 140 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 141 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 142 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 143 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 144 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 145 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 146 | - self::TYPE_BMP_STRING => "BMPString", |
|
| 147 | - self::TYPE_STRING => "Any String", |
|
| 148 | - self::TYPE_TIME => "Any Time" |
|
| 149 | - /* @formatter:on */ |
|
| 150 | - ]; |
|
| 110 | + /** |
|
| 111 | + * Mapping from universal type tag to human readable name. |
|
| 112 | + * |
|
| 113 | + * @internal |
|
| 114 | + * |
|
| 115 | + * @var array |
|
| 116 | + */ |
|
| 117 | + const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
| 118 | + self::TYPE_EOC => "EOC", |
|
| 119 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 120 | + self::TYPE_INTEGER => "INTEGER", |
|
| 121 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
| 122 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 123 | + self::TYPE_NULL => "NULL", |
|
| 124 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 125 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 126 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 127 | + self::TYPE_REAL => "REAL", |
|
| 128 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 129 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 130 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
| 131 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 132 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 133 | + self::TYPE_SET => "SET", |
|
| 134 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 135 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 136 | + self::TYPE_T61_STRING => "T61String", |
|
| 137 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 138 | + self::TYPE_IA5_STRING => "IA5String", |
|
| 139 | + self::TYPE_UTC_TIME => "UTCTime", |
|
| 140 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 141 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 142 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 143 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 144 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 145 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 146 | + self::TYPE_BMP_STRING => "BMPString", |
|
| 147 | + self::TYPE_STRING => "Any String", |
|
| 148 | + self::TYPE_TIME => "Any Time" |
|
| 149 | + /* @formatter:on */ |
|
| 150 | + ]; |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Element's type tag. |
|
| 154 | - * |
|
| 155 | - * @var int |
|
| 156 | - */ |
|
| 157 | - protected $_typeTag; |
|
| 152 | + /** |
|
| 153 | + * Element's type tag. |
|
| 154 | + * |
|
| 155 | + * @var int |
|
| 156 | + */ |
|
| 157 | + protected $_typeTag; |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * |
|
| 161 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 162 | - * @return int |
|
| 163 | - */ |
|
| 164 | - abstract public function typeClass(): int; |
|
| 159 | + /** |
|
| 160 | + * |
|
| 161 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 162 | + * @return int |
|
| 163 | + */ |
|
| 164 | + abstract public function typeClass(): int; |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * |
|
| 168 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - abstract public function isConstructed(): bool; |
|
| 166 | + /** |
|
| 167 | + * |
|
| 168 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + abstract public function isConstructed(): bool; |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Get the content encoded in DER. |
|
| 175 | - * |
|
| 176 | - * Returns the DER encoded content without identifier and length header |
|
| 177 | - * octets. |
|
| 178 | - * |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - abstract protected function _encodedContentDER(): string; |
|
| 173 | + /** |
|
| 174 | + * Get the content encoded in DER. |
|
| 175 | + * |
|
| 176 | + * Returns the DER encoded content without identifier and length header |
|
| 177 | + * octets. |
|
| 178 | + * |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + abstract protected function _encodedContentDER(): string; |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Decode type-specific element from DER. |
|
| 185 | - * |
|
| 186 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 187 | - * @param string $data DER data |
|
| 188 | - * @param int $offset Offset in data to the next byte after identifier |
|
| 189 | - * @throws DecodeException If decoding fails |
|
| 190 | - * @return self |
|
| 191 | - */ |
|
| 192 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 193 | - int &$offset): ElementBase |
|
| 194 | - { |
|
| 195 | - throw new \BadMethodCallException( |
|
| 196 | - __METHOD__ . " must be implemented in derived class."); |
|
| 197 | - } |
|
| 183 | + /** |
|
| 184 | + * Decode type-specific element from DER. |
|
| 185 | + * |
|
| 186 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 187 | + * @param string $data DER data |
|
| 188 | + * @param int $offset Offset in data to the next byte after identifier |
|
| 189 | + * @throws DecodeException If decoding fails |
|
| 190 | + * @return self |
|
| 191 | + */ |
|
| 192 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 193 | + int &$offset): ElementBase |
|
| 194 | + { |
|
| 195 | + throw new \BadMethodCallException( |
|
| 196 | + __METHOD__ . " must be implemented in derived class."); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Decode element from DER data. |
|
| 201 | - * |
|
| 202 | - * @param string $data DER encoded data |
|
| 203 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 204 | - * into the data where to start parsing. Variable is updated to |
|
| 205 | - * the offset next to the parsed element. If null, start from offset |
|
| 206 | - * 0. |
|
| 207 | - * @throws DecodeException If decoding fails |
|
| 208 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
| 209 | - * type, but decoding yields another type |
|
| 210 | - * @return ElementBase |
|
| 211 | - */ |
|
| 212 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 213 | - { |
|
| 214 | - // decode identifier |
|
| 215 | - $idx = $offset ? $offset : 0; |
|
| 216 | - $identifier = Identifier::fromDER($data, $idx); |
|
| 217 | - // determine class that implements type specific decoding |
|
| 218 | - $cls = self::_determineImplClass($identifier); |
|
| 219 | - try { |
|
| 220 | - // decode remaining element |
|
| 221 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 222 | - } catch (\LogicException $e) { |
|
| 223 | - // rethrow as a RuntimeException for unified exception handling |
|
| 224 | - throw new DecodeException( |
|
| 225 | - sprintf("Error while decoding %s.", |
|
| 226 | - self::tagToName($identifier->intTag())), 0, $e); |
|
| 227 | - } |
|
| 228 | - // if called in the context of a concrete class, check |
|
| 229 | - // that decoded type matches the type of a calling class |
|
| 230 | - $called_class = get_called_class(); |
|
| 231 | - if (self::class != $called_class) { |
|
| 232 | - if (!$element instanceof $called_class) { |
|
| 233 | - throw new \UnexpectedValueException( |
|
| 234 | - sprintf("%s expected, got %s.", $called_class, |
|
| 235 | - get_class($element))); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - // update offset for the caller |
|
| 239 | - if (isset($offset)) { |
|
| 240 | - $offset = $idx; |
|
| 241 | - } |
|
| 242 | - return $element; |
|
| 243 | - } |
|
| 199 | + /** |
|
| 200 | + * Decode element from DER data. |
|
| 201 | + * |
|
| 202 | + * @param string $data DER encoded data |
|
| 203 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 204 | + * into the data where to start parsing. Variable is updated to |
|
| 205 | + * the offset next to the parsed element. If null, start from offset |
|
| 206 | + * 0. |
|
| 207 | + * @throws DecodeException If decoding fails |
|
| 208 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
| 209 | + * type, but decoding yields another type |
|
| 210 | + * @return ElementBase |
|
| 211 | + */ |
|
| 212 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 213 | + { |
|
| 214 | + // decode identifier |
|
| 215 | + $idx = $offset ? $offset : 0; |
|
| 216 | + $identifier = Identifier::fromDER($data, $idx); |
|
| 217 | + // determine class that implements type specific decoding |
|
| 218 | + $cls = self::_determineImplClass($identifier); |
|
| 219 | + try { |
|
| 220 | + // decode remaining element |
|
| 221 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 222 | + } catch (\LogicException $e) { |
|
| 223 | + // rethrow as a RuntimeException for unified exception handling |
|
| 224 | + throw new DecodeException( |
|
| 225 | + sprintf("Error while decoding %s.", |
|
| 226 | + self::tagToName($identifier->intTag())), 0, $e); |
|
| 227 | + } |
|
| 228 | + // if called in the context of a concrete class, check |
|
| 229 | + // that decoded type matches the type of a calling class |
|
| 230 | + $called_class = get_called_class(); |
|
| 231 | + if (self::class != $called_class) { |
|
| 232 | + if (!$element instanceof $called_class) { |
|
| 233 | + throw new \UnexpectedValueException( |
|
| 234 | + sprintf("%s expected, got %s.", $called_class, |
|
| 235 | + get_class($element))); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + // update offset for the caller |
|
| 239 | + if (isset($offset)) { |
|
| 240 | + $offset = $idx; |
|
| 241 | + } |
|
| 242 | + return $element; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * |
|
| 247 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public function toDER(): string |
|
| 251 | - { |
|
| 252 | - $identifier = new Identifier($this->typeClass(), |
|
| 253 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 254 | - $this->_typeTag); |
|
| 255 | - $content = $this->_encodedContentDER(); |
|
| 256 | - $length = new Length(strlen($content)); |
|
| 257 | - return $identifier->toDER() . $length->toDER() . $content; |
|
| 258 | - } |
|
| 245 | + /** |
|
| 246 | + * |
|
| 247 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public function toDER(): string |
|
| 251 | + { |
|
| 252 | + $identifier = new Identifier($this->typeClass(), |
|
| 253 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 254 | + $this->_typeTag); |
|
| 255 | + $content = $this->_encodedContentDER(); |
|
| 256 | + $length = new Length(strlen($content)); |
|
| 257 | + return $identifier->toDER() . $length->toDER() . $content; |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * |
|
| 262 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 263 | - * @return int |
|
| 264 | - */ |
|
| 265 | - public function tag(): int |
|
| 266 | - { |
|
| 267 | - return $this->_typeTag; |
|
| 268 | - } |
|
| 260 | + /** |
|
| 261 | + * |
|
| 262 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 263 | + * @return int |
|
| 264 | + */ |
|
| 265 | + public function tag(): int |
|
| 266 | + { |
|
| 267 | + return $this->_typeTag; |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * |
|
| 272 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 273 | - * @return bool |
|
| 274 | - */ |
|
| 275 | - public function isType(int $tag): bool |
|
| 276 | - { |
|
| 277 | - // if element is context specific |
|
| 278 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 279 | - return false; |
|
| 280 | - } |
|
| 281 | - // negative tags identify an abstract pseudotype |
|
| 282 | - if ($tag < 0) { |
|
| 283 | - return $this->_isPseudoType($tag); |
|
| 284 | - } |
|
| 285 | - return $this->_isConcreteType($tag); |
|
| 286 | - } |
|
| 270 | + /** |
|
| 271 | + * |
|
| 272 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 273 | + * @return bool |
|
| 274 | + */ |
|
| 275 | + public function isType(int $tag): bool |
|
| 276 | + { |
|
| 277 | + // if element is context specific |
|
| 278 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 279 | + return false; |
|
| 280 | + } |
|
| 281 | + // negative tags identify an abstract pseudotype |
|
| 282 | + if ($tag < 0) { |
|
| 283 | + return $this->_isPseudoType($tag); |
|
| 284 | + } |
|
| 285 | + return $this->_isConcreteType($tag); |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - /** |
|
| 289 | - * |
|
| 290 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 291 | - * @return ElementBase |
|
| 292 | - */ |
|
| 293 | - public function expectType(int $tag): ElementBase |
|
| 294 | - { |
|
| 295 | - if (!$this->isType($tag)) { |
|
| 296 | - throw new \UnexpectedValueException( |
|
| 297 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 298 | - $this->_typeDescriptorString())); |
|
| 299 | - } |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 288 | + /** |
|
| 289 | + * |
|
| 290 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 291 | + * @return ElementBase |
|
| 292 | + */ |
|
| 293 | + public function expectType(int $tag): ElementBase |
|
| 294 | + { |
|
| 295 | + if (!$this->isType($tag)) { |
|
| 296 | + throw new \UnexpectedValueException( |
|
| 297 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 298 | + $this->_typeDescriptorString())); |
|
| 299 | + } |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Check whether the element is a concrete type of a given tag. |
|
| 305 | - * |
|
| 306 | - * @param int $tag |
|
| 307 | - * @return bool |
|
| 308 | - */ |
|
| 309 | - private function _isConcreteType(int $tag): bool |
|
| 310 | - { |
|
| 311 | - // if tag doesn't match |
|
| 312 | - if ($this->tag() != $tag) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - // if type is universal check that instance is of a correct class |
|
| 316 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 317 | - $cls = self::_determineUniversalImplClass($tag); |
|
| 318 | - if (!$this instanceof $cls) { |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - return true; |
|
| 323 | - } |
|
| 303 | + /** |
|
| 304 | + * Check whether the element is a concrete type of a given tag. |
|
| 305 | + * |
|
| 306 | + * @param int $tag |
|
| 307 | + * @return bool |
|
| 308 | + */ |
|
| 309 | + private function _isConcreteType(int $tag): bool |
|
| 310 | + { |
|
| 311 | + // if tag doesn't match |
|
| 312 | + if ($this->tag() != $tag) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + // if type is universal check that instance is of a correct class |
|
| 316 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 317 | + $cls = self::_determineUniversalImplClass($tag); |
|
| 318 | + if (!$this instanceof $cls) { |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + return true; |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** |
|
| 326 | - * Check whether the element is a pseudotype. |
|
| 327 | - * |
|
| 328 | - * @param int $tag |
|
| 329 | - * @return bool |
|
| 330 | - */ |
|
| 331 | - private function _isPseudoType(int $tag): bool |
|
| 332 | - { |
|
| 333 | - switch ($tag) { |
|
| 334 | - case self::TYPE_STRING: |
|
| 335 | - return $this instanceof StringType; |
|
| 336 | - case self::TYPE_TIME: |
|
| 337 | - return $this instanceof TimeType; |
|
| 338 | - } |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 325 | + /** |
|
| 326 | + * Check whether the element is a pseudotype. |
|
| 327 | + * |
|
| 328 | + * @param int $tag |
|
| 329 | + * @return bool |
|
| 330 | + */ |
|
| 331 | + private function _isPseudoType(int $tag): bool |
|
| 332 | + { |
|
| 333 | + switch ($tag) { |
|
| 334 | + case self::TYPE_STRING: |
|
| 335 | + return $this instanceof StringType; |
|
| 336 | + case self::TYPE_TIME: |
|
| 337 | + return $this instanceof TimeType; |
|
| 338 | + } |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | 341 | |
| 342 | - /** |
|
| 343 | - * |
|
| 344 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 345 | - * @return bool |
|
| 346 | - */ |
|
| 347 | - public function isTagged(): bool |
|
| 348 | - { |
|
| 349 | - return $this instanceof TaggedType; |
|
| 350 | - } |
|
| 342 | + /** |
|
| 343 | + * |
|
| 344 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 345 | + * @return bool |
|
| 346 | + */ |
|
| 347 | + public function isTagged(): bool |
|
| 348 | + { |
|
| 349 | + return $this instanceof TaggedType; |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - /** |
|
| 353 | - * |
|
| 354 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 355 | - * @return TaggedType |
|
| 356 | - */ |
|
| 357 | - public function expectTagged($tag = null): TaggedType |
|
| 358 | - { |
|
| 359 | - if (!$this->isTagged()) { |
|
| 360 | - throw new \UnexpectedValueException( |
|
| 361 | - sprintf("Context specific element expected, got %s.", |
|
| 362 | - Identifier::classToName($this->typeClass()))); |
|
| 363 | - } |
|
| 364 | - if (isset($tag) && $this->tag() != $tag) { |
|
| 365 | - throw new \UnexpectedValueException( |
|
| 366 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 367 | - } |
|
| 368 | - return $this; |
|
| 369 | - } |
|
| 352 | + /** |
|
| 353 | + * |
|
| 354 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 355 | + * @return TaggedType |
|
| 356 | + */ |
|
| 357 | + public function expectTagged($tag = null): TaggedType |
|
| 358 | + { |
|
| 359 | + if (!$this->isTagged()) { |
|
| 360 | + throw new \UnexpectedValueException( |
|
| 361 | + sprintf("Context specific element expected, got %s.", |
|
| 362 | + Identifier::classToName($this->typeClass()))); |
|
| 363 | + } |
|
| 364 | + if (isset($tag) && $this->tag() != $tag) { |
|
| 365 | + throw new \UnexpectedValueException( |
|
| 366 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 367 | + } |
|
| 368 | + return $this; |
|
| 369 | + } |
|
| 370 | 370 | |
| 371 | - /** |
|
| 372 | - * |
|
| 373 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 374 | - * @return Element |
|
| 375 | - */ |
|
| 376 | - final public function asElement(): Element |
|
| 377 | - { |
|
| 378 | - return $this; |
|
| 379 | - } |
|
| 371 | + /** |
|
| 372 | + * |
|
| 373 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 374 | + * @return Element |
|
| 375 | + */ |
|
| 376 | + final public function asElement(): Element |
|
| 377 | + { |
|
| 378 | + return $this; |
|
| 379 | + } |
|
| 380 | 380 | |
| 381 | - /** |
|
| 382 | - * Get element decorated with UnspecifiedType object. |
|
| 383 | - * |
|
| 384 | - * @return UnspecifiedType |
|
| 385 | - */ |
|
| 386 | - public function asUnspecified(): UnspecifiedType |
|
| 387 | - { |
|
| 388 | - return new UnspecifiedType($this); |
|
| 389 | - } |
|
| 381 | + /** |
|
| 382 | + * Get element decorated with UnspecifiedType object. |
|
| 383 | + * |
|
| 384 | + * @return UnspecifiedType |
|
| 385 | + */ |
|
| 386 | + public function asUnspecified(): UnspecifiedType |
|
| 387 | + { |
|
| 388 | + return new UnspecifiedType($this); |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * Determine the class that implements the type. |
|
| 393 | - * |
|
| 394 | - * @param Identifier $identifier |
|
| 395 | - * @return string Class name |
|
| 396 | - */ |
|
| 397 | - protected static function _determineImplClass(Identifier $identifier): string |
|
| 398 | - { |
|
| 399 | - // tagged type |
|
| 400 | - if ($identifier->isContextSpecific()) { |
|
| 401 | - return TaggedType::class; |
|
| 402 | - } |
|
| 403 | - // universal class |
|
| 404 | - if ($identifier->isUniversal()) { |
|
| 405 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 406 | - } |
|
| 407 | - // application type |
|
| 408 | - if ($identifier->isApplication()) { |
|
| 409 | - return ApplicationType::class; |
|
| 410 | - } |
|
| 411 | - throw new \UnexpectedValueException( |
|
| 412 | - sprintf("%s %d not implemented.", |
|
| 413 | - Identifier::classToName($identifier->typeClass()), |
|
| 414 | - $identifier->tag())); |
|
| 415 | - } |
|
| 391 | + /** |
|
| 392 | + * Determine the class that implements the type. |
|
| 393 | + * |
|
| 394 | + * @param Identifier $identifier |
|
| 395 | + * @return string Class name |
|
| 396 | + */ |
|
| 397 | + protected static function _determineImplClass(Identifier $identifier): string |
|
| 398 | + { |
|
| 399 | + // tagged type |
|
| 400 | + if ($identifier->isContextSpecific()) { |
|
| 401 | + return TaggedType::class; |
|
| 402 | + } |
|
| 403 | + // universal class |
|
| 404 | + if ($identifier->isUniversal()) { |
|
| 405 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 406 | + } |
|
| 407 | + // application type |
|
| 408 | + if ($identifier->isApplication()) { |
|
| 409 | + return ApplicationType::class; |
|
| 410 | + } |
|
| 411 | + throw new \UnexpectedValueException( |
|
| 412 | + sprintf("%s %d not implemented.", |
|
| 413 | + Identifier::classToName($identifier->typeClass()), |
|
| 414 | + $identifier->tag())); |
|
| 415 | + } |
|
| 416 | 416 | |
| 417 | - /** |
|
| 418 | - * Determine the class that implements an universal type of the given tag. |
|
| 419 | - * |
|
| 420 | - * @param int $tag |
|
| 421 | - * @throws \UnexpectedValueException |
|
| 422 | - * @return string Class name |
|
| 423 | - */ |
|
| 424 | - protected static function _determineUniversalImplClass(int $tag): string |
|
| 425 | - { |
|
| 426 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 427 | - throw new \UnexpectedValueException( |
|
| 428 | - "Universal tag $tag not implemented."); |
|
| 429 | - } |
|
| 430 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 431 | - } |
|
| 417 | + /** |
|
| 418 | + * Determine the class that implements an universal type of the given tag. |
|
| 419 | + * |
|
| 420 | + * @param int $tag |
|
| 421 | + * @throws \UnexpectedValueException |
|
| 422 | + * @return string Class name |
|
| 423 | + */ |
|
| 424 | + protected static function _determineUniversalImplClass(int $tag): string |
|
| 425 | + { |
|
| 426 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 427 | + throw new \UnexpectedValueException( |
|
| 428 | + "Universal tag $tag not implemented."); |
|
| 429 | + } |
|
| 430 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 431 | + } |
|
| 432 | 432 | |
| 433 | - /** |
|
| 434 | - * Get textual description of the type for debugging purposes. |
|
| 435 | - * |
|
| 436 | - * @return string |
|
| 437 | - */ |
|
| 438 | - protected function _typeDescriptorString(): string |
|
| 439 | - { |
|
| 440 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 441 | - return self::tagToName($this->_typeTag); |
|
| 442 | - } |
|
| 443 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 444 | - $this->_typeTag); |
|
| 445 | - } |
|
| 433 | + /** |
|
| 434 | + * Get textual description of the type for debugging purposes. |
|
| 435 | + * |
|
| 436 | + * @return string |
|
| 437 | + */ |
|
| 438 | + protected function _typeDescriptorString(): string |
|
| 439 | + { |
|
| 440 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 441 | + return self::tagToName($this->_typeTag); |
|
| 442 | + } |
|
| 443 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 444 | + $this->_typeTag); |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | - /** |
|
| 448 | - * Get human readable name for an universal tag. |
|
| 449 | - * |
|
| 450 | - * @param int $tag |
|
| 451 | - * @return string |
|
| 452 | - */ |
|
| 453 | - public static function tagToName(int $tag): string |
|
| 454 | - { |
|
| 455 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 456 | - return "TAG $tag"; |
|
| 457 | - } |
|
| 458 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 459 | - } |
|
| 447 | + /** |
|
| 448 | + * Get human readable name for an universal tag. |
|
| 449 | + * |
|
| 450 | + * @param int $tag |
|
| 451 | + * @return string |
|
| 452 | + */ |
|
| 453 | + public static function tagToName(int $tag): string |
|
| 454 | + { |
|
| 455 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 456 | + return "TAG $tag"; |
|
| 457 | + } |
|
| 458 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 459 | + } |
|
| 460 | 460 | } |
@@ -17,650 +17,650 @@ |
||
| 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 | - * @return self |
|
| 42 | - */ |
|
| 43 | - public static function fromDER(string $data): self |
|
| 44 | - { |
|
| 45 | - return Element::fromDER($data)->asUnspecified(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Initialize from ElementBase interface. |
|
| 50 | - * |
|
| 51 | - * @param ElementBase $el |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public static function fromElementBase(ElementBase $el): self |
|
| 55 | - { |
|
| 56 | - // if element is already wrapped |
|
| 57 | - if ($el instanceof self) { |
|
| 58 | - return $el; |
|
| 59 | - } |
|
| 60 | - return new self($el->asElement()); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Compatibility method to dispatch calls to the wrapped element. |
|
| 65 | - * |
|
| 66 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 67 | - * @param string $mtd Method name |
|
| 68 | - * @param array $args Arguments |
|
| 69 | - * @return mixed |
|
| 70 | - */ |
|
| 71 | - public function __call($mtd, array $args) |
|
| 72 | - { |
|
| 73 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Get the wrapped element as a context specific tagged type. |
|
| 78 | - * |
|
| 79 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 80 | - * @return TaggedType |
|
| 81 | - */ |
|
| 82 | - public function asTagged(): TaggedType |
|
| 83 | - { |
|
| 84 | - if (!$this->_element instanceof TaggedType) { |
|
| 85 | - throw new \UnexpectedValueException( |
|
| 86 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 87 | - } |
|
| 88 | - return $this->_element; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the wrapped element as an application specific type. |
|
| 93 | - * |
|
| 94 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 95 | - * @return Tagged\ApplicationType |
|
| 96 | - */ |
|
| 97 | - public function asApplication(): Tagged\ApplicationType |
|
| 98 | - { |
|
| 99 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 100 | - throw new \UnexpectedValueException( |
|
| 101 | - "Application type expected, got " . |
|
| 102 | - $this->_typeDescriptorString()); |
|
| 103 | - } |
|
| 104 | - return $this->_element; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the wrapped element as a boolean type. |
|
| 109 | - * |
|
| 110 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 111 | - * @return Primitive\Boolean |
|
| 112 | - */ |
|
| 113 | - public function asBoolean(): Primitive\Boolean |
|
| 114 | - { |
|
| 115 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 116 | - throw new \UnexpectedValueException( |
|
| 117 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 118 | - } |
|
| 119 | - return $this->_element; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Get the wrapped element as an integer type. |
|
| 124 | - * |
|
| 125 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 126 | - * @return Primitive\Integer |
|
| 127 | - */ |
|
| 128 | - public function asInteger(): Primitive\Integer |
|
| 129 | - { |
|
| 130 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 131 | - throw new \UnexpectedValueException( |
|
| 132 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 133 | - } |
|
| 134 | - return $this->_element; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Get the wrapped element as a bit string type. |
|
| 139 | - * |
|
| 140 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 141 | - * @return Primitive\BitString |
|
| 142 | - */ |
|
| 143 | - public function asBitString(): Primitive\BitString |
|
| 144 | - { |
|
| 145 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 146 | - throw new \UnexpectedValueException( |
|
| 147 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 148 | - } |
|
| 149 | - return $this->_element; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get the wrapped element as an octet string type. |
|
| 154 | - * |
|
| 155 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 156 | - * @return Primitive\OctetString |
|
| 157 | - */ |
|
| 158 | - public function asOctetString(): Primitive\OctetString |
|
| 159 | - { |
|
| 160 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 161 | - throw new \UnexpectedValueException( |
|
| 162 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 163 | - } |
|
| 164 | - return $this->_element; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get the wrapped element as a null type. |
|
| 169 | - * |
|
| 170 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 171 | - * @return Primitive\NullType |
|
| 172 | - */ |
|
| 173 | - public function asNull(): Primitive\NullType |
|
| 174 | - { |
|
| 175 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 176 | - throw new \UnexpectedValueException( |
|
| 177 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 178 | - } |
|
| 179 | - return $this->_element; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the wrapped element as an object identifier type. |
|
| 184 | - * |
|
| 185 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 186 | - * identifier |
|
| 187 | - * @return Primitive\ObjectIdentifier |
|
| 188 | - */ |
|
| 189 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 190 | - { |
|
| 191 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 192 | - throw new \UnexpectedValueException( |
|
| 193 | - $this->_generateExceptionMessage( |
|
| 194 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 195 | - } |
|
| 196 | - return $this->_element; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Get the wrapped element as an object descriptor type. |
|
| 201 | - * |
|
| 202 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 203 | - * descriptor |
|
| 204 | - * @return Primitive\ObjectDescriptor |
|
| 205 | - */ |
|
| 206 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 207 | - { |
|
| 208 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 209 | - throw new \UnexpectedValueException( |
|
| 210 | - $this->_generateExceptionMessage( |
|
| 211 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 212 | - } |
|
| 213 | - return $this->_element; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Get the wrapped element as a real type. |
|
| 218 | - * |
|
| 219 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 220 | - * @return Primitive\Real |
|
| 221 | - */ |
|
| 222 | - public function asReal(): Primitive\Real |
|
| 223 | - { |
|
| 224 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 225 | - throw new \UnexpectedValueException( |
|
| 226 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 227 | - } |
|
| 228 | - return $this->_element; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Get the wrapped element as an enumerated type. |
|
| 233 | - * |
|
| 234 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 235 | - * @return Primitive\Enumerated |
|
| 236 | - */ |
|
| 237 | - public function asEnumerated(): Primitive\Enumerated |
|
| 238 | - { |
|
| 239 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 240 | - throw new \UnexpectedValueException( |
|
| 241 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 242 | - } |
|
| 243 | - return $this->_element; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the wrapped element as a UTF8 string type. |
|
| 248 | - * |
|
| 249 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 250 | - * @return Primitive\UTF8String |
|
| 251 | - */ |
|
| 252 | - public function asUTF8String(): Primitive\UTF8String |
|
| 253 | - { |
|
| 254 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 255 | - throw new \UnexpectedValueException( |
|
| 256 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 257 | - } |
|
| 258 | - return $this->_element; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Get the wrapped element as a relative OID type. |
|
| 263 | - * |
|
| 264 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 265 | - * @return Primitive\RelativeOID |
|
| 266 | - */ |
|
| 267 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 268 | - { |
|
| 269 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 270 | - throw new \UnexpectedValueException( |
|
| 271 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 272 | - } |
|
| 273 | - return $this->_element; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Get the wrapped element as a sequence type. |
|
| 278 | - * |
|
| 279 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 280 | - * @return Constructed\Sequence |
|
| 281 | - */ |
|
| 282 | - public function asSequence(): Constructed\Sequence |
|
| 283 | - { |
|
| 284 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 285 | - throw new \UnexpectedValueException( |
|
| 286 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 287 | - } |
|
| 288 | - return $this->_element; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Get the wrapped element as a set type. |
|
| 293 | - * |
|
| 294 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 295 | - * @return Constructed\Set |
|
| 296 | - */ |
|
| 297 | - public function asSet(): Constructed\Set |
|
| 298 | - { |
|
| 299 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 300 | - throw new \UnexpectedValueException( |
|
| 301 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 302 | - } |
|
| 303 | - return $this->_element; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Get the wrapped element as a numeric string type. |
|
| 308 | - * |
|
| 309 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 310 | - * @return Primitive\NumericString |
|
| 311 | - */ |
|
| 312 | - public function asNumericString(): Primitive\NumericString |
|
| 313 | - { |
|
| 314 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 315 | - throw new \UnexpectedValueException( |
|
| 316 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 317 | - } |
|
| 318 | - return $this->_element; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Get the wrapped element as a printable string type. |
|
| 323 | - * |
|
| 324 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 325 | - * string |
|
| 326 | - * @return Primitive\PrintableString |
|
| 327 | - */ |
|
| 328 | - public function asPrintableString(): Primitive\PrintableString |
|
| 329 | - { |
|
| 330 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 331 | - throw new \UnexpectedValueException( |
|
| 332 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 333 | - } |
|
| 334 | - return $this->_element; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Get the wrapped element as a T61 string type. |
|
| 339 | - * |
|
| 340 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 341 | - * @return Primitive\T61String |
|
| 342 | - */ |
|
| 343 | - public function asT61String(): Primitive\T61String |
|
| 344 | - { |
|
| 345 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 346 | - throw new \UnexpectedValueException( |
|
| 347 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 348 | - } |
|
| 349 | - return $this->_element; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get the wrapped element as a videotex string type. |
|
| 354 | - * |
|
| 355 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 356 | - * @return Primitive\VideotexString |
|
| 357 | - */ |
|
| 358 | - public function asVideotexString(): Primitive\VideotexString |
|
| 359 | - { |
|
| 360 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 361 | - throw new \UnexpectedValueException( |
|
| 362 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 363 | - } |
|
| 364 | - return $this->_element; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Get the wrapped element as a IA5 string type. |
|
| 369 | - * |
|
| 370 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 371 | - * @return Primitive\IA5String |
|
| 372 | - */ |
|
| 373 | - public function asIA5String(): Primitive\IA5String |
|
| 374 | - { |
|
| 375 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 376 | - throw new \UnexpectedValueException( |
|
| 377 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 378 | - } |
|
| 379 | - return $this->_element; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Get the wrapped element as an UTC time type. |
|
| 384 | - * |
|
| 385 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 386 | - * @return Primitive\UTCTime |
|
| 387 | - */ |
|
| 388 | - public function asUTCTime(): Primitive\UTCTime |
|
| 389 | - { |
|
| 390 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 391 | - throw new \UnexpectedValueException( |
|
| 392 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 393 | - } |
|
| 394 | - return $this->_element; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Get the wrapped element as a generalized time type. |
|
| 399 | - * |
|
| 400 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 401 | - * time |
|
| 402 | - * @return Primitive\GeneralizedTime |
|
| 403 | - */ |
|
| 404 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 405 | - { |
|
| 406 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 407 | - throw new \UnexpectedValueException( |
|
| 408 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 409 | - } |
|
| 410 | - return $this->_element; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Get the wrapped element as a graphic string type. |
|
| 415 | - * |
|
| 416 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 417 | - * @return Primitive\GraphicString |
|
| 418 | - */ |
|
| 419 | - public function asGraphicString(): Primitive\GraphicString |
|
| 420 | - { |
|
| 421 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 422 | - throw new \UnexpectedValueException( |
|
| 423 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 424 | - } |
|
| 425 | - return $this->_element; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Get the wrapped element as a visible string type. |
|
| 430 | - * |
|
| 431 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 432 | - * @return Primitive\VisibleString |
|
| 433 | - */ |
|
| 434 | - public function asVisibleString(): Primitive\VisibleString |
|
| 435 | - { |
|
| 436 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 437 | - throw new \UnexpectedValueException( |
|
| 438 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 439 | - } |
|
| 440 | - return $this->_element; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Get the wrapped element as a general string type. |
|
| 445 | - * |
|
| 446 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 447 | - * @return Primitive\GeneralString |
|
| 448 | - */ |
|
| 449 | - public function asGeneralString(): Primitive\GeneralString |
|
| 450 | - { |
|
| 451 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 452 | - throw new \UnexpectedValueException( |
|
| 453 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 454 | - } |
|
| 455 | - return $this->_element; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Get the wrapped element as a universal string type. |
|
| 460 | - * |
|
| 461 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 462 | - * string |
|
| 463 | - * @return Primitive\UniversalString |
|
| 464 | - */ |
|
| 465 | - public function asUniversalString(): Primitive\UniversalString |
|
| 466 | - { |
|
| 467 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 468 | - throw new \UnexpectedValueException( |
|
| 469 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 470 | - } |
|
| 471 | - return $this->_element; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Get the wrapped element as a character string type. |
|
| 476 | - * |
|
| 477 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 478 | - * string |
|
| 479 | - * @return Primitive\CharacterString |
|
| 480 | - */ |
|
| 481 | - public function asCharacterString(): Primitive\CharacterString |
|
| 482 | - { |
|
| 483 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 484 | - throw new \UnexpectedValueException( |
|
| 485 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 486 | - } |
|
| 487 | - return $this->_element; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Get the wrapped element as a BMP string type. |
|
| 492 | - * |
|
| 493 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 494 | - * @return Primitive\BMPString |
|
| 495 | - */ |
|
| 496 | - public function asBMPString(): Primitive\BMPString |
|
| 497 | - { |
|
| 498 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 499 | - throw new \UnexpectedValueException( |
|
| 500 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 501 | - } |
|
| 502 | - return $this->_element; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Get the wrapped element as any string type. |
|
| 507 | - * |
|
| 508 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 509 | - * @return StringType |
|
| 510 | - */ |
|
| 511 | - public function asString(): StringType |
|
| 512 | - { |
|
| 513 | - if (!$this->_element instanceof StringType) { |
|
| 514 | - throw new \UnexpectedValueException( |
|
| 515 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 516 | - } |
|
| 517 | - return $this->_element; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Get the wrapped element as any time type. |
|
| 522 | - * |
|
| 523 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 524 | - * @return TimeType |
|
| 525 | - */ |
|
| 526 | - public function asTime(): TimeType |
|
| 527 | - { |
|
| 528 | - if (!$this->_element instanceof TimeType) { |
|
| 529 | - throw new \UnexpectedValueException( |
|
| 530 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 531 | - } |
|
| 532 | - return $this->_element; |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 537 | - * |
|
| 538 | - * @param int $tag Type tag of the expected element |
|
| 539 | - * @return string |
|
| 540 | - */ |
|
| 541 | - private function _generateExceptionMessage(int $tag): string |
|
| 542 | - { |
|
| 543 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 544 | - $this->_typeDescriptorString()); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 549 | - * |
|
| 550 | - * @return string |
|
| 551 | - */ |
|
| 552 | - private function _typeDescriptorString(): string |
|
| 553 | - { |
|
| 554 | - $type_cls = $this->_element->typeClass(); |
|
| 555 | - $tag = $this->_element->tag(); |
|
| 556 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 557 | - return Element::tagToName($tag); |
|
| 558 | - } |
|
| 559 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * |
|
| 564 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 565 | - * @return string |
|
| 566 | - */ |
|
| 567 | - public function toDER(): string |
|
| 568 | - { |
|
| 569 | - return $this->_element->toDER(); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * |
|
| 574 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 575 | - * @return int |
|
| 576 | - */ |
|
| 577 | - public function typeClass(): int |
|
| 578 | - { |
|
| 579 | - return $this->_element->typeClass(); |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * |
|
| 584 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 585 | - * @return bool |
|
| 586 | - */ |
|
| 587 | - public function isConstructed(): bool |
|
| 588 | - { |
|
| 589 | - return $this->_element->isConstructed(); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * |
|
| 594 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 595 | - * @return int |
|
| 596 | - */ |
|
| 597 | - public function tag(): int |
|
| 598 | - { |
|
| 599 | - return $this->_element->tag(); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * |
|
| 604 | - * {@inheritdoc} |
|
| 605 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 606 | - * @return bool |
|
| 607 | - */ |
|
| 608 | - public function isType(int $tag): bool |
|
| 609 | - { |
|
| 610 | - return $this->_element->isType($tag); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * |
|
| 615 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 616 | - * type strictness. |
|
| 617 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 618 | - * @return ElementBase |
|
| 619 | - */ |
|
| 620 | - public function expectType(int $tag): ElementBase |
|
| 621 | - { |
|
| 622 | - return $this->_element->expectType($tag); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * |
|
| 627 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 628 | - * @return bool |
|
| 629 | - */ |
|
| 630 | - public function isTagged(): bool |
|
| 631 | - { |
|
| 632 | - return $this->_element->isTagged(); |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - /** |
|
| 636 | - * |
|
| 637 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 638 | - * type strictness. |
|
| 639 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 640 | - * @return TaggedType |
|
| 641 | - */ |
|
| 642 | - public function expectTagged($tag = null): TaggedType |
|
| 643 | - { |
|
| 644 | - return $this->_element->expectTagged($tag); |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * |
|
| 649 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 650 | - * @return Element |
|
| 651 | - */ |
|
| 652 | - public function asElement(): Element |
|
| 653 | - { |
|
| 654 | - return $this->_element; |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * |
|
| 659 | - * {@inheritdoc} |
|
| 660 | - * @return UnspecifiedType |
|
| 661 | - */ |
|
| 662 | - public function asUnspecified(): UnspecifiedType |
|
| 663 | - { |
|
| 664 | - return $this; |
|
| 665 | - } |
|
| 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 | + * @return self |
|
| 42 | + */ |
|
| 43 | + public static function fromDER(string $data): self |
|
| 44 | + { |
|
| 45 | + return Element::fromDER($data)->asUnspecified(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Initialize from ElementBase interface. |
|
| 50 | + * |
|
| 51 | + * @param ElementBase $el |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public static function fromElementBase(ElementBase $el): self |
|
| 55 | + { |
|
| 56 | + // if element is already wrapped |
|
| 57 | + if ($el instanceof self) { |
|
| 58 | + return $el; |
|
| 59 | + } |
|
| 60 | + return new self($el->asElement()); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Compatibility method to dispatch calls to the wrapped element. |
|
| 65 | + * |
|
| 66 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 67 | + * @param string $mtd Method name |
|
| 68 | + * @param array $args Arguments |
|
| 69 | + * @return mixed |
|
| 70 | + */ |
|
| 71 | + public function __call($mtd, array $args) |
|
| 72 | + { |
|
| 73 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Get the wrapped element as a context specific tagged type. |
|
| 78 | + * |
|
| 79 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 80 | + * @return TaggedType |
|
| 81 | + */ |
|
| 82 | + public function asTagged(): TaggedType |
|
| 83 | + { |
|
| 84 | + if (!$this->_element instanceof TaggedType) { |
|
| 85 | + throw new \UnexpectedValueException( |
|
| 86 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 87 | + } |
|
| 88 | + return $this->_element; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the wrapped element as an application specific type. |
|
| 93 | + * |
|
| 94 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 95 | + * @return Tagged\ApplicationType |
|
| 96 | + */ |
|
| 97 | + public function asApplication(): Tagged\ApplicationType |
|
| 98 | + { |
|
| 99 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 100 | + throw new \UnexpectedValueException( |
|
| 101 | + "Application type expected, got " . |
|
| 102 | + $this->_typeDescriptorString()); |
|
| 103 | + } |
|
| 104 | + return $this->_element; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the wrapped element as a boolean type. |
|
| 109 | + * |
|
| 110 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 111 | + * @return Primitive\Boolean |
|
| 112 | + */ |
|
| 113 | + public function asBoolean(): Primitive\Boolean |
|
| 114 | + { |
|
| 115 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 116 | + throw new \UnexpectedValueException( |
|
| 117 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 118 | + } |
|
| 119 | + return $this->_element; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Get the wrapped element as an integer type. |
|
| 124 | + * |
|
| 125 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 126 | + * @return Primitive\Integer |
|
| 127 | + */ |
|
| 128 | + public function asInteger(): Primitive\Integer |
|
| 129 | + { |
|
| 130 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 131 | + throw new \UnexpectedValueException( |
|
| 132 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 133 | + } |
|
| 134 | + return $this->_element; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Get the wrapped element as a bit string type. |
|
| 139 | + * |
|
| 140 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 141 | + * @return Primitive\BitString |
|
| 142 | + */ |
|
| 143 | + public function asBitString(): Primitive\BitString |
|
| 144 | + { |
|
| 145 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 146 | + throw new \UnexpectedValueException( |
|
| 147 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 148 | + } |
|
| 149 | + return $this->_element; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get the wrapped element as an octet string type. |
|
| 154 | + * |
|
| 155 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 156 | + * @return Primitive\OctetString |
|
| 157 | + */ |
|
| 158 | + public function asOctetString(): Primitive\OctetString |
|
| 159 | + { |
|
| 160 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 161 | + throw new \UnexpectedValueException( |
|
| 162 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 163 | + } |
|
| 164 | + return $this->_element; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get the wrapped element as a null type. |
|
| 169 | + * |
|
| 170 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 171 | + * @return Primitive\NullType |
|
| 172 | + */ |
|
| 173 | + public function asNull(): Primitive\NullType |
|
| 174 | + { |
|
| 175 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 176 | + throw new \UnexpectedValueException( |
|
| 177 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 178 | + } |
|
| 179 | + return $this->_element; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the wrapped element as an object identifier type. |
|
| 184 | + * |
|
| 185 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 186 | + * identifier |
|
| 187 | + * @return Primitive\ObjectIdentifier |
|
| 188 | + */ |
|
| 189 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 190 | + { |
|
| 191 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 192 | + throw new \UnexpectedValueException( |
|
| 193 | + $this->_generateExceptionMessage( |
|
| 194 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 195 | + } |
|
| 196 | + return $this->_element; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Get the wrapped element as an object descriptor type. |
|
| 201 | + * |
|
| 202 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 203 | + * descriptor |
|
| 204 | + * @return Primitive\ObjectDescriptor |
|
| 205 | + */ |
|
| 206 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 207 | + { |
|
| 208 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 209 | + throw new \UnexpectedValueException( |
|
| 210 | + $this->_generateExceptionMessage( |
|
| 211 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 212 | + } |
|
| 213 | + return $this->_element; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Get the wrapped element as a real type. |
|
| 218 | + * |
|
| 219 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 220 | + * @return Primitive\Real |
|
| 221 | + */ |
|
| 222 | + public function asReal(): Primitive\Real |
|
| 223 | + { |
|
| 224 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 225 | + throw new \UnexpectedValueException( |
|
| 226 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 227 | + } |
|
| 228 | + return $this->_element; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Get the wrapped element as an enumerated type. |
|
| 233 | + * |
|
| 234 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 235 | + * @return Primitive\Enumerated |
|
| 236 | + */ |
|
| 237 | + public function asEnumerated(): Primitive\Enumerated |
|
| 238 | + { |
|
| 239 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 240 | + throw new \UnexpectedValueException( |
|
| 241 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 242 | + } |
|
| 243 | + return $this->_element; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the wrapped element as a UTF8 string type. |
|
| 248 | + * |
|
| 249 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 250 | + * @return Primitive\UTF8String |
|
| 251 | + */ |
|
| 252 | + public function asUTF8String(): Primitive\UTF8String |
|
| 253 | + { |
|
| 254 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 255 | + throw new \UnexpectedValueException( |
|
| 256 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 257 | + } |
|
| 258 | + return $this->_element; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Get the wrapped element as a relative OID type. |
|
| 263 | + * |
|
| 264 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 265 | + * @return Primitive\RelativeOID |
|
| 266 | + */ |
|
| 267 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 268 | + { |
|
| 269 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 270 | + throw new \UnexpectedValueException( |
|
| 271 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 272 | + } |
|
| 273 | + return $this->_element; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Get the wrapped element as a sequence type. |
|
| 278 | + * |
|
| 279 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 280 | + * @return Constructed\Sequence |
|
| 281 | + */ |
|
| 282 | + public function asSequence(): Constructed\Sequence |
|
| 283 | + { |
|
| 284 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 285 | + throw new \UnexpectedValueException( |
|
| 286 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 287 | + } |
|
| 288 | + return $this->_element; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Get the wrapped element as a set type. |
|
| 293 | + * |
|
| 294 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 295 | + * @return Constructed\Set |
|
| 296 | + */ |
|
| 297 | + public function asSet(): Constructed\Set |
|
| 298 | + { |
|
| 299 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 300 | + throw new \UnexpectedValueException( |
|
| 301 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 302 | + } |
|
| 303 | + return $this->_element; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Get the wrapped element as a numeric string type. |
|
| 308 | + * |
|
| 309 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 310 | + * @return Primitive\NumericString |
|
| 311 | + */ |
|
| 312 | + public function asNumericString(): Primitive\NumericString |
|
| 313 | + { |
|
| 314 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 315 | + throw new \UnexpectedValueException( |
|
| 316 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 317 | + } |
|
| 318 | + return $this->_element; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Get the wrapped element as a printable string type. |
|
| 323 | + * |
|
| 324 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 325 | + * string |
|
| 326 | + * @return Primitive\PrintableString |
|
| 327 | + */ |
|
| 328 | + public function asPrintableString(): Primitive\PrintableString |
|
| 329 | + { |
|
| 330 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 331 | + throw new \UnexpectedValueException( |
|
| 332 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 333 | + } |
|
| 334 | + return $this->_element; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Get the wrapped element as a T61 string type. |
|
| 339 | + * |
|
| 340 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 341 | + * @return Primitive\T61String |
|
| 342 | + */ |
|
| 343 | + public function asT61String(): Primitive\T61String |
|
| 344 | + { |
|
| 345 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 346 | + throw new \UnexpectedValueException( |
|
| 347 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 348 | + } |
|
| 349 | + return $this->_element; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get the wrapped element as a videotex string type. |
|
| 354 | + * |
|
| 355 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 356 | + * @return Primitive\VideotexString |
|
| 357 | + */ |
|
| 358 | + public function asVideotexString(): Primitive\VideotexString |
|
| 359 | + { |
|
| 360 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 361 | + throw new \UnexpectedValueException( |
|
| 362 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 363 | + } |
|
| 364 | + return $this->_element; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Get the wrapped element as a IA5 string type. |
|
| 369 | + * |
|
| 370 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 371 | + * @return Primitive\IA5String |
|
| 372 | + */ |
|
| 373 | + public function asIA5String(): Primitive\IA5String |
|
| 374 | + { |
|
| 375 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 376 | + throw new \UnexpectedValueException( |
|
| 377 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 378 | + } |
|
| 379 | + return $this->_element; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Get the wrapped element as an UTC time type. |
|
| 384 | + * |
|
| 385 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 386 | + * @return Primitive\UTCTime |
|
| 387 | + */ |
|
| 388 | + public function asUTCTime(): Primitive\UTCTime |
|
| 389 | + { |
|
| 390 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 391 | + throw new \UnexpectedValueException( |
|
| 392 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 393 | + } |
|
| 394 | + return $this->_element; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Get the wrapped element as a generalized time type. |
|
| 399 | + * |
|
| 400 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 401 | + * time |
|
| 402 | + * @return Primitive\GeneralizedTime |
|
| 403 | + */ |
|
| 404 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 405 | + { |
|
| 406 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 407 | + throw new \UnexpectedValueException( |
|
| 408 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 409 | + } |
|
| 410 | + return $this->_element; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Get the wrapped element as a graphic string type. |
|
| 415 | + * |
|
| 416 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 417 | + * @return Primitive\GraphicString |
|
| 418 | + */ |
|
| 419 | + public function asGraphicString(): Primitive\GraphicString |
|
| 420 | + { |
|
| 421 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 422 | + throw new \UnexpectedValueException( |
|
| 423 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 424 | + } |
|
| 425 | + return $this->_element; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Get the wrapped element as a visible string type. |
|
| 430 | + * |
|
| 431 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 432 | + * @return Primitive\VisibleString |
|
| 433 | + */ |
|
| 434 | + public function asVisibleString(): Primitive\VisibleString |
|
| 435 | + { |
|
| 436 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 437 | + throw new \UnexpectedValueException( |
|
| 438 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 439 | + } |
|
| 440 | + return $this->_element; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Get the wrapped element as a general string type. |
|
| 445 | + * |
|
| 446 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 447 | + * @return Primitive\GeneralString |
|
| 448 | + */ |
|
| 449 | + public function asGeneralString(): Primitive\GeneralString |
|
| 450 | + { |
|
| 451 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 452 | + throw new \UnexpectedValueException( |
|
| 453 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 454 | + } |
|
| 455 | + return $this->_element; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Get the wrapped element as a universal string type. |
|
| 460 | + * |
|
| 461 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 462 | + * string |
|
| 463 | + * @return Primitive\UniversalString |
|
| 464 | + */ |
|
| 465 | + public function asUniversalString(): Primitive\UniversalString |
|
| 466 | + { |
|
| 467 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 468 | + throw new \UnexpectedValueException( |
|
| 469 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 470 | + } |
|
| 471 | + return $this->_element; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Get the wrapped element as a character string type. |
|
| 476 | + * |
|
| 477 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 478 | + * string |
|
| 479 | + * @return Primitive\CharacterString |
|
| 480 | + */ |
|
| 481 | + public function asCharacterString(): Primitive\CharacterString |
|
| 482 | + { |
|
| 483 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 484 | + throw new \UnexpectedValueException( |
|
| 485 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 486 | + } |
|
| 487 | + return $this->_element; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Get the wrapped element as a BMP string type. |
|
| 492 | + * |
|
| 493 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 494 | + * @return Primitive\BMPString |
|
| 495 | + */ |
|
| 496 | + public function asBMPString(): Primitive\BMPString |
|
| 497 | + { |
|
| 498 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 499 | + throw new \UnexpectedValueException( |
|
| 500 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 501 | + } |
|
| 502 | + return $this->_element; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Get the wrapped element as any string type. |
|
| 507 | + * |
|
| 508 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 509 | + * @return StringType |
|
| 510 | + */ |
|
| 511 | + public function asString(): StringType |
|
| 512 | + { |
|
| 513 | + if (!$this->_element instanceof StringType) { |
|
| 514 | + throw new \UnexpectedValueException( |
|
| 515 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 516 | + } |
|
| 517 | + return $this->_element; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Get the wrapped element as any time type. |
|
| 522 | + * |
|
| 523 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 524 | + * @return TimeType |
|
| 525 | + */ |
|
| 526 | + public function asTime(): TimeType |
|
| 527 | + { |
|
| 528 | + if (!$this->_element instanceof TimeType) { |
|
| 529 | + throw new \UnexpectedValueException( |
|
| 530 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 531 | + } |
|
| 532 | + return $this->_element; |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 537 | + * |
|
| 538 | + * @param int $tag Type tag of the expected element |
|
| 539 | + * @return string |
|
| 540 | + */ |
|
| 541 | + private function _generateExceptionMessage(int $tag): string |
|
| 542 | + { |
|
| 543 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 544 | + $this->_typeDescriptorString()); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 549 | + * |
|
| 550 | + * @return string |
|
| 551 | + */ |
|
| 552 | + private function _typeDescriptorString(): string |
|
| 553 | + { |
|
| 554 | + $type_cls = $this->_element->typeClass(); |
|
| 555 | + $tag = $this->_element->tag(); |
|
| 556 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 557 | + return Element::tagToName($tag); |
|
| 558 | + } |
|
| 559 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * |
|
| 564 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 565 | + * @return string |
|
| 566 | + */ |
|
| 567 | + public function toDER(): string |
|
| 568 | + { |
|
| 569 | + return $this->_element->toDER(); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * |
|
| 574 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 575 | + * @return int |
|
| 576 | + */ |
|
| 577 | + public function typeClass(): int |
|
| 578 | + { |
|
| 579 | + return $this->_element->typeClass(); |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * |
|
| 584 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 585 | + * @return bool |
|
| 586 | + */ |
|
| 587 | + public function isConstructed(): bool |
|
| 588 | + { |
|
| 589 | + return $this->_element->isConstructed(); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * |
|
| 594 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 595 | + * @return int |
|
| 596 | + */ |
|
| 597 | + public function tag(): int |
|
| 598 | + { |
|
| 599 | + return $this->_element->tag(); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * |
|
| 604 | + * {@inheritdoc} |
|
| 605 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 606 | + * @return bool |
|
| 607 | + */ |
|
| 608 | + public function isType(int $tag): bool |
|
| 609 | + { |
|
| 610 | + return $this->_element->isType($tag); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * |
|
| 615 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 616 | + * type strictness. |
|
| 617 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 618 | + * @return ElementBase |
|
| 619 | + */ |
|
| 620 | + public function expectType(int $tag): ElementBase |
|
| 621 | + { |
|
| 622 | + return $this->_element->expectType($tag); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * |
|
| 627 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 628 | + * @return bool |
|
| 629 | + */ |
|
| 630 | + public function isTagged(): bool |
|
| 631 | + { |
|
| 632 | + return $this->_element->isTagged(); |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * |
|
| 637 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 638 | + * type strictness. |
|
| 639 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 640 | + * @return TaggedType |
|
| 641 | + */ |
|
| 642 | + public function expectTagged($tag = null): TaggedType |
|
| 643 | + { |
|
| 644 | + return $this->_element->expectTagged($tag); |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * |
|
| 649 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 650 | + * @return Element |
|
| 651 | + */ |
|
| 652 | + public function asElement(): Element |
|
| 653 | + { |
|
| 654 | + return $this->_element; |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * |
|
| 659 | + * {@inheritdoc} |
|
| 660 | + * @return UnspecifiedType |
|
| 661 | + */ |
|
| 662 | + public function asUnspecified(): UnspecifiedType |
|
| 663 | + { |
|
| 664 | + return $this; |
|
| 665 | + } |
|
| 666 | 666 | } |
@@ -17,58 +17,58 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class ImplicitlyTaggedType extends TaggedTypeWrap implements ImplicitTagging |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Constructor. |
|
| 22 | - * |
|
| 23 | - * @param int $tag Tag number |
|
| 24 | - * @param Element $element Wrapped element |
|
| 25 | - * @param int $class Type class |
|
| 26 | - */ |
|
| 27 | - public function __construct(int $tag, Element $element, |
|
| 28 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
| 29 | - { |
|
| 30 | - $this->_typeTag = $tag; |
|
| 31 | - $this->_element = $element; |
|
| 32 | - $this->_class = $class; |
|
| 33 | - } |
|
| 20 | + /** |
|
| 21 | + * Constructor. |
|
| 22 | + * |
|
| 23 | + * @param int $tag Tag number |
|
| 24 | + * @param Element $element Wrapped element |
|
| 25 | + * @param int $class Type class |
|
| 26 | + */ |
|
| 27 | + public function __construct(int $tag, Element $element, |
|
| 28 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
| 29 | + { |
|
| 30 | + $this->_typeTag = $tag; |
|
| 31 | + $this->_element = $element; |
|
| 32 | + $this->_class = $class; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * @see \ASN1\Element::isConstructed() |
|
| 38 | - * @return bool |
|
| 39 | - */ |
|
| 40 | - public function isConstructed(): bool |
|
| 41 | - { |
|
| 42 | - // depends on the underlying type |
|
| 43 | - return $this->_element->isConstructed(); |
|
| 44 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * @see \ASN1\Element::isConstructed() |
|
| 38 | + * @return bool |
|
| 39 | + */ |
|
| 40 | + public function isConstructed(): bool |
|
| 41 | + { |
|
| 42 | + // depends on the underlying type |
|
| 43 | + return $this->_element->isConstructed(); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * |
|
| 48 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - protected function _encodedContentDER(): string |
|
| 52 | - { |
|
| 53 | - // get only the content of the wrapped element. |
|
| 54 | - return $this->_element->_encodedContentDER(); |
|
| 55 | - } |
|
| 46 | + /** |
|
| 47 | + * |
|
| 48 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + protected function _encodedContentDER(): string |
|
| 52 | + { |
|
| 53 | + // get only the content of the wrapped element. |
|
| 54 | + return $this->_element->_encodedContentDER(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * |
|
| 59 | - * {@inheritdoc} |
|
| 60 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 61 | - * @return UnspecifiedType |
|
| 62 | - */ |
|
| 63 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 64 | - { |
|
| 65 | - $this->_element->expectType($tag); |
|
| 66 | - if ($this->_element->typeClass() != $class) { |
|
| 67 | - throw new \UnexpectedValueException( |
|
| 68 | - sprintf("Type class %s expected, got %s.", |
|
| 69 | - Identifier::classToName($class), |
|
| 70 | - Identifier::classToName($this->_element->typeClass()))); |
|
| 71 | - } |
|
| 72 | - return $this->_element->asUnspecified(); |
|
| 73 | - } |
|
| 57 | + /** |
|
| 58 | + * |
|
| 59 | + * {@inheritdoc} |
|
| 60 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 61 | + * @return UnspecifiedType |
|
| 62 | + */ |
|
| 63 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 64 | + { |
|
| 65 | + $this->_element->expectType($tag); |
|
| 66 | + if ($this->_element->typeClass() != $class) { |
|
| 67 | + throw new \UnexpectedValueException( |
|
| 68 | + sprintf("Type class %s expected, got %s.", |
|
| 69 | + Identifier::classToName($class), |
|
| 70 | + Identifier::classToName($this->_element->typeClass()))); |
|
| 71 | + } |
|
| 72 | + return $this->_element->asUnspecified(); |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -11,27 +11,27 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class TaggedTypeWrap extends TaggedType |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Wrapped element. |
|
| 16 | - * |
|
| 17 | - * @var \ASN1\Element $_element |
|
| 18 | - */ |
|
| 19 | - protected $_element; |
|
| 14 | + /** |
|
| 15 | + * Wrapped element. |
|
| 16 | + * |
|
| 17 | + * @var \ASN1\Element $_element |
|
| 18 | + */ |
|
| 19 | + protected $_element; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Type class. |
|
| 23 | - * |
|
| 24 | - * @var int |
|
| 25 | - */ |
|
| 26 | - protected $_class; |
|
| 21 | + /** |
|
| 22 | + * Type class. |
|
| 23 | + * |
|
| 24 | + * @var int |
|
| 25 | + */ |
|
| 26 | + protected $_class; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * @see \ASN1\Element::typeClass() |
|
| 31 | - * @return int |
|
| 32 | - */ |
|
| 33 | - public function typeClass(): int |
|
| 34 | - { |
|
| 35 | - return $this->_class; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * @see \ASN1\Element::typeClass() |
|
| 31 | + * @return int |
|
| 32 | + */ |
|
| 33 | + public function typeClass(): int |
|
| 34 | + { |
|
| 35 | + return $this->_class; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -13,13 +13,13 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ApplicationType extends DERTaggedType |
| 15 | 15 | { |
| 16 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 17 | - int &$offset): ElementBase |
|
| 18 | - { |
|
| 19 | - $idx = $offset; |
|
| 20 | - $type = new self($identifier, $data, $idx); |
|
| 21 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 22 | - $offset = $idx + $length; |
|
| 23 | - return $type; |
|
| 24 | - } |
|
| 16 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 17 | + int &$offset): ElementBase |
|
| 18 | + { |
|
| 19 | + $idx = $offset; |
|
| 20 | + $type = new self($identifier, $data, $idx); |
|
| 21 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 22 | + $offset = $idx + $length; |
|
| 23 | + return $type; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | class ApplicationType extends DERTaggedType |
| 15 | 15 | { |
| 16 | 16 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
| 17 | - int &$offset): ElementBase |
|
| 17 | + int & $offset): ElementBase |
|
| 18 | 18 | { |
| 19 | 19 | $idx = $offset; |
| 20 | 20 | $type = new self($identifier, $data, $idx); |
@@ -16,53 +16,53 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class ExplicitlyTaggedType extends TaggedTypeWrap implements ExplicitTagging |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param int $tag Tag number |
|
| 23 | - * @param Element $element Wrapped element |
|
| 24 | - * @param int $class Type class |
|
| 25 | - */ |
|
| 26 | - public function __construct(int $tag, Element $element, |
|
| 27 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
| 28 | - { |
|
| 29 | - $this->_typeTag = $tag; |
|
| 30 | - $this->_element = $element; |
|
| 31 | - $this->_class = $class; |
|
| 32 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param int $tag Tag number |
|
| 23 | + * @param Element $element Wrapped element |
|
| 24 | + * @param int $class Type class |
|
| 25 | + */ |
|
| 26 | + public function __construct(int $tag, Element $element, |
|
| 27 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
| 28 | + { |
|
| 29 | + $this->_typeTag = $tag; |
|
| 30 | + $this->_element = $element; |
|
| 31 | + $this->_class = $class; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * |
|
| 36 | - * @see \ASN1\Element::isConstructed() |
|
| 37 | - * @return bool |
|
| 38 | - */ |
|
| 39 | - public function isConstructed(): bool |
|
| 40 | - { |
|
| 41 | - return true; |
|
| 42 | - } |
|
| 34 | + /** |
|
| 35 | + * |
|
| 36 | + * @see \ASN1\Element::isConstructed() |
|
| 37 | + * @return bool |
|
| 38 | + */ |
|
| 39 | + public function isConstructed(): bool |
|
| 40 | + { |
|
| 41 | + return true; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * |
|
| 46 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - protected function _encodedContentDER(): string |
|
| 50 | - { |
|
| 51 | - // get the full encoding of the wrapped element |
|
| 52 | - return $this->_element->toDER(); |
|
| 53 | - } |
|
| 44 | + /** |
|
| 45 | + * |
|
| 46 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + protected function _encodedContentDER(): string |
|
| 50 | + { |
|
| 51 | + // get the full encoding of the wrapped element |
|
| 52 | + return $this->_element->toDER(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 59 | - * @return UnspecifiedType |
|
| 60 | - */ |
|
| 61 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 62 | - { |
|
| 63 | - if (isset($expectedTag)) { |
|
| 64 | - $this->_element->expectType($expectedTag); |
|
| 65 | - } |
|
| 66 | - return $this->_element->asUnspecified(); |
|
| 67 | - } |
|
| 55 | + /** |
|
| 56 | + * |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 59 | + * @return UnspecifiedType |
|
| 60 | + */ |
|
| 61 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 62 | + { |
|
| 63 | + if (isset($expectedTag)) { |
|
| 64 | + $this->_element->expectType($expectedTag); |
|
| 65 | + } |
|
| 66 | + return $this->_element->asUnspecified(); |
|
| 67 | + } |
|
| 68 | 68 | } |