| Conditions | 4 |
| Paths | 4 |
| Total Lines | 34 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | public static function downloadCertificateFromUrl(string $url, int $timeout = 30): array |
||
| 11 | { |
||
| 12 | $hostName = (new Url($url))->getHostName(); |
||
| 13 | |||
| 14 | $streamContext = stream_context_create([ |
||
| 15 | 'ssl' => [ |
||
| 16 | 'capture_peer_cert' => true, |
||
| 17 | ], |
||
| 18 | ]); |
||
| 19 | |||
| 20 | try { |
||
| 21 | $client = stream_socket_client( |
||
| 22 | "ssl://{$hostName}:443", |
||
| 23 | $errorNumber, |
||
| 24 | $errorDescription, |
||
| 25 | $timeout, |
||
| 26 | STREAM_CLIENT_CONNECT, |
||
| 27 | $streamContext); |
||
| 28 | } catch (Throwable $thrown) { |
||
| 29 | if (str_contains($thrown->getMessage(), 'getaddrinfo failed')) { |
||
| 30 | throw CouldNotDownloadCertificate::hostDoesNotExist($hostName); |
||
| 31 | } |
||
| 32 | |||
| 33 | if (str_contains($thrown->getMessage(), 'error:14090086')) { |
||
| 34 | throw CouldNotDownloadCertificate::noCertifcateInstalled($hostName); |
||
| 35 | } |
||
| 36 | |||
| 37 | throw CouldNotDownloadCertificate::unknownError($hostName, $thrown->getMessage()); |
||
| 38 | } |
||
| 39 | |||
| 40 | $response = stream_context_get_params($client); |
||
| 41 | |||
| 42 | return openssl_x509_parse($response['options']['ssl']['peer_certificate']); |
||
| 43 | } |
||
| 44 | } |
||
| 45 |