SecurityTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * @expectedException \InvalidArgumentException
  96. * @return void
  97. */
  98. public function testRijndaelInvalidOperation()
  99. {
  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. * @expectedException \InvalidArgumentException
  108. * @return void
  109. */
  110. public function testRijndaelInvalidKey()
  111. {
  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. * @expectedException \InvalidArgumentException
  176. * @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
  177. * @return void
  178. */
  179. public function testEncryptInvalidKey()
  180. {
  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. * @expectedException \InvalidArgumentException
  208. * @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
  209. * @return void
  210. */
  211. public function testDecryptInvalidKey()
  212. {
  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. * @expectedException \InvalidArgumentException
  221. * @expectedExceptionMessage The data to decrypt cannot be empty.
  222. * @return void
  223. */
  224. public function testDecryptInvalidData()
  225. {
  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. * @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. $restore = Security::engine();
  239. $txt = "Obi-wan you're our only hope";
  240. $key = 'This is my secret key phrase it is quite long.';
  241. $salt = 'A tasty salt that is delicious';
  242. Security::engine(new Mcrypt());
  243. $cipher = Security::encrypt($txt, $key, $salt);
  244. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  245. Security::engine(new OpenSsl());
  246. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  247. Security::engine(new OpenSsl());
  248. $cipher = Security::encrypt($txt, $key, $salt);
  249. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  250. Security::engine(new Mcrypt());
  251. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  252. }
  253. /**
  254. * Tests that the salt can be set and retrieved
  255. *
  256. * @return void
  257. */
  258. public function testSalt()
  259. {
  260. Security::salt('foobarbaz');
  261. $this->assertEquals('foobarbaz', Security::salt());
  262. }
  263. /**
  264. * Tests that the salt can be set and retrieved
  265. *
  266. * @return void
  267. */
  268. public function testGetSetSalt()
  269. {
  270. Security::setSalt('foobarbaz');
  271. $this->assertEquals('foobarbaz', Security::getSalt());
  272. }
  273. /**
  274. * Test the randomBytes method.
  275. *
  276. * @return void
  277. */
  278. public function testRandomBytes()
  279. {
  280. $value = Security::randomBytes(16);
  281. $this->assertSame(16, strlen($value));
  282. $value = Security::randomBytes(64);
  283. $this->assertSame(64, strlen($value));
  284. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  285. }
  286. /**
  287. * Test the insecureRandomBytes method
  288. *
  289. * @return void
  290. */
  291. public function testInsecureRandomBytes()
  292. {
  293. $value = Security::insecureRandomBytes(16);
  294. $this->assertSame(16, strlen($value));
  295. $value = Security::insecureRandomBytes(64);
  296. $this->assertSame(64, strlen($value));
  297. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  298. }
  299. }