diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-08-13 12:34:21 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-08-13 12:34:21 +0200 |
commit | f282a5cff00d2e7ecbfaa0d93d7ab0bf30921701 (patch) | |
tree | e5d23e0cac3374fac043f04151cc4dabe33920eb /apps/files_encryption/tests | |
parent | fc46fbd1541bda8eb094bd8ee64827fa1cbf1fd0 (diff) | |
parent | ffa6b330477193dd5f438980bd2736555aa738e6 (diff) | |
download | nextcloud-server-f282a5cff00d2e7ecbfaa0d93d7ab0bf30921701.tar.gz nextcloud-server-f282a5cff00d2e7ecbfaa0d93d7ab0bf30921701.zip |
Merge pull request #9754 from owncloud/enc_support_aes_256
[encryption] support aes 256
Diffstat (limited to 'apps/files_encryption/tests')
-rwxr-xr-x | apps/files_encryption/tests/crypt.php | 203 | ||||
-rw-r--r-- | apps/files_encryption/tests/keymanager.php | 36 | ||||
-rwxr-xr-x | apps/files_encryption/tests/share.php | 6 | ||||
-rwxr-xr-x | apps/files_encryption/tests/util.php | 2 |
4 files changed, 164 insertions, 83 deletions
diff --git a/apps/files_encryption/tests/crypt.php b/apps/files_encryption/tests/crypt.php index a1a51c749b0..5eb9580e3b4 100755 --- a/apps/files_encryption/tests/crypt.php +++ b/apps/files_encryption/tests/crypt.php @@ -96,6 +96,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { } $this->assertTrue(\OC_FileProxy::$enabled); + \OCP\Config::deleteSystemValue('cipher'); } public static function tearDownAfterClass() { @@ -121,7 +122,9 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // test successful decrypt $crypted = Encryption\Crypt::symmetricEncryptFileContent($this->genPrivateKey, 'hat'); - $decrypted = Encryption\Crypt::decryptPrivateKey($crypted, 'hat'); + $header = Encryption\Crypt::generateHeader(); + + $decrypted = Encryption\Crypt::decryptPrivateKey($header . $crypted, 'hat'); $this->assertEquals($this->genPrivateKey, $decrypted); @@ -154,12 +157,28 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { /** * @medium */ + function testSymmetricEncryptFileContentAes128() { + + # TODO: search in keyfile for actual content as IV will ensure this test always passes + + $crypted = Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat', 'AES-128-CFB'); + + $this->assertNotEquals($this->dataShort, $crypted); + + + $decrypt = Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat', 'AES-128-CFB'); + + $this->assertEquals($this->dataShort, $decrypt); + + } + + /** + * @medium + */ function testSymmetricStreamEncryptShortFileContent() { $filename = 'tmp-' . uniqid() . '.test'; - $util = new Encryption\Util(new \OC\Files\View(), $this->userId); - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort); // Test that data was successfully written @@ -178,26 +197,52 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // Check that the file was encrypted before being written to disk $this->assertNotEquals($this->dataShort, $retreivedCryptedFile); - // Get the encrypted keyfile - $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename); + // Get file contents with the encryption wrapper + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); + + // Check that decrypted data matches + $this->assertEquals($this->dataShort, $decrypted); + + // Teardown + $this->view->unlink($this->userId . '/files/' . $filename); + + Encryption\Keymanager::deleteFileKey($this->view, $filename); + } + + /** + * @medium + */ + function testSymmetricStreamEncryptShortFileContentAes128() { + + $filename = 'tmp-' . uniqid() . '.test'; + + \OCP\Config::setSystemValue('cipher', 'AES-128-CFB'); + + $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort); - // Attempt to fetch the user's shareKey - $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename); + // Test that data was successfully written + $this->assertTrue(is_int($cryptedFile)); - // get session - $session = new \OCA\Encryption\Session($this->view); + \OCP\Config::deleteSystemValue('cipher'); - // get private key - $privateKey = $session->getPrivateKey($this->userId); + // Disable encryption proxy to prevent recursive calls + $proxyStatus = \OC_FileProxy::$enabled; + \OC_FileProxy::$enabled = false; + + // Get file contents without using any wrapper to get it's actual contents on disk + $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); + + // Re-enable proxy - our work is done + \OC_FileProxy::$enabled = $proxyStatus; - // Decrypt keyfile with shareKey - $plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey); + // Check that the file was encrypted before being written to disk + $this->assertNotEquals($this->dataShort, $retreivedCryptedFile); - // Manually decrypt - $manualDecrypt = Encryption\Crypt::symmetricDecryptFileContent($retreivedCryptedFile, $plainKeyfile); + // Get file contents with the encryption wrapper + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); // Check that decrypted data matches - $this->assertEquals($this->dataShort, $manualDecrypt); + $this->assertEquals($this->dataShort, $decrypted); // Teardown $this->view->unlink($this->userId . '/files/' . $filename); @@ -217,8 +262,6 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // Generate a a random filename $filename = 'tmp-' . uniqid() . '.test'; - $util = new Encryption\Util(new \OC\Files\View(), $this->userId); - // Save long data as encrypted file using stream wrapper $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); @@ -239,50 +282,57 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // Check that the file was encrypted before being written to disk $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile); - // Manuallly split saved file into separate IVs and encrypted chunks - $r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE); + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - //print_r($r); + $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); - // Join IVs and their respective data chunks - $e = array(); - $i = 0; - while ($i < count($r)-1) { - $e[] = $r[$i] . $r[$i+1]; - $i = $i + 2; - } + // Teardown - //print_r($e); + $this->view->unlink($this->userId . '/files/' . $filename); - // Get the encrypted keyfile - $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename); + Encryption\Keymanager::deleteFileKey($this->view, $filename); - // Attempt to fetch the user's shareKey - $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename); + } - // get session - $session = new \OCA\Encryption\Session($this->view); + /** + * @medium + * Test that data that is written by the crypto stream wrapper with AES 128 + * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read + * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual + * reassembly of its data + */ + function testSymmetricStreamEncryptLongFileContentAes128() { - // get private key - $privateKey = $session->getPrivateKey($this->userId); + // Generate a a random filename + $filename = 'tmp-' . uniqid() . '.test'; - // Decrypt keyfile with shareKey - $plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey); + \OCP\Config::setSystemValue('cipher', 'AES-128-CFB'); - // Set var for reassembling decrypted content - $decrypt = ''; + // Save long data as encrypted file using stream wrapper + $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); - // Manually decrypt chunk - foreach ($e as $chunk) { + // Test that data was successfully written + $this->assertTrue(is_int($cryptedFile)); - $chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent($chunk, $plainKeyfile); + // Disable encryption proxy to prevent recursive calls + $proxyStatus = \OC_FileProxy::$enabled; + \OC_FileProxy::$enabled = false; - // Assemble decrypted chunks - $decrypt .= $chunkDecrypt; + \OCP\Config::deleteSystemValue('cipher'); + + // Get file contents without using any wrapper to get it's actual contents on disk + $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); + + // Re-enable proxy - our work is done + \OC_FileProxy::$enabled = $proxyStatus; - } - $this->assertEquals($this->dataLong . $this->dataLong, $decrypt); + // Check that the file was encrypted before being written to disk + $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile); + + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); + + $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); // Teardown @@ -294,14 +344,22 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { /** * @medium - * Test that data that is read by the crypto stream wrapper + * Test that data that is written by the crypto stream wrapper with AES 128 + * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read + * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual + * reassembly of its data */ - function testSymmetricStreamDecryptShortFileContent() { + function testStreamDecryptLongFileContentWithoutHeader() { - $filename = 'tmp-' . uniqid(); + // Generate a a random filename + $filename = 'tmp-' . uniqid() . '.test'; + + \OCP\Config::setSystemValue('cipher', 'AES-128-CFB'); // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///'. $this->userId . '/files/' . $filename, $this->dataShort); + $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); + + \OCP\Config::deleteSystemValue('cipher'); // Test that data was successfully written $this->assertTrue(is_int($cryptedFile)); @@ -310,39 +368,30 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; - $this->assertTrue(Encryption\Crypt::isEncryptedMeta($filename)); - - \OC_FileProxy::$enabled = $proxyStatus; + // Get file contents without using any wrapper to get it's actual contents on disk + $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); + // Check that the file was encrypted before being written to disk + $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile); - $this->assertEquals($this->dataShort, $decrypt); + // remove the header to check if we can also decrypt old files without a header, + // this files should fall back to AES-128 + $cryptedWithoutHeader = substr($retreivedCryptedFile, Encryption\Crypt::BLOCKSIZE); + $this->view->file_put_contents($this->userId . '/files/' . $filename, $cryptedWithoutHeader); - // tear down - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - */ - function testSymmetricStreamDecryptLongFileContent() { + // Re-enable proxy - our work is done + \OC_FileProxy::$enabled = $proxyStatus; - $filename = 'tmp-' . uniqid(); + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong); + $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + // Teardown - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); + $this->view->unlink($this->userId . '/files/' . $filename); - $this->assertEquals($this->dataLong, $decrypt); + Encryption\Keymanager::deleteFileKey($this->view, $filename); - // tear down - $this->view->unlink($this->userId . '/files/' . $filename); } /** @@ -354,7 +403,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { $this->assertFalse(Encryption\Crypt::isCatfileContent($this->legacyEncryptedData)); - $keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat'); + $keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat', 'AES-128-CFB'); $this->assertTrue(Encryption\Crypt::isCatfileContent($keyfileContent)); diff --git a/apps/files_encryption/tests/keymanager.php b/apps/files_encryption/tests/keymanager.php index e779f8341e6..f90832280a2 100644 --- a/apps/files_encryption/tests/keymanager.php +++ b/apps/files_encryption/tests/keymanager.php @@ -107,7 +107,7 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase { $key = Encryption\Keymanager::getPrivateKey($this->view, $this->userId); - $privateKey = Encryption\Crypt::symmetricDecryptFileContent($key, $this->pass); + $privateKey = Encryption\Crypt::decryptPrivateKey($key, $this->pass); $res = openssl_pkey_get_private($privateKey); @@ -177,6 +177,38 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase { /** * @medium */ + function testSetPrivateKey() { + + $key = "dummy key"; + + Encryption\Keymanager::setPrivateKey($key, 'dummyUser'); + + $this->assertTrue($this->view->file_exists('/dummyUser/files_encryption/dummyUser.private.key')); + + //clean up + $this->view->deleteAll('/dummyUser'); + } + + /** + * @medium + */ + function testSetPrivateSystemKey() { + + $key = "dummy key"; + $keyName = "myDummyKey.private.key"; + + Encryption\Keymanager::setPrivateSystemKey($key, $keyName); + + $this->assertTrue($this->view->file_exists('/owncloud_private_key/' . $keyName)); + + // clean up + $this->view->unlink('/owncloud_private_key/' . $keyName); + } + + + /** + * @medium + */ function testGetUserKeys() { $keys = Encryption\Keymanager::getUserKeys($this->view, $this->userId); @@ -189,7 +221,7 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase { $this->assertArrayHasKey('key', $sslInfoPublic); - $privateKey = Encryption\Crypt::symmetricDecryptFileContent($keys['privateKey'], $this->pass); + $privateKey = Encryption\Crypt::decryptPrivateKey($keys['privateKey'], $this->pass); $resPrivate = openssl_pkey_get_private($privateKey); diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php index 7bbea6488bc..1f1304bb527 100755 --- a/apps/files_encryption/tests/share.php +++ b/apps/files_encryption/tests/share.php @@ -540,9 +540,9 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase { . $this->filename . '.' . $publicShareKeyId . '.shareKey')); // some hacking to simulate public link - $GLOBALS['app'] = 'files_sharing'; - $GLOBALS['fileOwner'] = \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1; - \OC_User::setUserId(false); + //$GLOBALS['app'] = 'files_sharing'; + //$GLOBALS['fileOwner'] = \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1; + \Test_Encryption_Util::logoutHelper(); // get file contents $retrievedCryptedFile = file_get_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index ae93e87d013..f337eb46355 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -528,7 +528,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { public static function logoutHelper() { \OC_Util::tearDownFS(); - \OC_User::setUserId(''); + \OC_User::setUserId(false); \OC\Files\Filesystem::tearDown(); } |