|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jalle19\CertificateParser\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Jalle19\CertificateParser\Provider\Exception\ConnectionTimeoutException; |
|
6
|
|
|
use Jalle19\CertificateParser\Provider\Exception\DomainMismatchException; |
|
7
|
|
|
use Jalle19\CertificateParser\Provider\Exception\NameResolutionException; |
|
8
|
|
|
use Jalle19\CertificateParser\Provider\Exception\CertificateNotFoundException; |
|
9
|
|
|
use Jalle19\CertificateParser\Provider\Exception\ProviderException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class StreamSocketProvider |
|
13
|
|
|
* @package Jalle19\CertificateParser\Provider |
|
14
|
|
|
*/ |
|
15
|
|
|
class StreamSocketProvider implements ProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
const DEFAULT_TIMEOUT_SECONDS = 15; |
|
19
|
|
|
const DEFAULT_PORT = 443; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $hostname; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $port; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
private $timeout; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var StreamContext |
|
38
|
|
|
*/ |
|
39
|
|
|
private $streamContext; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array maps partial error messages to exception types |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $errorExceptionMap = [ |
|
45
|
|
|
// Check for name resolution errors |
|
46
|
|
|
'getaddrinfo failed' => NameResolutionException::class, |
|
47
|
|
|
// Check for unknown SSL protocol (usually means no SSL is configured on the endpoint) |
|
48
|
|
|
'GET_SERVER_HELLO:unknown protocol' => CertificateNotFoundException::class, |
|
49
|
|
|
// Check for domain mismatches |
|
50
|
|
|
'did not match expected' => DomainMismatchException::class, |
|
51
|
|
|
// Check for connection timeouts |
|
52
|
|
|
'timed out' => ConnectionTimeoutException::class, |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* StreamSocketProvider constructor. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $hostname |
|
60
|
|
|
* @param int $port (optional) |
|
61
|
|
|
* @param int $timeout (optional) |
|
62
|
|
|
* @param StreamContext|null $streamContext (optional) |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct( |
|
65
|
|
|
$hostname, |
|
66
|
|
|
$port = self::DEFAULT_PORT, |
|
67
|
|
|
$timeout = self::DEFAULT_TIMEOUT_SECONDS, |
|
68
|
|
|
$streamContext = null |
|
69
|
|
|
) { |
|
70
|
|
|
$this->hostname = $hostname; |
|
71
|
|
|
$this->port = $port; |
|
72
|
|
|
$this->timeout = $timeout; |
|
73
|
|
|
$this->streamContext = $streamContext ?: new StreamContext(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param StreamContext $streamContext |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setStreamContext($streamContext) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->streamContext = $streamContext; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getRawCertificate() |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
|
|
$client = stream_socket_client( |
|
93
|
|
|
$this->getRequestUrl(), |
|
94
|
|
|
$errorNumber, |
|
95
|
|
|
$errorDescription, |
|
96
|
|
|
$this->timeout, |
|
97
|
|
|
STREAM_CLIENT_CONNECT, |
|
98
|
|
|
$this->streamContext->getResource() |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$response = stream_context_get_params($client); |
|
102
|
|
|
|
|
103
|
|
|
return $response['options']['ssl']['peer_certificate']; |
|
104
|
|
|
} catch (\Throwable $e) { |
|
105
|
|
|
$errorMessage = $e->getMessage(); |
|
106
|
|
|
|
|
107
|
|
|
// Throw mapped exceptions |
|
108
|
|
|
foreach (self::$errorExceptionMap as $needle => $exceptionClass) { |
|
109
|
|
|
if (strpos($errorMessage, $needle) !== false) { |
|
110
|
|
|
throw new $exceptionClass($errorMessage); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new ProviderException($errorMessage); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private function getRequestUrl() |
|
123
|
|
|
{ |
|
124
|
|
|
return 'ssl://' . $this->hostname . ':' . $this->port; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|