Issues (910)

framework/base/Security.php (1 issue)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use yii\helpers\StringHelper;
11
12
/**
13
 * Security provides a set of methods to handle common security-related tasks.
14
 *
15
 * In particular, Security supports the following features:
16
 *
17
 * - Encryption/decryption: [[encryptByKey()]], [[decryptByKey()]], [[encryptByPassword()]] and [[decryptByPassword()]]
18
 * - Key derivation using standard algorithms: [[pbkdf2()]] and [[hkdf()]]
19
 * - Data tampering prevention: [[hashData()]] and [[validateData()]]
20
 * - Password validation: [[generatePasswordHash()]] and [[validatePassword()]]
21
 *
22
 * > Note: this class requires 'OpenSSL' PHP extension for random key/string generation on Windows and
23
 * for encryption/decryption on all platforms. For the highest security level PHP version >= 5.5.0 is recommended.
24
 *
25
 * For more details and usage information on Security, see the [guide article on security](guide:security-overview).
26
 *
27
 * @author Qiang Xue <[email protected]>
28
 * @author Tom Worster <[email protected]>
29
 * @author Klimov Paul <[email protected]>
30
 * @since 2.0
31
 */
32
class Security extends Component
33
{
34
    /**
35
     * @var string The cipher to use for encryption and decryption.
36
     */
37
    public $cipher = 'AES-128-CBC';
38
    /**
39
     * @var array[] Look-up table of block sizes and key sizes for each supported OpenSSL cipher.
40
     *
41
     * In each element, the key is one of the ciphers supported by OpenSSL (@see openssl_get_cipher_methods()).
42
     * The value is an array of two integers, the first is the cipher's block size in bytes and the second is
43
     * the key size in bytes.
44
     *
45
     * > Warning: All OpenSSL ciphers that we recommend are in the default value, i.e. AES in CBC mode.
46
     *
47
     * > Note: Yii's encryption protocol uses the same size for cipher key, HMAC signature key and key
48
     * derivation salt.
49
     */
50
    public $allowedCiphers = [
51
        'AES-128-CBC' => [16, 16],
52
        'AES-192-CBC' => [16, 24],
53
        'AES-256-CBC' => [16, 32],
54
    ];
55
    /**
56
     * @var string Hash algorithm for key derivation. Recommend sha256, sha384 or sha512.
57
     * @see [hash_algos()](https://www.php.net/manual/en/function.hash-algos.php)
58
     */
59
    public $kdfHash = 'sha256';
60
    /**
61
     * @var string Hash algorithm for message authentication. Recommend sha256, sha384 or sha512.
62
     * @see [hash_algos()](https://www.php.net/manual/en/function.hash-algos.php)
63
     */
64
    public $macHash = 'sha256';
65
    /**
66
     * @var string HKDF info value for derivation of message authentication key.
67
     * @see hkdf()
68
     */
69
    public $authKeyInfo = 'AuthorizationKey';
70
    /**
71
     * @var int derivation iterations count.
72
     * Set as high as possible to hinder dictionary password attacks.
73
     */
74
    public $derivationIterations = 100000;
75
    /**
76
     * @var string strategy, which should be used to generate password hash.
77
     * Available strategies:
78
     * - 'password_hash' - use of PHP `password_hash()` function with PASSWORD_DEFAULT algorithm.
79
     *   This option is recommended, but it requires PHP version >= 5.5.0
80
     * - 'crypt' - use PHP `crypt()` function.
81
     * @deprecated since version 2.0.7, [[generatePasswordHash()]] ignores [[passwordHashStrategy]] and
82
     * uses `password_hash()` when available or `crypt()` when not.
83
     */
84
    public $passwordHashStrategy;
85
    /**
86
     * @var int Default cost used for password hashing.
87
     * Allowed value is between 4 and 31.
88
     * @see generatePasswordHash()
89
     * @since 2.0.6
90
     */
91
    public $passwordHashCost = 13;
92
93
    /**
94
     * @var boolean if LibreSSL should be used.
95
     * The recent (> 2.1.5) LibreSSL RNGs are faster and likely better than /dev/urandom.
96
     */
97
    private $_useLibreSSL;
98
99
100
    /**
101
     * @return bool if LibreSSL should be used
102
     * Use version is 2.1.5 or higher.
103
     * @since 2.0.36
104
     */
105
    protected function shouldUseLibreSSL()
106
    {
107
        if ($this->_useLibreSSL === null) {
108
            // Parse OPENSSL_VERSION_TEXT because OPENSSL_VERSION_NUMBER is no use for LibreSSL.
109
            // https://bugs.php.net/bug.php?id=71143
110
            $this->_useLibreSSL = defined('OPENSSL_VERSION_TEXT')
111
                && preg_match('{^LibreSSL (\d\d?)\.(\d\d?)\.(\d\d?)$}', OPENSSL_VERSION_TEXT, $matches)
112
                && (10000 * $matches[1]) + (100 * $matches[2]) + $matches[3] >= 20105;
113
        }
114
115
        return $this->_useLibreSSL;
116
    }
117
118
    /**
119
     * Encrypts data using a password.
120
     * Derives keys for encryption and authentication from the password using PBKDF2 and a random salt,
121
     * which is deliberately slow to protect against dictionary attacks. Use [[encryptByKey()]] to
122
     * encrypt fast using a cryptographic key rather than a password. Key derivation time is
123
     * determined by [[$derivationIterations]], which should be set as high as possible.
124
     * The encrypted data includes a keyed message authentication code (MAC) so there is no need
125
     * to hash input or output data.
126
     * > Note: Avoid encrypting with passwords wherever possible. Nothing can protect against
127
     * poor-quality or compromised passwords.
128
     * @param string $data the data to encrypt
129
     * @param string $password the password to use for encryption
130
     * @return string the encrypted data as byte string
131
     * @see decryptByPassword()
132
     * @see encryptByKey()
133
     */
134 1
    public function encryptByPassword($data, $password)
135
    {
136 1
        return $this->encrypt($data, true, $password, null);
137
    }
138
139
    /**
140
     * Encrypts data using a cryptographic key.
141
     * Derives keys for encryption and authentication from the input key using HKDF and a random salt,
142
     * which is very fast relative to [[encryptByPassword()]]. The input key must be properly
143
     * random -- use [[generateRandomKey()]] to generate keys.
144
     * The encrypted data includes a keyed message authentication code (MAC) so there is no need
145
     * to hash input or output data.
146
     * @param string $data the data to encrypt
147
     * @param string $inputKey the input to use for encryption and authentication
148
     * @param string|null $info optional context and application specific information, see [[hkdf()]]
149
     * @return string the encrypted data as byte string
150
     * @see decryptByKey()
151
     * @see encryptByPassword()
152
     */
153 1
    public function encryptByKey($data, $inputKey, $info = null)
154
    {
155 1
        return $this->encrypt($data, false, $inputKey, $info);
156
    }
157
158
    /**
159
     * Verifies and decrypts data encrypted with [[encryptByPassword()]].
160
     * @param string $data the encrypted data to decrypt
161
     * @param string $password the password to use for decryption
162
     * @return bool|string the decrypted data or false on authentication failure
163
     * @see encryptByPassword()
164
     */
165 10
    public function decryptByPassword($data, $password)
166
    {
167 10
        return $this->decrypt($data, true, $password, null);
168
    }
169
170
    /**
171
     * Verifies and decrypts data encrypted with [[encryptByKey()]].
172
     * @param string $data the encrypted data to decrypt
173
     * @param string $inputKey the input to use for encryption and authentication
174
     * @param string|null $info optional context and application specific information, see [[hkdf()]]
175
     * @return bool|string the decrypted data or false on authentication failure
176
     * @see encryptByKey()
177
     */
178 10
    public function decryptByKey($data, $inputKey, $info = null)
179
    {
180 10
        return $this->decrypt($data, false, $inputKey, $info);
181
    }
182
183
    /**
184
     * Encrypts data.
185
     *
186
     * @param string $data data to be encrypted
187
     * @param bool $passwordBased set true to use password-based key derivation
188
     * @param string $secret the encryption password or key
189
     * @param string|null $info context/application specific information, e.g. a user ID
190
     * See [RFC 5869 Section 3.2](https://tools.ietf.org/html/rfc5869#section-3.2) for more details.
191
     *
192
     * @return string the encrypted data as byte string
193
     * @throws InvalidConfigException on OpenSSL not loaded
194
     * @throws Exception on OpenSSL error
195
     * @see decrypt()
196
     */
197 2
    protected function encrypt($data, $passwordBased, $secret, $info)
198
    {
199 2
        if (!extension_loaded('openssl')) {
200
            throw new InvalidConfigException('Encryption requires the OpenSSL PHP extension');
201
        }
202 2
        if (!isset($this->allowedCiphers[$this->cipher][0], $this->allowedCiphers[$this->cipher][1])) {
203
            throw new InvalidConfigException($this->cipher . ' is not an allowed cipher');
204
        }
205
206 2
        list($blockSize, $keySize) = $this->allowedCiphers[$this->cipher];
207
208 2
        $keySalt = $this->generateRandomKey($keySize);
209 2
        if ($passwordBased) {
210 1
            $key = $this->pbkdf2($this->kdfHash, $secret, $keySalt, $this->derivationIterations, $keySize);
211
        } else {
212 1
            $key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
213
        }
214
215 2
        $iv = $this->generateRandomKey($blockSize);
216
217 2
        $encrypted = openssl_encrypt($data, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
218 2
        if ($encrypted === false) {
219
            throw new \yii\base\Exception('OpenSSL failure on encryption: ' . openssl_error_string());
220
        }
221
222 2
        $authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
223 2
        $hashed = $this->hashData($iv . $encrypted, $authKey);
224
225
        /*
226
         * Output: [keySalt][MAC][IV][ciphertext]
227
         * - keySalt is KEY_SIZE bytes long
228
         * - MAC: message authentication code, length same as the output of MAC_HASH
229
         * - IV: initialization vector, length $blockSize
230
         */
231 2
        return $keySalt . $hashed;
232
    }
233
234
    /**
235
     * Decrypts data.
236
     *
237
     * @param string $data encrypted data to be decrypted.
238
     * @param bool $passwordBased set true to use password-based key derivation
239
     * @param string $secret the decryption password or key
240
     * @param string|null $info context/application specific information, @see encrypt()
241
     *
242
     * @return bool|string the decrypted data or false on authentication failure
243
     * @throws InvalidConfigException on OpenSSL not loaded
244
     * @throws Exception on OpenSSL error
245
     * @see encrypt()
246
     */
247 20
    protected function decrypt($data, $passwordBased, $secret, $info)
248
    {
249 20
        if (!extension_loaded('openssl')) {
250
            throw new InvalidConfigException('Encryption requires the OpenSSL PHP extension');
251
        }
252 20
        if (!isset($this->allowedCiphers[$this->cipher][0], $this->allowedCiphers[$this->cipher][1])) {
253
            throw new InvalidConfigException($this->cipher . ' is not an allowed cipher');
254
        }
255
256 20
        list($blockSize, $keySize) = $this->allowedCiphers[$this->cipher];
257
258 20
        $keySalt = StringHelper::byteSubstr($data, 0, $keySize);
259 20
        if ($passwordBased) {
260 10
            $key = $this->pbkdf2($this->kdfHash, $secret, $keySalt, $this->derivationIterations, $keySize);
261
        } else {
262 10
            $key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
263
        }
264
265 20
        $authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
266 20
        $data = $this->validateData(StringHelper::byteSubstr($data, $keySize, null), $authKey);
267 20
        if ($data === false) {
268 2
            return false;
269
        }
270
271 20
        $iv = StringHelper::byteSubstr($data, 0, $blockSize);
272 20
        $encrypted = StringHelper::byteSubstr($data, $blockSize, null);
273
274 20
        $decrypted = openssl_decrypt($encrypted, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
275 20
        if ($decrypted === false) {
276
            throw new \yii\base\Exception('OpenSSL failure on decryption: ' . openssl_error_string());
277
        }
278
279 20
        return $decrypted;
280
    }
281
282
    /**
283
     * Derives a key from the given input key using the standard HKDF algorithm.
284
     * Implements HKDF specified in [RFC 5869](https://tools.ietf.org/html/rfc5869).
285
     * Recommend use one of the SHA-2 hash algorithms: sha224, sha256, sha384 or sha512.
286
     * @param string $algo a hash algorithm supported by `hash_hmac()`, e.g. 'SHA-256'
287
     * @param string $inputKey the source key
288
     * @param string|null $salt the random salt
289
     * @param string|null $info optional info to bind the derived key material to application-
290
     * and context-specific information, e.g. a user ID or API version, see
291
     * [RFC 5869](https://tools.ietf.org/html/rfc5869)
292
     * @param int $length length of the output key in bytes. If 0, the output key is
293
     * the length of the hash algorithm output.
294
     * @throws InvalidArgumentException when HMAC generation fails.
295
     * @return string the derived key
296
     */
297 27
    public function hkdf($algo, $inputKey, $salt = null, $info = null, $length = 0)
298
    {
299 27
        if (function_exists('hash_hkdf')) {
300 27
            $outputKey = hash_hkdf((string)$algo, (string)$inputKey, $length, (string)$info, (string)$salt);
301 27
            if ($outputKey === false) {
302
                throw new InvalidArgumentException('Invalid parameters to hash_hkdf()');
303
            }
304
305 27
            return $outputKey;
306
        }
307
308
        $test = @hash_hmac($algo, '', '', true);
309
        if (!$test) {
310
            throw new InvalidArgumentException('Failed to generate HMAC with hash algorithm: ' . $algo);
311
        }
312
        $hashLength = StringHelper::byteLength($test);
313
        if (is_string($length) && preg_match('{^\d{1,16}$}', $length)) {
314
            $length = (int) $length;
315
        }
316
        if (!is_int($length) || $length < 0 || $length > 255 * $hashLength) {
317
            throw new InvalidArgumentException('Invalid length');
318
        }
319
        $blocks = $length !== 0 ? ceil($length / $hashLength) : 1;
320
321
        if ($salt === null) {
322
            $salt = str_repeat("\0", $hashLength);
323
        }
324
        $prKey = hash_hmac($algo, $inputKey, $salt, true);
325
326
        $hmac = '';
327
        $outputKey = '';
328
        for ($i = 1; $i <= $blocks; $i++) {
329
            $hmac = hash_hmac($algo, $hmac . $info . chr($i), $prKey, true);
330
            $outputKey .= $hmac;
331
        }
332
333
        if ($length !== 0) {
334
            $outputKey = StringHelper::byteSubstr($outputKey, 0, $length);
335
        }
336
337
        return $outputKey;
338
    }
339
340
    /**
341
     * Derives a key from the given password using the standard PBKDF2 algorithm.
342
     * Implements HKDF2 specified in [RFC 2898](https://datatracker.ietf.org/doc/html/rfc2898#section-5.2)
343
     * Recommend use one of the SHA-2 hash algorithms: sha224, sha256, sha384 or sha512.
344
     * @param string $algo a hash algorithm supported by `hash_hmac()`, e.g. 'SHA-256'
345
     * @param string $password the source password
346
     * @param string $salt the random salt
347
     * @param int $iterations the number of iterations of the hash algorithm. Set as high as
348
     * possible to hinder dictionary password attacks.
349
     * @param int $length length of the output key in bytes. If 0, the output key is
350
     * the length of the hash algorithm output.
351
     * @return string the derived key
352
     * @throws InvalidArgumentException when hash generation fails due to invalid params given.
353
     */
354 18
    public function pbkdf2($algo, $password, $salt, $iterations, $length = 0)
355
    {
356 18
        if (function_exists('hash_pbkdf2') && PHP_VERSION_ID >= 50500) {
357 18
            $outputKey = hash_pbkdf2($algo, $password, $salt, $iterations, $length, true);
358 18
            if ($outputKey === false) {
359
                throw new InvalidArgumentException('Invalid parameters to hash_pbkdf2()');
360
            }
361
362 18
            return $outputKey;
363
        }
364
365
        // todo: is there a nice way to reduce the code repetition in hkdf() and pbkdf2()?
366
        $test = @hash_hmac($algo, '', '', true);
367
        if (!$test) {
368
            throw new InvalidArgumentException('Failed to generate HMAC with hash algorithm: ' . $algo);
369
        }
370
        if (is_string($iterations) && preg_match('{^\d{1,16}$}', $iterations)) {
371
            $iterations = (int) $iterations;
372
        }
373
        if (!is_int($iterations) || $iterations < 1) {
374
            throw new InvalidArgumentException('Invalid iterations');
375
        }
376
        if (is_string($length) && preg_match('{^\d{1,16}$}', $length)) {
377
            $length = (int) $length;
378
        }
379
        if (!is_int($length) || $length < 0) {
380
            throw new InvalidArgumentException('Invalid length');
381
        }
382
        $hashLength = StringHelper::byteLength($test);
383
        $blocks = $length !== 0 ? ceil($length / $hashLength) : 1;
384
385
        $outputKey = '';
386
        for ($j = 1; $j <= $blocks; $j++) {
387
            $hmac = hash_hmac($algo, $salt . pack('N', $j), $password, true);
388
            $xorsum = $hmac;
389
            for ($i = 1; $i < $iterations; $i++) {
390
                $hmac = hash_hmac($algo, $hmac, $password, true);
391
                $xorsum ^= $hmac;
392
            }
393
            $outputKey .= $xorsum;
394
        }
395
396
        if ($length !== 0) {
397
            $outputKey = StringHelper::byteSubstr($outputKey, 0, $length);
398
        }
399
400
        return $outputKey;
401
    }
402
403
    /**
404
     * Prefixes data with a keyed hash value so that it can later be detected if it is tampered.
405
     * There is no need to hash inputs or outputs of [[encryptByKey()]] or [[encryptByPassword()]]
406
     * as those methods perform the task.
407
     * @param string $data the data to be protected
408
     * @param string $key the secret key to be used for generating hash. Should be a secure
409
     * cryptographic key.
410
     * @param bool $rawHash whether the generated hash value is in raw binary format. If false, lowercase
411
     * hex digits will be generated.
412
     * @return string the data prefixed with the keyed hash
413
     * @throws InvalidConfigException when HMAC generation fails.
414
     * @see validateData()
415
     * @see generateRandomKey()
416
     * @see hkdf()
417
     * @see pbkdf2()
418
     */
419 12
    public function hashData($data, $key, $rawHash = false)
420
    {
421 12
        $hash = hash_hmac($this->macHash, $data, $key, $rawHash);
422 12
        if (!$hash) {
423
            throw new InvalidConfigException('Failed to generate HMAC with hash algorithm: ' . $this->macHash);
424
        }
425
426 12
        return $hash . $data;
427
    }
428
429
    /**
430
     * Validates if the given data is tampered.
431
     * @param string $data the data to be validated. The data must be previously
432
     * generated by [[hashData()]].
433
     * @param string $key the secret key that was previously used to generate the hash for the data in [[hashData()]].
434
     * function to see the supported hashing algorithms on your system. This must be the same
435
     * as the value passed to [[hashData()]] when generating the hash for the data.
436
     * @param bool $rawHash this should take the same value as when you generate the data using [[hashData()]].
437
     * It indicates whether the hash value in the data is in binary format. If false, it means the hash value consists
438
     * of lowercase hex digits only.
439
     * hex digits will be generated.
440
     * @return string|false the real data with the hash stripped off. False if the data is tampered.
441
     * @throws InvalidConfigException when HMAC generation fails.
442
     * @see hashData()
443
     */
444 21
    public function validateData($data, $key, $rawHash = false)
445
    {
446 21
        $test = @hash_hmac($this->macHash, '', '', $rawHash);
447 21
        if (!$test) {
448
            throw new InvalidConfigException('Failed to generate HMAC with hash algorithm: ' . $this->macHash);
449
        }
450 21
        $hashLength = StringHelper::byteLength($test);
451 21
        if (StringHelper::byteLength($data) >= $hashLength) {
452 21
            $hash = StringHelper::byteSubstr($data, 0, $hashLength);
453 21
            $pureData = StringHelper::byteSubstr($data, $hashLength, null);
454
455 21
            $calculatedHash = hash_hmac($this->macHash, $pureData, $key, $rawHash);
456
457 21
            if ($this->compareString($hash, $calculatedHash)) {
458 21
                return $pureData;
459
            }
460
        }
461
462 3
        return false;
463
    }
464
465
    /**
466
     * Generates specified number of random bytes.
467
     * Note that output may not be ASCII.
468
     * @see generateRandomString() if you need a string.
469
     *
470
     * @param int $length the number of bytes to generate
471
     * @return string the generated random bytes
472
     * @throws InvalidArgumentException if wrong length is specified
473
     * @throws Exception on failure.
474
     */
475 117
    public function generateRandomKey($length = 32)
476
    {
477 117
        if (!is_int($length)) {
478 3
            throw new InvalidArgumentException('First parameter ($length) must be an integer');
479
        }
480
481 114
        if ($length < 1) {
482 3
            throw new InvalidArgumentException('First parameter ($length) must be greater than 0');
483
        }
484
485 111
        return random_bytes($length);
486
    }
487
488
    /**
489
     * Generates a random string of specified length.
490
     * The string generated matches [A-Za-z0-9_-]+ and is transparent to URL-encoding.
491
     *
492
     * @param int $length the length of the key in characters
493
     * @return string the generated random key
494
     * @throws Exception on failure.
495
     */
496 101
    public function generateRandomString($length = 32)
497
    {
498 101
        if (!is_int($length)) {
499
            throw new InvalidArgumentException('First parameter ($length) must be an integer');
500
        }
501
502 101
        if ($length < 1) {
503
            throw new InvalidArgumentException('First parameter ($length) must be greater than 0');
504
        }
505
506 101
        $bytes = $this->generateRandomKey($length);
507 101
        return substr(StringHelper::base64UrlEncode($bytes), 0, $length);
508
    }
509
510
    /**
511
     * Generates a secure hash from a password and a random salt.
512
     *
513
     * The generated hash can be stored in database.
514
     * Later when a password needs to be validated, the hash can be fetched and passed
515
     * to [[validatePassword()]]. For example,
516
     *
517
     * ```php
518
     * // generates the hash (usually done during user registration or when the password is changed)
519
     * $hash = Yii::$app->getSecurity()->generatePasswordHash($password);
520
     * // ...save $hash in database...
521
     *
522
     * // during login, validate if the password entered is correct using $hash fetched from database
523
     * if (Yii::$app->getSecurity()->validatePassword($password, $hash)) {
524
     *     // password is good
525
     * } else {
526
     *     // password is bad
527
     * }
528
     * ```
529
     *
530
     * @param string $password The password to be hashed.
531
     * @param int|null $cost Cost parameter used by the Blowfish hash algorithm.
532
     * The higher the value of cost,
533
     * the longer it takes to generate the hash and to verify a password against it. Higher cost
534
     * therefore slows down a brute-force attack. For best protection against brute-force attacks,
535
     * set it to the highest value that is tolerable on production servers. The time taken to
536
     * compute the hash doubles for every increment by one of $cost.
537
     * @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt',
538
     * the output is always 60 ASCII characters, when set to 'password_hash' the output length
539
     * might increase in future versions of PHP (https://www.php.net/manual/en/function.password-hash.php)
540
     * @throws Exception on bad password parameter or cost parameter.
541
     * @see validatePassword()
542
     */
543 1
    public function generatePasswordHash($password, $cost = null)
544
    {
545 1
        if ($cost === null) {
546 1
            $cost = $this->passwordHashCost;
547
        }
548
549 1
        if (function_exists('password_hash')) {
550
            /* @noinspection PhpUndefinedConstantInspection */
551 1
            return password_hash($password, PASSWORD_DEFAULT, ['cost' => $cost]);
552
        }
553
554
        $salt = $this->generateSalt($cost);
555
        $hash = crypt($password, $salt);
556
        // strlen() is safe since crypt() returns only ascii
557
        if (!is_string($hash) || strlen($hash) !== 60) {
558
            throw new Exception('Unknown error occurred while generating hash.');
559
        }
560
561
        return $hash;
562
    }
563
564
    /**
565
     * Verifies a password against a hash.
566
     * @param string $password The password to verify.
567
     * @param string $hash The hash to verify the password against.
568
     * @return bool whether the password is correct.
569
     * @throws InvalidArgumentException on bad password/hash parameters or if crypt() with Blowfish hash is not available.
570
     * @see generatePasswordHash()
571
     */
572 1
    public function validatePassword($password, $hash)
573
    {
574 1
        if (!is_string($password) || $password === '') {
575
            throw new InvalidArgumentException('Password must be a string and cannot be empty.');
576
        }
577
578
        if (
579 1
            !preg_match('/^\$2[axy]\$(\d\d)\$[\.\/0-9A-Za-z]{22}/', $hash, $matches)
580 1
            || $matches[1] < 4
581 1
            || $matches[1] > 30
582
        ) {
583
            throw new InvalidArgumentException('Hash is invalid.');
584
        }
585
586 1
        if (function_exists('password_verify')) {
587 1
            return password_verify($password, $hash);
588
        }
589
590
        $test = crypt($password, $hash);
591
        $n = strlen($test);
592
        if ($n !== 60) {
593
            return false;
594
        }
595
596
        return $this->compareString($test, $hash);
597
    }
598
599
    /**
600
     * Generates a salt that can be used to generate a password hash.
601
     *
602
     * The PHP [crypt()](https://www.php.net/manual/en/function.crypt.php) built-in function
603
     * requires, for the Blowfish hash algorithm, a salt string in a specific format:
604
     * "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters
605
     * from the alphabet "./0-9A-Za-z".
606
     *
607
     * @param int $cost the cost parameter
608
     * @return string the random salt value.
609
     * @throws InvalidArgumentException if the cost parameter is out of the range of 4 to 31.
610
     */
611
    protected function generateSalt($cost = 13)
612
    {
613
        $cost = (int) $cost;
614
        if ($cost < 4 || $cost > 31) {
615
            throw new InvalidArgumentException('Cost must be between 4 and 31.');
616
        }
617
618
        // Get a 20-byte random string
619
        $rand = $this->generateRandomKey(20);
620
        // Form the prefix that specifies Blowfish (bcrypt) algorithm and cost parameter.
621
        $salt = sprintf('$2y$%02d$', $cost);
622
        // Append the random salt data in the required base64 format.
623
        $salt .= str_replace('+', '.', substr(base64_encode($rand), 0, 22));
624
625
        return $salt;
626
    }
627
628
    /**
629
     * Performs string comparison using timing attack resistant approach.
630
     * @see https://codereview.stackexchange.com/q/13512
631
     * @param string $expected string to compare.
632
     * @param string $actual user-supplied string.
633
     * @return bool whether strings are equal.
634
     */
635 39
    public function compareString($expected, $actual)
636
    {
637 39
        if (!is_string($expected)) {
638
            throw new InvalidArgumentException('Expected expected value to be a string, ' . gettype($expected) . ' given.');
639
        }
640
641 39
        if (!is_string($actual)) {
642
            throw new InvalidArgumentException('Expected actual value to be a string, ' . gettype($actual) . ' given.');
643
        }
644
645 39
        if (function_exists('hash_equals')) {
646 39
            return hash_equals($expected, $actual);
647
        }
648
649
        $expected .= "\0";
650
        $actual .= "\0";
651
        $expectedLength = StringHelper::byteLength($expected);
652
        $actualLength = StringHelper::byteLength($actual);
653
        $diff = $expectedLength - $actualLength;
654
        for ($i = 0; $i < $actualLength; $i++) {
655
            $diff |= (ord($actual[$i]) ^ ord($expected[$i % $expectedLength]));
656
        }
657
658
        return $diff === 0;
659
    }
660
661
    /**
662
     * Masks a token to make it uncompressible.
663
     * Applies a random mask to the token and prepends the mask used to the result making the string always unique.
664
     * Used to mitigate BREACH attack by randomizing how token is outputted on each request.
665
     * @param string $token An unmasked token.
666
     * @return string A masked token.
667
     * @since 2.0.12
668
     */
669 93
    public function maskToken($token)
670
    {
671
        // The number of bytes in a mask is always equal to the number of bytes in a token.
672 93
        $mask = $this->generateRandomKey(StringHelper::byteLength($token));
673 92
        return StringHelper::base64UrlEncode($mask . ($mask ^ $token));
674
    }
675
676
    /**
677
     * Unmasks a token previously masked by `maskToken`.
678
     * @param string $maskedToken A masked token.
679
     * @return string An unmasked token, or an empty string in case of token format is invalid.
680
     * @since 2.0.12
681
     */
682 9
    public function unmaskToken($maskedToken)
683
    {
684 9
        $decoded = StringHelper::base64UrlDecode($maskedToken);
685 9
        $length = StringHelper::byteLength($decoded) / 2;
686
        // Check if the masked token has an even length.
687 9
        if (!is_int($length)) {
0 ignored issues
show
The condition is_int($length) is always true.
Loading history...
688 1
            return '';
689
        }
690
691 9
        return StringHelper::byteSubstr($decoded, $length, $length) ^ StringHelper::byteSubstr($decoded, 0, $length);
692
    }
693
}
694