Skip to content

Commit

Permalink
Merge pull request #11104 from owncloud/enc_create_backup_on_recovery
Browse files Browse the repository at this point in the history
[encryption] create backup from all keys before recovery
  • Loading branch information
LukasReschke committed Sep 19, 2014
2 parents e94ec40 + a280859 commit fb033cc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/files_encryption/hooks/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ public static function setPassphrase($params) {
|| !$util->userKeysExists()
|| !$view->file_exists($user . '/files')) {

// backup old keys
$util->backupAllKeys('recovery');

$newUserPassword = $params['password'];

// make sure that the users home is mounted
Expand Down
2 changes: 1 addition & 1 deletion apps/files_encryption/lib/proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function isExcludedPath($path, $uid) {
$view = new \OC\Files\View();

// files outside of the files-folder are excluded
if(strpos($path, '/' . $uid . '/files') !== 0) {
if(strpos($path, '/' . $uid . '/files/') !== 0) {
return true;
}

Expand Down
16 changes: 16 additions & 0 deletions apps/files_encryption/lib/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,22 @@ public function recoverUsersFiles($recoveryPassword) {
$this->recoverAllFiles('/', $privateKey);
}

/**
* create a backup of all keys from the user
*
* @param string $purpose (optional) define the purpose of the backup, will be part of the backup folder
*/
public function backupAllKeys($purpose = '') {
$this->userId;
$backupDir = $this->encryptionDir . '/backup.';
$backupDir .= ($purpose === '') ? date("Y-m-d_H-i-s") . '/' : $purpose . '.' . date("Y-m-d_H-i-s") . '/';
$this->view->mkdir($backupDir);
$this->view->copy($this->shareKeysPath, $backupDir . 'share-keys/');
$this->view->copy($this->keyfilesPath, $backupDir . 'keyfiles/');
$this->view->copy($this->privateKeyPath, $backupDir . $this->userId . '.private.key');
$this->view->copy($this->publicKeyPath, $backupDir . $this->userId . '.public.key');
}

/**
* check if the file is stored on a system wide mount point
* @param string $path relative to /data/user with leading '/'
Expand Down
42 changes: 42 additions & 0 deletions apps/files_encryption/tests/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,48 @@ function testDecryptAll() {

}

/**
* test if all keys get moved to the backup folder correctly
*/
function testBackupAllKeys() {
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);

// create some dummy key files
$encPath = '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '/files_encryption';
$this->view->file_put_contents($encPath . '/keyfiles/foo.key', 'key');
$this->view->file_put_contents($encPath . '/share-keys/foo.user1.shareKey', 'share key');

$util = new \OCA\Encryption\Util($this->view, self::TEST_ENCRYPTION_UTIL_USER1);

$util->backupAllKeys('testing');

$encFolderContent = $this->view->getDirectoryContent($encPath);

$backupPath = '';
foreach ($encFolderContent as $c) {
$name = $c['name'];
if (substr($name, 0, strlen('backup')) === 'backup') {
$backupPath = $encPath . '/'. $c['name'];
break;
}
}

$this->assertTrue($backupPath !== '');

// check backupDir Content
$this->assertTrue($this->view->is_dir($backupPath . '/keyfiles'));
$this->assertTrue($this->view->is_dir($backupPath . '/share-keys'));
$this->assertTrue($this->view->file_exists($backupPath . '/keyfiles/foo.key'));
$this->assertTrue($this->view->file_exists($backupPath . '/share-keys/foo.user1.shareKey'));
$this->assertTrue($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.private.key'));
$this->assertTrue($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.public.key'));

//cleanup
$this->view->deleteAll($backupPath);
$this->view->unlink($encPath . '/keyfiles/foo.key', 'key');
$this->view->unlink($encPath . '/share-keys/foo.user1.shareKey', 'share key');
}


function testDescryptAllWithBrokenFiles() {

Expand Down

0 comments on commit fb033cc

Please sign in to comment.