Completed
Push — remove-hhvm ( 3f0295 )
by Alexander
13:56 queued 10:07
created

Security::generateRandomKey()   F

Complexity

Conditions 28
Paths 979

Size

Total Lines 97
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 40.25

Importance

Changes 0
Metric Value
dl 0
loc 97
ccs 36
cts 48
cp 0.75
rs 2.1804
c 0
b 0
f 0
cc 28
eloc 49
nc 979
nop 1
crap 40.25

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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