-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Encryption] New component #39344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[Encryption] New component #39344
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1dd1b20
Adding new Encryption component
Nyholm 2348d74
Apply suggestions from code review
Nyholm 2aa4938
Fixed exceptions
Nyholm 3fedf0b
Use PHP8 syntax
Nyholm 7a63069
Minor fixes
Nyholm 4000685
Apply suggestions from code review
Nyholm 4792b64
minior
Nyholm 3c608b3
From suggestion by Alexander
Nyholm bc85b34
updated comment
Nyholm 3ac8156
Readme update
Nyholm e49230e
Remove signing
Nyholm 045c278
Move nonce to the cyphertext headers
Nyholm 117c77b
Rename cihpertext
Nyholm fe7806f
Update src/Symfony/Bundle/FrameworkBundle/Resources/config/encryption…
Nyholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Symfony/Bundle/FrameworkBundle/Resources/config/encryption.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\Encryption\EncryptionInterface; | ||
use Symfony\Component\Encryption\Sodium\SodiumEncryption; | ||
|
||
return static function (ContainerConfigurator $container) { | ||
$container->services() | ||
->set('encryption.sodium', SodiumEncryption::class) | ||
->alias(EncryptionInterface::class, 'encryption.sodium') | ||
->alias('encryption', 'encryption.sodium') | ||
; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/Tests export-ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
vendor/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.0 | ||
--- | ||
|
||
* Introduced the component as experimental |
110 changes: 110 additions & 0 deletions
110
src/Symfony/Component/Encryption/EncryptionInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption; | ||
|
||
use Symfony\Component\Encryption\Exception\DecryptionException; | ||
use Symfony\Component\Encryption\Exception\EncryptionException; | ||
use Symfony\Component\Encryption\Exception\InvalidKeyException; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
interface EncryptionInterface | ||
{ | ||
/** | ||
* Generates a new key to be used for encryption. | ||
* | ||
* Don't lose your private key and make sure to keep it a secret. | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param string|null $secret A secret to be used in symmetric encryption. A | ||
* new secret is generated if none is provided. | ||
*/ | ||
public function generateKey(string $secret = null): KeyInterface; | ||
|
||
/** | ||
* Gets an encrypted version of the message. | ||
* | ||
* Symmetric encryption uses the same key to encrypt and decrypt a message. | ||
* The key should be kept safe and should not be exposed to the public. Symmetric | ||
* encryption should be used when you are sending the encrypted message to | ||
* yourself. | ||
* | ||
* Example: You store a value on disk or in a cookie and don't want anyone else | ||
* to read it. | ||
* | ||
* Symmetric encryption is in theory weaker than asymmetric encryption. | ||
* | ||
* <code> | ||
* $key = $encryption->generateKey(); | ||
* $ciphertext = $encryption->encrypt('input', $key); | ||
* $message = $encryption->decrypt($ciphertext, $key); | ||
* </code> | ||
* | ||
* @param string $message Plain text version of the message | ||
* @param KeyInterface $key A key that holds a string secret | ||
* | ||
* @return string formatted as a Symfony Encryption Token | ||
* | ||
* @throws EncryptionException | ||
* @throws InvalidKeyException | ||
*/ | ||
public function encrypt(string $message, KeyInterface $key): string; | ||
|
||
/** | ||
* Gets an encrypted version of the message that only the recipient can read. | ||
* | ||
* Asymmetric encryption uses a "key pair" i.e. a public key and a private key. | ||
* It is safe to share the public key, but the private key should always be | ||
* kept a secret. | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* When Alice and Bob want to communicate securely, they share their public keys with | ||
* each other. Alice will encrypt a message with Bob's public key. When Bob | ||
* receives the message, he will decrypt it with his private key. | ||
* | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* <code> | ||
* // Bob: | ||
* $bobKey = $encryption->generateKey(); | ||
* $bobPublicOnly = $bobKey->extractPublicKey(); | ||
* // Bob sends $bobPublicOnly to Alice | ||
* | ||
* // Alice: | ||
* $ciphertext = $encryption->encryptFor('input', $bobPublicOnly); | ||
* // Alice sends $ciphertext to Bob | ||
* | ||
* // Bob: | ||
* $message = $encryption->decrypt($ciphertext, $bobKey); | ||
* </code> | ||
* | ||
* @param string $message Plain text version of the message | ||
* @param KeyInterface $recipientKey Key with a public key of the recipient | ||
* | ||
* @return string formatted as a Symfony Encryption Token | ||
* | ||
* @throws EncryptionException | ||
* @throws InvalidKeyException | ||
*/ | ||
public function encryptFor(string $message, KeyInterface $recipientKey): string; | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Gets a plain text version of the encrypted message. | ||
* | ||
* @param string $message formatted in the Symfony Encryption Token format | ||
* @param KeyInterface $key Key of the recipient, it should contain a private key | ||
* | ||
* @throws DecryptionException | ||
* @throws InvalidKeyException | ||
*/ | ||
public function decrypt(string $message, KeyInterface $key): string; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Encryption/Exception/DecryptionException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption\Exception; | ||
|
||
/** | ||
* Thrown when a message cannot be decrypted. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
class DecryptionException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
public function __construct(string $message = null, \Throwable $previous = null) | ||
{ | ||
parent::__construct($message ?? 'Could not decrypt the ciphertext.', 0, $previous); | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Encryption/Exception/EncryptionException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption\Exception; | ||
|
||
/** | ||
* Thrown when a message cannot be encrypted. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
class EncryptionException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
public function __construct(string $message = null, \Throwable $previous = null) | ||
{ | ||
parent::__construct($message ?? 'Could not encrypt the message.', 0, $previous); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Encryption/Exception/ExceptionInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption\Exception; | ||
|
||
/** | ||
* Base ExceptionInterface for the Encryption Component. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
interface ExceptionInterface extends \Throwable | ||
{ | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Encryption/Exception/InvalidKeyException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption\Exception; | ||
|
||
/** | ||
* Thrown when there is an issue with the Key. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
class InvalidKeyException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
public function __construct(string $message = null, \Throwable $previous = null) | ||
{ | ||
parent::__construct($message ?? 'This key is not valid.', 0, $previous); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Encryption/Exception/MalformedCipherException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption\Exception; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
class MalformedCipherException extends DecryptionException | ||
{ | ||
public function __construct(string $message = null, \Throwable $previous = null) | ||
{ | ||
parent::__construct($message ?? 'The message you provided is not a valid ciphertext.', $previous); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Encryption/Exception/UnsupportedAlgorithmException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Encryption\Exception; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* | ||
* @experimental in 6.0 | ||
*/ | ||
class UnsupportedAlgorithmException extends DecryptionException | ||
{ | ||
public function __construct(string $algorithm, \Throwable $previous = null) | ||
{ | ||
parent::__construct(sprintf('The ciphertext is encrypted with "%s" algorithm. Decryption of that algorithm is not supported.', $algorithm), $previous); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.