TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Crypto\Cipher;
|
||||
|
||||
class CipherDecryptionFailedException extends CipherException {}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Crypto\Cipher;
|
||||
|
||||
use TYPO3\CMS\Core\Exception;
|
||||
|
||||
class CipherException extends Exception {}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Crypto\Cipher;
|
||||
|
||||
/**
|
||||
* Provides encryption and decryption based on XChaCha20-Poly1305.
|
||||
*/
|
||||
final readonly class CipherService
|
||||
{
|
||||
/**
|
||||
* Encrypts the provided plain text using a shared key and additional authenticated data.
|
||||
*
|
||||
* @param string $plainText The plain text to be encrypted.
|
||||
* @param SharedKey $key The shared key used for encryption.
|
||||
* @param string $additionalData Optional additional authenticated data that will be included in the encryption.
|
||||
*/
|
||||
public function encrypt(string $plainText, SharedKey $key, string $additionalData = ''): CipherValue
|
||||
{
|
||||
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
|
||||
$cipher = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
|
||||
$plainText,
|
||||
$additionalData,
|
||||
$nonce,
|
||||
$key->value
|
||||
);
|
||||
return new CipherValue($nonce, $cipher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts the provided cipher value using a shared key and optional additional authenticated data.
|
||||
*
|
||||
* @param CipherValue $cipherValue The cipher value containing encrypted data and nonce.
|
||||
* @param SharedKey $key The shared key used for decryption.
|
||||
* @param string $additionalData Optional additional authenticated data that was included during encryption.
|
||||
* @throws CipherDecryptionFailedException If decryption fails or the integrity check is invalid.
|
||||
*/
|
||||
public function decrypt(CipherValue $cipherValue, SharedKey $key, string $additionalData = ''): string
|
||||
{
|
||||
$result = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
|
||||
$cipherValue->cipher,
|
||||
$additionalData,
|
||||
$cipherValue->nonce,
|
||||
$key->value
|
||||
);
|
||||
if ($result === false) {
|
||||
throw new CipherDecryptionFailedException('Cipher could not be decrypted', 1762465681);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Crypto\Cipher;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\StringUtility;
|
||||
|
||||
final readonly class CipherValue implements \Stringable
|
||||
{
|
||||
public static function fromSerialized(string $value): self
|
||||
{
|
||||
$data = json_decode(StringUtility::base64urlDecode($value) ?: '', true);
|
||||
$nonce = StringUtility::base64urlDecode($data['nonce'] ?? '');
|
||||
$cipher = StringUtility::base64urlDecode($data['cipher'] ?? '');
|
||||
if (empty($nonce) || empty($cipher)) {
|
||||
throw new CipherException('Incorrect encoded message format', 1762450821);
|
||||
}
|
||||
return new self($nonce, $cipher);
|
||||
}
|
||||
|
||||
public function __construct(public string $nonce, public string $cipher)
|
||||
{
|
||||
if (strlen($nonce) !== SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
|
||||
throw new CipherException('Incorrect nonce byte length', 1762450477);
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->encode();
|
||||
}
|
||||
|
||||
public function encode(): string
|
||||
{
|
||||
$data = [
|
||||
'nonce' => StringUtility::base64urlEncode($this->nonce),
|
||||
'cipher' => StringUtility::base64urlEncode($this->cipher),
|
||||
];
|
||||
try {
|
||||
return StringUtility::base64urlEncode(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
|
||||
} catch (\JsonException) {
|
||||
throw new CipherException('Failed to encode cipher value', 1763068727);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Crypto\Cipher;
|
||||
|
||||
final readonly class KeyFactory
|
||||
{
|
||||
/**
|
||||
* Derives a 32-byte key, based on the existing encryptionKey.
|
||||
* The key is supposed to be used in a symmetric XChaCha20-Poly1305 ciphering.
|
||||
*
|
||||
* @param string $seed (non-secret) value, used to build an 8-byte context (e.g. classname)
|
||||
* @param int $subKeyId variation to the resulting derived key (value from 0 to PHP_INT_MAX)
|
||||
* @throws CipherException
|
||||
* @throws \SodiumException
|
||||
*/
|
||||
public function deriveSharedKeyFromEncryptionKey(string $seed, int $subKeyId = 1): SharedKey
|
||||
{
|
||||
$key = $this->adjustKeyLength($this->resolveEncryptionKey());
|
||||
// context must be exactly 8 bytes
|
||||
$context = hash('xxh64', $seed, true);
|
||||
return new SharedKey(
|
||||
sodium_crypto_kdf_derive_from_key(
|
||||
SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES,
|
||||
$subKeyId,
|
||||
$context,
|
||||
$key
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SharedKey instance from a given key.
|
||||
*
|
||||
* @throws CipherException
|
||||
*/
|
||||
public function createSharedKeyFromString(#[\SensitiveParameter] string $key): SharedKey
|
||||
{
|
||||
return new SharedKey($this->adjustKeyLength($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a SharedKey instance from a random key.
|
||||
*
|
||||
* @throws CipherException
|
||||
* @throws \Random\RandomException
|
||||
*/
|
||||
public function generateSharedKey(): SharedKey
|
||||
{
|
||||
return new SharedKey(random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures to use a 32-byte key for XChaCha20-Poly1305 encryption
|
||||
* (having a length of `SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES`).
|
||||
*/
|
||||
private function adjustKeyLength(#[\SensitiveParameter] $key): string
|
||||
{
|
||||
if (strlen($key) === SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
|
||||
return $key;
|
||||
}
|
||||
return hash('sha3-256', $key, true);
|
||||
}
|
||||
|
||||
private function resolveEncryptionKey(): string
|
||||
{
|
||||
$key = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] ?? null;
|
||||
if (!is_string($key) || $key === '') {
|
||||
throw new CipherException('No encryption key configured', 1762897148);
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Crypto\Cipher;
|
||||
|
||||
/**
|
||||
* Holds the secret key to be used with XChaCha20-Poly1305
|
||||
* (and basically other Sodium-based algorithms), having 32 bytes.
|
||||
*/
|
||||
final readonly class SharedKey
|
||||
{
|
||||
public function __construct(#[\SensitiveParameter] public string $value)
|
||||
{
|
||||
if (strlen($this->value) !== SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
|
||||
throw new CipherException(
|
||||
sprintf(
|
||||
'Length of key value must be %d bytes',
|
||||
SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES
|
||||
),
|
||||
1762508248
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user