From 2b7cf212bb2e179294a4190745dbe003d971bc46 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 9 Feb 2021 16:42:21 +0100 Subject: [PATCH 1/2] Move to AES-256-GCM for openssl_seal/open Signed-off-by: Roeland Jago Douma --- apps/encryption/lib/Crypto/Crypt.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/encryption/lib/Crypto/Crypt.php b/apps/encryption/lib/Crypto/Crypt.php index f8ba3d69b80f8..dbbd8df1dc8ec 100644 --- a/apps/encryption/lib/Crypto/Crypt.php +++ b/apps/encryption/lib/Crypto/Crypt.php @@ -707,11 +707,25 @@ public function multiKeyDecrypt($encKeyFile, $shareKey, $privateKey) { throw new MultiKeyDecryptException('Cannot multikey decrypt empty plain content'); } + $prev = null; + + // We need to be able to extract the IV + if (strlen($encKeyFile) > 12) { + $iv = substr($encKeyFile, -12); + $encrypted = substr($encKeyFile, 0, -12); + + if (openssl_open($encrypted, $plainContent, $shareKey, $privateKey, 'aes-256-gcm', $iv)) { + return $plainContent; + } + + $prev = new MultiKeyDecryptException('multikeydecrypt with share key failed (aes-256-gcm):' . openssl_error_string()); + } + if (openssl_open($encKeyFile, $plainContent, $shareKey, $privateKey, 'RC4')) { return $plainContent; - } else { - throw new MultiKeyDecryptException('multikeydecrypt with share key failed:' . openssl_error_string()); } + + throw new MultiKeyDecryptException('multikeydecrypt with share key failed (rc4):' . openssl_error_string(), '', 0, $prev); } /** @@ -732,7 +746,8 @@ public function multiKeyEncrypt($plainContent, array $keyFiles) { $shareKeys = []; $mappedShareKeys = []; - if (openssl_seal($plainContent, $sealed, $shareKeys, $keyFiles, 'RC4')) { + $iv = \random_bytes(12); + if (openssl_seal($plainContent, $sealed, $shareKeys, $keyFiles, 'aes-256-gcm', $iv)) { $i = 0; // Ensure each shareKey is labelled with its corresponding key id @@ -742,12 +757,11 @@ public function multiKeyEncrypt($plainContent, array $keyFiles) { } return [ - 'keys' => $mappedShareKeys, + 'keys' => $mappedShareKeys . $iv, 'data' => $sealed ]; - } else { - throw new MultiKeyEncryptException('multikeyencryption failed ' . openssl_error_string()); } + throw new MultiKeyEncryptException('multikeyencryption failed ' . openssl_error_string()); } public function useLegacyBase64Encoding(): bool { From 905e6d8d8d5f474490922e062c2f014a31d05ed0 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 9 Feb 2021 19:33:05 +0100 Subject: [PATCH 2/2] fixup! Move to AES-256-GCM for openssl_seal/open Signed-off-by: Roeland Jago Douma --- apps/encryption/lib/Crypto/Crypt.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/encryption/lib/Crypto/Crypt.php b/apps/encryption/lib/Crypto/Crypt.php index dbbd8df1dc8ec..16d4c24f22866 100644 --- a/apps/encryption/lib/Crypto/Crypt.php +++ b/apps/encryption/lib/Crypto/Crypt.php @@ -757,8 +757,8 @@ public function multiKeyEncrypt($plainContent, array $keyFiles) { } return [ - 'keys' => $mappedShareKeys . $iv, - 'data' => $sealed + 'keys' => $mappedShareKeys, + 'data' => $sealed . $iv, ]; } throw new MultiKeyEncryptException('multikeyencryption failed ' . openssl_error_string());