*/
struct CadetTunnelKXCtx
{
+ /**
+ * Encryption ("our") old key, for encrypting traffic sent by us
+ * end before the key exchange is finished or times out.
+ */
+ struct GNUNET_CRYPTO_SymmetricSessionKey e_key_old;
+
/**
* Decryption ("their") old key, for decrypting traffic sent by the
* other end before the key exchange started.
* Challenge to send in a ping and expect in the pong.
*/
uint32_t challenge;
+
+ /**
+ * When the rekey started. One minute after this the new key will be used.
+ */
+ struct GNUNET_TIME_Absolute rekey_start_time;
+
+ /**
+ * Task for delayed destruction of the Key eXchange context, to allow delayed
+ * messages with the old key to be decrypted successfully.
+ */
+ GNUNET_SCHEDULER_TaskIdentifier finish_task;
};
/**
* Struct containing all information regarding a tunnel to a peer.
*/
-struct CadetTunnel3
+struct CadetTunnel
{
/**
* Endpoint of the tunnel.
/**
* State of the tunnel connectivity.
*/
- enum CadetTunnel3CState cstate;
+ enum CadetTunnelCState cstate;
/**
* State of the tunnel encryption.
*/
- enum CadetTunnel3EState estate;
+ enum CadetTunnelEState estate;
/**
* Key eXchange context.
*/
struct CadetTunnelKXCtx *kx_ctx;
+ /**
+ * Peer's ephemeral key, to recreate @c e_key and @c d_key when own ephemeral
+ * key changes.
+ */
+ struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
+
/**
* Encryption ("our") key.
*/
/**
* Tunnel.
*/
- struct CadetTunnel3 *t;
+ struct CadetTunnel *t;
/**
* Tunnel queue given to the channel to cancel request. Update on send_queued.
*/
- struct CadetTunnel3Queue *tq;
+ struct CadetTunnelQueue *tq;
/**
* Message to send.
/**
* Handle for messages queued but not yet sent.
*/
-struct CadetTunnel3Queue
+struct CadetTunnelQueue
{
/**
* Connection queue handle, to cancel if necessary.
* @return String representation.
*/
static const char *
-cstate2s (enum CadetTunnel3CState cs)
+cstate2s (enum CadetTunnelCState cs)
{
static char buf[128];
* @return String representation.
*/
static const char *
-estate2s (enum CadetTunnel3EState es)
+estate2s (enum CadetTunnelEState es)
{
static char buf[128];
* @return #GNUNET_YES if ready, #GNUNET_NO otherwise
*/
static int
-is_ready (struct CadetTunnel3 *t)
+is_ready (struct CadetTunnel *t)
{
int ready;
* @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
*/
int
-check_ephemeral (struct CadetTunnel3 *t,
+check_ephemeral (struct CadetTunnel *t,
const struct GNUNET_CADET_KX_Ephemeral *msg)
{
/* Check message size */
}
+/**
+ * Calculate HMAC.
+ *
+ * @param t Tunnel to get keys from.
+ * @param plaintext Content to HMAC.
+ * @param size Size of @c plaintext.
+ * @param iv Initialization vector for the message.
+ * @param outgoing Is this an outgoing message that we encrypted?
+ * @param hmac Destination to store the HMAC.
+ */
+static void
+t_hmac (struct CadetTunnel *t, const void *plaintext, size_t size, uint32_t iv,
+ int outgoing, struct GNUNET_CADET_Hash *hmac)
+{
+ struct GNUNET_CRYPTO_AuthKey auth_key;
+ static const char ctx[] = "cadet authentication key";
+ struct GNUNET_CRYPTO_SymmetricSessionKey *key;
+ struct GNUNET_HashCode hash;
+
+ key = outgoing ? &t->e_key : &t->d_key;
+ GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
+ &iv, sizeof (iv),
+ key, sizeof (*key),
+ ctx, sizeof (ctx),
+ NULL);
+ GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
+ memcpy (hmac, &hash, sizeof (*hmac));
+}
+
+
/**
* Encrypt data with the tunnel key.
*
* @param src Source of the plaintext. Can overlap with @c dst.
* @param size Size of the plaintext.
* @param iv Initialization Vector to use.
+ * @param force_newest_key Force the use of the newest key, otherwise
+ * CADET will use the old key when allowed.
+ * This can happen in the case when a KX is going on
+ * and the old one hasn't expired.
*/
static int
-t_encrypt (struct CadetTunnel3 *t,
- void *dst, const void *src,
- size_t size, uint32_t iv)
+t_encrypt (struct CadetTunnel *t, void *dst, const void *src,
+ size_t size, uint32_t iv, int force_newest_key)
{
struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
+ struct GNUNET_CRYPTO_SymmetricSessionKey *e_key;
size_t out_size;
LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt start\n");
- GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (iv), NULL);
+ if (GNUNET_NO == force_newest_key
+ && NULL != t->kx_ctx
+ && GNUNET_SCHEDULER_NO_TASK == t->kx_ctx->finish_task)
+ {
+ struct GNUNET_TIME_Relative age;
+
+ age = GNUNET_TIME_absolute_get_duration (t->kx_ctx->rekey_start_time);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " key exchange in progress, started %s ago\n",
+ GNUNET_STRINGS_relative_time_to_string (age, GNUNET_YES));
+ if (age.rel_value_us < GNUNET_TIME_UNIT_MINUTES.rel_value_us)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " using old key\n");
+ e_key = &t->kx_ctx->e_key_old;
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " using new key\n");
+ e_key = &t->e_key;
+ }
+ }
+ else
+ {
+ e_key = &t->e_key;
+ }
+ GNUNET_CRYPTO_symmetric_derive_iv (&siv, e_key, &iv, sizeof (iv), NULL);
LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt IV derived\n");
- out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
+ out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, e_key, &siv, dst);
LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt end\n");
return out_size;
/**
- * Decrypt data with the tunnel key.
+ * Decrypt and verify data with the appropriate tunnel key.
+ *
+ * @param key Key to use.
+ * @param dst Destination for the plaintext.
+ * @param src Source of the encrypted data. Can overlap with @c dst.
+ * @param size Size of the encrypted data.
+ * @param iv Initialization Vector to use.
+ *
+ * @return Size of the decrypted data, -1 if an error was encountered.
+ */
+static int
+decrypt (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
+ void *dst, const void *src, size_t size, uint32_t iv)
+{
+ struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
+ size_t out_size;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt start\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt iv\n");
+ GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt iv done\n");
+ out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt end\n");
+
+ return out_size;
+}
+
+
+/**
+ * Decrypt and verify data with the most recent tunnel key.
*
* @param t Tunnel whose key to use.
* @param dst Destination for the plaintext.
* @param src Source of the encrypted data. Can overlap with @c dst.
* @param size Size of the encrypted data.
* @param iv Initialization Vector to use.
+ *
+ * @return Size of the decrypted data, -1 if an error was encountered.
*/
static int
-t_decrypt (struct CadetTunnel3 *t,
- void *dst, const void *src,
+t_decrypt (struct CadetTunnel *t, void *dst, const void *src,
size_t size, uint32_t iv)
{
- struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
struct GNUNET_CRYPTO_SymmetricSessionKey *key;
size_t out_size;
{
key = &t->d_key;
}
- else if (NULL != t->kx_ctx)
- {
- key = &t->kx_ctx->d_key_old;
- }
else
{
GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "WARNING got data on %s without a valid key\n",
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "got data on %s without a valid key\n",
GCT_2s (t));
GCT_debug (t);
- return 0;
+ return -1;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv\n");
- GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv done\n");
- out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt end\n");
+ out_size = decrypt (key, dst, src, size, iv);
return out_size;
}
+/**
+ * Decrypt and verify data with the appropriate tunnel key and verify that the
+ * data has not been altered since it was sent by the remote peer.
+ *
+ * @param t Tunnel whose key to use.
+ * @param dst Destination for the plaintext.
+ * @param src Source of the encrypted data. Can overlap with @c dst.
+ * @param size Size of the encrypted data.
+ * @param iv Initialization Vector to use.
+ * @param msg_hmac HMAC of the message, cannot be NULL.
+ *
+ * @return Size of the decrypted data, -1 if an error was encountered.
+ */
+static int
+t_decrypt_and_validate (struct CadetTunnel *t,
+ void *dst, const void *src,
+ size_t size, uint32_t iv,
+ const struct GNUNET_CADET_Hash *msg_hmac)
+{
+ struct GNUNET_CRYPTO_SymmetricSessionKey *key;
+ struct GNUNET_CADET_Hash hmac;
+ int decrypted_size;
+
+ /* Try primary (newest) key */
+ key = &t->d_key;
+ decrypted_size = decrypt (key, dst, src, size, iv);
+ t_hmac (t, src, size, iv, GNUNET_NO, &hmac);
+ if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
+ return decrypted_size;
+
+ /* If no key exchange is going on, we just failed */
+ if (NULL == t->kx_ctx)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Failed checksum validation on tunnel %s with no KX\n",
+ GCT_2s (t));
+ GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
+ return -1;
+ }
+
+ /* Try secondary (from previous KX period) key */
+ key = &t->kx_ctx->d_key_old;
+ decrypted_size = decrypt (key, dst, src, size, iv);
+ t_hmac (t, src, size, iv, GNUNET_NO, &hmac);
+ if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
+ return decrypted_size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Failed checksum validation on tunnel %s with KX\n",
+ GCT_2s (t));
+ GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
+ return -1;
+}
+
+
/**
* Create key material by doing ECDH on the local and remote ephemeral keys.
*
}
}
+
/**
* Create a symmetic key from the identities of both ends and the key material
* from ECDH.
NULL);
}
+
+/**
+ * Derive the tunnel's keys using our own and the peer's ephemeral keys.
+ *
+ * @param t Tunnel for which to create the keys.
+ */
+static void
+create_keys (struct CadetTunnel *t)
+{
+ struct GNUNET_HashCode km;
+
+ derive_key_material (&km, &t->peers_ephemeral_key);
+ derive_symmertic (&t->e_key, &my_full_id, GCP_get_id (t->peer), &km);
+ derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
+}
+
+
/**
* Pick a connection on which send the next data message.
*
* @return The connection on which to send the next message.
*/
static struct CadetConnection *
-tunnel_get_connection (struct CadetTunnel3 *t)
+tunnel_get_connection (struct CadetTunnel *t)
{
struct CadetTConnection *iter;
struct CadetConnection *best;
struct CadetConnectionQueue *q,
uint16_t type, int fwd, size_t size)
{
- struct CadetTunnel3Queue *qt = cls;
- struct CadetTunnel3 *t;
+ struct CadetTunnelQueue *qt = cls;
+ struct CadetTunnel *t;
LOG (GNUNET_ERROR_TYPE_DEBUG, "tun_message_sent\n");
* @param msg Message itself (copy will be made).
*/
static struct CadetTunnelDelayed *
-queue_data (struct CadetTunnel3 *t, const struct GNUNET_MessageHeader *msg)
+queue_data (struct CadetTunnel *t, const struct GNUNET_MessageHeader *msg)
{
struct CadetTunnelDelayed *tqd;
uint16_t size = ntohs (msg->size);
}
-/**
- * Calculate HMAC.
- *
- * @param t Tunnel to get keys from.
- * @param plaintext Content to HMAC.
- * @param size Size of @c plaintext.
- * @param iv Initialization vector for the message.
- * @param outgoing Is this an outgoing message that we encrypted?
- * @param hmac Destination to store the HMAC.
- */
-static void
-t_hmac (struct CadetTunnel3 *t, const void *plaintext, size_t size, uint32_t iv,
- int outgoing, struct GNUNET_CADET_Hash *hmac)
-{
- struct GNUNET_CRYPTO_AuthKey auth_key;
- static const char ctx[] = "cadet authentication key";
- struct GNUNET_CRYPTO_SymmetricSessionKey *key;
- struct GNUNET_HashCode hash;
-
- key = outgoing ? &t->e_key : &t->d_key;
- GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
- &iv, sizeof (iv),
- key, sizeof (*key),
- ctx, sizeof (ctx),
- NULL);
- GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
- memcpy (hmac, &hash, sizeof (*hmac));
-}
-
-
/**
* Sends an already built message on a tunnel, encrypting it and
* choosing the best connection.
*
* @return Handle to cancel message. NULL if @c cont is NULL.
*/
-static struct CadetTunnel3Queue *
+static struct CadetTunnelQueue *
send_prebuilt_message (const struct GNUNET_MessageHeader *message,
- struct CadetTunnel3 *t, struct CadetConnection *c,
+ struct CadetTunnel *t, struct CadetConnection *c,
int force, GCT_sent cont, void *cont_cls,
- struct CadetTunnel3Queue *existing_q)
+ struct CadetTunnelQueue *existing_q)
{
- struct CadetTunnel3Queue *tq;
+ struct CadetTunnelQueue *tq;
struct GNUNET_CADET_Encrypted *msg;
size_t size = ntohs (message->size);
char cbuf[sizeof (struct GNUNET_CADET_Encrypted) + size];
tqd = queue_data (t, message);
if (NULL == cont)
return NULL;
- tq = GNUNET_new (struct CadetTunnel3Queue);
+ tq = GNUNET_new (struct CadetTunnelQueue);
tq->tqd = tqd;
tqd->tq = tq;
tq->cont = cont;
msg = (struct GNUNET_CADET_Encrypted *) cbuf;
msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED);
msg->iv = iv;
- GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv) == size);
+ GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv, GNUNET_NO) == size);
t_hmac (t, &msg[1], size, iv, GNUNET_YES, &msg->hmac);
msg->header.size = htons (sizeof (struct GNUNET_CADET_Encrypted) + size);
}
if (NULL == existing_q)
{
- tq = GNUNET_new (struct CadetTunnel3Queue); /* FIXME valgrind: leak*/
+ tq = GNUNET_new (struct CadetTunnelQueue); /* FIXME valgrind: leak*/
}
else
{
* @param t Tunnel that holds the messages. Cannot be loopback.
*/
static void
-send_queued_data (struct CadetTunnel3 *t)
+send_queued_data (struct CadetTunnel *t)
{
struct CadetTunnelDelayed *tqd;
struct CadetTunnelDelayed *next;
* @param message Message to send. Function modifies it.
*/
static void
-send_kx (struct CadetTunnel3 *t,
+send_kx (struct CadetTunnel *t,
const struct GNUNET_MessageHeader *message)
{
struct CadetConnection *c;
* @param t Tunnel on which to send the key.
*/
static void
-send_ephemeral (struct CadetTunnel3 *t)
+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));
kx_msg.sender_status = htonl (t->estate);
send_kx (t, &kx_msg.header);
* @param t Tunnel on which to send the ping.
*/
static void
-send_ping (struct CadetTunnel3 *t)
+send_ping (struct CadetTunnel *t)
{
struct GNUNET_CADET_KX_Ping msg;
- LOG (GNUNET_ERROR_TYPE_INFO, "=> PING for %s\n", GCT_2s (t));
+ LOG (GNUNET_ERROR_TYPE_INFO, "===> PING for %s\n", GCT_2s (t));
msg.header.size = htons (sizeof (msg));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PING);
msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
- t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
+ t_encrypt (t, &msg.target, &msg.target,
+ ping_encryption_size(), msg.iv, GNUNET_YES);
LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
/**
* Send a pong message on a tunnel.
- *
+ *d_
* @param t Tunnel on which to send the pong.
* @param challenge Value sent in the ping that we have to send back.
*/
static void
-send_pong (struct CadetTunnel3 *t, uint32_t challenge)
+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));
msg.header.size = htons (sizeof (msg));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PONG);
msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
msg.nonce = challenge;
LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
- t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
+ t_encrypt (t, &msg.nonce, &msg.nonce,
+ sizeof (msg.nonce), msg.iv, GNUNET_YES);
LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
send_kx (t, &msg.header);
static void
rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- struct CadetTunnel3 *t = cls;
+ struct CadetTunnel *t = cls;
t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
t->kx_ctx->challenge =
GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
t->kx_ctx->d_key_old = t->d_key;
+ t->kx_ctx->e_key_old = t->e_key;
+ create_keys (t);
+ t->kx_ctx->rekey_start_time = GNUNET_TIME_absolute_get ();
LOG (GNUNET_ERROR_TYPE_DEBUG, " new challenge for %s: %u\n",
GCT_2s (t), t->kx_ctx->challenge);
}
+
send_ephemeral (t);
+
switch (t->estate)
{
case CADET_TUNNEL3_KEY_UNINITIALIZED:
LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
}
+ // FIXME exponential backoff
LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
/**
- * Out ephemeral key has changed, create new session key on all tunnels.
+ * Our ephemeral key has changed, create new session key on all tunnels.
+ *
+ * Each tunnel will start the Key Exchange with a random delay between
+ * 0 and number_of_tunnels*100 milliseconds, so there are 10 key exchanges
+ * per second, on average.
*
* @param cls Closure (size of the hashmap).
* @param key Current public key.
const struct GNUNET_PeerIdentity *key,
void *value)
{
- struct CadetTunnel3 *t = value;
+ struct CadetTunnel *t = value;
struct GNUNET_TIME_Relative delay;
long n = (long) cls;
uint32_t r;
const struct GNUNET_PeerIdentity *key,
void *value)
{
- struct CadetTunnel3 *t = value;
+ struct CadetTunnel *t = value;
LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_shutdown destroying tunnel at %p\n", t);
GCT_destroy (t);
* @param gid ID of the channel.
*/
static void
-send_channel_destroy (struct CadetTunnel3 *t, unsigned int gid)
+send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
{
struct GNUNET_CADET_ChannelManage msg;
* #GNUNET_SYSERR if message on a one-ended channel (remote)
*/
static void
-handle_data (struct CadetTunnel3 *t,
+handle_data (struct CadetTunnel *t,
const struct GNUNET_CADET_Data *msg,
int fwd)
{
* #GNUNET_SYSERR if message on a one-ended channel (remote)
*/
static void
-handle_data_ack (struct CadetTunnel3 *t,
+handle_data_ack (struct CadetTunnel *t,
const struct GNUNET_CADET_DataACK *msg,
int fwd)
{
* @param msg Data message.
*/
static void
-handle_ch_create (struct CadetTunnel3 *t,
+handle_ch_create (struct CadetTunnel *t,
const struct GNUNET_CADET_ChannelCreate *msg)
{
struct CadetChannel *ch;
* @param msg NACK message.
*/
static void
-handle_ch_nack (struct CadetTunnel3 *t,
+handle_ch_nack (struct CadetTunnel *t,
const struct GNUNET_CADET_ChannelManage *msg)
{
struct CadetChannel *ch;
* #GNUNET_SYSERR if message on a one-ended channel (remote)
*/
static void
-handle_ch_ack (struct CadetTunnel3 *t,
+handle_ch_ack (struct CadetTunnel *t,
const struct GNUNET_CADET_ChannelManage *msg,
int fwd)
{
}
-
/**
* Handle a channel destruction message.
*
* #GNUNET_SYSERR if message on a one-ended channel (remote)
*/
static void
-handle_ch_destroy (struct CadetTunnel3 *t,
+handle_ch_destroy (struct CadetTunnel *t,
const struct GNUNET_CADET_ChannelManage *msg,
int fwd)
{
* @param msg Key eXchange message.
*/
static void
-handle_ephemeral (struct CadetTunnel3 *t,
+handle_ephemeral (struct CadetTunnel *t,
const struct GNUNET_CADET_KX_Ephemeral *msg)
{
- struct GNUNET_HashCode km;
LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GCT_2s (t));
if (GNUNET_OK != check_ephemeral (t, msg))
GNUNET_break_op (0);
return;
}
- derive_key_material (&km, &msg->ephemeral_key);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " km is %s\n", GNUNET_h2s (&km));
- derive_symmertic (&t->e_key, &my_full_id, GCP_get_id (t->peer), &km);
- derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
+ t->peers_ephemeral_key = msg->ephemeral_key;
+ create_keys (t);
if (CADET_TUNNEL3_KEY_SENT == t->estate)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, send ping\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, sending ping\n");
send_ping (t);
t->estate = CADET_TUNNEL3_KEY_PING;
}
* @param msg Key eXchange Ping message.
*/
static void
-handle_ping (struct CadetTunnel3 *t,
+handle_ping (struct CadetTunnel *t,
const struct GNUNET_CADET_KX_Ping *msg)
{
struct GNUNET_CADET_KX_Ping res;
send_pong (t, res.nonce);
}
+/**
+ * @brief Finish the Key eXchange and destory 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)
+{
+ struct CadetTunnel *t = cls;
+
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
+
+ GNUNET_free (t->kx_ctx);
+ t->kx_ctx = NULL;
+}
/**
* @param msg Key eXchange Pong message.
*/
static void
-handle_pong (struct CadetTunnel3 *t,
+handle_pong (struct CadetTunnel *t,
const struct GNUNET_CADET_KX_Pong *msg)
{
uint32_t challenge;
}
GNUNET_SCHEDULER_cancel (t->rekey_task);
t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
- GNUNET_free (t->kx_ctx);
- t->kx_ctx = NULL;
+
+ /* Don't free the old keys right away, but after a delay.
+ * Rationale: the KX could have happened over a very fast connection,
+ * with payload traffic still signed with the old key stuck in a slower
+ * connection.
+ */
+ if (GNUNET_SCHEDULER_NO_TASK == t->kx_ctx->finish_task)
+ {
+ t->kx_ctx->finish_task =
+ GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES, finish_kx, t);
+ }
GCT_change_estate (t, CADET_TUNNEL3_KEY_OK);
}
* #GNUNET_SYSERR if message on a one-ended channel (remote)
*/
static void
-handle_decrypted (struct CadetTunnel3 *t,
+handle_decrypted (struct CadetTunnel *t,
const struct GNUNET_MessageHeader *msgh,
int fwd)
{
* @param msg Encrypted message.
*/
void
-GCT_handle_encrypted (struct CadetTunnel3 *t,
+GCT_handle_encrypted (struct CadetTunnel *t,
const struct GNUNET_CADET_Encrypted *msg)
{
size_t size = ntohs (msg->header.size);
size_t payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
- size_t decrypted_size;
+ int decrypted_size;
char cbuf [payload_size];
struct GNUNET_MessageHeader *msgh;
unsigned int off;
- struct GNUNET_CADET_Hash hmac;
- decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
- t_hmac (t, &msg[1], payload_size, msg->iv, GNUNET_NO, &hmac);
- if (0 != memcmp (&hmac, &msg->hmac, sizeof (hmac)))
- {
- /* checksum failed */
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed checksum validation for a message on tunnel `%s'\n",
- GCT_2s (t));
- GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
- return;
- }
+ decrypted_size = t_decrypt_and_validate (t, cbuf, &msg[1], payload_size,
+ msg->iv, &msg->hmac);
+
off = 0;
while (off < decrypted_size)
{
* @param message Payload of KX message.
*/
void
-GCT_handle_kx (struct CadetTunnel3 *t,
+GCT_handle_kx (struct CadetTunnel *t,
const struct GNUNET_MessageHeader *message)
{
uint16_t type;
*
* @param destination Peer this tunnel is towards.
*/
-struct CadetTunnel3 *
+struct CadetTunnel *
GCT_new (struct CadetPeer *destination)
{
- struct CadetTunnel3 *t;
+ struct CadetTunnel *t;
- t = GNUNET_new (struct CadetTunnel3);
+ t = GNUNET_new (struct CadetTunnel);
t->next_chid = 0;
t->peer = destination;
* @param cstate New connection state.
*/
void
-GCT_change_cstate (struct CadetTunnel3* t, enum CadetTunnel3CState cstate)
+GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
{
if (NULL == t)
return;
* @param state New encryption state.
*/
void
-GCT_change_estate (struct CadetTunnel3* t, enum CadetTunnel3EState state)
+GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
{
if (NULL == t)
return;
static void
trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- struct CadetTunnel3 *t = cls;
+ struct CadetTunnel *t = cls;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
* @param c Connection.
*/
void
-GCT_add_connection (struct CadetTunnel3 *t, struct CadetConnection *c)
+GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c)
{
struct CadetTConnection *aux;
* @param path Invalid path to remove. Is destroyed after removal.
*/
void
-GCT_remove_path (struct CadetTunnel3 *t, struct CadetPeerPath *path)
+GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path)
{
GCP_remove_path (t->peer, path);
}
* @param c Connection.
*/
void
-GCT_remove_connection (struct CadetTunnel3 *t,
+GCT_remove_connection (struct CadetTunnel *t,
struct CadetConnection *c)
{
struct CadetTConnection *aux;
* @param ch Channel.
*/
void
-GCT_add_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
+GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
{
struct CadetTChannel *aux;
* @param ch Channel.
*/
void
-GCT_remove_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
+GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
{
struct CadetTChannel *aux;
* @return channel handler, NULL if doesn't exist
*/
struct CadetChannel *
-GCT_get_channel (struct CadetTunnel3 *t, CADET_ChannelNumber chid)
+GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
{
struct CadetTChannel *iter;
static void
delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- struct CadetTunnel3 *t = cls;
+ struct CadetTunnel *t = cls;
struct CadetTConnection *iter;
LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
* @param t Tunnel to destroy.
*/
void
-GCT_destroy_empty (struct CadetTunnel3 *t)
+GCT_destroy_empty (struct CadetTunnel *t)
{
if (GNUNET_YES == shutting_down)
return; /* Will be destroyed immediately anyway */
* @param t Tunnel to destroy if empty.
*/
void
-GCT_destroy_if_empty (struct CadetTunnel3 *t)
+GCT_destroy_if_empty (struct CadetTunnel *t)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s destroy if empty\n", GCT_2s (t));
if (1 < GCT_count_channels (t))
* @param t The tunnel to destroy.
*/
void
-GCT_destroy (struct CadetTunnel3 *t)
+GCT_destroy (struct CadetTunnel *t)
{
struct CadetTConnection *iter_c;
struct CadetTConnection *next_c;
{
GNUNET_SCHEDULER_cancel (t->rekey_task);
t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
- if (NULL != t->kx_ctx)
- GNUNET_free (t->kx_ctx);
- else
- GNUNET_break (0);
}
-
+ if (NULL != t->kx_ctx)
+ {
+ if (GNUNET_SCHEDULER_NO_TASK != t->kx_ctx->finish_task)
+ GNUNET_SCHEDULER_cancel (t->kx_ctx->finish_task);
+ GNUNET_free (t->kx_ctx);
+ }
GNUNET_free (t);
}
* @return Connection created.
*/
struct CadetConnection *
-GCT_use_path (struct CadetTunnel3 *t, struct CadetPeerPath *p)
+GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
{
struct CadetConnection *c;
struct GNUNET_CADET_Hash cid;
* @return Number of connections.
*/
unsigned int
-GCT_count_connections (struct CadetTunnel3 *t)
+GCT_count_connections (struct CadetTunnel *t)
{
struct CadetTConnection *iter;
unsigned int count;
* @return Number of channels.
*/
unsigned int
-GCT_count_channels (struct CadetTunnel3 *t)
+GCT_count_channels (struct CadetTunnel *t)
{
struct CadetTChannel *iter;
unsigned int count;
*
* @return Tunnel's connectivity state.
*/
-enum CadetTunnel3CState
-GCT_get_cstate (struct CadetTunnel3 *t)
+enum CadetTunnelCState
+GCT_get_cstate (struct CadetTunnel *t)
{
if (NULL == t)
{
GNUNET_assert (0);
- return (enum CadetTunnel3CState) -1;
+ return (enum CadetTunnelCState) -1;
}
return t->cstate;
}
*
* @return Tunnel's encryption state.
*/
-enum CadetTunnel3EState
-GCT_get_estate (struct CadetTunnel3 *t)
+enum CadetTunnelEState
+GCT_get_estate (struct CadetTunnel *t)
{
if (NULL == t)
{
GNUNET_assert (0);
- return (enum CadetTunnel3EState) -1;
+ return (enum CadetTunnelEState) -1;
}
return t->estate;
}
* @return Biggest buffer space offered by any channel in the tunnel.
*/
unsigned int
-GCT_get_channels_buffer (struct CadetTunnel3 *t)
+GCT_get_channels_buffer (struct CadetTunnel *t)
{
struct CadetTChannel *iter;
unsigned int buffer;
* @return Buffer space offered by all connections in the tunnel.
*/
unsigned int
-GCT_get_connections_buffer (struct CadetTunnel3 *t)
+GCT_get_connections_buffer (struct CadetTunnel *t)
{
struct CadetTConnection *iter;
unsigned int buffer;
* @return ID of the destination peer.
*/
const struct GNUNET_PeerIdentity *
-GCT_get_destination (struct CadetTunnel3 *t)
+GCT_get_destination (struct CadetTunnel *t)
{
return GCP_get_id (t->peer);
}
* @return GID of a channel free to use.
*/
CADET_ChannelNumber
-GCT_get_next_chid (struct CadetTunnel3 *t)
+GCT_get_next_chid (struct CadetTunnel *t)
{
CADET_ChannelNumber chid;
CADET_ChannelNumber mask;
* @param t Channel which has some free buffer space.
*/
void
-GCT_unchoke_channels (struct CadetTunnel3 *t)
+GCT_unchoke_channels (struct CadetTunnel *t)
{
struct CadetTChannel *iter;
unsigned int buffer;
* @param t Tunnel.
*/
void
-GCT_send_connection_acks (struct CadetTunnel3 *t)
+GCT_send_connection_acks (struct CadetTunnel *t)
{
struct CadetTConnection *iter;
uint32_t allowed;
* @param q Handle to the queue.
*/
void
-GCT_cancel (struct CadetTunnel3Queue *q)
+GCT_cancel (struct CadetTunnelQueue *q)
{
if (NULL != q->cq)
{
*
* @return Handle to cancel message. NULL if @c cont is NULL.
*/
-struct CadetTunnel3Queue *
+struct CadetTunnelQueue *
GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
- struct CadetTunnel3 *t, struct CadetConnection *c,
+ struct CadetTunnel *t, struct CadetConnection *c,
int force, GCT_sent cont, void *cont_cls)
{
return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
* @return #GNUNET_YES if it is loopback.
*/
int
-GCT_is_loopback (const struct CadetTunnel3 *t)
+GCT_is_loopback (const struct CadetTunnel *t)
{
return (myid == GCP_get_short_id (t->peer));
}
* @return #GNUNET_YES a connection uses this path.
*/
int
-GCT_is_path_used (const struct CadetTunnel3 *t, const struct CadetPeerPath *p)
+GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p)
{
struct CadetTConnection *iter;
* @return Cost of the path (path length + number of overlapping nodes)
*/
unsigned int
-GCT_get_path_cost (const struct CadetTunnel3 *t,
+GCT_get_path_cost (const struct CadetTunnel *t,
const struct CadetPeerPath *path)
{
struct CadetTConnection *iter;
* @return Static string the destination peer's ID.
*/
const char *
-GCT_2s (const struct CadetTunnel3 *t)
+GCT_2s (const struct CadetTunnel *t)
{
if (NULL == t)
return "(NULL)";
* @param t Tunnel to debug.
*/
void
-GCT_debug (const struct CadetTunnel3 *t)
+GCT_debug (const struct CadetTunnel *t)
{
struct CadetTChannel *iterch;
struct CadetTConnection *iterc;
* @param cls Closure for @c iter.
*/
void
-GCT_iterate_connections (struct CadetTunnel3 *t, GCT_conn_iter iter, void *cls)
+GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls)
{
struct CadetTConnection *ct;
* @param cls Closure for @c iter.
*/
void
-GCT_iterate_channels (struct CadetTunnel3 *t, GCT_chan_iter iter, void *cls)
+GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls)
{
struct CadetTChannel *cht;