-skeletons for transport-ng
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.c
index 8a5025a5b3d8a57091e06808965c7a636ddcfe37..b9f0e1fa25f9ff13855c9592f02081b2503436c9 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -188,6 +188,11 @@ struct CadetTunnelSkippedKey
    * Message key.
    */
   struct GNUNET_CRYPTO_SymmetricSessionKey MK;
+
+  /**
+   * Key number for a given HK.
+   */
+  unsigned int Kn;
 };
 
 
@@ -878,6 +883,14 @@ new_ephemeral (struct CadetTunnel *t)
 {
   GNUNET_free_non_null (t->ax->DHRs);
   t->ax->DHRs = GNUNET_CRYPTO_ecdhe_key_create();
+  #if DUMP_KEYS_TO_STDERR
+  {
+    struct GNUNET_CRYPTO_EcdhePublicKey pub;
+    GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &pub);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  new DHRs generated: pub  %s\n",
+        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &pub));
+  }
+  #endif
 }
 
 
@@ -1059,7 +1072,7 @@ t_ax_encrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &MK, NULL, 0, NULL);
 
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "  CKs: %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  CKs: %s\n",
        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKs));
   LOG (GNUNET_ERROR_TYPE_INFO, "  AX_ENC with key %u: %s\n", ax->Ns,
        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &MK));
@@ -1101,7 +1114,7 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &MK, NULL, 0, NULL);
 
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "  CKr: %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  CKr: %s\n",
        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKr));
   LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC with key %u: %s\n", ax->Nr,
        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &MK));
@@ -1181,7 +1194,7 @@ t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_AX *src,
 
   GNUNET_assert (AX_HEADER_SIZE == out_size);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_ax_decrypt end\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  t_h_decrypt end\n");
 }
 
 
@@ -1325,38 +1338,76 @@ t_decrypt_and_validate (struct CadetTunnel *t,
  * @return Size of the decrypted data, -1 if an error was encountered.
  */
 static int
-try_old_ax_keys (struct CadetTunnel *t, struct GNUNET_CADET_AX *dst,
+try_old_ax_keys (struct CadetTunnel *t, void *dst,
                  const struct GNUNET_CADET_AX *src, size_t size)
 {
   struct CadetTunnelSkippedKey *key;
-  struct GNUNET_CADET_Hash hmac;
+  struct GNUNET_CADET_Hash *hmac;
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
+  struct GNUNET_CADET_AX plaintext_header;
+  struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
+  size_t esize;
   size_t res;
   size_t len;
+  unsigned int N;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n");
+  hmac = &plaintext_header.hmac;
+  esize = size - sizeof (struct GNUNET_CADET_AX);
 
+  /* Find a correct Header Key */
   for (key = t->ax->skipped_head; NULL != key; key = key->next)
   {
-    t_hmac (&src->Ns, AX_HEADER_SIZE, 0, &key->HK, &hmac);
-    if (0 != memcmp (&hmac, &src->hmac, sizeof (hmac)))
+    #if DUMP_KEYS_TO_STDERR
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  Trying hmac with key %s\n",
+         GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->HK));
+    #endif
+    t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &key->HK, hmac);
+    if (0 == memcmp (hmac, &src->hmac, sizeof (*hmac)))
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  hmac correct\n");
+      valid_HK = &key->HK;
       break;
+    }
   }
   if (NULL == key)
     return -1;
 
+  /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
+  GNUNET_assert (size > sizeof (struct GNUNET_CADET_AX));
+  len = size - sizeof (struct GNUNET_CADET_AX);
+  GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
+
+  /* Decrypt header */
+  GNUNET_CRYPTO_symmetric_derive_iv (&iv, &key->HK, NULL, 0, NULL);
+  res = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns, AX_HEADER_SIZE,
+                                         &key->HK, &iv, &plaintext_header.Ns);
+  GNUNET_assert (AX_HEADER_SIZE == res);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  Message %u, previous: %u\n",
+       ntohl (plaintext_header.Ns), ntohl (plaintext_header.PNs));
+
+  /* Find the correct Message Key */
+  N = ntohl (plaintext_header.Ns);
+  while (NULL != key && N != key->Kn)
+    key = key->next;
+  if (NULL == key || 0 != memcmp (&key->HK, valid_HK, sizeof (*valid_HK)))
+    return -1;
+
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC with skipped key %s\n",
-       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
+  LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC_H with skipped key %s\n",
+       GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->HK));
+  LOG (GNUNET_ERROR_TYPE_INFO, "  AX_DEC with skipped key %u: %s\n",
+       key->Kn, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
   #endif
 
-  GNUNET_assert (size > sizeof (struct GNUNET_CADET_AX));
-  len = size - sizeof (struct GNUNET_CADET_AX);
+  /* Decrypt payload */
   GNUNET_CRYPTO_symmetric_derive_iv (&iv, &key->MK, NULL, 0, NULL);
-  res = GNUNET_CRYPTO_symmetric_decrypt (&src[1], len, &key->MK, &iv, &dst[1]);
+  res = GNUNET_CRYPTO_symmetric_decrypt (&src[1], len, &key->MK, &iv, dst);
 
+  /* Remove key */
   GNUNET_CONTAINER_DLL_remove (t->ax->skipped_head, t->ax->skipped_tail, key);
   t->ax->skipped--;
-  GNUNET_free (key);
+  GNUNET_free (key); /* GNUNET_free overwrites memory with 0xbaadf00d */
 
   return res;
 }
@@ -1376,11 +1427,13 @@ store_skipped_key (struct CadetTunnel *t,
 
   key = GNUNET_new (struct CadetTunnelSkippedKey);
   key->timestamp = GNUNET_TIME_absolute_get ();
+  key->Kn = t->ax->Nr;
+  key->HK = t->ax->HKr;
   t_hmac_derive_key (&t->ax->CKr, &key->MK, "0", 1);
   #if DUMP_KEYS_TO_STDERR
-  LOG (GNUNET_ERROR_TYPE_INFO, "    storing MK for Nr %u: %s\n",
-       t->ax->Nr, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
-  LOG (GNUNET_ERROR_TYPE_INFO, "    for CKr: %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    storing MK for Nr %u: %s\n",
+       key->Kn, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    for CKr: %s\n",
        GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->ax->CKr));
   #endif
   t_hmac_derive_key (&t->ax->CKr, &t->ax->CKr, "1", 1);
@@ -1413,21 +1466,33 @@ delete_skipped_key (struct CadetTunnel *t, struct CadetTunnelSkippedKey *key)
  * @param t Tunnel where to stage the keys.
  * @param HKr Header key.
  * @param Np Received meesage number.
+ *
+ * @return GNUNET_OK if keys were stored.
+ *         GNUNET_SYSERR if an error ocurred (Np not expected).
  */
-static void
+static int
 store_ax_keys (struct CadetTunnel *t,
                const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
                uint32_t Np)
 {
   int gap;
 
+
   gap = Np - t->ax->Nr;
-  if (MAX_KEY_GAP < gap || 0 > gap)
+  LOG (GNUNET_ERROR_TYPE_INFO, "Storing keys [%u, %u)\n", t->ax->Nr, Np);
+  if (MAX_KEY_GAP < gap)
   {
     /* Avoid DoS (forcing peer to do 2*33 chain HMAC operations) */
     /* TODO: start new key exchange on return */
     GNUNET_break_op (0);
-    return;
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Got message %u, expected %u+\n",
+         Np, t->ax->Nr);
+    return GNUNET_SYSERR;
+  }
+  if (0 > gap)
+  {
+    /* Delayed message: don't store keys, flag to try old keys. */
+    return GNUNET_SYSERR;
   }
 
   while (t->ax->Nr < Np)
@@ -1435,6 +1500,8 @@ store_ax_keys (struct CadetTunnel *t,
 
   while (t->ax->skipped > MAX_SKIPPED_KEYS)
     delete_skipped_key (t, t->ax->skipped_tail);
+
+  return GNUNET_OK;
 }
 
 
@@ -1456,14 +1523,13 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
   struct CadetTunnelAxolotl *ax;
   struct GNUNET_CADET_Hash msg_hmac;
   struct GNUNET_HashCode hmac;
-  struct GNUNET_CADET_AX *dstmsg;
+  struct GNUNET_CADET_AX plaintext_header;
   uint32_t Np;
   uint32_t PNp;
   size_t esize;
   size_t osize;
 
   ax = t->ax;
-  dstmsg = dst;
   esize = size - sizeof (struct GNUNET_CADET_AX);
 
   if (NULL == ax)
@@ -1480,20 +1546,21 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
     struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
 
     /* Try Next HK */
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  trying next HK\n");
     t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &ax->NHKr, &msg_hmac);
     if (0 != memcmp (&msg_hmac, &src->hmac, sizeof (msg_hmac)))
     {
       /* Try the skipped keys, if that fails, we're out of luck. */
       return try_old_ax_keys (t, dst, src, size);
     }
-    LOG (GNUNET_ERROR_TYPE_INFO, "next HK\n");
+    LOG (GNUNET_ERROR_TYPE_INFO, "next HK worked\n");
 
     HK = ax->HKr;
     ax->HKr = ax->NHKr;
-    t_h_decrypt (t, src, dstmsg);
-    Np = ntohl (dstmsg->Ns);
-    PNp = ntohl (dstmsg->PNs);
-    DHRp = &dstmsg->DHRs;
+    t_h_decrypt (t, src, &plaintext_header);
+    Np = ntohl (plaintext_header.Ns);
+    PNp = ntohl (plaintext_header.PNs);
+    DHRp = &plaintext_header.DHRs;
     store_ax_keys (t, &HK, PNp);
 
     /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
@@ -1513,17 +1580,19 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
   else
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "current HK\n");
-    t_h_decrypt (t, src, dstmsg);
-    Np = ntohl (dstmsg->Ns);
-    PNp = ntohl (dstmsg->PNs);
+    t_h_decrypt (t, src, &plaintext_header);
+    Np = ntohl (plaintext_header.Ns);
+    PNp = ntohl (plaintext_header.PNs);
   }
+  LOG (GNUNET_ERROR_TYPE_INFO, "  got AX Nr %u\n", Np);
+  if (Np != ax->Nr)
+    if (GNUNET_OK != store_ax_keys (t, &ax->HKr, Np))
+      /* Try the skipped keys, if that fails, we're out of luck. */
+      return try_old_ax_keys (t, dst, src, size);
 
-  if (Np > ax->Nr)
-    store_ax_keys (t, &ax->HKr, Np);
-
+  osize = t_ax_decrypt (t, dst, &src[1], esize);
   ax->Nr = Np + 1;
 
-  osize = t_ax_decrypt (t, dst, &src[1], esize);
   if (osize != esize)
   {
     GNUNET_break_op (0);
@@ -1661,21 +1730,13 @@ create_kx_ctx (struct CadetTunnel *t)
  * @brief Finish the Key eXchange and destroy the old keys.
  *
  * @param cls Closure (Tunnel for which to finish the KX).
- * @param tc Task context.
  */
 static void
-finish_kx (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+finish_kx (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   LOG (GNUNET_ERROR_TYPE_INFO, "finish KX for %s\n", GCT_2s (t));
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    LOG (GNUNET_ERROR_TYPE_INFO, "  shutdown\n");
-    return;
-  }
-
   GNUNET_free (t->kx_ctx);
   t->kx_ctx = NULL;
 }
@@ -1698,14 +1759,15 @@ destroy_kx_ctx (struct CadetTunnel *t)
 
   if (is_key_null (&t->kx_ctx->e_key_old))
   {
-    t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_now (finish_kx, t);
+    t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_now (&finish_kx, t);
     return;
   }
 
   delay = GNUNET_TIME_relative_divide (rekey_period, 4);
   delay = GNUNET_TIME_relative_min (delay, GNUNET_TIME_UNIT_MINUTES);
 
-  t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_delayed (delay, finish_kx, t);
+  t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_delayed (delay,
+                                                        &finish_kx, t);
 }
 
 
@@ -1956,8 +2018,8 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
   if (NULL == cont)
   {
-    GNUNET_break (NULL == GCC_send_prebuilt_message (msg, type,
-                                                     mid, c, fwd, force, NULL, NULL));
+    GNUNET_break (NULL == GCC_send_prebuilt_message (msg, type, mid, c, fwd,
+                                                     force, NULL, NULL));
     return NULL;
   }
   if (NULL == existing_q)
@@ -2029,18 +2091,13 @@ send_queued_data (struct CadetTunnel *t)
  * @brief Resend the AX KX until we complete the handshake.
  *
  * @param cls Closure (tunnel).
- * @param tc Task context.
  */
 static void
-ax_kx_resend (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ax_kx_resend (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   t->rekey_task = NULL;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
   if (CADET_TUNNEL_KEY_OK == t->estate)
   {
     /* Should have been canceled on estate change */
@@ -2048,7 +2105,7 @@ ax_kx_resend (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;
   }
 
-  GCT_send_ax_kx (t, GNUNET_YES);
+  GCT_send_ax_kx (t, CADET_TUNNEL_KEY_SENT >= t->estate);
 }
 
 
@@ -2207,7 +2264,7 @@ send_kx (struct CadetTunnel *t,
 static void
 send_ephemeral (struct CadetTunnel *t)
 {
-  LOG (GNUNET_ERROR_TYPE_INFO, "===> EPHM for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "==> EPHM for %s\n", GCT_2s (t));
   if (NULL != t->ephm_h)
   {
     LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
@@ -2236,7 +2293,7 @@ send_pong (struct CadetTunnel *t, uint32_t challenge)
 {
   struct GNUNET_CADET_KX_Pong msg;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "===> PONG for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "==> PONG for %s\n", GCT_2s (t));
   if (NULL != t->pong_h)
   {
     LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
@@ -2259,25 +2316,21 @@ send_pong (struct CadetTunnel *t, uint32_t challenge)
  * Initiate a rekey with the remote peer.
  *
  * @param cls Closure (tunnel).
- * @param tc TaskContext.
  */
 static void
-rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+rekey_tunnel (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   t->rekey_task = NULL;
-
   LOG (GNUNET_ERROR_TYPE_INFO, "Re-key Tunnel %s\n", GCT_2s (t));
-  if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-    return;
-
   GNUNET_assert (NULL != t->kx_ctx);
   struct GNUNET_TIME_Relative duration;
 
   duration = GNUNET_TIME_absolute_get_duration (t->kx_ctx->rekey_start_time);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " kx started %s ago\n",
-        GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       " kx started %s ago\n",
+       GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
 
   // FIXME make duration of old keys configurable
   if (duration.rel_value_us >= GNUNET_TIME_UNIT_MINUTES.rel_value_us)
@@ -2377,19 +2430,14 @@ rekey_iterator (void *cls,
  * Create a new ephemeral key and key message, schedule next rekeying.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext.
  */
 static void
-global_otr_rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+global_otr_rekey (void *cls)
 {
   struct GNUNET_TIME_Absolute time;
   long n;
 
   rekey_task = NULL;
-
-  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-    return;
-
   GNUNET_free_non_null (otr_ephemeral_key);
   otr_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
 
@@ -2431,7 +2479,8 @@ destroy_iterator (void *cls,
 {
   struct CadetTunnel *t = value;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_shutdown destroying tunnel at %p\n", t);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "GCT_shutdown destroying tunnel at %p\n", t);
   GCT_destroy (t);
   return GNUNET_YES;
 }
@@ -2491,7 +2540,7 @@ handle_data (struct CadetTunnel *t,
   }
   type = ntohs (msg[1].header.type);
   LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n", GC_m2s (type));
-  sprintf (buf, "# received payload of type %hu", type);
+  SPRINTF (buf, "# received payload of type %hu", type);
   GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
 
 
@@ -2501,8 +2550,7 @@ handle_data (struct CadetTunnel *t,
   {
     GNUNET_STATISTICS_update (stats, "# data on unknown channel",
                               1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
-         ntohl (msg->chid));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "channel 0x%X unknown\n", ntohl (msg->chid));
     send_channel_destroy (t, ntohl (msg->chid));
     return;
   }
@@ -2555,8 +2603,8 @@ handle_data_ack (struct CadetTunnel *t,
 /**
  * Handle channel create.
  *
- * @param t Tunnel on which the data came.
- * @param msg Data message.
+ * @param t Tunnel on which the message came.
+ * @param msg ChannelCreate message.
  */
 static void
 handle_ch_create (struct CadetTunnel *t,
@@ -2569,7 +2617,7 @@ handle_ch_create (struct CadetTunnel *t,
   size = ntohs (msg->header.size);
   if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
   {
-    GNUNET_break (0);
+    GNUNET_break_op (0);
     return;
   }
 
@@ -2745,7 +2793,7 @@ static void
 handle_ephemeral (struct CadetTunnel *t,
                   const struct GNUNET_CADET_KX_Ephemeral *msg)
 {
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== EPHM for %s\n", GCT_2s (t));
 
   /* Some old versions are still around, don't log as error. */
   if (GNUNET_OK != check_ephemeral (t, msg))
@@ -2764,7 +2812,7 @@ handle_ephemeral (struct CadetTunnel *t,
       GNUNET_break (0);
       return;
     }
-    rekey_tunnel (t, NULL);
+    rekey_tunnel (t);
     GNUNET_STATISTICS_update (stats, "# otr-downgrades", -1, GNUNET_NO);
   }
 
@@ -2796,7 +2844,7 @@ handle_ephemeral (struct CadetTunnel *t,
     }
     if (NULL != t->rekey_task)
       GNUNET_SCHEDULER_cancel (t->rekey_task);
-    t->rekey_task = GNUNET_SCHEDULER_add_now (rekey_tunnel, t);
+    t->rekey_task = GNUNET_SCHEDULER_add_now (&rekey_tunnel, t);
   }
   if (CADET_TUNNEL_KEY_SENT == t->estate)
   {
@@ -2831,7 +2879,7 @@ handle_pong (struct CadetTunnel *t,
 {
   uint32_t challenge;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== PONG for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== PONG for %s\n", GCT_2s (t));
   if (NULL == t->rekey_task)
   {
     GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
@@ -2883,7 +2931,7 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
   const struct GNUNET_PeerIdentity *pid;
   int am_I_alice;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== AX_KX on %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== {     AX_KX} on %s\n", GCT_2s (t));
 
   if (NULL == t->ax)
   {
@@ -2892,6 +2940,7 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     GNUNET_break (CADET_Axolotl == t->enc_type);
     return;
   }
+  ax = t->ax;
 
   pid = GCT_get_destination (t);
   if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, pid))
@@ -2904,23 +2953,31 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     return;
   }
 
-  if (GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY ==
-      (GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
+  if (0 != (GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
+  {
+    if (NULL != t->rekey_task)
+    {
+      GNUNET_SCHEDULER_cancel (t->rekey_task);
+      t->rekey_task = NULL;
+    }
     GCT_send_ax_kx (t, GNUNET_NO);
+  }
 
-  if (CADET_TUNNEL_KEY_OK == t->estate)
+  if (0 == memcmp (&ax->DHRr, &msg->ratchet_key, sizeof(msg->ratchet_key)))
+  {
+    LOG (GNUNET_ERROR_TYPE_INFO, " known ratchet key, exit\n");
     return;
+  }
 
   LOG (GNUNET_ERROR_TYPE_INFO, " is Alice? %s\n", am_I_alice ? "YES" : "NO");
 
-  ax = t->ax;
   ax->DHRr = msg->ratchet_key;
 
   /* ECDH A B0 */
   if (GNUNET_YES == am_I_alice)
   {
     GNUNET_CRYPTO_eddsa_ecdh (id_key,              /* A */
-                              &msg->ephemeral_key,  /* B0 */
+                              &msg->ephemeral_key, /* B0 */
                               &key_material[0]);
   }
   else
@@ -2940,7 +2997,7 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
   else
   {
     GNUNET_CRYPTO_eddsa_ecdh (id_key,              /* A */
-                              &msg->ephemeral_key,  /* B0 */
+                              &msg->ephemeral_key, /* B0 */
                               &key_material[1]);
 
 
@@ -2967,6 +3024,11 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
                      salt, sizeof (salt),
                      &key_material, sizeof (key_material), NULL);
 
+  if (0 == memcmp (&ax->RK, &keys[0], sizeof(ax->RK)))
+  {
+    LOG (GNUNET_ERROR_TYPE_INFO, " known handshake key, exit\n");
+    return;
+  }
   ax->RK = keys[0];
   if (GNUNET_YES == am_I_alice)
   {
@@ -2988,6 +3050,9 @@ handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
     ax->ratchet_expiration =
       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), ratchet_time);
   }
+  ax->PNs = 0;
+  ax->Nr = 0;
+  ax->Ns = 0;
   GCT_change_estate (t, CADET_TUNNEL_KEY_PING);
   send_queued_data (t);
 }
@@ -3013,11 +3078,10 @@ handle_decrypted (struct CadetTunnel *t,
   char buf[256];
 
   type = ntohs (msgh->type);
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s on %s\n", GC_m2s (type), GCT_2s (t));
-  sprintf (buf, "# received encrypted of type %hu (%s)", type, GC_m2s (type));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "<-- %s on %s\n", GC_m2s (type), GCT_2s (t));
+  SPRINTF (buf, "# received encrypted of type %hu (%s)", type, GC_m2s (type));
   GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
 
-
   switch (type)
   {
     case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
@@ -3076,7 +3140,6 @@ GCT_handle_encrypted (struct CadetTunnel *t,
 {
   uint16_t size = ntohs (msg->size);
   char cbuf [size];
-  size_t payload_size;
   int decrypted_size;
   uint16_t type;
   const struct GNUNET_MessageHeader *msgh;
@@ -3088,6 +3151,7 @@ GCT_handle_encrypted (struct CadetTunnel *t,
   case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
     {
       const struct GNUNET_CADET_Encrypted *emsg;
+      size_t payload_size;
 
       GNUNET_STATISTICS_update (stats, "# received OTR", 1, GNUNET_NO);
       emsg = (const struct GNUNET_CADET_Encrypted *) msg;
@@ -3124,7 +3188,7 @@ GCT_handle_encrypted (struct CadetTunnel *t,
      this loop may be unaligned, see util's MST for
      how to do this right. */
   off = 0;
-  while (off < decrypted_size)
+  while (off + sizeof (struct GNUNET_MessageHeader) <= decrypted_size)
   {
     uint16_t msize;
 
@@ -3135,6 +3199,11 @@ GCT_handle_encrypted (struct CadetTunnel *t,
       GNUNET_break_op (0);
       return;
     }
+    if (off + msize < decrypted_size)
+    {
+      GNUNET_break_op (0);
+      return;
+    }
     handle_decrypted (t, msgh, GNUNET_SYSERR);
     off += msize;
   }
@@ -3248,6 +3317,7 @@ GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
 void
 GCT_shutdown (void)
 {
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down tunnels\n");
   if (NULL != rekey_task)
   {
     GNUNET_SCHEDULER_cancel (rekey_task);
@@ -3376,18 +3446,13 @@ GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
  * of being created/processed.
  *
  * @param cls Closure (Tunnel to check).
- * @param tc Task context.
  */
 static void
-trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+trim_connections (void *cls)
 {
   struct CadetTunnel *t = cls;
 
   t->trim_connections_task = NULL;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
   if (GCT_count_connections (t) > 2 * CONNECTIONS_PER_TUNNEL)
   {
     struct CadetTConnection *iter;
@@ -3490,7 +3555,6 @@ GCT_remove_connection (struct CadetTunnel *t,
 
   /* Start new connections if needed */
   if (CONNECTIONS_PER_TUNNEL > conns
-      && NULL == t->destroy_task
       && CADET_TUNNEL_SHUTDOWN != t->cstate
       && GNUNET_NO == shutting_down)
   {
@@ -3534,8 +3598,11 @@ GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
 
   aux = GNUNET_new (struct CadetTChannel);
   aux->ch = ch;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
-  GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       " adding %p to %p\n", aux, t->channel_head);
+  GNUNET_CONTAINER_DLL_insert_tail (t->channel_head,
+                                   t->channel_tail,
+                                   aux);
 
   if (NULL != t->destroy_task)
   {
@@ -3563,7 +3630,9 @@ GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
     if (aux->ch == ch)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GCCH_2s (ch));
-      GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
+      GNUNET_CONTAINER_DLL_remove (t->channel_head,
+                                  t->channel_tail,
+                                  aux);
       GNUNET_free (aux);
       return;
     }
@@ -3605,25 +3674,18 @@ GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
  * the tunnel. This way we avoid a new public key handshake.
  *
  * @param cls Closure (tunnel to destroy).
- * @param tc Task context.
  */
 static void
-delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+delayed_destroy (void *cls)
 {
   struct CadetTunnel *t = cls;
   struct CadetTConnection *iter;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
-  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "Not destroying tunnel, due to shutdown. "
-         "Tunnel at %p should have been freed by GCT_shutdown\n", t);
-    return;
-  }
   t->destroy_task = NULL;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "delayed destroying tunnel %p\n",
+       t);
   t->cstate = CADET_TUNNEL_SHUTDOWN;
-
   for (iter = t->connection_head; NULL != iter; iter = iter->next)
   {
     GCC_send_destroy (iter->c);
@@ -3665,7 +3727,7 @@ GCT_destroy_empty (struct CadetTunnel *t)
   // FIXME make delay a config option
   t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
                                                   &delayed_destroy, t);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %llu\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %p\n",
        t, t->destroy_task);
 }
 
@@ -3709,8 +3771,9 @@ GCT_destroy (struct CadetTunnel *t)
   if (NULL == t)
     return;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GCP_2s (t->peer));
-
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "destroying tunnel %s\n",
+       GCP_2s (t->peer));
   GNUNET_break (GNUNET_YES ==
                 GNUNET_CONTAINER_multipeermap_remove (tunnels,
                                                       GCP_get_id (t->peer), t));
@@ -3741,12 +3804,17 @@ GCT_destroy (struct CadetTunnel *t)
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "one keepalive left behind on tunnel shutdown\n");
     }
+    else if (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY == type)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "tunnel destroyed before a CHANNEL_DESTROY was sent to peer\n");
+    }
     else
     {
       GNUNET_break (0);
-      LOG (GNUNET_ERROR_TYPE_WARNING,
+      LOG (GNUNET_ERROR_TYPE_ERROR,
            "message left behind on tunnel shutdown: %s\n",
-           GC_m2s (ntohs (mh->type)));
+           GC_m2s (type));
     }
     unqueue_data (t->tq_head);
   }
@@ -3754,14 +3822,16 @@ GCT_destroy (struct CadetTunnel *t)
 
   if (NULL != t->destroy_task)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling dest: %llX\n", t->destroy_task);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "cancelling dest: %p\n",
+        t->destroy_task);
     GNUNET_SCHEDULER_cancel (t->destroy_task);
     t->destroy_task = NULL;
   }
 
   if (NULL != t->trim_connections_task)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling trim: %llX\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling trim: %p\n",
          t->trim_connections_task);
     GNUNET_SCHEDULER_cancel (t->trim_connections_task);
     t->trim_connections_task = NULL;
@@ -3800,13 +3870,13 @@ GCT_destroy (struct CadetTunnel *t)
  * @return Connection created.
  */
 struct CadetConnection *
-GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
+GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *path)
 {
   struct CadetConnection *c;
   struct GNUNET_CADET_Hash cid;
   unsigned int own_pos;
 
-  if (NULL == t || NULL == p)
+  if (NULL == t || NULL == path)
   {
     GNUNET_break (0);
     return NULL;
@@ -3818,19 +3888,19 @@ GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
     return NULL;
   }
 
-  for (own_pos = 0; own_pos < p->length; own_pos++)
+  for (own_pos = 0; own_pos < path->length; own_pos++)
   {
-    if (p->peers[own_pos] == myid)
+    if (path->peers[own_pos] == myid)
       break;
   }
-  if (own_pos >= p->length)
+  if (own_pos >= path->length)
   {
     GNUNET_break_op (0);
     return NULL;
   }
 
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
-  c = GCC_new (&cid, t, p, own_pos);
+  c = GCC_new (&cid, t, path, own_pos);
   if (NULL == c)
   {
     /* Path was flawed */
@@ -4279,7 +4349,7 @@ GCT_send_ax_kx (struct CadetTunnel *t, int force_reply)
   struct GNUNET_CADET_AX_KX msg;
   enum GNUNET_CADET_AX_KX_Flags flags;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "===> AX_KX for %s\n", GCT_2s (t));
+  LOG (GNUNET_ERROR_TYPE_INFO, "==> {     AX_KX} on %s\n", GCT_2s (t));
   if (NULL != t->ephm_h)
   {
     LOG (GNUNET_ERROR_TYPE_INFO, "     already queued\n");
@@ -4296,7 +4366,7 @@ GCT_send_ax_kx (struct CadetTunnel *t, int force_reply)
   GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &msg.ratchet_key);
 
   t->ephm_h = send_kx (t, &msg.header);
-  if (CADET_TUNNEL_KEY_OK != t->estate)
+  if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
     GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
 }
 
@@ -4323,7 +4393,8 @@ GCT_resend_message (const struct GNUNET_MessageHeader *message,
     return;
   }
   fwd = GCC_is_origin (c, GNUNET_YES);
-  GNUNET_break (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
+  GNUNET_break (NULL == GCC_send_prebuilt_message (message, UINT16_MAX, 0,
+                                                   c, fwd,
                                                    GNUNET_YES, NULL, NULL));
 }
 
@@ -4521,12 +4592,12 @@ GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level)
   }
 #endif
   LOG2 (level, "TTT  tq_head %p, tq_tail %p\n", t->tq_head, t->tq_tail);
-  LOG2 (level, "TTT  destroy %u\n", t->destroy_task);
+  LOG2 (level, "TTT  destroy %p\n", t->destroy_task);
 
   LOG2 (level, "TTT  channels:\n");
   for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
   {
-    LOG2 (level, "TTT  - %s\n", GCCH_2s (iterch->ch));
+    GCCH_debug (iterch->ch, level);
   }
 
   LOG2 (level, "TTT  connections:\n");