This repository has been archived by the owner on Feb 9, 2020. It is now read-only.
/
library.php
321 lines (261 loc) · 8.25 KB
/
library.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\File;
// most of this code is based on Perl code from
// https://github.com/psypete/public-bin
function detectCertFormat($filePath)
{
$f = file_get_contents($filePath);
$fmt = "x509";
if (preg_match("/BEGIN PKCS(7|12)/", $f, $matches)) {
$inform = "pem";
if ($matches[1] == "7") {
$fmt = "pkcs7";
} elseif ($matches[1] == "12") {
$fmt = "pkcs12";
}
} else if (preg_match("/BEGIN CERTIFICATE/", $f, $matches)) {
$inform = "pem";
} else if (preg_match("/\.p7c$/", $filePath)) {
$fmt = "pkcs7";
$inform = "der";
} else {
$inform = "der";
}
return [$inform, $fmt];
}
function readCertificate($filePath, $inform, $format)
{
if ($format == "x509") {
$cmd = sprintf(
"openssl %s -noout -text -inform %s -in %s",
$format,
$inform,
escapeshellarg($filePath)
);
} else {
$cmd = sprintf(
"openssl %s -text -print_certs -inform %s -in %s",
$format,
$inform,
escapeshellarg($filePath)
);
}
return execute($cmd);
}
function parseFormattedCert($parsedCert)
{
$lines = $parsedCert;
for ($i = 0; $i < sizeof($parsedCert); $i++) {
$line = $lines[$i];
$matches = [];
if (preg_match("/^\s+Issuer: (.+)$/", $line, $matches)) {
// .p7c might contain multiple cert which we don't handle at the
// moment so stop processing after the first one
if (isset($cert["issuer"])) {
break;
}
$cert["issuer"] = trim($matches[1]);
}
if (preg_match("/^\s+Subject: (.+)/", $line, $matches)) {
$cert["subject"] = trim($matches[1]);
}
if (preg_match("/CA Issuers - URI:(.+)/", $line, $matches)) {
$cert["issuers"][] = trim($matches[1]);
}
if (preg_match("/X509v3 Subject Key Identifier:/", $line, $matches)) {
$cert["subj_key_ident"] = trim($lines[++$i]);
}
if (preg_match("/X509v3 Authority Key Identifier:/", $line, $matches)) {
$cert["auth_key_ident"] = str_replace(
"keyid:",
"",
trim($lines[++$i])
);
}
if (preg_match("/^\s+Validity\s*$/", $line)) {
for ($j = 0; $j < 3; $j++) {
$sline = $lines[$j + $i];
if (preg_match("/^\s+Not (Before|After)\s*: (.+)$/", $sline, $smatches)) {
list ($when, $time) = [$smatches[1], $smatches[2]];
if ( $when == "Before") {
$cert['created'] = strtotime(trim($time));
} else if ($when == "After") {
$cert['expires'] = strtotime(trim($time));
}
}
}
}
}
return $cert;
}
function isExpired($cert, $expiry = null)
{
if (is_null($expiry)) {
$expiry = time();
}
if (isset($cert["expires"])) {
// @todo: better validation of expires
return $cert["expires"] < $expiry;
} else {
throw new Exception("Certificate without expiration time!");
}
}
// where $cert1 is the "higher" certificate in the chain
function areCertsLinked($cert1, $cert2)
{
$pair1 = ["subj_key_ident", "auth_key_ident"];
$pair2 = ["subject", "issuer"];
foreach ([$pair1, $pair2] as $field) {
list($f1, $f2) = $field;
if (isset($cert1[$f1]) && strlen($cert1[$f1]) > 0
&& isset($cert2[$f2]) && strlen($cert2[$f2]) > 0
) {
if ($cert1[$f1] == $cert2[$f2]) {
return true;
}
}
}
return false;
}
function findMatchingRoot($cert)
{
foreach (glob("/etc/ssl/certs/*") as $root) {
list ($inform, $format) = detectCertFormat($root);
$rootCert = parseFormattedCert(readCertificate($root, $inform, $format));
if (areCertsLinked($rootCert, $cert)) {
if (!areCertsLinked($rootCert, $rootCert)) {
throw new Exception("Invalid root certificate $root!");
}
return $root;
}
}
throw new Exception("Unable to find matching root certificate");
}
function downloadIssuer($uri)
{
$ext = pathinfo($uri, PATHINFO_EXTENSION);
$hash = sha1($uri);
$path = __DIR__ . "/cache/ca-issuer-$hash.$ext";
if (!file_exists($path)) {
$cmd = sprintf(
"wget %s -O %s",
escapeshellarg($uri),
escapeshellarg($path)
);
execute($cmd);
}
return $path;
}
function buildChain($cert, $certPath, $includeRoot = false)
{
if (isExpired($cert)) {
throw new Exception("Certificate has expired");
}
if (areCertsLinked($cert, $cert)) {
throw new Exception("Self-signed or CA cert");
}
$uris = $cert["issuers"];
if (!$uris) {
throw new Exception("Certificate doesn't specify issuers");
}
$c = $cert;
$chain = [];
while (sizeof($uris) > 0) {
$old = $c;
$uri = array_shift($uris);
$path = downloadIssuer($uri);
list ($inform, $format) = detectCertFormat($path);
$c = parseFormattedCert(readCertificate($path, $inform, $format));
if (isExpired($c)) {
throw new Exception("Expired intermediate in the chain");
}
if (areCertsLinked($c, $c)) {
break;
}
if (!areCertsLinked($c, $old)) {
$msg = "Intermediate doesn't match previous certificate in the chain";
throw new Exception($msg);
}
$chain[] = $path;
if (isset($c["issuers"])) {
foreach ($c["issuers"] as $i) {
$uris[] = $i;
// we don't currently have a good way of handling multiple
// issuers
break;
}
}
}
// we are at the end of the chain, see if there's matching root CA
$cacheDir = __DIR__ . '/cache';
$adapter = new File($cacheDir);
$adapter->setOption('ttl', 600);
$cache = new Cache($adapter);
if (!$cache->get(md5($path) . "-root")) {
$root = findMatchingRoot($c);
$chain[] = $root;
$cache->set(md5($path) . "-root", $root);
} else {
$chain[] = $cache->get(md5($path) . "-root");
}
// build certificate bundle
foreach ($chain as $i => $path) {
list ($inform, $format) = detectCertFormat($path);
$cmd = sprintf(
"openssl x509 -inform %s -outform pem -in %s -out %s",
escapeshellarg($inform),
escapeshellarg($path),
escapeshellarg(__DIR__ . "/tmp/" . sha1($cert["subject"]) . "-$i.pem")
);
exec($cmd);
}
unlink(__DIR__ . "/tmp/bundle.crt");
foreach ($chain as $i => $path) {
file_put_contents(
__DIR__ . "/tmp/bundle.crt",
file_get_contents(__DIR__ . "/tmp/" . sha1($cert["subject"]) . "-$i.pem"),
FILE_APPEND
);
}
// verify the chain is valid
$cmd = sprintf(
"openssl verify -verbose -purpose sslserver -CAfile %s/tmp/bundle.crt %s",
__DIR__,
escapeshellarg($certPath)
);
try {
execute($cmd);
} catch (Exception $e) {
$err = implode("\n", $e->output);
throw new Exception("Can't verify the bundle: {$err}");
}
// extract the original cert (it might contain some, or all, parts of the
// chain already)
$cmd = sprintf(
"openssl x509 -inform pem -outform pem -in %s",
$certPath
);
$out = implode("\n", execute($cmd));
$out .= "\n";
if (!$includeRoot) {
array_pop($chain);
}
foreach ($chain as $i => $path) {
$out .= file_get_contents(__DIR__ . "/tmp/" . sha1($cert["subject"]) . "-$i.pem");
unlink(__DIR__ . "/tmp/" . sha1($cert["subject"]) . "-$i.pem");
}
return $out;
}
function execute($cmd)
{
$cmd = $cmd . " 2>&1";
exec($cmd, $output, $code);
if ($code != 0) {
$e = new \Exception("Command '$cmd' failed exit code $code");
$e->output = $output;
$e->cmd = $cmd;
throw $e;
}
return $output;
}