Fixed OID encoding.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 1 Jan 2006 12:58:53 +0000 (12:58 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 1 Jan 2006 12:58:53 +0000 (12:58 +0000)
lib/silcasn1/silcasn1_encode.c
lib/silcasn1/tests/test_silcasn1.c
lib/silccrypt/DIRECTORY
lib/silccrypt/silcpkcs.c
lib/silccrypt/silcpkcs1.c
lib/silcutil/silcbuffmt.c

index d8706b73a9dbd31643d3a0c99913a63cea5f0ac9..fb24c0648687beba13ed17346714c0c868f87a1d 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
@@ -184,7 +184,10 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
            SILC_LOG_DEBUG(("Error decoding underlaying node for ANY"));
            goto fail;
          }
-         assert(enc == SILC_BER_ENC_CONSTRUCTED);
+         if (enc != SILC_BER_ENC_CONSTRUCTED) {
+           SILC_LOG_DEBUG(("ANY was not constructed type"));
+           goto fail;
+         }
 
          /* Now encode with implicit tagging */
          len = silc_ber_encoded_len(tag, d_len, FALSE);
@@ -297,15 +300,14 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
        /* 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;
          }
@@ -314,6 +316,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"));
@@ -339,7 +352,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;
@@ -347,11 +360,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--;
            }
 
@@ -360,7 +373,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,
index fad097b5b3644cd9d9ad931b2003b96254ea6c3f..1400a2692627b9df669de167d915a3a274fc003f 100644 (file)
@@ -665,7 +665,7 @@ int main(int argc, char **argv)
   }
   SILC_LOG_DEBUG(("Encoding success"));
   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
-  SILC_LOG_DEBUG(("Decoding ASN.1 tree 9"));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree 10 (INTEGER)"));
   success =
     silc_asn1_decode(asn1, &node,
                     SILC_ASN1_INT(&mpint2),
@@ -681,6 +681,35 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Decoding success"));
   printf("\n");
 
+
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree 11 (OID)"));
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
+                    SILC_ASN1_OID("1.2.840.113549"),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree 11 (OID)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
+                    SILC_ASN1_OID(&str),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Decoding success"));
+  SILC_LOG_DEBUG(("OID %s", str));
+  printf("\n");
+
+
 #endif
   silc_asn1_free(asn1);
 
index 6c7c743205336bde1cca4d360c05a4cd09279e64..4a777cd5d8a517b192e98fd40bbb29e1522896c7 100644 (file)
@@ -5,7 +5,8 @@
 @LINK=silcrng.html:SILC RNG Interface
 @LINK=silccipher.html:SILC Cipher API
 @LINK=silcpkcs.html:SILC PKCS API
-@LINK=silcpkcs1.html:SILC PKCS#1 API
+@LINK=silcpk.html:SILC Public Key API
+@LINK=silcpkcs1.html:SILC PKCS #1 API
 @LINK=silchash.html:SILC Hash Interface
 @LINK=silchmac.html:SILC HMAC Interface
 -->
index 7aeb3d1c09ecf41979fafe3abf4eefc8af9a93eb..baf042be51372bd5fc78749e49e597f771ec4123 100644 (file)
@@ -672,13 +672,17 @@ SilcBool silc_pkcs_load_public_key(const char *filename,
 
     if (public_key->pkcs->import_public_key_file(data, data_len,
                                                 SILC_PKCS_FILE_BASE64,
-                                                &public_key->public_key))
+                                                &public_key->public_key)) {
+      silc_free(data);
       return TRUE;
+    }
 
     if (public_key->pkcs->import_public_key_file(data, data_len,
                                                 SILC_PKCS_FILE_BIN,
-                                                &public_key->public_key))
+                                                &public_key->public_key)) {
+      silc_free(data);
       return TRUE;
+    }
   }
 
   silc_free(data);
@@ -745,19 +749,25 @@ SilcBool silc_pkcs_load_private_key(const char *filename,
     if (!private_key->pkcs)
       continue;
 
-    if (private_key->pkcs->import_private_key_file(data, data_len,
-                                                  passphrase,
-                                                  passphrase_len,
-                                                  SILC_PKCS_FILE_BIN,
-                                                  &private_key->private_key))
+    if (private_key->pkcs->import_private_key_file(
+                                             data, data_len,
+                                             passphrase,
+                                             passphrase_len,
+                                             SILC_PKCS_FILE_BIN,
+                                             &private_key->private_key)) {
+      silc_free(data);
       return TRUE;
+    }
 
-    if (private_key->pkcs->import_private_key_file(data, data_len,
-                                                  passphrase,
-                                                  passphrase_len,
-                                                  SILC_PKCS_FILE_BASE64,
-                                                  &private_key->private_key))
+    if (private_key->pkcs->import_private_key_file(
+                                             data, data_len,
+                                             passphrase,
+                                             passphrase_len,
+                                             SILC_PKCS_FILE_BASE64,
+                                             &private_key->private_key)) {
+      silc_free(data);
       return TRUE;
+    }
   }
 
   silc_free(data);
index 57f25e675fa2b597926db3bd163c85cb2de2ed60..5820861d9fd36774b421dbf704d87b9728162588 100644 (file)
@@ -239,8 +239,8 @@ SilcBool silc_pkcs1_import_public_key(unsigned char *key,
   if (!silc_asn1_decode(asn1, &alg_key,
                        SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
                        SILC_ASN1_SEQUENCE,
-                       SILC_ASN1_INT(&pubkey->n),
-                       SILC_ASN1_INT(&pubkey->e),
+                         SILC_ASN1_INT(&pubkey->n),
+                         SILC_ASN1_INT(&pubkey->e),
                        SILC_ASN1_END, SILC_ASN1_END))
     goto err;
 
@@ -275,8 +275,8 @@ unsigned char *silc_pkcs1_export_public_key(void *public_key,
   if (!silc_asn1_encode(asn1, &alg_key,
                        SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
                        SILC_ASN1_SEQUENCE,
-                       SILC_ASN1_INT(&key->n),
-                       SILC_ASN1_INT(&key->e),
+                         SILC_ASN1_INT(&key->n),
+                         SILC_ASN1_INT(&key->e),
                        SILC_ASN1_END, SILC_ASN1_END))
     goto err;
 
@@ -372,15 +372,15 @@ SilcBool silc_pkcs1_import_private_key(unsigned char *key,
   if (!silc_asn1_decode(asn1, &alg_key,
                        SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
                        SILC_ASN1_SEQUENCE,
-                       SILC_ASN1_INT(NULL),
-                       SILC_ASN1_INT(&privkey->n),
-                       SILC_ASN1_INT(&privkey->e),
-                       SILC_ASN1_INT(&privkey->d),
-                       SILC_ASN1_INT(&privkey->p),
-                       SILC_ASN1_INT(&privkey->q),
-                       SILC_ASN1_INT(&privkey->dP),
-                       SILC_ASN1_INT(&privkey->dQ),
-                       SILC_ASN1_INT(&privkey->qP),
+                         SILC_ASN1_INT(NULL),
+                         SILC_ASN1_INT(&privkey->n),
+                         SILC_ASN1_INT(&privkey->e),
+                         SILC_ASN1_INT(&privkey->d),
+                         SILC_ASN1_INT(&privkey->p),
+                         SILC_ASN1_INT(&privkey->q),
+                         SILC_ASN1_INT(&privkey->dP),
+                         SILC_ASN1_INT(&privkey->dQ),
+                         SILC_ASN1_INT(&privkey->qP),
                        SILC_ASN1_END, SILC_ASN1_END))
     goto err;
 
@@ -418,15 +418,15 @@ unsigned char *silc_pkcs1_export_private_key(void *private_key,
   if (!silc_asn1_encode(asn1, &alg_key,
                        SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
                        SILC_ASN1_SEQUENCE,
-                       SILC_ASN1_INT(&version),
-                       SILC_ASN1_INT(&key->n),
-                       SILC_ASN1_INT(&key->e),
-                       SILC_ASN1_INT(&key->d),
-                       SILC_ASN1_INT(&key->p),
-                       SILC_ASN1_INT(&key->q),
-                       SILC_ASN1_INT(&key->dP),
-                       SILC_ASN1_INT(&key->dQ),
-                       SILC_ASN1_INT(&key->qP),
+                         SILC_ASN1_INT(&version),
+                         SILC_ASN1_INT(&key->n),
+                         SILC_ASN1_INT(&key->e),
+                         SILC_ASN1_INT(&key->d),
+                         SILC_ASN1_INT(&key->p),
+                         SILC_ASN1_INT(&key->q),
+                         SILC_ASN1_INT(&key->dP),
+                         SILC_ASN1_INT(&key->dQ),
+                         SILC_ASN1_INT(&key->qP),
                        SILC_ASN1_END, SILC_ASN1_END))
     goto err;
   silc_mp_uninit(&version);
index 3c96f862ca1338f7bc93888a60407c170ff3adc2..fb251a8f4e774212541caf594bf02252688682fd 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2005 Pekka Riikonen
+  Copyright (C) 1997 - 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
@@ -602,7 +602,7 @@ int silc_buffer_sstrformat(SilcStack stack, SilcBuffer dst, ...)
       goto ok;
 
     slen = strlen(string);
-    d = silc_srealloc_ua(stack, len, dst->head,
+    d = silc_srealloc_ua(stack, len + 1, dst->head,
                         sizeof(*dst->head) * (slen + len + 1));
     if (!d)
       return -1;