| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\ASN1; |
| 6 |
|
| 7 |
use BadMethodCallException; |
| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class DERData extends Element |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
private readonly string $der; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private readonly Identifier $identifier; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private int $contentOffset = 0; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private function __construct(string $data) |
| 40 |
{ |
| 41 |
$this->identifier = Identifier::fromDER($data, $this->contentOffset); |
| 42 |
|
| 43 |
Length::expectFromDER($data, $this->contentOffset); |
| 44 |
$this->der = $data; |
| 45 |
parent::__construct($this->identifier->intTag()); |
| 46 |
} |
| 47 |
|
| 48 |
public static function create(string $data): self |
| 49 |
{ |
| 50 |
return new self($data); |
| 51 |
} |
| 52 |
|
| 53 |
public function typeClass(): int |
| 54 |
{ |
| 55 |
return $this->identifier->typeClass(); |
| 56 |
} |
| 57 |
|
| 58 |
public function isConstructed(): bool |
| 59 |
{ |
| 60 |
return $this->identifier->isConstructed(); |
| 61 |
} |
| 62 |
|
| 63 |
public function toDER(): string |
| 64 |
{ |
| 65 |
return $this->der; |
| 66 |
} |
| 67 |
|
| 68 |
protected function encodedAsDER(): string |
| 69 |
{ |
| 70 |
|
| 71 |
if (mb_strlen($this->der, '8bit') === $this->contentOffset) { |
| 72 |
return ''; |
| 73 |
} |
| 74 |
return mb_substr($this->der, $this->contentOffset, null, '8bit'); |
| 75 |
} |
| 76 |
|
| 77 |
protected static function decodeFromDER(Identifier $identifier, string $data, int &$offset): ElementBase |
| 78 |
{ |
| 79 |
throw new BadMethodCallException(__METHOD__ . ' must be implemented in derived class.'); |
| 80 |
} |
| 81 |
} |