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.

* If it's the client's turn to speak, abort. */<br />

if (ctx->nonce[0] != SPC_CLIENT_DISTINGUISHER) abort();<br />

ctx->nonce[0] = SPC_SERVER_DISTINGUISHER;<br />

spc_get_status_and_nonce(ctx->fd, &status, nonce);<br />

*end = status;<br />

return spc_finish_decryption(ctx, status, nonce, len);<br />

}<br />

static void spc_get_status_and_nonce(int fd, unsigned char *status,<br />

unsigned char *nonce) {<br />

/* Read the status byte. If it's 0x00 or 0xff, we're going to look at<br />

* the rest of the message, otherwise we'll just give up right away. */<br />

spc_ssock_read(fd, status, 1);<br />

if (*status != spc_msg_ok && *status != spc_msg_end) abort( );<br />

spc_ssock_read(fd, nonce, SPC_CWC_NONCE_LEN);<br />

}<br />

static unsigned char *spc_finish_decryption(spc_ssock_t *ctx, unsigned char status,<br />

unsigned char *nonce, size_t *len) {<br />

size_t ctlen = 0, i;<br />

unsigned char *ct, encoded_len[SPC_MLEN_FIELD_LEN];<br />

/* Check the nonce. */<br />

for (i = 0; i < SPC_CWC_NONCE_LEN; i++)<br />

if (nonce[i] != ctx->nonce[i]) abort();<br />

/* Read the length field. */<br />

spc_ssock_read(ctx->fd, encoded_len, SPC_MLEN_FIELD_LEN);<br />

for (i = 0; i < SPC_MLEN_FIELD_LEN; i++) {<br />

ctlen cwc), &status, 1, ct, ctlen, nonce, ct)) {<br />

free(ct);<br />

abort();<br />

}<br />

*len = ctlen - SPC_CWC_TAG_LEN;<br />

/* We'll go ahead and avoid the realloc(), leaving SPC_CWC_TAG_LEN extra<br />

* bytes at the end of the buffer than we need to leave.<br />

*/<br />

return ct;<br />

}<br />

static void spc_ssock_read(int fd, unsigned char *msg, size_t mlen) {<br />

ssize_t r;<br />

498 | Chapter 9: Networking<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!