SecurityTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. * @return void
  64. */
  65. public function testInvalidHashTypeException()
  66. {
  67. try {
  68. Security::hash('test', 'doesnotexist', false);
  69. $this->fail('Expected \RuntimeException');
  70. } catch (RuntimeException $e) {
  71. $this->assertTextContains('The hash type `doesnotexist` was not found. Available algorithms are:', $e->getMessage());
  72. }
  73. }
  74. /**
  75. * testRijndael method
  76. *
  77. * @return void
  78. */
  79. public function testRijndael()
  80. {
  81. $this->skipIf(!function_exists('mcrypt_encrypt') || version_compare(PHP_VERSION, '7.1', '>='));
  82. $engine = Security::engine();
  83. Security::engine(new Mcrypt());
  84. $txt = 'The quick brown fox jumped over the lazy dog.';
  85. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  86. $result = Security::rijndael($txt, $key, 'encrypt');
  87. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  88. $result = Security::rijndael('', $key, 'encrypt');
  89. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  90. $key = 'this is my key of over 32 chars, yes it is';
  91. $result = Security::rijndael($txt, $key, 'encrypt');
  92. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  93. Security::engine($engine);
  94. }
  95. /**
  96. * testRijndaelInvalidOperation method
  97. *
  98. * @expectedException \InvalidArgumentException
  99. * @return void
  100. */
  101. public function testRijndaelInvalidOperation()
  102. {
  103. $txt = 'The quick brown fox jumped over the lazy dog.';
  104. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  105. Security::rijndael($txt, $key, 'foo');
  106. }
  107. /**
  108. * testRijndaelInvalidKey method
  109. *
  110. * @expectedException \InvalidArgumentException
  111. * @return void
  112. */
  113. public function testRijndaelInvalidKey()
  114. {
  115. $txt = 'The quick brown fox jumped over the lazy dog.';
  116. $key = 'too small';
  117. Security::rijndael($txt, $key, 'encrypt');
  118. }
  119. /**
  120. * Test encrypt/decrypt.
  121. *
  122. * @return void
  123. */
  124. public function testEncryptDecrypt()
  125. {
  126. $txt = 'The quick brown fox';
  127. $key = 'This key is longer than 32 bytes long.';
  128. $result = Security::encrypt($txt, $key);
  129. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  130. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  131. $this->assertEquals($txt, Security::decrypt($result, $key));
  132. }
  133. /**
  134. * Test that changing the key causes decryption to fail.
  135. *
  136. * @return void
  137. */
  138. public function testDecryptKeyFailure()
  139. {
  140. $txt = 'The quick brown fox';
  141. $key = 'This key is longer than 32 bytes long.';
  142. $result = Security::encrypt($txt, $key);
  143. $key = 'Not the same key. This one will fail';
  144. $this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
  145. }
  146. /**
  147. * Test that decrypt fails when there is an hmac error.
  148. *
  149. * @return void
  150. */
  151. public function testDecryptHmacFailure()
  152. {
  153. $txt = 'The quick brown fox';
  154. $key = 'This key is quite long and works well.';
  155. $salt = 'this is a delicious salt!';
  156. $result = Security::encrypt($txt, $key, $salt);
  157. // Change one of the bytes in the hmac.
  158. $result[10] = 'x';
  159. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  160. }
  161. /**
  162. * Test that changing the hmac salt will cause failures.
  163. *
  164. * @return void
  165. */
  166. public function testDecryptHmacSaltFailure()
  167. {
  168. $txt = 'The quick brown fox';
  169. $key = 'This key is quite long and works well.';
  170. $salt = 'this is a delicious salt!';
  171. $result = Security::encrypt($txt, $key, $salt);
  172. $salt = 'humpty dumpty had a great fall.';
  173. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  174. }
  175. /**
  176. * Test that short keys cause errors
  177. *
  178. * @expectedException \InvalidArgumentException
  179. * @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
  180. * @return void
  181. */
  182. public function testEncryptInvalidKey()
  183. {
  184. $txt = 'The quick brown fox jumped over the lazy dog.';
  185. $key = 'this is too short';
  186. Security::encrypt($txt, $key);
  187. }
  188. /**
  189. * Test encrypting falsey data
  190. *
  191. * @return void
  192. */
  193. public function testEncryptDecryptFalseyData()
  194. {
  195. $key = 'This is a key that is long enough to be ok.';
  196. $result = Security::encrypt('', $key);
  197. $this->assertSame('', Security::decrypt($result, $key));
  198. $result = Security::encrypt(false, $key);
  199. $this->assertSame('', Security::decrypt($result, $key));
  200. $result = Security::encrypt(null, $key);
  201. $this->assertSame('', Security::decrypt($result, $key));
  202. $result = Security::encrypt(0, $key);
  203. $this->assertSame('0', Security::decrypt($result, $key));
  204. $result = Security::encrypt('0', $key);
  205. $this->assertSame('0', Security::decrypt($result, $key));
  206. }
  207. /**
  208. * Test that short keys cause errors
  209. *
  210. * @expectedException \InvalidArgumentException
  211. * @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
  212. * @return void
  213. */
  214. public function testDecryptInvalidKey()
  215. {
  216. $txt = 'The quick brown fox jumped over the lazy dog.';
  217. $key = 'this is too short';
  218. Security::decrypt($txt, $key);
  219. }
  220. /**
  221. * Test that empty data cause errors
  222. *
  223. * @expectedException \InvalidArgumentException
  224. * @expectedExceptionMessage The data to decrypt cannot be empty.
  225. * @return void
  226. */
  227. public function testDecryptInvalidData()
  228. {
  229. $txt = '';
  230. $key = 'This is a key that is long enough to be ok.';
  231. Security::decrypt($txt, $key);
  232. }
  233. /**
  234. * Test that values encrypted with open ssl can be decrypted with mcrypt and the reverse.
  235. *
  236. * @return void
  237. */
  238. public function testEngineEquivalence()
  239. {
  240. $this->skipIf(!function_exists('mcrypt_encrypt') || version_compare(PHP_VERSION, '7.1', '>='), 'This needs mcrypt extension to be loaded.');
  241. $restore = Security::engine();
  242. $txt = "Obi-wan you're our only hope";
  243. $key = 'This is my secret key phrase it is quite long.';
  244. $salt = 'A tasty salt that is delicious';
  245. Security::engine(new Mcrypt());
  246. $cipher = Security::encrypt($txt, $key, $salt);
  247. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  248. Security::engine(new OpenSsl());
  249. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  250. Security::engine(new OpenSsl());
  251. $cipher = Security::encrypt($txt, $key, $salt);
  252. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  253. Security::engine(new Mcrypt());
  254. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  255. }
  256. /**
  257. * Tests that the salt can be set and retrieved
  258. *
  259. * @return void
  260. */
  261. public function testSalt()
  262. {
  263. Security::salt('foobarbaz');
  264. $this->assertEquals('foobarbaz', Security::salt());
  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 insecureRandomBytes method
  291. *
  292. * @return void
  293. */
  294. public function testInsecureRandomBytes()
  295. {
  296. $value = Security::insecureRandomBytes(16);
  297. $this->assertSame(16, strlen($value));
  298. $value = Security::insecureRandomBytes(64);
  299. $this->assertSame(64, strlen($value));
  300. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  301. }
  302. }