ROBODoc documented lib/silcrypt/silccipher.h. Patch by Toni
[silc.git] / lib / silccore / silcpacket.c
index 7c3f2b0d90cb40a5f0a49d5646f3b3209f923571..c6cba515af1b558f8ad96060113a9c574fd56b5b 100644 (file)
@@ -101,8 +101,9 @@ void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, SilcUInt32 sequence,
   /* Encrypt the data area of the packet. */
   if (cipher) {
     SILC_LOG_DEBUG(("Encrypting packet, cipher %s, len %d", 
-                   cipher->cipher->name, len));
-    silc_cipher_encrypt(cipher, buffer->data, buffer->data, len, cipher->iv);
+                   silc_cipher_get_name(cipher), len));
+    silc_cipher_encrypt(cipher, buffer->data, buffer->data, len,
+                       silc_cipher_get_iv(cipher));
   }
 
   /* Pull the HMAC into the visible data area in the buffer */
@@ -326,14 +327,15 @@ bool silc_packet_receive_process(SilcSocketConnection sock,
     /* Decrypt first 16 bytes of the packet */
     if (!SILC_IS_INBUF_PENDING(sock) && cipher)
       silc_cipher_decrypt(cipher, sock->inbuf->data, sock->inbuf->data, 
-                         SILC_PACKET_MIN_HEADER_LEN, cipher->iv);
+                         SILC_PACKET_MIN_HEADER_LEN,
+                         silc_cipher_get_iv(cipher));
 
     /* Get packet lenght and full packet length with padding */
     SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
 
     /* Sanity checks */
     if (packetlen < SILC_PACKET_MIN_LEN) {
-      SILC_LOG_DEBUG(("Received invalid packet, dropped"));
+      SILC_LOG_ERROR(("Received invalid packet, dropped"));
       silc_buffer_clear(sock->inbuf);
       return FALSE;
     }
@@ -389,12 +391,16 @@ bool silc_packet_receive_process(SilcSocketConnection sock,
       if (silc_packet_decrypt(cipher, hmac, parse_ctx->packet->sequence, 
                              parse_ctx->packet->buffer, 
                              parse_ctx->normal) == -1) {
-       SILC_LOG_WARNING(("Packet decryption failed %s:%d [%s]", 
+       SILC_LOG_WARNING(("Packet decryption failed %s:%d [%s] [%s]", 
                          sock->hostname, sock->port,
+                         silc_get_packet_name(parse_ctx->packet->type),
                          (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
                           sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
                           sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
                           "Router")));
+       silc_packet_context_free(parse_ctx->packet);
+       silc_free(parse_ctx);
+       return FALSE;
       }
 
     /* Pull the packet from inbuf thus we'll get the next one
@@ -463,7 +469,7 @@ static int silc_packet_decrypt_rest(SilcCipher cipher, SilcHmac hmac,
       if ((buffer->len - silc_hmac_len(hmac)) > SILC_PACKET_MIN_LEN) {
        silc_buffer_push_tail(buffer, silc_hmac_len(hmac));
       } else {
-       SILC_LOG_DEBUG(("Bad MAC length in packet, packet dropped"));
+       SILC_LOG_ERROR(("Bad MAC length in packet, packet dropped"));
        return FALSE;
       }
     }
@@ -473,7 +479,7 @@ static int silc_packet_decrypt_rest(SilcCipher cipher, SilcHmac hmac,
     /* Decrypt rest of the packet */
     silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN);
     silc_cipher_decrypt(cipher, buffer->data, buffer->data, buffer->len, 
-                       cipher->iv);
+                       silc_cipher_get_iv(cipher));
     silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN);
 
     SILC_LOG_HEXDUMP(("Fully decrypted packet, len %d", buffer->len),
@@ -502,7 +508,7 @@ static int silc_packet_decrypt_rest_special(SilcCipher cipher,
       if ((buffer->len - silc_hmac_len(hmac)) > SILC_PACKET_MIN_LEN) {
        silc_buffer_push_tail(buffer, silc_hmac_len(hmac));
       } else {
-       SILC_LOG_DEBUG(("Bad MAC length in packet, packet dropped"));
+       SILC_LOG_ERROR(("Bad MAC length in packet, packet dropped"));
        return FALSE;
       }
     }
@@ -517,11 +523,12 @@ static int silc_packet_decrypt_rest_special(SilcCipher cipher,
 
     silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN);
     if (len > buffer->len) {
-      SILC_LOG_DEBUG(("Garbage in header of packet, bad packet length, "
+      SILC_LOG_ERROR(("Garbage in header of packet, bad packet length, "
                      "packet dropped"));
       return FALSE;
     }
-    silc_cipher_decrypt(cipher, buffer->data, buffer->data, len, cipher->iv);
+    silc_cipher_decrypt(cipher, buffer->data, buffer->data, len,
+                       silc_cipher_get_iv(cipher));
     silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN);
     SILC_LOG_HEXDUMP(("packet, len %d", buffer->len), 
                     buffer->data, buffer->len);
@@ -585,6 +592,7 @@ SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher)
   SilcBuffer buffer = ctx->buffer;
   SilcUInt8 tmp;
   int len, ret;
+  SilcUInt8 src_id_len, src_id_type, dst_id_len, dst_id_type, padlen;
 
   SILC_LOG_DEBUG(("Parsing incoming packet"));
 
@@ -599,34 +607,46 @@ SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher)
                             SILC_STR_UI_SHORT(&ctx->truelen),
                             SILC_STR_UI_CHAR(&ctx->flags),
                             SILC_STR_UI_CHAR(&ctx->type),
-                            SILC_STR_UI_CHAR(&ctx->padlen),
+                            SILC_STR_UI_CHAR(&padlen),
                             SILC_STR_UI_CHAR(&tmp),
-                            SILC_STR_UI_CHAR(&ctx->src_id_len),
-                            SILC_STR_UI_CHAR(&ctx->dst_id_len),
-                            SILC_STR_UI_CHAR(&ctx->src_id_type),
+                            SILC_STR_UI_CHAR(&src_id_len),
+                            SILC_STR_UI_CHAR(&dst_id_len),
+                            SILC_STR_UI_CHAR(&src_id_type),
                             SILC_STR_END);
   if (len == -1 || tmp != 0)
     return SILC_PACKET_NONE;
 
-  if (ctx->src_id_len > SILC_PACKET_MAX_ID_LEN ||
-      ctx->dst_id_len > SILC_PACKET_MAX_ID_LEN) {
+  if (src_id_len > SILC_PACKET_MAX_ID_LEN ||
+      dst_id_len > SILC_PACKET_MAX_ID_LEN) {
     SILC_LOG_ERROR(("Bad ID lengths in packet (%d and %d)",
-                   ctx->src_id_len, ctx->dst_id_len));
+                   src_id_len, dst_id_len));
     return SILC_PACKET_NONE;
   }
 
   silc_buffer_pull(buffer, len);
   ret = silc_buffer_unformat(buffer, 
                             SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id,
-                                                       ctx->src_id_len),
-                            SILC_STR_UI_CHAR(&ctx->dst_id_type),
+                                                       src_id_len),
+                            SILC_STR_UI_CHAR(&dst_id_type),
                             SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id,
-                                                       ctx->dst_id_len),
-                            SILC_STR_UI_XNSTRING(NULL, ctx->padlen),
+                                                       dst_id_len),
+                            SILC_STR_UI_XNSTRING(NULL, padlen),
                             SILC_STR_END);
   if (ret == -1)
     return SILC_PACKET_NONE;
 
+  if (src_id_type > SILC_ID_CHANNEL || dst_id_type > SILC_ID_CHANNEL) {
+    SILC_LOG_ERROR(("Bad ID types in packet (%d and %d)",
+                  src_id_type, dst_id_type));
+    return SILC_PACKET_NONE;
+  }
+
+  ctx->src_id_len = src_id_len;
+  ctx->dst_id_len = dst_id_len;
+  ctx->src_id_type = src_id_type;
+  ctx->dst_id_type = dst_id_type;
+  ctx->padlen = padlen;
+
   silc_buffer_push(buffer, len);
 
   SILC_LOG_HEXDUMP(("parsed packet, len %d", ctx->buffer->len), 
@@ -653,6 +673,7 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx,
   SilcBuffer buffer = ctx->buffer;
   SilcUInt8 tmp;
   int len, ret;
+  SilcUInt8 src_id_len, src_id_type, dst_id_len, dst_id_type, padlen;
 
   SILC_LOG_DEBUG(("Parsing incoming packet"));
 
@@ -667,38 +688,50 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx,
                             SILC_STR_UI_SHORT(&ctx->truelen),
                             SILC_STR_UI_CHAR(&ctx->flags),
                             SILC_STR_UI_CHAR(&ctx->type),
-                            SILC_STR_UI_CHAR(&ctx->padlen),
+                            SILC_STR_UI_CHAR(&padlen),
                             SILC_STR_UI_CHAR(&tmp),
-                            SILC_STR_UI_CHAR(&ctx->src_id_len),
-                            SILC_STR_UI_CHAR(&ctx->dst_id_len),
-                            SILC_STR_UI_CHAR(&ctx->src_id_type),
+                            SILC_STR_UI_CHAR(&src_id_len),
+                            SILC_STR_UI_CHAR(&dst_id_len),
+                            SILC_STR_UI_CHAR(&src_id_type),
                             SILC_STR_END);
   if (len == -1 || tmp != 0) {
     SILC_LOG_ERROR(("Malformed packet header, packet dropped"));
     return SILC_PACKET_NONE;
   }
 
-  if (ctx->src_id_len > SILC_PACKET_MAX_ID_LEN ||
-      ctx->dst_id_len > SILC_PACKET_MAX_ID_LEN) {
+  if (src_id_len > SILC_PACKET_MAX_ID_LEN ||
+      dst_id_len > SILC_PACKET_MAX_ID_LEN) {
     SILC_LOG_ERROR(("Bad ID lengths in packet (%d and %d)",
-                   ctx->src_id_len, ctx->dst_id_len));
+                   src_id_len, dst_id_len));
     return SILC_PACKET_NONE;
   }
 
   silc_buffer_pull(buffer, len);
   ret = silc_buffer_unformat(buffer, 
                             SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id,
-                                                       ctx->src_id_len),
-                            SILC_STR_UI_CHAR(&ctx->dst_id_type),
+                                                       src_id_len),
+                            SILC_STR_UI_CHAR(&dst_id_type),
                             SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id,
-                                                       ctx->dst_id_len),
-                            SILC_STR_UI_XNSTRING(NULL, ctx->padlen),
+                                                       dst_id_len),
+                            SILC_STR_UI_XNSTRING(NULL, padlen),
                             SILC_STR_END);
   if (ret == -1) {
     SILC_LOG_ERROR(("Malformed packet header, packet dropped"));
     return SILC_PACKET_NONE;
   }
 
+  if (src_id_type > SILC_ID_CHANNEL || dst_id_type > SILC_ID_CHANNEL) {
+    SILC_LOG_ERROR(("Bad ID types in packet (%d and %d)",
+                  src_id_type, dst_id_type));
+    return SILC_PACKET_NONE;
+  }
+
+  ctx->src_id_len = src_id_len;
+  ctx->dst_id_len = dst_id_len;
+  ctx->src_id_type = src_id_type;
+  ctx->dst_id_type = dst_id_type;
+  ctx->padlen = padlen;
+
   silc_buffer_push(buffer, len);
 
   SILC_LOG_HEXDUMP(("parsed packet, len %d", ctx->buffer->len),