Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Verify public key format before usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jost125 committed Feb 25, 2017
1 parent 32dc091 commit d2c4198
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Cryptography/CryptographyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function getBkpCode(string $pkpCode): string

public function addWSESignature(string $request): string
{
$this->tryLoadPublicKey();
$securityKey = new \RobRichards\XMLSecLibs\XMLSecurityKey(\RobRichards\XMLSecLibs\XMLSecurityKey::RSA_SHA256, ['type' => 'private']);
$document = new \DOMDocument('1.0');
$document->loadXML($request);
Expand All @@ -79,4 +80,13 @@ public function addWSESignature(string $request): string
return $wse->saveXML();
}

private function tryLoadPublicKey()
{
$publicKeyResource = openssl_get_publickey(file_get_contents($this->publicKeyFile));
if ($publicKeyResource === false) {
throw new PublicKeyFileException($this->publicKeyFile);
}
openssl_free_key($publicKeyResource);
}

}
13 changes: 13 additions & 0 deletions tests/SlevomatEET/Cryptography/CryptographyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ public function testWSESignatureWithInvalidPrivateKeyPassword()
$crypto->addWSESignature($request);
}

public function testWSESignatureWithInvalidPublicKey()
{
$request = $this->getRequestData();
$crypto = $cryptoService = new CryptographyService(
self::PRIVATE_KEY_WITHOUT_PASSWORD_PATH,
self::INVALID_KEY_PATH
);

$this->expectException(PublicKeyFileException::class);
$this->expectExceptionMessage('Public key could not be loaded from file \'/var/www/eet-client/tests/SlevomatEET/Cryptography/invalid-certificate.pem');
$crypto->addWSESignature($request);
}

private function getReceiptData(): array
{
return [
Expand Down

0 comments on commit d2c4198

Please sign in to comment.