Hasher.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // Verify whether it matches a legacy PHPass or SHA1 string
  125. // Retry with empty passwordsalt for cases where it was not set
  126. $hashLength = \strlen($hash);
  127. if (($hashLength === 60 && password_verify($message, $hash)) ||
  128. ($hashLength === 40 && hash_equals($hash, sha1($message)))) {
  129. $newHash = $this->hash($message);
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Verify V1 (blowfish) hashes
  136. * Verify V2 (argon2i) hashes
  137. * Verify V3 (argon2id) hashes
  138. * @param string $message Message to verify
  139. * @param string $hash Assumed hash of the message
  140. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  141. * @return bool Whether $hash is a valid hash of $message
  142. */
  143. protected function verifyHash(string $message, string $hash, &$newHash = null): bool {
  144. if (password_verify($message, $hash)) {
  145. if ($this->needsRehash($hash)) {
  146. $newHash = $this->hash($message);
  147. }
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * @param string $message Message to verify
  154. * @param string $hash Assumed hash of the message
  155. * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
  156. * @return bool Whether $hash is a valid hash of $message
  157. */
  158. public function verify(string $message, string $hash, &$newHash = null): bool {
  159. $splittedHash = $this->splitHash($hash);
  160. if (isset($splittedHash['version'])) {
  161. switch ($splittedHash['version']) {
  162. case 3:
  163. case 2:
  164. case 1:
  165. return $this->verifyHash($message, $splittedHash['hash'], $newHash);
  166. }
  167. } else {
  168. return $this->legacyHashVerify($message, $hash, $newHash);
  169. }
  170. return false;
  171. }
  172. private function needsRehash(string $hash): bool {
  173. $algorithm = $this->getPrefferedAlgorithm();
  174. return password_needs_rehash($hash, $algorithm, $this->options);
  175. }
  176. private function getPrefferedAlgorithm() {
  177. $default = PASSWORD_BCRYPT;
  178. if (\defined('PASSWORD_ARGON2I')) {
  179. $default = PASSWORD_ARGON2I;
  180. }
  181. if (\defined('PASSWORD_ARGON2ID')) {
  182. $default = PASSWORD_ARGON2ID;
  183. }
  184. // Check if we should use PASSWORD_DEFAULT
  185. if ($this->config->getSystemValue('hashing_default_password', false) === true) {
  186. $default = PASSWORD_DEFAULT;
  187. }
  188. return $default;
  189. }
  190. }