2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Core Security
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v .0.10.0.1233
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('String', 'Utility');
|
2010-12-03 23:07:21 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Security Library contains utility methods related to security
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:28:54 +00:00
|
|
|
class Security {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Default hash method
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-24 01:28:54 +00:00
|
|
|
public static $hashType = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* Default cost
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $hashCost = '10';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Get allowed minutes of inactivity based on security level.
|
|
|
|
*
|
2013-01-22 08:52:23 +00:00
|
|
|
* @deprecated Exists for backwards compatibility only, not used by the core
|
2009-07-26 10:46:07 +00:00
|
|
|
* @return integer Allowed inactivity in minutes
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function inactiveMins() {
|
2008-10-23 00:10:44 +00:00
|
|
|
switch (Configure::read('Security.level')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'high':
|
|
|
|
return 10;
|
|
|
|
case 'medium':
|
|
|
|
return 100;
|
|
|
|
case 'low':
|
|
|
|
default:
|
|
|
|
return 300;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Generate authorization hash.
|
|
|
|
*
|
|
|
|
* @return string Hash
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function generateAuthKey() {
|
2008-09-24 23:05:51 +00:00
|
|
|
return Security::hash(String::uuid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Validate authorization hash.
|
|
|
|
*
|
|
|
|
* @param string $authKey Authorization hash
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public static function validateAuthKey($authKey) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-11-11 21:25:08 +00:00
|
|
|
* Create a hash from string using given method or fallback on next available method.
|
|
|
|
*
|
|
|
|
* #### Using Blowfish
|
|
|
|
*
|
|
|
|
* - Creating Hashes: *Do not supply a salt*. Cake handles salt creation for
|
|
|
|
* you ensuring that each hashed password will have a *unique* salt.
|
|
|
|
* - Comparing Hashes: Simply pass the originally hashed password as the salt.
|
|
|
|
* The salt is prepended to the hash and php handles the parsing automagically.
|
|
|
|
* For convenience the BlowfishAuthenticate adapter is available for use with
|
|
|
|
* the AuthComponent.
|
|
|
|
* - Do NOT use a constant salt for blowfish!
|
|
|
|
*
|
|
|
|
* Creating a blowfish/bcrypt hash:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $hash = Security::hash($password, 'blowfish');
|
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string String to hash
|
2012-11-11 21:25:08 +00:00
|
|
|
* @param string $type Method to use (sha1/sha256/md5/blowfish)
|
2013-08-12 10:52:34 +00:00
|
|
|
* @param mixed $salt If true, automatically prepends the application's salt
|
2012-07-21 16:48:14 +00:00
|
|
|
* value to $string (Security.salt). If you are using blowfish the salt
|
|
|
|
* must be false or a previously generated salt.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Hash
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function hash($string, $type = null, $salt = false) {
|
2012-07-21 16:48:14 +00:00
|
|
|
if (empty($type)) {
|
|
|
|
$type = self::$hashType;
|
|
|
|
}
|
|
|
|
$type = strtolower($type);
|
|
|
|
|
|
|
|
if ($type === 'blowfish') {
|
2012-11-01 13:15:52 +00:00
|
|
|
return self::_crypt($string, $salt);
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($salt) {
|
2012-11-01 13:15:52 +00:00
|
|
|
if (!is_string($salt)) {
|
|
|
|
$salt = Configure::read('Security.salt');
|
2008-10-02 00:03:52 +00:00
|
|
|
}
|
2012-11-01 13:15:52 +00:00
|
|
|
$string = $salt . $string;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if (!$type || $type === 'sha1') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (function_exists('sha1')) {
|
2012-11-01 13:15:52 +00:00
|
|
|
return sha1($string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
$type = 'sha256';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($type === 'sha256' && function_exists('mhash')) {
|
2008-08-02 14:44:42 +00:00
|
|
|
return bin2hex(mhash(MHASH_SHA256, $string));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 17:13:09 +00:00
|
|
|
if (function_exists('hash')) {
|
2008-08-02 14:44:42 +00:00
|
|
|
return hash($type, $string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
return md5($string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Sets the default hash method for the Security object. This affects all objects using
|
2008-05-30 11:40:08 +00:00
|
|
|
* Security::hash().
|
|
|
|
*
|
2012-07-21 16:48:14 +00:00
|
|
|
* @param string $hash Method to use (sha1/sha256/md5/blowfish)
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see Security::hash()
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function setHash($hash) {
|
2010-04-24 01:28:54 +00:00
|
|
|
self::$hashType = $hash;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* Sets the cost for they blowfish hash method.
|
|
|
|
*
|
|
|
|
* @param integer $cost Valid values are 4-31
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setCost($cost) {
|
2012-11-01 13:15:52 +00:00
|
|
|
if ($cost < 4 || $cost > 31) {
|
|
|
|
trigger_error(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'Invalid value, cost must be between %s and %s',
|
|
|
|
array(4, 31)
|
|
|
|
), E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
2012-07-21 16:48:14 +00:00
|
|
|
self::$hashCost = $cost;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-05-07 03:03:20 +00:00
|
|
|
* Runs $text through a XOR cipher.
|
|
|
|
*
|
|
|
|
* *Note* This is not a cryptographically strong method and should not be used
|
|
|
|
* for sensitive data. Additionally this method does *not* work in environments
|
|
|
|
* where suhosin is enabled.
|
|
|
|
*
|
|
|
|
* Instead you should use Security::rijndael() when you need strong
|
|
|
|
* encryption.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $text Encrypted string to decrypt, normal string to encrypt
|
|
|
|
* @param string $key Key to use
|
|
|
|
* @return string Encrypted/Decrypted string
|
2013-08-12 10:51:12 +00:00
|
|
|
* @deprecated Will be removed in 3.0.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function cipher($text, $key) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($key)) {
|
2013-08-16 09:39:38 +00:00
|
|
|
trigger_error(__d('cake_dev', 'You cannot use an empty key for %s', 'Security::cipher()'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2010-01-19 19:53:20 +00:00
|
|
|
srand(Configure::read('Security.cipherSeed'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = '';
|
2010-01-11 20:51:04 +00:00
|
|
|
$keyLength = strlen($key);
|
2010-03-27 21:19:42 +00:00
|
|
|
for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
|
|
|
|
$j = ord(substr($key, $i % $keyLength, 1));
|
|
|
|
while ($j--) {
|
2010-01-11 20:51:04 +00:00
|
|
|
rand(0, 255);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-07-05 15:03:46 +00:00
|
|
|
$mask = rand(0, 255);
|
2010-03-27 21:19:42 +00:00
|
|
|
$out .= chr(ord(substr($text, $i, 1)) ^ $mask);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-19 19:53:20 +00:00
|
|
|
srand();
|
2008-05-30 11:40:08 +00:00
|
|
|
return $out;
|
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
/**
|
|
|
|
* Encrypts/Decrypts a text using the given key using rijndael method.
|
|
|
|
*
|
2013-02-09 19:55:13 +00:00
|
|
|
* Prior to 2.3.1, a fixed initialization vector was used. This was not
|
|
|
|
* secure. This method now uses a random iv, and will silently upgrade values when
|
|
|
|
* they are re-encrypted.
|
|
|
|
*
|
2012-05-29 23:25:01 +00:00
|
|
|
* @param string $text Encrypted string to decrypt, normal string to encrypt
|
2013-02-09 19:14:27 +00:00
|
|
|
* @param string $key Key to use as the encryption key for encrypted data.
|
2012-05-29 23:25:01 +00:00
|
|
|
* @param string $operation Operation to perform, encrypt or decrypt
|
2013-03-05 07:05:14 +00:00
|
|
|
* @return string Encrypted/Decrypted string
|
2012-05-29 23:25:01 +00:00
|
|
|
*/
|
|
|
|
public static function rijndael($text, $key, $operation) {
|
|
|
|
if (empty($key)) {
|
2013-08-16 09:39:38 +00:00
|
|
|
trigger_error(__d('cake_dev', 'You cannot use an empty key for %s', 'Security::rijndael()'), E_USER_WARNING);
|
2012-05-29 23:25:01 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (empty($operation) || !in_array($operation, array('encrypt', 'decrypt'))) {
|
|
|
|
trigger_error(__d('cake_dev', 'You must specify the operation for Security::rijndael(), either encrypt or decrypt'), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (strlen($key) < 32) {
|
|
|
|
trigger_error(__d('cake_dev', 'You must use a key larger than 32 bytes for Security::rijndael()'), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
2013-02-09 19:14:27 +00:00
|
|
|
$algorithm = MCRYPT_RIJNDAEL_256;
|
|
|
|
$mode = MCRYPT_MODE_CBC;
|
2013-02-09 19:55:13 +00:00
|
|
|
$ivSize = mcrypt_get_iv_size($algorithm, $mode);
|
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
$cryptKey = substr($key, 0, 32);
|
2012-11-01 13:15:52 +00:00
|
|
|
|
2012-05-29 23:25:01 +00:00
|
|
|
if ($operation === 'encrypt') {
|
2013-02-09 19:55:13 +00:00
|
|
|
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
|
|
|
|
return $iv . '$$' . mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv);
|
|
|
|
}
|
|
|
|
// Backwards compatible decrypt with fixed iv
|
|
|
|
if (substr($text, $ivSize, 2) !== '$$') {
|
|
|
|
$iv = substr($key, strlen($key) - 32, 32);
|
|
|
|
return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
|
2012-05-29 23:25:01 +00:00
|
|
|
}
|
2013-02-09 19:55:13 +00:00
|
|
|
$iv = substr($text, 0, $ivSize);
|
|
|
|
$text = substr($text, $ivSize + 2);
|
2012-11-01 13:15:52 +00:00
|
|
|
return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
|
2012-05-29 23:25:01 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 16:48:14 +00:00
|
|
|
/**
|
|
|
|
* Generates a pseudo random salt suitable for use with php's crypt() function.
|
|
|
|
* The salt length should not exceed 27. The salt will be composed of
|
|
|
|
* [./0-9A-Za-z]{$length}.
|
|
|
|
*
|
|
|
|
* @param integer $length The length of the returned salt
|
|
|
|
* @return string The generated salt
|
|
|
|
*/
|
2012-11-11 21:25:08 +00:00
|
|
|
protected static function _salt($length = 22) {
|
2012-08-24 13:35:33 +00:00
|
|
|
$salt = str_replace(
|
|
|
|
array('+', '='),
|
|
|
|
'.',
|
|
|
|
base64_encode(sha1(uniqid(Configure::read('Security.salt'), true), true))
|
|
|
|
);
|
|
|
|
return substr($salt, 0, $length);
|
2012-07-21 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-11 21:25:08 +00:00
|
|
|
* One way encryption using php's crypt() function. To use blowfish hashing see ``Security::hash()``
|
2012-07-21 16:48:14 +00:00
|
|
|
*
|
|
|
|
* @param string $password The string to be encrypted.
|
|
|
|
* @param mixed $salt false to generate a new salt or an existing salt.
|
2012-11-11 21:25:08 +00:00
|
|
|
* @return string The hashed string or an empty string on error.
|
2012-07-21 16:48:14 +00:00
|
|
|
*/
|
2012-11-01 13:15:52 +00:00
|
|
|
protected static function _crypt($password, $salt = false) {
|
2012-07-21 16:48:14 +00:00
|
|
|
if ($salt === false) {
|
2012-11-11 21:25:08 +00:00
|
|
|
$salt = self::_salt(22);
|
2012-11-01 13:15:52 +00:00
|
|
|
$salt = vsprintf('$2a$%02d$%s', array(self::$hashCost, $salt));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($salt === true || strpos($salt, '$2a$') !== 0 || strlen($salt) < 29) {
|
2012-07-21 16:48:14 +00:00
|
|
|
trigger_error(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'Invalid salt: %s for %s Please visit http://www.php.net/crypt and read the appropriate section for building %s salts.',
|
2012-11-01 13:15:52 +00:00
|
|
|
array($salt, 'blowfish', 'blowfish')
|
2012-07-21 16:48:14 +00:00
|
|
|
), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return crypt($password, $salt);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|