|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SlevomatEET\Cryptography; |
|
4
|
|
|
|
|
5
|
|
|
class CryptographyService |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** @var string */ |
|
9
|
|
|
private $privateKeyFile; |
|
10
|
|
|
|
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
private $privateKeyPassword; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $publicKeyFile; |
|
16
|
|
|
|
|
17
|
9 |
|
public function __construct(string $privateKeyFile, string $publicKeyFile, string $privateKeyPassword = '') |
|
18
|
|
|
{ |
|
19
|
9 |
|
if (!file_exists($privateKeyFile)) { |
|
20
|
1 |
|
throw new PrivateKeyFileNotFoundException($privateKeyFile); |
|
21
|
|
|
} |
|
22
|
8 |
|
if (!file_exists($publicKeyFile)) { |
|
23
|
1 |
|
throw new PublicKeyFileNotFoundException($publicKeyFile); |
|
24
|
|
|
} |
|
25
|
7 |
|
$this->privateKeyFile = $privateKeyFile; |
|
26
|
7 |
|
$this->publicKeyFile = $publicKeyFile; |
|
27
|
7 |
|
$this->privateKeyPassword = $privateKeyPassword; |
|
28
|
7 |
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function getPkpCode(array $body): string |
|
31
|
|
|
{ |
|
32
|
|
|
$values = [ |
|
33
|
3 |
|
$body['dic_popl'], |
|
34
|
3 |
|
$body['id_provoz'], |
|
35
|
3 |
|
$body['id_pokl'], |
|
36
|
3 |
|
$body['porad_cis'], |
|
37
|
3 |
|
$body['dat_trzby'], |
|
38
|
3 |
|
$body['celk_trzba'], |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
3 |
|
$plaintext = implode('|', $values); |
|
42
|
|
|
|
|
43
|
3 |
|
$privateKey = file_get_contents($this->privateKeyFile); |
|
44
|
3 |
|
$privateKeyId = openssl_pkey_get_private($privateKey, $this->privateKeyPassword); |
|
45
|
3 |
|
if ($privateKeyId === false) { |
|
46
|
1 |
|
throw new PrivateKeyFileException($this->privateKeyFile); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
$ok = openssl_sign($plaintext, $signature, $privateKeyId, OPENSSL_ALGO_SHA256); |
|
50
|
2 |
|
if (!$ok) { |
|
51
|
1 |
|
throw new SigningFailedException($values); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
openssl_free_key($privateKeyId); |
|
55
|
|
|
|
|
56
|
1 |
|
return $signature; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function getBkpCode(string $pkpCode): string |
|
60
|
|
|
{ |
|
61
|
1 |
|
$bkp = strtoupper(sha1($pkpCode)); |
|
62
|
|
|
|
|
63
|
1 |
|
return implode('-', str_split($bkp, 8)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
public function addWSESignature(string $request): string |
|
67
|
|
|
{ |
|
68
|
4 |
|
$this->tryLoadPublicKey(); |
|
69
|
3 |
|
$securityKey = new \RobRichards\XMLSecLibs\XMLSecurityKey(\RobRichards\XMLSecLibs\XMLSecurityKey::RSA_SHA256, ['type' => 'private']); |
|
70
|
3 |
|
$document = new \DOMDocument('1.0'); |
|
71
|
3 |
|
$document->loadXML($request); |
|
72
|
3 |
|
$wse = new \RobRichards\WsePhp\WSSESoap($document); |
|
73
|
3 |
|
$securityKey->passphrase = $this->privateKeyPassword; |
|
74
|
3 |
|
$securityKey->loadKey($this->privateKeyFile, true); |
|
75
|
3 |
|
$wse->addTimestamp(); |
|
76
|
3 |
|
$wse->signSoapDoc($securityKey, ['algorithm' => \RobRichards\XMLSecLibs\XMLSecurityDSig::SHA256]); |
|
77
|
2 |
|
$binaryToken = $wse->addBinaryToken(file_get_contents($this->publicKeyFile)); |
|
78
|
2 |
|
$wse->attachTokentoSig($binaryToken); |
|
79
|
|
|
|
|
80
|
2 |
|
return $wse->saveXML(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
private function tryLoadPublicKey() |
|
84
|
|
|
{ |
|
85
|
4 |
|
$publicKeyResource = openssl_get_publickey(file_get_contents($this->publicKeyFile)); |
|
86
|
4 |
|
if ($publicKeyResource === false) { |
|
87
|
1 |
|
throw new PublicKeyFileException($this->publicKeyFile); |
|
88
|
|
|
} |
|
89
|
3 |
|
openssl_free_key($publicKeyResource); |
|
90
|
3 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|