Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/PolicyMappingsExtension.php

raw symbols SCIP
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 * Implements 'Policy Mappings' certificate extension.
21 *
22 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.5
23 */
24 final class PolicyMappingsExtension extends Extension implements Countable, IteratorAggregate
25 {
26 /**
27 * Policy mappings.
28 *
29 * @var PolicyMapping[]
30 */
31 private readonly array $mappings;
32
33 /**
34 * @param PolicyMapping ...$mappings One or more PolicyMapping objects
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 * Get all mappings.
49 *
50 * @return PolicyMapping[]
51 */
52 public function mappings(): array
53 {
54 return $this->mappings;
55 }
56
57 /**
58 * Get mappings flattened into a single array of arrays of subject domains keyed by issuer domain.
59 *
60 * Eg. if policy mappings contains multiple mappings with the same issuer domain policy, their corresponding subject
61 * domain policies are placed under the same key.
62 *
63 * @return (string[])[]
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 * Get all subject domain policy OIDs that are mapped to given issuer domain policy OID.
80 *
81 * @param string $oid Issuer domain policy
82 *
83 * @return string[] List of OIDs in dotted format
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 * Get all mapped issuer domain policy OIDs.
98 *
99 * @return string[]
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 * Check whether policy mappings have anyPolicy mapped.
109 *
110 * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either to or from the special value anyPolicy".
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 * Get the number of mappings.
127 *
128 * @see \Countable::count()
129 */
130 public function count(): int
131 {
132 return count($this->mappings);
133 }
134
135 /**
136 * Get iterator for policy mappings.
137 *
138 * @see \IteratorAggregate::getIterator()
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 }