diff --git a/src/Downloader.php b/src/Downloader.php index 03e954f..3c3e3a3 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -70,6 +70,7 @@ public function setTimeout(int $timeOutInSeconds) public function getCertificates(string $hostName): array { $response = $this->fetchCertificates($hostName); + $remoteAddress = $response['remoteAddress']; $peerCertificate = $response['options']['ssl']['peer_certificate']; @@ -77,7 +78,7 @@ public function getCertificates(string $hostName): array $fullCertificateChain = array_merge([$peerCertificate], $peerCertificateChain); - $certificates = array_map(function ($certificate) { + $certificates = array_map(function ($certificate) use ($remoteAddress) { $certificateFields = openssl_x509_parse($certificate); $fingerprint = openssl_x509_fingerprint($certificate); @@ -86,7 +87,8 @@ public function getCertificates(string $hostName): array return new SslCertificate( $certificateFields, $fingerprint, - $fingerprintSha256 + $fingerprintSha256, + $remoteAddress ); }, $fullCertificateChain); @@ -144,6 +146,8 @@ protected function fetchCertificates(string $hostName): array $response = stream_context_get_params($client); + $response['remoteAddress'] = stream_socket_get_name($client, true); + fclose($client); return $response; diff --git a/src/SslCertificate.php b/src/SslCertificate.php index 95c6099..682e791 100644 --- a/src/SslCertificate.php +++ b/src/SslCertificate.php @@ -18,6 +18,9 @@ class SslCertificate /** @var string */ private $fingerprintSha256 = ''; + /** @var string */ + private $remoteAddress = ''; + public static function download(): Downloader { return new Downloader(); @@ -31,13 +34,16 @@ public static function createForHostName(string $url, int $timeout = 30): self public function __construct( array $rawCertificateFields, string $fingerprint = '', - string $fingerprintSha256 = '') + string $fingerprintSha256 = '', + string $remoteAddress = '') { $this->rawCertificateFields = $rawCertificateFields; $this->fingerprint = $fingerprint; $this->fingerprintSha256 = $fingerprintSha256; + + $this->remoteAddress = $remoteAddress; } public function getRawCertificateFields(): array @@ -224,6 +230,11 @@ public function getHash(): string return md5($this->getRawCertificateFieldsJson()); } + public function getRemoteAddress(): string + { + return $this->remoteAddress; + } + public function __toString(): string { return $this->getRawCertificateFieldsJson(); diff --git a/tests/DownloaderTest.php b/tests/DownloaderTest.php index fd71683..7dedc87 100644 --- a/tests/DownloaderTest.php +++ b/tests/DownloaderTest.php @@ -66,4 +66,12 @@ public function it_can_detect_when_no_certificate_is_installed() Downloader::downloadCertificateFromUrl('hipsteadresjes.gent'); } + + /** @test */ + public function it_can_retrieve_the_ip_address_of_the_server_that_served_the_certificates() + { + $sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be'); + + $this->assertEquals('138.197.187.74:443', $sslCertificate->getRemoteAddress()); + } }