Skip to content
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

[MAINTENANCE] Get encryption key using configuration manager instead of global access #1257

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Classes/Common/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Kitodo\Dlf\Common;

use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\Uri;
Expand Down Expand Up @@ -207,7 +208,7 @@ public static function decrypt(string $encrypted)
self::log('OpenSSL library doesn\'t support cipher and/or hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
if (empty(self::getEncryptionKey())) {
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
Expand All @@ -222,7 +223,7 @@ public static function decrypt(string $encrypted)
$binary = base64_decode($encrypted);
$iv = substr($binary, 0, openssl_cipher_iv_length(self::$cipherAlgorithm));
$data = substr($binary, openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], self::$hashAlgorithm, true);
$key = openssl_digest(self::getEncryptionKey(), self::$hashAlgorithm, true);
// Decrypt data.
return openssl_decrypt($data, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
}
Expand Down Expand Up @@ -342,13 +343,13 @@ public static function encrypt(string $string)
self::log('OpenSSL library doesn\'t support cipher and/or hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
if (empty(self::getEncryptionKey())) {
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
// Generate random initialization vector.
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], self::$hashAlgorithm, true);
$key = openssl_digest(self::getEncryptionKey(), self::$hashAlgorithm, true);
// Encrypt data.
$encrypted = openssl_encrypt($string, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
// Merge initialization vector and encrypted data.
Expand Down Expand Up @@ -1018,4 +1019,19 @@ public static function isValidXmlId($id): bool
{
return preg_match('/^[_a-z][_a-z0-9-.]*$/i', $id) === 1;
}

/**
* Get encryption key from configuration.
*
* @access private
*
* @static
*
* @return string|null
*/
private static function getEncryptionKey(): ?string
{
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
return $configurationManager->getLocalConfigurationValueByPath('SYS/encryptionKey');
}
}