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)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, 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
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
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.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
break;
|
|
|
|
case 'medium':
|
|
|
|
return 100;
|
|
|
|
break;
|
|
|
|
case 'low':
|
|
|
|
default:
|
|
|
|
return 300;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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
|
2008-09-25 16:49:56 +00:00
|
|
|
* @todo Complete implementation
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Create a hash from string using given method.
|
2008-07-07 17:13:09 +00:00
|
|
|
* Fallback on next available method.
|
2012-07-21 16:48:14 +00:00
|
|
|
* If you are using blowfish, for comparisons simply pass the originally hashed
|
|
|
|
* string as the salt (the salt is prepended to the hash and php handles the
|
|
|
|
* parsing automagically. Do NOT use a constant salt for blowfish.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string String to hash
|
|
|
|
* @param string $type Method to use (sha1/sha256/md5)
|
2012-07-21 16:48:14 +00:00
|
|
|
* @param mixed $salt If true, automatically appends the application's salt
|
|
|
|
* 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') {
|
|
|
|
return self::_crypt($string, $type, $salt);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($salt) {
|
2008-10-02 00:03:52 +00:00
|
|
|
if (is_string($salt)) {
|
|
|
|
$string = $salt . $string;
|
|
|
|
} else {
|
|
|
|
$string = Configure::read('Security.salt') . $string;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type == 'sha1' || $type == null) {
|
|
|
|
if (function_exists('sha1')) {
|
|
|
|
$return = sha1($string);
|
|
|
|
return $return;
|
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
$type = 'sha256';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 14:44:42 +00:00
|
|
|
if ($type == 'sha256' && function_exists('mhash')) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Sets the default hash method for the Security object. This affects all objects using
|
|
|
|
* 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) {
|
|
|
|
self::$hashCost = $cost;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-20 18:05:26 +00:00
|
|
|
* Encrypts/Decrypts a text using the given key.
|
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
|
|
|
|
*/
|
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)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'You cannot use an empty key for 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.
|
|
|
|
*
|
|
|
|
* @param string $text Encrypted string to decrypt, normal string to encrypt
|
|
|
|
* @param string $key Key to use
|
|
|
|
* @param string $operation Operation to perform, encrypt or decrypt
|
|
|
|
* @return string Encrypted/Descrypted string
|
|
|
|
*/
|
|
|
|
public static function rijndael($text, $key, $operation) {
|
|
|
|
if (empty($key)) {
|
|
|
|
trigger_error(__d('cake_dev', 'You cannot use an empty key for Security::rijndael()'), E_USER_WARNING);
|
|
|
|
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 '';
|
|
|
|
}
|
|
|
|
$algorithm = 'rijndael-256';
|
|
|
|
$mode = 'cbc';
|
|
|
|
$cryptKey = substr($key, 0, 32);
|
|
|
|
$iv = substr($key, strlen($key) - 32, 32);
|
|
|
|
$out = '';
|
|
|
|
if ($operation === 'encrypt') {
|
|
|
|
$out .= mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv);
|
|
|
|
} elseif ($operation === 'decrypt') {
|
|
|
|
$out .= rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static function salt($length = 22) {
|
|
|
|
return substr(str_replace('+', '.', base64_encode(sha1(uniqid(Configure::read('Security.salt'), true), true))), 0, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One way encryption using php's crypt() function.
|
|
|
|
*
|
|
|
|
* @param string $password The string to be encrypted.
|
|
|
|
* @param string $type The encryption method to use (blowfish)
|
|
|
|
* @param mixed $salt false to generate a new salt or an existing salt.
|
|
|
|
*/
|
|
|
|
protected static function _crypt($password, $type = null, $salt = false) {
|
|
|
|
$options = array(
|
|
|
|
'saltFormat' => array(
|
2012-07-23 01:28:49 +00:00
|
|
|
'blowfish' => '$2a$%02d$%s',
|
2012-07-21 16:48:14 +00:00
|
|
|
),
|
|
|
|
'saltLength' => array(
|
|
|
|
'blowfish' => 22,
|
|
|
|
),
|
|
|
|
'costLimits' => array(
|
|
|
|
'blowfish' => array(4, 31),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
extract($options);
|
|
|
|
if ($type === null) {
|
|
|
|
$hashType = self::$hashType;
|
|
|
|
} else {
|
|
|
|
$hashType = $type;
|
|
|
|
}
|
|
|
|
$cost = self::$hashCost;
|
|
|
|
if ($salt === false) {
|
|
|
|
if (isset($costLimits[$hashType]) && ($cost < $costLimits[$hashType][0] || $cost > $costLimits[$hashType][1])) {
|
|
|
|
trigger_error(__d(
|
|
|
|
'cake_dev',
|
|
|
|
'When using %s you must specify a cost between %s and %s',
|
|
|
|
array(
|
|
|
|
$hashType,
|
|
|
|
$costLimits[$hashType][0],
|
|
|
|
$costLimits[$hashType][1]
|
|
|
|
)
|
|
|
|
), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$salt = self::salt($saltLength[$hashType]);
|
2012-07-23 01:28:49 +00:00
|
|
|
$vspArgs = array(
|
|
|
|
$cost,
|
|
|
|
$salt,
|
|
|
|
);
|
2012-07-21 16:48:14 +00:00
|
|
|
$salt = vsprintf($saltFormat[$hashType], $vspArgs);
|
|
|
|
} elseif ($salt === true || strpos($salt, '$2a$') !== 0 || strlen($salt) < 29) {
|
|
|
|
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.',
|
|
|
|
array($salt, $hashType, $hashType)
|
|
|
|
), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return crypt($password, $salt);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|