primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,127 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Cipher;
use Defuse\Crypto\Crypto as DefuseCrypto;
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
use Defuse\Crypto\Key as DefuseKey;
use Defuse\Crypto\RuntimeTests;
use Joomla\Crypt\CipherInterface;
use Joomla\Crypt\Exception\DecryptionException;
use Joomla\Crypt\Exception\EncryptionException;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Exception\InvalidKeyTypeException;
use Joomla\Crypt\Key;
/**
* Joomla cipher for encryption, decryption and key generation via the php-encryption library.
*
* @since 2.0.0
*/
class Crypto implements CipherInterface
{
/**
* Method to decrypt a data string.
*
* @param string $data The encrypted string to decrypt.
* @param Key $key The key object to use for decryption.
*
* @return string The decrypted data string.
*
* @since 2.0.0
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'crypto') {
throw new InvalidKeyTypeException('crypto', $key->getType());
}
// Decrypt the data.
try {
return DefuseCrypto::decrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate()));
} catch (WrongKeyOrModifiedCiphertextException $ex) {
throw new DecryptionException('DANGER! DANGER! The ciphertext has been tampered with!', $ex->getCode(), $ex);
} catch (EnvironmentIsBrokenException $ex) {
throw new DecryptionException('Cannot safely perform decryption', $ex->getCode(), $ex);
}
}
/**
* Method to encrypt a data string.
*
* @param string $data The data string to encrypt.
* @param Key $key The key object to use for encryption.
*
* @return string The encrypted data string.
*
* @since 2.0.0
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'crypto') {
throw new InvalidKeyTypeException('crypto', $key->getType());
}
// Encrypt the data.
try {
return DefuseCrypto::encrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate()));
} catch (EnvironmentIsBrokenException $ex) {
throw new EncryptionException('Cannot safely perform encryption', $ex->getCode(), $ex);
}
}
/**
* Method to generate a new encryption key object.
*
* @param array $options Key generation options.
*
* @return Key
*
* @since 2.0.0
* @throws InvalidKeyException if the key cannot be generated
*/
public function generateKey(array $options = [])
{
// Generate the encryption key.
try {
$public = DefuseKey::createNewRandomKey();
} catch (EnvironmentIsBrokenException $ex) {
throw new InvalidKeyException('Cannot safely create a key', $ex->getCode(), $ex);
}
// Create the new encryption key object.
return new Key('crypto', $public->saveToAsciiSafeString(), $public->getRawBytes());
}
/**
* Check if the cipher is supported in this environment.
*
* @return boolean
*
* @since 2.0.0
*/
public static function isSupported(): bool
{
try {
RuntimeTests::runtimeTest();
return true;
} catch (EnvironmentIsBrokenException $e) {
return false;
}
}
}

View File

@ -0,0 +1,144 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Cipher;
use Joomla\Crypt\CipherInterface;
use Joomla\Crypt\Exception\DecryptionException;
use Joomla\Crypt\Exception\EncryptionException;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Exception\InvalidKeyTypeException;
use Joomla\Crypt\Key;
/**
* Joomla cipher for encryption, decryption and key generation via the openssl extension.
*
* @since 2.0.0
*/
class OpenSSL implements CipherInterface
{
/**
* Initialisation vector for key generator method.
*
* @var string
* @since 2.0.0
*/
private $iv;
/**
* Method to use for encryption.
*
* @var string
* @since 2.0.0
*/
private $method;
/**
* Instantiate the cipher.
*
* @param string $iv The initialisation vector to use
* @param string $method The encryption method to use
*
* @since 2.0.0
*/
public function __construct(string $iv, string $method)
{
$this->iv = $iv;
$this->method = $method;
}
/**
* Method to decrypt a data string.
*
* @param string $data The encrypted string to decrypt.
* @param Key $key The key object to use for decryption.
*
* @return string The decrypted data string.
*
* @since 2.0.0
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'openssl') {
throw new InvalidKeyTypeException('openssl', $key->getType());
}
$cleartext = openssl_decrypt($data, $this->method, $key->getPrivate(), true, $this->iv);
if ($cleartext === false) {
throw new DecryptionException('Failed to decrypt data');
}
return $cleartext;
}
/**
* Method to encrypt a data string.
*
* @param string $data The data string to encrypt.
* @param Key $key The key object to use for encryption.
*
* @return string The encrypted data string.
*
* @since 2.0.0
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'openssl') {
throw new InvalidKeyTypeException('openssl', $key->getType());
}
$encrypted = openssl_encrypt($data, $this->method, $key->getPrivate(), true, $this->iv);
if ($encrypted === false) {
throw new EncryptionException('Unable to encrypt data');
}
return $encrypted;
}
/**
* Method to generate a new encryption key object.
*
* @param array $options Key generation options.
*
* @return Key
*
* @since 2.0.0
* @throws InvalidKeyException if the key cannot be generated
*/
public function generateKey(array $options = [])
{
$passphrase = $options['passphrase'] ?? false;
if ($passphrase === false) {
throw new InvalidKeyException('Missing passphrase file');
}
return new Key('openssl', $passphrase, 'unused');
}
/**
* Check if the cipher is supported in this environment.
*
* @return boolean
*
* @since 2.0.0
*/
public static function isSupported(): bool
{
return \extension_loaded('openssl');
}
}

View File

@ -0,0 +1,209 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Cipher;
use Joomla\Crypt\CipherInterface;
use Joomla\Crypt\Exception\DecryptionException;
use Joomla\Crypt\Exception\EncryptionException;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Exception\InvalidKeyTypeException;
use Joomla\Crypt\Exception\UnsupportedCipherException;
use Joomla\Crypt\Key;
use ParagonIE\Sodium\Compat;
/**
* Cipher for sodium algorithm encryption, decryption and key generation.
*
* @since 1.4.0
*/
class Sodium implements CipherInterface
{
/**
* The message nonce to be used with encryption/decryption
*
* @var string
* @since 1.4.0
*/
private $nonce;
/**
* Method to decrypt a data string.
*
* @param string $data The encrypted string to decrypt.
* @param Key $key The key object to use for decryption.
*
* @return string The decrypted data string.
*
* @since 1.4.0
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'sodium') {
throw new InvalidKeyTypeException('sodium', $key->getType());
}
if (!$this->nonce) {
throw new DecryptionException('Missing nonce to decrypt data');
}
// Use the sodium extension (PHP 7.2 native, PECL 2.x, or paragonie/sodium_compat) if able
if (\function_exists('sodium_crypto_box_open')) {
try {
$decrypted = sodium_crypto_box_open(
$data,
$this->nonce,
sodium_crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic())
);
if ($decrypted === false) {
throw new DecryptionException('Malformed message or invalid MAC');
}
} catch (\SodiumException $exception) {
throw new DecryptionException('Malformed message or invalid MAC', $exception->getCode(), $exception);
}
return $decrypted;
}
// Use the libsodium extension (PECL 1.x) if able; purposefully skipping sodium_compat fallback here as that will match the above check
if (\extension_loaded('libsodium')) {
$decrypted = \Sodium\crypto_box_open(
$data,
$this->nonce,
\Sodium\crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic())
);
if ($decrypted === false) {
throw new DecryptionException('Malformed message or invalid MAC');
}
return $decrypted;
}
// Well this is awkward
throw new UnsupportedCipherException(static::class);
}
/**
* Method to encrypt a data string.
*
* @param string $data The data string to encrypt.
* @param Key $key The key object to use for encryption.
*
* @return string The encrypted data string.
*
* @since 1.4.0
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
{
// Validate key.
if ($key->getType() !== 'sodium') {
throw new InvalidKeyTypeException('sodium', $key->getType());
}
if (!$this->nonce) {
throw new EncryptionException('Missing nonce to decrypt data');
}
// Use the sodium extension (PHP 7.2 native, PECL 2.x, or paragonie/sodium_compat) if able
if (\function_exists('sodium_crypto_box')) {
try {
return sodium_crypto_box(
$data,
$this->nonce,
sodium_crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic())
);
} catch (\SodiumException $exception) {
throw new EncryptionException('Could not encrypt file.', $exception->getCode(), $exception);
}
}
// Use the libsodium extension (PECL 1.x) if able; purposefully skipping sodium_compat fallback here as that will match the above check
if (\extension_loaded('libsodium')) {
return \Sodium\crypto_box(
$data,
$this->nonce,
\Sodium\crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic())
);
}
// Well this is awkward
throw new UnsupportedCipherException(static::class);
}
/**
* Method to generate a new encryption key object.
*
* @param array $options Key generation options.
*
* @return Key
*
* @since 1.4.0
* @throws InvalidKeyException if the key cannot be generated
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function generateKey(array $options = [])
{
// Use the sodium extension (PHP 7.2 native, PECL 2.x, or paragonie/sodium_compat) if able
if (\function_exists('sodium_crypto_box_keypair')) {
try {
// Generate the encryption key.
$pair = sodium_crypto_box_keypair();
return new Key('sodium', sodium_crypto_box_secretkey($pair), sodium_crypto_box_publickey($pair));
} catch (\SodiumException $exception) {
throw new InvalidKeyException('Could not generate encryption key.', $exception->getCode(), $exception);
}
}
// Use the libsodium extension (PECL 1.x) if able; purposefully skipping sodium_compat fallback here as that will match the above check
if (\extension_loaded('libsodium')) {
// Generate the encryption key.
$pair = \Sodium\crypto_box_keypair();
return new Key('sodium', \Sodium\crypto_box_secretkey($pair), \Sodium\crypto_box_publickey($pair));
}
// Well this is awkward
throw new UnsupportedCipherException(static::class);
}
/**
* Check if the cipher is supported in this environment.
*
* @return boolean
*
* @since 2.0.0
*/
public static function isSupported(): bool
{
// Prefer ext/sodium, then ext/libsodium, then presence of paragonie/sodium_compat
return \function_exists('sodium_crypto_box') || \extension_loaded('libsodium') || class_exists(Compat::class);
}
/**
* Set the nonce to use for encrypting/decrypting messages
*
* @param string $nonce The message nonce
*
* @return void
*
* @since 1.4.0
*/
public function setNonce($nonce)
{
$this->nonce = $nonce;
}
}

View File

@ -0,0 +1,76 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt;
use Joomla\Crypt\Exception\DecryptionException;
use Joomla\Crypt\Exception\EncryptionException;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Exception\InvalidKeyTypeException;
use Joomla\Crypt\Exception\UnsupportedCipherException;
/**
* Joomla Framework Cipher interface.
*
* @since 1.0
*/
interface CipherInterface
{
/**
* Method to decrypt a data string.
*
* @param string $data The encrypted string to decrypt.
* @param Key $key The key[/pair] object to use for decryption.
*
* @return string The decrypted data string.
*
* @since 1.0
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function decrypt($data, Key $key);
/**
* Method to encrypt a data string.
*
* @param string $data The data string to encrypt.
* @param Key $key The key[/pair] object to use for encryption.
*
* @return string The encrypted data string.
*
* @since 1.0
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function encrypt($data, Key $key);
/**
* Method to generate a new encryption key[/pair] object.
*
* @param array $options Key generation options.
*
* @return Key
*
* @since 1.0
* @throws InvalidKeyException if the key cannot be generated
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function generateKey(array $options = []);
/**
* Check if the cipher is supported in this environment.
*
* @return boolean
*
* @since 2.0.0
*/
public static function isSupported(): bool;
}

View File

@ -0,0 +1,139 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt;
use Joomla\Crypt\Cipher\Crypto;
use Joomla\Crypt\Exception\DecryptionException;
use Joomla\Crypt\Exception\EncryptionException;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Exception\InvalidKeyTypeException;
use Joomla\Crypt\Exception\UnsupportedCipherException;
/**
* Crypt is a Joomla Framework class for handling basic encryption/decryption of data.
*
* @since 1.0
*/
class Crypt
{
/**
* The encryption cipher object.
*
* @var CipherInterface
* @since 1.0
*/
private $cipher;
/**
* The encryption key[/pair)].
*
* @var Key
* @since 1.0
*/
private $key;
/**
* Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the
* secret word from the configuration object is used.
*
* @param ?CipherInterface $cipher The encryption cipher object.
* @param ?Key $key The encryption key[/pair)].
*
* @since 1.0
*/
public function __construct(?CipherInterface $cipher = null, ?Key $key = null)
{
// Set the encryption cipher.
$this->cipher = $cipher ?: new Crypto();
// Set the encryption key[/pair)].
$this->key = $key ?: $this->generateKey();
}
/**
* Method to decrypt a data string.
*
* @param string $data The encrypted string to decrypt.
*
* @return string The decrypted data string.
*
* @since 1.0
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function decrypt($data)
{
return $this->cipher->decrypt($data, $this->key);
}
/**
* Method to encrypt a data string.
*
* @param string $data The data string to encrypt.
*
* @return string The encrypted data string.
*
* @since 1.0
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function encrypt($data)
{
return $this->cipher->encrypt($data, $this->key);
}
/**
* Method to generate a new encryption key[/pair] object.
*
* @param array $options Key generation options.
*
* @return Key
*
* @since 1.0
* @throws InvalidKeyException if the key cannot be generated
* @throws UnsupportedCipherException if the cipher is not supported on the current environment
*/
public function generateKey(array $options = [])
{
return $this->cipher->generateKey($options);
}
/**
* Method to set the encryption key[/pair] object.
*
* @param Key $key The key object to set.
*
* @return Crypt Instance of $this to allow chaining.
*
* @since 1.0
*/
public function setKey(Key $key)
{
$this->key = $key;
return $this;
}
/**
* Generate random bytes.
*
* @param integer $length Length of the random data to generate
*
* @return string Random binary data
*
* @since 1.0
*/
public static function genRandomBytes($length = 16)
{
return random_bytes($length);
}
}

View File

@ -0,0 +1,19 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Exception;
/**
* Interface defining all crypt package exceptions
*
* @since 2.0.0
*/
interface CryptExceptionInterface extends \Throwable
{
}

View File

@ -0,0 +1,19 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Exception;
/**
* Exception representing an error decrypting data
*
* @since 2.0.0
*/
class DecryptionException extends \RuntimeException implements CryptExceptionInterface
{
}

View File

@ -0,0 +1,19 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Exception;
/**
* Exception representing an error encrypting data
*
* @since 2.0.0
*/
class EncryptionException extends \RuntimeException implements CryptExceptionInterface
{
}

View File

@ -0,0 +1,19 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Exception;
/**
* Exception representing an error generating an encryption key
*
* @since 2.0.0
*/
class InvalidKeyException extends \RuntimeException implements CryptExceptionInterface
{
}

View File

@ -0,0 +1,31 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Exception;
/**
* Exception representing an invalid Joomla\Crypt\Key type for a cipher
*
* @since 1.4.0
*/
class InvalidKeyTypeException extends \InvalidArgumentException implements CryptExceptionInterface
{
/**
* InvalidKeyTypeException constructor.
*
* @param string $expectedKeyType The expected key type.
* @param string $actualKeyType The actual key type.
*
* @since 1.4.0
*/
public function __construct($expectedKeyType, $actualKeyType)
{
parent::__construct("Invalid key of type: $actualKeyType. Expected $expectedKeyType.");
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt\Exception;
/**
* Exception representing an error encrypting data
*
* @since 2.0.0
*/
class UnsupportedCipherException extends \LogicException implements CryptExceptionInterface
{
/**
* UnsupportedCipherException constructor.
*
* @param string $class The class name of the unsupported cipher.
*
* @since 2.0.0
*/
public function __construct(string $class)
{
parent::__construct("The '$class' cipher is not supported in this environment.");
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* Part of the Joomla Framework Crypt Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Crypt;
/**
* Encryption key object for the Joomla Framework.
*
* @since 1.0
*/
class Key
{
/**
* The private key.
*
* @var string
* @since 1.0
*/
private $private;
/**
* The public key.
*
* @var string
* @since 1.0
*/
private $public;
/**
* The key type.
*
* @var string
* @since 1.0
*/
private $type;
/**
* Constructor.
*
* @param string $type The key type.
* @param string $private The private key.
* @param string $public The public key.
*
* @since 1.0
*/
public function __construct(string $type, string $private, string $public)
{
// Set the key type.
$this->type = $type;
// Set the public/private key strings.
$this->private = $private;
$this->public = $public;
}
/**
* Retrieve the private key
*
* @return string
*
* @since 2.0.0
*/
public function getPrivate(): string
{
return $this->private;
}
/**
* Retrieve the public key
*
* @return string
*
* @since 2.0.0
*/
public function getPublic(): string
{
return $this->public;
}
/**
* Retrieve the key type
*
* @return string
*
* @since 2.0.0
*/
public function getType(): string
{
return $this->type;
}
}