Changed SILC_ASN1_OCTET_STRING to plain binary data.
[silc.git] / lib / silcasn1 / silcasn1_encode.c
index 123539f6fcd93d42b690832db1a0fbd14e430efb..f4937390e76de8fd575eb91d133ae20550cc1370 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 Pekka Riikonen
+  Copyright (C) 2003 - 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
@@ -199,6 +199,8 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
          len = silc_buffer_len(node);
          dest = silc_buffer_srealloc_size(stack1, dest,
                                           silc_buffer_truelen(dest) + len);
+         if (!dest)
+           goto fail;
          silc_buffer_put(dest, node->data, len);
        }
        break;
@@ -266,11 +268,15 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
             bytes in 1s complement */
        } else {
          /* Positive */
-         len = (silc_mp_sizeinbase(mpint, 2) + 7) / 8;
-         len += len & 7 ? 1: 0;
+         len = silc_mp_sizeinbase(mpint, 2);
+         if (!(len & 7))
+           len = ((len + 7) / 8) + 1;
+         else
+           len = (len + 7) / 8;
          silc_stack_push(stack2, &frame);
          silc_buffer_srealloc_size(stack2, &buf,
                                    silc_buffer_truelen(&buf) + len);
+         buf.data[0] = 0x00;
          silc_mp_mp2bin_noalloc(mpint, buf.data, silc_buffer_len(&buf));
        }
 
@@ -286,20 +292,58 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
        break;
       }
 
+    case SILC_ASN1_TAG_SHORT_INTEGER:
+      {
+       /* Short Integer */
+       SilcUInt32 sint = va_arg(asn1->ap, SilcUInt32);
+       SilcMPInt z;
+
+       if (tag == SILC_ASN1_TAG_SHORT_INTEGER)
+         tag = SILC_ASN1_TAG_INTEGER;
+
+       memset(&buf, 0, sizeof(buf));
+
+       silc_stack_push(stack2, &frame);
+       silc_mp_sinit(stack2, &z);
+       silc_mp_set_ui(&z, sint);
+
+       len = silc_mp_sizeinbase(&z, 2);
+       if (!(len & 7))
+         len = ((len + 7) / 8) + 1;
+       else
+         len = (len + 7) / 8;
+       silc_buffer_srealloc_size(stack2, &buf,
+                                 silc_buffer_truelen(&buf) + len);
+       buf.data[0] = 0x00;
+       silc_mp_mp2bin_noalloc(&z, buf.data, silc_buffer_len(&buf));
+       silc_mp_uninit(&z);
+
+       /* Encode the integer */
+       len = silc_ber_encoded_len(tag, len, indef);
+       dest = silc_buffer_srealloc_size(stack1, dest,
+                                        silc_buffer_truelen(dest) + len);
+       ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
+                             tag, buf.data, silc_buffer_len(&buf), FALSE);
+       silc_stack_pop(stack2);
+       if (!ret)
+         goto fail;
+       break;
+      }
+      break;
+
     case SILC_ASN1_TAG_OID:
       {
        /* Object identifier */
        char *cp, *oidstr = va_arg(asn1->ap, char *);
        SilcUInt32 words[24], oid, mask;
-       int i, c = -1;
+       int i, k, c = 0;
        if (!oidstr)
          break;
 
        /* Get OID words from the string */
        cp = strchr(oidstr, '.');
        while (cp) {
-         c = sscanf(oidstr, "%lu", (unsigned long *)&oid);
-         if (c < 1) {
+         if (sscanf(oidstr, "%lu", (unsigned long *)&oid) != 1) {
            SILC_LOG_DEBUG(("Malformed OID string"));
            goto fail;
          }
@@ -308,6 +352,17 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
          words[c++] = oid;
          oidstr = cp + 1;
          cp = strchr(oidstr, '.');
+
+         if (!cp) {
+           if (sscanf(oidstr, "%lu", (unsigned long *)&oid) != 1) {
+             SILC_LOG_DEBUG(("Malformed OID string"));
+             goto fail;
+           }
+           if (c + 1 > sizeof(words) / sizeof(words[0]))
+             goto fail;
+           words[c++] = oid;
+           break;
+         }
        }
        if (c < 2) {
          SILC_LOG_DEBUG(("Malfromed OID string"));
@@ -333,7 +388,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
        for (i = 2, len = 1; i < c; i++) {
          oid = words[i];
          if (oid) {
-           c = len;
+           k = len;
            mask = 0;
            while (oid) {
              buf.data[len++] = (oid & 0x7f) | mask;
@@ -341,11 +396,11 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
              mask |= 0x80;
            }
            mask = len - 1;
-           while (c < mask) {
-             oid = buf.data[c];
-             buf.data[c] = buf.data[mask];
+           while (k < mask) {
+             oid = buf.data[k];
+             buf.data[k] = buf.data[mask];
              buf.data[mask] = oid;
-             c++;
+             k++;
              mask--;
            }
 
@@ -354,7 +409,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
          buf.data[len++] = 0x00;
        }
 
-       len = silc_ber_encoded_len(tag, len, indef);
+       len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
        dest = silc_buffer_srealloc_size(stack1, dest,
                                         silc_buffer_truelen(dest) + len);
        ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
@@ -500,8 +555,17 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
 
     case SILC_ASN1_TAG_OCTET_STRING:
       {
-       /* Octet string.  We put it in as 8-bit ASCII */
-       SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
+       /* Octet string.  Put data as is. */
+       unsigned char *d = va_arg(asn1->ap, unsigned char *);
+       SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
+
+       len = silc_ber_encoded_len(tag, d_len, indef);
+       dest = silc_buffer_srealloc_size(stack1, dest,
+                                        silc_buffer_truelen(dest) + len);
+       ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
+                             tag, d, d_len, indef);
+       if (!ret)
+         goto fail;
        break;
       }
 
@@ -631,7 +695,6 @@ SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...)
   SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
   if (!type) {
     va_end(asn1->ap);
-    asn1->ap = NULL;
     return FALSE;
   }
 
@@ -686,7 +749,6 @@ SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...)
     asn1->stack1 = stack1;
 
   va_end(asn1->ap);
-  asn1->ap = NULL;
 
   return ret;
 }