Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationPath;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\CryptoBridge\Crypto;
13 use SpomkyLabs\Pki\X509\Certificate\Certificate;
14 use SpomkyLabs\Pki\X509\Certificate\CertificateBundle;
15 use SpomkyLabs\Pki\X509\Certificate\CertificateChain;
16 use SpomkyLabs\Pki\X509\CertificationPath\PathBuilding\CertificationPathBuilder;
17 use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidationConfig;
18 use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidationResult;
19 use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidator;
20
21 /**
22 * Implements certification path structure.
23 *
24 * Certification path is a list of certificates from the trust anchor to the end entity certificate, possibly spanning
25 * over multiple intermediate certificates.
26 *
27 * @see https://tools.ietf.org/html/rfc5280#section-3.2
28 */
29 final class CertificationPath implements Countable, IteratorAggregate
30 {
31 /**
32 * Certification path.
33 *
34 * @var Certificate[]
35 */
36 private readonly array $certificates;
37
38 /**
39 * @param Certificate ...$certificates Certificates from the trust anchor
40 * to the target end-entity certificate
41 */
42 private function __construct(Certificate ...$certificates)
43 {
44 $this->certificates = $certificates;
45 }
46
47 public static function create(Certificate ...$certificates): self
48 {
49 return new self(...$certificates);
50 }
51
52 /**
53 * Initialize from a certificate chain.
54 */
55 public static function fromCertificateChain(CertificateChain $chain): self
56 {
57 return self::create(...array_reverse($chain->certificates(), false));
58 }
59
60 /**
61 * Build certification path to given target.
62 *
63 * @param Certificate $target Target end-entity certificate
64 * @param CertificateBundle $trust_anchors List of trust anchors
65 * @param null|CertificateBundle $intermediate Optional intermediate certificates
66 */
67 public static function toTarget(
68 Certificate $target,
69 CertificateBundle $trust_anchors,
70 ?CertificateBundle $intermediate = null
71 ): self {
72 return CertificationPathBuilder::create($trust_anchors)->shortestPathToTarget($target, $intermediate);
73 }
74
75 /**
76 * Build certification path from given trust anchor to target certificate, using intermediate certificates from
77 * given bundle.
78 *
79 * @param Certificate $trust_anchor Trust anchor certificate
80 * @param Certificate $target Target end-entity certificate
81 * @param null|CertificateBundle $intermediate Optional intermediate certificates
82 */
83 public static function fromTrustAnchorToTarget(
84 Certificate $trust_anchor,
85 Certificate $target,
86 ?CertificateBundle $intermediate = null
87 ): self {
88 return self::toTarget($target, CertificateBundle::create($trust_anchor), $intermediate);
89 }
90
91 /**
92 * Get certificates.
93 *
94 * @return Certificate[]
95 */
96 public function certificates(): array
97 {
98 return $this->certificates;
99 }
100
101 /**
102 * Get the trust anchor certificate from the path.
103 */
104 public function trustAnchorCertificate(): Certificate
105 {
106 if (count($this->certificates) === 0) {
107 throw new LogicException('No certificates.');
108 }
109 return $this->certificates[0];
110 }
111
112 /**
113 * Get the end-entity certificate from the path.
114 */
115 public function endEntityCertificate(): Certificate
116 {
117 if (count($this->certificates) === 0) {
118 throw new LogicException('No certificates.');
119 }
120 return $this->certificates[count($this->certificates) - 1];
121 }
122
123 /**
124 * Get certification path as a certificate chain.
125 */
126 public function certificateChain(): CertificateChain
127 {
128 return CertificateChain::create(...array_reverse($this->certificates, false));
129 }
130
131 /**
132 * Check whether certification path starts with one ore more given certificates in parameter order.
133 *
134 * @param Certificate ...$certs Certificates
135 */
136 public function startsWith(Certificate ...$certs): bool
137 {
138 $n = count($certs);
139 if ($n > count($this->certificates)) {
140 return false;
141 }
142 for ($i = 0; $i < $n; ++$i) {
143 if (! $certs[$i]->equals($this->certificates[$i])) {
144 return false;
145 }
146 }
147 return true;
148 }
149
150 /**
151 * Validate certification path.
152 *
153 * @param null|Crypto $crypto Crypto engine, use default if not set
154 */
155 public function validate(PathValidationConfig $config, ?Crypto $crypto = null): PathValidationResult
156 {
157 $crypto ??= Crypto::getDefault();
158 return PathValidator::create($crypto, $config, ...$this->certificates)->validate();
159 }
160
161 /**
162 * @see \Countable::count()
163 */
164 public function count(): int
165 {
166 return count($this->certificates);
167 }
168
169 /**
170 * Get iterator for certificates.
171 *
172 * @see \IteratorAggregate::getIterator()
173 */
174 public function getIterator(): ArrayIterator
175 {
176 return new ArrayIterator($this->certificates);
177 }
178 }