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

Kontrola existence cesty k veřejnému klíči #24

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Cryptography/CryptographyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class CryptographyService

public function __construct(string $privateKeyFile, string $publicKeyFile, string $privateKeyPassword = '')
{
if (!file_exists($privateKeyFile)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spíš is_file file_exists vrací true i pro adresáře

throw new PrivateKeyFileNotFoundException($privateKeyFile);
}
if (!file_exists($publicKeyFile)) {
throw new PublicKeyFileNotFoundException($publicKeyFile);
}
$this->privateKeyFile = $privateKeyFile;
$this->publicKeyFile = $publicKeyFile;
$this->privateKeyPassword = $privateKeyPassword;
Expand Down
8 changes: 8 additions & 0 deletions src/Cryptography/PrivateKeyFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace SlevomatEET\Cryptography;

class PrivateKeyFileNotFoundException extends PrivateKeyFileException
{

}
28 changes: 28 additions & 0 deletions src/Cryptography/PublicKeyFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace SlevomatEET\Cryptography;

class PublicKeyFileException extends \Exception
{

/**
* @var string
*/
private $publicKeyFile;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anotaci na jeden řádek


public function __construct(string $publicKeyFile, \Throwable $previous = null)
{
parent::__construct(sprintf(
'Public key could not be loaded from file \'%s\'. Please make sure that the file contains valid public key in PEM format.',
$publicKeyFile
), 0, $previous);

$this->publicKeyFile = $publicKeyFile;
}

public function getPublicKeyFile(): string
{
return $this->publicKeyFile;
}

}
8 changes: 8 additions & 0 deletions src/Cryptography/PublicKeyFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace SlevomatEET\Cryptography;

class PublicKeyFileNotFoundException extends PublicKeyFileException
{

}
71 changes: 50 additions & 21 deletions tests/SlevomatEET/Cryptography/CryptographyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,52 @@ class CryptographyServiceTest extends \PHPUnit\Framework\TestCase

const EXPECTED_PKP = 'a0asEiJhFCBlVtptSspKvEZhcrvnzF7SQ55C4DhnStnSu1b37GUI2+Dlme9P94UCPZ1oCUPJdsYOBZ3IX6aEgEe0FJKXYX0kXraYCJKIo3g64wRchE7iblIOBCK1uHh8qqHA66Isnhb6hqBOOdlt2aWO/0jCzlfeQr0axpPF1mohMnP3h3ICaxZh0dnMdju5OmMrq+91PL5T9KkR7bfGHqAoWJ0kmxY/mZumtRfGil2/xf7I5pdVeYXPgDO/Tojzm6J95n68fPDOXTDrTzKYmqDjpg3kmWepLNQKFXRmkQrkBLToJWG1LDUDm3UTTmPWzq4c0XnGcXJDZglxfolGpA==';
const EXPECTED_BKP = '9356D566-A3E48838-FB403790-D201244E-95DCBD92';
const PRIVATE_KEY_WITHOUT_PASSWORD_PATH = __DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key';
const PRIVATE_KEY_WITH_PASSWORD_PATH = __DIR__ . '/../../../cert/EET_CA1_Playground_With_Password-CZ00000019.key';
const PUBLIC_KEY_PATH = __DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub';

public function testGetCodes()
{
$data = $this->getReceiptData();
$crypto = new CryptographyService(__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key', __DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub');
$crypto = $this->createCryptographyServiceWithoutPassword();

$expectedPkp = base64_decode(self::EXPECTED_PKP);
$pkpCode = $crypto->getPkpCode($data);
self::assertSame($expectedPkp, $pkpCode);
self::assertSame(self::EXPECTED_BKP, $crypto->getBkpCode($pkpCode));
}

public function testExceptions()
/**
* @dataProvider provideInvalidKeyPaths
*/
public function testInvalidKeyPaths(string $privateKeyPath, string $publicKeyPath, string $expectedExceptionType)
{
try {
new CryptographyService($privateKeyPath, $publicKeyPath);
$this->fail('Exception ' . $expectedExceptionType . ' expected');
} catch (\PHPUnit\Framework\AssertionFailedError $exception) {
throw $exception;
} catch (\Throwable $exception) {
$this->assertInstanceOf($expectedExceptionType, $exception);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady by se hodilo spíš $this->expectException

}
}

/**
* @return array[]
*/
public function provideInvalidKeyPaths(): array
{
return [
[self::PRIVATE_KEY_WITHOUT_PASSWORD_PATH, './foo/path', PublicKeyFileNotFoundException::class],
['./foo/path', self::PUBLIC_KEY_PATH, PrivateKeyFileNotFoundException::class],
];
}

public function testInvalidPrivateKeyInPkpCalculation()
{
$cryptoService = new CryptographyService(
__DIR__ . '/invalid-certificate.pem',
__DIR__ . '/invalid-certificate.pem'
self::PUBLIC_KEY_PATH
);

try {
Expand All @@ -44,10 +73,7 @@ public function testExceptions2()
{
include __DIR__ . '/OpenSslFunctionsMock.php';

$cryptoService = new CryptographyService(
__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key',
__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub'
);
$cryptoService = $cryptoService = $this->createCryptographyServiceWithoutPassword();

try {
$cryptoService->getPkpCode($this->getReceiptData());
Expand All @@ -61,34 +87,23 @@ public function testExceptions2()
public function testWSESignatureWithoutPrivateKeyPassword()
{
$request = $this->getRequestData();
$crypto = new CryptographyService(
__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key',
__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub'
);
$crypto = $this->createCryptographyServiceWithoutPassword();

$this->assertNotEmpty($crypto->addWSESignature($request));
}

public function testWSESignatureWithPrivateKeyPassword()
{
$request = $this->getRequestData();
$crypto = new CryptographyService(
__DIR__ . '/../../../cert/EET_CA1_Playground_With_Password-CZ00000019.key',
__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub',
'eet'
);
$crypto = $this->createCryptographyServiceWithPassword('eet');

$this->assertNotEmpty($crypto->addWSESignature($request));
}

public function testWSESignatureWithInvalidPrivateKeyPassword()
{
$request = $this->getRequestData();
$crypto = new CryptographyService(
__DIR__ . '/../../../cert/EET_CA1_Playground_With_Password-CZ00000019.key',
__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub',
'invalid'
);
$crypto = $this->createCryptographyServiceWithPassword('invalid');

$this->expectException(\PHPUnit\Framework\Error\Error::class);
$this->expectExceptionMessage('openssl_sign(): supplied key param cannot be coerced into a private key');
Expand Down Expand Up @@ -127,4 +142,18 @@ private function getRequestData(): string
return $request;
}

private function createCryptographyServiceWithoutPassword(): CryptographyService
{
return new CryptographyService(self::PRIVATE_KEY_WITHOUT_PASSWORD_PATH, self::PUBLIC_KEY_PATH);
}

private function createCryptographyServiceWithPassword(string $password): CryptographyService
{
return new CryptographyService(
self::PRIVATE_KEY_WITH_PASSWORD_PATH,
self::PUBLIC_KEY_PATH,
$password
);
}

}