| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate\Extension; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use LogicException; |
| 12 |
use SpomkyLabs\Pki\ASN1\Element; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 15 |
use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation; |
| 16 |
use SpomkyLabs\Pki\X509\Certificate\Extension\PolicyMappings\PolicyMapping; |
| 17 |
use UnexpectedValueException; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final class PolicyMappingsExtension extends Extension implements Countable, IteratorAggregate |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private readonly array $mappings; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private function __construct(bool $critical, PolicyMapping ...$mappings) |
| 37 |
{ |
| 38 |
parent::__construct(self::OID_POLICY_MAPPINGS, $critical); |
| 39 |
$this->mappings = $mappings; |
| 40 |
} |
| 41 |
|
| 42 |
public static function create(bool $critical, PolicyMapping ...$mappings): self |
| 43 |
{ |
| 44 |
return new self($critical, ...$mappings); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function mappings(): array |
| 53 |
{ |
| 54 |
return $this->mappings; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function flattenedMappings(): array |
| 66 |
{ |
| 67 |
$mappings = []; |
| 68 |
foreach ($this->mappings as $mapping) { |
| 69 |
$idp = $mapping->issuerDomainPolicy(); |
| 70 |
if (! isset($mappings[$idp])) { |
| 71 |
$mappings[$idp] = []; |
| 72 |
} |
| 73 |
array_push($mappings[$idp], $mapping->subjectDomainPolicy()); |
| 74 |
} |
| 75 |
return $mappings; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function issuerMappings(string $oid): array |
| 86 |
{ |
| 87 |
$oids = []; |
| 88 |
foreach ($this->mappings as $mapping) { |
| 89 |
if ($mapping->issuerDomainPolicy() === $oid) { |
| 90 |
$oids[] = $mapping->subjectDomainPolicy(); |
| 91 |
} |
| 92 |
} |
| 93 |
return $oids; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function issuerDomainPolicies(): array |
| 102 |
{ |
| 103 |
$idps = array_map(static fn (PolicyMapping $mapping) => $mapping->issuerDomainPolicy(), $this->mappings); |
| 104 |
return array_values(array_unique($idps)); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function hasAnyPolicyMapping(): bool |
| 113 |
{ |
| 114 |
foreach ($this->mappings as $mapping) { |
| 115 |
if ($mapping->issuerDomainPolicy() === PolicyInformation::OID_ANY_POLICY) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
if ($mapping->subjectDomainPolicy() === PolicyInformation::OID_ANY_POLICY) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
} |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function count(): int |
| 131 |
{ |
| 132 |
return count($this->mappings); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function getIterator(): ArrayIterator |
| 141 |
{ |
| 142 |
return new ArrayIterator($this->mappings); |
| 143 |
} |
| 144 |
|
| 145 |
protected static function fromDER(string $data, bool $critical): static |
| 146 |
{ |
| 147 |
$mappings = array_map( |
| 148 |
static fn (UnspecifiedType $el) => PolicyMapping::fromASN1($el->asSequence()), |
| 149 |
UnspecifiedType::fromDER($data)->asSequence()->elements() |
| 150 |
); |
| 151 |
if (count($mappings) === 0) { |
| 152 |
throw new UnexpectedValueException('PolicyMappings must have at least one mapping.'); |
| 153 |
} |
| 154 |
return self::create($critical, ...$mappings); |
| 155 |
} |
| 156 |
|
| 157 |
protected function valueASN1(): Element |
| 158 |
{ |
| 159 |
if (count($this->mappings) === 0) { |
| 160 |
throw new LogicException('No mappings.'); |
| 161 |
} |
| 162 |
$elements = array_map(static fn (PolicyMapping $mapping) => $mapping->toASN1(), $this->mappings); |
| 163 |
return Sequence::create(...$elements); |
| 164 |
} |
| 165 |
} |