Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silcclient / client_channel.c
index 28cc6147099f53b100bb654e1ef5af0a11bfb30a..7dd4f0a6ad59a23572262fb48970de4decda3616 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2004, 2006 Pekka Riikonen
+  Copyright (C) 1997 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 
 /************************** Channel Message Send ****************************/
 
+typedef struct {
+  SilcClient client;
+  SilcClientConnection conn;
+  SilcChannelEntry channel;
+} *SilcClientChannelMessageContext;
+
+/* Message payload encoding callback */
+
+static void silc_client_send_channel_message_final(SilcBuffer message,
+                                                  void *context)
+{
+  SilcClientChannelMessageContext c = context;
+
+  /* Send the channel message */
+  if (message)
+    silc_packet_send_ext(c->conn->stream, SILC_PACKET_CHANNEL_MESSAGE, 0,
+                        0, NULL, SILC_ID_CHANNEL, &c->channel->id,
+                        silc_buffer_datalen(message), NULL, NULL);
+
+  silc_client_unref_channel(c->client, c->conn, c->channel);
+  silc_free(c);
+}
+
 /* Sends channel message to `channel'. */
 
 SilcBool silc_client_send_channel_message(SilcClient client,
@@ -35,21 +58,26 @@ SilcBool silc_client_send_channel_message(SilcClient client,
                                          unsigned char *data,
                                          SilcUInt32 data_len)
 {
+  SilcClientChannelMessageContext c;
   SilcChannelUser chu;
-  SilcBuffer buffer;
   SilcCipher cipher;
   SilcHmac hmac;
-  SilcBool ret;
+  SilcID sid, rid;
 
   SILC_LOG_DEBUG(("Sending channel message"));
 
-  if (!client || !conn || !channel)
+  if (silc_unlikely(!client || !conn || !channel))
+    return FALSE;
+  if (silc_unlikely(flags & SILC_MESSAGE_FLAG_SIGNED && !hash)) {
+    SILC_LOG_ERROR(("Cannot send signed message without hash, missing "
+                   "arguments"));
     return FALSE;
-  if (flags & SILC_MESSAGE_FLAG_SIGNED && !hash)
+  }
+  if (silc_unlikely(conn->internal->disconnected))
     return FALSE;
 
   chu = silc_client_on_channel(channel, conn->local_entry);
-  if (!chu) {
+  if (silc_unlikely(!chu)) {
     client->internal->ops->say(conn->client, conn,
                               SILC_CLIENT_MESSAGE_AUDIT,
                               "Cannot talk to channel: not joined");
@@ -57,25 +85,26 @@ SilcBool silc_client_send_channel_message(SilcClient client,
   }
 
   /* Check if it is allowed to send messages to this channel by us. */
-  if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS && !chu->mode)
+  if (silc_unlikely(channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS &&
+                   !chu->mode))
     return FALSE;
-  if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
-      chu->mode & SILC_CHANNEL_UMODE_CHANOP &&
-      !(chu->mode & SILC_CHANNEL_UMODE_CHANFO))
+  if (silc_unlikely(channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
+                   chu->mode & SILC_CHANNEL_UMODE_CHANOP &&
+                   !(chu->mode & SILC_CHANNEL_UMODE_CHANFO)))
     return FALSE;
-  if (chu->mode & SILC_CHANNEL_UMODE_QUIET)
+  if (silc_unlikely(chu->mode & SILC_CHANNEL_UMODE_QUIET))
     return FALSE;
 
   /* Take the key to be used */
   if (channel->internal.private_keys) {
     if (key) {
       /* Use key application specified */
-      cipher = key->cipher;
+      cipher = key->send_key;
       hmac = key->hmac;
     } else if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
               channel->internal.curr_key) {
       /* Use current private key */
-      cipher = channel->internal.curr_key->cipher;
+      cipher = channel->internal.curr_key->send_key;
       hmac = channel->internal.curr_key->hmac;
     } else if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
               !channel->internal.curr_key &&
@@ -84,43 +113,47 @@ SilcBool silc_client_send_channel_message(SilcClient client,
         and private keys are set. */
       silc_dlist_start(channel->internal.private_keys);
       key = silc_dlist_get(channel->internal.private_keys);
-      cipher = key->cipher;
+      cipher = key->send_key;
       hmac = key->hmac;
 
       /* Use this key as current private key */
       channel->internal.curr_key = key;
     } else {
       /* Use normal channel key generated by the server */
-      cipher = channel->internal.channel_key;
+      cipher = channel->internal.send_key;
       hmac = channel->internal.hmac;
     }
   } else {
     /* Use normal channel key generated by the server */
-    cipher = channel->internal.channel_key;
+    cipher = channel->internal.send_key;
     hmac = channel->internal.hmac;
   }
 
-  if (!cipher || !hmac) {
+  if (silc_unlikely(!cipher || !hmac)) {
     SILC_LOG_ERROR(("No cipher and HMAC for channel"));
     return FALSE;
   }
 
-  /* Encode the message payload. This also encrypts the message payload. */
-  buffer = silc_message_payload_encode(flags, data, data_len, TRUE, FALSE,
-                                      cipher, hmac, client->rng, NULL,
-                                      conn->private_key, hash, NULL);
-  if (!buffer) {
-    SILC_LOG_ERROR(("Error encoding channel message"));
+  c = silc_calloc(1, sizeof(*c));
+  if (!c)
     return FALSE;
-  }
 
-  /* Send the channel message */
-  ret = silc_packet_send_ext(conn->stream, SILC_PACKET_CHANNEL_MESSAGE, 0,
-                            0, NULL, SILC_ID_CHANNEL, &channel->id,
-                            silc_buffer_datalen(buffer), NULL, NULL);
+  c->client = client;
+  c->conn = conn;
+  c->channel = silc_client_ref_channel(client, conn, channel);
+
+  sid.type = SILC_ID_CLIENT;
+  sid.u.client_id = chu->client->id;
+  rid.type = SILC_ID_CHANNEL;
+  rid.u.channel_id = chu->channel->id;
 
-  silc_buffer_free(buffer);
-  return ret;
+  /* Encode the message payload. This also encrypts the message payload. */
+  silc_message_payload_encode(flags, data, data_len, TRUE, FALSE,
+                             cipher, hmac, client->rng, NULL,
+                             conn->private_key, hash, &sid, &rid, NULL,
+                             silc_client_send_channel_message_final, c);
+
+  return TRUE;
 }
 
 /************************* Channel Message Receive **************************/
@@ -160,14 +193,18 @@ SILC_FSM_STATE(silc_client_channel_message)
 
   SILC_LOG_DEBUG(("Received channel message"));
 
-  if (packet->dst_id_type != SILC_ID_CHANNEL) {
+  SILC_LOG_HEXDUMP(("Channel message"), silc_buffer_data(buffer),
+                  silc_buffer_len(buffer));
+
+  if (silc_unlikely(packet->dst_id_type != SILC_ID_CHANNEL)) {
     /** Invalid packet */
     silc_fsm_next(fsm, silc_client_channel_message_error);
     return SILC_FSM_CONTINUE;
   }
 
-  if (!silc_id_str2id(packet->src_id, packet->src_id_len, SILC_ID_CLIENT,
-                     &remote_id, sizeof(remote_id))) {
+  if (silc_unlikely(!silc_id_str2id(packet->src_id,
+                                   packet->src_id_len, SILC_ID_CLIENT,
+                                   &remote_id, sizeof(remote_id)))) {
     /** Invalid source ID */
     silc_fsm_next(fsm, silc_client_channel_message_error);
     return SILC_FSM_CONTINUE;
@@ -175,7 +212,7 @@ SILC_FSM_STATE(silc_client_channel_message)
 
   /* Get sender client entry */
   client_entry = silc_client_get_client_by_id(client, conn, &remote_id);
-  if (!client_entry || !client_entry->nickname[0]) {
+  if (!client_entry || !client_entry->internal.valid) {
     /** Resolve client info */
     silc_client_unref_client(client, conn, client_entry);
     SILC_FSM_CALL(silc_client_get_client_by_id_resolve(
@@ -185,8 +222,9 @@ SILC_FSM_STATE(silc_client_channel_message)
     /* NOT REACHED */
   }
 
-  if (!silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL,
-                     &channel_id, sizeof(channel_id))) {
+  if (silc_unlikely(!silc_id_str2id(packet->dst_id, packet->dst_id_len,
+                                   SILC_ID_CHANNEL, &channel_id,
+                                   sizeof(channel_id)))) {
     /** Invalid destination ID */
     silc_fsm_next(fsm, silc_client_channel_message_error);
     return SILC_FSM_CONTINUE;
@@ -194,12 +232,21 @@ SILC_FSM_STATE(silc_client_channel_message)
 
   /* Find the channel */
   channel = silc_client_get_channel_by_id(client, conn, &channel_id);
-  if (!channel) {
+  if (silc_unlikely(!channel)) {
     /** Unknown channel */
     silc_fsm_next(fsm, silc_client_channel_message_error);
     return SILC_FSM_CONTINUE;
   }
 
+  /* Check that user is on channel */
+  if (silc_unlikely(!silc_client_on_channel(channel, client_entry))) {
+    /** User not on channel */
+    SILC_LOG_WARNING(("Message from user not on channel, client or "
+                     "server bug"));
+    silc_fsm_next(fsm, silc_client_channel_message_error);
+    return SILC_FSM_CONTINUE;
+  }
+
   /* If there is no channel private key then just decrypt the message
      with the channel key. If private keys are set then just go through
      all private keys and check what decrypts correctly. */
@@ -207,14 +254,16 @@ SILC_FSM_STATE(silc_client_channel_message)
     /* Parse the channel message payload. This also decrypts the payload */
     payload = silc_message_payload_parse(silc_buffer_data(buffer),
                                         silc_buffer_len(buffer), FALSE,
-                                        FALSE, channel->internal.channel_key,
-                                        channel->internal.hmac, NULL,
-                                        FALSE, NULL);
+                                        FALSE, channel->internal.receive_key,
+                                        channel->internal.hmac,
+                                        packet->src_id, packet->src_id_len,
+                                        packet->dst_id, packet->dst_id_len,
+                                        NULL, FALSE, NULL);
 
     /* If decryption failed and we have just performed channel key rekey
        we will use the old key in decryption. If that fails too then we
        cannot do more and will drop the packet. */
-    if (!payload) {
+    if (silc_unlikely(!payload)) {
       SilcCipher cipher;
       SilcHmac hmac;
 
@@ -234,6 +283,10 @@ SILC_FSM_STATE(silc_client_channel_message)
        payload = silc_message_payload_parse(silc_buffer_data(buffer),
                                             silc_buffer_len(buffer),
                                             FALSE, FALSE, cipher, hmac,
+                                            packet->src_id,
+                                            packet->src_id_len,
+                                            packet->dst_id,
+                                            packet->dst_id_len,
                                             NULL, FALSE, NULL);
        if (payload)
          break;
@@ -248,9 +301,13 @@ SILC_FSM_STATE(silc_client_channel_message)
       payload = silc_message_payload_parse(silc_buffer_data(buffer),
                                           silc_buffer_len(buffer),
                                           FALSE, FALSE,
-                                          channel->internal.channel_key,
-                                          channel->internal.hmac, NULL,
-                                          FALSE, NULL);
+                                          channel->internal.receive_key,
+                                          channel->internal.hmac,
+                                          packet->src_id,
+                                          packet->src_id_len,
+                                          packet->dst_id,
+                                          packet->dst_id_len,
+                                          NULL, FALSE, NULL);
 
     if (!payload) {
       silc_dlist_start(channel->internal.private_keys);
@@ -258,8 +315,12 @@ SILC_FSM_STATE(silc_client_channel_message)
        /* Parse the message payload. This also decrypts the payload */
        payload = silc_message_payload_parse(silc_buffer_data(buffer),
                                             silc_buffer_len(buffer),
-                                            FALSE, FALSE, key->cipher,
-                                            key->hmac, NULL, FALSE, NULL);
+                                            FALSE, FALSE, key->receive_key,
+                                            key->hmac, packet->src_id,
+                                            packet->src_id_len,
+                                            packet->dst_id,
+                                            packet->dst_id_len,
+                                            NULL, FALSE, NULL);
        if (payload)
          break;
       }
@@ -337,6 +398,8 @@ SilcBool silc_client_save_channel_key(SilcClient client,
   SilcChannelID id;
   SilcChannelKeyPayload payload;
 
+  SILC_LOG_DEBUG(("New channel key"));
+
   payload = silc_channel_key_payload_parse(silc_buffer_data(key_payload),
                                           silc_buffer_len(key_payload));
   if (!payload)
@@ -357,6 +420,7 @@ SilcBool silc_client_save_channel_key(SilcClient client,
   if (!channel) {
     channel = silc_client_get_channel_by_id(client, conn, &id);
     if (!channel) {
+      SILC_LOG_DEBUG(("Key for unknown channel"));
       silc_channel_key_payload_free(payload);
       return FALSE;
     }
@@ -373,7 +437,7 @@ SilcBool silc_client_save_channel_key(SilcClient client,
     channel->internal.old_hmacs = silc_dlist_init();
   if (channel->internal.old_channel_keys && channel->internal.old_hmacs) {
     silc_dlist_add(channel->internal.old_channel_keys,
-                  channel->internal.channel_key);
+                  channel->internal.receive_key);
     silc_dlist_add(channel->internal.old_hmacs, channel->internal.hmac);
     silc_schedule_task_add_timeout(client->schedule,
                                   silc_client_save_channel_key_rekey,
@@ -382,7 +446,17 @@ SilcBool silc_client_save_channel_key(SilcClient client,
 
   /* Get channel cipher */
   cipher = silc_channel_key_get_cipher(payload, NULL);
-  if (!silc_cipher_alloc(cipher, &channel->internal.channel_key)) {
+  if (!silc_cipher_alloc(cipher, &channel->internal.send_key)) {
+    client->internal->ops->say(
+                          conn->client, conn,
+                          SILC_CLIENT_MESSAGE_AUDIT,
+                          "Cannot talk to channel: unsupported cipher %s",
+                          cipher);
+    silc_client_unref_channel(client, conn, channel);
+    silc_channel_key_payload_free(payload);
+    return FALSE;
+  }
+  if (!silc_cipher_alloc(cipher, &channel->internal.receive_key)) {
     client->internal->ops->say(
                           conn->client, conn,
                           SILC_CLIENT_MESSAGE_AUDIT,
@@ -393,9 +467,10 @@ SilcBool silc_client_save_channel_key(SilcClient client,
     return FALSE;
   }
 
-  /* Set the cipher key */
+  /* Set the cipher key.  Both sending and receiving keys are same */
   key = silc_channel_key_get_key(payload, &tmp_len);
-  silc_cipher_set_key(channel->internal.channel_key, key, tmp_len * 8);
+  silc_cipher_set_key(channel->internal.send_key, key, tmp_len * 8, TRUE);
+  silc_cipher_set_key(channel->internal.receive_key, key, tmp_len * 8, FALSE);
 
   /* Get channel HMAC */
   hmac = (channel->internal.hmac ?
@@ -412,12 +487,16 @@ SilcBool silc_client_save_channel_key(SilcClient client,
     return FALSE;
   }
 
+  channel->cipher = silc_cipher_get_name(channel->internal.send_key);
+  channel->hmac = silc_hmac_get_name(channel->internal.hmac);
+
   /* Set HMAC key */
   silc_hash_make(silc_hmac_get_hash(channel->internal.hmac), key,
                 tmp_len, hash);
   silc_hmac_set_key(channel->internal.hmac, hash,
                    silc_hash_len(silc_hmac_get_hash(channel->internal.hmac)));
   memset(hash, 0, sizeof(hash));
+  silc_channel_key_payload_free(payload);
 
   silc_client_unref_channel(client, conn, channel);
 
@@ -492,21 +571,31 @@ SilcBool silc_client_add_channel_private_key(SilcClient client,
   }
   entry->name = name ? strdup(name) : NULL;
 
-  /* Allocate the cipher and set the key*/
-  if (!silc_cipher_alloc(cipher, &entry->cipher)) {
+  /* Allocate the cipher and set the key */
+  if (!silc_cipher_alloc(cipher, &entry->send_key)) {
     silc_free(entry);
     silc_free(entry->name);
     silc_ske_free_key_material(keymat);
     return FALSE;
   }
-  silc_cipher_set_key(entry->cipher, keymat->send_enc_key,
-                     keymat->enc_key_len);
+  if (!silc_cipher_alloc(cipher, &entry->receive_key)) {
+    silc_free(entry);
+    silc_free(entry->name);
+    silc_cipher_free(entry->send_key);
+    silc_ske_free_key_material(keymat);
+    return FALSE;
+  }
+  silc_cipher_set_key(entry->send_key, keymat->send_enc_key,
+                     keymat->enc_key_len, TRUE);
+  silc_cipher_set_key(entry->receive_key, keymat->send_enc_key,
+                     keymat->enc_key_len, FALSE);
 
   /* Generate HMAC key from the channel key data and set it */
   if (!silc_hmac_alloc(hmac, NULL, &entry->hmac)) {
     silc_free(entry);
     silc_free(entry->name);
-    silc_cipher_free(entry->cipher);
+    silc_cipher_free(entry->send_key);
+    silc_cipher_free(entry->receive_key);
     silc_ske_free_key_material(keymat);
     return FALSE;
   }
@@ -519,8 +608,11 @@ SilcBool silc_client_add_channel_private_key(SilcClient client,
   /* Add to the private keys list */
   silc_dlist_add(channel->internal.private_keys, entry);
 
-  if (!channel->internal.curr_key)
+  if (!channel->internal.curr_key) {
     channel->internal.curr_key = entry;
+    channel->cipher = silc_cipher_get_name(entry->send_key);
+    channel->hmac = silc_cipher_get_name(entry->send_key);
+  }
 
   /* Free the key material */
   silc_ske_free_key_material(keymat);
@@ -551,12 +643,21 @@ SilcBool silc_client_del_channel_private_keys(SilcClient client,
   while ((entry = silc_dlist_get(channel->internal.private_keys))) {
     silc_dlist_del(channel->internal.private_keys, entry);
     silc_free(entry->name);
-    silc_cipher_free(entry->cipher);
+    silc_cipher_free(entry->send_key);
+    silc_cipher_free(entry->receive_key);
     silc_hmac_free(entry->hmac);
     silc_free(entry);
   }
 
   channel->internal.curr_key = NULL;
+  if (channel->internal.send_key)
+    channel->cipher = silc_cipher_get_name(channel->internal.send_key);
+  else
+    channel->cipher = NULL;
+  if (channel->internal.hmac)
+    channel->hmac = silc_hmac_get_name(channel->internal.hmac);
+  else
+    channel->hmac = NULL;
 
   silc_dlist_uninit(channel->internal.private_keys);
   channel->internal.private_keys = NULL;
@@ -588,12 +689,16 @@ SilcBool silc_client_del_channel_private_key(SilcClient client,
     if (entry != key)
       continue;
 
-    if (channel->internal.curr_key == entry)
+    if (channel->internal.curr_key == entry) {
       channel->internal.curr_key = NULL;
+      channel->cipher = silc_cipher_get_name(channel->internal.send_key);
+      channel->hmac = silc_hmac_get_name(channel->internal.hmac);
+    }
 
     silc_dlist_del(channel->internal.private_keys, entry);
     silc_free(entry->name);
-    silc_cipher_free(entry->cipher);
+    silc_cipher_free(entry->send_key);
+    silc_cipher_free(entry->receive_key);
     silc_hmac_free(entry->hmac);
     silc_free(entry);
 
@@ -650,6 +755,8 @@ void silc_client_current_channel_private_key(SilcClient client,
   if (!channel)
     return;
   channel->internal.curr_key = key;
+  channel->cipher = silc_cipher_get_name(key->send_key);
+  channel->hmac = silc_hmac_get_name(key->hmac);
 }
 
 /***************************** Utility routines *****************************/
@@ -671,7 +778,8 @@ SilcChannelUser silc_client_on_channel(SilcChannelEntry channel,
 }
 
 /* Adds client to channel.  Returns TRUE if user was added or is already
-   added to the channel, FALSE on error. */
+   added to the channel, FALSE on error.  Must be called with both `channel'
+   and `client_entry' locked. */
 
 SilcBool silc_client_add_to_channel(SilcClient client,
                                    SilcClientConnection conn,
@@ -684,6 +792,8 @@ SilcBool silc_client_add_to_channel(SilcClient client,
   if (silc_client_on_channel(channel, client_entry))
     return TRUE;
 
+  SILC_LOG_DEBUG(("Add client %s to channel", client_entry->nickname));
+
   chu = silc_calloc(1, sizeof(*chu));
   if (!chu)
     return FALSE;
@@ -701,7 +811,8 @@ SilcBool silc_client_add_to_channel(SilcClient client,
   return TRUE;
 }
 
-/* Removes client from a channel */
+/* Removes client from a channel.  Returns FALSE if user is not on channel.
+   This handles entry locking internally. */
 
 SilcBool silc_client_remove_from_channel(SilcClient client,
                                         SilcClientConnection conn,
@@ -714,17 +825,30 @@ SilcBool silc_client_remove_from_channel(SilcClient client,
   if (!chu)
     return FALSE;
 
+  SILC_LOG_DEBUG(("Remove client %s from channel", client_entry->nickname));
+
+  silc_rwlock_wrlock(client_entry->internal.lock);
+  silc_rwlock_wrlock(channel->internal.lock);
+
   silc_hash_table_del(chu->client->channels, chu->channel);
   silc_hash_table_del(chu->channel->user_list, chu->client);
   silc_free(chu);
 
+  /* If channel became empty, delete it */
+  if (!silc_hash_table_count(channel->user_list))
+    silc_client_del_channel(client, conn, channel);
+
+  silc_rwlock_unlock(client_entry->internal.lock);
+  silc_rwlock_unlock(channel->internal.lock);
+
   silc_client_unref_client(client, conn, client_entry);
   silc_client_unref_channel(client, conn, channel);
 
   return TRUE;
 }
 
-/* Removes a client entry from all channels it has joined. */
+/* Removes a client entry from all channels it has joined.  This handles
+   entry locking internally. */
 
 void silc_client_remove_from_channels(SilcClient client,
                                      SilcClientConnection conn,
@@ -733,19 +857,37 @@ void silc_client_remove_from_channels(SilcClient client,
   SilcHashTableList htl;
   SilcChannelUser chu;
 
+  if (!silc_hash_table_count(client_entry->channels))
+    return;
+
+  SILC_LOG_DEBUG(("Remove client from all joined channels"));
+
+  silc_rwlock_wrlock(client_entry->internal.lock);
+
   silc_hash_table_list(client_entry->channels, &htl);
   while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
+    silc_rwlock_wrlock(chu->channel->internal.lock);
+
     silc_hash_table_del(chu->client->channels, chu->channel);
     silc_hash_table_del(chu->channel->user_list, chu->client);
+
+    /* If channel became empty, delete it */
+    if (!silc_hash_table_count(chu->channel->user_list))
+      silc_client_del_channel(client, conn, chu->channel);
+
+    silc_rwlock_unlock(chu->channel->internal.lock);
+
     silc_client_unref_client(client, conn, chu->client);
     silc_client_unref_channel(client, conn, chu->channel);
     silc_free(chu);
   }
 
+  silc_rwlock_unlock(client_entry->internal.lock);
+
   silc_hash_table_list_reset(&htl);
 }
 
-/* Empties channel from users. */
+/* Empties channel from users.  This handles entry locking internally. */
 
 void silc_client_empty_channel(SilcClient client,
                               SilcClientConnection conn,
@@ -754,6 +896,8 @@ void silc_client_empty_channel(SilcClient client,
   SilcHashTableList htl;
   SilcChannelUser chu;
 
+  silc_rwlock_wrlock(channel->internal.lock);
+
   silc_hash_table_list(channel->user_list, &htl);
   while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
     silc_hash_table_del(chu->client->channels, chu->channel);
@@ -762,7 +906,73 @@ void silc_client_empty_channel(SilcClient client,
     silc_client_unref_channel(client, conn, chu->channel);
     silc_free(chu);
   }
+
+  silc_rwlock_unlock(channel->internal.lock);
+
   silc_hash_table_list_reset(&htl);
+}
+
+/* Save public keys to channel public key list.  Removes keys that are
+   marked to be removed.  Must be called with `channel' locked. */
+
+SilcBool silc_client_channel_save_public_keys(SilcChannelEntry channel,
+                                             unsigned char *chpk_list,
+                                             SilcUInt32 chpk_list_len,
+                                             SilcBool remove_all)
+{
+  SilcArgumentDecodedList a, b;
+  SilcDList chpks;
+  SilcBool found;
 
-  silc_hash_table_free(channel->user_list);
+  if (remove_all) {
+    /* Remove all channel public keys */
+    if (!channel->channel_pubkeys)
+      return FALSE;
+
+    silc_dlist_start(channel->channel_pubkeys);
+    while ((b = silc_dlist_get(channel->channel_pubkeys)))
+      silc_dlist_del(channel->channel_pubkeys, b);
+
+    silc_dlist_uninit(channel->channel_pubkeys);
+    channel->channel_pubkeys = NULL;
+
+    return TRUE;
+  }
+
+  /* Parse channel public key list and add or remove public keys */
+  chpks = silc_argument_list_parse_decoded(chpk_list, chpk_list_len,
+                                          SILC_ARGUMENT_PUBLIC_KEY);
+  if (!chpks)
+    return FALSE;
+
+  if (!channel->channel_pubkeys) {
+    channel->channel_pubkeys = silc_dlist_init();
+    if (!channel->channel_pubkeys) {
+      silc_argument_list_free(chpks, SILC_ARGUMENT_PUBLIC_KEY);
+      return FALSE;
+    }
+  }
+
+  silc_dlist_start(chpks);
+  while ((a = silc_dlist_get(chpks))) {
+    found = FALSE;
+    silc_dlist_start(channel->channel_pubkeys);
+    while ((b = silc_dlist_get(channel->channel_pubkeys))) {
+      if (silc_pkcs_public_key_compare(a->argument, b->argument)) {
+       found = TRUE;
+       break;
+      }
+    }
+
+    if ((a->arg_type == 0x00 || a->arg_type == 0x03) && !found) {
+      silc_dlist_add(channel->channel_pubkeys, a);
+      silc_dlist_del(chpks, a);
+    } else if (a->arg_type == 0x01 && found) {
+      silc_dlist_del(channel->channel_pubkeys, b);
+    }
+  }
+
+  silc_argument_list_free(chpks, SILC_ARGUMENT_PUBLIC_KEY);
+
+  return TRUE;
 }