Skip to content

Commit

Permalink
New InvalidCertificateException and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jan 27, 2016
1 parent 7ef2719 commit b4ea243
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
16 changes: 12 additions & 4 deletions lib/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public function __construct($pem) {
}

if (!$cert = @openssl_x509_read($pem)) {
throw new \InvalidArgumentException("Invalid PEM encoded certificate!");
throw new InvalidCertificateException("Invalid PEM encoded certificate!");
}

$this->pem = $pem;

if (!$this->info = openssl_x509_parse($cert)) {
throw new \InvalidArgumentException("Invalid PEM encoded certificate!");
throw new InvalidCertificateException("Invalid PEM encoded certificate!");
}
}

Expand Down Expand Up @@ -116,17 +116,25 @@ public function __debugInfo() {
}

public static function derToPem($der) {
if (!is_string($der)) {
throw new \InvalidArgumentException("\$der must be a string, " . gettype($der) . " given.");
}

return sprintf(
"-----BEGIN CERTIFICATE-----\n%s-----END CERTIFICATE-----",
"-----BEGIN CERTIFICATE-----\n%s-----END CERTIFICATE-----\n",
chunk_split(base64_encode($der), 64, "\n")
);
}

public static function pemToDer($pem) {
if (!is_string($pem)) {
throw new \InvalidArgumentException("\$pem must be a string, " . gettype($pem) . " given.");
}

$pattern = "@-----BEGIN CERTIFICATE-----\n([a-zA-Z0-9+/=\n]+)-----END CERTIFICATE-----@";

if (!preg_match($pattern, $pem, $match)) {
throw new \RuntimeException("Invalid PEM could not be converted to DER format.");
throw new InvalidCertificateException("Invalid PEM could not be converted to DER format.");
}

return base64_decode(str_replace(["\n", "\r"], "", trim($match[1])));
Expand Down
7 changes: 7 additions & 0 deletions lib/InvalidCertificateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Kelunik\Certificate;

class InvalidCertificateException extends \Exception {

}
36 changes: 30 additions & 6 deletions test/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public function testCommon() {
$this->assertSame("Let's Encrypt", $cert->getIssuer()->getOrganizationName());
$this->assertSame("Let's Encrypt Authority X1", $cert->getIssuer()->getCommonName());
$this->assertFalse($cert->isSelfSigned());
$this->assertSame($raw, (string) $cert);
$this->assertSame(trim($raw), trim((string) $cert));
$this->assertSame(trim($raw), trim($cert->toPem()));
$this->assertSame(trim($raw), trim(Certificate::derToPem($cert->toDer())));
$this->assertSame([
"commonName" => "www.kelunik.com",
"names" => ["kelunik.com", "www.kelunik.com"],
Expand Down Expand Up @@ -43,23 +45,45 @@ public function testSignature() {
}

public function testDerToPem() {
$raw = file_get_contents(__DIR__ . "/data/localhost.pem");
$transformed = Certificate::pemToDer(Certificate::derToPem($raw));
$pem = file_get_contents(__DIR__ . "/data/localhost.pem");
$der = file_get_contents(__DIR__ . "/data/localhost.der");

$this->assertSame($raw, $transformed);
$this->assertSame($der, Certificate::pemToDer($pem));
$this->assertSame($pem, Certificate::derToPem($der));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testNonString() {
new Certificate(0);
public function testInvalidDerType() {
Certificate::derToPem(0);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidPemType() {
Certificate::pemToDer(0);
}

/**
* @expectedException \Kelunik\Certificate\InvalidCertificateException
*/
public function testInvalidPem() {
Certificate::pemToDer("");
}

/**
* @expectedException \InvalidArgumentException
*/
public function testNonString() {
new Certificate(0);
}

/**
* @expectedException \Kelunik\Certificate\InvalidCertificateException
*/
public function testInvalidPemConstruct() {
new Certificate("");
}
}
Binary file added test/data/localhost.der
Binary file not shown.

0 comments on commit b4ea243

Please sign in to comment.