| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\ASN1; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use function mb_strlen; |
| 9 |
use SpomkyLabs\Pki\ASN1\Component\Identifier; |
| 10 |
use SpomkyLabs\Pki\ASN1\Component\Length; |
| 11 |
use SpomkyLabs\Pki\ASN1\Feature\ElementBase; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\Constructed; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\ConstructedString; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 15 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Set; |
| 16 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString; |
| 17 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\BMPString; |
| 18 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Boolean; |
| 19 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\CharacterString; |
| 20 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Enumerated; |
| 21 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\EOC; |
| 22 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\GeneralizedTime; |
| 23 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\GeneralString; |
| 24 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\GraphicString; |
| 25 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\IA5String; |
| 26 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 27 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\NullType; |
| 28 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\NumericString; |
| 29 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectDescriptor; |
| 30 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier; |
| 31 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString; |
| 32 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\PrintableString; |
| 33 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Real; |
| 34 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\RelativeOID; |
| 35 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\T61String; |
| 36 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\UniversalString; |
| 37 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\UTCTime; |
| 38 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\UTF8String; |
| 39 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\VideotexString; |
| 40 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\VisibleString; |
| 41 |
use SpomkyLabs\Pki\ASN1\Type\StringType; |
| 42 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ApplicationType; |
| 43 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ContextSpecificType; |
| 44 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\PrivateType; |
| 45 |
use SpomkyLabs\Pki\ASN1\Type\TaggedType; |
| 46 |
use SpomkyLabs\Pki\ASN1\Type\TimeType; |
| 47 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 48 |
use function sprintf; |
| 49 |
use UnexpectedValueException; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
abstract class Element implements ElementBase |
| 56 |
{ |
| 57 |
|
| 58 |
public const TYPE_EOC = 0x00; |
| 59 |
|
| 60 |
public const TYPE_BOOLEAN = 0x01; |
| 61 |
|
| 62 |
public const TYPE_INTEGER = 0x02; |
| 63 |
|
| 64 |
public const TYPE_BIT_STRING = 0x03; |
| 65 |
|
| 66 |
public const TYPE_OCTET_STRING = 0x04; |
| 67 |
|
| 68 |
public const TYPE_NULL = 0x05; |
| 69 |
|
| 70 |
public const TYPE_OBJECT_IDENTIFIER = 0x06; |
| 71 |
|
| 72 |
public const TYPE_OBJECT_DESCRIPTOR = 0x07; |
| 73 |
|
| 74 |
public const TYPE_EXTERNAL = 0x08; |
| 75 |
|
| 76 |
public const TYPE_REAL = 0x09; |
| 77 |
|
| 78 |
public const TYPE_ENUMERATED = 0x0a; |
| 79 |
|
| 80 |
public const TYPE_EMBEDDED_PDV = 0x0b; |
| 81 |
|
| 82 |
public const TYPE_UTF8_STRING = 0x0c; |
| 83 |
|
| 84 |
public const TYPE_RELATIVE_OID = 0x0d; |
| 85 |
|
| 86 |
public const TYPE_SEQUENCE = 0x10; |
| 87 |
|
| 88 |
public const TYPE_SET = 0x11; |
| 89 |
|
| 90 |
public const TYPE_NUMERIC_STRING = 0x12; |
| 91 |
|
| 92 |
public const TYPE_PRINTABLE_STRING = 0x13; |
| 93 |
|
| 94 |
public const TYPE_T61_STRING = 0x14; |
| 95 |
|
| 96 |
public const TYPE_VIDEOTEX_STRING = 0x15; |
| 97 |
|
| 98 |
public const TYPE_IA5_STRING = 0x16; |
| 99 |
|
| 100 |
public const TYPE_UTC_TIME = 0x17; |
| 101 |
|
| 102 |
public const TYPE_GENERALIZED_TIME = 0x18; |
| 103 |
|
| 104 |
public const TYPE_GRAPHIC_STRING = 0x19; |
| 105 |
|
| 106 |
public const TYPE_VISIBLE_STRING = 0x1a; |
| 107 |
|
| 108 |
public const TYPE_GENERAL_STRING = 0x1b; |
| 109 |
|
| 110 |
public const TYPE_UNIVERSAL_STRING = 0x1c; |
| 111 |
|
| 112 |
public const TYPE_CHARACTER_STRING = 0x1d; |
| 113 |
|
| 114 |
public const TYPE_BMP_STRING = 0x1e; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public const TYPE_STRING = -1; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public const TYPE_TIME = -2; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public const TYPE_CONSTRUCTED_STRING = -3; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private const MAP_TAG_TO_CLASS = [ |
| 151 |
self::TYPE_EOC => EOC::class, |
| 152 |
self::TYPE_BOOLEAN => Boolean::class, |
| 153 |
self::TYPE_INTEGER => Integer::class, |
| 154 |
self::TYPE_BIT_STRING => BitString::class, |
| 155 |
self::TYPE_OCTET_STRING => OctetString::class, |
| 156 |
self::TYPE_NULL => NullType::class, |
| 157 |
self::TYPE_OBJECT_IDENTIFIER => ObjectIdentifier::class, |
| 158 |
self::TYPE_OBJECT_DESCRIPTOR => ObjectDescriptor::class, |
| 159 |
self::TYPE_REAL => Real::class, |
| 160 |
self::TYPE_ENUMERATED => Enumerated::class, |
| 161 |
self::TYPE_UTF8_STRING => UTF8String::class, |
| 162 |
self::TYPE_RELATIVE_OID => RelativeOID::class, |
| 163 |
self::TYPE_SEQUENCE => Sequence::class, |
| 164 |
self::TYPE_SET => Set::class, |
| 165 |
self::TYPE_NUMERIC_STRING => NumericString::class, |
| 166 |
self::TYPE_PRINTABLE_STRING => PrintableString::class, |
| 167 |
self::TYPE_T61_STRING => T61String::class, |
| 168 |
self::TYPE_VIDEOTEX_STRING => VideotexString::class, |
| 169 |
self::TYPE_IA5_STRING => IA5String::class, |
| 170 |
self::TYPE_UTC_TIME => UTCTime::class, |
| 171 |
self::TYPE_GENERALIZED_TIME => GeneralizedTime::class, |
| 172 |
self::TYPE_GRAPHIC_STRING => GraphicString::class, |
| 173 |
self::TYPE_VISIBLE_STRING => VisibleString::class, |
| 174 |
self::TYPE_GENERAL_STRING => GeneralString::class, |
| 175 |
self::TYPE_UNIVERSAL_STRING => UniversalString::class, |
| 176 |
self::TYPE_CHARACTER_STRING => CharacterString::class, |
| 177 |
self::TYPE_BMP_STRING => BMPString::class, |
| 178 |
]; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private const MAP_TYPE_TO_NAME = [ |
| 188 |
self::TYPE_EOC => 'EOC', |
| 189 |
self::TYPE_BOOLEAN => 'BOOLEAN', |
| 190 |
self::TYPE_INTEGER => 'INTEGER', |
| 191 |
self::TYPE_BIT_STRING => 'BIT STRING', |
| 192 |
self::TYPE_OCTET_STRING => 'OCTET STRING', |
| 193 |
self::TYPE_NULL => 'NULL', |
| 194 |
self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
| 195 |
self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
| 196 |
self::TYPE_EXTERNAL => 'EXTERNAL', |
| 197 |
self::TYPE_REAL => 'REAL', |
| 198 |
self::TYPE_ENUMERATED => 'ENUMERATED', |
| 199 |
self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
| 200 |
self::TYPE_UTF8_STRING => 'UTF8String', |
| 201 |
self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
| 202 |
self::TYPE_SEQUENCE => 'SEQUENCE', |
| 203 |
self::TYPE_SET => 'SET', |
| 204 |
self::TYPE_NUMERIC_STRING => 'NumericString', |
| 205 |
self::TYPE_PRINTABLE_STRING => 'PrintableString', |
| 206 |
self::TYPE_T61_STRING => 'T61String', |
| 207 |
self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
| 208 |
self::TYPE_IA5_STRING => 'IA5String', |
| 209 |
self::TYPE_UTC_TIME => 'UTCTime', |
| 210 |
self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
| 211 |
self::TYPE_GRAPHIC_STRING => 'GraphicString', |
| 212 |
self::TYPE_VISIBLE_STRING => 'VisibleString', |
| 213 |
self::TYPE_GENERAL_STRING => 'GeneralString', |
| 214 |
self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
| 215 |
self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
| 216 |
self::TYPE_BMP_STRING => 'BMPString', |
| 217 |
self::TYPE_STRING => 'Any String', |
| 218 |
self::TYPE_TIME => 'Any Time', |
| 219 |
self::TYPE_CONSTRUCTED_STRING => 'Constructed String', |
| 220 |
]; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
protected function __construct( |
| 226 |
protected readonly int $typeTag, |
| 227 |
protected bool $indefiniteLength = false |
| 228 |
) { |
| 229 |
} |
| 230 |
|
| 231 |
abstract public function typeClass(): int; |
| 232 |
|
| 233 |
abstract public function isConstructed(): bool; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public static function fromDER(string $data, ?int &$offset = null): static |
| 245 |
{ |
| 246 |
$idx = $offset ?? 0; |
| 247 |
|
| 248 |
$identifier = Identifier::fromDER($data, $idx); |
| 249 |
|
| 250 |
$cls = self::determineImplClass($identifier); |
| 251 |
|
| 252 |
$element = $cls::decodeFromDER($identifier, $data, $idx); |
| 253 |
|
| 254 |
|
| 255 |
$called_class = static::class; |
| 256 |
if ($called_class !== self::class) { |
| 257 |
if (! $element instanceof $called_class) { |
| 258 |
throw new UnexpectedValueException(sprintf('%s expected, got %s.', $called_class, $element::class)); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if (isset($offset)) { |
| 263 |
$offset = $idx; |
| 264 |
} |
| 265 |
return $element; |
| 266 |
} |
| 267 |
|
| 268 |
public function toDER(): string |
| 269 |
{ |
| 270 |
$identifier = Identifier::create( |
| 271 |
$this->typeClass(), |
| 272 |
$this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
| 273 |
$this->typeTag |
| 274 |
); |
| 275 |
$content = $this->encodedAsDER(); |
| 276 |
if ($this->indefiniteLength) { |
| 277 |
$length = Length::create(0, true); |
| 278 |
$eoc = EOC::create(); |
| 279 |
return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER(); |
| 280 |
} |
| 281 |
$length = Length::create(mb_strlen($content, '8bit')); |
| 282 |
return $identifier->toDER() . $length->toDER() . $content; |
| 283 |
} |
| 284 |
|
| 285 |
public function tag(): int |
| 286 |
{ |
| 287 |
return $this->typeTag; |
| 288 |
} |
| 289 |
|
| 290 |
public function isType(int $tag): bool |
| 291 |
{ |
| 292 |
|
| 293 |
if ($this->typeClass() === Identifier::CLASS_CONTEXT_SPECIFIC) { |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
if ($tag < 0) { |
| 298 |
return $this->isPseudoType($tag); |
| 299 |
} |
| 300 |
return $this->isConcreteType($tag); |
| 301 |
} |
| 302 |
|
| 303 |
public function expectType(int $tag): ElementBase |
| 304 |
{ |
| 305 |
if (! $this->isType($tag)) { |
| 306 |
throw new UnexpectedValueException( |
| 307 |
sprintf('%s expected, got %s.', self::tagToName($tag), $this->typeDescriptorString()) |
| 308 |
); |
| 309 |
} |
| 310 |
return $this; |
| 311 |
} |
| 312 |
|
| 313 |
public function isTagged(): bool |
| 314 |
{ |
| 315 |
return $this instanceof TaggedType; |
| 316 |
} |
| 317 |
|
| 318 |
public function expectTagged(?int $tag = null): TaggedType |
| 319 |
{ |
| 320 |
if (! $this->isTagged()) { |
| 321 |
throw new UnexpectedValueException( |
| 322 |
sprintf('Context specific element expected, got %s.', Identifier::classToName($this->typeClass())) |
| 323 |
); |
| 324 |
} |
| 325 |
if (isset($tag) && $this->tag() !== $tag) { |
| 326 |
throw new UnexpectedValueException(sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
| 327 |
} |
| 328 |
return $this; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function hasIndefiniteLength(): bool |
| 335 |
{ |
| 336 |
return $this->indefiniteLength; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public function withIndefiniteLength(bool $indefinite = true): self |
| 345 |
{ |
| 346 |
$obj = clone $this; |
| 347 |
$obj->indefiniteLength = $indefinite; |
| 348 |
return $obj; |
| 349 |
} |
| 350 |
|
| 351 |
final public function asElement(): self |
| 352 |
{ |
| 353 |
return $this; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public function asUnspecified(): UnspecifiedType |
| 360 |
{ |
| 361 |
return UnspecifiedType::create($this); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public static function tagToName(int $tag): string |
| 368 |
{ |
| 369 |
if (! array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
| 370 |
return "TAG {$tag}"; |
| 371 |
} |
| 372 |
return self::MAP_TYPE_TO_NAME[$tag]; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
abstract protected function encodedAsDER(): string; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
abstract protected static function decodeFromDER(Identifier $identifier, string $data, int &$offset): ElementBase; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
protected static function determineImplClass(Identifier $identifier): string |
| 397 |
{ |
| 398 |
switch ($identifier->typeClass()) { |
| 399 |
case Identifier::CLASS_UNIVERSAL: |
| 400 |
$cls = self::determineUniversalImplClass($identifier->intTag()); |
| 401 |
|
| 402 |
if ($identifier->isConstructed() |
| 403 |
&& is_subclass_of($cls, StringType::class)) { |
| 404 |
$cls = ConstructedString::class; |
| 405 |
} |
| 406 |
return $cls; |
| 407 |
case Identifier::CLASS_CONTEXT_SPECIFIC: |
| 408 |
return ContextSpecificType::class; |
| 409 |
case Identifier::CLASS_APPLICATION: |
| 410 |
return ApplicationType::class; |
| 411 |
case Identifier::CLASS_PRIVATE: |
| 412 |
return PrivateType::class; |
| 413 |
} |
| 414 |
throw new UnexpectedValueException(sprintf( |
| 415 |
'%s %d not implemented.', |
| 416 |
Identifier::classToName($identifier->typeClass()), |
| 417 |
$identifier->tag() |
| 418 |
)); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
protected static function determineUniversalImplClass(int $tag): string |
| 427 |
{ |
| 428 |
if (! array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
| 429 |
throw new UnexpectedValueException("Universal tag {$tag} not implemented."); |
| 430 |
} |
| 431 |
return self::MAP_TAG_TO_CLASS[$tag]; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
protected function typeDescriptorString(): string |
| 438 |
{ |
| 439 |
if ($this->typeClass() === Identifier::CLASS_UNIVERSAL) { |
| 440 |
return self::tagToName($this->typeTag); |
| 441 |
} |
| 442 |
return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), $this->typeTag); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
private function isConcreteType(int $tag): bool |
| 449 |
{ |
| 450 |
|
| 451 |
if ($this->tag() !== $tag) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
|
| 455 |
if ($this->typeClass() === Identifier::CLASS_UNIVERSAL) { |
| 456 |
$cls = self::determineUniversalImplClass($tag); |
| 457 |
if (! $this instanceof $cls) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
} |
| 461 |
return true; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
private function isPseudoType(int $tag): bool |
| 468 |
{ |
| 469 |
return match ($tag) { |
| 470 |
self::TYPE_STRING => $this instanceof StringType, |
| 471 |
self::TYPE_TIME => $this instanceof TimeType, |
| 472 |
self::TYPE_CONSTRUCTED_STRING => $this instanceof ConstructedString, |
| 473 |
default => false, |
| 474 |
}; |
| 475 |
} |
| 476 |
} |