-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
429 lines (397 loc) · 12.3 KB
/
index.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
require_once("functions.php");
$default = array();
$default['domain'] = 'tlsa.puggan.se';
$default['port'] = 443;
$default['protocol'] = 'auto';
$default['update_cert'] = FALSE;
$default['update_dns'] = FALSE;
$_POST += $default;
$status_class = '';
$chain = array();
ob_start();
{
if($_POST['update_cert'])
{
$update_cert = ssl_fetch_cert($_POST['domain'], $_POST['port'], $_POST['protocol']);
}
$cert_meta = ssl_load_cert($_POST['domain'], $_POST['port']);
if(!$cert_meta AND !$_POST['update_cert'])
{
$update_cert = ssl_fetch_cert($_POST['domain'], $_POST['port'], $_POST['protocol']);
$cert_meta = ssl_load_cert($_POST['domain'], $_POST['port']);
}
$tlsa = dns_fetch_tls($_POST['domain'], $_POST['port']);
if($cert_meta AND $cert_meta['cert'])
{
$cert_meta['sha512'] = openssl_x509_fingerprint($cert_meta['cert'], 'sha512');
$cert_meta['sha1'] = openssl_x509_fingerprint($cert_meta['cert'], 'sha1');
$cert_meta['md5'] = openssl_x509_fingerprint($cert_meta['cert'], 'md5');
$cert_meta['parsed'] = openssl_x509_parse($cert_meta['cert']);
$cert_meta['hex_only_content'] = str_replace("\n", "", substr($cert_meta['content'], strlen('-----BEGIN CERTIFICATE-----'), -strlen('-----END CERTIFICATE-----')));
$cert_meta['pub_key'] = openssl_pkey_get_details(openssl_pkey_get_public($cert_meta['cert']))['key'];
$cert_meta['pub_key_hex_only_content'] = str_replace("\n", "", substr($cert_meta['pub_key'], strlen('-----BEGIN PUBLIC KEY-----'), -strlen('-----END PUBLIC KEY-----')));
$pub_key_raw_content = base64_decode($cert_meta['pub_key_hex_only_content']);
$cert_meta['pub_key_sha256'] = hash('sha256', $pub_key_raw_content);
$cert_meta['pub_key_sha512'] = hash('sha512', $pub_key_raw_content);
$current = $cert_meta;
$chain[] = $current;
while($current['parent'])
{
$current = ssl_load_cert_sha256($current['parent']);
if($current)
{
$current['sha512'] = openssl_x509_fingerprint($current['cert'], 'sha512');
$current['hex_only_content'] = str_replace("\n", "", substr($current['content'], strlen('-----BEGIN CERTIFICATE-----'), -strlen('-----END CERTIFICATE-----')));
$current['pub_key'] = openssl_pkey_get_details(openssl_pkey_get_public($current['cert']))['key'];
$current['pub_key_hex_only_content'] = str_replace("\n", "", substr($current['pub_key'], strlen('-----BEGIN PUBLIC KEY-----'), -strlen('-----END PUBLIC KEY-----')));
$pub_key_raw_content = base64_decode($current['pub_key_hex_only_content']);
$current['pub_key_sha256'] = hash('sha256', $pub_key_raw_content);
$current['pub_key_sha512'] = hash('sha512', $pub_key_raw_content);
$chain[] = $current;
}
}
}
}
// echo "<pre>" . print_r(, 1) . "</pre>";
$errors = ob_get_clean();
if($tlsa)
{
$usages_list = array('CA constraint', 'Service certificate constraint', 'Trust anchor assertion', 'Domain-issued certificate');
$selector_list = array('Full certificate', 'SubjectPublicKeyInfo');
$matchingtype_list = array('No hash used', 'SHA-256', 'SHA-512');
$tlsa['usage_text'] = isset($usages_list[$tlsa['usage']]) ? $usages_list[$tlsa['usage']] : '?';
$tlsa['selector_text'] = isset($selector_list[$tlsa['selector']]) ? $selector_list[$tlsa['selector']] : '?';
$tlsa['matchingtype_text'] = isset($matchingtype_list[$tlsa['matchingtype']]) ? $matchingtype_list[$tlsa['matchingtype']] : '?';
$key_names = array();
$key_names[0][1]='hex_only_content';
$key_names[0][1]='sha256';
$key_names[0][2]='sha512';
$key_names[1][1]='pub_key_hex_only_content';
$key_names[1][1]='pub_key_sha256';
$key_names[1][2]='pub_key_sha512';
$key_titles = array();
$key_titles[0][1]='certificate content';
$key_titles[0][1]='certificate sha256-hash';
$key_titles[0][2]='certificate sha512-hash';
$key_titles[1][1]='pubkey content';
$key_titles[1][1]='pubkey sha256-hash';
$key_titles[1][2]='pubkey sha512-hash';
if(isset($key_names[$tlsa['selector']][$tlsa['matchingtype']]))
{
$key_name = $key_names[$tlsa['selector']][$tlsa['matchingtype']];
$key_title = $key_titles[$tlsa['selector']][$tlsa['matchingtype']];
$tlsa_hash = $tlsa['matchingtype'] ? strtolower($tlsa['hash']) : $tlsa['hash'];
}
else
{
$key_name = NULL;
}
if(!$cert_meta)
{
$tlsa['status'] = 'Unknown (certificate not loaded)';
}
else if($tlsa['usage'] == 3)
{
if($tlsa_hash == $cert_meta[$key_name])
{
$tlsa['status'] = "OK, {$key_title} matches";
$status_class = 'status_ok';
}
else
{
$tlsa['status'] = "BAD, {$key_title} differ";
$status_class = 'status_bad';
}
}
else if($tlsa['usage'] > 3 OR $tlsa['usage'] < 0)
{
$tlsa['status'] = 'Unknown (usage type not supported yet)';
}
else if($key_name)
{
$tlsa['status'] = "Unknown (usage type not supported yet), didn't find certificate in chain";
$status_class = 'status_bad';
foreach($chain as $current)
{
if($tlsa_hash == $current[$key_name])
{
$tlsa['status'] = "Unknown (usage type not supported yet), found certificate in chain";
$status_class = 'status_ok';
break;
}
}
}
else if($tlsa['selector'] > 1 OR $tlsa['selector'] < 0)
{
$tlsa['status'] = 'Unknown (selector type not supported yet)';
}
else
{
$tlsa['status'] = 'Unknown (matchingtype type not supported yet)';
}
}
else
{
$tlsa = array('usage' => NULL, 'selector' => NULL, 'matchingtype' => NULL, 'hash' => NULL, 'usage_text' => NULL, 'selector_text' => NULL, 'matchingtype_text' => NULL);
$tlsa['status'] = 'No record';
}
$domain_html = htmlentities($_POST['domain']);
$port_html = (int) $_POST['port'];
$protocol_options = html_options(array('auto' => 'guess from port', '' => 'pure ssl', 'smtp' => 'smtp'), $_POST['protocol']);
$subject_html = htmlentities($cert_meta['parsed']['subject']['CN']);
$issuer_html = htmlentities($cert_meta['parsed']['issuer']['CN']);
$hash_html = chunk_split($tlsa['hash'], 64);
$sha256_html = chunk_split($cert_meta['sha256'], 64);
$sha512_html = chunk_split($cert_meta['sha512'], 64);
$expire_date = gmdate("Y-m-d H:i:s", $cert_meta['parsed']['validTo_time_t']);
$created_date = gmdate("Y-m-d H:i:s", $cert_meta['parsed']['validFrom_time_t']);
$pule_date = gmdate("Y-m-d H:i:s", strtotime($cert_meta['pulse']));
$export_html = htmlentities($cert_meta['content']);
$pub_key_html = htmlentities($cert_meta['pub_key']);
echo <<<HTML_BLOCK
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>TLSA info of {$domain_html}:{$port_html}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type='text/css'>
BODY
{
font-size: 16px;
}
TABLE
{
border: solid gray 1px;
}
TH, TD
{
border: solid gray 1px;
padding: 2px 5px;
text-align: left;
vertical-align: top;
}
TR
{
background-color: #EEE;
}
TR:nth-child(odd)
{
background-color: #DDD;
}
PRE.cert_export
{
background-color: #EEE;
width: -moz-fit-content;
padding: 5px;
}
TD.status_ok
{
color: green;
font-weight: bold;
}
TD.status_bad
{
color: red;
font-weight: bold;
}
LABEL > SPAN
{
display: inline-block;
width: 70px;
}
INPUT[name="domain"]
{
width: 300px;
}
INPUT[name="port"]
{
width: 30px;
}
.hash
{
font-family: monospace;
width: 600px;
}
</style>
</head>
<body>
<form method='post' action='/'>
<h1>TLSA info of {$domain_html}:{$port_html}</h1>
<p>This page show information about SSL connections, and their TLSA records.</p>
<fieldset>
<label>
<span>Domain:</span>
<input name='domain' value='{$domain_html}' />
</label><br />
<label>
<span>Port:</span>
<input name='port' value='{$port_html}' />
</label><br />
<label>
<label>
<span>Protocol:</span>
<select name='protocol'>
{$protocol_options}
</select>
</label><br />
<label>
<span></span>
<input type='submit' value='Fetch' />
</label><br />
</fieldset>
{$errors}
<h2>TLSA DNS record</h2>
<p>The current TLSA DNS-record (you may also want to check the <a href='http://dnsviz.net/d/{$domain_html}/dnssec/'>DNSSEC status</a>)</p>
<table>
<tbody>
<tr>
<th>TLSA DNS target</th>
<td>_{$port_html}._tcp.{$domain_html}</td>
</tr>
<tr>
<th>Cert. Usage</th>
<td>{$tlsa['usage']}: {$tlsa['usage_text']}</td>
</tr>
<tr>
<th>Selector</th>
<td>{$tlsa['selector']}: {$tlsa['selector_text']}</td>
</tr>
<tr>
<th>Matching Type</th>
<td>{$tlsa['matchingtype']}: {$tlsa['matchingtype_text']}</td>
</tr>
<tr>
<th>Hash</th>
<td class='hash'>{$hash_html}</td>
</tr>
<tr>
<th>Status</th>
<td class='{$status_class}'>{$tlsa['status']}</td>
</tr>
</tbody>
</table>
<!-- p><input type='submit' name='update_dns' value='Update DNS cache' /></p -->
<h2>TLSA sugestions</h2>
<p>Valid TLSA DNS records sugestions</p>
<table>
<thead>
<tr>
<th>Target</th>
<th>Usage</th>
<th>Selector</th>
<th>M-type</th>
<th>Hash</th>
</tr>
</thead>
<tbody>
HTML_BLOCK;
foreach($chain as $current)
{
$cert_sha256_html = chunk_split($current['sha256'], 64);
$cert_sha512_html = chunk_split($current['sha512'], 64);
$pub_key_sha256_html = chunk_split($current['pub_key_sha256'], 64);
$pub_key_sha512_html = chunk_split($current['pub_key_sha512'], 64);
$hex_only_content_length = strlen($current['hex_only_content']);
$hex_only_content_rows = ceil($hex_only_content_length / 64);
$hex_only_content_html_part = substr($current['hex_only_content'], 0, 64) . "<br/>...(" . ($hex_only_content_rows - 2) . " more rows)...<br/>" . substr($current['hex_only_content'], $hex_only_content_rows*64 - 64);
$pub_key_content_html = chunk_split($current['pub_key_hex_only_content'], 64);
$usage = '2?';
if(!$current['parent']) $usage = '0?';
$usage = '?';
if($current['sha256'] == $cert_meta['sha256']) $usage = 3;
echo <<<HTML_BLOCK
<tr>
<td>_{$port_html}._tcp.{$domain_html}</td>
<td>{$usage}</td>
<td>0</td>
<td>1</td>
<td class='hash'>{$cert_sha256_html}</td>
</tr>
<tr>
<td>_{$port_html}._tcp.{$domain_html}</td>
<td>{$usage}</td>
<td>1</td>
<td>1</td>
<td class='hash'>{$pub_key_sha256_html}</td>
</tr>
<tr>
<td>_{$port_html}._tcp.{$domain_html}</td>
<td>{$usage}</td>
<td>0</td>
<td>2</td>
<td class='hash'>{$cert_sha512_html}</td>
</tr>
<tr>
<td>_{$port_html}._tcp.{$domain_html}</td>
<td>{$usage}</td>
<td>1</td>
<td>2</td>
<td class='hash'>{$pub_key_sha512_html}</td>
</tr>
<tr>
<td>_{$port_html}._tcp.{$domain_html}</td>
<td>{$usage}</td>
<td>1</td>
<td>0</td>
<td class='hash'>{$pub_key_content_html}</td>
</tr>
<tr>
<td>_{$port_html}._tcp.{$domain_html}</td>
<td>{$usage}</td>
<td>0</td>
<td>0</td>
<td class='hash'>{$hex_only_content_html_part}</td>
</tr>
HTML_BLOCK;
}
echo <<<HTML_BLOCK
</tbody>
</table>
<h2>SSL Cert</h2>
<p>The current certificate at the given connetion</p>
<table>
<tbody>
<tr>
<th>Domain/Subject</th>
<td>{$subject_html}</th>
</tr>
<tr>
<th>Fingerprint SHA256</th>
<td class='hash'>{$sha256_html}</td>
</tr>
<tr>
<th>Fingerprint SHA512</th>
<td class='hash'>{$sha512_html}</td>
</tr>
<tr>
<th>Fingerprint SHA1</th>
<td class='hash'>{$cert_meta['sha1']}</td>
</tr>
<tr>
<th>Fingerprint MD5</th>
<td class='hash'>{$cert_meta['md5']}</td>
</tr>
<tr>
<th>Issuer</th>
<td>{$issuer_html}</td>
</tr>
<tr>
<th>Expires</th>
<td>{$expire_date} Z</td>
</tr>
<tr>
<th>Created</th>
<td>{$created_date} Z</td>
</tr>
</tbody>
</table>
<p>certificate fetched at {$pule_date} Z <input type='submit' name='update_cert' value='Update SSL cache' /></p>
<h3>Certificate at {$domain_html}:{$port_html}</h3>
<pre class='cert_export'>{$export_html}</pre>
<h3>Public key at {$domain_html}:{$port_html}</h3>
<pre class='cert_export'>{$pub_key_html}</pre>
</form>
</body>
</html>
HTML_BLOCK;