From b5efe8ac07194d6e2447bef423292fa5909cf2fd Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Apr 2012 04:31:03 +0200 Subject: Retry in case of a handshake conflict --- src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src') diff --git a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c index 8336db5..886a372 100644 --- a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c +++ b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c @@ -528,6 +528,16 @@ static inline const fastd_peer_config* match_sender_key(fastd_context *ctx, cons return NULL; } +static void kill_handshakes(fastd_context *ctx, fastd_peer *peer) { + pr_debug(ctx, "there is a handshake conflict, retrying in a moment..."); + + free_handshake(peer->protocol_state->initiating_handshake); + peer->protocol_state->initiating_handshake = NULL; + + free_handshake(peer->protocol_state->accepting_handshake); + peer->protocol_state->accepting_handshake = NULL; +} + static void protocol_handshake_handle(fastd_context *ctx, fastd_peer *peer, const fastd_handshake *handshake) { init_peer_state(ctx, peer); @@ -566,6 +576,11 @@ static void protocol_handshake_handle(fastd_context *ctx, fastd_peer *peer, cons switch(handshake->type) { case 1: + if (peer->protocol_state->initiating_handshake) { + kill_handshakes(ctx, peer); + return; + } + new_handshake(ctx, peer, peer_config, false); memcpy(peer->protocol_state->accepting_handshake->peer_key.p, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, PUBLICKEYBYTES); respond_handshake(ctx, peer, handshake); @@ -593,6 +608,12 @@ static void protocol_handshake_handle(fastd_context *ctx, fastd_peer *peer, cons } pr_debug(ctx, "received handshake response from %P", peer); + + if (peer->protocol_state->accepting_handshake) { + kill_handshakes(ctx, peer); + return; + } + memcpy(peer->protocol_state->initiating_handshake->peer_key.p, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, PUBLICKEYBYTES); finish_handshake(ctx, peer, handshake); @@ -619,6 +640,13 @@ static void protocol_handshake_handle(fastd_context *ctx, fastd_peer *peer, cons return; } + pr_debug(ctx, "received handshake finish from %P", peer); + + if (peer->protocol_state->initiating_handshake) { + kill_handshakes(ctx, peer); + return; + } + handle_finish_handshake(ctx, peer, handshake); break; -- cgit v1.2.3