From 14d6915f098c6d51a6e879b81f70d9b9dba0b867 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Apr 2012 22:18:22 +0200 Subject: Use 2 bytes to encode handshake field types and lengths; breaks compatiblity with v0.1-rc2 and earlier --- src/handshake.c | 20 ++++++++++---------- src/handshake.h | 20 ++++++++++++-------- src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c | 6 +++--- 3 files changed, 25 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/handshake.c b/src/handshake.c index 839951b..a17fd2b 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -58,8 +58,8 @@ static const char const *REPLY_TYPES[REPLY_MAX] = { fastd_buffer fastd_handshake_new_init(fastd_context *ctx, fastd_peer *peer, size_t tail_space) { size_t protocol_len = strlen(ctx->conf->protocol->name); fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, - 2*3 + /* handshake type, mode */ - 2+protocol_len+ /* protocol name */ + 2*5 + /* handshake type, mode */ + 4+protocol_len+ /* protocol name */ tail_space ); fastd_packet *request = buffer.data; @@ -77,7 +77,7 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, fastd_peer *peer, size fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, fastd_peer *peer, const fastd_handshake *handshake, size_t tail_space) { fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, - 2*3 + /* handshake type, reply code */ + 2*5 + /* handshake type, reply code */ tail_space ); fastd_packet *request = buffer.data; @@ -105,19 +105,19 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer b uint8_t *ptr = packet->tlv_data; while (true) { - if (ptr+2 > (uint8_t*)buffer.data + buffer.len) + if (ptr+4 > (uint8_t*)buffer.data + buffer.len) break; - uint8_t type = ptr[0]; - uint8_t len = ptr[1]; + uint16_t type = ptr[0] + (ptr[1] << 8); + uint16_t len = ptr[2] + (ptr[3] << 8); - if (ptr+2+len > (uint8_t*)buffer.data + buffer.len) + if (ptr+4+len > (uint8_t*)buffer.data + buffer.len) break; handshake.records[type].length = len; - handshake.records[type].data = ptr+2; + handshake.records[type].data = ptr+4; - ptr += 2+len; + ptr += 4+len; } handshake.req_id = packet->req_id; @@ -160,7 +160,7 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer b send_reply: if (reply_code) { - fastd_buffer reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, 3*3 /* enough space for handshake type, reply code and error detail */); + fastd_buffer reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, 3*5 /* enough space for handshake type, reply code and error detail */); fastd_packet *reply = reply_buffer.data; reply->req_id = packet->req_id; diff --git a/src/handshake.h b/src/handshake.h index c59d141..d540de0 100644 --- a/src/handshake.h +++ b/src/handshake.h @@ -72,29 +72,33 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer b static inline void fastd_handshake_add(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, size_t len, const void *data) { - if ((uint8_t*)buffer->data + buffer->len + 2 + len > (uint8_t*)buffer->base + buffer->base_len) + if ((uint8_t*)buffer->data + buffer->len + 4 + len > (uint8_t*)buffer->base + buffer->base_len) exit_bug(ctx, "not enough buffer allocated for handshake"); uint8_t *dst = (uint8_t*)buffer->data + buffer->len; dst[0] = type; - dst[1] = len; - memcpy(dst+2, data, len); + dst[1] = type >> 8; + dst[2] = len; + dst[3] = len >> 8; + memcpy(dst+4, data, len); - buffer->len += 2 + len; + buffer->len += 4 + len; } static inline void fastd_handshake_add_uint8(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, uint8_t value) { - if ((uint8_t*)buffer->data + buffer->len + 3 > (uint8_t*)buffer->base + buffer->base_len) + if ((uint8_t*)buffer->data + buffer->len + 5 > (uint8_t*)buffer->base + buffer->base_len) exit_bug(ctx, "not enough buffer allocated for handshake"); uint8_t *dst = (uint8_t*)buffer->data + buffer->len; dst[0] = type; - dst[1] = 1; - dst[2] = value; + dst[1] = type >> 8; + dst[2] = 1; + dst[3] = 0; + dst[4] = value; - buffer->len += 3; + buffer->len += 5; } diff --git a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c index 326d1b5..29c02fa 100644 --- a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c +++ b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c @@ -277,7 +277,7 @@ static protocol_handshake* new_handshake(fastd_context *ctx, fastd_peer *peer, c static void protocol_handshake_init(fastd_context *ctx, fastd_peer *peer) { init_peer_state(ctx, peer); - fastd_buffer buffer = fastd_handshake_new_init(ctx, peer, 3*(2+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); + fastd_buffer buffer = fastd_handshake_new_init(ctx, peer, 3*(4+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); protocol_handshake *handshake = new_handshake(ctx, peer, peer->config, true); @@ -343,7 +343,7 @@ static void respond_handshake(fastd_context *ctx, fastd_peer *peer, const fastd_ crypto_auth_hmacsha256(hmacbuf, hashinput, 2*PUBLICKEYBYTES, peer->protocol_state->accepting_handshake->shared_handshake_key); - fastd_buffer buffer = fastd_handshake_new_reply(ctx, peer, handshake, 4*(2+PUBLICKEYBYTES) + 2+HMACBYTES); + fastd_buffer buffer = fastd_handshake_new_reply(ctx, peer, handshake, 4*(4+PUBLICKEYBYTES) + 4+HMACBYTES); fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, ctx->conf->protocol_config->public_key.p); fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, peer->protocol_state->accepting_handshake->peer_config->protocol_config->public_key.p); @@ -475,7 +475,7 @@ static void finish_handshake(fastd_context *ctx, fastd_peer *peer, const fastd_h memcpy(hashinput+PUBLICKEYBYTES, peer->protocol_state->initiating_handshake->public_key.p, PUBLICKEYBYTES); crypto_auth_hmacsha256(hmacbuf, hashinput, 2*PUBLICKEYBYTES, peer->protocol_state->initiating_handshake->shared_handshake_key); - fastd_buffer buffer = fastd_handshake_new_reply(ctx, peer, handshake, 4*(2+PUBLICKEYBYTES) + 2+HMACBYTES); + fastd_buffer buffer = fastd_handshake_new_reply(ctx, peer, handshake, 4*(4+PUBLICKEYBYTES) + 4+HMACBYTES); fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, ctx->conf->protocol_config->public_key.p); fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, peer->protocol_state->initiating_handshake->peer_config->protocol_config->public_key.p); -- cgit v1.2.3