Added preliminary Symbian support.
[silc.git] / lib / silcasn1 / silcasn1_decode.c
index 34b93ca26ab182020458843602b5556469b3f439..911515b2b88ae472c2ba7d5ac2d26cc4ad1e0d05 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 Pekka Riikonen
+  Copyright (C) 2003 - 2006 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
@@ -21,7 +21,6 @@
 #include "silcasn1.h"
 #include "silcber.h"
 
-
 /************************** ASN.1 Decoder routines **************************/
 
 /* Internal SEQUENCE OF and SET OF decoder.  This is used only when decoding
@@ -311,6 +310,11 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
       }
     }
 
+    /* Short integer is actually big integer in the BER data, so handle
+       it correctly */
+    if (type == SILC_ASN1_TAG_SHORT_INTEGER && type == tag)
+      tag = SILC_ASN1_TAG_INTEGER;
+
     /* Now decode a BER encoded block from the source buffer.  It must be
        exactly the same user is expecting. */
     ret = silc_ber_decode(src, &rclass, &renc, (SilcUInt32 *)&rtag, &rdata,
@@ -472,7 +476,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
 
            /* 2s complement and change sign */
            silc_mp_init(&z);
-           silc_mp_set(&z, 0);
+           silc_mp_set_ui(&z, 0);
            silc_mp_add_ui(*intval, *intval, 1);
            silc_mp_sub(*intval, &z, *intval);
            silc_mp_uninit(&z);
@@ -484,6 +488,28 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
          break;
        }
 
+      case SILC_ASN1_TAG_SHORT_INTEGER:
+       {
+         /* Short Integer */
+         SilcMPInt z;
+         SILC_ASN1_VAD(asn1, opts, SilcUInt32, intval);
+
+         if (rdata_len < 1) {
+           SILC_LOG_DEBUG(("Malformed integer value"));
+           SILC_ASN1_VA_FREE(opts, intval);
+           ret = FALSE;
+           goto fail;
+         }
+
+         silc_stack_push(asn1->stack1, NULL);
+         silc_mp_sinit(asn1->stack1, &z);
+         silc_mp_bin2mp((unsigned char *)rdata, rdata_len, &z);
+         *(*intval) = silc_mp_get_ui(&z);
+         silc_mp_uninit(&z);
+         silc_stack_pop(asn1->stack1);
+         break;
+       }
+
       case SILC_ASN1_TAG_OID:
        {
          /* Object identifier */
@@ -494,6 +520,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
 
          if (rdata_len < 1) {
            SILC_LOG_DEBUG(("Malformed object identifier value"));
+           SILC_ASN1_VA_FREE(opts, oidstr);
            ret = FALSE;
            goto fail;
          }
@@ -501,7 +528,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
          /* Set two OID values */
          memset(&tmpb, 0, sizeof(tmpb));
          memset(tmpstr, 0, sizeof(tmpstr));
-         snprintf(tmpstr, sizeof(tmpstr) - 1, "%lu.%lu",
+         silc_snprintf(tmpstr, sizeof(tmpstr) - 1, "%lu.%lu",
                   (unsigned long)(rdata[0] & 0xff) / 40,
                   (unsigned long)(rdata[0] & 0xff) % 40);
          silc_buffer_sstrformat(asn1->stack1, &tmpb, tmpstr, SILC_STR_END);
@@ -523,7 +550,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
            oid |= rdata[i];
 
            memset(tmpstr, 0, sizeof(tmpstr));
-           snprintf(tmpstr, sizeof(tmpstr) - 1, ".%lu", (unsigned long)oid);
+           silc_snprintf(tmpstr, sizeof(tmpstr) - 1, ".%lu", (unsigned long)oid);
            silc_buffer_sstrformat(asn1->stack1, &tmpb, tmpstr, SILC_STR_END);
          }
          *oidstr = tmpb.head;
@@ -554,6 +581,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
 
          if (rdata_len < 2) {
            SILC_LOG_DEBUG(("Malformed bit string value"));
+           SILC_ASN1_VA_FREE(opts, d);
            ret = FALSE;
            goto fail;
          }
@@ -580,6 +608,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
 
          if (rdata_len < 1) {
            SILC_LOG_DEBUG(("Malformed UTC time value"));
+           SILC_ASN1_VA_FREE(opts, t);
            ret = FALSE;
            goto fail;
          }
@@ -601,6 +630,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
 
          if (rdata_len < 1) {
            SILC_LOG_DEBUG(("Malformed generalized time value"));
+           SILC_ASN1_VA_FREE(opts, t);
            ret = FALSE;
            goto fail;
          }
@@ -608,6 +638,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
          /* Parse the time string */
          if (!silc_time_generalized(rdata, *t)) {
            SILC_LOG_DEBUG(("Malformed generalized time value"));
+           SILC_ASN1_VA_FREE(opts, t);
            ret = FALSE;
            goto fail;
          }
@@ -622,6 +653,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
 
          if (!silc_utf8_valid(rdata, rdata_len)) {
            SILC_LOG_DEBUG(("Malformed UTF-8 string value"));
+           SILC_ASN1_VA_FREE(opts, s);
            ret = FALSE;
            goto fail;
          }
@@ -813,7 +845,6 @@ SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...)
   SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
   if (!type) {
     va_end(asn1->ap);
-    asn1->ap = NULL;
     return FALSE;
   }
 
@@ -886,7 +917,6 @@ SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...)
   }
 
   va_end(asn1->ap);
-  asn1->ap = NULL;
 
   return ret;
 }