Skip to content

Commit

Permalink
Update hash generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriy Svirin committed Jan 11, 2021
1 parent 7a10113 commit f3481d5
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace AndrewSvirin\Ebics\Contracts;
namespace AndrewSvirin\Ebics\Contracts\BankLetter;

use AndrewSvirin\Ebics\Models\BankLetter;

/**
* EBICS BankLetterFormatterInterface representation.
* EBICS formatter for bank letter.
*
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
interface BankLetterFormatterInterface
interface FormatterInterface
{

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Contracts/BankLetter/HashGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AndrewSvirin\Ebics\Contracts\BankLetter;

use AndrewSvirin\Ebics\Contracts\SignatureInterface;

/**
* EBICS Generate hash for bank letter.
* Strategy pattern.
*
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
interface HashGeneratorInterface
{

/**
* Generate hash.
*
* @param SignatureInterface $signature
*
* @return string
*/
public function generate(SignatureInterface $signature): string;
}
23 changes: 17 additions & 6 deletions src/EbicsBankLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace AndrewSvirin\Ebics;

use AndrewSvirin\Ebics\Contracts\BankLetterFormatterInterface;
use AndrewSvirin\Ebics\Contracts\BankLetter\FormatterInterface;
use AndrewSvirin\Ebics\Factories\BankLetterFactory;
use AndrewSvirin\Ebics\Models\Bank;
use AndrewSvirin\Ebics\Models\BankLetter;
use AndrewSvirin\Ebics\Models\KeyRing;
use AndrewSvirin\Ebics\Models\User;
use AndrewSvirin\Ebics\Services\BankLetter\HashGenerator\CertificateHashGenerator;
use AndrewSvirin\Ebics\Services\BankLetter\HashGenerator\PublicKeyHashGenerator;
use AndrewSvirin\Ebics\Services\BankLetterService;

/**
Expand Down Expand Up @@ -48,20 +50,29 @@ public function __construct()
*/
public function prepareBankLetter(Bank $bank, User $user, KeyRing $keyRing): BankLetter
{
if ($bank->isCertified()) {
$hashGenerator = new CertificateHashGenerator();
} else {
$hashGenerator = new PublicKeyHashGenerator();
}

$bankLetter = $this->bankLetterFactory->create(
$bank,
$user,
$this->bankLetterService->formatSignatureForBankLetter(
$keyRing->getUserSignatureA(),
$keyRing->getUserSignatureAVersion()
$keyRing->getUserSignatureAVersion(),
$hashGenerator
),
$this->bankLetterService->formatSignatureForBankLetter(
$keyRing->getUserSignatureE(),
$keyRing->getUserSignatureEVersion()
$keyRing->getUserSignatureEVersion(),
$hashGenerator
),
$this->bankLetterService->formatSignatureForBankLetter(
$keyRing->getUserSignatureX(),
$keyRing->getUserSignatureXVersion()
$keyRing->getUserSignatureXVersion(),
$hashGenerator
)
);

Expand All @@ -72,11 +83,11 @@ public function prepareBankLetter(Bank $bank, User $user, KeyRing $keyRing): Ban
* Format bank letter.
*
* @param BankLetter $bankLetter
* @param BankLetterFormatterInterface $formatter
* @param FormatterInterface $formatter
*
* @return mixed
*/
public function formatBankLetter(BankLetter $bankLetter, BankLetterFormatterInterface $formatter)
public function formatBankLetter(BankLetter $bankLetter, FormatterInterface $formatter)
{
return $formatter->format($bankLetter);
}
Expand Down
6 changes: 3 additions & 3 deletions src/EbicsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function INI(DateTime $dateTime = null): Response
}
$signatureA = $this->signatureFactory->createSignatureAFromKeys(
$this->cryptService->generateKeys($this->keyRing),
$this->bank->getIsCertified() ? $this->x509Generator : null
$this->bank->isCertified() ? $this->x509Generator : null
);
$request = $this->requestFactory->buildINI($signatureA, $dateTime);
$response = $this->httpClient->post($this->bank->getUrl(), $request);
Expand All @@ -149,11 +149,11 @@ public function HIA(DateTime $dateTime = null): Response
}
$signatureE = $this->signatureFactory->createSignatureEFromKeys(
$this->cryptService->generateKeys($this->keyRing),
$this->bank->getIsCertified() ? $this->x509Generator : null
$this->bank->isCertified() ? $this->x509Generator : null
);
$signatureX = $this->signatureFactory->createSignatureXFromKeys(
$this->cryptService->generateKeys($this->keyRing),
$this->bank->getIsCertified() ? $this->x509Generator : null
$this->bank->isCertified() ? $this->x509Generator : null
);
$request = $this->requestFactory->buildHIA($signatureE, $signatureX, $dateTime);
$response = $this->httpClient->post($this->bank->getUrl(), $request);
Expand Down
6 changes: 3 additions & 3 deletions src/Handlers/OrderDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function handleINI(DOMDocument $xml, SignatureInterface $certificateA, Da
$xmlSignaturePubKeyInfo = $xml->createElement('SignaturePubKeyInfo');
$xmlSignaturePubKeyOrderData->appendChild($xmlSignaturePubKeyInfo);

if ($this->bank->getIsCertified()) {
if ($this->bank->isCertified()) {
$this->handleX509Data($xmlSignaturePubKeyInfo, $xml, $certificateA);
}
$this->handlePubKeyValue($xmlSignaturePubKeyInfo, $xml, $certificateA, $dateTime);
Expand Down Expand Up @@ -144,7 +144,7 @@ public function handleHIA(
$xmlAuthenticationPubKeyInfo = $xml->createElement('AuthenticationPubKeyInfo');
$xmlHIARequestOrderData->appendChild($xmlAuthenticationPubKeyInfo);

if ($this->bank->getIsCertified()) {
if ($this->bank->isCertified()) {
$this->handleX509Data($xmlAuthenticationPubKeyInfo, $xml, $certificateX);
}
$this->handlePubKeyValue($xmlAuthenticationPubKeyInfo, $xml, $certificateX, $dateTime);
Expand All @@ -158,7 +158,7 @@ public function handleHIA(
$xmlEncryptionPubKeyInfo = $xml->createElement('EncryptionPubKeyInfo');
$xmlHIARequestOrderData->appendChild($xmlEncryptionPubKeyInfo);

if ($this->bank->getIsCertified()) {
if ($this->bank->isCertified()) {
$this->handleX509Data($xmlEncryptionPubKeyInfo, $xml, $certificateE);
}
$this->handlePubKeyValue($xmlEncryptionPubKeyInfo, $xml, $certificateE, $dateTime);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Bank.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function setIsCertified(bool $isCertified): void
/**
* @return bool
*/
public function getIsCertified(): bool
public function isCertified(): bool
{
return (bool)$this->isCertified;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace AndrewSvirin\Ebics\Services\BankLetterFormatter;
namespace AndrewSvirin\Ebics\Services\BankLetter\Formatter;

use AndrewSvirin\Ebics\Contracts\BankLetterFormatterInterface;
use AndrewSvirin\Ebics\Contracts\BankLetter\FormatterInterface;
use AndrewSvirin\Ebics\Models\Bank;
use AndrewSvirin\Ebics\Models\BankLetter;
use AndrewSvirin\Ebics\Models\SignatureBankLetter;
Expand All @@ -16,7 +16,7 @@
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
class BankLetterFormatterHtml implements BankLetterFormatterInterface
class HtmlBankLetterFormatter implements FormatterInterface
{

public function format(BankLetter $bankLetter)
Expand Down Expand Up @@ -138,7 +138,7 @@ private function formatSection(Bank $bank, User $user, SignatureBankLetter $sign
<br/><br/>
<b>{$translations['hash']}</b>
<br/>
{$signatureBankLetter->getKeyHash()}
{$this->formatBytes($signatureBankLetter->getKeyHash())}
EOF;

return $result;
Expand Down Expand Up @@ -220,10 +220,12 @@ private function formatBytes(string $bytes): string
*/
private function formatCertificateContent(string $certificateContent): string
{
return trim(str_replace(
['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----'],
$result = trim(str_replace(
['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----', "\n", "\r"],
'',
$certificateContent
));

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace AndrewSvirin\Ebics\Services\BankLetterFormatter;
namespace AndrewSvirin\Ebics\Services\BankLetter\Formatter;

use AndrewSvirin\Ebics\Contracts\BankLetterFormatterInterface;
use AndrewSvirin\Ebics\Contracts\BankLetter\FormatterInterface;
use AndrewSvirin\Ebics\Factories\PdfFactory;
use AndrewSvirin\Ebics\Models\BankLetter;

Expand All @@ -12,11 +12,11 @@
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
class BankLetterFormatterPdf implements BankLetterFormatterInterface
class PdfBankLetterFormatter implements FormatterInterface
{

/**
* @var BankLetterFormatterHtml
* @var HtmlBankLetterFormatter
*/
private $bankLetterFormatterHtml;

Expand All @@ -27,7 +27,7 @@ class BankLetterFormatterPdf implements BankLetterFormatterInterface

public function __construct()
{
$this->bankLetterFormatterHtml = new BankLetterFormatterHtml();
$this->bankLetterFormatterHtml = new HtmlBankLetterFormatter();
$this->pdfFactory = new PdfFactory();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace AndrewSvirin\Ebics\Services\BankLetterFormatter;
namespace AndrewSvirin\Ebics\Services\BankLetter\Formatter;

use AndrewSvirin\Ebics\Contracts\BankLetterFormatterInterface;
use AndrewSvirin\Ebics\Contracts\BankLetter\FormatterInterface;
use AndrewSvirin\Ebics\Models\BankLetter;
use AndrewSvirin\Ebics\Models\SignatureBankLetter;
use RuntimeException;
Expand All @@ -13,7 +13,7 @@
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
class BankLetterFormatterTxt implements BankLetterFormatterInterface
class TxtBankLetterFormatter implements FormatterInterface
{

public function format(BankLetter $bankLetter)
Expand Down
36 changes: 36 additions & 0 deletions src/Services/BankLetter/HashGenerator/CertificateHashGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace AndrewSvirin\Ebics\Services\BankLetter\HashGenerator;

use AndrewSvirin\Ebics\Contracts\BankLetter\HashGeneratorInterface;
use AndrewSvirin\Ebics\Contracts\SignatureInterface;
use AndrewSvirin\Ebics\Services\CryptService;

/**
* Generate hash for certificate.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
class CertificateHashGenerator implements HashGeneratorInterface
{

/**
* @var CryptService
*/
private $cryptService;

public function __construct()
{
$this->cryptService = new CryptService();
}

/**
* @inheritDoc
*/
public function generate(SignatureInterface $signature): string
{
$key = $signature->getCertificateContent();

return $this->cryptService->calculateKeyHash($key);
}
}
42 changes: 42 additions & 0 deletions src/Services/BankLetter/HashGenerator/PublicKeyHashGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace AndrewSvirin\Ebics\Services\BankLetter\HashGenerator;

use AndrewSvirin\Ebics\Contracts\BankLetter\HashGeneratorInterface;
use AndrewSvirin\Ebics\Contracts\SignatureInterface;
use AndrewSvirin\Ebics\Services\CryptService;

/**
* Generate hash for public key.
*
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrew Svirin
*/
class PublicKeyHashGenerator implements HashGeneratorInterface
{

/**
* @var CryptService
*/
private $cryptService;

public function __construct()
{
$this->cryptService = new CryptService();
}

/**
* @inheritDoc
*/
public function generate(SignatureInterface $signature): string
{
$publicKeyDetails = $this->cryptService->getPublicKeyDetails($signature->getPublicKey());

$key = $this->cryptService->calculateKey(
$publicKeyDetails['e'],
$publicKeyDetails['m']
);

return $this->cryptService->calculateKeyHash($key);
}
}
11 changes: 7 additions & 4 deletions src/Services/BankLetterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AndrewSvirin\Ebics\Services;

use AndrewSvirin\Ebics\Contracts\BankLetter\HashGeneratorInterface;
use AndrewSvirin\Ebics\Contracts\SignatureInterface;
use AndrewSvirin\Ebics\Factories\CertificateX509Factory;
use AndrewSvirin\Ebics\Factories\SignatureBankLetterFactory;
Expand Down Expand Up @@ -42,19 +43,21 @@ public function __construct()
* @param SignatureInterface $signature
* @param string $version
*
* @param HashGeneratorInterface $hashGenerator
*
* @return SignatureBankLetter
*/
public function formatSignatureForBankLetter(
SignatureInterface $signature,
string $version
string $version,
HashGeneratorInterface $hashGenerator
): SignatureBankLetter {
$publicKeyDetails = $this->cryptService->getPublicKeyDetails($signature->getPublicKey());

$exponentFormatted = $this->formatBytesForBank($publicKeyDetails['e']);
$modulusFormatted = $this->formatBytesForBank($publicKeyDetails ['m']);
$modulusFormatted = $this->formatBytesForBank($publicKeyDetails['m']);

$key = $this->cryptService->calculateKey($exponentFormatted, $modulusFormatted);
$keyHash = $this->cryptService->calculateKeyHash($key);
$keyHash = $hashGenerator->generate($signature);
$keyHashFormatted = $this->formatKeyHashForBankLetter($keyHash);

$signatureBankLetter = $this->signatureBankLetterFactory->create(
Expand Down
8 changes: 4 additions & 4 deletions src/Services/CryptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ public function binToArray(string $bytes): array
*/
public function calculateDigest(SignatureInterface $signature, $algorithm = 'sha256'): string
{
$publicKey = $this->rsaFactory->create();
$publicKey->loadKey($signature->getPublicKey());
$exponent = $publicKey->getExponent()->toHex(true);
$modulus = $publicKey->getModulus()->toHex(true);
$rsa = $this->rsaFactory->create();
$rsa->loadKey($signature->getPublicKey());
$exponent = $rsa->getExponent()->toHex(true);
$modulus = $rsa->getModulus()->toHex(true);
// If key was formed incorrect with Modulus and Exponent mismatch, then change the place of key parts.
if (strlen($exponent) > strlen($modulus)) {
$buffer = $exponent;
Expand Down
Loading

0 comments on commit f3481d5

Please sign in to comment.