Web.ClientSession

clientsession-0.8.0.1: Securely store session data in a client-side cookie.

Portability | portable
Stability | Stable
Maintainer | Michael Snoyman <michael@snoyman.com>
Safe Haskell | None

Web.ClientSession

Contents

- Automatic key generation
- Actual encryption/decryption

Description

Stores session data in a client cookie. In order to do so, we:

- Encrypt the cookie data using AES in CTR mode. This allows you to store sensitive information on the client side without worrying about eavesdropping.
- Authenticate the encrypted cookie data using Skein-MAC-512-256. Besides detecting potential errors in storage or transmission of the cookies (integrity), the MAC also avoids malicious modifications of the cookie data by assuring you that the cookie data really was generated by this server (authenticity).
- Encode everything using Base64. Thus we avoid problems with non-printable characters by giving the browser a simple string.

Simple usage of the library involves just calling `getDefaultKey` on the startup of your server, `encryptIO` when serializing cookies and `decrypt` when parsing then back.

Synopsis

- data Key
- type IV = IV AES256
- randomIV :: IO IV
- mkIV :: ByteString -> Maybe IV
- getKey :: FilePath -> IO Key
- defaultKeyFile :: FilePath
- getDefaultKey :: IO Key
- initKey :: ByteString -> Either String Key
- randomKey :: IO (ByteString, Key)
- encrypt :: Key -> IV -> ByteString -> ByteString
- encryptIO :: Key -> ByteString -> IO ByteString
- decrypt :: Key -> ByteString -> Maybe ByteString

# Automatic key generation

data Key

The keys used to store the cookies. We have an AES key used to encrypt the cookie and a Skein-MAC-512-256 key used verify the authencity and integrity of the cookie. The AES key needs to have exactly 32 bytes (256 bits) while Skein-MAC-512-256 should have 64 bytes (512 bits).

See also `getDefaultKey` and `initKey`.

Instances

Eq Key |
Show Key | Dummy `Show` instance.
Serialize Key |

type IV = IV AES256

The initialization vector used by AES. Should be exactly 16 bytes long.

randomIV :: IO IV

Randomly construct a fresh initialization vector. You should not reuse initialization vectors.

mkIV :: ByteString -> Maybe IV

Construct an initialization vector from a `ByteString`. Fails if there isn't exactly 16 bytes.

getKey

Arguments

:: FilePath | File name where key is stored.
-> IO Key | The actual key.

Get a key from the given text file.

If the file does not exist or is corrupted a random key will be generated and stored in that file.

defaultKeyFile :: FilePath

The default key file.

getDefaultKey :: IO Key

Simply calls `getKey` `defaultKeyFile`.

initKey :: ByteString -> Either String Key

Initializes a `Key` from a random `ByteString`. Fails if there isn't exactly 96 bytes (256 bits for AES and 512 bits for Skein-MAC-512-512).

randomKey :: IO (ByteString, Key)

Generate a random `Key`. Besides the `Key`, the `ByteString` passed to `initKey` is returned so that it can be saved for later use.

# Actual encryption/decryption

encrypt

Arguments

:: Key | Key of the server.
-> IV | New, random initialization vector (see `randomIV`).
-> ByteString | Serialized cookie data.
-> ByteString | Encoded cookie data to be given to the client browser.

Encrypt (AES-CTR), authenticate (Skein-MAC-512-256) and encode (Base64) the given cookie data. The returned byte string is ready to be used in a response header.

encryptIO :: Key -> ByteString -> IO ByteString

Same as `encrypt`, however randomly generates the initialization vector for you.

decrypt

Arguments

:: Key | Key of the server.
-> ByteString | Encoded cookie data given by the browser.
-> Maybe ByteString | Serialized cookie data.

Decode (Base64), verify the integrity and authenticity (Skein-MAC-512-256) and decrypt (AES-CTR) the given encoded cookie data. Returns the original serialized cookie data. Fails if the data is corrupted.