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.

while (mlen) {<br />

if ((r = read(fd, msg, mlen)) == -1) {<br />

switch (errno) {<br />

case EINTR:<br />

break;<br />

default:<br />

abort();<br />

}<br />

} else {<br />

mlen -= r;<br />

msg += r;<br />

}<br />

}<br />

}<br />

The client is responsible for deallocating the memory for messages.<br />

We recommend securely wiping messages before doing so, as discussed<br />

in Recipe 13.2. In addition, you should securely erase the spc_<br />

ssock_t context when you are done with it.<br />

That’s everything on the client side. Now we can move on to the server. The server<br />

can share the spc_ssock_t type that the client uses, as well as all the helper functions,<br />

such as spc_ssock_read( ) and spc_ssock_write( ). But the API for initialization,<br />

reading, and writing must change.<br />

Here’s the server-side initialization function that should get called once the key<br />

exchange is complete but before the client’s first message is read:<br />

void spc_init_server(spc_ssock_t *ctx, unsigned char *key, size_t klen, int fd) {<br />

if (klen != 16 && klen != 24 && klen != 32) abort();<br />

/* Remember that cwc_init() erases the key we pass in! */<br />

cwc_init(&(ctx->cwc), key, klen * 8);<br />

/* We need to wait for the random portion of the nonce from the client.<br />

* The counter portion we can initialize to zero. We'll set the distinguisher<br />

* to SPC_SERVER_LACKS_NONCE, so that we know to copy in the random portion<br />

* of the nonce when we receive a message.<br />

*/<br />

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

memset(ctx->nonce + SPC_CTR_IX, 0, SPC_CTR_LEN);<br />

ctx->fd = fd;<br />

}<br />

The first thing the server does is read data from the client’s socket. In practice, the<br />

following code isn’t designed for a single-threaded server that uses select( ) to determine<br />

which client has data to be read. This is because once we start reading data, we<br />

keep reading until we’ve taken in the entire message, and all the reads are blocking.<br />

The code is not designed to work in a nonblocking environment.<br />

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