Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use SpomkyLabs\Pki\CryptoEncoding\PEM;
12 use SpomkyLabs\Pki\CryptoEncoding\PEMBundle;
13
14 /**
15 * Implements a list of certificates.
16 */
17 final class CertificateBundle implements Countable, IteratorAggregate
18 {
19 /**
20 * Certificates.
21 *
22 * @var Certificate[]
23 */
24 private array $certs;
25
26 /**
27 * Mapping from public key id to array of certificates.
28 *
29 * @var null|(Certificate[])[]
30 */
31 private ?array $keyIdMap = null;
32
33 /**
34 * @param Certificate ...$certs Certificate objects
35 */
36 private function __construct(Certificate ...$certs)
37 {
38 $this->certs = $certs;
39 }
40
41 /**
42 * Reset internal cached variables on clone.
43 */
44 public function __clone()
45 {
46 $this->keyIdMap = null;
47 }
48
49 public static function create(Certificate ...$certs): self
50 {
51 return new self(...$certs);
52 }
53
54 /**
55 * Initialize from PEMs.
56 *
57 * @param PEM ...$pems PEM objects
58 */
59 public static function fromPEMs(PEM ...$pems): self
60 {
61 $certs = array_map(Certificate::fromPEM(...), $pems);
62 return self::create(...$certs);
63 }
64
65 /**
66 * Initialize from PEM bundle.
67 */
68 public static function fromPEMBundle(PEMBundle $pem_bundle): self
69 {
70 return self::fromPEMs(...$pem_bundle->all());
71 }
72
73 /**
74 * Get self with certificates added.
75 */
76 public function withCertificates(Certificate ...$cert): self
77 {
78 $obj = clone $this;
79 $obj->certs = array_merge($obj->certs, $cert);
80 return $obj;
81 }
82
83 /**
84 * Get self with certificates from PEMBundle added.
85 */
86 public function withPEMBundle(PEMBundle $pem_bundle): self
87 {
88 $certs = $this->certs;
89 foreach ($pem_bundle as $pem) {
90 $certs[] = Certificate::fromPEM($pem);
91 }
92 return self::create(...$certs);
93 }
94
95 /**
96 * Get self with single certificate from PEM added.
97 */
98 public function withPEM(PEM $pem): self
99 {
100 $certs = $this->certs;
101 $certs[] = Certificate::fromPEM($pem);
102 return self::create(...$certs);
103 }
104
105 /**
106 * Check whether bundle contains a given certificate.
107 */
108 public function contains(Certificate $cert): bool
109 {
110 $id = self::_getCertKeyId($cert);
111 $map = $this->_getKeyIdMap();
112 if (! isset($map[$id])) {
113 return false;
114 }
115 foreach ($map[$id] as $c) {
116 /** @var Certificate $c */
117 if ($cert->equals($c)) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124 /**
125 * Get all certificates that have given subject key identifier.
126 *
127 * @return Certificate[]
128 */
129 public function allBySubjectKeyIdentifier(string $id): array
130 {
131 $map = $this->_getKeyIdMap();
132 if (! isset($map[$id])) {
133 return [];
134 }
135 return $map[$id];
136 }
137
138 /**
139 * Get all certificates in a bundle.
140 *
141 * @return Certificate[]
142 */
143 public function all(): array
144 {
145 return $this->certs;
146 }
147
148 /**
149 * @see \Countable::count()
150 */
151 public function count(): int
152 {
153 return count($this->certs);
154 }
155
156 /**
157 * Get iterator for certificates.
158 *
159 * @see \IteratorAggregate::getIterator()
160 */
161 public function getIterator(): ArrayIterator
162 {
163 return new ArrayIterator($this->certs);
164 }
165
166 /**
167 * Get certificate mapping by public key id.
168 *
169 * @return (Certificate[])[]
170 */
171 private function _getKeyIdMap(): array
172 {
173 // lazily build mapping
174 if (! isset($this->keyIdMap)) {
175 $this->keyIdMap = [];
176 foreach ($this->certs as $cert) {
177 $id = self::_getCertKeyId($cert);
178 if (! isset($this->keyIdMap[$id])) {
179 $this->keyIdMap[$id] = [];
180 }
181 array_push($this->keyIdMap[$id], $cert);
182 }
183 }
184 return $this->keyIdMap;
185 }
186
187 /**
188 * Get public key id for the certificate.
189 */
190 private static function _getCertKeyId(Certificate $cert): string
191 {
192 $exts = $cert->tbsCertificate()
193 ->extensions();
194 if ($exts->hasSubjectKeyIdentifier()) {
195 return $exts->subjectKeyIdentifier()
196 ->keyIdentifier();
197 }
198 return $cert->tbsCertificate()
199 ->subjectPublicKeyInfo()
200 ->keyIdentifier();
201 }
202 }