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.

char *spc_cookie_encode(char *cookie, unsigned char *nonce) {<br />

size_t cookielen;<br />

unsigned char *out;<br />

cookielen = strlen(cookie);<br />

if (!(out = (unsigned char *)malloc(cookielen + 16))) return 0;<br />

if (!nonce) nonce = spc_cookie_nonce;<br />

cwc_encrypt_message(&spc_cookie_cwc, 0, 0, cookie, cookielen, nonce, out);<br />

cookie = spc_base64_encode(out, cookielen + 16, 0);<br />

free(out);<br />

return cookie;<br />

}<br />

When the cookies are received by the server from the client, you can pass the<br />

encrypted and MAC’d data to spc_cookie_decode( ), which will decrypt the data and<br />

verify its integrity. If there is any error, spc_cookie_decode( ) will return NULL; otherwise,<br />

it will return the decrypted data in a dynamically allocated buffer that you are<br />

responsible for freeing with free( ).<br />

char *spc_cookie_decode(char *data, unsigned char *nonce) {<br />

int error;<br />

char *out;<br />

size_t cookielen;<br />

unsigned char *cookie;<br />

if (!(cookie = spc_base64_decode(data, &cookielen, 1, &error))) return 0;<br />

if (!(out = (char *)malloc(cookielen - 16 + 1))) {<br />

free(cookie);<br />

return 0;<br />

}<br />

if (!nonce) nonce = spc_cookie_nonce;<br />

error = !cwc_decrypt_message(&spc_cookie_cwc, 0, 0, cookie, cookielen,<br />

nonce, out);<br />

free(cookie);<br />

if (error) {<br />

free(out);<br />

return 0;<br />

}<br />

out[cookielen - 16] = 0;<br />

return out;<br />

}<br />

See Also<br />

Recipes 4.5, 4.6, 4.9, 5.10, 11.2<br />

Authenticating with HTTP Cookies | 421<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!