Skip to content

Commit a99df21

Browse files
committed
Changed options for createUserSignatures.
1 parent 91876ae commit a99df21

32 files changed

+590
-221
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5
2+
3+
* Changed `options` for `createUserSignatures`.
4+
15
## 2.4
26

37
* Fixed HVE order type.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use EbicsApi\Ebics\Models\X509\BankX509Generator;
5757
// Prepare `workspace` dir in the __PATH_TO_WORKSPACES_DIR__ manually.
5858
// "__EBICS_VERSION__" should have value "VERSION_30" for EBICS 3.0
5959
$keyringPath = __PATH_TO_WORKSPACES_DIR__ . '/workspace/keyring.json';
60-
$keyringManager = new FileKeyringManager(new KeyringFactory);
60+
$keyringManager = new FileKeyringManager();
6161
if (is_file($keyringPath)) {
6262
$keyring = $keyringManager->loadKeyring($keyringPath, __PASSWORD__, __EBICS_VERSION__);
6363
} else {

src/Contracts/Crypt/RSAInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace EbicsApi\Ebics\Contracts\Crypt;
44

5+
use EbicsApi\Ebics\Models\Crypt\Key;
56
use EbicsApi\Ebics\Models\Crypt\KeyPair;
67
use EbicsApi\Ebics\Models\Crypt\RSA;
78

src/Contracts/EbicsClientInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface EbicsClientInterface
3535

3636
/**
3737
* Create user signatures A, E and X on first launch.
38-
* @param array|null $options Setup to specify custom certificate, private_key and version
38+
* @param array|null $options Setup to specify custom certificate, private, public keys and version
3939
* for Electronic Signature, Authorization and Identification, Encryption details.
4040
*/
4141
public function createUserSignatures(?array $options = null): void;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace EbicsApi\Ebics\Contracts;
4+
5+
use EbicsApi\Ebics\Models\Crypt\Key;
6+
7+
/**
8+
* KeyStorageInterface.
9+
*
10+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
11+
* @author Andrew Svirin
12+
*/
13+
interface KeyStorageInterface
14+
{
15+
/**
16+
* Write public key to storage.
17+
*
18+
* @param Key $key
19+
*
20+
* @return string
21+
*/
22+
public function writePublicKey(Key $key): string;
23+
24+
/**
25+
* Read public key from storage.
26+
*
27+
* @param string $key
28+
*
29+
* @return Key
30+
*/
31+
public function readPublicKey(string $key): Key;
32+
33+
/**
34+
* Write private key to storage.
35+
*
36+
* @param Key $key
37+
*
38+
* @return string
39+
*/
40+
public function writePrivateKey(Key $key): string;
41+
42+
/**
43+
* Read private key from storage.
44+
*
45+
* @param string $key
46+
*
47+
* @return Key
48+
*/
49+
public function readPrivateKey(string $key): Key;
50+
51+
/**
52+
* Write certificate to storage.
53+
*
54+
* @param string $certificate
55+
*
56+
* @return string
57+
*/
58+
public function writeCertificate(string $certificate): string;
59+
60+
/**
61+
* Read certificate from storage.
62+
*
63+
* @param string $certificate
64+
*
65+
* @return string
66+
*/
67+
public function readCertificate(string $certificate): string;
68+
}

src/Contracts/SignatureInterface.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace EbicsApi\Ebics\Contracts;
44

5+
use EbicsApi\Ebics\Models\Crypt\Key;
6+
57
/**
68
* EBICS SignatureInterface representation.
79
*
@@ -26,14 +28,14 @@ interface SignatureInterface
2628
public function getType(): string;
2729

2830
/**
29-
* @return string
31+
* @return Key
3032
*/
31-
public function getPublicKey(): string;
33+
public function getPublicKey(): Key;
3234

3335
/**
34-
* @return string|null
36+
* @return Key|null
3537
*/
36-
public function getPrivateKey(): ?string;
38+
public function getPrivateKey(): ?Key;
3739

3840
/**
3941
* @param string|null $certificateContent

src/EbicsBankLetter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use EbicsApi\Ebics\Contracts\BankLetter\FormatterInterface;
66
use EbicsApi\Ebics\Factories\BankLetterFactory;
77
use EbicsApi\Ebics\Factories\CertificateX509Factory;
8+
use EbicsApi\Ebics\Factories\Crypt\AESFactory;
9+
use EbicsApi\Ebics\Factories\Crypt\RSAFactory;
810
use EbicsApi\Ebics\Factories\EbicsFactoryV24;
911
use EbicsApi\Ebics\Factories\EbicsFactoryV25;
1012
use EbicsApi\Ebics\Factories\EbicsFactoryV30;
@@ -18,6 +20,7 @@
1820
use EbicsApi\Ebics\Services\BankLetter\Formatter\TxtBankLetterFormatter;
1921
use EbicsApi\Ebics\Services\BankLetterService;
2022
use EbicsApi\Ebics\Services\CryptService;
23+
use EbicsApi\Ebics\Services\RandomService;
2124
use LogicException;
2225

2326
/**
@@ -33,9 +36,13 @@ final class EbicsBankLetter
3336
private BankLetterFactory $bankLetterFactory;
3437
private CryptService $cryptService;
3538

36-
public function __construct()
39+
public function __construct(array $options = [])
3740
{
38-
$this->cryptService = new CryptService();
41+
$this->cryptService = new CryptService(
42+
new RSAFactory($options['rsa_class_map'] ?? null),
43+
new AESFactory(),
44+
new RandomService()
45+
);
3946
$this->bankLetterService = new BankLetterService(
4047
$this->cryptService,
4148
new SignatureBankLetterFactory(),

src/EbicsClient.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
use EbicsApi\Ebics\Exceptions\PasswordEbicsException;
2222
use EbicsApi\Ebics\Factories\BufferFactory;
2323
use EbicsApi\Ebics\Factories\CertificateX509Factory;
24+
use EbicsApi\Ebics\Factories\Crypt\AESFactory;
2425
use EbicsApi\Ebics\Factories\Crypt\BigIntegerFactory;
26+
use EbicsApi\Ebics\Factories\Crypt\RSAFactory;
2527
use EbicsApi\Ebics\Factories\DocumentFactory;
2628
use EbicsApi\Ebics\Factories\EbicsExceptionFactory;
2729
use EbicsApi\Ebics\Factories\EbicsFactoryV24;
@@ -35,6 +37,9 @@
3537
use EbicsApi\Ebics\Handlers\OrderDataHandler;
3638
use EbicsApi\Ebics\Handlers\ResponseHandler;
3739
use EbicsApi\Ebics\Models\Bank;
40+
use EbicsApi\Ebics\Models\Crypt\Key;
41+
use EbicsApi\Ebics\Models\Crypt\KeyPair;
42+
use EbicsApi\Ebics\Models\Crypt\RSA;
3843
use EbicsApi\Ebics\Models\DownloadOrderResult;
3944
use EbicsApi\Ebics\Models\DownloadSegment;
4045
use EbicsApi\Ebics\Models\DownloadTransaction;
@@ -50,6 +55,7 @@
5055
use EbicsApi\Ebics\Models\X509\ContentX509Generator;
5156
use EbicsApi\Ebics\Services\CryptService;
5257
use EbicsApi\Ebics\Services\CurlHttpClient;
58+
use EbicsApi\Ebics\Services\RandomService;
5359
use EbicsApi\Ebics\Services\SchemaValidator;
5460
use EbicsApi\Ebics\Services\XmlService;
5561
use EbicsApi\Ebics\Services\ZipService;
@@ -104,10 +110,12 @@ public function __construct(Bank $bank, User $user, Keyring $keyring, array $opt
104110
throw new LogicException(sprintf('Version "%s" is not implemented', $keyring->getVersion()));
105111
}
106112

113+
$rsaFactory = new RSAFactory($options['rsa_class_map'] ?? null);
114+
107115
$this->segmentFactory = new SegmentFactory();
108-
$this->cryptService = new CryptService();
116+
$this->cryptService = new CryptService($rsaFactory, new AESFactory(), new RandomService());
109117
$this->zipService = new ZipService();
110-
$this->signatureFactory = new SignatureFactory();
118+
$this->signatureFactory = new SignatureFactory($rsaFactory);
111119
$this->bufferFactory = new BufferFactory($options['buffer_filename'] ?? 'php://memory');
112120

113121
$this->orderDataHandler = $ebicsFactory->createOrderDataHandler(
@@ -1330,10 +1338,9 @@ private function createUserSignature(string $type, ?array $details = null): Sign
13301338
$keyPair = $this->cryptService->generateKeyPair($this->keyring->getPassword());
13311339
$certificateGenerator = $this->keyring->getCertificateGenerator();
13321340
} else {
1333-
$keyPair = $this->cryptService->changePrivateKeyPassword(
1334-
$details['privatekey'],
1335-
$details['password'],
1336-
$this->keyring->getPassword()
1341+
$keyPair = new KeyPair(
1342+
new Key($details['publickey'], $details['publickey_type']),
1343+
new Key($details['privatekey'], $details['privatekey_type'])
13371344
);
13381345
$certificateGenerator = new ContentX509Generator();
13391346
$certificateGenerator->setAContent($details['certificate']);
@@ -1393,7 +1400,10 @@ public function checkKeyring(): bool
13931400
public function changeKeyringPassword(string $newPassword): void
13941401
{
13951402
$keyPair = $this->cryptService->changePrivateKeyPassword(
1396-
$this->keyring->getUserSignatureA()->getPrivateKey(),
1403+
new KeyPair(
1404+
$this->keyring->getUserSignatureA()->getPublicKey(),
1405+
$this->keyring->getUserSignatureA()->getPrivateKey()
1406+
),
13971407
$this->keyring->getPassword(),
13981408
$newPassword
13991409
);
@@ -1407,7 +1417,10 @@ public function changeKeyringPassword(string $newPassword): void
14071417
$this->keyring->setUserSignatureA($signature);
14081418

14091419
$keyPair = $this->cryptService->changePrivateKeyPassword(
1410-
$this->keyring->getUserSignatureX()->getPrivateKey(),
1420+
new KeyPair(
1421+
$this->keyring->getUserSignatureX()->getPublicKey(),
1422+
$this->keyring->getUserSignatureX()->getPrivateKey()
1423+
),
14111424
$this->keyring->getPassword(),
14121425
$newPassword
14131426
);
@@ -1421,7 +1434,10 @@ public function changeKeyringPassword(string $newPassword): void
14211434
$this->keyring->setUserSignatureX($signature);
14221435

14231436
$keyPair = $this->cryptService->changePrivateKeyPassword(
1424-
$this->keyring->getUserSignatureE()->getPrivateKey(),
1437+
new KeyPair(
1438+
$this->keyring->getUserSignatureE()->getPublicKey(),
1439+
$this->keyring->getUserSignatureE()->getPrivateKey()
1440+
),
14251441
$this->keyring->getPassword(),
14261442
$newPassword
14271443
);

src/Factories/Crypt/RSAFactory.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EbicsApi\Ebics\Factories\Crypt;
44

55
use EbicsApi\Ebics\Contracts\Crypt\RSAInterface;
6+
use EbicsApi\Ebics\Models\Crypt\Key;
67
use EbicsApi\Ebics\Models\Crypt\RSA;
78

89
/**
@@ -14,41 +15,56 @@
1415
final class RSAFactory
1516
{
1617
/**
18+
* @var array<int, class-string<RSAInterface>>
19+
*/
20+
private array $classMap;
21+
22+
/**
23+
* @param array<int, class-string<RSAInterface>> $classMap
24+
*/
25+
public function __construct(?array $classMap = null)
26+
{
27+
$this->classMap = $classMap ?? [
28+
RSA::PRIVATE_FORMAT_PKCS1 => RSA::class,
29+
RSA::PUBLIC_FORMAT_PKCS1 => RSA::class,
30+
];
31+
}
32+
33+
/**
34+
* @param int $type
1735
* @return RSAInterface
1836
*/
19-
public function create(): RSAInterface
37+
public function create(int $type): RSAInterface
2038
{
21-
return new RSA();
39+
return new $this->classMap[$type];
2240
}
2341

2442
/**
2543
* Create RSA from private key.
2644
*
27-
* @param string $privateKey
45+
* @param Key $privateKey
2846
* @param string $password
29-
*
3047
* @return RSAInterface
3148
*/
32-
public function createPrivate(string $privateKey, string $password): RSAInterface
49+
public function createPrivate(Key $privateKey, string $password): RSAInterface
3350
{
34-
$rsa = $this->create();
51+
$rsa = $this->create($privateKey->getType());
3552
$rsa->setPassword($password);
36-
$rsa->loadKey($privateKey, RSA::PRIVATE_FORMAT_PKCS1);
53+
$rsa->loadKey($privateKey->getKey(), $privateKey->getType());
3754

3855
return $rsa;
3956
}
4057

4158
/**
4259
* Create RSA from public key.
4360
*
44-
* @param string|array $publicKey
45-
*
61+
* @param Key $publicKey
4662
* @return RSAInterface
4763
*/
48-
public function createPublic($publicKey): RSAInterface
64+
public function createPublic(Key $publicKey): RSAInterface
4965
{
50-
$rsa = $this->create();
51-
$rsa->loadKey($publicKey);
66+
$rsa = $this->create($publicKey->getType());
67+
$rsa->loadKey($publicKey->getKey());
5268
$rsa->setPublicKey();
5369

5470
return $rsa;

0 commit comments

Comments
 (0)