Hasher.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /** @var IConfig */
  51. private $config;
  52. /** @var array Options passed to password_hash and password_needs_rehash */
  53. private $options = [];
  54. /** @var string Salt used for legacy passwords */
  55. private $legacySalt = null;
  56. /**
  57. * @param IConfig $config
  58. */
  59. public function __construct(IConfig $config) {
  60. $this->config = $config;
  61. if (\defined('PASSWORD_ARGON2ID') || \defined('PASSWORD_ARGON2I')) {
  62. // password_hash fails, when the minimum values are undershot.
  63. // In this case, apply minimum.
  64. $this->options['threads'] = max($this->config->getSystemValueInt('hashingThreads', PASSWORD_ARGON2_DEFAULT_THREADS), 1);
  65. // The minimum memory cost is 8 KiB per thread.
  66. $this->options['memory_cost'] = max($this->config->getSystemValueInt('hashingMemoryCost', PASSWORD_ARGON2_DEFAULT_MEMORY_COST), $this->options['threads'] * 8);
  67. $this->options['time_cost'] = max($this->config->getSystemValueInt('hashingTimeCost', PASSWORD_ARGON2_DEFAULT_TIME_COST), 1);
  68. }
  69. $hashingCost = $this->config->getSystemValue('hashingCost', null);
  70. if (!\is_null($hashingCost)) {
  71. $this->options['cost'] = $hashingCost;
  72. }
  73. }
  74. /**
  75. * Hashes a message using PHP's `password_hash` functionality.
  76. * Please note that the size of the returned string is not guaranteed
  77. * and can be up to 255 characters.
  78. *
  79. * @param string $message Message to generate hash from
  80. * @return string Hash of the message with appended version parameter
  81. */
  82. public function hash(string $message): string {
  83. $alg = $this->getPrefferedAlgorithm();
  84. if (\defined('PASSWORD_ARGON2ID') && $alg === PASSWORD_ARGON2ID) {
  85. return 3 . '|' . password_hash($message, PASSWORD_ARGON2ID, $this->options);
  86. }
  87. if (\defined('PASSWORD_ARGON2I') && $alg === PASSWORD_ARGON2I) {
  88. return 2 . '|' . password_hash($message, PASSWORD_ARGON2I, $this->options);
  89. }
  90. return 1 . '|' . password_hash($message, PASSWORD_BCRYPT, $this->options);
  91. }
  92. /**
  93. * Get the version and hash from a prefixedHash
  94. * @param string $prefixedHash
  95. * @return null|array Null if the hash is not prefixed, otherwise array('version' => 1, 'hash' => 'foo')
  96. */
  97. protected function splitHash(string $prefixedHash) {
  98. $explodedString = explode('|', $prefixedHash, 2);
  99. if (\count($explodedString) === 2) {
  100. if ((int)$explodedString[0] > 0) {
  101. return ['version' => (int)$explodedString[0], 'hash' => $explodedString[1]];
  102. }
  103. }
  104. return null;
  105. }
  106. /**
  107. * Verify legacy hashes
  108. * @param string $message Message to verify
  109. * @param string $hash Assumed hash of the message
  110. * @param null|string &$newHash Reference will contain the updated hash
  111. * @return bool Whether $hash is a valid hash of $message
  112. */
  113. protected function legacyHashVerify($message, $hash, &$newHash = null): bool {
  114. if (empty($this->legacySalt)) {
  115. $this->legacySalt = $this->config->getSystemValue('passwordsalt', '');
  116. }
  117. // Verify whether it matches a legacy PHPass or SHA1 string
  118. $hashLength = \strlen($hash);
  119. if (($hashLength === 60 && password_verify($message.$this->legacySalt, $hash)) ||
  120. ($hashLength === 40 && hash_equals($hash, sha1($message)))) {
  121. $newHash = $this->hash($message);
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. * Verify V1 (blowfish) hashes
  128. * Verify V2 (argon2i) hashes
  129. * Verify V3 (argon2id) hashes
  130. * @param string $message Message to verify
  131. * @param string $hash Assumed hash of the message
  132. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  133. * @return bool Whether $hash is a valid hash of $message
  134. */
  135. protected function verifyHash(string $message, string $hash, &$newHash = null): bool {
  136. if (password_verify($message, $hash)) {
  137. if ($this->needsRehash($hash)) {
  138. $newHash = $this->hash($message);
  139. }
  140. return true;
  141. }
  142. return false;
  143. }
  144. /**
  145. * @param string $message Message to verify
  146. * @param string $hash Assumed hash of the message
  147. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  148. * @return bool Whether $hash is a valid hash of $message
  149. */
  150. public function verify(string $message, string $hash, &$newHash = null): bool {
  151. $splittedHash = $this->splitHash($hash);
  152. if (isset($splittedHash['version'])) {
  153. switch ($splittedHash['version']) {
  154. case 3:
  155. case 2:
  156. case 1:
  157. return $this->verifyHash($message, $splittedHash['hash'], $newHash);
  158. }
  159. } else {
  160. return $this->legacyHashVerify($message, $hash, $newHash);
  161. }
  162. return false;
  163. }
  164. private function needsRehash(string $hash): bool {
  165. $algorithm = $this->getPrefferedAlgorithm();
  166. return password_needs_rehash($hash, $algorithm, $this->options);
  167. }
  168. private function getPrefferedAlgorithm() {
  169. $default = PASSWORD_BCRYPT;
  170. if (\defined('PASSWORD_ARGON2I')) {
  171. $default = PASSWORD_ARGON2I;
  172. }
  173. if (\defined('PASSWORD_ARGON2ID')) {
  174. $default = PASSWORD_ARGON2ID;
  175. }
  176. // Check if we should use PASSWORD_DEFAULT
  177. if ($this->config->getSystemValue('hashing_default_password', false) === true) {
  178. $default = PASSWORD_DEFAULT;
  179. }
  180. return $default;
  181. }
  182. }