Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationPath\PathBuilding;
6
7 use function count;
8 use SpomkyLabs\Pki\X509\Certificate\Certificate;
9 use SpomkyLabs\Pki\X509\Certificate\CertificateBundle;
10 use SpomkyLabs\Pki\X509\CertificationPath\CertificationPath;
11 use SpomkyLabs\Pki\X509\CertificationPath\Exception\PathBuildingException;
12
13 /**
14 * Class for resolving certification paths.
15 *
16 * @see https://tools.ietf.org/html/rfc4158
17 */
18 final class CertificationPathBuilder
19 {
20 /**
21 * @param CertificateBundle $trustList List of trust anchors
22 */
23 private function __construct(
24 private readonly CertificateBundle $trustList
25 ) {
26 }
27
28 public static function create(CertificateBundle $trustList): self
29 {
30 return new self($trustList);
31 }
32
33 /**
34 * Get all certification paths to given target certificate from any trust anchor.
35 *
36 * @param Certificate $target Target certificate
37 * @param null|CertificateBundle $intermediate Optional intermediate certificates
38 *
39 * @return CertificationPath[]
40 */
41 public function allPathsToTarget(Certificate $target, ?CertificateBundle $intermediate = null): array
42 {
43 $paths = $this->resolvePathsToTarget($target, $intermediate);
44 // map paths to CertificationPath objects
45 return array_map(static fn ($certs) => CertificationPath::create(...$certs), $paths);
46 }
47
48 /**
49 * Get the shortest path to given target certificate from any trust anchor.
50 *
51 * @param Certificate $target Target certificate
52 * @param null|CertificateBundle $intermediate Optional intermediate certificates
53 */
54 public function shortestPathToTarget(
55 Certificate $target,
56 ?CertificateBundle $intermediate = null
57 ): CertificationPath {
58 $paths = $this->allPathsToTarget($target, $intermediate);
59 if (count($paths) === 0) {
60 throw new PathBuildingException('No certification paths.');
61 }
62 usort($paths, fn ($a, $b) => count($a) < count($b) ? -1 : 1);
63 return reset($paths);
64 }
65
66 /**
67 * Find all issuers of the target certificate from a given bundle.
68 *
69 * @param Certificate $target Target certificate
70 * @param CertificateBundle $bundle Certificates to search
71 *
72 * @return Certificate[]
73 */
74 private function findIssuers(Certificate $target, CertificateBundle $bundle): array
75 {
76 $issuers = [];
77 $issuer_name = $target->tbsCertificate()
78 ->issuer();
79 $extensions = $target->tbsCertificate()
80 ->extensions();
81 // find by authority key identifier
82 if ($extensions->hasAuthorityKeyIdentifier()) {
83 $ext = $extensions->authorityKeyIdentifier();
84 if ($ext->hasKeyIdentifier()) {
85 foreach ($bundle->allBySubjectKeyIdentifier($ext->keyIdentifier()) as $issuer) {
86 // check that issuer name matches
87 if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) {
88 $issuers[] = $issuer;
89 }
90 }
91 }
92 }
93 return $issuers;
94 }
95
96 /**
97 * Resolve all possible certification paths from any trust anchor to the target certificate, using optional
98 * intermediate certificates.
99 *
100 * Helper method for allPathsToTarget to be called recursively.
101 *
102 * @return array<int, array<Certificate>> Array of arrays containing path certificates
103 * @todo Implement loop detection
104 */
105 private function resolvePathsToTarget(Certificate $target, ?CertificateBundle $intermediate = null): array
106 {
107 // array of possible paths
108 $paths = [];
109 // signed by certificate in the trust list
110 foreach ($this->findIssuers($target, $this->trustList) as $issuer) {
111 // if target is self-signed, path consists of only
112 // the target certificate
113 if ($target->equals($issuer)) {
114 $paths[] = [$target];
115 } else {
116 $paths[] = [$issuer, $target];
117 }
118 }
119 if (isset($intermediate)) {
120 // signed by intermediate certificate
121 foreach ($this->findIssuers($target, $intermediate) as $issuer) {
122 // intermediate certificate must not be self-signed
123 if ($issuer->isSelfIssued()) {
124 continue;
125 }
126 // resolve paths to issuer
127 $subpaths = $this->resolvePathsToTarget($issuer, $intermediate);
128 foreach ($subpaths as $path) {
129 $paths[] = array_merge($path, [$target]);
130 }
131 }
132 }
133
134 return $paths;
135 }
136 }