SecurityTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.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. * testRijndael method
  61. *
  62. * @return void
  63. */
  64. public function testRijndael()
  65. {
  66. $this->skipIf(!function_exists('mcrypt_encrypt') || version_compare(PHP_VERSION, '7.1', '>='));
  67. $engine = Security::engine();
  68. Security::engine(new Mcrypt());
  69. $txt = 'The quick brown fox jumped over the lazy dog.';
  70. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  71. $result = Security::rijndael($txt, $key, 'encrypt');
  72. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  73. $result = Security::rijndael('', $key, 'encrypt');
  74. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  75. $key = 'this is my key of over 32 chars, yes it is';
  76. $result = Security::rijndael($txt, $key, 'encrypt');
  77. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  78. Security::engine($engine);
  79. }
  80. /**
  81. * testRijndaelInvalidOperation method
  82. *
  83. * @expectedException \InvalidArgumentException
  84. * @return void
  85. */
  86. public function testRijndaelInvalidOperation()
  87. {
  88. $txt = 'The quick brown fox jumped over the lazy dog.';
  89. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  90. Security::rijndael($txt, $key, 'foo');
  91. }
  92. /**
  93. * testRijndaelInvalidKey method
  94. *
  95. * @expectedException \InvalidArgumentException
  96. * @return void
  97. */
  98. public function testRijndaelInvalidKey()
  99. {
  100. $txt = 'The quick brown fox jumped over the lazy dog.';
  101. $key = 'too small';
  102. Security::rijndael($txt, $key, 'encrypt');
  103. }
  104. /**
  105. * Test encrypt/decrypt.
  106. *
  107. * @return void
  108. */
  109. public function testEncryptDecrypt()
  110. {
  111. $txt = 'The quick brown fox';
  112. $key = 'This key is longer than 32 bytes long.';
  113. $result = Security::encrypt($txt, $key);
  114. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  115. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  116. $this->assertEquals($txt, Security::decrypt($result, $key));
  117. }
  118. /**
  119. * Test that changing the key causes decryption to fail.
  120. *
  121. * @return void
  122. */
  123. public function testDecryptKeyFailure()
  124. {
  125. $txt = 'The quick brown fox';
  126. $key = 'This key is longer than 32 bytes long.';
  127. $result = Security::encrypt($txt, $key);
  128. $key = 'Not the same key. This one will fail';
  129. $this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
  130. }
  131. /**
  132. * Test that decrypt fails when there is an hmac error.
  133. *
  134. * @return void
  135. */
  136. public function testDecryptHmacFailure()
  137. {
  138. $txt = 'The quick brown fox';
  139. $key = 'This key is quite long and works well.';
  140. $salt = 'this is a delicious salt!';
  141. $result = Security::encrypt($txt, $key, $salt);
  142. // Change one of the bytes in the hmac.
  143. $result[10] = 'x';
  144. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  145. }
  146. /**
  147. * Test that changing the hmac salt will cause failures.
  148. *
  149. * @return void
  150. */
  151. public function testDecryptHmacSaltFailure()
  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. $salt = 'humpty dumpty had a great fall.';
  158. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  159. }
  160. /**
  161. * Test that short keys cause errors
  162. *
  163. * @expectedException \InvalidArgumentException
  164. * @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
  165. * @return void
  166. */
  167. public function testEncryptInvalidKey()
  168. {
  169. $txt = 'The quick brown fox jumped over the lazy dog.';
  170. $key = 'this is too short';
  171. Security::encrypt($txt, $key);
  172. }
  173. /**
  174. * Test encrypting falsey data
  175. *
  176. * @return void
  177. */
  178. public function testEncryptDecryptFalseyData()
  179. {
  180. $key = 'This is a key that is long enough to be ok.';
  181. $result = Security::encrypt('', $key);
  182. $this->assertSame('', Security::decrypt($result, $key));
  183. $result = Security::encrypt(false, $key);
  184. $this->assertSame('', Security::decrypt($result, $key));
  185. $result = Security::encrypt(null, $key);
  186. $this->assertSame('', Security::decrypt($result, $key));
  187. $result = Security::encrypt(0, $key);
  188. $this->assertSame('0', Security::decrypt($result, $key));
  189. $result = Security::encrypt('0', $key);
  190. $this->assertSame('0', Security::decrypt($result, $key));
  191. }
  192. /**
  193. * Test that short keys cause errors
  194. *
  195. * @expectedException \InvalidArgumentException
  196. * @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
  197. * @return void
  198. */
  199. public function testDecryptInvalidKey()
  200. {
  201. $txt = 'The quick brown fox jumped over the lazy dog.';
  202. $key = 'this is too short';
  203. Security::decrypt($txt, $key);
  204. }
  205. /**
  206. * Test that empty data cause errors
  207. *
  208. * @expectedException \InvalidArgumentException
  209. * @expectedExceptionMessage The data to decrypt cannot be empty.
  210. * @return void
  211. */
  212. public function testDecryptInvalidData()
  213. {
  214. $txt = '';
  215. $key = 'This is a key that is long enough to be ok.';
  216. Security::decrypt($txt, $key);
  217. }
  218. /**
  219. * Test that values encrypted with open ssl can be decrypted with mcrypt and the reverse.
  220. *
  221. * @return void
  222. */
  223. public function testEngineEquivalence()
  224. {
  225. $this->skipIf(!function_exists('mcrypt_encrypt') || version_compare(PHP_VERSION, '7.1', '>='), 'This needs mcrypt extension to be loaded.');
  226. $restore = Security::engine();
  227. $txt = "Obi-wan you're our only hope";
  228. $key = 'This is my secret key phrase it is quite long.';
  229. $salt = 'A tasty salt that is delicious';
  230. Security::engine(new Mcrypt());
  231. $cipher = Security::encrypt($txt, $key, $salt);
  232. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  233. Security::engine(new OpenSsl());
  234. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  235. Security::engine(new OpenSsl());
  236. $cipher = Security::encrypt($txt, $key, $salt);
  237. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  238. Security::engine(new Mcrypt());
  239. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  240. }
  241. /**
  242. * Tests that the salt can be set and retrieved
  243. *
  244. * @return void
  245. */
  246. public function testSalt()
  247. {
  248. Security::salt('foobarbaz');
  249. $this->assertEquals('foobarbaz', Security::salt());
  250. }
  251. /**
  252. * Tests that the salt can be set and retrieved
  253. *
  254. * @return void
  255. */
  256. public function testGetSetSalt()
  257. {
  258. Security::setSalt('foobarbaz');
  259. $this->assertEquals('foobarbaz', Security::getSalt());
  260. }
  261. /**
  262. * Test the randomBytes method.
  263. *
  264. * @return void
  265. */
  266. public function testRandomBytes()
  267. {
  268. $value = Security::randomBytes(16);
  269. $this->assertSame(16, strlen($value));
  270. $value = Security::randomBytes(64);
  271. $this->assertSame(64, strlen($value));
  272. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  273. }
  274. /**
  275. * Test the insecureRandomBytes method
  276. *
  277. * @return void
  278. */
  279. public function testInsecureRandomBytes()
  280. {
  281. $value = Security::insecureRandomBytes(16);
  282. $this->assertSame(16, strlen($value));
  283. $value = Security::insecureRandomBytes(64);
  284. $this->assertSame(64, strlen($value));
  285. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  286. }
  287. }