Skip to content

Commit 5954d29

Browse files
committed
Refactor RSAParameters to use nullable passphrase and improve key encryption/decryption methods
1 parent 0641e28 commit 5954d29

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

src/RSAParameters.php

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class RSAParameters
88
{
99
private string $privateKey;
1010
private string $publicKey;
11-
private string $passphrase;
11+
private ?string $passphrase = 'test_passphrase';
1212

1313
protected array $config = [
1414
'digest_alg' => 'sha256',
@@ -31,15 +31,9 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
3131
{
3232
$keys = openssl_pkey_new($this->config);
3333

34-
if ($passphrase != null) {
35-
$this->passphrase = $passphrase;
36-
} else {
37-
$this->passphrase = (string)rand(100000, 999999);
38-
}
39-
4034
if ($keys) {
41-
openssl_pkey_export($keys, $private, $passphrase, $configArgs);
42-
$this->privateKey = $private;
35+
openssl_pkey_export($keys, $private);
36+
$this->privateKey = $this->_encryptPrivateKey(privateKey: $private);
4337

4438
$pub = openssl_pkey_get_details($keys);
4539

@@ -51,22 +45,40 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
5145
return $this;
5246
}
5347

48+
protected function _encryptPrivateKey(string $privateKey, string $salt = 'salt'): string
49+
{
50+
$aes = new AESCryptoServiceProvider();
51+
$aes->generateIV();
52+
$k = new CryptoKey();
53+
$key = $k->getCryptographicKey($this->passphrase, $salt);
54+
$aes->setKey($key);
55+
56+
return $aes->encrypt($privateKey);
57+
}
58+
59+
protected function _decryptPrivateKey(string $privateKey, string $salt = 'salt'): string
60+
{
61+
$aes = new AESCryptoServiceProvider();
62+
$k = new CryptoKey();
63+
$key = $k->getCryptographicKey($this->passphrase, $salt);
64+
$aes->setKey($key);
65+
66+
return $aes->decrypt($privateKey);
67+
}
68+
5469
/**
5570
* Returns Decrypted Key
5671
*
5772
* @return string|\OpenSSLAsymmetricKey
5873
* @throws DecryptPrivateKeyException
5974
*/
60-
public function getPrivateKey(): \OpenSSLAsymmetricKey|string
75+
public function getPrivateKey(string $salt = 'salt', bool $encrypted = false): \OpenSSLAsymmetricKey|string
6176
{
62-
if ($this->passphrase != null && $this->privateKey != null) {
63-
$privateKeyResource = openssl_pkey_get_private($this->privateKey, $this->passphrase);
64-
65-
if ($privateKeyResource == false) {
66-
throw new DecryptPrivateKeyException();
67-
}
68-
69-
return $privateKeyResource;
77+
if (!$encrypted) {
78+
return $this->_decryptPrivateKey(
79+
privateKey: $this->privateKey,
80+
salt: $salt
81+
);
7082
}
7183

7284
return $this->privateKey;
@@ -78,7 +90,7 @@ public function getPrivateKey(): \OpenSSLAsymmetricKey|string
7890
* @param string $privateKey
7991
* @param string $passphrase
8092
*/
81-
public function setPrivateKey(string $privateKey, string $passphrase): void
93+
public function setPrivateKey(string $privateKey, string $passphrase, string $salt = 'salt'): void
8294
{
8395
$this->passphrase = $passphrase;
8496
$this->privateKey = $privateKey;
@@ -109,7 +121,7 @@ public function setPublicKey(string $publicKey): void
109121
*
110122
* @return string
111123
*/
112-
public function getPassphrase(): string
124+
public function getPassphrase(): ?string
113125
{
114126
return $this->passphrase;
115127
}

0 commit comments

Comments
 (0)