Skip to content

Commit c6d5653

Browse files
authored
Merge pull request #26323 from J0WI/crypt-const
Use constant for supported formats
2 parents 092ff40 + e617361 commit c6d5653

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

apps/encryption/lib/Crypto/Crypt.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,20 @@
5656
* @package OCA\Encryption\Crypto
5757
*/
5858
class Crypt {
59+
public const SUPPORTED_CIPHERS_AND_KEY_SIZE = [
60+
'AES-256-CTR' => 32,
61+
'AES-128-CTR' => 16,
62+
'AES-256-CFB' => 32,
63+
'AES-128-CFB' => 16,
64+
];
65+
// one out of SUPPORTED_CIPHERS_AND_KEY_SIZE
5966
public const DEFAULT_CIPHER = 'AES-256-CTR';
6067
// default cipher from old Nextcloud versions
6168
public const LEGACY_CIPHER = 'AES-128-CFB';
6269

70+
public const SUPPORTED_KEY_FORMATS = ['hash', 'password'];
71+
// one out of SUPPORTED_KEY_FORMATS
72+
public const DEFAULT_KEY_FORMAT = 'hash';
6373
// default key format, old Nextcloud version encrypted the private key directly
6474
// with the user password
6575
public const LEGACY_KEY_FORMAT = 'password';
@@ -76,20 +86,9 @@ class Crypt {
7686
/** @var IConfig */
7787
private $config;
7888

79-
/** @var array */
80-
private $supportedKeyFormats;
81-
8289
/** @var IL10N */
8390
private $l;
8491

85-
/** @var array */
86-
private $supportedCiphersAndKeySize = [
87-
'AES-256-CTR' => 32,
88-
'AES-128-CTR' => 16,
89-
'AES-256-CFB' => 32,
90-
'AES-128-CFB' => 16,
91-
];
92-
9392
/** @var bool */
9493
private $supportLegacy;
9594

@@ -104,8 +103,6 @@ public function __construct(ILogger $logger, IUserSession $userSession, IConfig
104103
$this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : '"no user given"';
105104
$this->config = $config;
106105
$this->l = $l;
107-
$this->supportedKeyFormats = ['hash', 'password'];
108-
109106
$this->supportLegacy = $this->config->getSystemValueBool('encryption.legacy_format_support', false);
110107
}
111108

@@ -206,12 +203,12 @@ public function symmetricEncryptFileContent($plainContent, $passPhrase, $version
206203
/**
207204
* generate header for encrypted file
208205
*
209-
* @param string $keyFormat (can be 'hash' or 'password')
206+
* @param string $keyFormat see SUPPORTED_KEY_FORMATS
210207
* @return string
211208
* @throws \InvalidArgumentException
212209
*/
213-
public function generateHeader($keyFormat = 'hash') {
214-
if (in_array($keyFormat, $this->supportedKeyFormats, true) === false) {
210+
public function generateHeader($keyFormat = self::DEFAULT_KEY_FORMAT) {
211+
if (in_array($keyFormat, self::SUPPORTED_KEY_FORMATS, true) === false) {
215212
throw new \InvalidArgumentException('key format "' . $keyFormat . '" is not supported');
216213
}
217214

@@ -258,14 +255,15 @@ private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::D
258255
*/
259256
public function getCipher() {
260257
$cipher = $this->config->getSystemValue('cipher', self::DEFAULT_CIPHER);
261-
if (!isset($this->supportedCiphersAndKeySize[$cipher])) {
258+
if (!isset(self::SUPPORTED_CIPHERS_AND_KEY_SIZE[$cipher])) {
262259
$this->logger->warning(
263-
sprintf(
264-
'Unsupported cipher (%s) defined in config.php supported. Falling back to %s',
265-
$cipher,
266-
self::DEFAULT_CIPHER
267-
),
268-
['app' => 'encryption']);
260+
sprintf(
261+
'Unsupported cipher (%s) defined in config.php supported. Falling back to %s',
262+
$cipher,
263+
self::DEFAULT_CIPHER
264+
),
265+
['app' => 'encryption']
266+
);
269267
$cipher = self::DEFAULT_CIPHER;
270268
}
271269

@@ -280,8 +278,8 @@ public function getCipher() {
280278
* @throws \InvalidArgumentException
281279
*/
282280
protected function getKeySize($cipher) {
283-
if (isset($this->supportedCiphersAndKeySize[$cipher])) {
284-
return $this->supportedCiphersAndKeySize[$cipher];
281+
if (isset(self::SUPPORTED_CIPHERS_AND_KEY_SIZE[$cipher])) {
282+
return self::SUPPORTED_CIPHERS_AND_KEY_SIZE[$cipher];
285283
}
286284

287285
throw new \InvalidArgumentException(
@@ -403,7 +401,7 @@ public function decryptPrivateKey($privateKey, $password = '', $uid = '') {
403401
$keyFormat = self::LEGACY_KEY_FORMAT;
404402
}
405403

406-
if ($keyFormat === 'hash') {
404+
if ($keyFormat === self::DEFAULT_KEY_FORMAT) {
407405
$password = $this->generatePasswordHash($password, $cipher, $uid);
408406
}
409407

0 commit comments

Comments
 (0)