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.

Initialization requires a key to use for encrypting and MAC’ing the data in cookies.<br />

The implementation of CWC described in Recipe 5.10 can use keys that are 128,<br />

192, or 256 bits in size. Before calling spc_cookie_init( ), you should create a key<br />

using spc_rand( ), as defined in Recipe 11.2. If the cookies you are sending to the client<br />

are persistent, you should store the key on the server so that the same key is<br />

always used, rather than generating a new one every time the server starts up. You<br />

can either hardcode the key into your program or store it in a file somewhere that is<br />

inaccessible through the web server so that you are sure it cannot be compromised.<br />

#include <br />

#include <br />

#include <br />

static cwc_t spc_cookie_cwc;<br />

static unsigned char spc_cookie_nonce[11];<br />

int spc_cookie_init(unsigned char *key, size_t keylen) {<br />

memset(spc_cookie_nonce, 0, sizeof(spc_cookie_nonce));<br />

return cwc_init(&spc_cookie_cwc, key, keylen * 8);<br />

}<br />

To encrypt and MAC the data to send in a cookie, use the following spc_cookie_<br />

encode( ) function, which requires two arguments:<br />

cookie<br />

Data to be encrypted and MAC’d. spc_cookie_encode( ) expects the data to be a<br />

C-style string, which means that it should not contain binary data and should be<br />

NULL terminated.<br />

nonce<br />

11-byte buffer that contains the nonce to use (see Recipe 4.9 for a discussion of<br />

nonces). If you specify this argument as NULL, a default buffer that contains all<br />

NULL bytes will be used for the nonce.<br />

The problem with using a nonce with cookies is that the same nonce must be used<br />

for decrypting and verifying the integrity of the data received from the client. To be<br />

able to do this, you need a second plaintext cookie that allows you to recover the<br />

nonce before decrypting and verifying the encrypted cookie data. Typically, this<br />

would be the user’s name, and the server would maintain a list of nonces that it has<br />

encoded for each logged-in user.<br />

If you do not use a nonce, your system will be susceptible to capture<br />

replay attacks. It is worth expending the effort to use a nonce.<br />

The return from spc_cookie_encode( ) will be a dynamically allocated buffer that<br />

contains the base64-encoded ciphertext and MAC of the data passed into it. You are<br />

responsible for freeing the memory by calling free( ).<br />

420 | Chapter 8: Authentication and Key Exchange<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!