@@ -9,69 +9,69 @@ |
||
| 9 | 9 | |
| 10 | 10 | class CookieStoreTest extends AbstractTest |
| 11 | 11 | { |
| 12 | - protected function getStore() |
|
| 13 | - { |
|
| 14 | - $store = Injector::inst()->get(CookieStore::class); |
|
| 15 | - $store->setKey(uniqid()); |
|
| 16 | - $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
|
| 12 | + protected function getStore() |
|
| 13 | + { |
|
| 14 | + $store = Injector::inst()->get(CookieStore::class); |
|
| 15 | + $store->setKey(uniqid()); |
|
| 16 | + $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
|
| 17 | 17 | |
| 18 | - return $store; |
|
| 19 | - } |
|
| 18 | + return $store; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function testStoreLargeData() |
|
| 22 | - { |
|
| 23 | - $session = uniqid(); |
|
| 24 | - $store = $this->getStore(); |
|
| 21 | + public function testStoreLargeData() |
|
| 22 | + { |
|
| 23 | + $session = uniqid(); |
|
| 24 | + $store = $this->getStore(); |
|
| 25 | 25 | |
| 26 | - // Test new session is blank |
|
| 27 | - $result = $store->read($session); |
|
| 28 | - $this->assertEmpty($result); |
|
| 26 | + // Test new session is blank |
|
| 27 | + $result = $store->read($session); |
|
| 28 | + $this->assertEmpty($result); |
|
| 29 | 29 | |
| 30 | - // Save data against session |
|
| 31 | - $data1 = array( |
|
| 32 | - 'Large' => str_repeat('A', 600), |
|
| 33 | - 'Content' => str_repeat('B', 600) |
|
| 34 | - ); |
|
| 35 | - $store->write($session, serialize($data1)); |
|
| 36 | - $result = $store->read($session); |
|
| 30 | + // Save data against session |
|
| 31 | + $data1 = array( |
|
| 32 | + 'Large' => str_repeat('A', 600), |
|
| 33 | + 'Content' => str_repeat('B', 600) |
|
| 34 | + ); |
|
| 35 | + $store->write($session, serialize($data1)); |
|
| 36 | + $result = $store->read($session); |
|
| 37 | 37 | |
| 38 | - // Cookies should not try to store data that large |
|
| 39 | - $this->assertEmpty($result); |
|
| 40 | - } |
|
| 38 | + // Cookies should not try to store data that large |
|
| 39 | + $this->assertEmpty($result); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Ensure that subsequent reads without the necessary write do not report data |
|
| 44 | - */ |
|
| 45 | - public function testReadInvalidatesData() |
|
| 46 | - { |
|
| 47 | - $session = uniqid(); |
|
| 48 | - $store = $this->getStore(); |
|
| 42 | + /** |
|
| 43 | + * Ensure that subsequent reads without the necessary write do not report data |
|
| 44 | + */ |
|
| 45 | + public function testReadInvalidatesData() |
|
| 46 | + { |
|
| 47 | + $session = uniqid(); |
|
| 48 | + $store = $this->getStore(); |
|
| 49 | 49 | |
| 50 | - // Test new session is blank |
|
| 51 | - $result = $store->read($session); |
|
| 52 | - $this->assertEmpty($result); |
|
| 50 | + // Test new session is blank |
|
| 51 | + $result = $store->read($session); |
|
| 52 | + $this->assertEmpty($result); |
|
| 53 | 53 | |
| 54 | - // Save data against session |
|
| 55 | - $data1 = array( |
|
| 56 | - 'Color' => 'red', |
|
| 57 | - 'Animal' => 'elephant' |
|
| 58 | - ); |
|
| 59 | - $store->write($session, serialize($data1)); |
|
| 60 | - $result = $store->read($session); |
|
| 61 | - $this->assertEquals($data1, unserialize($result)); |
|
| 54 | + // Save data against session |
|
| 55 | + $data1 = array( |
|
| 56 | + 'Color' => 'red', |
|
| 57 | + 'Animal' => 'elephant' |
|
| 58 | + ); |
|
| 59 | + $store->write($session, serialize($data1)); |
|
| 60 | + $result = $store->read($session); |
|
| 61 | + $this->assertEquals($data1, unserialize($result)); |
|
| 62 | 62 | |
| 63 | - // Since we have read the data into the result, the application could modify this content |
|
| 64 | - // and be unable to write it back due to headers being sent. We should thus assume |
|
| 65 | - // that subsequent reads without a successful write do not purport to have valid data |
|
| 66 | - $data1['Color'] = 'blue'; |
|
| 67 | - $result = $store->read($session); |
|
| 68 | - $this->assertEmpty($result); |
|
| 63 | + // Since we have read the data into the result, the application could modify this content |
|
| 64 | + // and be unable to write it back due to headers being sent. We should thus assume |
|
| 65 | + // that subsequent reads without a successful write do not purport to have valid data |
|
| 66 | + $data1['Color'] = 'blue'; |
|
| 67 | + $result = $store->read($session); |
|
| 68 | + $this->assertEmpty($result); |
|
| 69 | 69 | |
| 70 | - // Check that writing to cookie fails after headers are sent and these results remain |
|
| 71 | - // invalidated |
|
| 72 | - TestCookieStore::$override_headers_sent = true; |
|
| 73 | - $store->write($session, serialize($data1)); |
|
| 74 | - $result = $store->read($session); |
|
| 75 | - $this->assertEmpty($result); |
|
| 76 | - } |
|
| 70 | + // Check that writing to cookie fails after headers are sent and these results remain |
|
| 71 | + // invalidated |
|
| 72 | + TestCookieStore::$override_headers_sent = true; |
|
| 73 | + $store->write($session, serialize($data1)); |
|
| 74 | + $result = $store->read($session); |
|
| 75 | + $this->assertEmpty($result); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | { |
| 14 | 14 | $store = Injector::inst()->get(CookieStore::class); |
| 15 | 15 | $store->setKey(uniqid()); |
| 16 | - $store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
|
| 16 | + $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE'); |
|
| 17 | 17 | |
| 18 | 18 | return $store; |
| 19 | 19 | } |
@@ -8,109 +8,109 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class McryptCrypto implements CryptoHandler |
| 10 | 10 | { |
| 11 | - protected $key; |
|
| 12 | - |
|
| 13 | - protected $ivSize; |
|
| 14 | - |
|
| 15 | - protected $keySize; |
|
| 16 | - |
|
| 17 | - protected $salt; |
|
| 18 | - |
|
| 19 | - protected $saltedKey; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - public function getKey() |
|
| 25 | - { |
|
| 26 | - return $this->key; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function getSalt() |
|
| 33 | - { |
|
| 34 | - return $this->salt; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param $key a per-site secret string which is used as the base encryption key. |
|
| 39 | - * @param $salt a per-session random string which is used as a salt to generate a per-session key |
|
| 40 | - * |
|
| 41 | - * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session, |
|
| 42 | - * and even modify & re-sign it. |
|
| 43 | - * |
|
| 44 | - * The salt is a random per-session string that is used with the base encryption key to create a per-session key. |
|
| 45 | - * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key. |
|
| 46 | - * |
|
| 47 | - * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't |
|
| 48 | - * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes |
|
| 49 | - * a great salt, so no need to generate & handle another one. |
|
| 50 | - */ |
|
| 51 | - public function __construct($key, $salt) |
|
| 52 | - { |
|
| 53 | - $this->ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); |
|
| 54 | - $this->keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); |
|
| 55 | - |
|
| 56 | - $this->key = $key; |
|
| 57 | - $this->salt = $salt; |
|
| 58 | - $this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, $this->keySize, true); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Encrypt and then sign some cleartext |
|
| 63 | - * |
|
| 64 | - * @param $cleartext - The cleartext to encrypt and sign |
|
| 65 | - * @return string - The encrypted-and-signed message as base64 ASCII. |
|
| 66 | - */ |
|
| 67 | - public function encrypt($cleartext) |
|
| 68 | - { |
|
| 69 | - $iv = mcrypt_create_iv($this->ivSize, MCRYPT_DEV_URANDOM); |
|
| 70 | - |
|
| 71 | - $enc = mcrypt_encrypt( |
|
| 72 | - MCRYPT_RIJNDAEL_256, |
|
| 73 | - $this->saltedKey, |
|
| 74 | - $cleartext, |
|
| 75 | - MCRYPT_MODE_CBC, |
|
| 76 | - $iv |
|
| 77 | - ); |
|
| 78 | - |
|
| 79 | - $hash = hash_hmac('sha256', $enc, $this->saltedKey); |
|
| 80 | - |
|
| 81 | - return base64_encode($iv . $hash . $enc); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Check the signature on an encrypted-and-signed message, and if valid |
|
| 86 | - * decrypt the content |
|
| 87 | - * |
|
| 88 | - * @param $data - The encrypted-and-signed message as base64 ASCII |
|
| 89 | - * |
|
| 90 | - * @return bool|string - The decrypted cleartext or false if signature failed |
|
| 91 | - */ |
|
| 92 | - public function decrypt($data) |
|
| 93 | - { |
|
| 94 | - $data = base64_decode($data); |
|
| 95 | - |
|
| 96 | - $iv = substr($data, 0, $this->ivSize); |
|
| 97 | - $hash = substr($data, $this->ivSize, 64); |
|
| 98 | - $enc = substr($data, $this->ivSize + 64); |
|
| 99 | - |
|
| 100 | - $cleartext = rtrim(mcrypt_decrypt( |
|
| 101 | - MCRYPT_RIJNDAEL_256, |
|
| 102 | - $this->saltedKey, |
|
| 103 | - $enc, |
|
| 104 | - MCRYPT_MODE_CBC, |
|
| 105 | - $iv |
|
| 106 | - ), "\x00"); |
|
| 107 | - |
|
| 108 | - // Needs to be after decrypt so it always runs, to avoid timing attack |
|
| 109 | - $gen_hash = hash_hmac('sha256', $enc, $this->saltedKey); |
|
| 110 | - |
|
| 111 | - if ($gen_hash == $hash) { |
|
| 112 | - return $cleartext; |
|
| 113 | - } |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 11 | + protected $key; |
|
| 12 | + |
|
| 13 | + protected $ivSize; |
|
| 14 | + |
|
| 15 | + protected $keySize; |
|
| 16 | + |
|
| 17 | + protected $salt; |
|
| 18 | + |
|
| 19 | + protected $saltedKey; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + public function getKey() |
|
| 25 | + { |
|
| 26 | + return $this->key; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function getSalt() |
|
| 33 | + { |
|
| 34 | + return $this->salt; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param $key a per-site secret string which is used as the base encryption key. |
|
| 39 | + * @param $salt a per-session random string which is used as a salt to generate a per-session key |
|
| 40 | + * |
|
| 41 | + * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session, |
|
| 42 | + * and even modify & re-sign it. |
|
| 43 | + * |
|
| 44 | + * The salt is a random per-session string that is used with the base encryption key to create a per-session key. |
|
| 45 | + * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key. |
|
| 46 | + * |
|
| 47 | + * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't |
|
| 48 | + * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes |
|
| 49 | + * a great salt, so no need to generate & handle another one. |
|
| 50 | + */ |
|
| 51 | + public function __construct($key, $salt) |
|
| 52 | + { |
|
| 53 | + $this->ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); |
|
| 54 | + $this->keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); |
|
| 55 | + |
|
| 56 | + $this->key = $key; |
|
| 57 | + $this->salt = $salt; |
|
| 58 | + $this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, $this->keySize, true); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Encrypt and then sign some cleartext |
|
| 63 | + * |
|
| 64 | + * @param $cleartext - The cleartext to encrypt and sign |
|
| 65 | + * @return string - The encrypted-and-signed message as base64 ASCII. |
|
| 66 | + */ |
|
| 67 | + public function encrypt($cleartext) |
|
| 68 | + { |
|
| 69 | + $iv = mcrypt_create_iv($this->ivSize, MCRYPT_DEV_URANDOM); |
|
| 70 | + |
|
| 71 | + $enc = mcrypt_encrypt( |
|
| 72 | + MCRYPT_RIJNDAEL_256, |
|
| 73 | + $this->saltedKey, |
|
| 74 | + $cleartext, |
|
| 75 | + MCRYPT_MODE_CBC, |
|
| 76 | + $iv |
|
| 77 | + ); |
|
| 78 | + |
|
| 79 | + $hash = hash_hmac('sha256', $enc, $this->saltedKey); |
|
| 80 | + |
|
| 81 | + return base64_encode($iv . $hash . $enc); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Check the signature on an encrypted-and-signed message, and if valid |
|
| 86 | + * decrypt the content |
|
| 87 | + * |
|
| 88 | + * @param $data - The encrypted-and-signed message as base64 ASCII |
|
| 89 | + * |
|
| 90 | + * @return bool|string - The decrypted cleartext or false if signature failed |
|
| 91 | + */ |
|
| 92 | + public function decrypt($data) |
|
| 93 | + { |
|
| 94 | + $data = base64_decode($data); |
|
| 95 | + |
|
| 96 | + $iv = substr($data, 0, $this->ivSize); |
|
| 97 | + $hash = substr($data, $this->ivSize, 64); |
|
| 98 | + $enc = substr($data, $this->ivSize + 64); |
|
| 99 | + |
|
| 100 | + $cleartext = rtrim(mcrypt_decrypt( |
|
| 101 | + MCRYPT_RIJNDAEL_256, |
|
| 102 | + $this->saltedKey, |
|
| 103 | + $enc, |
|
| 104 | + MCRYPT_MODE_CBC, |
|
| 105 | + $iv |
|
| 106 | + ), "\x00"); |
|
| 107 | + |
|
| 108 | + // Needs to be after decrypt so it always runs, to avoid timing attack |
|
| 109 | + $gen_hash = hash_hmac('sha256', $enc, $this->saltedKey); |
|
| 110 | + |
|
| 111 | + if ($gen_hash == $hash) { |
|
| 112 | + return $cleartext; |
|
| 113 | + } |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | 116 | } |
@@ -78,7 +78,7 @@ |
||
| 78 | 78 | |
| 79 | 79 | $hash = hash_hmac('sha256', $enc, $this->saltedKey); |
| 80 | 80 | |
| 81 | - return base64_encode($iv . $hash . $enc); |
|
| 81 | + return base64_encode($iv.$hash.$enc); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -8,90 +8,90 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class OpenSSLCrypto implements CryptoHandler |
| 10 | 10 | { |
| 11 | - protected $key; |
|
| 11 | + protected $key; |
|
| 12 | 12 | |
| 13 | - protected $salt; |
|
| 13 | + protected $salt; |
|
| 14 | 14 | |
| 15 | - protected $saltedKey; |
|
| 15 | + protected $saltedKey; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return string |
|
| 19 | - */ |
|
| 20 | - public function getKey() |
|
| 21 | - { |
|
| 22 | - return $this->key; |
|
| 23 | - } |
|
| 17 | + /** |
|
| 18 | + * @return string |
|
| 19 | + */ |
|
| 20 | + public function getKey() |
|
| 21 | + { |
|
| 22 | + return $this->key; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @return string |
|
| 27 | - */ |
|
| 28 | - public function getSalt() |
|
| 29 | - { |
|
| 30 | - return $this->salt; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * @return string |
|
| 27 | + */ |
|
| 28 | + public function getSalt() |
|
| 29 | + { |
|
| 30 | + return $this->salt; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param string $key a per-site secret string which is used as the base encryption key. |
|
| 35 | - * @param string $salt a per-session random string which is used as a salt to generate a per-session key |
|
| 36 | - * |
|
| 37 | - * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session, |
|
| 38 | - * and even modify & re-sign it. |
|
| 39 | - * |
|
| 40 | - * The salt is a random per-session string that is used with the base encryption key to create a per-session key. |
|
| 41 | - * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key. |
|
| 42 | - * |
|
| 43 | - * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't |
|
| 44 | - * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes |
|
| 45 | - * a great salt, so no need to generate & handle another one. |
|
| 46 | - */ |
|
| 47 | - public function __construct($key, $salt) |
|
| 48 | - { |
|
| 49 | - $this->key = $key; |
|
| 50 | - $this->salt = $salt; |
|
| 51 | - $this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, 0, true); |
|
| 52 | - } |
|
| 33 | + /** |
|
| 34 | + * @param string $key a per-site secret string which is used as the base encryption key. |
|
| 35 | + * @param string $salt a per-session random string which is used as a salt to generate a per-session key |
|
| 36 | + * |
|
| 37 | + * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session, |
|
| 38 | + * and even modify & re-sign it. |
|
| 39 | + * |
|
| 40 | + * The salt is a random per-session string that is used with the base encryption key to create a per-session key. |
|
| 41 | + * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key. |
|
| 42 | + * |
|
| 43 | + * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't |
|
| 44 | + * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes |
|
| 45 | + * a great salt, so no need to generate & handle another one. |
|
| 46 | + */ |
|
| 47 | + public function __construct($key, $salt) |
|
| 48 | + { |
|
| 49 | + $this->key = $key; |
|
| 50 | + $this->salt = $salt; |
|
| 51 | + $this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, 0, true); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Encrypt and then sign some cleartext |
|
| 56 | - * |
|
| 57 | - * @param string $cleartext - The cleartext to encrypt and sign |
|
| 58 | - * @return string - The encrypted-and-signed message as base64 ASCII. |
|
| 59 | - */ |
|
| 60 | - public function encrypt($cleartext) |
|
| 61 | - { |
|
| 62 | - $cipher = "AES-256-CBC"; |
|
| 63 | - $ivlen = openssl_cipher_iv_length($cipher); |
|
| 64 | - $iv = openssl_random_pseudo_bytes($ivlen); |
|
| 65 | - $ciphertext_raw = openssl_encrypt($cleartext, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv); |
|
| 66 | - $hmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true); |
|
| 67 | - $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw); |
|
| 54 | + /** |
|
| 55 | + * Encrypt and then sign some cleartext |
|
| 56 | + * |
|
| 57 | + * @param string $cleartext - The cleartext to encrypt and sign |
|
| 58 | + * @return string - The encrypted-and-signed message as base64 ASCII. |
|
| 59 | + */ |
|
| 60 | + public function encrypt($cleartext) |
|
| 61 | + { |
|
| 62 | + $cipher = "AES-256-CBC"; |
|
| 63 | + $ivlen = openssl_cipher_iv_length($cipher); |
|
| 64 | + $iv = openssl_random_pseudo_bytes($ivlen); |
|
| 65 | + $ciphertext_raw = openssl_encrypt($cleartext, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv); |
|
| 66 | + $hmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true); |
|
| 67 | + $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw); |
|
| 68 | 68 | |
| 69 | - return base64_encode($iv . $hmac . $ciphertext_raw); |
|
| 70 | - } |
|
| 69 | + return base64_encode($iv . $hmac . $ciphertext_raw); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Check the signature on an encrypted-and-signed message, and if valid |
|
| 74 | - * decrypt the content |
|
| 75 | - * |
|
| 76 | - * @param string $data - The encrypted-and-signed message as base64 ASCII |
|
| 77 | - * |
|
| 78 | - * @return bool|string - The decrypted cleartext or false if signature failed |
|
| 79 | - */ |
|
| 80 | - public function decrypt($data) |
|
| 81 | - { |
|
| 82 | - $c = base64_decode($data); |
|
| 83 | - $cipher = "AES-256-CBC"; |
|
| 84 | - $ivlen = openssl_cipher_iv_length($cipher); |
|
| 85 | - $iv = substr($c, 0, $ivlen); |
|
| 86 | - $hmac = substr($c, $ivlen, $sha2len = 32); |
|
| 87 | - $ciphertext_raw = substr($c, $ivlen + $sha2len); |
|
| 88 | - $cleartext = openssl_decrypt($ciphertext_raw, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv); |
|
| 89 | - $calcmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true); |
|
| 72 | + /** |
|
| 73 | + * Check the signature on an encrypted-and-signed message, and if valid |
|
| 74 | + * decrypt the content |
|
| 75 | + * |
|
| 76 | + * @param string $data - The encrypted-and-signed message as base64 ASCII |
|
| 77 | + * |
|
| 78 | + * @return bool|string - The decrypted cleartext or false if signature failed |
|
| 79 | + */ |
|
| 80 | + public function decrypt($data) |
|
| 81 | + { |
|
| 82 | + $c = base64_decode($data); |
|
| 83 | + $cipher = "AES-256-CBC"; |
|
| 84 | + $ivlen = openssl_cipher_iv_length($cipher); |
|
| 85 | + $iv = substr($c, 0, $ivlen); |
|
| 86 | + $hmac = substr($c, $ivlen, $sha2len = 32); |
|
| 87 | + $ciphertext_raw = substr($c, $ivlen + $sha2len); |
|
| 88 | + $cleartext = openssl_decrypt($ciphertext_raw, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv); |
|
| 89 | + $calcmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true); |
|
| 90 | 90 | |
| 91 | - if (hash_equals($hmac, $calcmac)) { |
|
| 92 | - return $cleartext; |
|
| 93 | - } |
|
| 91 | + if (hash_equals($hmac, $calcmac)) { |
|
| 92 | + return $cleartext; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - return false; |
|
| 96 | - } |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | 97 | } |
@@ -64,9 +64,9 @@ |
||
| 64 | 64 | $iv = openssl_random_pseudo_bytes($ivlen); |
| 65 | 65 | $ciphertext_raw = openssl_encrypt($cleartext, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv); |
| 66 | 66 | $hmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true); |
| 67 | - $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw); |
|
| 67 | + $ciphertext = base64_encode($iv.$hmac.$ciphertext_raw); |
|
| 68 | 68 | |
| 69 | - return base64_encode($iv . $hmac . $ciphertext_raw); |
|
| 69 | + return base64_encode($iv.$hmac.$ciphertext_raw); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -22,184 +22,184 @@ |
||
| 22 | 22 | class CookieStore extends BaseStore |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Maximum length of a cookie value in characters |
|
| 27 | - * |
|
| 28 | - * @var int |
|
| 29 | - * @config |
|
| 30 | - */ |
|
| 31 | - private static $max_length = 1024; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Encryption service |
|
| 35 | - * |
|
| 36 | - * @var HybridSessionStore_Crypto |
|
| 37 | - */ |
|
| 38 | - protected $crypto; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Name of cookie |
|
| 42 | - * |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - protected $cookie; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Known unmodified value of this cookie. If the cookie backend has been read into the application, |
|
| 49 | - * then the backend is unable to verify the modification state of this value internally within the |
|
| 50 | - * system, so this will be left null unless written back. |
|
| 51 | - * |
|
| 52 | - * If the content exceeds max_length then the backend can also not maintain this cookie, also |
|
| 53 | - * setting this variable to null. |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - protected $currentCookieData; |
|
| 58 | - |
|
| 59 | - public function open($save_path, $name) |
|
| 60 | - { |
|
| 61 | - $this->cookie = $name . '_2'; |
|
| 62 | - |
|
| 63 | - // Read the incoming value, then clear the cookie - we might not be able |
|
| 64 | - // to do so later if write() is called after headers are sent |
|
| 65 | - // This is intended to force a failover to the database store if the |
|
| 66 | - // modified session cannot be emitted. |
|
| 67 | - $this->currentCookieData = Cookie::get($this->cookie); |
|
| 68 | - |
|
| 69 | - if ($this->currentCookieData) { |
|
| 70 | - Cookie::set($this->cookie, ''); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function close() |
|
| 75 | - { |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get the cryptography store for the specified session |
|
| 80 | - * |
|
| 81 | - * @param string $session_id |
|
| 82 | - * @return HybridSessionStore_Crypto |
|
| 83 | - */ |
|
| 84 | - protected function getCrypto($session_id) |
|
| 85 | - { |
|
| 86 | - $key = $this->getKey(); |
|
| 87 | - |
|
| 88 | - if (!$key) { |
|
| 89 | - return null; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - if (!$this->crypto || $this->crypto->getSalt() != $session_id) { |
|
| 93 | - $this->crypto = Injector::inst()->create(CryptoHandler::class, $key, $session_id); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return $this->crypto; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function read($session_id) |
|
| 100 | - { |
|
| 101 | - // Check ability to safely decrypt content |
|
| 102 | - if (!$this->currentCookieData |
|
| 103 | - || !($crypto = $this->getCrypto($session_id)) |
|
| 104 | - ) { |
|
| 105 | - return; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Decrypt and invalidate old data |
|
| 109 | - $cookieData = $crypto->decrypt($this->currentCookieData); |
|
| 110 | - $this->currentCookieData = null; |
|
| 111 | - |
|
| 112 | - // Verify expiration |
|
| 113 | - if ($cookieData) { |
|
| 114 | - $expiry = (int)substr($cookieData, 0, 10); |
|
| 115 | - $data = substr($cookieData, 10); |
|
| 116 | - |
|
| 117 | - if ($expiry > $this->getNow()) { |
|
| 118 | - return $data; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Determine if the session could be verifably written to cookie storage |
|
| 125 | - * |
|
| 126 | - * @return bool |
|
| 127 | - */ |
|
| 128 | - protected function canWrite() |
|
| 129 | - { |
|
| 130 | - return !headers_sent(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function write($session_id, $session_data) |
|
| 134 | - { |
|
| 135 | - $canWrite = $this->canWrite(); |
|
| 136 | - $isExceedingCookieLimit = (strlen($session_data) > static::config()->get('max_length')); |
|
| 137 | - $crypto = $this->getCrypto($session_id); |
|
| 138 | - |
|
| 139 | - // Check ability to safely encrypt and write content |
|
| 140 | - if (!$canWrite || $isExceedingCookieLimit || !$crypto) { |
|
| 141 | - if ($canWrite && $isExceedingCookieLimit) { |
|
| 142 | - $params = session_get_cookie_params(); |
|
| 143 | - // Clear stored cookie value and cookie when length exceeds the set limit |
|
| 144 | - $this->currentCookieData = null; |
|
| 145 | - Cookie::set( |
|
| 146 | - $this->cookie, |
|
| 147 | - '', |
|
| 148 | - 0, |
|
| 149 | - $params['path'], |
|
| 150 | - $params['domain'], |
|
| 151 | - $params['secure'], |
|
| 152 | - $params['httponly'] |
|
| 153 | - ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Prepare content for write |
|
| 160 | - $params = session_get_cookie_params(); |
|
| 161 | - // Total max lifetime, stored internally |
|
| 162 | - $lifetime = $this->getLifetime(); |
|
| 163 | - $expiry = $this->getNow() + $lifetime; |
|
| 164 | - |
|
| 165 | - // Restore the known good cookie value |
|
| 166 | - $this->currentCookieData = $this->crypto->encrypt( |
|
| 167 | - sprintf('%010u', $expiry) . $session_data |
|
| 168 | - ); |
|
| 169 | - |
|
| 170 | - // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
|
| 171 | - $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 172 | - |
|
| 173 | - Cookie::set( |
|
| 174 | - $this->cookie, |
|
| 175 | - $this->currentCookieData, |
|
| 176 | - $cookieLifetime / 86400, |
|
| 177 | - $params['path'], |
|
| 178 | - $params['domain'], |
|
| 179 | - $params['secure'], |
|
| 180 | - $params['httponly'] |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - return true; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - public function destroy($session_id) |
|
| 187 | - { |
|
| 188 | - $this->currentCookieData = null; |
|
| 189 | - |
|
| 190 | - $params = session_get_cookie_params(); |
|
| 191 | - |
|
| 192 | - Cookie::force_expiry( |
|
| 193 | - $this->cookie, |
|
| 194 | - $params['path'], |
|
| 195 | - $params['domain'], |
|
| 196 | - $params['secure'], |
|
| 197 | - $params['httponly'] |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - public function gc($maxlifetime) |
|
| 202 | - { |
|
| 203 | - // NOP |
|
| 204 | - } |
|
| 25 | + /** |
|
| 26 | + * Maximum length of a cookie value in characters |
|
| 27 | + * |
|
| 28 | + * @var int |
|
| 29 | + * @config |
|
| 30 | + */ |
|
| 31 | + private static $max_length = 1024; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Encryption service |
|
| 35 | + * |
|
| 36 | + * @var HybridSessionStore_Crypto |
|
| 37 | + */ |
|
| 38 | + protected $crypto; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Name of cookie |
|
| 42 | + * |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + protected $cookie; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Known unmodified value of this cookie. If the cookie backend has been read into the application, |
|
| 49 | + * then the backend is unable to verify the modification state of this value internally within the |
|
| 50 | + * system, so this will be left null unless written back. |
|
| 51 | + * |
|
| 52 | + * If the content exceeds max_length then the backend can also not maintain this cookie, also |
|
| 53 | + * setting this variable to null. |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + protected $currentCookieData; |
|
| 58 | + |
|
| 59 | + public function open($save_path, $name) |
|
| 60 | + { |
|
| 61 | + $this->cookie = $name . '_2'; |
|
| 62 | + |
|
| 63 | + // Read the incoming value, then clear the cookie - we might not be able |
|
| 64 | + // to do so later if write() is called after headers are sent |
|
| 65 | + // This is intended to force a failover to the database store if the |
|
| 66 | + // modified session cannot be emitted. |
|
| 67 | + $this->currentCookieData = Cookie::get($this->cookie); |
|
| 68 | + |
|
| 69 | + if ($this->currentCookieData) { |
|
| 70 | + Cookie::set($this->cookie, ''); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function close() |
|
| 75 | + { |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get the cryptography store for the specified session |
|
| 80 | + * |
|
| 81 | + * @param string $session_id |
|
| 82 | + * @return HybridSessionStore_Crypto |
|
| 83 | + */ |
|
| 84 | + protected function getCrypto($session_id) |
|
| 85 | + { |
|
| 86 | + $key = $this->getKey(); |
|
| 87 | + |
|
| 88 | + if (!$key) { |
|
| 89 | + return null; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + if (!$this->crypto || $this->crypto->getSalt() != $session_id) { |
|
| 93 | + $this->crypto = Injector::inst()->create(CryptoHandler::class, $key, $session_id); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return $this->crypto; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function read($session_id) |
|
| 100 | + { |
|
| 101 | + // Check ability to safely decrypt content |
|
| 102 | + if (!$this->currentCookieData |
|
| 103 | + || !($crypto = $this->getCrypto($session_id)) |
|
| 104 | + ) { |
|
| 105 | + return; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Decrypt and invalidate old data |
|
| 109 | + $cookieData = $crypto->decrypt($this->currentCookieData); |
|
| 110 | + $this->currentCookieData = null; |
|
| 111 | + |
|
| 112 | + // Verify expiration |
|
| 113 | + if ($cookieData) { |
|
| 114 | + $expiry = (int)substr($cookieData, 0, 10); |
|
| 115 | + $data = substr($cookieData, 10); |
|
| 116 | + |
|
| 117 | + if ($expiry > $this->getNow()) { |
|
| 118 | + return $data; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Determine if the session could be verifably written to cookie storage |
|
| 125 | + * |
|
| 126 | + * @return bool |
|
| 127 | + */ |
|
| 128 | + protected function canWrite() |
|
| 129 | + { |
|
| 130 | + return !headers_sent(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function write($session_id, $session_data) |
|
| 134 | + { |
|
| 135 | + $canWrite = $this->canWrite(); |
|
| 136 | + $isExceedingCookieLimit = (strlen($session_data) > static::config()->get('max_length')); |
|
| 137 | + $crypto = $this->getCrypto($session_id); |
|
| 138 | + |
|
| 139 | + // Check ability to safely encrypt and write content |
|
| 140 | + if (!$canWrite || $isExceedingCookieLimit || !$crypto) { |
|
| 141 | + if ($canWrite && $isExceedingCookieLimit) { |
|
| 142 | + $params = session_get_cookie_params(); |
|
| 143 | + // Clear stored cookie value and cookie when length exceeds the set limit |
|
| 144 | + $this->currentCookieData = null; |
|
| 145 | + Cookie::set( |
|
| 146 | + $this->cookie, |
|
| 147 | + '', |
|
| 148 | + 0, |
|
| 149 | + $params['path'], |
|
| 150 | + $params['domain'], |
|
| 151 | + $params['secure'], |
|
| 152 | + $params['httponly'] |
|
| 153 | + ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Prepare content for write |
|
| 160 | + $params = session_get_cookie_params(); |
|
| 161 | + // Total max lifetime, stored internally |
|
| 162 | + $lifetime = $this->getLifetime(); |
|
| 163 | + $expiry = $this->getNow() + $lifetime; |
|
| 164 | + |
|
| 165 | + // Restore the known good cookie value |
|
| 166 | + $this->currentCookieData = $this->crypto->encrypt( |
|
| 167 | + sprintf('%010u', $expiry) . $session_data |
|
| 168 | + ); |
|
| 169 | + |
|
| 170 | + // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
|
| 171 | + $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 172 | + |
|
| 173 | + Cookie::set( |
|
| 174 | + $this->cookie, |
|
| 175 | + $this->currentCookieData, |
|
| 176 | + $cookieLifetime / 86400, |
|
| 177 | + $params['path'], |
|
| 178 | + $params['domain'], |
|
| 179 | + $params['secure'], |
|
| 180 | + $params['httponly'] |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + return true; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + public function destroy($session_id) |
|
| 187 | + { |
|
| 188 | + $this->currentCookieData = null; |
|
| 189 | + |
|
| 190 | + $params = session_get_cookie_params(); |
|
| 191 | + |
|
| 192 | + Cookie::force_expiry( |
|
| 193 | + $this->cookie, |
|
| 194 | + $params['path'], |
|
| 195 | + $params['domain'], |
|
| 196 | + $params['secure'], |
|
| 197 | + $params['httponly'] |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + public function gc($maxlifetime) |
|
| 202 | + { |
|
| 203 | + // NOP |
|
| 204 | + } |
|
| 205 | 205 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | public function open($save_path, $name) |
| 60 | 60 | { |
| 61 | - $this->cookie = $name . '_2'; |
|
| 61 | + $this->cookie = $name.'_2'; |
|
| 62 | 62 | |
| 63 | 63 | // Read the incoming value, then clear the cookie - we might not be able |
| 64 | 64 | // to do so later if write() is called after headers are sent |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | // Verify expiration |
| 113 | 113 | if ($cookieData) { |
| 114 | - $expiry = (int)substr($cookieData, 0, 10); |
|
| 114 | + $expiry = (int) substr($cookieData, 0, 10); |
|
| 115 | 115 | $data = substr($cookieData, 10); |
| 116 | 116 | |
| 117 | 117 | if ($expiry > $this->getNow()) { |
@@ -164,11 +164,11 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | // Restore the known good cookie value |
| 166 | 166 | $this->currentCookieData = $this->crypto->encrypt( |
| 167 | - sprintf('%010u', $expiry) . $session_data |
|
| 167 | + sprintf('%010u', $expiry).$session_data |
|
| 168 | 168 | ); |
| 169 | 169 | |
| 170 | 170 | // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
| 171 | - $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
|
| 171 | + $cookieLifetime = min((int) $params['lifetime'], $lifetime); |
|
| 172 | 172 | |
| 173 | 173 | Cookie::set( |
| 174 | 174 | $this->cookie, |