Prevent a server crash when using together APC cache and Mcrypt

SVN:trunk[2498]
This commit is contained in:
Denis Flaven
2012-12-04 14:01:51 +00:00
parent 1e155ffc13
commit 442c0d6956
2 changed files with 26 additions and 3 deletions

View File

@@ -1609,6 +1609,22 @@ class AttributeEncryptedString extends AttributeString
self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
}
}
/**
* When the attribute definitions are stored in APC cache:
* 1) The static class variable $sKey is NOT serialized
* 2) The object's constructor is NOT called upon wakeup
* 3) mcrypt may crash the server if passed an empty key !!
*
* So let's restore the key (if needed) when waking up
**/
public function __wakeup()
{
if (self::$sKey == null)
{
self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
}
}
protected function GetSQLCol() {return "TINYBLOB";}

View File

@@ -220,9 +220,16 @@ class SimpleCryptMcryptEngine implements CryptEngine
{
$iv = substr($encrypted_data, 0, mcrypt_enc_get_iv_size($this->td));
$string = substr($encrypted_data, mcrypt_enc_get_iv_size($this->td));
mcrypt_generic_init($this->td, $key, $iv);
$decrypted_data = rtrim(mdecrypt_generic($this->td, $string), "\0");
mcrypt_generic_deinit($this->td);
$r = mcrypt_generic_init($this->td, $key, $iv);
if (($r < 0) || ($r === false))
{
$decrypted_data = '** decryption error **';
}
else
{
$decrypted_data = rtrim(mdecrypt_generic($this->td, $string), "\0");
mcrypt_generic_deinit($this->td);
}
return $decrypted_data;
}