| 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\Boolean; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class AAControlsExtension extends Extension |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private function __construct( |
| 28 |
bool $critical, |
| 29 |
private readonly ?int $pathLenConstraint, |
| 30 |
private readonly ?array $permittedAttrs, |
| 31 |
private readonly ?array $excludedAttrs, |
| 32 |
private readonly bool $permitUnSpecified |
| 33 |
) { |
| 34 |
parent::__construct(self::OID_AA_CONTROLS, $critical); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static function create( |
| 44 |
bool $critical, |
| 45 |
?int $pathLenConstraint = null, |
| 46 |
?array $permittedAttrs = null, |
| 47 |
?array $excludedAttrs = null, |
| 48 |
bool $permitUnSpecified = true |
| 49 |
): self { |
| 50 |
return new self($critical, $pathLenConstraint, $permittedAttrs, $excludedAttrs, $permitUnSpecified); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function hasPathLen(): bool |
| 57 |
{ |
| 58 |
return isset($this->pathLenConstraint); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function pathLen(): int |
| 65 |
{ |
| 66 |
if (! $this->hasPathLen()) { |
| 67 |
throw new LogicException('pathLen not set.'); |
| 68 |
} |
| 69 |
return $this->pathLenConstraint; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function hasPermittedAttrs(): bool |
| 76 |
{ |
| 77 |
return isset($this->permittedAttrs); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function permittedAttrs(): array |
| 86 |
{ |
| 87 |
if (! $this->hasPermittedAttrs()) { |
| 88 |
throw new LogicException('permittedAttrs not set.'); |
| 89 |
} |
| 90 |
return $this->permittedAttrs; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function hasExcludedAttrs(): bool |
| 97 |
{ |
| 98 |
return isset($this->excludedAttrs); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function excludedAttrs(): array |
| 107 |
{ |
| 108 |
if (! $this->hasExcludedAttrs()) { |
| 109 |
throw new LogicException('excludedAttrs not set.'); |
| 110 |
} |
| 111 |
return $this->excludedAttrs; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function permitUnspecified(): bool |
| 118 |
{ |
| 119 |
return $this->permitUnSpecified; |
| 120 |
} |
| 121 |
|
| 122 |
protected static function fromDER(string $data, bool $critical): static |
| 123 |
{ |
| 124 |
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
| 125 |
$path_len = null; |
| 126 |
$permitted = null; |
| 127 |
$excluded = null; |
| 128 |
$permit_unspecified = true; |
| 129 |
$idx = 0; |
| 130 |
if ($seq->has($idx, Element::TYPE_INTEGER)) { |
| 131 |
$path_len = $seq->at($idx++) |
| 132 |
->asInteger() |
| 133 |
->intNumber(); |
| 134 |
} |
| 135 |
if ($seq->hasTagged(0)) { |
| 136 |
$attr_seq = $seq->getTagged(0) |
| 137 |
->asImplicit(Element::TYPE_SEQUENCE) |
| 138 |
->asSequence(); |
| 139 |
$permitted = array_map( |
| 140 |
static fn (UnspecifiedType $el) => $el->asObjectIdentifier() |
| 141 |
->oid(), |
| 142 |
$attr_seq->elements() |
| 143 |
); |
| 144 |
++$idx; |
| 145 |
} |
| 146 |
if ($seq->hasTagged(1)) { |
| 147 |
$attr_seq = $seq->getTagged(1) |
| 148 |
->asImplicit(Element::TYPE_SEQUENCE) |
| 149 |
->asSequence(); |
| 150 |
$excluded = array_map( |
| 151 |
static fn (UnspecifiedType $el) => $el->asObjectIdentifier() |
| 152 |
->oid(), |
| 153 |
$attr_seq->elements() |
| 154 |
); |
| 155 |
++$idx; |
| 156 |
} |
| 157 |
if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
| 158 |
$permit_unspecified = $seq->at($idx++) |
| 159 |
->asBoolean() |
| 160 |
->value(); |
| 161 |
} |
| 162 |
return self::create($critical, $path_len, $permitted, $excluded, $permit_unspecified); |
| 163 |
} |
| 164 |
|
| 165 |
protected function valueASN1(): Element |
| 166 |
{ |
| 167 |
$elements = []; |
| 168 |
if (isset($this->pathLenConstraint)) { |
| 169 |
$elements[] = Integer::create($this->pathLenConstraint); |
| 170 |
} |
| 171 |
if (isset($this->permittedAttrs)) { |
| 172 |
$oids = array_map(ObjectIdentifier::create(...), $this->permittedAttrs); |
| 173 |
$elements[] = ImplicitlyTaggedType::create(0, Sequence::create(...$oids)); |
| 174 |
} |
| 175 |
if (isset($this->excludedAttrs)) { |
| 176 |
$oids = array_map(ObjectIdentifier::create(...), $this->excludedAttrs); |
| 177 |
$elements[] = ImplicitlyTaggedType::create(1, Sequence::create(...$oids)); |
| 178 |
} |
| 179 |
if ($this->permitUnSpecified !== true) { |
| 180 |
$elements[] = Boolean::create(false); |
| 181 |
} |
| 182 |
return Sequence::create(...$elements); |
| 183 |
} |
| 184 |
} |