PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.3 KiB

4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
8 years ago
4 years ago
8 years ago
8 years ago
4 years ago
8 years ago
4 years ago
4 years ago
8 years ago
4 years ago
8 years ago
8 years ago
4 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. class PaCryptTest extends \PHPUnit\Framework\TestCase {
  3. public function testMd5Crypt() {
  4. $hash = _pacrypt_md5crypt('test', '');
  5. $this->assertNotEmpty($hash);
  6. $this->assertNotEquals('test', $hash);
  7. $this->assertEquals($hash, _pacrypt_md5crypt('test', $hash));
  8. }
  9. public function testCrypt() {
  10. // E_NOTICE if we pass in '' for the salt
  11. $hash = _pacrypt_crypt('test', 'sa');
  12. $this->assertNotEmpty($hash);
  13. $this->assertNotEquals('test', $hash);
  14. $this->assertEquals($hash, _pacrypt_crypt('test', $hash));
  15. }
  16. public function testMySQLEncrypt() {
  17. if (!db_mysql()) {
  18. $this->markTestSkipped('Not using MySQL');
  19. }
  20. $hash = _pacrypt_mysql_encrypt('test1');
  21. $hash2 = _pacrypt_mysql_encrypt('test2');
  22. $this->assertNotEquals($hash, $hash2);
  23. $this->assertNotEmpty($hash);
  24. $this->assertNotEquals('test', $hash);
  25. $this->assertNotEquals('test', $hash2);
  26. $this->assertTrue( hash_equals($hash, _pacrypt_mysql_encrypt('test1', $hash) ), "hashes should equal....");
  27. }
  28. public function testAuthlib() {
  29. global $CONF;
  30. // too many options!
  31. foreach (
  32. [
  33. 'md5raw' => '098f6bcd4621d373cade4e832627b4f6',
  34. 'md5' => 'CY9rzUYh03PK3k6DJie09g==',
  35. // crypt requires salt ...
  36. 'SHA' => 'qUqP5cyxm6YcTAhz05Hph5gvu9M='
  37. ] as $flavour => $hash
  38. ) {
  39. $CONF['authlib_default_flavour'] = $flavour;
  40. $stored = "{" . $flavour . "}$hash";
  41. $hash = _pacrypt_authlib('test', $stored);
  42. $this->assertEquals($hash, $stored, "Hash: $hash vs Stored: $stored");
  43. //var_dump("Hash: $hash from $flavour");
  44. }
  45. }
  46. public function testPacryptDovecot() {
  47. global $CONF;
  48. if (!file_exists('/usr/bin/doveadm')) {
  49. $this->markTestSkipped("No /usr/bin/doveadm");
  50. }
  51. $CONF['encrypt'] = 'dovecot:SHA1';
  52. $expected_hash = '{SHA1}qUqP5cyxm6YcTAhz05Hph5gvu9M=';
  53. $this->assertEquals($expected_hash, _pacrypt_dovecot('test', ''));
  54. $this->assertEquals($expected_hash, _pacrypt_dovecot('test', $expected_hash));
  55. // This should also work.
  56. $sha512 = '{SHA512}ClAmHr0aOQ/tK/Mm8mc8FFWCpjQtUjIElz0CGTN/gWFqgGmwElh89WNfaSXxtWw2AjDBmyc1AO4BPgMGAb8kJQ=='; // foobar
  57. $this->assertEquals($sha512, _pacrypt_dovecot('foobar', $sha512));
  58. $sha512 = '{SHA512}ClAmHr0aOQ/tK/Mm8mc8FFWCpjQtUjIElz0CGTN/gWFqgGmwElh89WNfaSXxtWw2AjDBmyc1AO4BPgMGAb8kJQ=='; // foobar
  59. $this->assertNotEquals($sha512, _pacrypt_dovecot('foobarbaz', $sha512));
  60. }
  61. public function testPhpCrypt() {
  62. $config = Config::getInstance();
  63. Config::write('encrypt', 'php_crypt');
  64. $CONF = Config::getInstance()->getAll();
  65. $sha512_crypt = '$6$ijF8bgunALqnEHTo$LHVa6XQBpM5Gt16RMFQuXqrGAS0y0ymaLS8pnkeVUTSx3t2DrGqWwRj6q4ef3V3SWYkb5xkuN9bv7joxNd8kA1';
  66. $enc = _pacrypt_php_crypt('foo', $sha512_crypt);
  67. $this->assertEquals($enc, $sha512_crypt);
  68. $fail = _pacrypt_php_crypt('bar', $sha512_crypt);
  69. $this->assertNotEquals($fail, $sha512_crypt);
  70. }
  71. public function testPhpCryptMd5() {
  72. $config = Config::getInstance();
  73. Config::write('encrypt', 'php_crypt:MD5');
  74. $CONF = Config::getInstance()->getAll();
  75. $expected = '$1$z2DG4z9d$jBu3Cl3BPQZrkNqnflnSO.';
  76. $enc = _pacrypt_php_crypt('foo', $expected);
  77. $this->assertEquals($enc, $expected);
  78. $fail = _pacrypt_php_crypt('bar', $expected);
  79. }
  80. public function testPhpCryptHandlesPrefixAndOrRounds() {
  81. // try with 1000 rounds
  82. Config::write('encrypt', 'php_crypt:SHA256:1000');
  83. $password = 'hello';
  84. $randomHash = '$5$VhqhhsXJtPFeBX9e$kz3/CMIEu80bKdtDAcISIrDfdwtc.ilR68Vb3hNhu/7';
  85. $randomHashWithPrefix = '{SHA256-CRYPT}' . $randomHash;
  86. $new = _pacrypt_php_crypt($password, '');
  87. $this->assertNotEquals($randomHash, $new); // salts should be different.
  88. $enc = _pacrypt_php_crypt($password, $randomHash);
  89. $this->assertEquals($enc, $randomHash);
  90. $this->assertEquals($randomHash, _pacrypt_php_crypt("hello", $randomHash));
  91. $this->assertEquals($randomHash, _pacrypt_crypt("hello", $randomHash));
  92. Config::write('encrypt', 'php_crypt:SHA256::{SHA256-CRYPT}');
  93. $enc = _pacrypt_php_crypt("hello", $randomHash);
  94. $this->assertEquals($randomHash, $enc); // we passed in something lacking the prefix, so we shouldn't have added it in.
  95. $this->assertTrue(hash_equals($randomHash, $enc));
  96. // should cope with this :
  97. $enc = _pacrypt_php_crypt($password, '');
  98. $this->assertEquals($enc, _pacrypt_php_crypt($password, $enc));
  99. $this->assertRegExp('/^\{SHA256-CRYPT\}/', $enc);
  100. $this->assertGreaterThan(20, strlen($enc));
  101. }
  102. public function testPhpCryptRandomString() {
  103. $str1 = _php_crypt_random_string('abcdefg123456789', 2);
  104. $str2 = _php_crypt_random_string('abcdefg123456789', 2);
  105. $str3 = _php_crypt_random_string('abcdefg123456789', 2);
  106. $this->assertNotEmpty($str1);
  107. $this->assertNotEmpty($str2);
  108. $this->assertNotEmpty($str3);
  109. // it should be difficult for us to get three salts of the same value back...
  110. // not impossible though.
  111. $this->assertFalse(strcmp($str1, $str2) == 0 && strcmp($str1, $str3) == 0);
  112. }
  113. public function testSha512B64() {
  114. $str1 = _pacrypt_sha512_b64('test', '');
  115. $str2 = _pacrypt_sha512_b64('test', '');
  116. $this->assertNotEmpty($str1);
  117. $this->assertNotEmpty($str2);
  118. $this->assertNotEquals($str1, $str2); // should have different salts
  119. $actualHash = '{SHA512-CRYPT.B64}JDYkM2NWcFM1WFNlUHl5MzdwSiRZWW80d0FmeWg5MXpxcS4uY3dtYUR1Y1RodTJGTDY1NHpXNUNvRU0wT3hXVFFzZkxIZ1JJSTZmT281OVpDUWJOTTF2L0JXajloME0vVjJNbENNMUdwLg==';
  120. $check = _pacrypt_sha512_b64('test', $actualHash);
  121. $this->assertTrue(hash_equals($check, $actualHash));
  122. $str3 = _pacrypt_sha512_b64('foo', '');
  123. $this->assertNotEmpty($str3);
  124. $this->assertFalse(hash_equals('test', $str3));
  125. $this->assertTrue(hash_equals(_pacrypt_sha512_b64('foo', $str3), $str3));
  126. }
  127. }