Skip to content

Commit

Permalink
Use master key for public links as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Petry authored and schiessle committed Mar 24, 2017
1 parent 3057183 commit 6b9ef15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
19 changes: 15 additions & 4 deletions apps/encryption/lib/KeyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,28 @@ public function getPrivateKey($userId) {
* @return string
*/
public function getFileKey($path, $uid) {
if ($uid === '') {
$uid = null;
}
$publicAccess = is_null($uid);
$encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);

if (empty($encryptedFileKey)) {
return '';
}

if (!is_null($uid) && $this->util->isMasterKeyEnabled()) {
if ($this->util->isMasterKeyEnabled()) {
$uid = $this->getMasterKeyId();
}

if (is_null($uid)) {
$shareKey = $this->getShareKey($path, $uid);
if ($publicAccess) {
$privateKey = $this->getSystemPrivateKey($uid);
$privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid);
} else {
// when logged in, the master key is already decrypted in the session
$privateKey = $this->session->getPrivateKey();
}
} else if ($publicAccess) {
// use public share key for public links
$uid = $this->getPublicShareKeyId();
$shareKey = $this->getShareKey($path, $uid);
$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
Expand Down
39 changes: 20 additions & 19 deletions apps/encryption/tests/KeyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ public function testGetEncryptedFileKey() {
$this->assertTrue($this->instance->getEncryptedFileKey('/'));
}

public function dataTestGetFileKey() {
return [
['user1', false, 'privateKey', true],
['user1', false, false, ''],
['user1', true, 'privateKey', true],
['user1', true, false, ''],
[null, false, 'privateKey', true],
[null, false, false, ''],
[null, true, 'privateKey', true],
[null, true, false, '']
];
}

/**
* @dataProvider dataTestGetFileKey
*
Expand All @@ -363,6 +376,10 @@ public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected

if ($isMasterKeyEnabled) {
$expectedUid = 'masterKeyId';
$this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
->willReturn('password');
} else if (!$uid) {
$expectedUid = 'systemKeyId';
} else {
$expectedUid = $uid;
}
Expand All @@ -379,6 +396,9 @@ public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected
->with($path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE')
->willReturn(true);

$this->utilMock->expects($this->any())->method('isMasterKeyEnabled')
->willReturn($isMasterKeyEnabled);

if (is_null($uid)) {
$this->keyStorageMock->expects($this->once())
->method('getSystemUserKey')
Expand All @@ -389,8 +409,6 @@ public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected
} else {
$this->keyStorageMock->expects($this->never())
->method('getSystemUserKey');
$this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
->willReturn($isMasterKeyEnabled);
$this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn($privateKey);
}

Expand All @@ -409,23 +427,6 @@ public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected

}

public function dataTestGetFileKey() {
return [
['user1', false, 'privateKey', true],
['user1', false, false, ''],
['user1', true, 'privateKey', true],
['user1', true, false, ''],
['', false, 'privateKey', true],
['', false, false, ''],
['', true, 'privateKey', true],
['', true, false, ''],
[null, false, 'privateKey', true],
[null, false, false, ''],
[null, true, 'privateKey', true],
[null, true, false, '']
];
}

public function testDeletePrivateKey() {
$this->keyStorageMock->expects($this->once())
->method('deleteUserKey')
Expand Down

0 comments on commit 6b9ef15

Please sign in to comment.