Merged silc_1_0_branch to trunk.
[silc.git] / lib / silccore / silcid.c
index a46892c1f1725be39c1f67c46371488784dd16f5..ad0bf94b76ecc3df13e71514c7fd2a20530a18cf 100644 (file)
@@ -2,14 +2,13 @@
 
   id.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2005 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
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
+  the Free Software Foundation; version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -48,8 +47,6 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
   SilcIDPayload newp;
   int ret;
 
-  SILC_LOG_DEBUG(("Parsing ID payload"));
-
   silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
   newp = silc_calloc(1, sizeof(*newp));
   if (!newp)
@@ -62,9 +59,12 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
   if (ret == -1)
     goto err;
 
+  if (newp->type > SILC_ID_CHANNEL)
+    goto err;
+
   silc_buffer_pull(&buffer, 4);
 
-  if (newp->len > buffer.len)
+  if (newp->len > buffer.len || newp->len > SILC_PACKET_MAX_ID_LEN)
     goto err;
 
   ret = silc_buffer_unformat(&buffer,
@@ -78,18 +78,20 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
   return newp;
 
  err:
+  SILC_LOG_DEBUG(("Error parsing ID payload"));
   silc_free(newp);
   return NULL;
 }
 
 /* Return the ID directly from the raw payload data. */
 
-void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
+void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
+                              SilcIdType *ret_type)
 {
   SilcBufferStruct buffer;
   SilcIdType type;
   SilcUInt16 idlen;
-  unsigned char *id_data = NULL;
+  unsigned char *id_data;
   int ret;
   void *id;
 
@@ -101,22 +103,29 @@ void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
   if (ret == -1)
     goto err;
 
+  if (type > SILC_ID_CHANNEL)
+    goto err;
+
   silc_buffer_pull(&buffer, 4);
 
-  if (idlen > buffer.len)
+  if (idlen > buffer.len || idlen > SILC_PACKET_MAX_ID_LEN)
     goto err;
 
   ret = silc_buffer_unformat(&buffer,
-                            SILC_STR_UI_XNSTRING_ALLOC(&id_data, idlen),
+                            SILC_STR_UI_XNSTRING(&id_data, idlen),
                             SILC_STR_END);
   if (ret == -1)
     goto err;
 
   id = silc_id_str2id(id_data, idlen, type);
-  silc_free(id_data);
+
+  if (ret_type)
+    *ret_type = type;
+
   return id;
 
  err:
+  SILC_LOG_DEBUG(("Error parsing ID payload"));
   return NULL;
 }
 
@@ -141,10 +150,6 @@ SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
 {
   SilcBuffer buffer;
 
-  SILC_LOG_DEBUG(("Encoding %s ID payload",
-                 type == SILC_ID_CLIENT ? "Client" :
-                 type == SILC_ID_SERVER ? "Server" : "Channel"));
-
   buffer = silc_buffer_alloc_size(4 + id_len);
   if (!buffer)
     return NULL;
@@ -208,6 +213,9 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type)
   SilcChannelID *channel_id;
   SilcUInt32 id_len = silc_id_get_len(id, type);
 
+  if (id_len > SILC_PACKET_MAX_ID_LEN)
+    return NULL;
+
   switch(type) {
   case SILC_ID_SERVER:
     server_id = (SilcServerID *)id;
@@ -247,8 +255,11 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type)
 
 /* Converts string to a ID */
 
-void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type)
+void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, 
+                    SilcIdType type)
 {
+  if (id_len > SILC_PACKET_MAX_ID_LEN)
+    return NULL;
 
   switch(type) {
   case SILC_ID_SERVER: