Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7708a90

Browse filesBrowse files
committed
Implemented the EncryptionInterface with Sodium
1 parent 12804bb commit 7708a90
Copy full SHA for 7708a90

20 files changed

+390
-647
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Symfony\Component\Config\Definition\ConfigurationInterface;
2323
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2424
use Symfony\Component\DependencyInjection\Exception\LogicException;
25-
use Symfony\Component\Encryption\SymmetricEncryptionInterface;
25+
use Symfony\Component\Encryption\EncryptionInterface;
2626
use Symfony\Component\Form\Form;
2727
use Symfony\Component\HttpClient\HttpClient;
2828
use Symfony\Component\HttpFoundation\Cookie;
@@ -1897,7 +1897,7 @@ private function addEncryptionSection(ArrayNodeDefinition $rootNode)
18971897
->children()
18981898
->arrayNode('encryption')
18991899
->info('encryption configuration')
1900-
->{!class_exists(FullStack::class) && interface_exists(SymmetricEncryptionInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1900+
->{!class_exists(FullStack::class) && interface_exists(EncryptionInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
19011901
->end()
19021902
->end()
19031903
;

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/encryption.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/encryption.php
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use phpseclib\Crypt\AES;
15-
use Symfony\Component\Encryption\AsymmetricEncryptionInterface;
16-
use Symfony\Component\Encryption\Provider\PhpseclibEncryption;
17-
use Symfony\Component\Encryption\Provider\SodiumEncryption;
18-
use Symfony\Component\Encryption\SymmetricEncryptionInterface;
15+
use Symfony\Component\Encryption\EncryptionInterface;
16+
use Symfony\Component\Encryption\Phpseclib\PhpseclibEncryption;
17+
use Symfony\Component\Encryption\Sodium\SodiumEncryption;
1918

2019
return static function (ContainerConfigurator $container) {
2120
$sodiumInstalled = \function_exists('sodium_crypto_box_keypair');
@@ -31,7 +30,6 @@
3130
->args([
3231
'%kernel.secret%',
3332
])
34-
->alias(SymmetricEncryptionInterface::class, $phpseclibInstalled && !$sodiumInstalled ? 'security.encryption.phpseclib' : 'security.encryption.sodium')
35-
->alias(AsymmetricEncryptionInterface::class, $phpseclibInstalled && !$sodiumInstalled ? 'security.encryption.phpseclib' : 'security.encryption.sodium')
33+
->alias(EncryptionInterface::class, $phpseclibInstalled && !$sodiumInstalled ? 'security.encryption.phpseclib' : 'security.encryption.sodium')
3634
;
3735
};

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Bundle\FullStack;
1818
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1919
use Symfony\Component\Config\Definition\Processor;
20-
use Symfony\Component\Encryption\SymmetricEncryptionInterface;
20+
use Symfony\Component\Encryption\EncryptionInterface;
2121
use Symfony\Component\HttpClient\HttpClient;
2222
use Symfony\Component\Lock\Store\SemaphoreStore;
2323
use Symfony\Component\Mailer\Mailer;
@@ -537,7 +537,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
537537
'limiters' => [],
538538
],
539539
'encryption' => [
540-
'enabled' => !class_exists(FullStack::class) && interface_exists(SymmetricEncryptionInterface::class),
540+
'enabled' => !class_exists(FullStack::class) && interface_exists(EncryptionInterface::class),
541541
],
542542
];
543543
}

‎src/Symfony/Component/Encryption/AsymmetricEncryptionInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Encryption/AsymmetricEncryptionInterface.php
-74Lines changed: 0 additions & 74 deletions
This file was deleted.

‎src/Symfony/Component/Encryption/EncryptionInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Encryption/EncryptionInterface.php
+51-3Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ interface EncryptionInterface
2626
*
2727
* Don't lose your private key and make sure to keep it a secret.
2828
*
29+
* @param string|null $secret to be used in symmetric encryption. A new secret
30+
* is generated if none is provided.
31+
*
2932
* @throws EncryptionException
3033
*/
31-
public function generateKey(): KeyInterface;
34+
public function generateKey(string $secret = null): KeyInterface;
3235

3336
/**
3437
* Get an encrypted version of the message.
@@ -43,7 +46,14 @@ public function generateKey(): KeyInterface;
4346
*
4447
* Symmetric encryption is in theory weaker than asymmetric encryption.
4548
*
46-
* @param string $message plain text version of the message
49+
* <code>
50+
* $key = $encryption->generateKey();
51+
* $ciphertext = $encryption->encrypt('input', $key);
52+
* $message = $encryption->decrypt($ciphertext, $key);
53+
* </code>
54+
*
55+
* @param string $message plain text version of the message
56+
* @param KeyInterface $myKey a key that holds a string secret
4757
*
4858
* @return string the output
4959
*
@@ -61,12 +71,34 @@ public function encrypt(string $message, KeyInterface $myKey): string;
6171
* When Alice and Bob wants to communicate they share their public keys with
6272
* each other. Alice will encrypt a message with bobs public key. When Bob
6373
* receive the message, he will decrypt it with his private key.
74+
*
75+
*
76+
* <code>
77+
* $bobKey = $encryption->generateKey();
78+
* $bobPublicKey = serialize($bobKey->createPublicKey());
79+
*
80+
* // Bob sends the public key to Alice
81+
*
82+
* $key = unserialize($bobPublicKey);
83+
* $ciphertext = $encryption->encryptFor('input', $key);
84+
*
85+
* // Alice sends ciphertext to bob
86+
*
87+
* $message = $encryption->decrypt($ciphertext, $bobKey);
88+
* </code>
89+
*
90+
* @param string $message plain text version of the message
91+
* @param KeyInterface $recipientKey A key with a public key of the recipient
92+
*
93+
* @return string the output
94+
*
95+
* @throws EncryptionException
6496
*/
6597
public function encryptFor(string $message, KeyInterface $recipientKey): string;
6698

6799
/**
68100
* Get an encrypted version of the message that only the recipient can read.
69-
* The recipient can also verify who sent the message
101+
* The recipient can also verify who sent the message.
70102
*
71103
* Asymmetric encryption uses a "key pair" ie a public key and a private key.
72104
* It is safe to share your public key, but the private key should always be
@@ -75,6 +107,22 @@ public function encryptFor(string $message, KeyInterface $recipientKey): string;
75107
* When Alice and Bob wants to communicate they share their public keys with
76108
* each other. Alice will encrypt a message with keypair [ alice_private, bob_public ].
77109
* When Bob receive the message, he will decrypt it with keypair [ bob_private, alice_public ].
110+
*
111+
* <code>
112+
* $aliceKey = $encryption->generateKey();
113+
* $bobKey = $encryption->generateKey();
114+
* $keypair = $aliceKey->createKeypair($bobKey);
115+
*
116+
* $ciphertext = $encryption->encryptForAndSign('input', $keypair);
117+
* $message = $encryption->decrypt($ciphertext, $bobKey->createKeypair($aliceKey));
118+
* </code>
119+
*
120+
* @param string $message plain text version of the message
121+
* @param KeyInterface $keypair A key with a public key of the recipient and a private key of the sender
122+
*
123+
* @return string the output
124+
*
125+
* @throws EncryptionException
78126
*/
79127
public function encryptForAndSign(string $message, KeyInterface $keypair): string;
80128

‎src/Symfony/Component/Encryption/KeyInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Encryption/KeyInterface.php
+20-7Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
namespace Symfony\Component\Encryption;
1313

14-
use Symfony\Component\Encryption\Exception\DecryptionException;
15-
use Symfony\Component\Encryption\Exception\EncryptionException;
16-
1714
/**
1815
* A Key for a specific user and specific Encryption implementation. Keys cannot
1916
* be shared between Encryption implementations.
@@ -27,19 +24,35 @@
2724
interface KeyInterface
2825
{
2926
/**
30-
* Returns a string to be stored in a safe place
27+
* Returns a string to be stored in a safe place.
3128
*/
3229
public function toString(): string;
3330

3431
/**
35-
* Creates a Key from stored data
32+
* Creates a Key from stored data.
3633
*/
3734
public function fromString(string $string): self;
3835

3936
/**
40-
* Get the public key from this Key. Not all Keys have a public key.
37+
* Creates a new KeyInterface object.
38+
*
39+
* When Alice wants to send and sign a message to Bob. She takes her private
40+
* Key and pair it with Bob's public key.
41+
*
42+
* <code>
43+
* $aliceKey = SodiumKey::fromString('...');
44+
* $bobKey = SodiumKey::fromString('...');
45+
* $keypair = $aliceKey->createKeypair($bobKey);
46+
* </code>
47+
*/
48+
public function createKeypair(self $publicKey): self;
49+
50+
/**
51+
* Creates a new KeyInterface object.
52+
*
53+
* When Alice wants share her public key with Bob, she sends him this object.
4154
*
4255
* The public key can be shared.
4356
*/
44-
public function getPublicKey(): ?string;
57+
public function createPublicKey(): self;
4558
}

‎src/Symfony/Component/Encryption/Provider/PhpseclibEncryption.php renamed to ‎src/Symfony/Component/Encryption/Phpseclib/PhpseclibEncryption.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Encryption/Phpseclib/PhpseclibEncryption.php
+3-11Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Encryption\Provider;
12+
namespace Symfony\Component\Encryption\Phpseclib;
1313

1414
use phpseclib\Crypt\AES;
1515
use phpseclib\Crypt\Random;
1616
use phpseclib\Crypt\RSA;
1717
use Symfony\Component\Encryption\AsymmetricEncryptionInterface;
18+
use Symfony\Component\Encryption\Ciphertext;
1819
use Symfony\Component\Encryption\Exception\DecryptionException;
1920
use Symfony\Component\Encryption\Exception\EncryptionException;
2021
use Symfony\Component\Encryption\Exception\InvalidArgumentException;
2122
use Symfony\Component\Encryption\Exception\SignatureVerificationRequiredException;
2223
use Symfony\Component\Encryption\Exception\UnableToVerifySignatureException;
2324
use Symfony\Component\Encryption\Exception\UnsupportedAlgorithmException;
24-
use Symfony\Component\Encryption\Ciphertext;
2525
use Symfony\Component\Encryption\SymmetricEncryptionInterface;
2626

2727
if (!class_exists(RSA::class)) {
@@ -37,15 +37,7 @@
3737
*/
3838
class PhpseclibEncryption implements SymmetricEncryptionInterface, AsymmetricEncryptionInterface
3939
{
40-
private $secret;
41-
42-
/**
43-
* @var string application secret
44-
*/
45-
public function __construct(string $secret)
46-
{
47-
$this->secret = $secret;
48-
}
40+
// TODO fix this class
4941

5042
public function generateKeypair(): array
5143
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.