Code auditing weekend results and fixes committing.
[silc.git] / lib / silccrypt / silcpkcs.c
index e501d2742aed5672d91e738fb2ce12037fb72fd1..74870ec8a33f0c4ee4361190d4e521efa99841dc 100644 (file)
@@ -17,6 +17,7 @@
   GNU General Public License for more details.
 
 */
+/* $Id$ */
 
 #include "silcincludes.h"
 
@@ -256,7 +257,7 @@ char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
   }
 
   silc_buffer_push(buf, buf->data - buf->head);
-  identifier = silc_calloc(tlen, sizeof(*identifier));
+  identifier = silc_calloc(tlen + 1, sizeof(*identifier));
   memcpy(identifier, buf->data, tlen);
   silc_buffer_free(buf);
 
@@ -333,7 +334,7 @@ silc_pkcs_public_key_encode(SilcPublicKey public_key, unsigned int *len)
   unsigned char *ret;
 
   buf = silc_buffer_alloc(public_key->len);
-  silc_buffer_pull_tail(buf, public_key->len);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
 
   silc_buffer_format(buf,
                     SILC_STR_UI_INT(public_key->len),
@@ -342,7 +343,7 @@ silc_pkcs_public_key_encode(SilcPublicKey public_key, unsigned int *len)
                     SILC_STR_UI_SHORT(strlen(public_key->identifier)),
                     SILC_STR_UI32_STRING(public_key->identifier),
                     SILC_STR_UI_XNSTRING(public_key->pk, 
-                                           public_key->pk_len),
+                                         public_key->pk_len),
                     SILC_STR_END);
   if (len)
     *len = public_key->len;
@@ -367,7 +368,7 @@ silc_pkcs_public_key_data_encode(unsigned char *pk, unsigned int pk_len,
 
   totlen = 4 + 2 + strlen(pkcs) + 2 + strlen(identifier) + pk_len;
   buf = silc_buffer_alloc(totlen);
-  silc_buffer_pull_tail(buf, totlen);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
 
   silc_buffer_format(buf,
                     SILC_STR_UI_INT(totlen),
@@ -398,15 +399,20 @@ int silc_pkcs_public_key_decode(unsigned char *data, unsigned int data_len,
   unsigned short pkcs_len, identifier_len;
   unsigned int totlen, key_len;
   unsigned char *pkcs_name = NULL, *ident = NULL, *key_data = NULL;
+  int ret;
 
   buf = silc_buffer_alloc(data_len);
-  silc_buffer_pull_tail(buf, data_len);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
   silc_buffer_put(buf, data, data_len);
 
   /* Get length */
-  silc_buffer_unformat(buf,
-                      SILC_STR_UI_INT(&totlen),
-                      SILC_STR_END);
+  ret = silc_buffer_unformat(buf,
+                            SILC_STR_UI_INT(&totlen),
+                            SILC_STR_END);
+  if (ret == -1) {
+    silc_buffer_free(buf);
+    return FALSE;
+  }
 
   if (totlen != data_len) {
     silc_buffer_free(buf);
@@ -415,10 +421,13 @@ int silc_pkcs_public_key_decode(unsigned char *data, unsigned int data_len,
 
   /* Get algorithm name and identifier */
   silc_buffer_pull(buf, 4);
-  silc_buffer_unformat(buf,
-                      SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
-                      SILC_STR_UI16_NSTRING_ALLOC(&ident, &identifier_len),
-                      SILC_STR_END);
+  ret =
+    silc_buffer_unformat(buf,
+                        SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
+                        SILC_STR_UI16_NSTRING_ALLOC(&ident, &identifier_len),
+                        SILC_STR_END);
+  if (ret == -1)
+    goto err;
 
   if (pkcs_len < 1 || identifier_len < 3 || 
       pkcs_len + identifier_len > totlen)
@@ -436,9 +445,11 @@ int silc_pkcs_public_key_decode(unsigned char *data, unsigned int data_len,
   /* Get key data. We assume that rest of the buffer is key data. */
   silc_buffer_pull(buf, 2 + pkcs_len + 2 + identifier_len);
   key_len = buf->len;
-  silc_buffer_unformat(buf,
-                      SILC_STR_UI_XNSTRING_ALLOC(&key_data, key_len),
-                      SILC_STR_END);
+  ret = silc_buffer_unformat(buf,
+                            SILC_STR_UI_XNSTRING_ALLOC(&key_data, key_len),
+                            SILC_STR_END);
+  if (ret == -1)
+    goto err;
 
   /* Try to set the key. If this fails the key must be malformed. This
      code assumes that the PKCS routine checks the format of the key. */
@@ -481,7 +492,7 @@ silc_pkcs_private_key_encode(SilcPrivateKey private_key, unsigned int *len)
 
   totlen = 2 + strlen(private_key->name) + private_key->prv_len;
   buf = silc_buffer_alloc(totlen);
-  silc_buffer_pull_tail(buf, totlen);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
 
   silc_buffer_format(buf,
                     SILC_STR_UI_SHORT(strlen(private_key->name)),
@@ -539,15 +550,19 @@ int silc_pkcs_private_key_decode(unsigned char *data, unsigned int data_len,
   unsigned short pkcs_len;
   unsigned int key_len;
   unsigned char *pkcs_name = NULL, *key_data = NULL;
+  int ret;
 
   buf = silc_buffer_alloc(data_len);
-  silc_buffer_pull_tail(buf, data_len);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
   silc_buffer_put(buf, data, data_len);
 
   /* Get algorithm name and identifier */
-  silc_buffer_unformat(buf,
-                      SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
-                      SILC_STR_END);
+  ret = 
+    silc_buffer_unformat(buf,
+                        SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
+                        SILC_STR_END);
+  if (ret == -1)
+    goto err;
 
   if (pkcs_len < 1 || pkcs_len > buf->truelen)
     goto err;
@@ -559,9 +574,11 @@ int silc_pkcs_private_key_decode(unsigned char *data, unsigned int data_len,
   /* Get key data. We assume that rest of the buffer is key data. */
   silc_buffer_pull(buf, 2 + pkcs_len);
   key_len = buf->len;
-  silc_buffer_unformat(buf,
-                      SILC_STR_UI_XNSTRING_ALLOC(&key_data, key_len),
-                      SILC_STR_END);
+  ret = silc_buffer_unformat(buf,
+                            SILC_STR_UI_XNSTRING_ALLOC(&key_data, key_len),
+                            SILC_STR_END);
+  if (ret == -1)
+    goto err;
 
   /* Try to set the key. If this fails the key must be malformed. This
      code assumes that the PKCS routine checks the format of the key. */
@@ -611,7 +628,7 @@ static int silc_pkcs_save_public_key_internal(char *filename,
   len = data_len + (strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN) +
                    strlen(SILC_PKCS_PUBLIC_KEYFILE_END));
   buf = silc_buffer_alloc(len);
-  silc_buffer_pull_tail(buf, len);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
 
   silc_buffer_format(buf,
                     SILC_STR_UI32_STRING(SILC_PKCS_PUBLIC_KEYFILE_BEGIN),
@@ -674,7 +691,7 @@ static int silc_pkcs_save_private_key_internal(char *filename,
   len = data_len + (strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN) +
                    strlen(SILC_PKCS_PRIVATE_KEYFILE_END));
   buf = silc_buffer_alloc(len);
-  silc_buffer_pull_tail(buf, len);
+  silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
 
   silc_buffer_format(buf,
                     SILC_STR_UI32_STRING(SILC_PKCS_PRIVATE_KEYFILE_BEGIN),
@@ -758,7 +775,7 @@ int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
       break;
     }
 
-    if (!silc_pkcs_public_key_decode(data, len, public_key)) {
+    if (!data || !silc_pkcs_public_key_decode(data, len, public_key)) {
       memset(old, 0, data_len);
       silc_free(old);
       return FALSE;
@@ -810,7 +827,7 @@ int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
       break;
     }
 
-    if (!silc_pkcs_private_key_decode(data, len, private_key)) {
+    if (!data || !silc_pkcs_private_key_decode(data, len, private_key)) {
       memset(old, 0, data_len);
       silc_free(old);
       return FALSE;