-
Notifications
You must be signed in to change notification settings - Fork 0
/
SslController.php
329 lines (292 loc) · 11.1 KB
/
SslController.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
<?php
/**
* @category Kiatng
* @package Kiatng_Shooter
* @copyright Copyright (c) 2024 Ng Kiat Siong
* @license GNU GPL v3.0
*/
class Kiatng_Shooter_SslController extends Kiatng_Shooter_Controller_Abstract
{
/**
* Test SSL connection
*/
public function indexAction()
{
$host = $this->getRequest()->getParam('url');
if (!$host) {
echo 'Missing url param';
return;
}
$data = $this->_testCurlConnection($host);
$chainStatus = $this->_verifyCertificateChain($data['cert_info']);
$chain = $chainStatus['chain'];
$result = [
'summary' => [
'status' => [
'overall' => $data['info']['ssl_verify_result'] === 0 ? 'Valid' : 'Invalid',
'chain' => $chainStatus['verification']['complete'] ? 'Complete' : 'Incomplete',
'ocsp' => $this->_getOcspStatus($data['verbose'])
],
'connection' => [
'time' => round($data['info']['total_time'] * 1000, 2) . 'ms',
'protocol' => $this->_getProtocolName($data),
'cipher' => $this->_getCipherSuite($data['verbose'])
],
'warnings' => array_filter([
strpos($data['verbose'], 'No OCSP response received') !== false ? 'OCSP revocation check unavailable' : null
])
],
'certificates' => [
'server' => [
'subject' => $chain[0]['subject'],
'issuer' => $chain[0]['issuer'],
'validity' => [
'from' => $chain[0]['valid_from'],
'to' => $chain[0]['valid_to'],
'is_valid' => (
strtotime($chain[0]['valid_from']) <= time() &&
strtotime($chain[0]['valid_to']) >= time()
)
]
],
'intermediate' => [
'subject' => $chain[1]['subject'],
'issuer' => $chain[1]['issuer'],
'validity' => [
'from' => $chain[1]['valid_from'],
'to' => $chain[1]['valid_to'],
'is_valid' => (
strtotime($chain[1]['valid_from']) <= time() &&
strtotime($chain[1]['valid_to']) >= time()
)
]
]
],
'cert_verification' => [
'chain_complete' => $data['chain_verification']['verification']['complete'],
'chain_issues' => $data['chain_verification']['verification']['issues'],
'chain_length' => $data['chain_verification']['count'],
'ssl_verify_result' => $data['info']['ssl_verify_result'],
'ocsp_verification' => [
'status' => $this->_getOcspStatus($data['verbose']),
'stapling_supported' => strpos($data['verbose'], 'OCSP response: no response sent') === false,
'response_details' => $this->_parseOcspDetails($data['verbose'])
],
'root_authority' => $data['chain_verification']['root_cert']['subject'] ?? 'unknown'
],
'cert_chain' => array_map(function($cert) {
return [
'subject' => $cert['subject'],
'issuer' => $cert['issuer'],
'valid_from' => $cert['valid_from'],
'valid_to' => $cert['valid_to'],
'is_valid' => (
strtotime($cert['valid_from']) <= time() &&
strtotime($cert['valid_to']) >= time()
)
];
}, $data['chain_verification']['chain']),
'system_info' => [
'ca_bundle' => $data['ca_info'],
'connection' => array_intersect_key($data['info'], array_flip([
'primary_ip',
'primary_port',
'protocol',
'scheme'
])),
],
'debug' => [
'certificates' => $data['cert_info'],
'verbose_log' => $data['verbose']
]
];
return $this->_echo($result, "SSL Certificate Test: $host");
}
/**
* Helper method to test CURL connection
*
* @param string $url
* @param array $extraOpts
* @param string $testName
* @return array
*/
protected function _testCurlConnection($url, $extraOpts = [])
{
$ch = curl_init();
// Get the system's default CA bundle path
$caInfo = $this->_getSystemCaInfo();
$opts = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CERTINFO => true,
CURLOPT_VERBOSE => true,
// Explicitly set the CA bundle
CURLOPT_CAINFO => $caInfo['path'],
// Enable certificate chain verification
CURLOPT_SSL_VERIFYSTATUS => true
];
// Capture CURL verbose output
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt_array($ch, $opts + $extraOpts);
$response = curl_exec($ch);
$error = curl_error($ch);
$info = curl_getinfo($ch);
$certInfo = curl_getinfo($ch, CURLINFO_CERTINFO);
// Verify certificate chain
$chainVerification = $this->_verifyCertificateChain($certInfo);
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
curl_close($ch);
return [
'status' => $response === false ? 'Failed' : 'Success',
'error' => $error,
'info' => $info,
'cert_info' => $certInfo,
'verbose' => $verboseLog,
'chain_verification' => $chainVerification,
'ca_info' => $caInfo
];
}
/**
* Get system CA certificate information
*
* @return array
*/
protected function _getSystemCaInfo()
{
$possiblePaths = [
'/etc/ssl/certs/ca-certificates.crt', // Debian/Ubuntu
'/etc/pki/tls/certs/ca-bundle.crt', // RHEL/CentOS
'/etc/ssl/ca-bundle.pem', // OpenSUSE
'/usr/local/share/certs/ca-root-nss.crt', // FreeBSD
];
foreach ($possiblePaths as $path) {
if (file_exists($path)) {
return [
'path' => $path,
'exists' => true,
'readable' => is_readable($path),
'size' => filesize($path)
];
}
}
// Fallback to OpenSSL's default path
return [
'path' => openssl_get_cert_locations()['default_cert_file'] ?? null,
'exists' => false,
'error' => 'No standard CA bundle found'
];
}
/**
* Verify the certificate chain
*
* @param array $certInfo
* @return array
*/
protected function _verifyCertificateChain($certInfo)
{
if (empty($certInfo)) {
return [
'chain' => [],
'verification' => ['complete' => false, 'issues' => ['No certificate information available']],
'count' => 0,
'root_cert' => null
];
}
$chain = [];
// Build the certificate chain
foreach ($certInfo as $cert) {
if (empty($cert['Subject']) || empty($cert['Issuer'])) {
continue;
}
$chain[] = [
'subject' => $cert['Subject'],
'issuer' => $cert['Issuer'],
'valid_from' => $cert['Start date'] ?? null,
'valid_to' => $cert['Expire date'] ?? null,
'serial' => $cert['Serial Number'] ?? null
];
}
// Verify chain integrity
$chainStatus = ['complete' => false, 'issues' => []];
for ($i = 0; $i < count($chain) - 1; $i++) {
$current = $chain[$i];
$issuer = $chain[$i + 1];
// Check if the current cert's issuer matches the next cert's subject
if ($current['issuer'] !== $issuer['subject']) {
$chainStatus['issues'][] = sprintf(
"Break in chain: Certificate '%s' is issued by '%s' but next certificate subject is '%s'",
$current['subject'],
$current['issuer'],
$issuer['subject']
);
}
// Check certificate dates
$now = time();
$validFrom = strtotime($current['valid_from']);
$validTo = strtotime($current['valid_to']);
if ($now < $validFrom) {
$chainStatus['issues'][] = "Certificate not yet valid: " . $current['subject'];
}
if ($now > $validTo) {
$chainStatus['issues'][] = "Certificate expired: " . $current['subject'];
}
}
$chainStatus['complete'] = empty($chainStatus['issues']);
return [
'chain' => $chain,
'verification' => $chainStatus,
'count' => count($chain),
'root_cert' => end($chain)
];
}
protected function _getProtocolName($data)
{
// Get protocol from verbose log first as it's more accurate
if (preg_match('/SSL connection using (TLSv[\d\.]+)/', $data['verbose'], $matches)) {
return $matches[1];
}
// Fallback to protocol number mapping
$protocols = [
2 => 'TLSv1.2',
3 => 'TLSv1.3',
];
$protocol = $data['info']['protocol'];
return $protocols[$protocol] ?? "Unknown ($protocol)";
}
protected function _getOcspStatus($verboseLog)
{
if (strpos($verboseLog, 'No OCSP response received') !== false) {
return 'No Response';
}
if (strpos($verboseLog, 'OCSP response received') !== false) {
return 'Success';
}
return 'Unknown';
}
protected function _parseOcspDetails($verboseLog)
{
$details = [];
// Check if OCSP stapling is attempted
if (strpos($verboseLog, 'OCSP stapling:') !== false) {
$details['stapling_attempted'] = true;
}
// Extract response time if available
if (preg_match('/OCSP response received after (\d+)ms/', $verboseLog, $matches)) {
$details['response_time_ms'] = (int)$matches[1];
}
return $details;
}
protected function _getCipherSuite($verboseLog)
{
if (preg_match('/\* SSL connection using (?:TLSv[\d\.]+) \/ ([^\n]+)/', $verboseLog, $matches)) {
return trim($matches[1]);
}
return 'Unknown';
}
}