Skip to content

Commit

Permalink
Expose the remote address that served the certificates in the Downloa…
Browse files Browse the repository at this point in the history
…der (#110)

* Expose the remote address of the server that responded to the Download() request of the certificates

* Add test to expose remoteAddress

* style
  • Loading branch information
mattiasgeniar authored and freekmurze committed Sep 30, 2019
1 parent 4fdfe3b commit 3b1798a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public function setTimeout(int $timeOutInSeconds)
public function getCertificates(string $hostName): array
{
$response = $this->fetchCertificates($hostName);
$remoteAddress = $response['remoteAddress'];

$peerCertificate = $response['options']['ssl']['peer_certificate'];

$peerCertificateChain = $response['options']['ssl']['peer_certificate_chain'] ?? [];

$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);
Expand All @@ -86,7 +87,8 @@ public function getCertificates(string $hostName): array
return new SslCertificate(
$certificateFields,
$fingerprint,
$fingerprintSha256
$fingerprintSha256,
$remoteAddress
);
}, $fullCertificateChain);

Expand Down Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/SslCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class SslCertificate
/** @var string */
private $fingerprintSha256 = '';

/** @var string */
private $remoteAddress = '';

public static function download(): Downloader
{
return new Downloader();
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions tests/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 3b1798a

Please sign in to comment.