1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LiquidWeb\SslCertificate; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use phpseclib\File\X509; |
7
|
|
|
use LiquidWeb\SslCertificate\Exceptions\Handler; |
8
|
|
|
use LiquidWeb\SslCertificate\Exceptions\InvalidUrl; |
9
|
|
|
|
10
|
|
|
class Downloader |
11
|
|
|
{ |
12
|
6 |
|
public static function downloadCertificateFromUrl(string $url, int $timeout = 30): array |
13
|
|
|
{ |
14
|
|
|
// Trusted variable to keep track of SSL trust |
15
|
6 |
|
$trusted = true; |
16
|
6 |
|
$sslConfig = StreamConfig::configSecure(); |
17
|
6 |
|
$parsedUrl = new Url($url); |
18
|
1 |
|
$client = null; |
19
|
|
|
|
20
|
|
|
try { |
21
|
1 |
|
$client = stream_socket_client( |
22
|
1 |
|
"ssl://{$parsedUrl->getTestURL()}", |
23
|
1 |
|
$errorNumber, |
24
|
1 |
|
$errorDescription, |
25
|
1 |
|
$timeout, |
26
|
1 |
|
STREAM_CLIENT_CONNECT, |
27
|
1 |
|
$sslConfig->getContext() |
28
|
|
|
); |
29
|
|
|
unset($sslConfig); |
30
|
1 |
|
} catch (Throwable $thrown) { |
31
|
|
|
// Try agian in insecure mode |
32
|
1 |
|
$sslConfig = StreamConfig::configInsecure(); |
33
|
1 |
|
$trusted = false; |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
// As the URL failed verification we set to false |
37
|
1 |
|
$client = stream_socket_client( |
38
|
1 |
|
"ssl://{$parsedUrl->getTestURL()}", |
39
|
1 |
|
$errorNumber, |
40
|
1 |
|
$errorDescription, |
41
|
1 |
|
$timeout, |
42
|
1 |
|
STREAM_CLIENT_CONNECT, |
43
|
1 |
|
$sslConfig->getContext() |
44
|
|
|
); |
45
|
|
|
unset($sslConfig); |
46
|
1 |
|
} catch (Throwable $thrown) { |
47
|
1 |
|
(new Handler($thrown))->downloadHandler($parsedUrl); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return self::prepareCertificateResponse($client, $trusted, $parsedUrl); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private static function prepareCertificateResponse($resultClient, bool $trusted, Url $parsedUrl): array |
55
|
|
|
{ |
56
|
|
|
$response = stream_context_get_options($resultClient); |
57
|
|
|
$connectionInfo = stream_get_meta_data($resultClient)['crypto']; |
58
|
|
|
unset($resultClient); |
59
|
|
|
$mainCert = openssl_x509_parse($response['ssl']['peer_certificate'], true); |
60
|
|
|
|
61
|
|
|
$full_chain = []; |
62
|
|
|
if (count($response['ssl']['peer_certificate_chain']) > 1) { |
63
|
|
|
foreach ($response['ssl']['peer_certificate_chain'] as $cert) { |
64
|
|
|
$parsedCert = openssl_x509_parse($cert, true); |
65
|
|
|
$isChain = ! ($parsedCert['hash'] === $mainCert['hash']); |
66
|
|
|
if ($isChain === true) { |
67
|
|
|
array_push($full_chain, $parsedCert); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return [ |
73
|
|
|
'inputDomain' => $parsedUrl->getInputUrl(), |
74
|
|
|
'tested' => $parsedUrl->getTestURL(), |
75
|
|
|
'trusted' => $trusted, |
76
|
|
|
'dns-resolves-to' => $parsedUrl->getIp(), |
77
|
|
|
'cert' => $mainCert, |
78
|
|
|
'full_chain' => $full_chain, |
79
|
|
|
'connection' => $connectionInfo, |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function downloadRevocationListFromUrl(string $url): array |
84
|
|
|
{ |
85
|
|
|
$parsedUrl = new Url($url); |
86
|
|
|
$csrConfig = StreamConfig::configCrl(); |
87
|
|
|
$file = file_get_contents($parsedUrl->getValidatedURL(), false, $csrConfig->getContext()); |
88
|
|
|
unset($csrConfig, $parsedUrl); |
89
|
|
|
$x509 = new X509(); |
90
|
|
|
$crl = $x509->loadCRL($file); |
91
|
|
|
unset($x509, $file); |
92
|
|
|
|
93
|
|
|
return $crl; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|