SecurityTest.php 12 KB

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