SecurityTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\Crypto\Mcrypt;
  18. use Cake\Utility\Crypto\OpenSsl;
  19. use Cake\Utility\Security;
  20. /**
  21. * SecurityTest class
  22. */
  23. class SecurityTest extends TestCase
  24. {
  25. /**
  26. * testHash method
  27. *
  28. * @return void
  29. */
  30. public function testHash()
  31. {
  32. $_hashType = Security::$hashType;
  33. $key = 'someKey';
  34. $hash = 'someHash';
  35. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  36. $this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
  37. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  38. $this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
  39. $result = Security::hash($key, null, $hash);
  40. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  41. $result = Security::hash($key, 'sha1', $hash);
  42. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  43. $hashType = 'sha1';
  44. Security::setHash($hashType);
  45. $this->assertSame($hashType, Security::$hashType);
  46. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  47. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  48. $this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
  49. $this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
  50. $hashType = 'md5';
  51. Security::setHash($hashType);
  52. $this->assertSame($hashType, Security::$hashType);
  53. $this->assertSame(32, strlen(Security::hash($key, null, false)));
  54. $this->assertSame(32, strlen(Security::hash($key, null, true)));
  55. $this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
  56. $this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
  57. Security::setHash($_hashType);
  58. }
  59. /**
  60. * testInvalidHashTypeException
  61. *
  62. * @expectedException \RuntimeException
  63. * @expectedExceptionMessageRegExp /The hash type `doesnotexist` was not found. Available algorithms are: \w+/
  64. * @return void
  65. */
  66. public function testInvalidHashTypeException()
  67. {
  68. Security::hash('test', 'doesnotexist', false);
  69. }
  70. /**
  71. * testRijndael method
  72. *
  73. * @return void
  74. */
  75. public function testRijndael()
  76. {
  77. $this->skipIf(!function_exists('mcrypt_encrypt') || version_compare(PHP_VERSION, '7.1', '>='));
  78. $engine = Security::engine();
  79. Security::engine(new Mcrypt());
  80. $txt = 'The quick brown fox jumped over the lazy dog.';
  81. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  82. $result = Security::rijndael($txt, $key, 'encrypt');
  83. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  84. $result = Security::rijndael('', $key, 'encrypt');
  85. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  86. $key = 'this is my key of over 32 chars, yes it is';
  87. $result = Security::rijndael($txt, $key, 'encrypt');
  88. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  89. Security::engine($engine);
  90. }
  91. /**
  92. * testRijndaelInvalidOperation method
  93. *
  94. * @return void
  95. */
  96. public function testRijndaelInvalidOperation()
  97. {
  98. $this->expectException(\InvalidArgumentException::class);
  99. $txt = 'The quick brown fox jumped over the lazy dog.';
  100. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  101. Security::rijndael($txt, $key, 'foo');
  102. }
  103. /**
  104. * testRijndaelInvalidKey method
  105. *
  106. * @return void
  107. */
  108. public function testRijndaelInvalidKey()
  109. {
  110. $this->expectException(\InvalidArgumentException::class);
  111. $txt = 'The quick brown fox jumped over the lazy dog.';
  112. $key = 'too small';
  113. Security::rijndael($txt, $key, 'encrypt');
  114. }
  115. /**
  116. * Test encrypt/decrypt.
  117. *
  118. * @return void
  119. */
  120. public function testEncryptDecrypt()
  121. {
  122. $txt = 'The quick brown fox';
  123. $key = 'This key is longer than 32 bytes long.';
  124. $result = Security::encrypt($txt, $key);
  125. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  126. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  127. $this->assertEquals($txt, Security::decrypt($result, $key));
  128. }
  129. /**
  130. * Test that changing the key causes decryption to fail.
  131. *
  132. * @return void
  133. */
  134. public function testDecryptKeyFailure()
  135. {
  136. $txt = 'The quick brown fox';
  137. $key = 'This key is longer than 32 bytes long.';
  138. $result = Security::encrypt($txt, $key);
  139. $key = 'Not the same key. This one will fail';
  140. $this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
  141. }
  142. /**
  143. * Test that decrypt fails when there is an hmac error.
  144. *
  145. * @return void
  146. */
  147. public function testDecryptHmacFailure()
  148. {
  149. $txt = 'The quick brown fox';
  150. $key = 'This key is quite long and works well.';
  151. $salt = 'this is a delicious salt!';
  152. $result = Security::encrypt($txt, $key, $salt);
  153. // Change one of the bytes in the hmac.
  154. $result[10] = 'x';
  155. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  156. }
  157. /**
  158. * Test that changing the hmac salt will cause failures.
  159. *
  160. * @return void
  161. */
  162. public function testDecryptHmacSaltFailure()
  163. {
  164. $txt = 'The quick brown fox';
  165. $key = 'This key is quite long and works well.';
  166. $salt = 'this is a delicious salt!';
  167. $result = Security::encrypt($txt, $key, $salt);
  168. $salt = 'humpty dumpty had a great fall.';
  169. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  170. }
  171. /**
  172. * Test that short keys cause errors
  173. *
  174. * @return void
  175. */
  176. public function testEncryptInvalidKey()
  177. {
  178. $this->expectException(\InvalidArgumentException::class);
  179. $this->expectExceptionMessage('Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.');
  180. $txt = 'The quick brown fox jumped over the lazy dog.';
  181. $key = 'this is too short';
  182. Security::encrypt($txt, $key);
  183. }
  184. /**
  185. * Test encrypting falsey data
  186. *
  187. * @return void
  188. */
  189. public function testEncryptDecryptFalseyData()
  190. {
  191. $key = 'This is a key that is long enough to be ok.';
  192. $result = Security::encrypt('', $key);
  193. $this->assertSame('', Security::decrypt($result, $key));
  194. $result = Security::encrypt(false, $key);
  195. $this->assertSame('', Security::decrypt($result, $key));
  196. $result = Security::encrypt(null, $key);
  197. $this->assertSame('', Security::decrypt($result, $key));
  198. $result = Security::encrypt(0, $key);
  199. $this->assertSame('0', Security::decrypt($result, $key));
  200. $result = Security::encrypt('0', $key);
  201. $this->assertSame('0', Security::decrypt($result, $key));
  202. }
  203. /**
  204. * Test that short keys cause errors
  205. *
  206. * @return void
  207. */
  208. public function testDecryptInvalidKey()
  209. {
  210. $this->expectException(\InvalidArgumentException::class);
  211. $this->expectExceptionMessage('Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.');
  212. $txt = 'The quick brown fox jumped over the lazy dog.';
  213. $key = 'this is too short';
  214. Security::decrypt($txt, $key);
  215. }
  216. /**
  217. * Test that empty data cause errors
  218. *
  219. * @return void
  220. */
  221. public function testDecryptInvalidData()
  222. {
  223. $this->expectException(\InvalidArgumentException::class);
  224. $this->expectExceptionMessage('The data to decrypt cannot be empty.');
  225. $txt = '';
  226. $key = 'This is a key that is long enough to be ok.';
  227. Security::decrypt($txt, $key);
  228. }
  229. /**
  230. * Test that values encrypted with open ssl can be decrypted with mcrypt and the reverse.
  231. *
  232. * @group deprecated
  233. * @return void
  234. */
  235. public function testEngineEquivalence()
  236. {
  237. $this->skipIf(!function_exists('mcrypt_encrypt') || version_compare(PHP_VERSION, '7.1', '>='), 'This needs mcrypt extension to be loaded.');
  238. $this->deprecated(function () {
  239. $restore = Security::engine();
  240. $txt = "Obi-wan you're our only hope";
  241. $key = 'This is my secret key phrase it is quite long.';
  242. $salt = 'A tasty salt that is delicious';
  243. Security::engine(new Mcrypt());
  244. $cipher = Security::encrypt($txt, $key, $salt);
  245. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  246. Security::engine(new OpenSsl());
  247. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  248. Security::engine(new OpenSsl());
  249. $cipher = Security::encrypt($txt, $key, $salt);
  250. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  251. Security::engine(new Mcrypt());
  252. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  253. });
  254. }
  255. /**
  256. * Tests that the salt can be set and retrieved
  257. *
  258. * @return void
  259. */
  260. public function testSalt()
  261. {
  262. Security::setSalt('foobarbaz');
  263. $this->assertEquals('foobarbaz', Security::getSalt());
  264. }
  265. /**
  266. * Tests that the salt can be set and retrieved
  267. *
  268. * @return void
  269. */
  270. public function testGetSetSalt()
  271. {
  272. Security::setSalt('foobarbaz');
  273. $this->assertEquals('foobarbaz', Security::getSalt());
  274. }
  275. /**
  276. * Test the randomBytes method.
  277. *
  278. * @return void
  279. */
  280. public function testRandomBytes()
  281. {
  282. $value = Security::randomBytes(16);
  283. $this->assertSame(16, strlen($value));
  284. $value = Security::randomBytes(64);
  285. $this->assertSame(64, strlen($value));
  286. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  287. }
  288. /**
  289. * Test the randomString method.
  290. *
  291. * @return void
  292. */
  293. public function testRandomString()
  294. {
  295. $value = Security::randomString(7);
  296. $this->assertSame(7, strlen($value));
  297. $value = Security::randomString();
  298. $this->assertSame(64, strlen($value));
  299. $this->assertRegExp('/^[0-9a-f]+$/', $value, 'should return a ASCII string');
  300. }
  301. /**
  302. * Test the insecureRandomBytes method
  303. *
  304. * @return void
  305. */
  306. public function testInsecureRandomBytes()
  307. {
  308. $value = Security::insecureRandomBytes(16);
  309. $this->assertSame(16, strlen($value));
  310. $value = Security::insecureRandomBytes(64);
  311. $this->assertSame(64, strlen($value));
  312. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  313. }
  314. /**
  315. * test constantEquals
  316. *
  317. * @return void
  318. */
  319. public function testConstantEquals()
  320. {
  321. $this->assertFalse(Security::constantEquals('abcde', null));
  322. $this->assertFalse(Security::constantEquals('abcde', false));
  323. $this->assertFalse(Security::constantEquals('abcde', true));
  324. $this->assertFalse(Security::constantEquals('abcde', 1));
  325. $this->assertFalse(Security::constantEquals(null, 'abcde'));
  326. $this->assertFalse(Security::constantEquals(false, 'abcde'));
  327. $this->assertFalse(Security::constantEquals(1, 'abcde'));
  328. $this->assertFalse(Security::constantEquals(true, 'abcde'));
  329. $this->assertTrue(Security::constantEquals('abcde', 'abcde'));
  330. $this->assertFalse(Security::constantEquals('abcdef', 'abcde'));
  331. $this->assertFalse(Security::constantEquals('abcde', 'abcdef'));
  332. $this->assertFalse(Security::constantEquals('a', 'abcdef'));
  333. $snowman = "\xe2\x98\x83";
  334. $this->assertTrue(Security::constantEquals($snowman, $snowman));
  335. $this->assertFalse(Security::constantEquals(str_repeat($snowman, 3), $snowman));
  336. }
  337. }