Hasher.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author MichaIng <micha@dietpi.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Security;
  28. use OCP\IConfig;
  29. use OCP\Security\IHasher;
  30. /**
  31. * Class Hasher provides some basic hashing functions. Furthermore, it supports legacy hashes
  32. * used by previous versions of ownCloud and helps migrating those hashes to newer ones.
  33. *
  34. * The hashes generated by this class are prefixed (version|hash) with a version parameter to allow possible
  35. * updates in the future.
  36. * Possible versions:
  37. * - 1 (Initial version)
  38. *
  39. * Usage:
  40. * // Hashing a message
  41. * $hash = \OC::$server->getHasher()->hash('MessageToHash');
  42. * // Verifying a message - $newHash will contain the newly calculated hash
  43. * $newHash = null;
  44. * var_dump(\OC::$server->getHasher()->verify('a', '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', $newHash));
  45. * var_dump($newHash);
  46. *
  47. * @package OC\Security
  48. */
  49. class Hasher implements IHasher {
  50. /** Options passed to password_hash and password_needs_rehash */
  51. private array $options = [];
  52. /** Salt used for legacy passwords */
  53. private ?string $legacySalt = null;
  54. public function __construct(
  55. private IConfig $config,
  56. ) {
  57. if (\defined('PASSWORD_ARGON2ID') || \defined('PASSWORD_ARGON2I')) {
  58. // password_hash fails, when the minimum values are undershot.
  59. // In this case, apply minimum.
  60. $this->options['threads'] = max($this->config->getSystemValueInt('hashingThreads', PASSWORD_ARGON2_DEFAULT_THREADS), 1);
  61. // The minimum memory cost is 8 KiB per thread.
  62. $this->options['memory_cost'] = max($this->config->getSystemValueInt('hashingMemoryCost', PASSWORD_ARGON2_DEFAULT_MEMORY_COST), $this->options['threads'] * 8);
  63. $this->options['time_cost'] = max($this->config->getSystemValueInt('hashingTimeCost', PASSWORD_ARGON2_DEFAULT_TIME_COST), 1);
  64. }
  65. $hashingCost = $this->config->getSystemValue('hashingCost', null);
  66. if (!\is_null($hashingCost)) {
  67. $this->options['cost'] = $hashingCost;
  68. }
  69. }
  70. /**
  71. * Hashes a message using PHP's `password_hash` functionality.
  72. * Please note that the size of the returned string is not guaranteed
  73. * and can be up to 255 characters.
  74. *
  75. * @param string $message Message to generate hash from
  76. * @return string Hash of the message with appended version parameter
  77. */
  78. public function hash(string $message): string {
  79. $alg = $this->getPrefferedAlgorithm();
  80. if (\defined('PASSWORD_ARGON2ID') && $alg === PASSWORD_ARGON2ID) {
  81. return 3 . '|' . password_hash($message, PASSWORD_ARGON2ID, $this->options);
  82. }
  83. if (\defined('PASSWORD_ARGON2I') && $alg === PASSWORD_ARGON2I) {
  84. return 2 . '|' . password_hash($message, PASSWORD_ARGON2I, $this->options);
  85. }
  86. return 1 . '|' . password_hash($message, PASSWORD_BCRYPT, $this->options);
  87. }
  88. /**
  89. * Get the version and hash from a prefixedHash
  90. * @param string $prefixedHash
  91. * @return null|array Null if the hash is not prefixed, otherwise array('version' => 1, 'hash' => 'foo')
  92. */
  93. protected function splitHash(string $prefixedHash): ?array {
  94. $explodedString = explode('|', $prefixedHash, 2);
  95. if (\count($explodedString) === 2) {
  96. if ((int)$explodedString[0] > 0) {
  97. return ['version' => (int)$explodedString[0], 'hash' => $explodedString[1]];
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * Verify legacy hashes
  104. * @param string $message Message to verify
  105. * @param string $hash Assumed hash of the message
  106. * @param null|string &$newHash Reference will contain the updated hash
  107. * @return bool Whether $hash is a valid hash of $message
  108. */
  109. protected function legacyHashVerify($message, $hash, &$newHash = null): bool {
  110. if (empty($this->legacySalt)) {
  111. $this->legacySalt = $this->config->getSystemValue('passwordsalt', '');
  112. }
  113. // Verify whether it matches a legacy PHPass or SHA1 string
  114. $hashLength = \strlen($hash);
  115. if (($hashLength === 60 && password_verify($message.$this->legacySalt, $hash)) ||
  116. ($hashLength === 40 && hash_equals($hash, sha1($message)))) {
  117. $newHash = $this->hash($message);
  118. return true;
  119. }
  120. // Verify whether it matches a legacy PHPass or SHA1 string
  121. // Retry with empty passwordsalt for cases where it was not set
  122. $hashLength = \strlen($hash);
  123. if (($hashLength === 60 && password_verify($message, $hash)) ||
  124. ($hashLength === 40 && hash_equals($hash, sha1($message)))) {
  125. $newHash = $this->hash($message);
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * Verify V1 (blowfish) hashes
  132. * Verify V2 (argon2i) hashes
  133. * Verify V3 (argon2id) hashes
  134. * @param string $message Message to verify
  135. * @param string $hash Assumed hash of the message
  136. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  137. * @return bool Whether $hash is a valid hash of $message
  138. */
  139. protected function verifyHash(string $message, string $hash, &$newHash = null): bool {
  140. if (password_verify($message, $hash)) {
  141. if ($this->needsRehash($hash)) {
  142. $newHash = $this->hash($message);
  143. }
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * @param string $message Message to verify
  150. * @param string $hash Assumed hash of the message
  151. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  152. * @return bool Whether $hash is a valid hash of $message
  153. */
  154. public function verify(string $message, string $hash, &$newHash = null): bool {
  155. $splittedHash = $this->splitHash($hash);
  156. if (isset($splittedHash['version'])) {
  157. switch ($splittedHash['version']) {
  158. case 3:
  159. case 2:
  160. case 1:
  161. return $this->verifyHash($message, $splittedHash['hash'], $newHash);
  162. }
  163. } else {
  164. return $this->legacyHashVerify($message, $hash, $newHash);
  165. }
  166. return false;
  167. }
  168. private function needsRehash(string $hash): bool {
  169. $algorithm = $this->getPrefferedAlgorithm();
  170. return password_needs_rehash($hash, $algorithm, $this->options);
  171. }
  172. private function getPrefferedAlgorithm(): string {
  173. $default = PASSWORD_BCRYPT;
  174. if (\defined('PASSWORD_ARGON2I')) {
  175. $default = PASSWORD_ARGON2I;
  176. }
  177. if (\defined('PASSWORD_ARGON2ID')) {
  178. $default = PASSWORD_ARGON2ID;
  179. }
  180. // Check if we should use PASSWORD_DEFAULT
  181. if ($this->config->getSystemValueBool('hashing_default_password', false)) {
  182. $default = PASSWORD_DEFAULT;
  183. }
  184. return $default;
  185. }
  186. }