PHP Classes

File: examples/encrypting.php

Recommend this page to a friend!
  Classes of Colin McKinnon  >  Stackable PHP Session Handler  >  examples/encrypting.php  >  Download  
File: examples/encrypting.php
Role: Example script
Content type: text/plain
Description: A very security session data encrypting handler
Class: Stackable PHP Session Handler
Store session data with multiple session handlers
Author: By
Last change:
Date: 1 year ago
Size: 3,982 bytes


Class file image Download
'../handlers/'); // base class defn
require_once('../handlers/'); // encryption layer stacked on...
require_once('../handlers/'); // storage layer


$storage=new compatSessionHandler();
$enc=new encryptingSessionHandler($storage);

if (!
$enc->install()) {
"set handler failed";
logger("* about to call session_start()");

if (!isset(
$_SESSION['c'])) {

if ((integer)
$_SESSION['c'] && !($_SESSION['c'] % 3)) {
logger("* about to regenerate");
    if (
version_compare(PHP_VERSION, '5.5.1') < 0) {
// we have to tell the encryptor to create a new key
logger("about to finish");
<H1>The Encrypting Handler</H1>
This provides encryption of the session data making it safe to store sensitive information in backups
and on shared servers, albeit at the cost of some extra processing. View the source of this file for an example.
The handler generates a random encrpyption key which is sent to the client in a Cookie (using the same
parameters as have been configured for the session cookie). The default name for the cookie is 'x' + session_name(). This can be overridden in the constructor.
Note that the handler does not implement storage and needs to be stacked on top of something which does!
The session ID is used as an <a href="">Initialization Vector</a>.
The default encryption algorithm is AES-256.
<b>Important</b> Like the session ID, the encryption key is vulnerable to fixation, further, PHP prior to 5.5.1 does
not implement the hook for regenerating the session id, hence on any authentication transition (and preferably on presentation of a non-existent session ID)
your code should call session_regenerate_id() <em>and</em> the code necessary to create a new encryption key:<br />
    if (version_compare(PHP_VERSION, '5.5.1') < 0) {
<br />The code will emit a warning if the session ID changes but the encryption key stays the same.
While Suhosin also has the capability to encrypt session data, the <a href="">key generation</a> (using one or more of a fixed string,
the user agent, part of the client IP address and the path to the document root) can, in many cases, be determined
from other artefacts on the system. Also both the User agent and IP address can change mid session leading to a loss of session data. On the other hand
the native code implementation of Suhosin means that it will probably scale better.
There are a lot of other encrypting session handlers out there however
<li>Most fail to expose the key regeneration method allowing fixation attacks</li>
<li>Several undermine the encrpytion by doing silly things like hashing the key before use (reduces entropy)</li>
<li>You need to do a lot of coding to change the underlying storage mechanism</li>
The handler itself uses a slaved object (class included in file) to deal with the actual encryption. Hence you can change
the encryption algorithm easily, for example substituting the null encryption engine for debugging.
You can expect this to add a significant CPU cost to each request.
Here is the output logging generated by this script:<br />
print "OK:++\$_SESSION['c']=" . $_SESSION['c'] . "<pre>$statuslog</pre>";

"<hr />session = <pre>" . var_export($_SESSION, true) . "</pre>\n<hr />cookies = <pre>" . var_export($_COOKIE, true) . "</pre>";