21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8.14 Authenticating with HTTP Cookies<br />

<strong>Problem</strong><br />

You are developing a CGI application for the Web and need to store data on the client’s<br />

machine using a cookie, but you want to prevent the client from viewing the<br />

data or modifying it without your application being able to detect the change.<br />

Solution<br />

Web cookies are implemented by setting a value in the MIME headers sent to the client<br />

in a server response. If the client accepts the cookie, it will present the cookie<br />

back to the server every time the specified conditions are met. The cookie is stored<br />

on the client’s computer, typically in a plaintext file that can be modified with any<br />

editor. Many browsers even provide an interface for viewing and editing cookies that<br />

have been stored.<br />

A single MIME header is a header name followed by a colon, a space, and the header<br />

value. The format of the header value depends on the header name. Here, we’re concerned<br />

with only two headers: the Set-Cookie header, which can be sent to the client<br />

when presenting a web page, and the Cookie header, which the client presents to the<br />

server when the user browses to a site which stores a cookie.<br />

To ensure the integrity of the data that we store on the client’s computer with our<br />

cookie, we should encrypt and MAC the data. The server does encoding when setting<br />

a cookie, then decrypts and validates whenever the cookie comes back. The<br />

server does not share its keys with any other entity—it alone uses them to ensure<br />

that the data has not been read or modified since it originally left the server.<br />

Discussion<br />

When encrypting and MAC’ing the data stored in a cookie, we encounter a problem:<br />

we can use only a limited character set in cookie headers, yet the output of our<br />

cryptographic algorithms is always binary. To solve this problem, we encode the<br />

binary data into the base64 character set. The base64 character set uses the uppercase<br />

letters, the lowercase letters, the numbers, and a few pieces of punctuation to<br />

represent data. Out of necessity, the length of data grows considerably when base64encoded.<br />

We can use the spc_base64_encode( ) function from Recipe 4.5 for base64<br />

encoding to suit our purposes.<br />

The first thing that the server must do is call spc_cookie_init( ), which will initialize<br />

a context object that we’ll use for both encoding and decoding cookie data. To simplify<br />

the encryption and MAC’ing process, as well as reduce the complexity of sending<br />

and processing received cookies, we’ll use CWC mode from Recipe 5.10.<br />

Authenticating with HTTP Cookies | 419<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!