Skip to content

Commit a75caed

Browse files
authored
Merge pull request #25 from MayMeow/dev/fix-24
Remove passhrase from RSAParameters
2 parents bbd4737 + 81ef989 commit a75caed

File tree

5 files changed

+31
-46
lines changed

5 files changed

+31
-46
lines changed

src/RSAParameters.php

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

1312
protected array $config = [
1413
'digest_alg' => 'sha256',
@@ -27,13 +26,13 @@ public function __construct()
2726
* @param array|null $configArgs
2827
* @return $this
2928
*/
30-
public function generateKeys(?string $passphrase = null, ?array $configArgs = null): RSAParameters
29+
public function generateKeys(string $passphrase, ?array $configArgs = null, string $salt = 'salt'): RSAParameters
3130
{
3231
$keys = openssl_pkey_new($this->config);
3332

3433
if ($keys) {
3534
openssl_pkey_export($keys, $private);
36-
$this->privateKey = $this->encryptPrivateKey(privateKey: $private);
35+
$this->privateKey = $this->encryptPrivateKey(privateKey: $private, passphrase: $passphrase, salt: $salt);
3736

3837
$pub = openssl_pkey_get_details($keys);
3938

@@ -45,22 +44,22 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
4544
return $this;
4645
}
4746

48-
private function encryptPrivateKey(string $privateKey, string $salt = 'salt'): string
47+
private function encryptPrivateKey(string $passphrase, string $privateKey, string $salt): string
4948
{
5049
$aes = new AESCryptoServiceProvider();
5150
$aes->generateIV();
5251
$k = new CryptoKey();
53-
$key = $k->getCryptographicKey($this->passphrase, $salt);
52+
$key = $k->getCryptographicKey($passphrase, $salt);
5453
$aes->setKey($key);
5554

5655
return $aes->encrypt($privateKey);
5756
}
5857

59-
private function decryptPrivateKey(string $privateKey, string $salt = 'salt'): string
58+
private function decryptPrivateKey(string $passphrase, string $privateKey, string $salt): string
6059
{
6160
$aes = new AESCryptoServiceProvider();
6261
$k = new CryptoKey();
63-
$key = $k->getCryptographicKey($this->passphrase, $salt);
62+
$key = $k->getCryptographicKey($passphrase, $salt);
6463
$aes->setKey($key);
6564

6665
return $aes->decrypt($privateKey);
@@ -72,11 +71,12 @@ private function decryptPrivateKey(string $privateKey, string $salt = 'salt'): s
7271
* @return string|\OpenSSLAsymmetricKey
7372
* @throws DecryptPrivateKeyException
7473
*/
75-
public function getPrivateKey(string $salt = 'salt', bool $encrypted = false): \OpenSSLAsymmetricKey|string
74+
public function getPrivateKey(string $passphrase, string $salt = 'salt', bool $encrypted = false): \OpenSSLAsymmetricKey|string
7675
{
7776
if (!$encrypted) {
7877
return $this->decryptPrivateKey(
7978
privateKey: $this->privateKey,
79+
passphrase: $passphrase,
8080
salt: $salt
8181
);
8282
}
@@ -90,9 +90,8 @@ public function getPrivateKey(string $salt = 'salt', bool $encrypted = false): \
9090
* @param string $privateKey
9191
* @param string $passphrase
9292
*/
93-
public function setPrivateKey(string $privateKey, string $passphrase, string $salt = 'salt'): void
93+
public function setPrivateKey(string $privateKey): void
9494
{
95-
$this->passphrase = $passphrase;
9695
$this->privateKey = $privateKey;
9796
}
9897

@@ -116,29 +115,6 @@ public function setPublicKey(string $publicKey): void
116115
$this->publicKey = $publicKey;
117116
}
118117

119-
/**
120-
* Returns passphrase for private key decryption
121-
*
122-
* @return string
123-
*/
124-
public function getPassphrase(): ?string
125-
{
126-
return $this->passphrase;
127-
}
128-
129-
/**
130-
* Set passphrase for private key
131-
*
132-
* @param string $passphrase
133-
* @return $this
134-
*/
135-
public function setPassphrase(string $passphrase): RSAParameters
136-
{
137-
$this->passphrase = $passphrase;
138-
139-
return $this;
140-
}
141-
142118
/**
143119
* @return array
144120
*/

src/Tools/RsaParametersWriter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ public function __construct(RSAParametersLocatorInterface $locator)
2828
* @param RSAParameters $RSAParameters
2929
* @throws \MayMeow\Cryptography\Exceptions\DecryptPrivateKeyException
3030
*/
31-
public function write(RSAParameters $RSAParameters): void
31+
public function write(RSAParameters $RSAParameters, string $privateKeyPass, string $salt): void
3232
{
3333
file_put_contents($this->locator->locatePublicKey(), $RSAParameters->getPublicKey());
34-
file_put_contents($this->locator->locatePrivateKey(), $RSAParameters->getPrivateKey(encrypted: true));
35-
file_put_contents($this->locator->locatePassphrase(), $RSAParameters->getPassphrase());
34+
file_put_contents($this->locator->locatePrivateKey(), $RSAParameters->getPrivateKey(
35+
encrypted: true,
36+
passphrase: $privateKeyPass,
37+
salt: $salt
38+
));
39+
file_put_contents($this->locator->locatePassphrase(), $privateKeyPass);
3640
}
3741
}

src/Tools/RsaParametersWriterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ interface RsaParametersWriterInterface
99
/**
1010
* Write parameters to given location
1111
*/
12-
public function write(RSAParameters $RSAParameters): void;
12+
public function write(RSAParameters $RSAParameters, string $privateKeyPass, string $salt): void;
1313
}

tests/RSACryptoServiceProviderTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,37 @@
99

1010
class RSACryptoServiceProviderTest extends TestCase
1111
{
12+
protected string $salt = 'salt';
13+
protected string $passphrase = 'passphrase';
14+
1215
/** @test */
1316
public function canEncryptAndDecryptText()
1417
{
1518
$plainText = "This is going to be encrypted!";
1619
$parameters = new RSAParameters();
17-
$parameters->generateKeys("passphrase");
20+
$parameters->generateKeys(passphrase: $this->passphrase, salt: $this->salt);
1821

1922
$rsa = new RSACryptoServiceProvider();
2023
$rsa->setParameters($parameters);
2124
$encryptedTest = $rsa->encrypt($plainText);
2225

23-
$this->assertEquals($plainText, $rsa->decrypt($encryptedTest));
26+
$this->assertEquals($plainText, $rsa->decrypt($encryptedTest, privateKeyPass: $this->passphrase, salt: $this->salt));
2427
}
2528

2629
/** @test */
2730
public function canSealData()
2831
{
2932
$plainText = "This is going";
3033
$parameters = new RSAParameters();
31-
$parameters->generateKeys("passphrase");
34+
$parameters->generateKeys(passphrase: $this->passphrase, salt: $this->salt);
3235

3336
$rsa = new RSACryptoServiceProvider();
3437
$rsa->setParameters($parameters);
3538

3639
$aes = new AESCryptoServiceProvider();
3740

3841
$sealed = $aes->seal($plainText, $parameters, humanReadableData: true);
39-
$opened = $aes->open($sealed[1], $sealed[0], $parameters);
42+
$opened = $aes->open($sealed[1], $sealed[0], $parameters, $this->passphrase, $this->salt);
4043

4144
$this->assertEquals($plainText, $opened);
4245
}

tests/RSAParametersTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
class RSAParametersTest extends TestCase
1313
{
14-
14+
protected string $salt = 'salt';
15+
protected string $passphrase = 'passphrase';
16+
1517
/** @test */
1618
public function canGenerateKeys() :void
1719
{
1820
$parameters = new RSAParameters();
19-
$keys = $parameters->generateKeys();
21+
$keys = $parameters->generateKeys(passphrase: $this->passphrase, salt: $this->salt);
2022

2123
$this->assertInstanceOf(RSAParameters::class, $keys);
2224
}
@@ -25,11 +27,11 @@ public function canGenerateKeys() :void
2527
public function canExportKeysAndImportToFile() : void
2628
{
2729
$parameters = new RSAParameters();
28-
$parameters->generateKeys();
30+
$parameters->generateKeys(passphrase: $this->passphrase, salt: $this->salt);
2931
$locator = new TestingParametersLocator();
3032

3133
$writer = new RsaParametersWriter($locator);
32-
$writer->write($parameters);
34+
$writer->write($parameters, privateKeyPass: $this->passphrase, salt: $this->salt);
3335

3436
// Assert if exported files are on disk
3537
$this->assertTrue(file_exists($locator->locatePrivateKey()));
@@ -53,6 +55,6 @@ public function canExportKeysAndImportToFile() : void
5355
$csp2->setParameters($parameters2);
5456

5557
// Check if imported parameters are same as parameters that was exported
56-
$this->assertEquals($text, $csp2->decrypt($encryptedText));
58+
$this->assertEquals($text, $csp2->decrypt($encryptedText, privateKeyPass: $this->passphrase, salt: $this->salt));
5759
}
5860
}

0 commit comments

Comments
 (0)