Fixed connection authentication protocol aborting.
[silc.git] / lib / silccore / silcpacket.c
index 661fbbe47e53eff3bee4c8c0c19035ae1ef43ea2..8f1bded4ebaa69fb9ae6ccb8f94b810e3cc7f195 100644 (file)
@@ -825,6 +825,8 @@ SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
 
 void silc_packet_stream_destroy(SilcPacketStream stream)
 {
+  SilcPacketEngine engine;
+
   if (!stream)
     return;
 
@@ -841,17 +843,17 @@ void silc_packet_stream_destroy(SilcPacketStream stream)
 
   if (!stream->udp) {
     /* Delete from engine */
-    silc_mutex_lock(stream->sc->engine->lock);
-    silc_list_del(stream->sc->engine->streams, stream);
+    engine = stream->sc->engine;
+    silc_mutex_lock(engine->lock);
+    silc_list_del(engine->streams, stream);
 
     /* Remove per scheduler context, if it is not used anymore */
     if (stream->sc) {
       stream->sc->stream_count--;
       if (!stream->sc->stream_count)
-       silc_hash_table_del(stream->sc->engine->contexts,
-                           stream->sc->schedule);
+       silc_hash_table_del(engine->contexts, stream->sc->schedule);
     }
-    silc_mutex_unlock(stream->sc->engine->lock);
+    silc_mutex_unlock(engine->lock);
 
     /* Destroy the underlaying stream */
     if (stream->stream)
@@ -859,11 +861,13 @@ void silc_packet_stream_destroy(SilcPacketStream stream)
   } else {
     /* Delete from UDP remote hash table */
     char tuple[64];
-    silc_snprintf(tuple, sizeof(tuple), "%d%s", stream->remote_udp->remote_port,
-            stream->remote_udp->remote_ip);
-    silc_mutex_lock(stream->sc->engine->lock);
-    silc_hash_table_del(stream->sc->engine->udp_remote, tuple);
-    silc_mutex_unlock(stream->sc->engine->lock);
+    engine = stream->sc->engine;
+    silc_snprintf(tuple, sizeof(tuple), "%d%s",
+                 stream->remote_udp->remote_port
+                 stream->remote_udp->remote_ip);
+    silc_mutex_lock(engine->lock);
+    silc_hash_table_del(engine->udp_remote, tuple);
+    silc_mutex_unlock(engine->lock);
 
     silc_free(stream->remote_udp->remote_ip);
     silc_free(stream->remote_udp);
@@ -1373,12 +1377,15 @@ static inline void silc_packet_send_ctr_increment(SilcPacketStream stream,
                                                  unsigned char *ret_iv)
 {
   unsigned char *iv = silc_cipher_get_iv(cipher);
-  SilcUInt32 pc;
+  SilcUInt32 pc1, pc2;
 
-  /* Increment packet counter */
-  SILC_GET32_MSB(pc, iv + 8);
-  pc++;
-  SILC_PUT32_MSB(pc, iv + 8);
+  /* Increment 64-bit packet counter */
+  SILC_GET32_MSB(pc1, iv + 4);
+  SILC_GET32_MSB(pc2, iv + 8);
+  if (++pc2 == 0)
+    ++pc1;
+  SILC_PUT32_MSB(pc1, iv + 4);
+  SILC_PUT32_MSB(pc2, iv + 8);
 
   /* Reset block counter */
   memset(iv + 12, 0, 4);
@@ -1390,7 +1397,7 @@ static inline void silc_packet_send_ctr_increment(SilcPacketStream stream,
     ret_iv[1] = ret_iv[0] + iv[4];
     ret_iv[2] = ret_iv[0] ^ ret_iv[1];
     ret_iv[3] = ret_iv[0] + ret_iv[2];
-    SILC_PUT32_MSB(pc, ret_iv + 4);
+    SILC_PUT32_MSB(pc2, ret_iv + 4);
     SILC_LOG_HEXDUMP(("IV"), ret_iv, 8);
 
     /* Set new nonce to counter block */
@@ -1532,6 +1539,7 @@ static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
   /* Encrypt the packet */
   if (silc_likely(cipher)) {
     SILC_LOG_DEBUG(("Encrypting packet"));
+    silc_cipher_set_iv(cipher, NULL);
     if (silc_unlikely(!silc_cipher_encrypt(cipher, packet.data + ivlen,
                                           packet.data + ivlen, enclen,
                                           NULL))) {
@@ -1722,16 +1730,19 @@ static inline void silc_packet_receive_ctr_increment(SilcPacketStream stream,
                                                     unsigned char *iv,
                                                     unsigned char *packet_iv)
 {
-  SilcUInt32 pc;
+  SilcUInt32 pc1, pc2;
 
   /* If IV Included flag, set the IV from packet to block counter. */
   if (stream->iv_included) {
     memcpy(iv + 4, packet_iv, 8);
   } else {
-    /* Increment packet counter */
-    SILC_GET32_MSB(pc, iv + 8);
-    pc++;
-    SILC_PUT32_MSB(pc, iv + 8);
+    /* Increment 64-bit packet counter. */
+    SILC_GET32_MSB(pc1, iv + 4);
+    SILC_GET32_MSB(pc2, iv + 8);
+    if (++pc2 == 0)
+      ++pc1;
+    SILC_PUT32_MSB(pc1, iv + 4);
+    SILC_PUT32_MSB(pc2, iv + 8);
   }
 
   /* Reset block counter */
@@ -2049,8 +2060,9 @@ static void silc_packet_read_process(SilcPacketStream stream)
          silc_packet_receive_ctr_increment(stream, iv, NULL);
       }
 
-      silc_cipher_decrypt(cipher, inbuf->data + ivlen, tmp,
-                         block_len, iv);
+      if (silc_cipher_get_mode(cipher) == SILC_CIPHER_MODE_CTR)
+       silc_cipher_set_iv(cipher, NULL);
+      silc_cipher_decrypt(cipher, inbuf->data + ivlen, tmp, block_len, iv);
 
       header = tmp;
       if (stream->iv_included) {