-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathOrderCertificatesTrait.php
More file actions
165 lines (137 loc) · 4.74 KB
/
OrderCertificatesTrait.php
File metadata and controls
165 lines (137 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Handler\CertificateEngine;
use InvalidArgumentException;
trait OrderCertificatesTrait {
public function orderCertificates(array $certificates): array {
if (empty($certificates)) {
throw new InvalidArgumentException('Certificate list cannot be empty');
}
$this->ensureValidStructure($certificates);
return match (true) {
count($certificates) === 1 => $certificates,
default => $this->buildChain($certificates)
};
}
private function buildChain(array $certificates): array {
$leaf = $this->findLeafCertificate($certificates);
if (!$leaf) {
return $certificates;
}
$ordered = [$leaf];
$remaining = $this->excludeCertificate($certificates, $leaf);
while ($remaining && !$this->isRootCertificate(end($ordered))) {
[$next, $remaining] = $this->findIssuer(end($ordered), $remaining);
if (!$next) {
break;
}
$ordered[] = $next;
}
return [...$ordered, ...$remaining];
}
private function ensureValidStructure(array $certificates): void {
foreach ($certificates as $cert) {
if (!is_array($cert) || !isset($cert['subject'], $cert['issuer'], $cert['name'])) {
throw new InvalidArgumentException('Invalid certificate structure. Certificate must have "subject", "issuer", and "name".');
}
if (!is_array($cert['subject']) || !is_array($cert['issuer'])) {
throw new InvalidArgumentException('Invalid certificate structure. Certificate must have "subject", "issuer", and "name".');
}
}
$names = array_column($certificates, 'name');
if (count($names) !== count(array_unique($names))) {
throw new InvalidArgumentException('Duplicate certificate names detected');
}
}
private function findLeafCertificate(array $certificates): ?array {
$issuers = [];
foreach ($certificates as $cert) {
if (isset($cert['issuer'])) {
$issuers[] = $this->normalizeDistinguishedName($cert['issuer']);
}
}
foreach ($certificates as $cert) {
if (!isset($cert['subject'])) {
continue;
}
$subject = $this->normalizeDistinguishedName($cert['subject']);
if (!in_array($subject, $issuers, true)) {
return $cert;
}
}
return $certificates[0] ?? null;
}
private function findIssuer(array $cert, array $certificates): array {
foreach ($certificates as $index => $candidate) {
if ($this->isIssuedBy($cert, $candidate)) {
unset($certificates[$index]);
return [$candidate, array_values($certificates)];
}
}
return [null, $certificates];
}
private function excludeCertificate(array $certificates, array $toExclude): array {
return array_values(array_filter($certificates, fn ($cert) => $cert['name'] !== $toExclude['name']));
}
private function isRootCertificate(array $cert): bool {
if (!isset($cert['subject'], $cert['issuer']) || !is_array($cert['subject']) || !is_array($cert['issuer'])) {
return false;
}
return $this->normalizeDistinguishedName($cert['subject']) === $this->normalizeDistinguishedName($cert['issuer']);
}
private function isIssuedBy(array $child, array $parent): bool {
if (!isset($child['issuer'], $parent['subject']) || !is_array($child['issuer']) || !is_array($parent['subject'])) {
return false;
}
return $this->normalizeDistinguishedName($child['issuer']) === $this->normalizeDistinguishedName($parent['subject']);
}
private function normalizeDistinguishedName(array $dn): string {
ksort($dn);
return json_encode($dn, JSON_THROW_ON_ERROR);
}
public function validateCertificateChain(array $certificates): array {
return [
'valid' => $this->isValidChain($certificates),
'hasRoot' => $this->hasRootCertificate($certificates),
'isComplete' => $this->isCompleteChain($certificates),
'length' => count($certificates),
];
}
private function isValidChain(array $certificates): bool {
if (empty($certificates)) {
return false;
}
foreach ($certificates as $cert) {
if (!is_array($cert) || !isset($cert['subject'], $cert['issuer'], $cert['name'])
|| !is_array($cert['subject']) || !is_array($cert['issuer'])
|| empty($cert['subject']['CN']) || empty($cert['issuer']['CN'])) {
return false;
}
}
return true;
}
private function hasRootCertificate(array $certificates): bool {
foreach ($certificates as $cert) {
if ($this->isRootCertificate($cert)) {
return true;
}
}
return false;
}
private function isCompleteChain(array $certificates): bool {
if (!$this->hasRootCertificate($certificates)) {
return false;
}
$ordered = $this->orderCertificates($certificates);
for ($i = 0; $i < count($ordered) - 1; $i++) {
if (!$this->isIssuedBy($ordered[$i], $ordered[$i + 1])) {
return false;
}
}
return true;
}
}