[BUGFIX] Remove unused if in PasswordHashing
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Crypto / PasswordHashing / PhpassPasswordHash.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Crypto\PasswordHashing;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Crypto\Random;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Class that implements PHPass salted hashing based on Drupal's
23 * modified Openwall implementation.
24 *
25 * Derived from Drupal CMS
26 * original license: GNU General Public License (GPL)
27 *
28 * PHPass should work on every system.
29 * @see http://drupal.org/node/29706/
30 * @see http://www.openwall.com/phpass/
31 */
32 class PhpassPasswordHash implements PasswordHashInterface
33 {
34 /**
35 * Prefix for the password hash.
36 */
37 protected const PREFIX = '$P$';
38
39 /**
40 * @var array The default log2 number of iterations for password stretching.
41 */
42 protected $options = [
43 'hash_count' => 14
44 ];
45
46 /**
47 * Constructor sets options if given
48 *
49 * @param array $options
50 */
51 public function __construct(array $options = [])
52 {
53 $newOptions = $this->options;
54 if (isset($options['hash_count'])) {
55 if ((int)$options['hash_count'] < 7 || (int)$options['hash_count'] > 24) {
56 throw new \InvalidArgumentException(
57 'hash_count must not be lower than 7 or bigger than 24',
58 1533940454
59 );
60 }
61 $newOptions['hash_count'] = (int)$options['hash_count'];
62 }
63 $this->options = $newOptions;
64 }
65
66 /**
67 * Method checks if a given plaintext password is correct by comparing it with
68 * a given salted hashed password.
69 *
70 * @param string $plainPW Plain-text password to compare with salted hash
71 * @param string $saltedHashPW Salted hash to compare plain-text password with
72 * @return bool TRUE, if plain-text password matches the salted hash, otherwise FALSE
73 */
74 public function checkPassword(string $plainPW, string $saltedHashPW): bool
75 {
76 $hash = $this->cryptPassword($plainPW, $saltedHashPW);
77 return $hash && hash_equals($hash, $saltedHashPW);
78 }
79
80 /**
81 * Returns whether all prerequisites for the hashing methods are matched
82 *
83 * @return bool Method available
84 */
85 public function isAvailable(): bool
86 {
87 return true;
88 }
89
90 /**
91 * Method creates a salted hash for a given plaintext password
92 *
93 * @param string $password Plaintext password to create a salted hash from
94 * @return string|null salted hashed password
95 */
96 public function getHashedPassword(string $password)
97 {
98 $saltedPW = null;
99 if (!empty($password)) {
100 $salt = $this->getGeneratedSalt();
101 $saltedPW = $this->cryptPassword($password, $this->applySettingsToSalt($salt));
102 }
103 return $saltedPW;
104 }
105
106 /**
107 * Checks whether a user's hashed password needs to be replaced with a new hash.
108 *
109 * This is typically called during the login process when the plain text
110 * password is available. A new hash is needed when the desired iteration
111 * count has changed through a change in the variable $hashCount or HASH_COUNT.
112 *
113 * @param string $passString Salted hash to check if it needs an update
114 * @return bool TRUE if salted hash needs an update, otherwise FALSE
115 */
116 public function isHashUpdateNeeded(string $passString): bool
117 {
118 // Check whether this was an updated password.
119 if (strncmp($passString, '$P$', 3) || strlen($passString) != 34) {
120 return true;
121 }
122 // Check whether the iteration count used differs from the standard number.
123 return $this->getCountLog2($passString) < $this->options['hash_count'];
124 }
125
126 /**
127 * Method determines if a given string is a valid salted hashed password.
128 *
129 * @param string $saltedPW String to check
130 * @return bool TRUE if it's valid salted hashed password, otherwise FALSE
131 */
132 public function isValidSaltedPW(string $saltedPW): bool
133 {
134 $isValid = !strncmp(self::PREFIX, $saltedPW, strlen(self::PREFIX));
135 if ($isValid) {
136 $isValid = $this->isValidSalt($saltedPW);
137 }
138 return $isValid;
139 }
140
141 /**
142 * Method applies settings (prefix, hash count) to a salt.
143 *
144 * @param string $salt A salt to apply setting to
145 * @return string Salt with setting
146 */
147 protected function applySettingsToSalt(string $salt): string
148 {
149 $saltWithSettings = $salt;
150 $reqLenBase64 = $this->getLengthBase64FromBytes(6);
151 // Salt without setting
152 if (strlen($salt) == $reqLenBase64) {
153 // We encode the final log2 iteration count in base 64.
154 $itoa64 = $this->getItoa64();
155 $saltWithSettings = self::PREFIX . $itoa64[$this->options['hash_count']];
156 $saltWithSettings .= $salt;
157 }
158 return $saltWithSettings;
159 }
160
161 /**
162 * Hashes a password using a secure stretched hash.
163 *
164 * By using a salt and repeated hashing the password is "stretched". Its
165 * security is increased because it becomes much more computationally costly
166 * for an attacker to try to break the hash by brute-force computation of the
167 * hashes of a large number of plain-text words or strings to find a match.
168 *
169 * @param string $password Plain-text password to hash
170 * @param string $setting An existing hash or the output of getGeneratedSalt()
171 * @return mixed A string containing the hashed password (and salt)
172 */
173 protected function cryptPassword(string $password, string $setting)
174 {
175 $saltedPW = null;
176 $reqLenBase64 = $this->getLengthBase64FromBytes(6);
177 // Retrieving settings with salt
178 $setting = substr($setting, 0, strlen(self::PREFIX) + 1 + $reqLenBase64);
179 $count_log2 = $this->getCountLog2($setting);
180 // Hashes may be imported from elsewhere, so we allow != HASH_COUNT
181 if ($count_log2 >= 7 && $count_log2 <= 24) {
182 $salt = substr($setting, strlen(self::PREFIX) + 1, $reqLenBase64);
183 // We must use md5() or sha1() here since they are the only cryptographic
184 // primitives always available in PHP 5. To implement our own low-level
185 // cryptographic function in PHP would result in much worse performance and
186 // consequently in lower iteration counts and hashes that are quicker to crack
187 // (by non-PHP code).
188 $count = 1 << $count_log2;
189 $hash = md5($salt . $password, true);
190 do {
191 $hash = md5($hash . $password, true);
192 } while (--$count);
193 $saltedPW = $setting . $this->base64Encode($hash, 16);
194 // base64Encode() of a 16 byte MD5 will always be 22 characters.
195 return strlen($saltedPW) == 34 ? $saltedPW : false;
196 }
197 return $saltedPW;
198 }
199
200 /**
201 * Parses the log2 iteration count from a stored hash or setting string.
202 *
203 * @param string $setting Complete hash or a hash's setting string or to get log2 iteration count from
204 * @return int Used hashcount for given hash string
205 */
206 protected function getCountLog2(string $setting): int
207 {
208 return strpos($this->getItoa64(), $setting[strlen(self::PREFIX)]);
209 }
210
211 /**
212 * Generates a random base 64-encoded salt prefixed and suffixed with settings for the hash.
213 *
214 * Proper use of salts may defeat a number of attacks, including:
215 * - The ability to try candidate passwords against multiple hashes at once.
216 * - The ability to use pre-hashed lists of candidate passwords.
217 * - The ability to determine whether two users have the same (or different)
218 * password without actually having to guess one of the passwords.
219 *
220 * @return string A character string containing settings and a random salt
221 */
222 protected function getGeneratedSalt(): string
223 {
224 $randomBytes = GeneralUtility::makeInstance(Random::class)->generateRandomBytes(6);
225 return $this->base64Encode($randomBytes, 6);
226 }
227
228 /**
229 * Returns a string for mapping an int to the corresponding base 64 character.
230 *
231 * @return string String for mapping an int to the corresponding base 64 character
232 */
233 protected function getItoa64(): string
234 {
235 return './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
236 }
237
238 /**
239 * Method determines if a given string is a valid salt.
240 *
241 * @param string $salt String to check
242 * @return bool TRUE if it's valid salt, otherwise FALSE
243 */
244 protected function isValidSalt(string $salt): bool
245 {
246 $isValid = ($skip = false);
247 $reqLenBase64 = $this->getLengthBase64FromBytes(6);
248 if (strlen($salt) >= $reqLenBase64) {
249 // Salt with prefixed setting
250 if (!strncmp('$', $salt, 1)) {
251 if (!strncmp(self::PREFIX, $salt, strlen(self::PREFIX))) {
252 $isValid = true;
253 $salt = substr($salt, strrpos($salt, '$') + 2);
254 } else {
255 $skip = true;
256 }
257 }
258 // Checking base64 characters
259 if (!$skip && strlen($salt) >= $reqLenBase64) {
260 if (preg_match('/^[' . preg_quote($this->getItoa64(), '/') . ']{' . $reqLenBase64 . ',' . $reqLenBase64 . '}$/', substr($salt, 0, $reqLenBase64))) {
261 $isValid = true;
262 }
263 }
264 }
265 return $isValid;
266 }
267
268 /**
269 * Encodes bytes into printable base 64 using the *nix standard from crypt().
270 *
271 * @param string $input The string containing bytes to encode.
272 * @param int $count The number of characters (bytes) to encode.
273 * @return string Encoded string
274 */
275 protected function base64Encode(string $input, int $count): string
276 {
277 $output = '';
278 $i = 0;
279 $itoa64 = $this->getItoa64();
280 do {
281 $value = ord($input[$i++]);
282 $output .= $itoa64[$value & 63];
283 if ($i < $count) {
284 $value |= ord($input[$i]) << 8;
285 }
286 $output .= $itoa64[$value >> 6 & 63];
287 if ($i++ >= $count) {
288 break;
289 }
290 if ($i < $count) {
291 $value |= ord($input[$i]) << 16;
292 }
293 $output .= $itoa64[$value >> 12 & 63];
294 if ($i++ >= $count) {
295 break;
296 }
297 $output .= $itoa64[$value >> 18 & 63];
298 } while ($i < $count);
299 return $output;
300 }
301
302 /**
303 * Method determines required length of base64 characters for a given
304 * length of a byte string.
305 *
306 * @param int $byteLength Length of bytes to calculate in base64 chars
307 * @return int Required length of base64 characters
308 */
309 protected function getLengthBase64FromBytes(int $byteLength): int
310 {
311 // Calculates bytes in bits in base64
312 return (int)ceil($byteLength * 8 / 6);
313 }
314 }