| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate\Extension; |
| 6 |
|
| 7 |
use LogicException; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 10 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class PolicyConstraintsExtension extends Extension |
| 20 |
{ |
| 21 |
private function __construct( |
| 22 |
bool $critical, |
| 23 |
private readonly ?int $requireExplicitPolicy, |
| 24 |
private readonly ?int $inhibitPolicyMapping |
| 25 |
) { |
| 26 |
parent::__construct(self::OID_POLICY_CONSTRAINTS, $critical); |
| 27 |
} |
| 28 |
|
| 29 |
public static function create( |
| 30 |
bool $critical, |
| 31 |
?int $requireExplicitPolicy = null, |
| 32 |
?int $inhibitPolicyMapping = null |
| 33 |
): self { |
| 34 |
return new self($critical, $requireExplicitPolicy, $inhibitPolicyMapping); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function hasRequireExplicitPolicy(): bool |
| 41 |
{ |
| 42 |
return isset($this->requireExplicitPolicy); |
| 43 |
} |
| 44 |
|
| 45 |
public function requireExplicitPolicy(): int |
| 46 |
{ |
| 47 |
if (! $this->hasRequireExplicitPolicy()) { |
| 48 |
throw new LogicException('requireExplicitPolicy not set.'); |
| 49 |
} |
| 50 |
return $this->requireExplicitPolicy; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function hasInhibitPolicyMapping(): bool |
| 57 |
{ |
| 58 |
return isset($this->inhibitPolicyMapping); |
| 59 |
} |
| 60 |
|
| 61 |
public function inhibitPolicyMapping(): int |
| 62 |
{ |
| 63 |
if (! $this->hasInhibitPolicyMapping()) { |
| 64 |
throw new LogicException('inhibitPolicyMapping not set.'); |
| 65 |
} |
| 66 |
return $this->inhibitPolicyMapping; |
| 67 |
} |
| 68 |
|
| 69 |
protected static function fromDER(string $data, bool $critical): static |
| 70 |
{ |
| 71 |
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
| 72 |
$require_explicit_policy = null; |
| 73 |
$inhibit_policy_mapping = null; |
| 74 |
if ($seq->hasTagged(0)) { |
| 75 |
$require_explicit_policy = $seq->getTagged(0) |
| 76 |
->asImplicit(Element::TYPE_INTEGER)->asInteger()->intNumber(); |
| 77 |
} |
| 78 |
if ($seq->hasTagged(1)) { |
| 79 |
$inhibit_policy_mapping = $seq->getTagged(1) |
| 80 |
->asImplicit(Element::TYPE_INTEGER)->asInteger()->intNumber(); |
| 81 |
} |
| 82 |
return self::create($critical, $require_explicit_policy, $inhibit_policy_mapping); |
| 83 |
} |
| 84 |
|
| 85 |
protected function valueASN1(): Element |
| 86 |
{ |
| 87 |
$elements = []; |
| 88 |
if (isset($this->requireExplicitPolicy)) { |
| 89 |
$elements[] = ImplicitlyTaggedType::create(0, Integer::create($this->requireExplicitPolicy)); |
| 90 |
} |
| 91 |
if (isset($this->inhibitPolicyMapping)) { |
| 92 |
$elements[] = ImplicitlyTaggedType::create(1, Integer::create($this->inhibitPolicyMapping)); |
| 93 |
} |
| 94 |
return Sequence::create(...$elements); |
| 95 |
} |
| 96 |
} |