2015-02-24 21:05:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-04-07 18:02:49 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-02-24 21:05:19 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Tests;
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
use OC\Files\FileInfo;
|
|
|
|
use OC\Files\View;
|
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCA\Encryption\KeyManager;
|
2015-09-07 12:38:44 +03:00
|
|
|
use OCA\Encryption\Session;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCA\Encryption\Util;
|
2016-09-02 11:29:05 +03:00
|
|
|
use OCP\Encryption\Keys\IStorage;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCP\Files\Cache\ICache;
|
|
|
|
use OCP\Files\Storage;
|
2016-09-02 11:29:05 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUserSession;
|
2020-07-27 14:28:44 +03:00
|
|
|
use OCP\Lock\ILockingProvider;
|
|
|
|
use OCP\Lock\LockedException;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2015-02-24 21:05:19 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class KeyManagerTest extends TestCase {
|
|
|
|
/**
|
|
|
|
* @var KeyManager
|
|
|
|
*/
|
|
|
|
private $instance;
|
2015-03-31 00:01:50 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $userId;
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
/** @var string */
|
|
|
|
private $systemKeyId;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\Encryption\Keys\IStorage|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $keyStorageMock;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $cryptMock;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $userMock;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Encryption\Session|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $sessionMock;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $logMock;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Encryption\Util|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $utilMock;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-31 21:30:54 +03:00
|
|
|
private $configMock;
|
|
|
|
|
2020-07-27 14:28:44 +03:00
|
|
|
/** @var ILockingProvider|MockObject */
|
|
|
|
private $lockingProviderMock;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-02-24 21:05:19 +03:00
|
|
|
parent::setUp();
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->userId = 'user1';
|
|
|
|
$this->systemKeyId = 'systemKeyId';
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->keyStorageMock = $this->createMock(IStorage::class);
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->cryptMock = $this->getMockBuilder(Crypt::class)
|
2015-02-24 21:05:19 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->configMock = $this->createMock(IConfig::class);
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->configMock->expects($this->any())
|
|
|
|
->method('getAppValue')
|
|
|
|
->willReturn($this->systemKeyId);
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->userMock = $this->createMock(IUserSession::class);
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->sessionMock = $this->getMockBuilder(Session::class)
|
2015-03-31 21:30:54 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->logMock = $this->createMock(ILogger::class);
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->utilMock = $this->getMockBuilder(Util::class)
|
2015-03-31 00:01:50 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2020-07-27 14:28:44 +03:00
|
|
|
$this->lockingProviderMock = $this->createMock(ILockingProvider::class);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance = new KeyManager(
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock,
|
|
|
|
$this->cryptMock,
|
|
|
|
$this->configMock,
|
|
|
|
$this->userMock,
|
|
|
|
$this->sessionMock,
|
|
|
|
$this->logMock,
|
2020-07-27 14:28:44 +03:00
|
|
|
$this->utilMock,
|
|
|
|
$this->lockingProviderMock
|
|
|
|
);
|
2015-04-01 20:07:54 +03:00
|
|
|
}
|
2015-03-31 00:01:50 +03:00
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
public function testDeleteShareKey() {
|
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('deleteFileKey')
|
|
|
|
->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->deleteShareKey('/path', 'keyId')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getUserKey')
|
|
|
|
->with($this->equalTo($this->userId), $this->equalTo('privateKey'))
|
|
|
|
->willReturn('privateKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertSame('privateKey',
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->getPrivateKey($this->userId)
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPublicKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getUserKey')
|
|
|
|
->with($this->equalTo($this->userId), $this->equalTo('publicKey'))
|
|
|
|
->willReturn('publicKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertSame('publicKey',
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->getPublicKey($this->userId)
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecoveryKeyExists() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getSystemUserKey')
|
|
|
|
->with($this->equalTo($this->systemKeyId . '.publicKey'))
|
|
|
|
->willReturn('recoveryKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->assertTrue($this->instance->recoveryKeyExists());
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckRecoveryKeyPassword() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getSystemUserKey')
|
|
|
|
->with($this->equalTo($this->systemKeyId . '.privateKey'))
|
|
|
|
->willReturn('recoveryKey');
|
|
|
|
$this->cryptMock->expects($this->any())
|
|
|
|
->method('decryptPrivateKey')
|
|
|
|
->with($this->equalTo('recoveryKey'), $this->equalTo('pass'))
|
|
|
|
->willReturn('decryptedRecoveryKey');
|
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->assertTrue($this->instance->checkRecoveryPassword('pass'));
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPublicKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('setUserKey')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->userId),
|
|
|
|
$this->equalTo('publicKey'),
|
|
|
|
$this->equalTo('key'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->setPublicKey($this->userId, 'key')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('setUserKey')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->userId),
|
|
|
|
$this->equalTo('privateKey'),
|
|
|
|
$this->equalTo('key'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->setPrivateKey($this->userId, 'key')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestUserHasKeys
|
|
|
|
*/
|
|
|
|
public function testUserHasKeys($key, $expected) {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(2))
|
|
|
|
->method('getUserKey')
|
|
|
|
->with($this->equalTo($this->userId), $this->anything())
|
2015-07-08 20:08:41 +03:00
|
|
|
->willReturn($key);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
$this->assertSame($expected,
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->userHasKeys($this->userId)
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
public function dataTestUserHasKeys() {
|
|
|
|
return [
|
|
|
|
['key', true],
|
|
|
|
['', false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
public function testUserHasKeysMissingPrivateKey() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class);
|
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(2))
|
|
|
|
->method('getUserKey')
|
|
|
|
->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
|
2020-10-05 16:12:57 +03:00
|
|
|
if ($keyID === 'privateKey') {
|
2015-07-08 20:08:41 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return 'key';
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->instance->userHasKeys($this->userId);
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
public function testUserHasKeysMissingPublicKey() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class);
|
|
|
|
|
2015-07-08 20:08:41 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(2))
|
|
|
|
->method('getUserKey')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
|
2015-07-08 20:08:41 +03:00
|
|
|
if ($keyID === 'publicKey') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return 'key';
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->instance->userHasKeys($this->userId);
|
|
|
|
}
|
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestInit
|
|
|
|
*
|
|
|
|
* @param bool $useMasterKey
|
|
|
|
*/
|
|
|
|
public function testInit($useMasterKey) {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject $instance */
|
2017-10-26 14:46:16 +03:00
|
|
|
$instance = $this->getMockBuilder(KeyManager::class)
|
2015-09-07 12:38:44 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->keyStorageMock,
|
|
|
|
$this->cryptMock,
|
|
|
|
$this->configMock,
|
|
|
|
$this->userMock,
|
|
|
|
$this->sessionMock,
|
|
|
|
$this->logMock,
|
2020-07-27 14:28:44 +03:00
|
|
|
$this->utilMock,
|
|
|
|
$this->lockingProviderMock
|
2015-09-07 12:38:44 +03:00
|
|
|
]
|
|
|
|
)->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey'])
|
|
|
|
->getMock();
|
2015-07-08 20:08:41 +03:00
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
$this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
|
|
|
|
->willReturn($useMasterKey);
|
|
|
|
|
|
|
|
$this->sessionMock->expects($this->at(0))->method('setStatus')
|
|
|
|
->with(Session::INIT_EXECUTED);
|
|
|
|
|
|
|
|
$instance->expects($this->any())->method('getMasterKeyId')->willReturn('masterKeyId');
|
|
|
|
$instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
|
|
|
|
$instance->expects($this->any())->method('getSystemPrivateKey')->with('masterKeyId')->willReturn('privateMasterKey');
|
|
|
|
$instance->expects($this->any())->method('getPrivateKey')->with($this->userId)->willReturn('privateUserKey');
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($useMasterKey) {
|
2015-09-07 12:38:44 +03:00
|
|
|
$this->cryptMock->expects($this->once())->method('decryptPrivateKey')
|
|
|
|
->with('privateMasterKey', 'masterKeyPassword', 'masterKeyId')
|
|
|
|
->willReturn('key');
|
|
|
|
} else {
|
|
|
|
$this->cryptMock->expects($this->once())->method('decryptPrivateKey')
|
|
|
|
->with('privateUserKey', 'pass', $this->userId)
|
|
|
|
->willReturn('key');
|
|
|
|
}
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
$this->sessionMock->expects($this->once())->method('setPrivateKey')
|
|
|
|
->with('key');
|
2015-03-31 21:30:54 +03:00
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
$this->assertTrue($instance->init($this->userId, 'pass'));
|
|
|
|
}
|
2015-03-31 21:30:54 +03:00
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
public function dataTestInit() {
|
|
|
|
return [
|
|
|
|
[true],
|
|
|
|
[false]
|
|
|
|
];
|
2015-03-31 21:30:54 +03:00
|
|
|
}
|
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
public function testSetRecoveryKey() {
|
|
|
|
$this->keyStorageMock->expects($this->exactly(2))
|
|
|
|
->method('setSystemUserKey')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->cryptMock->expects($this->any())
|
2015-08-07 15:04:17 +03:00
|
|
|
->method('encryptPrivateKey')
|
2015-03-31 21:30:54 +03:00
|
|
|
->with($this->equalTo('privateKey'), $this->equalTo('pass'))
|
|
|
|
->willReturn('decryptedPrivateKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-06 22:08:09 +03:00
|
|
|
$this->instance->setRecoveryKey('pass',
|
2020-03-26 11:30:18 +03:00
|
|
|
['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-02 11:26:40 +03:00
|
|
|
public function testSetSystemPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(1))
|
|
|
|
->method('setSystemUserKey')
|
|
|
|
->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->setSystemPrivateKey('keyId', 'key')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-02 11:26:40 +03:00
|
|
|
public function testGetSystemPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(1))
|
2015-04-02 11:26:40 +03:00
|
|
|
->method('getSystemUserKey')
|
2015-03-31 21:30:54 +03:00
|
|
|
->with($this->equalTo('keyId.privateKey'))
|
|
|
|
->willReturn('systemPrivateKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertSame('systemPrivateKey',
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->getSystemPrivateKey('keyId')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
2015-04-06 22:08:09 +03:00
|
|
|
|
|
|
|
public function testGetEncryptedFileKey() {
|
|
|
|
$this->keyStorageMock->expects($this->once())
|
|
|
|
->method('getFileKey')
|
|
|
|
->with('/', 'fileKey')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->getEncryptedFileKey('/'));
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:11:01 +03:00
|
|
|
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, '']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-11 15:02:11 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestGetFileKey
|
|
|
|
*
|
|
|
|
* @param $uid
|
|
|
|
* @param $isMasterKeyEnabled
|
|
|
|
* @param $privateKey
|
|
|
|
* @param $expected
|
|
|
|
*/
|
|
|
|
public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected) {
|
|
|
|
$path = '/foo.txt';
|
|
|
|
|
|
|
|
if ($isMasterKeyEnabled) {
|
|
|
|
$expectedUid = 'masterKeyId';
|
2017-03-02 18:11:01 +03:00
|
|
|
$this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
|
|
|
|
->willReturn('password');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (!$uid) {
|
2017-03-02 18:11:01 +03:00
|
|
|
$expectedUid = 'systemKeyId';
|
2016-01-11 15:02:11 +03:00
|
|
|
} else {
|
|
|
|
$expectedUid = $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->invokePrivate($this->instance, 'masterKeyId', ['masterKeyId']);
|
|
|
|
|
|
|
|
$this->keyStorageMock->expects($this->at(0))
|
2015-04-06 22:08:09 +03:00
|
|
|
->method('getFileKey')
|
2016-01-11 15:02:11 +03:00
|
|
|
->with($path, 'fileKey', 'OC_DEFAULT_MODULE')
|
2015-04-06 22:08:09 +03:00
|
|
|
->willReturn(true);
|
|
|
|
|
2016-01-11 15:02:11 +03:00
|
|
|
$this->keyStorageMock->expects($this->at(1))
|
|
|
|
->method('getFileKey')
|
|
|
|
->with($path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE')
|
2015-04-06 22:08:09 +03:00
|
|
|
->willReturn(true);
|
|
|
|
|
2017-03-02 18:11:01 +03:00
|
|
|
$this->utilMock->expects($this->any())->method('isMasterKeyEnabled')
|
|
|
|
->willReturn($isMasterKeyEnabled);
|
|
|
|
|
2016-01-11 15:02:11 +03:00
|
|
|
if (is_null($uid)) {
|
|
|
|
$this->keyStorageMock->expects($this->once())
|
|
|
|
->method('getSystemUserKey')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('decryptPrivateKey')
|
|
|
|
->willReturn($privateKey);
|
|
|
|
} else {
|
|
|
|
$this->keyStorageMock->expects($this->never())
|
|
|
|
->method('getSystemUserKey');
|
|
|
|
$this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn($privateKey);
|
|
|
|
}
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($privateKey) {
|
2016-01-11 15:02:11 +03:00
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('multiKeyDecrypt')
|
|
|
|
->willReturn(true);
|
|
|
|
} else {
|
|
|
|
$this->cryptMock->expects($this->never())
|
|
|
|
->method('multiKeyDecrypt');
|
|
|
|
}
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2016-01-11 15:02:11 +03:00
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->instance->getFileKey($path, $uid)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-06 22:08:09 +03:00
|
|
|
public function testDeletePrivateKey() {
|
|
|
|
$this->keyStorageMock->expects($this->once())
|
|
|
|
->method('deleteUserKey')
|
|
|
|
->with('user1', 'privateKey')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
$this->assertTrue(self::invokePrivate($this->instance,
|
2015-04-06 22:08:09 +03:00
|
|
|
'deletePrivateKey',
|
|
|
|
[$this->userId]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteAllFileKeys() {
|
|
|
|
$this->keyStorageMock->expects($this->once())
|
|
|
|
->method('deleteAllFileKeys')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->deleteAllFileKeys('/'));
|
|
|
|
}
|
2015-04-29 18:18:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test add public share key and or recovery key to the list of public keys
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestAddSystemKeys
|
|
|
|
*
|
|
|
|
* @param array $accessList
|
|
|
|
* @param array $publicKeys
|
|
|
|
* @param string $uid
|
|
|
|
* @param array $expectedKeys
|
|
|
|
*/
|
|
|
|
public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) {
|
|
|
|
$publicShareKeyId = 'publicShareKey';
|
|
|
|
$recoveryKeyId = 'recoveryKey';
|
|
|
|
|
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getSystemUserKey')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($keyId, $encryptionModuleId) {
|
2015-04-29 18:18:41 +03:00
|
|
|
return $keyId;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->utilMock->expects($this->any())
|
|
|
|
->method('isRecoveryEnabledForUser')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2015-04-29 18:18:41 +03:00
|
|
|
if ($uid === 'user1') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// set key IDs
|
2015-06-03 13:03:02 +03:00
|
|
|
self::invokePrivate($this->instance, 'publicShareKeyId', [$publicShareKeyId]);
|
|
|
|
self::invokePrivate($this->instance, 'recoveryKeyId', [$recoveryKeyId]);
|
2015-04-29 18:18:41 +03:00
|
|
|
|
|
|
|
$result = $this->instance->addSystemKeys($accessList, $publicKeys, $uid);
|
|
|
|
|
|
|
|
foreach ($expectedKeys as $expected) {
|
|
|
|
$this->assertArrayHasKey($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSameSize($expectedKeys, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testAddSystemKeys()
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataTestAddSystemKeys() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']],
|
|
|
|
[['public' => false], [], 'user1', ['recoveryKey']],
|
|
|
|
[['public' => true],[], 'user2', ['publicShareKey']],
|
|
|
|
[['public' => false], [], 'user2', []],
|
|
|
|
];
|
2015-04-29 18:18:41 +03:00
|
|
|
}
|
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
public function testGetMasterKeyId() {
|
|
|
|
$this->assertSame('systemKeyId', $this->instance->getMasterKeyId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPublicMasterKey() {
|
|
|
|
$this->keyStorageMock->expects($this->once())->method('getSystemUserKey')
|
|
|
|
->with('systemKeyId.publicKey', \OCA\Encryption\Crypto\Encryption::ID)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->instance->getPublicMasterKey()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMasterKeyPassword() {
|
|
|
|
$this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
|
|
|
|
->willReturn('password');
|
|
|
|
|
|
|
|
$this->assertSame('password',
|
|
|
|
$this->invokePrivate($this->instance, 'getMasterKeyPassword', [])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
public function testGetMasterKeyPasswordException() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
$this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
|
|
|
|
->willReturn('');
|
|
|
|
|
|
|
|
$this->invokePrivate($this->instance, 'getMasterKeyPassword', []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestValidateMasterKey
|
|
|
|
*
|
|
|
|
* @param $masterKey
|
|
|
|
*/
|
|
|
|
public function testValidateMasterKey($masterKey) {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Encryption\KeyManager | \PHPUnit\Framework\MockObject\MockObject $instance */
|
2017-10-26 14:46:16 +03:00
|
|
|
$instance = $this->getMockBuilder(KeyManager::class)
|
2015-09-07 12:38:44 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->keyStorageMock,
|
|
|
|
$this->cryptMock,
|
|
|
|
$this->configMock,
|
|
|
|
$this->userMock,
|
|
|
|
$this->sessionMock,
|
|
|
|
$this->logMock,
|
2020-07-27 14:28:44 +03:00
|
|
|
$this->utilMock,
|
|
|
|
$this->lockingProviderMock
|
2015-09-07 12:38:44 +03:00
|
|
|
]
|
|
|
|
)->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$instance->expects($this->once())->method('getPublicMasterKey')
|
|
|
|
->willReturn($masterKey);
|
|
|
|
|
|
|
|
$instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
|
|
|
|
$this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (empty($masterKey)) {
|
2015-09-07 12:38:44 +03:00
|
|
|
$this->cryptMock->expects($this->once())->method('createKeyPair')
|
|
|
|
->willReturn(['publicKey' => 'public', 'privateKey' => 'private']);
|
|
|
|
$this->keyStorageMock->expects($this->once())->method('setSystemUserKey')
|
|
|
|
->with('systemKeyId.publicKey', 'public', \OCA\Encryption\Crypto\Encryption::ID);
|
|
|
|
$this->cryptMock->expects($this->once())->method('encryptPrivateKey')
|
|
|
|
->with('private', 'masterKeyPassword', 'systemKeyId')
|
|
|
|
->willReturn('EncryptedKey');
|
2020-07-27 14:28:44 +03:00
|
|
|
$this->lockingProviderMock->expects($this->once())
|
|
|
|
->method('acquireLock');
|
2015-09-07 12:38:44 +03:00
|
|
|
$instance->expects($this->once())->method('setSystemPrivateKey')
|
|
|
|
->with('systemKeyId', 'headerEncryptedKey');
|
|
|
|
} else {
|
|
|
|
$this->cryptMock->expects($this->never())->method('createKeyPair');
|
|
|
|
$this->keyStorageMock->expects($this->never())->method('setSystemUserKey');
|
|
|
|
$this->cryptMock->expects($this->never())->method('encryptPrivateKey');
|
|
|
|
$instance->expects($this->never())->method('setSystemPrivateKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
$instance->validateMasterKey();
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:28:44 +03:00
|
|
|
public function testValidateMasterKeyLocked() {
|
|
|
|
/** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */
|
|
|
|
$instance = $this->getMockBuilder(KeyManager::class)
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->keyStorageMock,
|
|
|
|
$this->cryptMock,
|
|
|
|
$this->configMock,
|
|
|
|
$this->userMock,
|
|
|
|
$this->sessionMock,
|
|
|
|
$this->logMock,
|
|
|
|
$this->utilMock,
|
|
|
|
$this->lockingProviderMock
|
|
|
|
]
|
|
|
|
)->setMethods(['getPublicMasterKey', 'getPrivateMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$instance->expects($this->once())->method('getPublicMasterKey')
|
|
|
|
->willReturn('');
|
|
|
|
$instance->expects($this->once())->method('getPrivateMasterKey')
|
|
|
|
->willReturn('');
|
|
|
|
|
|
|
|
$instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
|
|
|
|
$this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
|
|
|
|
|
|
|
|
$this->lockingProviderMock->expects($this->once())
|
|
|
|
->method('acquireLock')
|
|
|
|
->willThrowException(new LockedException('encryption-generateMasterKey'));
|
|
|
|
|
|
|
|
$this->expectException(LockedException::class);
|
|
|
|
$instance->validateMasterKey();
|
|
|
|
}
|
|
|
|
|
2015-09-07 12:38:44 +03:00
|
|
|
public function dataTestValidateMasterKey() {
|
|
|
|
return [
|
|
|
|
['masterKey'],
|
|
|
|
['']
|
|
|
|
];
|
|
|
|
}
|
2015-04-29 18:18:41 +03:00
|
|
|
|
2016-02-10 00:27:23 +03:00
|
|
|
public function testGetVersionWithoutFileInfo() {
|
2017-10-26 14:46:16 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-02-10 00:27:23 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/admin/files/myfile.txt')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OC\Files\View $view */
|
2016-02-10 00:27:23 +03:00
|
|
|
$this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetVersionWithFileInfo() {
|
2017-10-26 14:46:16 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-02-10 00:27:23 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$fileInfo = $this->getMockBuilder(FileInfo::class)
|
2016-02-10 00:27:23 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$fileInfo->expects($this->once())
|
|
|
|
->method('getEncryptedVersion')
|
|
|
|
->willReturn(1337);
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/admin/files/myfile.txt')
|
|
|
|
->willReturn($fileInfo);
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OC\Files\View $view */
|
2016-02-10 00:27:23 +03:00
|
|
|
$this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view));
|
|
|
|
}
|
|
|
|
|
2016-02-10 00:47:15 +03:00
|
|
|
public function testSetVersionWithFileInfo() {
|
2017-10-26 14:46:16 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-02-10 00:47:15 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$cache = $this->getMockBuilder(ICache::class)
|
2016-02-10 00:47:15 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$cache->expects($this->once())
|
|
|
|
->method('update')
|
|
|
|
->with(123, ['encrypted' => 5, 'encryptedVersion' => 5]);
|
2017-10-26 14:46:16 +03:00
|
|
|
$storage = $this->getMockBuilder(Storage::class)
|
2016-02-10 00:47:15 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('getCache')
|
|
|
|
->willReturn($cache);
|
2017-10-26 14:46:16 +03:00
|
|
|
$fileInfo = $this->getMockBuilder(FileInfo::class)
|
2016-02-10 00:47:15 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$fileInfo->expects($this->once())
|
|
|
|
->method('getStorage')
|
|
|
|
->willReturn($storage);
|
|
|
|
$fileInfo->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(123);
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/admin/files/myfile.txt')
|
|
|
|
->willReturn($fileInfo);
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OC\Files\View $view */
|
2016-02-10 00:47:15 +03:00
|
|
|
$this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetVersionWithoutFileInfo() {
|
2017-10-26 14:46:16 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-02-10 00:47:15 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/admin/files/myfile.txt')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OC\Files\View $view */
|
2016-02-10 00:47:15 +03:00
|
|
|
$this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
|
|
|
|
}
|
|
|
|
|
2017-01-02 23:24:37 +03:00
|
|
|
public function testBackupUserKeys() {
|
|
|
|
$this->keyStorageMock->expects($this->once())->method('backupUserKeys')
|
|
|
|
->with('OC_DEFAULT_MODULE', 'test', 'user1');
|
|
|
|
$this->instance->backupUserKeys('test', 'user1');
|
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|