|
12 | 12 |
|
13 | 13 | trait OrderCertificatesTrait { |
14 | 14 | public function orderCertificates(array $certificates): array { |
15 | | - $this->validateCertificateStructure($certificates); |
16 | | - $remainingCerts = []; |
| 15 | + if (empty($certificates)) { |
| 16 | + throw new InvalidArgumentException('Certificate list cannot be empty'); |
| 17 | + } |
| 18 | + |
| 19 | + $this->ensureValidStructure($certificates); |
| 20 | + |
| 21 | + return match (true) { |
| 22 | + count($certificates) === 1 => $certificates, |
| 23 | + default => $this->buildChain($certificates) |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + private function buildChain(array $certificates): array { |
| 28 | + $leaf = $this->findLeafCertificate($certificates); |
| 29 | + if (!$leaf) { |
| 30 | + return $certificates; |
| 31 | + } |
17 | 32 |
|
18 | | - // Add the root cert at ordered list and collect the remaining certs |
| 33 | + $ordered = [$leaf]; |
| 34 | + $remaining = $this->excludeCertificate($certificates, $leaf); |
| 35 | + |
| 36 | + while ($remaining && !$this->isRootCertificate(end($ordered))) { |
| 37 | + [$next, $remaining] = $this->findIssuer(end($ordered), $remaining); |
| 38 | + if (!$next) { |
| 39 | + break; |
| 40 | + } |
| 41 | + $ordered[] = $next; |
| 42 | + } |
| 43 | + |
| 44 | + return [...$ordered, ...$remaining]; |
| 45 | + } |
| 46 | + |
| 47 | + private function ensureValidStructure(array $certificates): void { |
19 | 48 | foreach ($certificates as $cert) { |
20 | | - if (!$this->arrayDiffCanonicalized($cert['subject'], $cert['issuer'])) { |
21 | | - $ordered = [$cert]; |
22 | | - continue; |
| 49 | + if (!is_array($cert) || !isset($cert['subject'], $cert['issuer'], $cert['name'])) { |
| 50 | + throw new InvalidArgumentException('Invalid certificate structure. Certificate must have "subject", "issuer", and "name".'); |
| 51 | + } |
| 52 | + if (!is_array($cert['subject']) || !is_array($cert['issuer'])) { |
| 53 | + throw new InvalidArgumentException('Invalid certificate structure. Certificate must have "subject", "issuer", and "name".'); |
23 | 54 | } |
24 | | - $remainingCerts[$cert['name']] = $cert; |
25 | 55 | } |
26 | 56 |
|
27 | | - if (!isset($ordered)) { |
28 | | - return $certificates; |
| 57 | + $names = array_column($certificates, 'name'); |
| 58 | + if (count($names) !== count(array_unique($names))) { |
| 59 | + throw new InvalidArgumentException('Duplicate certificate names detected'); |
29 | 60 | } |
| 61 | + } |
30 | 62 |
|
| 63 | + private function findLeafCertificate(array $certificates): ?array { |
| 64 | + $issuers = []; |
| 65 | + foreach ($certificates as $cert) { |
| 66 | + if (isset($cert['issuer'])) { |
| 67 | + $issuers[] = $this->normalizeDistinguishedName($cert['issuer']); |
| 68 | + } |
| 69 | + } |
31 | 70 |
|
32 | | - while (!empty($remainingCerts)) { |
33 | | - $found = false; |
34 | | - foreach ($remainingCerts as $name => $cert) { |
35 | | - $first = reset($ordered); |
36 | | - if (!$this->arrayDiffCanonicalized($first['subject'], $cert['issuer'])) { |
37 | | - array_unshift($ordered, $cert); |
38 | | - unset($remainingCerts[$name]); |
39 | | - $found = true; |
40 | | - break; |
41 | | - } |
| 71 | + foreach ($certificates as $cert) { |
| 72 | + if (!isset($cert['subject'])) { |
| 73 | + continue; |
42 | 74 | } |
| 75 | + $subject = $this->normalizeDistinguishedName($cert['subject']); |
| 76 | + if (!in_array($subject, $issuers, true)) { |
| 77 | + return $cert; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $certificates[0] ?? null; |
| 82 | + } |
43 | 83 |
|
44 | | - if (!$found) { |
45 | | - throw new InvalidArgumentException('Certificate chain is incomplete or invalid.'); |
| 84 | + private function findIssuer(array $cert, array $certificates): array { |
| 85 | + foreach ($certificates as $index => $candidate) { |
| 86 | + if ($this->isIssuedBy($cert, $candidate)) { |
| 87 | + unset($certificates[$index]); |
| 88 | + return [$candidate, array_values($certificates)]; |
46 | 89 | } |
47 | 90 | } |
| 91 | + return [null, $certificates]; |
| 92 | + } |
| 93 | + |
| 94 | + private function excludeCertificate(array $certificates, array $toExclude): array { |
| 95 | + return array_values(array_filter($certificates, fn ($cert) => $cert['name'] !== $toExclude['name'])); |
| 96 | + } |
| 97 | + |
| 98 | + private function isRootCertificate(array $cert): bool { |
| 99 | + if (!isset($cert['subject'], $cert['issuer']) || !is_array($cert['subject']) || !is_array($cert['issuer'])) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + return $this->normalizeDistinguishedName($cert['subject']) === $this->normalizeDistinguishedName($cert['issuer']); |
| 103 | + } |
48 | 104 |
|
49 | | - return $ordered; |
| 105 | + private function isIssuedBy(array $child, array $parent): bool { |
| 106 | + if (!isset($child['issuer'], $parent['subject']) || !is_array($child['issuer']) || !is_array($parent['subject'])) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + return $this->normalizeDistinguishedName($child['issuer']) === $this->normalizeDistinguishedName($parent['subject']); |
50 | 110 | } |
51 | 111 |
|
52 | | - private function validateCertificateStructure(array $certificates): void { |
| 112 | + private function normalizeDistinguishedName(array $dn): string { |
| 113 | + ksort($dn); |
| 114 | + return json_encode($dn, JSON_THROW_ON_ERROR); |
| 115 | + } |
| 116 | + |
| 117 | + public function validateCertificateChain(array $certificates): array { |
| 118 | + return [ |
| 119 | + 'valid' => $this->isValidChain($certificates), |
| 120 | + 'hasRoot' => $this->hasRootCertificate($certificates), |
| 121 | + 'isComplete' => $this->isCompleteChain($certificates), |
| 122 | + 'length' => count($certificates), |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
| 126 | + private function isValidChain(array $certificates): bool { |
53 | 127 | if (empty($certificates)) { |
54 | | - throw new InvalidArgumentException('Certificate list cannot be empty'); |
| 128 | + return false; |
55 | 129 | } |
56 | 130 |
|
57 | 131 | foreach ($certificates as $cert) { |
58 | | - if (!isset($cert['subject'], $cert['issuer'], $cert['name'])) { |
59 | | - throw new InvalidArgumentException( |
60 | | - 'Invalid certificate structure. Certificate must have "subject", "issuer", and "name".' |
61 | | - ); |
| 132 | + if (!is_array($cert) || !isset($cert['subject'], $cert['issuer'], $cert['name']) |
| 133 | + || !is_array($cert['subject']) || !is_array($cert['issuer']) |
| 134 | + || empty($cert['subject']['CN']) || empty($cert['issuer']['CN'])) { |
| 135 | + return false; |
62 | 136 | } |
63 | 137 | } |
64 | 138 |
|
65 | | - $names = array_column($certificates, 'name'); |
66 | | - if (count($names) !== count(array_unique($names))) { |
67 | | - throw new InvalidArgumentException('Duplicate certificate names detected'); |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + private function hasRootCertificate(array $certificates): bool { |
| 143 | + foreach ($certificates as $cert) { |
| 144 | + if ($this->isRootCertificate($cert)) { |
| 145 | + return true; |
| 146 | + } |
68 | 147 | } |
| 148 | + return false; |
69 | 149 | } |
70 | 150 |
|
71 | | - private function arrayDiffCanonicalized(array $array1, array $array2): array { |
72 | | - sort($array1); |
73 | | - sort($array2); |
| 151 | + private function isCompleteChain(array $certificates): bool { |
| 152 | + if (!$this->hasRootCertificate($certificates)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + $ordered = $this->orderCertificates($certificates); |
| 157 | + for ($i = 0; $i < count($ordered) - 1; $i++) { |
| 158 | + if (!$this->isIssuedBy($ordered[$i], $ordered[$i + 1])) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
74 | 162 |
|
75 | | - return array_diff($array1, $array2); |
| 163 | + return true; |
76 | 164 | } |
77 | 165 | } |
0 commit comments