Completed
Push — 2.1 ( adca09...a94e80 )
by Alexander
117:38 queued 77:42
created

Security::decrypt()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0852

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 13
cts 15
cp 0.8667
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 8
nop 4
crap 6.0852
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;
11
use yii\helpers\StringHelper;
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
    /**
78
     * @var int Default cost used for password hashing.
79
     * Allowed value is between 4 and 31.
80
     * @see generatePasswordHash()
81
     * @since 2.0.6
82
     */
83
    public $passwordHashCost = 13;
84
85
86
    /**
87
     * Encrypts data using a password.
88
     * Derives keys for encryption and authentication from the password using PBKDF2 and a random salt,
89
     * which is deliberately slow to protect against dictionary attacks. Use [[encryptByKey()]] to
90
     * encrypt fast using a cryptographic key rather than a password. Key derivation time is
91
     * determined by [[$derivationIterations]], which should be set as high as possible.
92
     * The encrypted data includes a keyed message authentication code (MAC) so there is no need
93
     * to hash input or output data.
94
     * > Note: Avoid encrypting with passwords wherever possible. Nothing can protect against
95
     * poor-quality or compromised passwords.
96
     * @param string $data the data to encrypt
97
     * @param string $password the password to use for encryption
98
     * @return string the encrypted data
99
     * @see decryptByPassword()
100
     * @see encryptByKey()
101
     */
102
    public function encryptByPassword($data, $password)
103
    {
104
        return $this->encrypt($data, true, $password, null);
105
    }
106
107
    /**
108
     * Encrypts data using a cryptographic key.
109
     * Derives keys for encryption and authentication from the input key using HKDF and a random salt,
110
     * which is very fast relative to [[encryptByPassword()]]. The input key must be properly
111 1
     * random -- use [[generateRandomKey()]] to generate keys.
112
     * The encrypted data includes a keyed message authentication code (MAC) so there is no need
113 1
     * to hash input or output data.
114
     * @param string $data the data to encrypt
115
     * @param string $inputKey the input to use for encryption and authentication
116
     * @param string $info optional context and application specific information, see [[hkdf()]]
117
     * @return string the encrypted data
118
     * @see decryptByKey()
119
     * @see encryptByPassword()
120
     */
121
    public function encryptByKey($data, $inputKey, $info = null)
122
    {
123
        return $this->encrypt($data, false, $inputKey, $info);
124
    }
125
126
    /**
127
     * Verifies and decrypts data encrypted with [[encryptByPassword()]].
128
     * @param string $data the encrypted data to decrypt
129
     * @param string $password the password to use for decryption
130 1
     * @return bool|string the decrypted data or false on authentication failure
131
     * @see encryptByPassword()
132 1
     */
133
    public function decryptByPassword($data, $password)
134
    {
135
        return $this->decrypt($data, true, $password, null);
136
    }
137
138
    /**
139
     * Verifies and decrypts data encrypted with [[encryptByKey()]].
140
     * @param string $data the encrypted data to decrypt
141
     * @param string $inputKey the input to use for encryption and authentication
142 10
     * @param string $info optional context and application specific information, see [[hkdf()]]
143
     * @return bool|string the decrypted data or false on authentication failure
144 10
     * @see encryptByKey()
145
     */
146
    public function decryptByKey($data, $inputKey, $info = null)
147
    {
148
        return $this->decrypt($data, false, $inputKey, $info);
149
    }
150
151
    /**
152
     * Encrypts data.
153
     *
154
     * @param string $data data to be encrypted
155 10
     * @param bool $passwordBased set true to use password-based key derivation
156
     * @param string $secret the encryption password or key
157 10
     * @param string|null $info context/application specific information, e.g. a user ID
158
     * See [RFC 5869 Section 3.2](https://tools.ietf.org/html/rfc5869#section-3.2) for more details.
159
     *
160
     * @return string the encrypted data
161
     * @throws InvalidConfigException on OpenSSL not loaded
162
     * @throws Exception on OpenSSL error
163
     * @see decrypt()
164
     */
165
    protected function encrypt($data, $passwordBased, $secret, $info)
166
    {
167
        if (!extension_loaded('openssl')) {
168
            throw new InvalidConfigException('Encryption requires the OpenSSL PHP extension');
169
        }
170
        if (!isset($this->allowedCiphers[$this->cipher][0], $this->allowedCiphers[$this->cipher][1])) {
171
            throw new InvalidConfigException($this->cipher . ' is not an allowed cipher');
172
        }
173
174 2
        [$blockSize, $keySize] = $this->allowedCiphers[$this->cipher];
0 ignored issues
show
Bug introduced by
The variable $blockSize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $keySize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
175
176 2
        $keySalt = $this->generateRandomKey($keySize);
177
        if ($passwordBased) {
178
            $key = $this->pbkdf2($this->kdfHash, $secret, $keySalt, $this->derivationIterations, $keySize);
179 2
        } else {
180
            $key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
181
        }
182
183 2
        $iv = $this->generateRandomKey($blockSize);
184
185 2
        $encrypted = openssl_encrypt($data, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
186 2
        if ($encrypted === false) {
187 1
            throw new \yii\base\Exception('OpenSSL failure on encryption: ' . openssl_error_string());
188
        }
189 1
190
        $authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
191
        $hashed = $this->hashData($iv . $encrypted, $authKey);
192 2
193
        /*
194 2
         * Output: [keySalt][MAC][IV][ciphertext]
195 2
         * - keySalt is KEY_SIZE bytes long
196
         * - MAC: message authentication code, length same as the output of MAC_HASH
197
         * - IV: initialization vector, length $blockSize
198
         */
199 2
        return $keySalt . $hashed;
200 2
    }
201
202
    /**
203
     * Decrypts data.
204
     *
205
     * @param string $data encrypted data to be decrypted.
206
     * @param bool $passwordBased set true to use password-based key derivation
207
     * @param string $secret the decryption password or key
208 2
     * @param string|null $info context/application specific information, @see encrypt()
209
     *
210
     * @return bool|string the decrypted data or false on authentication failure
211
     * @throws InvalidConfigException on OpenSSL not loaded
212
     * @throws Exception on OpenSSL error
213
     * @see encrypt()
214
     */
215
    protected function decrypt($data, $passwordBased, $secret, $info)
216
    {
217
        if (!extension_loaded('openssl')) {
218
            throw new InvalidConfigException('Encryption requires the OpenSSL PHP extension');
219
        }
220
        if (!isset($this->allowedCiphers[$this->cipher][0], $this->allowedCiphers[$this->cipher][1])) {
221
            throw new InvalidConfigException($this->cipher . ' is not an allowed cipher');
222
        }
223
224 20
        [$blockSize, $keySize] = $this->allowedCiphers[$this->cipher];
0 ignored issues
show
Bug introduced by
The variable $blockSize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $keySize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
225
226 20
        $keySalt = StringHelper::byteSubstr($data, 0, $keySize);
227
        if ($passwordBased) {
228
            $key = $this->pbkdf2($this->kdfHash, $secret, $keySalt, $this->derivationIterations, $keySize);
229 20
        } else {
230
            $key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
231
        }
232
233 20
        $authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
234
        $data = $this->validateData(StringHelper::byteSubstr($data, $keySize), $authKey);
235 20
        if ($data === false) {
236 20
            return false;
237 10
        }
238
239 10
        $iv = StringHelper::byteSubstr($data, 0, $blockSize);
240
        $encrypted = StringHelper::byteSubstr($data, $blockSize);
241
242 20
        $decrypted = openssl_decrypt($encrypted, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
243 20
        if ($decrypted === false) {
244 20
            throw new \yii\base\Exception('OpenSSL failure on decryption: ' . openssl_error_string());
245 2
        }
246
247
        return $decrypted;
248 20
    }
249 20
250
    /**
251 20
     * Derives a key from the given input key using the standard HKDF algorithm.
252 20
     * Implements HKDF specified in [RFC 5869](https://tools.ietf.org/html/rfc5869).
253
     * Recommend use one of the SHA-2 hash algorithms: sha224, sha256, sha384 or sha512.
254
     * @param string $algo a hash algorithm supported by `hash_hmac()`, e.g. 'SHA-256'
255
     * @param string $inputKey the source key
256 20
     * @param string $salt the random salt
257
     * @param string $info optional info to bind the derived key material to application-
258
     * and context-specific information, e.g. a user ID or API version, see
259
     * [RFC 5869](https://tools.ietf.org/html/rfc5869)
260
     * @param int $length length of the output key in bytes. If 0, the output key is
261
     * the length of the hash algorithm output.
262
     * @throws InvalidArgumentException when HMAC generation fails.
263
     * @return string the derived key
264
     */
265
    public function hkdf($algo, $inputKey, $salt = null, $info = null, $length = 0)
266
    {
267
        if (function_exists('hash_hkdf')) {
268
            $outputKey = hash_hkdf($algo, $inputKey, $length, $info, $salt);
269
            if ($outputKey === false) {
270
                throw new InvalidArgumentException('Invalid parameters to hash_hkdf()');
271
            }
272
            return $outputKey;
273
        }
274 27
275
        $test = @hash_hmac($algo, '', '', true);
276 27
        if (!$test) {
277 27
            throw new InvalidArgumentException('Failed to generate HMAC with hash algorithm: ' . $algo);
278 27
        }
279
        $hashLength = StringHelper::byteLength($test);
280
        if (is_string($length) && preg_match('{^\d{1,16}$}', $length)) {
281 27
            $length = (int) $length;
282
        }
283
        if (!is_int($length) || $length < 0 || $length > 255 * $hashLength) {
284
            throw new InvalidArgumentException('Invalid length');
285
        }
286
        $blocks = $length !== 0 ? ceil($length / $hashLength) : 1;
287
288
        if ($salt === null) {
289
            $salt = str_repeat("\0", $hashLength);
290
        }
291
        $prKey = hash_hmac($algo, $inputKey, $salt, true);
292
293
        $hmac = '';
294
        $outputKey = '';
295
        for ($i = 1; $i <= $blocks; $i++) {
296
            $hmac = hash_hmac($algo, $hmac . $info . chr($i), $prKey, true);
297
            $outputKey .= $hmac;
298
        }
299
300
        if ($length !== 0) {
301
            $outputKey = StringHelper::byteSubstr($outputKey, 0, $length);
302
        }
303
        return $outputKey;
304
    }
305
306
    /**
307
     * Derives a key from the given password using the standard PBKDF2 algorithm.
308
     * Implements HKDF2 specified in [RFC 2898](http://tools.ietf.org/html/rfc2898#section-5.2)
309
     * Recommend use one of the SHA-2 hash algorithms: sha224, sha256, sha384 or sha512.
310
     * @param string $algo a hash algorithm supported by `hash_hmac()`, e.g. 'SHA-256'
311
     * @param string $password the source password
312
     * @param string $salt the random salt
313
     * @param int $iterations the number of iterations of the hash algorithm. Set as high as
314
     * possible to hinder dictionary password attacks.
315
     * @param int $length length of the output key in bytes. If 0, the output key is
316
     * the length of the hash algorithm output.
317
     * @return string the derived key
318
     * @throws InvalidArgumentException when hash generation fails due to invalid params given.
319
     */
320
    public function pbkdf2($algo, $password, $salt, $iterations, $length = 0)
321
    {
322
        $outputKey = hash_pbkdf2($algo, $password, $salt, $iterations, $length, true);
323
        if ($outputKey === false) {
324
            throw new InvalidArgumentException('Invalid parameters to hash_pbkdf2()');
325
        }
326
        return $outputKey;
327
    }
328
329 19
    /**
330
     * Prefixes data with a keyed hash value so that it can later be detected if it is tampered.
331 19
     * There is no need to hash inputs or outputs of [[encryptByKey()]] or [[encryptByPassword()]]
332 19
     * as those methods perform the task.
333 19
     * @param string $data the data to be protected
334
     * @param string $key the secret key to be used for generating hash. Should be a secure
335
     * cryptographic key.
336 19
     * @param bool $rawHash whether the generated hash value is in raw binary format. If false, lowercase
337
     * hex digits will be generated.
338
     * @return string the data prefixed with the keyed hash
339
     * @throws InvalidConfigException when HMAC generation fails.
340
     * @see validateData()
341
     * @see generateRandomKey()
342
     * @see hkdf()
343
     * @see pbkdf2()
344
     */
345
    public function hashData($data, $key, $rawHash = false)
346
    {
347
        $hash = hash_hmac($this->macHash, $data, $key, $rawHash);
348
        if (!$hash) {
349
            throw new InvalidConfigException('Failed to generate HMAC with hash algorithm: ' . $this->macHash);
350
        }
351
        return $hash . $data;
352
    }
353
354
    /**
355
     * Validates if the given data is tampered.
356
     * @param string $data the data to be validated. The data must be previously
357
     * generated by [[hashData()]].
358
     * @param string $key the secret key that was previously used to generate the hash for the data in [[hashData()]].
359
     * function to see the supported hashing algorithms on your system. This must be the same
360
     * as the value passed to [[hashData()]] when generating the hash for the data.
361
     * @param bool $rawHash this should take the same value as when you generate the data using [[hashData()]].
362
     * It indicates whether the hash value in the data is in binary format. If false, it means the hash value consists
363
     * of lowercase hex digits only.
364
     * hex digits will be generated.
365
     * @return string|false the real data with the hash stripped off. False if the data is tampered.
366
     * @throws InvalidConfigException when HMAC generation fails.
367
     * @see hashData()
368
     */
369
    public function validateData($data, $key, $rawHash = false)
370
    {
371
        $test = @hash_hmac($this->macHash, '', '', $rawHash);
372
        if (!$test) {
373
            throw new InvalidConfigException('Failed to generate HMAC with hash algorithm: ' . $this->macHash);
374
        }
375
        $hashLength = StringHelper::byteLength($test);
376
        if (StringHelper::byteLength($data) >= $hashLength) {
377
            $hash = StringHelper::byteSubstr($data, 0, $hashLength);
378
            $pureData = StringHelper::byteSubstr($data, $hashLength);
379
380
            $calculatedHash = hash_hmac($this->macHash, $pureData, $key, $rawHash);
381
382
            if ($this->compareString($hash, $calculatedHash)) {
383
                return $pureData;
384
            }
385
        }
386
        return false;
387
    }
388
389
    /**
390
     * Generates specified number of random bytes.
391
     * Note that output may not be ASCII.
392 3
     * @see generateRandomString() if you need a string.
393
     *
394 3
     * @param int $length the number of bytes to generate
395 3
     * @return string the generated random bytes
396
     * @throws InvalidArgumentException if wrong length is specified
397
     * @throws Exception on failure.
398 3
     */
399
    public function generateRandomKey($length = 32)
400
    {
401
        if (!is_int($length)) {
402
            throw new InvalidArgumentException('First parameter ($length) must be an integer');
403
        }
404
405
        if ($length < 1) {
406
            throw new InvalidArgumentException('First parameter ($length) must be greater than 0');
407
        }
408
409
        return random_bytes($length);
410
    }
411
412
    /**
413
     * Generates a random string of specified length.
414
     * The string generated matches [A-Za-z0-9_-]+ and is transparent to URL-encoding.
415
     *
416 21
     * @param int $length the length of the key in characters
417
     * @return string the generated random key
418 21
     * @throws Exception on failure.
419 21
     */
420
    public function generateRandomString($length = 32)
421
    {
422 21
        if (!is_int($length)) {
423 21
            throw new InvalidArgumentException('First parameter ($length) must be an integer');
424 21
        }
425 21
426
        if ($length < 1) {
427 21
            throw new InvalidArgumentException('First parameter ($length) must be greater than 0');
428
        }
429 21
430 21
        $bytes = $this->generateRandomKey($length);
431
        return substr(StringHelper::base64UrlEncode($bytes), 0, $length);
432
    }
433 3
434
    /**
435
     * Generates a secure hash from a password and a random salt.
436
     *
437
     * The generated hash can be stored in database.
438
     * Later when a password needs to be validated, the hash can be fetched and passed
439
     * to [[validatePassword()]]. For example,
440
     *
441
     * ```php
442
     * // generates the hash (usually done during user registration or when the password is changed)
443
     * $hash = Yii::$app->getSecurity()->generatePasswordHash($password);
444
     * // ...save $hash in database...
445
     *
446
     * // during login, validate if the password entered is correct using $hash fetched from database
447
     * if (Yii::$app->getSecurity()->validatePassword($password, $hash) {
448
     *     // password is good
449 67
     * } else {
450
     *     // password is bad
451 67
     * }
452 3
     * ```
453
     *
454
     * @param string $password The password to be hashed.
455 64
     * @param int $cost Cost parameter used by the Blowfish hash algorithm.
456 3
     * The higher the value of cost,
457
     * the longer it takes to generate the hash and to verify a password against it. Higher cost
458
     * therefore slows down a brute-force attack. For best protection against brute-force attacks,
459
     * set it to the highest value that is tolerable on production servers. The time taken to
460 61
     * compute the hash doubles for every increment by one of $cost.
461 57
     * @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt',
462
     * the output is always 60 ASCII characters, when set to 'password_hash' the output length
463
     * might increase in future versions of PHP (http://php.net/manual/en/function.password-hash.php)
464
     * @throws Exception on bad password parameter or cost parameter.
465
     * @see validatePassword()
466
     */
467 4
    public function generatePasswordHash($password, $cost = null)
468 4
    {
469 4
        if ($cost === null) {
470
            $cost = $this->passwordHashCost;
471
        }
472
473
        return password_hash($password, PASSWORD_DEFAULT, ['cost' => $cost]);
474
    }
475 4
476
    /**
477 4
     * Verifies a password against a hash.
478 4
     * @param string $password The password to verify.
479 4
     * @param string $hash The hash to verify the password against.
480
     * @return bool whether the password is correct.
481
     * @throws InvalidArgumentException on bad password/hash parameters or if crypt() with Blowfish hash is not
482
     * available.
483
     * @see generatePasswordHash()
484
     */
485
    public function validatePassword($password, $hash)
486
    {
487
        if (!is_string($password) || $password === '') {
488
            throw new InvalidArgumentException('Password must be a string and cannot be empty.');
489
        }
490
491
        return password_verify($password, $hash);
492
    }
493
494
    /**
495 4
     * Performs string comparison using timing attack resistant approach.
496
     *
497
     * @param string $expected string to compare.
498
     * @param string $actual user-supplied string.
499
     * @return bool whether strings are equal.
500
     */
501
    public function compareString($expected, $actual)
502
    {
503 4
        return hash_equals($expected, $actual);
504
    }
505 4
506
    /**
507
     * Masks a token to make it uncompressible.
508 4
     * Applies a random mask to the token and prepends the mask used to the result making the string always unique.
509 4
     * Used to mitigate BREACH attack by randomizing how token is outputted on each request.
510 4
     * @param string $token An unmasked token.
511
     * @return string A masked token.
512 4
     * @since 2.0.12
513
     */
514
    public function maskToken($token)
515
    {
516 3
        // The number of bytes in a mask is always equal to the number of bytes in a token.
517
        $mask = $this->generateRandomKey(StringHelper::byteLength($token));
518 3
        return StringHelper::base64UrlEncode($mask . ($mask ^ $token));
519 3
    }
520
521
    /**
522
     * Unmasks a token previously masked by `maskToken`.
523
     * @param string $maskedToken A masked token.
524
     * @return string An unmasked token, or an empty string in case of token format is invalid.
525 4
     * @since 2.0.12
526 3
     */
527 3
    public function unmaskToken($maskedToken)
528 3
    {
529 3
        $decoded = StringHelper::base64UrlDecode($maskedToken);
530 3
        $length = StringHelper::byteLength($decoded) / 2;
531 1
        // Check if the masked token has an even length.
532
        if (!is_int($length)) {
533 2
            return '';
534 2
        }
535 2
        return StringHelper::byteSubstr($decoded, $length, $length) ^ StringHelper::byteSubstr($decoded, 0, $length);
536
    }
537
}
538