Merge pull request #2 from fredrikt/master

Avoid DoS
Add syscalls that seem to be needed to use with softhsm.

softhsm 1.3.0-1build1
Ubuntu 12.04.1
Graceful shutdown on SIGTERM.

Helps find resource leaks, if nothing else.
Fix massive memory leak.

Commit 09669a08 introduced ds->cs.sock along side ds->socket.
The code that closed the socket only set ds->cs.sock to -1, so
the cleanup code (that looked at ds->socket) never realized it
should free the message buffers.

This commit removes ds->socket completely, and makes the
cleanup code look if ds->cs.sock is -1. Memory usage now stays
constant when looping 'pkcs11-tool --test --slot 0' at least.
proto_read_byte_buffer: Allocate buffer even for 0 bytes.

Best for the proxy to be as transparent as possible. This fixes a test
case with C_GenerateRandom of 0 bytes, found in pkcs11-tool --test.
Merge pull request #6 from fredrikt/fix_zero_sized_buffers

Fix zero sized buffers
proto_read_null_string: alloc space for NULL byte too.
rpc_C_Finalize: Change order of pReserved-check.

This matches the PKCS#11 specification better, and also matches
the test cases for softhsm.
Check pkcs11_initialized in a number of functions.

This harmonizes with the softhsm test suite.
indent
Call C_Finalize when client disconnects.

Necessary to close all ongoing sessions.
More input validation.
proto_read_attribute_array: allow len == 0

Since proto_write_attribute_array() happily writes arrays with len == 0,
we should probably parse them. Tounge in cheek.
proto_read_byte_array: Don't miss n_array when ! valid.

Sending side gck_rpc_message_write_byte_array() adds length data
when valid == 0, so receiving code (this) need to parse that obviosly.
Debug output return values in hex, to match pkcs11.h.
proto_read_byte_array: Don't pass size_t to egg_buffer_get_uint32.

Passing size_t to _get_uint32 might leave garbage in top 32 bits of size_t
on 64 bits platforms. While initializing the size_t to 0 would probably
work, using a temporary uint32_t and casting that is more explicit IMO.
proto_read_byte_buffer: Support undefined buffer size.

A number of PKCS#11 functions take a pointer to a buffer size as
argument. To be a transparent proxy of PKCS#11 calls, it is necessary to
support invoking these functions with a NULL pointer. pkcs11-proxy used
to send the buffer size as an integer and create a pointer to the integer
on the server side, but this is different to the backend PKCS#11 module
in some cases.

E.g. the C_Encrypt call is specified to have side effectes (finalizing)
when called with a NULL encrypted data length. The softhsm test suite
exposed that these side effects never occured because the NULL data length
pointer was conveyed as a valid pointer to the integer zero.

Since an additional uint8_t was added to "byte buffers", this is an
backwards incompatible change. As such, the version number in the protocol
greeting was increased (GCK_RPC_HANDSHAKE).
Check pkcs11_initialized "everywhere".

Since PKCS#11 module initialization is done globally on the server side,
the real PKCS#11 modules check for initialization is made a no-op. The
only place it is possible to check for proper initialization by the
application is here - on the client side.
Remove obsolete input data checks.

Remove a bunch of input data checks now that byte buffers support
NULL data length pointers in order to be as transparent as possible.
rpc_C_GenerateKeyPair: Remove redundant check.