2015-04-09 15:59:03 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
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>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
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-09 15:59:03 +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-04-09 15:59:03 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
namespace OCA\Encryption\Tests\Crypto;
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IConfig;
|
2016-09-02 11:29:05 +03:00
|
|
|
use OCP\IL10N;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\ILogger;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCP\IUserSession;
|
2015-04-09 15:59:03 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
class CryptTest extends TestCase {
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2015-04-09 15:59:03 +03:00
|
|
|
private $logger;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2015-04-09 15:59:03 +03:00
|
|
|
private $userSession;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2015-04-09 15:59:03 +03:00
|
|
|
private $config;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2016-01-05 14:51:05 +03:00
|
|
|
private $l;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var Crypt */
|
2015-04-09 15:59:03 +03:00
|
|
|
private $crypt;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-04-09 15:59:03 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->logger = $this->getMockBuilder(ILogger::class)
|
2015-04-09 15:59:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->logger->expects($this->any())
|
|
|
|
->method('warning')
|
|
|
|
->willReturn(true);
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->userSession = $this->getMockBuilder(IUserSession::class)
|
2015-04-09 15:59:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)
|
2015-04-09 15:59:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->l = $this->createMock(IL10N::class);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
$this->crypt = new Crypt($this->logger, $this->userSession, $this->config, $this->l);
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test getOpenSSLConfig without any additional parameters
|
|
|
|
*/
|
|
|
|
public function testGetOpenSSLConfigBasic() {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with($this->equalTo('openssl'), $this->equalTo([]))
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn([]);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'getOpenSSLConfig');
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertArrayHasKey('private_key_bits', $result);
|
|
|
|
$this->assertSame(4096, $result['private_key_bits']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test getOpenSSLConfig with additional parameters defined in config.php
|
|
|
|
*/
|
|
|
|
public function testGetOpenSSLConfig() {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with($this->equalTo('openssl'), $this->equalTo([]))
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['foo' => 'bar', 'private_key_bits' => 1028]);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'getOpenSSLConfig');
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->assertSame(2, count($result));
|
|
|
|
$this->assertArrayHasKey('private_key_bits', $result);
|
|
|
|
$this->assertArrayHasKey('foo', $result);
|
|
|
|
$this->assertSame(1028, $result['private_key_bits']);
|
|
|
|
$this->assertSame('bar', $result['foo']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-08-07 15:04:17 +03:00
|
|
|
* test generateHeader with valid key formats
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestGenerateHeader
|
2015-04-09 15:59:03 +03:00
|
|
|
*/
|
2015-08-07 15:04:17 +03:00
|
|
|
public function testGenerateHeader($keyFormat, $expected) {
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2016-01-04 23:00:55 +03:00
|
|
|
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
|
2015-04-09 15:59:03 +03:00
|
|
|
->willReturn('AES-128-CFB');
|
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
if ($keyFormat) {
|
|
|
|
$result = $this->crypt->generateHeader($keyFormat);
|
|
|
|
} else {
|
|
|
|
$result = $this->crypt->generateHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test generateHeader with invalid key format
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testGenerateHeaderInvalid() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
$this->crypt->generateHeader('unknown');
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-08-07 15:04:17 +03:00
|
|
|
public function dataTestGenerateHeader() {
|
|
|
|
return [
|
|
|
|
[null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:HEND'],
|
|
|
|
['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:HEND'],
|
|
|
|
['hash', 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:HEND']
|
|
|
|
];
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
public function testGetCipherWithInvalidCipher() {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
|
|
|
|
->willReturn('Not-Existing-Cipher');
|
|
|
|
$this->logger
|
|
|
|
->expects($this->once())
|
|
|
|
->method('warning')
|
|
|
|
->with('Unsupported cipher (Not-Existing-Cipher) defined in config.php supported. Falling back to AES-256-CTR');
|
|
|
|
|
|
|
|
$this->assertSame('AES-256-CTR', $this->crypt->getCipher());
|
|
|
|
}
|
|
|
|
|
2015-04-09 15:59:03 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderGetCipher
|
|
|
|
* @param string $configValue
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testGetCipher($configValue, $expected) {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
2016-01-04 23:00:55 +03:00
|
|
|
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
|
2015-04-09 15:59:03 +03:00
|
|
|
->willReturn($configValue);
|
|
|
|
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->crypt->getCipher()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testGetCipher
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataProviderGetCipher() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['AES-128-CFB', 'AES-128-CFB'],
|
|
|
|
['AES-256-CFB', 'AES-256-CFB'],
|
|
|
|
['AES-128-CTR', 'AES-128-CTR'],
|
|
|
|
['AES-256-CTR', 'AES-256-CTR'],
|
2016-01-04 23:00:55 +03:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
['unknown', 'AES-256-CTR']
|
|
|
|
];
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test concatIV()
|
|
|
|
*/
|
|
|
|
public function testConcatIV() {
|
2015-06-03 13:03:02 +03:00
|
|
|
$result = self::invokePrivate(
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->crypt,
|
|
|
|
'concatIV',
|
2020-03-26 11:30:18 +03:00
|
|
|
['content', 'my_iv']);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
$this->assertSame('content00iv00my_iv',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-05 21:01:03 +03:00
|
|
|
* @dataProvider dataTestSplitMetaData
|
2015-04-09 15:59:03 +03:00
|
|
|
*/
|
2016-01-05 21:01:03 +03:00
|
|
|
public function testSplitMetaData($data, $expected) {
|
2020-08-12 14:22:24 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->with('encryption_skip_signature_check', false)
|
|
|
|
->willReturn(true);
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'splitMetaData', [$data, 'AES-256-CFB']);
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->assertTrue(is_array($result));
|
2016-01-05 21:01:03 +03:00
|
|
|
$this->assertSame(3, count($result));
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->assertArrayHasKey('encrypted', $result);
|
|
|
|
$this->assertArrayHasKey('iv', $result);
|
2016-01-05 21:01:03 +03:00
|
|
|
$this->assertArrayHasKey('signature', $result);
|
|
|
|
$this->assertSame($expected['encrypted'], $result['encrypted']);
|
|
|
|
$this->assertSame($expected['iv'], $result['iv']);
|
|
|
|
$this->assertSame($expected['signature'], $result['signature']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestSplitMetaData() {
|
|
|
|
return [
|
|
|
|
['encryptedContent00iv001234567890123456xx',
|
|
|
|
['encrypted' => 'encryptedContent', 'iv' => '1234567890123456', 'signature' => false]],
|
|
|
|
['encryptedContent00iv00123456789012345600sig00e1992521e437f6915f9173b190a512cfc38a00ac24502db44e0ba10c2bb0cc86xxx',
|
|
|
|
['encrypted' => 'encryptedContent', 'iv' => '1234567890123456', 'signature' => 'e1992521e437f6915f9173b190a512cfc38a00ac24502db44e0ba10c2bb0cc86']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestHasSignature
|
|
|
|
*/
|
|
|
|
public function testHasSignature($data, $expected) {
|
2020-08-12 14:22:24 +03:00
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->with('encryption_skip_signature_check', false)
|
|
|
|
->willReturn(true);
|
2016-01-05 21:01:03 +03:00
|
|
|
$this->assertSame($expected,
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->invokePrivate($this->crypt, 'hasSignature', [$data, 'AES-256-CFB'])
|
2016-01-05 21:01:03 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestHasSignature() {
|
|
|
|
return [
|
|
|
|
['encryptedContent00iv001234567890123456xx', false],
|
|
|
|
['encryptedContent00iv00123456789012345600sig00e1992521e437f6915f9173b190a512cfc38a00ac24502db44e0ba10c2bb0cc86xxx', true]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestHasSignatureFail
|
|
|
|
*/
|
|
|
|
public function testHasSignatureFail($cipher) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Encryption\Exceptions\GenericEncryptionException::class);
|
|
|
|
|
2016-01-05 21:01:03 +03:00
|
|
|
$data = 'encryptedContent00iv001234567890123456xx';
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->invokePrivate($this->crypt, 'hasSignature', [$data, $cipher]);
|
2016-01-05 21:01:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestHasSignatureFail() {
|
|
|
|
return [
|
|
|
|
['AES-256-CTR'],
|
|
|
|
['aes-256-ctr'],
|
|
|
|
['AES-128-CTR'],
|
|
|
|
['ctr-256-ctr']
|
|
|
|
];
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test addPadding()
|
|
|
|
*/
|
|
|
|
public function testAddPadding() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'addPadding', ['data']);
|
2016-01-05 21:01:03 +03:00
|
|
|
$this->assertSame('dataxxx', $result);
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test removePadding()
|
|
|
|
*
|
|
|
|
* @dataProvider dataProviderRemovePadding
|
|
|
|
* @param $data
|
|
|
|
* @param $expected
|
|
|
|
*/
|
|
|
|
public function testRemovePadding($data, $expected) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'removePadding', [$data]);
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testRemovePadding
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function dataProviderRemovePadding() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['dataxx', 'data'],
|
|
|
|
['data', false]
|
|
|
|
];
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test parseHeader()
|
|
|
|
*/
|
|
|
|
public function testParseHeader() {
|
2020-10-05 16:12:57 +03:00
|
|
|
$header = 'HBEGIN:foo:bar:cipher:AES-256-CFB:HEND';
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'parseHeader', [$header]);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($result));
|
|
|
|
$this->assertSame(2, count($result));
|
|
|
|
$this->assertArrayHasKey('foo', $result);
|
|
|
|
$this->assertArrayHasKey('cipher', $result);
|
|
|
|
$this->assertSame('bar', $result['foo']);
|
|
|
|
$this->assertSame('AES-256-CFB', $result['cipher']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test encrypt()
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function testEncrypt() {
|
|
|
|
$decrypted = 'content';
|
|
|
|
$password = 'password';
|
2015-06-03 13:03:02 +03:00
|
|
|
$iv = self::invokePrivate($this->crypt, 'generateIv');
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
$this->assertTrue(is_string($iv));
|
|
|
|
$this->assertSame(16, strlen($iv));
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = self::invokePrivate($this->crypt, 'encrypt', [$decrypted, $iv, $password]);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
$this->assertTrue(is_string($result));
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
2015-04-09 15:59:03 +03:00
|
|
|
'password' => $password,
|
|
|
|
'iv' => $iv,
|
|
|
|
'encrypted' => $result,
|
2020-03-26 11:30:18 +03:00
|
|
|
'decrypted' => $decrypted];
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test decrypt()
|
|
|
|
*
|
|
|
|
* @depends testEncrypt
|
|
|
|
*/
|
|
|
|
public function testDecrypt($data) {
|
2015-06-03 13:03:02 +03:00
|
|
|
$result = self::invokePrivate(
|
2015-04-09 15:59:03 +03:00
|
|
|
$this->crypt,
|
|
|
|
'decrypt',
|
2020-03-26 11:30:18 +03:00
|
|
|
[$data['encrypted'], $data['iv'], $data['password']]);
|
2015-04-09 15:59:03 +03:00
|
|
|
|
|
|
|
$this->assertSame($data['decrypted'], $result);
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
/**
|
|
|
|
* test return values of valid ciphers
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestGetKeySize
|
|
|
|
*/
|
|
|
|
public function testGetKeySize($cipher, $expected) {
|
|
|
|
$result = $this->invokePrivate($this->crypt, 'getKeySize', [$cipher]);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test exception if cipher is unknown
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testGetKeySizeFailure() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
$this->invokePrivate($this->crypt, 'getKeySize', ['foo']);
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-08-07 15:04:17 +03:00
|
|
|
public function dataTestGetKeySize() {
|
|
|
|
return [
|
|
|
|
['AES-256-CFB', 32],
|
|
|
|
['AES-128-CFB', 16],
|
2016-01-04 23:00:55 +03:00
|
|
|
['AES-256-CTR', 32],
|
|
|
|
['AES-128-CTR', 16],
|
2015-08-07 15:04:17 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestDecryptPrivateKey
|
|
|
|
*/
|
|
|
|
public function testDecryptPrivateKey($header, $privateKey, $expectedCipher, $isValidKey, $expected) {
|
2020-08-12 14:22:24 +03:00
|
|
|
$this->config->method('getSystemValueBool')
|
|
|
|
->with('encryption.legacy_format_support', false)
|
|
|
|
->willReturn(true);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\Encryption\Crypto\Crypt | \PHPUnit\Framework\MockObject\MockObject $crypt */
|
2017-10-26 14:46:16 +03:00
|
|
|
$crypt = $this->getMockBuilder(Crypt::class)
|
2015-08-07 15:04:17 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->logger,
|
|
|
|
$this->userSession,
|
2016-01-05 21:01:03 +03:00
|
|
|
$this->config,
|
|
|
|
$this->l
|
2015-08-07 15:04:17 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(
|
|
|
|
[
|
|
|
|
'parseHeader',
|
|
|
|
'generatePasswordHash',
|
|
|
|
'symmetricDecryptFileContent',
|
|
|
|
'isValidPrivateKey'
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$crypt->expects($this->once())->method('parseHeader')->willReturn($header);
|
|
|
|
if (isset($header['keyFormat']) && $header['keyFormat'] === 'hash') {
|
|
|
|
$crypt->expects($this->once())->method('generatePasswordHash')->willReturn('hash');
|
|
|
|
$password = 'hash';
|
|
|
|
} else {
|
|
|
|
$crypt->expects($this->never())->method('generatePasswordHash');
|
|
|
|
$password = 'password';
|
|
|
|
}
|
|
|
|
|
|
|
|
$crypt->expects($this->once())->method('symmetricDecryptFileContent')
|
|
|
|
->with('privateKey', $password, $expectedCipher)->willReturn('key');
|
|
|
|
$crypt->expects($this->once())->method('isValidPrivateKey')->willReturn($isValidKey);
|
|
|
|
|
|
|
|
$result = $crypt->decryptPrivateKey($privateKey, 'password');
|
|
|
|
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-08-07 15:04:17 +03:00
|
|
|
public function dataTestDecryptPrivateKey() {
|
|
|
|
return [
|
|
|
|
[['cipher' => 'AES-128-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-128-CFB', true, 'key'],
|
|
|
|
[['cipher' => 'AES-256-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-256-CFB', true, 'key'],
|
|
|
|
[['cipher' => 'AES-256-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-256-CFB', false, false],
|
|
|
|
[['cipher' => 'AES-256-CFB', 'keyFormat' => 'hash'], 'HBEGIN:HENDprivateKey', 'AES-256-CFB', true, 'key'],
|
|
|
|
[['cipher' => 'AES-256-CFB'], 'HBEGIN:HENDprivateKey', 'AES-256-CFB', true, 'key'],
|
|
|
|
[[], 'privateKey', 'AES-128-CFB', true, 'key'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-08-25 16:58:07 +03:00
|
|
|
public function testIsValidPrivateKey() {
|
|
|
|
$res = openssl_pkey_new();
|
|
|
|
openssl_pkey_export($res, $privateKey);
|
|
|
|
|
|
|
|
// valid private key
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->invokePrivate($this->crypt, 'isValidPrivateKey', [$privateKey])
|
|
|
|
);
|
|
|
|
|
|
|
|
// invalid private key
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->invokePrivate($this->crypt, 'isValidPrivateKey', ['foo'])
|
|
|
|
);
|
|
|
|
}
|
2015-04-09 15:59:03 +03:00
|
|
|
}
|