1 | <?php namespace Arcanedev\Stripe\Http\Curl; |
||
12 | class SslChecker implements SslCheckerContract |
||
13 | { |
||
14 | /* ------------------------------------------------------------------------------------------------ |
||
15 | | Properties |
||
16 | | ------------------------------------------------------------------------------------------------ |
||
17 | */ |
||
18 | /** @var string */ |
||
19 | protected $url = ''; |
||
20 | |||
21 | /* ------------------------------------------------------------------------------------------------ |
||
22 | | Getters & Setters |
||
23 | | ------------------------------------------------------------------------------------------------ |
||
24 | */ |
||
25 | /** |
||
26 | * Get URL. |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | 6 | public function getUrl() |
|
34 | |||
35 | /** |
||
36 | * Set URL. |
||
37 | * |
||
38 | * @param string $url |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | 2 | public function setUrl($url) |
|
48 | |||
49 | /* ------------------------------------------------------------------------------------------------ |
||
50 | | Main Functions |
||
51 | | ------------------------------------------------------------------------------------------------ |
||
52 | */ |
||
53 | /** |
||
54 | * Preflight the SSL certificate presented by the backend. This isn't 100% |
||
55 | * bulletproof, in that we're not actually validating the transport used to |
||
56 | * communicate with Stripe, merely that the first attempt to does not use a |
||
57 | * revoked certificate. |
||
58 | * |
||
59 | * Unfortunately the interface to OpenSSL doesn't make it easy to check the |
||
60 | * certificate before sending potentially sensitive data on the wire. This |
||
61 | * approach raises the bar for an attacker significantly. |
||
62 | * |
||
63 | * @param string $url |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function checkCert($url) |
||
87 | |||
88 | /* ------------------------------------------------------------------------------------------------ |
||
89 | | Check Functions |
||
90 | | ------------------------------------------------------------------------------------------------ |
||
91 | */ |
||
92 | /** |
||
93 | * Check black list. |
||
94 | * |
||
95 | * @param string $pemCert |
||
96 | * |
||
97 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
98 | */ |
||
99 | 2 | public function checkBlackList($pemCert) |
|
100 | { |
||
101 | 2 | if ($this->isBlackListed($pemCert)) { |
|
102 | 2 | throw new ApiConnectionException( |
|
103 | 'Invalid server certificate. You tried to connect to a server that has a revoked SSL certificate, '. |
||
104 | 2 | 'which means we cannot securely send data to that server. '. |
|
105 | 'Please email [email protected] if you need help connecting to the correct API server.' |
||
106 | 2 | ); |
|
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Checks if a valid PEM encoded certificate is blacklisted. |
||
112 | * |
||
113 | * @param string $cert |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 4 | public function isBlackListed($cert) |
|
132 | |||
133 | |||
134 | /** |
||
135 | * Stream Extension exists - Return true if one of the extensions not found. |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | private function hasStreamExtensions() |
||
144 | |||
145 | /** |
||
146 | * Check if has errors or empty result. |
||
147 | * |
||
148 | * @param mixed $result |
||
149 | * @param int|null $errorNo |
||
150 | * @param string $errorStr |
||
151 | * |
||
152 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
153 | */ |
||
154 | 4 | private function checkResult($result, $errorNo, $errorStr) |
|
165 | |||
166 | /** |
||
167 | * Check if has SSL Errors |
||
168 | * |
||
169 | * @param int $errorNum |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | 308 | public static function hasCertErrors($errorNum) |
|
174 | { |
||
175 | 308 | return in_array($errorNum, [ |
|
176 | 308 | CURLE_SSL_CACERT, |
|
177 | 308 | CURLE_SSL_PEER_CERTIFICATE, |
|
178 | CURLE_SSL_CACERT_BADFILE |
||
179 | 308 | ]); |
|
180 | } |
||
181 | |||
182 | /* ------------------------------------------------------------------------------------------------ |
||
183 | | Other Functions |
||
184 | | ------------------------------------------------------------------------------------------------ |
||
185 | */ |
||
186 | /** |
||
187 | * Prepare SSL URL. |
||
188 | * |
||
189 | * @param string $url |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 2 | private function prepareUrl($url) |
|
199 | |||
200 | /** |
||
201 | * Open a socket connection. |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | private function streamSocketClient() |
||
224 | |||
225 | /** |
||
226 | * Get the certificates file path. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 2 | public static function caBundle() |
|
234 | |||
235 | /** |
||
236 | * Show Stream Extension Warning (stream_socket_enable_crypto is not supported in HHVM). |
||
237 | * |
||
238 | * @return true |
||
239 | */ |
||
240 | 2 | private function showStreamExtensionWarning() |
|
251 | } |
||
252 |