2014-08-26 21:02:40 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:02:58 +03:00
|
|
|
namespace Test\Security;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Security\Crypto;
|
2014-08-26 21:02:40 +04:00
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
class CryptoTest extends \Test\TestCase {
|
2020-04-10 15:19:56 +03:00
|
|
|
public function defaultEncryptionProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'],
|
|
|
|
[''],
|
|
|
|
['我看这本书。 我看這本書']
|
|
|
|
];
|
2014-09-03 15:51:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Crypto */
|
|
|
|
protected $crypto;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
2020-05-11 11:31:46 +03:00
|
|
|
$this->crypto = new Crypto(\OC::$server->getConfig());
|
2014-09-03 15:51:44 +04:00
|
|
|
}
|
2014-08-26 21:02:40 +04:00
|
|
|
|
2014-09-03 15:51:44 +04:00
|
|
|
/**
|
|
|
|
* @dataProvider defaultEncryptionProvider
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testDefaultEncrypt($stringToEncrypt) {
|
2014-09-03 15:51:44 +04:00
|
|
|
$ciphertext = $this->crypto->encrypt($stringToEncrypt);
|
|
|
|
$this->assertEquals($stringToEncrypt, $this->crypto->decrypt($ciphertext));
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
|
|
|
|
2020-05-11 11:31:46 +03:00
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testWrongPassword() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('HMAC does not match.');
|
|
|
|
|
2014-08-26 21:02:40 +04:00
|
|
|
$stringToEncrypt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.';
|
2014-09-03 15:51:44 +04:00
|
|
|
$ciphertext = $this->crypto->encrypt($stringToEncrypt);
|
|
|
|
$this->crypto->decrypt($ciphertext, 'A wrong password!');
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testLaterDecryption() {
|
2014-08-26 21:02:40 +04:00
|
|
|
$stringToEncrypt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.';
|
2014-09-03 13:03:27 +04:00
|
|
|
$encryptedString = '44a35023cca2e7a6125e06c29fc4b2ad9d8a33d0873a8b45b0de4ef9284f260c6c46bf25dc62120644c59b8bafe4281ddc47a70c35ae6c29ef7a63d79eefacc297e60b13042ac582733598d0a6b4de37311556bb5c480fd2633de4e6ebafa868c2d1e2d80a5d24f9660360dba4d6e0c8|lhrFgK0zd9U160Wo|a75e57ab701f9124e1113543fd1dc596f21e20d456a0d1e813d5a8aaec9adcb11213788e96598b67fe9486a9f0b99642c18296d0175db44b1ae426e4e91080ee';
|
2014-09-03 15:51:44 +04:00
|
|
|
$this->assertEquals($stringToEncrypt, $this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd'));
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
|
|
|
|
2020-05-11 11:31:46 +03:00
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testWrongIV() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('HMAC does not match.');
|
|
|
|
|
2014-08-26 21:02:40 +04:00
|
|
|
$encryptedString = '560f5436ba864b9f12f7f7ca6d41c327554a6f2c0a160a03316b202af07c65163274993f3a46e7547c07ba89304f00594a2f3bd99f83859097c58049c39d0d4ade10e0de914ff0604961e7c849d0271ed6c0b23f984ba16e7d033e3305fb0910e7b6a2a65c988d17dbee71d8f953684d|d2kdFUspVjC0o0sr|1a5feacf87eaa6869a6abdfba9a296e7bbad45b6ad89f7dce67cdc98e2da5dc4379cc672cc655e52bbf19599bf59482fbea13a73937697fa656bf10f3fc4f1aa';
|
2014-09-03 15:51:44 +04:00
|
|
|
$this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
|
|
|
|
2020-05-11 11:31:46 +03:00
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testWrongParameters() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Authenticated ciphertext could not be decoded.');
|
|
|
|
|
2014-08-26 21:02:40 +04:00
|
|
|
$encryptedString = '1|2';
|
2014-09-03 15:51:44 +04:00
|
|
|
$this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|
2020-05-11 11:31:46 +03:00
|
|
|
|
|
|
|
public function testLegacy() {
|
|
|
|
$cipherText = 'e16599188e3d212f5c7f17fdc2abca46|M1WfLAxbcAmITeD6|509457885d6ca5e6c3bfd3741852687a7f2bffce197f8d5ae97b65818b15a1b7f616b68326ff312371540f4ca8ac55f8e2de4aa13aab3474bd3431e51214e3ee';
|
|
|
|
$password = 'mypass';
|
|
|
|
|
|
|
|
$this->assertSame('legacy test', $this->crypto->decrypt($cipherText, $password));
|
|
|
|
}
|
2014-08-26 21:02:40 +04:00
|
|
|
}
|