Completed
Push — master ( ecc3bc...185a26 )
by Morris
76:10 queued 55:16
created

Hasher::verifyHashV2()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, ownCloud, Inc.
5
 *
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Security;
27
28
use OCP\IConfig;
29
use OCP\Security\IHasher;
30
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
	/**
59
	 * @param IConfig $config
60
	 */
61
	public function __construct(IConfig $config) {
62
		$this->config = $config;
63
64
		$hashingCost = $this->config->getSystemValue('hashingCost', null);
65
		if(!\is_null($hashingCost)) {
66
			$this->options['cost'] = $hashingCost;
67
		}
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
		if (\defined('PASSWORD_ARGON2I')) {
80
			return 2 . '|' . password_hash($message, PASSWORD_ARGON2I, $this->options);
81
		} else {
82
			return 1 . '|' . password_hash($message, PASSWORD_BCRYPT, $this->options);
83
		}
84
	}
85
86
	/**
87
	 * Get the version and hash from a prefixedHash
88
	 * @param string $prefixedHash
89
	 * @return null|array Null if the hash is not prefixed, otherwise array('version' => 1, 'hash' => 'foo')
90
	 */
91
	protected function splitHash(string $prefixedHash) {
92
		$explodedString = explode('|', $prefixedHash, 2);
93
		if(\count($explodedString) === 2) {
94
			if((int)$explodedString[0] > 0) {
95
				return ['version' => (int)$explodedString[0], 'hash' => $explodedString[1]];
96
			}
97
		}
98
99
		return null;
100
	}
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
114
		// Verify whether it matches a legacy PHPass or SHA1 string
115
		$hashLength = \strlen($hash);
116
		if(($hashLength === 60 && password_verify($message.$this->legacySalt, $hash)) ||
117
			($hashLength === 40 && hash_equals($hash, sha1($message)))) {
118
			$newHash = $this->hash($message);
119
			return true;
120
		}
121
122
		return false;
123
	}
124
125
	/**
126
	 * Verify V1 (blowfish) hashes
127
	 * @param string $message Message to verify
128
	 * @param string $hash Assumed hash of the message
129
	 * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
130
	 * @return bool Whether $hash is a valid hash of $message
131
	 */
132
	protected function verifyHashV1(string $message, string $hash, &$newHash = null): bool {
133
		if(password_verify($message, $hash)) {
134
			$algo = PASSWORD_BCRYPT;
135
			if (\defined('PASSWORD_ARGON2I')) {
136
				$algo = PASSWORD_ARGON2I;
137
			}
138
139
			if(password_needs_rehash($hash, $algo, $this->options)) {
140
				$newHash = $this->hash($message);
141
			}
142
			return true;
143
		}
144
145
		return false;
146
	}
147
148
	/**
149
	 * Verify V2 (argon2i) hashes
150
	 * @param string $message Message to verify
151
	 * @param string $hash Assumed hash of the message
152
	 * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
153
	 * @return bool Whether $hash is a valid hash of $message
154
	 */
155
	protected function verifyHashV2(string $message, string $hash, &$newHash = null) : bool {
156
		if(password_verify($message, $hash)) {
157
			if(password_needs_rehash($hash, PASSWORD_ARGON2I, $this->options)) {
158
				$newHash = $this->hash($message);
159
			}
160
			return true;
161
		}
162
163
		return false;
164
	}
165
166
	/**
167
	 * @param string $message Message to verify
168
	 * @param string $hash Assumed hash of the message
169
	 * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one.
170
	 * @return bool Whether $hash is a valid hash of $message
171
	 */
172
	public function verify(string $message, string $hash, &$newHash = null): bool {
173
		$splittedHash = $this->splitHash($hash);
174
175
		if(isset($splittedHash['version'])) {
176
			switch ($splittedHash['version']) {
177
				case 2:
178
					return $this->verifyHashV2($message, $splittedHash['hash'], $newHash);
179
				case 1:
180
					return $this->verifyHashV1($message, $splittedHash['hash'], $newHash);
181
			}
182
		} else {
183
			return $this->legacyHashVerify($message, $hash, $newHash);
184
		}
185
186
187
		return false;
188
	}
189
190
}
191