57
57
* @package OCA\Encryption\Crypto
58
58
*/
59
59
class Crypt {
60
+ public const SUPPORTED_CIPHERS_AND_KEY_SIZE = [
61
+ 'AES-256-CTR ' => 32 ,
62
+ 'AES-128-CTR ' => 16 ,
63
+ 'AES-256-CFB ' => 32 ,
64
+ 'AES-128-CFB ' => 16 ,
65
+ ];
66
+ // one out of SUPPORTED_CIPHERS_AND_KEY_SIZE
60
67
public const DEFAULT_CIPHER = 'AES-256-CTR ' ;
61
68
// default cipher from old Nextcloud versions
62
69
public const LEGACY_CIPHER = 'AES-128-CFB ' ;
63
70
71
+ public const SUPPORTED_KEY_FORMATS = ['hash ' , 'password ' ];
72
+ // one out of SUPPORTED_KEY_FORMATS
73
+ public const DEFAULT_KEY_FORMAT = 'hash ' ;
64
74
// default key format, old Nextcloud version encrypted the private key directly
65
75
// with the user password
66
76
public const LEGACY_KEY_FORMAT = 'password ' ;
@@ -77,20 +87,9 @@ class Crypt {
77
87
/** @var IConfig */
78
88
private $ config ;
79
89
80
- /** @var array */
81
- private $ supportedKeyFormats ;
82
-
83
90
/** @var IL10N */
84
91
private $ l ;
85
92
86
- /** @var array */
87
- private $ supportedCiphersAndKeySize = [
88
- 'AES-256-CTR ' => 32 ,
89
- 'AES-128-CTR ' => 16 ,
90
- 'AES-256-CFB ' => 32 ,
91
- 'AES-128-CFB ' => 16 ,
92
- ];
93
-
94
93
/** @var bool */
95
94
private $ supportLegacy ;
96
95
@@ -105,8 +104,6 @@ public function __construct(ILogger $logger, IUserSession $userSession, IConfig
105
104
$ this ->user = $ userSession && $ userSession ->isLoggedIn () ? $ userSession ->getUser ()->getUID () : '"no user given" ' ;
106
105
$ this ->config = $ config ;
107
106
$ this ->l = $ l ;
108
- $ this ->supportedKeyFormats = ['hash ' , 'password ' ];
109
-
110
107
$ this ->supportLegacy = $ this ->config ->getSystemValueBool ('encryption.legacy_format_support ' , false );
111
108
}
112
109
@@ -207,12 +204,12 @@ public function symmetricEncryptFileContent($plainContent, $passPhrase, $version
207
204
/**
208
205
* generate header for encrypted file
209
206
*
210
- * @param string $keyFormat (can be 'hash' or 'password')
207
+ * @param string $keyFormat see SUPPORTED_KEY_FORMATS
211
208
* @return string
212
209
* @throws \InvalidArgumentException
213
210
*/
214
- public function generateHeader ($ keyFormat = ' hash ' ) {
215
- if (in_array ($ keyFormat , $ this -> supportedKeyFormats , true ) === false ) {
211
+ public function generateHeader ($ keyFormat = self :: DEFAULT_KEY_FORMAT ) {
212
+ if (in_array ($ keyFormat , self :: SUPPORTED_KEY_FORMATS , true ) === false ) {
216
213
throw new \InvalidArgumentException ('key format " ' . $ keyFormat . '" is not supported ' );
217
214
}
218
215
@@ -259,14 +256,15 @@ private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::D
259
256
*/
260
257
public function getCipher () {
261
258
$ cipher = $ this ->config ->getSystemValue ('cipher ' , self ::DEFAULT_CIPHER );
262
- if (!isset ($ this -> supportedCiphersAndKeySize [$ cipher ])) {
259
+ if (!isset (self :: SUPPORTED_CIPHERS_AND_KEY_SIZE [$ cipher ])) {
263
260
$ this ->logger ->warning (
264
- sprintf (
265
- 'Unsupported cipher (%s) defined in config.php supported. Falling back to %s ' ,
266
- $ cipher ,
267
- self ::DEFAULT_CIPHER
268
- ),
269
- ['app ' => 'encryption ' ]);
261
+ sprintf (
262
+ 'Unsupported cipher (%s) defined in config.php supported. Falling back to %s ' ,
263
+ $ cipher ,
264
+ self ::DEFAULT_CIPHER
265
+ ),
266
+ ['app ' => 'encryption ' ]
267
+ );
270
268
$ cipher = self ::DEFAULT_CIPHER ;
271
269
}
272
270
@@ -288,8 +286,8 @@ public function getCipher() {
288
286
* @throws \InvalidArgumentException
289
287
*/
290
288
protected function getKeySize ($ cipher ) {
291
- if (isset ($ this -> supportedCiphersAndKeySize [$ cipher ])) {
292
- return $ this -> supportedCiphersAndKeySize [$ cipher ];
289
+ if (isset (self :: SUPPORTED_CIPHERS_AND_KEY_SIZE [$ cipher ])) {
290
+ return self :: SUPPORTED_CIPHERS_AND_KEY_SIZE [$ cipher ];
293
291
}
294
292
295
293
throw new \InvalidArgumentException (
@@ -411,7 +409,7 @@ public function decryptPrivateKey($privateKey, $password = '', $uid = '') {
411
409
$ keyFormat = self ::LEGACY_KEY_FORMAT ;
412
410
}
413
411
414
- if ($ keyFormat === ' hash ' ) {
412
+ if ($ keyFormat === self :: DEFAULT_KEY_FORMAT ) {
415
413
$ password = $ this ->generatePasswordHash ($ password , $ cipher , $ uid );
416
414
}
417
415
0 commit comments