Skip to content

Commit

Permalink
add the ability to load from file a DER based certificate (#177)
Browse files Browse the repository at this point in the history
* add the ability to load from file a DER based certificate

* move der2pem function to SslCertificate class as a static function

* modify the url for different port test (connection problem fo previous host)
  • Loading branch information
marco-introini authored Oct 28, 2022
1 parent b40b2a5 commit 69a61d2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/SslCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public static function createForHostName(string $url, int $timeout = 30, bool $v

public static function createFromFile(string $pathToCertificate): self
{
return self::createFromString(file_get_contents($pathToCertificate));
$fileContents = file_get_contents($pathToCertificate);
if (!str_contains($fileContents,'BEGIN CERTIFICATE')) {
$fileContents = self::der2pem($fileContents);
}
return self::createFromString($fileContents);
}

public static function createFromString(string $certificatePem): self
Expand All @@ -48,6 +52,12 @@ public static function createFromArray(array $properties): self
);
}

public static function der2pem($der_data): string
{
$pem = chunk_split(base64_encode($der_data), 64, "\n");
return "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n";
}

public function __construct(
protected array $rawCertificateFields,
protected string $fingerprint = '',
Expand Down
6 changes: 3 additions & 3 deletions tests/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ protected function setUp(): void
{
parent::setUp();

$this->domainWithDifferentPort = 'ben.hotweb.de';
$this->ipDomainWithDifferentPort = '178.254.8.25';
$this->domainWithDifferentPort = 'psd2.b2b.belfius.be';
$this->ipDomainWithDifferentPort = '141.96.1.12';
$this->differentPort = 8443;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ public function it_can_download_a_certificate_for_a_host_name_from_an_ipv6_addre
$this->markTestSkipped('Github Actions have no IPv6 Support');
}
$sslCertificate = SslCertificate::download()
->fromIpAddress('2a00:1450:4001:80e::200e')
->fromIpAddress('2607:f8b0:4004:c07::64')
->forHost('google.com');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
Expand Down
43 changes: 43 additions & 0 deletions tests/SslCertificateFromFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Spatie\SslCertificate\Test;

use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;
use Spatie\SslCertificate\SslCertificate;

class SslCertificateFromFileTest extends TestCase
{
use MatchesSnapshots;

/** @var Spatie\SslCertificate\SslCertificate */
protected $certificate;


protected function setUp(): void
{
parent::setUp();

Carbon::setTestNow(Carbon::create('2020', '01', '13', '03', '18', '13', 'utc'));


}

/** @test */
public function it_can_load_pem_certificate()
{
$this->certificate = SslCertificate::createFromFile(__DIR__.'/stubs/spatieCertificate.pem');
$this->assertSame("Let's Encrypt", $this->certificate->getOrganization());
$this->assertSame("analytics.spatie.be", $this->certificate->getDomain());
}

/** @test */
public function it_can_load_der_certificate()
{
$this->certificate = SslCertificate::createFromFile(__DIR__.'/stubs/derCertificate.der');
$this->assertSame("Let's Encrypt", $this->certificate->getOrganization());
$this->assertSame("analytics.spatie.be", $this->certificate->getDomain());
}

}
2 changes: 1 addition & 1 deletion tests/SslCertificateFromStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function setUp(): void

$this->certificate = SslCertificate::createFromString($certificate);

$this->domainWithDifferentPort = 'ben.hotweb.de';
$this->domainWithDifferentPort = 'psd2.b2b.belfius.be';
$this->differentPort = 8443;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/SslCertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void

$this->certificate = new SslCertificate($rawCertificateFields);

$this->domainWithDifferentPort = 'ben.hotweb.de';
$this->domainWithDifferentPort = 'psd2.b2b.belfius.be';
$this->differentPort = 8443;
}

Expand Down
Binary file added tests/stubs/derCertificate.der
Binary file not shown.

0 comments on commit 69a61d2

Please sign in to comment.