updates.
[silc.git] / lib / silccore / silcchannel.c
index 4cfe30022fb45114732eb0c5c943ca6ce94cfa92..433236eb4be27dacb695362e8938fb0da334039c 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.2  2000/07/05 06:06:35  priikone
- *     Global cosmetic change.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* Channel Payload and Channel Key Payload implementations. */
+/* $Id$ */
 
 #include "silcincludes.h"
 #include "silcchannel.h"
 
-/* Channel Payload structure. Contents of this structure is parsed
+/******************************************************************************
+
+                          Channel Message Payload
+
+******************************************************************************/
+
+/* Channel Message Payload structure. Contents of this structure is parsed
    from SILC packets. */
 struct SilcChannelPayloadStruct {
-  unsigned short nick_len;
-  unsigned char *nick;
   unsigned short data_len;
   unsigned char *data;
-  unsigned short iv_len;
+  unsigned char *mac;
   unsigned char *iv;
 };
 
-/* Channel Key Payload structrue. Channel keys are parsed from SILC
-   packets into this structure. */
-struct SilcChannelKeyPayloadStruct {
-  unsigned short id_len;
-  unsigned char *id;
-  unsigned short cipher_len;
-  unsigned char *cipher;
-  unsigned short key_len;
-  unsigned char *key;
-};
+/* Decrypts the channel message payload. */
+
+int silc_channel_payload_decrypt(unsigned char *data,
+                                size_t data_len,
+                                SilcCipher cipher,
+                                SilcHmac hmac)
+{
+  unsigned int iv_len, mac_len;
+  unsigned char *end, *mac, mac2[32];
+
+  /* Decrypt the channel message. First push the IV out of the packet.
+     The IV is used in the decryption process. Then decrypt the message.
+     After decyprtion, take the MAC from the decrypted packet, compute MAC
+     and compare the MACs.  If they match, the decryption was successfull
+     and we have the channel message ready to be displayed. */
+  end = data + data_len;
+
+  /* Push the IV out of the packet */
+  iv_len = silc_cipher_get_block_len(cipher);
+
+  /* Decrypt the channel message */
+  silc_cipher_decrypt(cipher, data, data, data_len - iv_len, (end - iv_len));
+
+  /* Take the MAC */
+  if (hmac) {
+    mac_len = silc_hmac_len(hmac);
+    mac = (end - iv_len - mac_len);
+
+    /* Check the MAC of the message */
+    SILC_LOG_DEBUG(("Checking channel message MACs"));
+    silc_hmac_make(hmac, data, (data_len - iv_len - mac_len), mac2, &mac_len);
+    if (memcmp(mac, mac2, mac_len)) {
+      SILC_LOG_DEBUG(("Channel message MACs does not match"));
+      return FALSE;
+    }
+    SILC_LOG_DEBUG(("MAC is Ok"));
+  }
+
+  return TRUE;
+}
 
-/* Parses channel payload returning new channel payload structure */
+/* Parses channel payload returning new channel payload structure. This
+   also decrypts it and checks the MAC. */
 
-SilcChannelPayload silc_channel_parse_payload(SilcBuffer buffer)
+SilcChannelPayload silc_channel_payload_parse(SilcBuffer buffer,
+                                             SilcCipher cipher,
+                                             SilcHmac hmac)
 {
   SilcChannelPayload new;
+  int ret;
+  unsigned int iv_len, mac_len;
 
   SILC_LOG_DEBUG(("Parsing channel payload"));
 
+  /* Decrypt the payload */
+  ret = silc_channel_payload_decrypt(buffer->data, buffer->len,
+                                    cipher, hmac);
+  if (ret == FALSE)
+    return NULL;
+
+  iv_len = silc_cipher_get_block_len(cipher);
+  mac_len = silc_hmac_len(hmac);
+
   new = silc_calloc(1, sizeof(*new));
 
-  /* Parse the Channel Payload. Ignore padding and IV, we don't need
-     them. */
-  silc_buffer_unformat(buffer,
-                      SILC_STR_UI16_NSTRING_ALLOC(&new->nick, &new->nick_len),
-                      SILC_STR_UI16_NSTRING_ALLOC(&new->data, &new->data_len),
-                      SILC_STR_UI16_NSTRING_ALLOC(NULL, NULL),
-                      SILC_STR_END);
+  /* Parse the Channel Payload. Ignore the padding. */
+  ret = silc_buffer_unformat(buffer,
+                            SILC_STR_UI16_NSTRING_ALLOC(&new->data, 
+                                                        &new->data_len),
+                            SILC_STR_UI16_NSTRING(NULL, NULL),
+                            SILC_STR_UI_XNSTRING(&new->mac, mac_len),
+                            SILC_STR_UI_XNSTRING(&new->iv, iv_len),
+                            SILC_STR_END);
+  if (ret == -1)
+    goto err;
 
-  if (new->data_len < 1) {
+  if (new->data_len < 1 || new->data_len > buffer->len) {
     SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped"));
     goto err;
   }
@@ -80,13 +122,7 @@ SilcChannelPayload silc_channel_parse_payload(SilcBuffer buffer)
   return new;
 
  err:
-  if (new->nick)
-    silc_free(new->nick);
-  if (new->data)
-    silc_free(new->data);
-  if (new->iv)
-    silc_free(new->iv);
-  silc_free(new);
+  silc_channel_payload_free(new);
   return NULL;
 }
 
@@ -95,75 +131,79 @@ SilcChannelPayload silc_channel_parse_payload(SilcBuffer buffer)
    encrypted separately from other parts of the packet padding must
    be applied to the payload. */
 
-SilcBuffer silc_channel_encode_payload(unsigned short nick_len,
-                                      unsigned char *nick,
-                                      unsigned short data_len,
+SilcBuffer silc_channel_payload_encode(unsigned short data_len,
                                       unsigned char *data,
                                       unsigned short iv_len,
                                       unsigned char *iv,
+                                      SilcCipher cipher,
+                                      SilcHmac hmac,
                                       SilcRng rng)
 {
   int i;
   SilcBuffer buffer;
-  unsigned int len, pad_len;
+  unsigned int len, pad_len, mac_len;
   unsigned char pad[SILC_PACKET_MAX_PADLEN];
+  unsigned char mac[32];
 
   SILC_LOG_DEBUG(("Encoding channel payload"));
 
   /* Calculate length of padding. IV is not included into the calculation
      since it is not encrypted. */
-  len = 2 + nick_len + 2 + data_len + 2;
+  mac_len = silc_hmac_len(hmac);
+  len = 4 + data_len + mac_len;
   pad_len = SILC_PACKET_PADLEN((len + 2));
 
   /* Allocate channel payload buffer */
-  len += pad_len;
-  buffer = silc_buffer_alloc(len + iv_len);
+  len += pad_len + iv_len;
+  buffer = silc_buffer_alloc(len);
 
   /* Generate padding */
-  for (i = 0; i < pad_len; i++)
-    pad[i] = silc_rng_get_byte(rng);
-
-  silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+  for (i = 0; i < pad_len; i++) pad[i] = silc_rng_get_byte(rng);
 
   /* Encode the Channel Payload */
+  silc_buffer_pull_tail(buffer, 4 + data_len + pad_len);
   silc_buffer_format(buffer, 
-                    SILC_STR_UI_SHORT(nick_len),
-                    SILC_STR_UI_XNSTRING(nick, nick_len),
                     SILC_STR_UI_SHORT(data_len),
                     SILC_STR_UI_XNSTRING(data, data_len),
                     SILC_STR_UI_SHORT(pad_len),
                     SILC_STR_UI_XNSTRING(pad, pad_len),
+                    SILC_STR_END);
+
+  /* Compute the MAC of the channel message data */
+  silc_hmac_make(hmac, buffer->data, buffer->len, mac, &mac_len);
+
+  /* Put rest of the data to the payload */
+  silc_buffer_pull_tail(buffer, mac_len + iv_len);
+  silc_buffer_pull(buffer, 4 + data_len + pad_len);
+  silc_buffer_format(buffer, 
+                    SILC_STR_UI_XNSTRING(mac, mac_len),
                     SILC_STR_UI_XNSTRING(iv, iv_len),
                     SILC_STR_END);
+  silc_buffer_push(buffer, 4 + data_len + pad_len);
+
+  /* Encrypt payload of the packet. This is encrypted with the channel key. */
+  silc_cipher_encrypt(cipher, buffer->data, buffer->data, 
+                     buffer->len - iv_len, iv);
+
+  memset(pad, 0, sizeof(pad));
+  memset(mac, 0, sizeof(mac));
 
-  memset(pad, 0, pad_len);
   return buffer;
 }
 
 /* Free's Channel Payload */
 
-void silc_channel_free_payload(SilcChannelPayload payload)
+void silc_channel_payload_free(SilcChannelPayload payload)
 {
   if (payload) {
-    if (payload->data)
+    if (payload->data) {
+      memset(payload->data, 0, payload->data_len);
       silc_free(payload->data);
-    if (payload->iv)
-      silc_free(payload->iv);
+    }
     silc_free(payload);
   }
 }
 
-/* Return nickname */
-
-unsigned char *silc_channel_get_nickname(SilcChannelPayload payload,
-                                        unsigned int *nick_len)
-{
-  if (nick_len)
-    *nick_len = payload->nick_len;
-
-  return payload->nick;
-}
-
 /* Return data */
 
 unsigned char *silc_channel_get_data(SilcChannelPayload payload,
@@ -175,34 +215,58 @@ unsigned char *silc_channel_get_data(SilcChannelPayload payload,
   return payload->data;
 }
 
-/* Return initial vector */
+/* Return MAC. The caller knows the length of the MAC */
 
-unsigned char *silc_channel_get_iv(SilcChannelPayload payload,
-                                  unsigned int *iv_len)
+unsigned char *silc_channel_get_mac(SilcChannelPayload payload)
 {
-  if (iv_len)
-    *iv_len = payload->iv_len;
+  return payload->mac;
+}
+
+/* Return IV. The caller knows the length of the IV */
 
+unsigned char *silc_channel_get_iv(SilcChannelPayload payload)
+{
   return payload->iv;
 }
 
+/******************************************************************************
+
+                             Channel Key Payload
+
+******************************************************************************/
+
+/* Channel Key Payload structrue. Channel keys are parsed from SILC
+   packets into this structure. */
+struct SilcChannelKeyPayloadStruct {
+  unsigned short id_len;
+  unsigned char *id;
+  unsigned short cipher_len;
+  unsigned char *cipher;
+  unsigned short key_len;
+  unsigned char *key;
+};
+
 /* Parses channel key payload returning new channel key payload structure */
 
-SilcChannelKeyPayload silc_channel_key_parse_payload(SilcBuffer buffer)
+SilcChannelKeyPayload silc_channel_key_payload_parse(SilcBuffer buffer)
 {
   SilcChannelKeyPayload new;
+  int ret;
 
   SILC_LOG_DEBUG(("Parsing channel key payload"));
 
   new = silc_calloc(1, sizeof(*new));
 
   /* Parse the Channel Key Payload */
-  silc_buffer_unformat(buffer,
-                      SILC_STR_UI16_NSTRING_ALLOC(&new->id, &new->id_len),
-                      SILC_STR_UI16_NSTRING_ALLOC(&new->cipher, 
-                                                  &new->cipher_len),
-                      SILC_STR_UI16_NSTRING_ALLOC(&new->key, &new->key_len),
-                      SILC_STR_END);
+  ret =
+    silc_buffer_unformat(buffer,
+                        SILC_STR_UI16_NSTRING_ALLOC(&new->id, &new->id_len),
+                        SILC_STR_UI16_NSTRING_ALLOC(&new->cipher, 
+                                                    &new->cipher_len),
+                        SILC_STR_UI16_NSTRING_ALLOC(&new->key, &new->key_len),
+                        SILC_STR_END);
+  if (ret == -1)
+    goto err;
 
   if (new->id_len < 1 || new->key_len < 1 || new->cipher_len < 1) {
     SILC_LOG_ERROR(("Incorrect channel key payload in packet"));
@@ -225,7 +289,7 @@ SilcChannelKeyPayload silc_channel_key_parse_payload(SilcBuffer buffer)
 /* Encodes channel key payload into a buffer and returns it. This is used 
    to add channel key payload into a packet. */
 
-SilcBuffer silc_channel_key_encode_payload(unsigned short id_len,
+SilcBuffer silc_channel_key_payload_encode(unsigned short id_len,
                                           unsigned char *id,
                                           unsigned short cipher_len,
                                           unsigned char *cipher,
@@ -237,10 +301,6 @@ SilcBuffer silc_channel_key_encode_payload(unsigned short id_len,
 
   SILC_LOG_DEBUG(("Encoding channel key payload"));
 
-  /* Sanity checks */
-  if (!id_len || !key_len || !id || !key || !cipher_len || !cipher)
-    return NULL;
-
   /* Allocate channel payload buffer. Length is 2 + id + 2 + key + 
      2 + cipher */
   len = 2 + id_len + 2 + key_len + 2 + cipher_len;
@@ -263,7 +323,7 @@ SilcBuffer silc_channel_key_encode_payload(unsigned short id_len,
 
 /* Free's Channel Key Payload */
 
-void silc_channel_key_free_payload(SilcChannelKeyPayload payload)
+void silc_channel_key_payload_free(SilcChannelKeyPayload payload)
 {
   if (payload) {
     if (payload->id)