updates.
[silc.git] / lib / silccore / silcid.c
index 870400638b721c0fdc712e5ccd983163b7768153..6a7fb8f2c72c38c20902cd95f137045b61b32c00 100644 (file)
@@ -64,7 +64,7 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
 
   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,
@@ -84,12 +84,13 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
 
 /* 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;
 
@@ -99,25 +100,25 @@ void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
                             SILC_STR_UI_SHORT(&idlen),
                             SILC_STR_END);
   if (ret == -1)
-    goto err;
+    return NULL;
 
   silc_buffer_pull(&buffer, 4);
 
-  if (idlen > buffer.len)
-    goto err;
+  if (idlen > buffer.len || idlen > SILC_PACKET_MAX_ID_LEN)
+    return NULL;
 
   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;
+    return NULL;
 
   id = silc_id_str2id(id_data, idlen, type);
-  silc_free(id_data);
-  return id;
 
- err:
-  return NULL;
+  if (ret_type)
+    *ret_type = type;
+
+  return id;
 }
 
 /* Encodes ID Payload */
@@ -215,8 +216,8 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type)
     if (!ret_id)
       return NULL;
     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
-    SILC_PUT16_MSB(server_id->port, &ret_id[4]);
-    SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
+    SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
+    SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
     return ret_id;
     break;
   case SILC_ID_CLIENT:
@@ -225,8 +226,9 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type)
     if (!ret_id)
       return NULL;
     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
-    ret_id[4] = client_id->rnd;
-    memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
+    ret_id[client_id->ip.data_len] = client_id->rnd;
+    memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash, 
+          CLIENTID_HASH_LEN);
     return ret_id;
     break;
   case SILC_ID_CHANNEL:
@@ -235,8 +237,8 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type)
     if (!ret_id)
       return NULL;
     memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
-    SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
-    SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
+    SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
+    SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
     return ret_id;
     break;
   }
@@ -246,7 +248,8 @@ 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)
 {
 
   switch(type) {
@@ -264,8 +267,8 @@ void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type
       memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
                                      16 : 4));
       server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
-      SILC_GET16_MSB(server_id->port, &id[4]);
-      SILC_GET16_MSB(server_id->rnd, &id[6]);
+      SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
+      SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
       return server_id;
     }
     break;
@@ -283,8 +286,9 @@ void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type
       memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
                                      16 : 4));
       client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
-      client_id->rnd = id[4];
-      memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
+      client_id->rnd = id[client_id->ip.data_len];
+      memcpy(client_id->hash, &id[client_id->ip.data_len + 1], 
+            CLIENTID_HASH_LEN);
       return client_id;
     }
     break;
@@ -302,8 +306,8 @@ void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type
       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
                                       16 : 4));
       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
-      SILC_GET16_MSB(channel_id->port, &id[4]);
-      SILC_GET16_MSB(channel_id->rnd, &id[6]);
+      SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
+      SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
       return channel_id;
     }
     break;