updates.
[silc.git] / lib / silcclient / client_channel.c
index bec415ab8ff218d60e7916e138ea1e631f8a4a34..fb23a10c33f8157e4523196b2df5488702575360 100644 (file)
@@ -36,8 +36,9 @@ void silc_client_send_channel_message(SilcClient client,
                                      SilcClientConnection conn,
                                      SilcChannelEntry channel,
                                      SilcChannelPrivateKey key,
+                                     SilcMessageFlags flags,
                                      unsigned char *data, 
-                                     unsigned int data_len, 
+                                     uint32 data_len, 
                                      int force_send)
 {
   int i;
@@ -47,17 +48,10 @@ void silc_client_send_channel_message(SilcClient client,
   SilcCipher cipher;
   SilcHmac hmac;
   unsigned char *id_string;
-  unsigned int iv_len;
+  uint32 iv_len;
 
   SILC_LOG_DEBUG(("Sending packet to channel"));
 
-  if (!channel || !channel->hmac || 
-      (!channel->channel_key && !key && !channel->private_keys)) {
-    client->ops->say(client, conn, 
-                    "Cannot talk to channel: key does not exist");
-    return;
-  }
-
   /* Take the key to be used */
   if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
     if (key) {
@@ -89,6 +83,9 @@ void silc_client_send_channel_message(SilcClient client,
     hmac = channel->hmac;
   }
 
+  if (!cipher || !hmac)
+    return;
+
   /* Generate IV */
   iv_len = silc_cipher_get_block_len(cipher);
   if (channel->iv[0] == '\0')
@@ -98,12 +95,12 @@ void silc_client_send_channel_message(SilcClient client,
     silc_hash_make(client->md5hash, channel->iv, iv_len, channel->iv);
 
   /* Encode the channel payload. This also encrypts the message payload. */
-  payload = silc_channel_message_payload_encode(0, data_len, data, iv_len, 
+  payload = silc_channel_message_payload_encode(flags, data_len, data, iv_len, 
                                                channel->iv, cipher, hmac);
 
   /* Get data used in packet header encryption, keys and stuff. */
   cipher = conn->send_key;
-  hmac = conn->hmac;
+  hmac = conn->hmac_send;
   id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
 
   /* Set the packet context pointers. The destination ID is always
@@ -112,10 +109,10 @@ void silc_client_send_channel_message(SilcClient client,
   packetdata.flags = 0;
   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
   packetdata.src_id = conn->local_id_data;
-  packetdata.src_id_len = SILC_ID_CLIENT_LEN;
+  packetdata.src_id_len = silc_id_get_len(conn->local_id, SILC_ID_CLIENT);
   packetdata.src_id_type = SILC_ID_CLIENT;
   packetdata.dst_id = id_string;
-  packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
+  packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
   packetdata.dst_id_type = SILC_ID_CHANNEL;
   packetdata.truelen = payload->len + SILC_PACKET_HEADER_LEN + 
     packetdata.src_id_len + packetdata.dst_id_len;
@@ -149,7 +146,7 @@ void silc_client_send_channel_message(SilcClient client,
                   sock->outbuf->data, sock->outbuf->len);
 
   /* Now actually send the packet */
-  silc_client_packet_send_real(client, sock, force_send);
+  silc_client_packet_send_real(client, sock, force_send, FALSE);
   silc_buffer_free(payload);
   silc_free(id_string);
 }
@@ -188,8 +185,7 @@ void silc_client_channel_message(SilcClient client,
     goto out;
 
   /* Find the channel entry from channels on this connection */
-  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
-                                  SILC_ID_CHANNEL, &id_cache))
+  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id, &id_cache))
     goto out;
 
   channel = (SilcChannelEntry)id_cache->context;
@@ -225,7 +221,7 @@ void silc_client_channel_message(SilcClient client,
   /* Find client entry */
   silc_list_start(channel->clients);
   while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
-    if (!SILC_ID_CLIENT_COMPARE(chu->client->id, client_id)) {
+    if (SILC_ID_CLIENT_COMPARE(chu->client->id, client_id)) {
       found = TRUE;
       break;
     }
@@ -233,7 +229,9 @@ void silc_client_channel_message(SilcClient client,
 
   /* Pass the message to application */
   client->ops->channel_message(client, conn, found ? chu->client : NULL,
-                              channel, message);
+                              channel, 
+                              silc_channel_message_get_flags(payload),
+                              message);
 
  out:
   if (id)
@@ -253,7 +251,7 @@ void silc_client_save_channel_key(SilcClientConnection conn,
                                  SilcChannelEntry channel)
 {
   unsigned char *id_string, *key, *cipher, hash[32];
-  unsigned int tmp_len;
+  uint32 tmp_len;
   SilcChannelID *id;
   SilcIDCacheEntry id_cache = NULL;
   SilcChannelKeyPayload payload;
@@ -276,8 +274,8 @@ void silc_client_save_channel_key(SilcClientConnection conn,
 
   /* Find channel. */
   if (!channel) {
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
-                                    SILC_ID_CHANNEL, &id_cache))
+    if (!silc_idcache_find_by_id_one(conn->channel_cache, 
+                                    (void *)id, &id_cache))
       goto out;
     
     /* Get channel entry */
@@ -307,9 +305,6 @@ void silc_client_save_channel_key(SilcClientConnection conn,
   silc_hmac_set_key(channel->hmac, hash, silc_hash_len(channel->hmac->hash));
   memset(hash, 0, sizeof(hash));
 
-  /* Client is now joined to the channel */
-  channel->on_channel = TRUE;
-
  out:
   silc_free(id);
   silc_channel_key_payload_free(payload);
@@ -339,7 +334,7 @@ void silc_client_receive_channel_key(SilcClient client,
    several private keys per one channel. In this case only some of the
    clients on the channel may know the one key and only some the other key.
 
-   if `cipher' and/or `hmac' is NULL then default values will be used 
+   If `cipher' and/or `hmac' is NULL then default values will be used 
    (aes-256-cbc for cipher and hmac-sha1-96 for hmac).
 
    The private key for channel is optional. If it is not set then the
@@ -366,10 +361,11 @@ int silc_client_add_channel_private_key(SilcClient client,
                                        char *cipher,
                                        char *hmac,
                                        unsigned char *key,
-                                       unsigned int key_len)
+                                       uint32 key_len)
 {
   SilcChannelPrivateKey entry;
   unsigned char hash[32];
+  SilcSKEKeyMaterial *keymat;
 
   if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
     return FALSE;
@@ -385,6 +381,13 @@ int silc_client_add_channel_private_key(SilcClient client,
   if (!silc_hmac_is_supported(hmac))
     return FALSE;
 
+  /* Produce the key material */
+  keymat = silc_calloc(1, sizeof(*keymat));
+  if (silc_ske_process_key_material_data(key, key_len, 16, 256, 16, 
+                                        client->md5hash, keymat) 
+      != SILC_SKE_STATUS_OK)
+    return FALSE;
+
   /* Remove the current key, if it exists. */
   if (channel->channel_key) {
     silc_cipher_free(channel->channel_key);
@@ -394,25 +397,27 @@ int silc_client_add_channel_private_key(SilcClient client,
     channel->key = NULL;
     channel->key_len = 0;
   }
-  if (channel->hmac)
+  if (channel->hmac) {
     silc_hmac_free(channel->hmac);
+    channel->hmac = NULL;
+  }
 
   if (!channel->private_keys)
     channel->private_keys = silc_dlist_init();
 
   /* Save the key */
   entry = silc_calloc(1, sizeof(*entry));
-  entry->key = silc_calloc(key_len, sizeof(*entry->key));
-  memcpy(entry->key, key, key_len);
-  entry->key_len = key_len;
+  entry->key = silc_calloc(keymat->enc_key_len / 8, sizeof(*entry->key));
+  memcpy(entry->key, keymat->send_enc_key, keymat->enc_key_len / 8);
+  entry->key_len = keymat->enc_key_len / 8;
 
   /* Allocate the cipher and set the key*/
   silc_cipher_alloc(cipher, &entry->cipher);
-  silc_cipher_set_key(entry->cipher, key, key_len * 8);
+  silc_cipher_set_key(entry->cipher, entry->key, keymat->enc_key_len);
 
   /* Generate HMAC key from the channel key data and set it */
   silc_hmac_alloc(hmac, NULL, &entry->hmac);
-  silc_hash_make(entry->hmac->hash, key, key_len, hash);
+  silc_hash_make(entry->hmac->hash, entry->key, entry->key_len, hash);
   silc_hmac_set_key(entry->hmac, hash, silc_hash_len(entry->hmac->hash));
   memset(hash, 0, sizeof(hash));
 
@@ -422,6 +427,9 @@ int silc_client_add_channel_private_key(SilcClient client,
   if (!channel->curr_key)
     channel->curr_key = entry;
 
+  /* Free the key material */
+  silc_ske_free_key_material(keymat);
+
   return TRUE;
 }
 
@@ -507,10 +515,10 @@ SilcChannelPrivateKey *
 silc_client_list_channel_private_keys(SilcClient client,
                                      SilcClientConnection conn,
                                      SilcChannelEntry channel,
-                                     unsigned int *key_count)
+                                     uint32 *key_count)
 {
   SilcChannelPrivateKey *keys = NULL, entry;
-  unsigned int count = 0;
+  uint32 count = 0;
 
   if (!channel->private_keys)
     return NULL;
@@ -531,7 +539,7 @@ silc_client_list_channel_private_keys(SilcClient client,
 /* Frees the SilcChannelPrivateKey array. */
 
 void silc_client_free_channel_private_keys(SilcChannelPrivateKey *keys,
-                                          unsigned int key_count)
+                                          uint32 key_count)
 {
   silc_free(keys);
 }