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.

}<br />

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

ssize_t w;<br />

while (mlen) {<br />

if ((w = write(fd, msg, mlen)) == -1) {<br />

switch (errno) {<br />

case EINTR:<br />

break;<br />

default:<br />

abort();<br />

}<br />

} else {<br />

mlen -= w;<br />

msg += w;<br />

}<br />

}<br />

}<br />

Let’s look at the rest of the client side of the connection, before we turn our attention<br />

to the server side. When the client wishes to terminate the connection politely, it<br />

will send an empty message but pass 0xff as the status byte. It must still send the<br />

proper nonce and encrypt a zero-length message (which CWC will quite happily do).<br />

That can be done with code very similar to the code shown previously, so we won’t<br />

waste space by duplicating the code.<br />

Now let’s look at what happens when the client receives a message. The status byte<br />

should be 0x00. The nonce we get from the server should be unchanged from the one<br />

we just sent, except that the first byte should be SPC_SERVER_DISTINGUISHER. If the<br />

nonce is invalid, we’ll just fail by aborting, though you could instead discard the<br />

message if you choose to do so (doing so is a bit problematic, though, because you<br />

then have to resync the connection somehow).<br />

Next, we’ll read the length value, dynamically allocating a buffer that’s big enough to<br />

hold the ciphertext. This code can never allocate more than 232–1 bytes of memory.<br />

In practice, you should probably have a maximum message length and check to<br />

make sure the length field doesn’t exceed that. Such a test can keep an attacker from<br />

launching a denial of service attack in which she has you allocate enough memory to<br />

slow down your machine.<br />

Finally, we’ll call cwc_decrypt_message( ) and see if the MAC validates. If it does,<br />

we’ll return the message. Otherwise, we will abort.<br />

static void spc_ssock_read(int, unsigned char *, size_t);<br />

static void spc_get_status_and_nonce(int, unsigned char *, unsigned char *);<br />

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

unsigned char *, size_t *);<br />

unsigned char *spc_client_read(spc_ssock_t *ctx, size_t *len, size_t *end) {<br />

unsigned char status;<br />

unsigned char nonce[SPC_CWC_NONCE_LEN];<br />

Building an Authenticated Secure Channel Without SSL | 497<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!