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.

211 lines
6.8 KiB

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