diff options
-rwxr-xr-x | apps/files_encryption/lib/crypt.php | 7 | ||||
-rw-r--r-- | apps/files_encryption/lib/util.php | 18 | ||||
-rwxr-xr-x | apps/files_encryption/tests/util.php | 99 |
3 files changed, 117 insertions, 7 deletions
diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php index a5278ad3308..5e6ebd7a86e 100755 --- a/apps/files_encryption/lib/crypt.php +++ b/apps/files_encryption/lib/crypt.php @@ -452,8 +452,8 @@ class Crypt { }
/**
- * @brief Encrypts content symmetrically and generated keyfile asymmetrically
- * @returns array keys: data, key
+ * @brief Encrypts content symmetrically and generates keyfile asymmetrically
+ * @returns array keys: encrypted, key
* @note this method is a wrapper for combining other crypt class methods
*/
public static function keyEncryptKeyfile( $plainContent, $publicKey ) {
@@ -469,7 +469,8 @@ class Crypt { }
/**
- * @brief Encrypts content symmetrically and generated keyfile asymmetrically
+ * @brief Takes encrypted data, encrypted catfile, and private key, and
+ * performs decryption
* @returns decrypted content
* @note this method is a wrapper for combining other crypt class methods
*/
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index ea2791650f9..051ac46091a 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -341,10 +341,22 @@ class Util { $bf = $this->getBlowfish( $passphrase ); - $data = $bf->decrypt( $content ); + $decrypted = $bf->decrypt( $content ); - return $data; + $trimmed = rtrim( $decrypted, "\0" ); + return $trimmed; + + } + + public function legacyKeyRecryptKeyfile( $legacyEncryptedContent, $legacyPassphrase, $publicKey, $newPassphrase ) { + + $decrypted = $this->legacyDecrypt( $legacyEncryptedContent, $legacyPassphrase ); + + $recrypted = Crypt::keyEncryptKeyfile( $decrypted, $publicKey ); + + return $recrypted; + } /** @@ -354,7 +366,7 @@ class Util { * * This function decrypts an content */ - public function legacyRecrypt( $legacyContent ) { + public function legacyRecrypt( $legacyContent, $legacyPassphrase, $newPassphrase ) { # TODO: write me diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index 0044844eb84..44e779d1717 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -8,6 +8,7 @@ require_once "PHPUnit/Framework/TestCase.php"; require_once realpath( dirname(__FILE__).'/../../../lib/base.php' ); +require_once realpath( dirname(__FILE__).'/../../../3rdparty/Crypt_Blowfish/Blowfish.php' ); require_once realpath( dirname(__FILE__).'/../../../3rdparty/mockery/Mockery.php' ); require_once realpath( dirname(__FILE__).'/../../../3rdparty/mockery/Mockery/Container.php' ); require_once realpath( dirname(__FILE__).'/../../../3rdparty/mockery/Mockery/Generator.php' ); @@ -29,12 +30,20 @@ class Test_Util extends \PHPUnit_Framework_TestCase { function setUp() { // set content for encrypting / decrypting in tests - $this->data = realpath( dirname(__FILE__).'/../lib/crypt.php' ); + $this->dataUrl = realpath( dirname(__FILE__).'/../lib/crypt.php' ); + $this->dataShort = 'hats'; + $this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) ); $this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' ); $this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' ); $this->userId = 'admin'; $this->pass = 'admin'; + + $keypair = Encryption\Crypt::createKeypair(); + + $this->genPublicKey = $keypair['publicKey']; + $this->genPrivateKey = $keypair['privateKey']; + $this->publicKeyDir = '/' . 'public-keys'; $this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption'; $this->keyfilesPath = $this->encryptionDir . '/' . 'keyfiles'; @@ -42,6 +51,9 @@ class Test_Util extends \PHPUnit_Framework_TestCase { $this->privateKeyPath = $this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key $this->view = new OC_FilesystemView( '/admin' ); + + $this->mockView = m::mock('OC_FilesystemView'); + $this->util = new Encryption\Util( $this->mockView, $this->userId ); } @@ -137,6 +149,91 @@ class Test_Util extends \PHPUnit_Framework_TestCase { } + /** + * @brief test encryption using legacy blowfish method + */ + function testLegacyEncryptShort() { + + $crypted = $this->util->legacyEncrypt( $this->dataShort, $this->pass ); + + $this->assertNotEquals( $this->dataShort, $crypted ); + + # TODO: search inencrypted text for actual content to ensure it + # genuine transformation + + return $crypted; + + } + + /** + * @brief test decryption using legacy blowfish method + * @depends testLegacyEncryptShort + */ + function testLegacyDecryptShort( $crypted ) { + + $decrypted = $this->util->legacyDecrypt( $crypted, $this->pass ); + + $this->assertEquals( $this->dataShort, $decrypted ); + + } + + /** + * @brief test encryption using legacy blowfish method + */ + function testLegacyEncryptLong() { + + $crypted = $this->util->legacyEncrypt( $this->dataLong, $this->pass ); + + $this->assertNotEquals( $this->dataLong, $crypted ); + + # TODO: search inencrypted text for actual content to ensure it + # genuine transformation + + return $crypted; + + } + + /** + * @brief test decryption using legacy blowfish method + * @depends testLegacyEncryptLong + */ + function testLegacyDecryptLong( $crypted ) { + + $decrypted = $this->util->legacyDecrypt( $crypted, $this->pass ); + + $this->assertEquals( $this->dataLong, $decrypted ); + + } + + /** + * @brief test decryption using legacy blowfish method + * @depends testLegacyEncryptLong + */ + function testLegacyKeyRecryptKeyfileEncrypt( $crypted ) { + + $recrypted = $this->util->LegacyKeyRecryptKeyfile( $crypted, $this->pass, $this->genPublicKey, $this->pass ); + + $this->assertNotEquals( $this->dataLong, $recrypted['data'] ); + + return $recrypted; + + # TODO: search inencrypted text for actual content to ensure it + # genuine transformation + + } + +// /** +// * @brief test decryption using legacy blowfish method +// * @depends testLegacyEncryptLong +// */ +// function testLegacyKeyRecryptKeyfileDecrypt( $recrypted ) { +// +// $decrypted = Encryption\Crypt::keyDecryptKeyfile( $recrypted['data'], $recrypted['key'], $this->genPrivateKey ); +// +// $this->assertEquals( $this->dataLong, $decrypted ); +// +// } + // // Cannot use this test for now due to hidden dependencies in OC_FileCache // function testIsLegacyEncryptedContent() { // |