]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
asn1: Add function to convert known OIDs to strings
authorTobias Brunner <tobias@strongswan.org>
Mon, 16 Sep 2013 12:54:11 +0000 (14:54 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 16 Sep 2013 13:23:10 +0000 (15:23 +0200)
src/libstrongswan/asn1/asn1.c
src/libstrongswan/asn1/asn1.h

index d860ad9a2a1a3af8fffc7a01d8e66a23812d3f38..2cb5250108c2f9986063b0aa5c466e6b7bc342f5 100644 (file)
@@ -91,35 +91,54 @@ int asn1_known_oid(chunk_t object)
        return -1;
 }
 
+/**
+ * Fill the octets of a known OID into the given chunk (already allocated with
+ * len == oid_names[idx].level + 1).
+ */
+static bool fill_known_oid(chunk_t chunk, int oid)
+{
+       int i;
+
+       i = oid_names[oid].level + 1;
+       if (chunk.len < i)
+       {
+               return FALSE;
+       }
+       do
+       {
+               if (oid_names[oid].level >= i)
+               {
+                       oid--;
+                       continue;
+               }
+               chunk.ptr[--i] = oid_names[oid--].octet;
+       }
+       while (i > 0);
+       return TRUE;
+}
+
 /*
  * Defined in header.
  */
 chunk_t asn1_build_known_oid(int n)
 {
        chunk_t oid;
-       int i;
+       int len;
 
        if (n < 0 || n >= OID_MAX)
        {
                return chunk_empty;
        }
 
-       i = oid_names[n].level + 1;
-       oid = chunk_alloc(2 + i);
+       len = oid_names[n].level + 1;
+       oid = chunk_alloc(2 + len);
        oid.ptr[0] = ASN1_OID;
-       oid.ptr[1] = i;
-
-       do
+       oid.ptr[1] = len;
+       if (!fill_known_oid(chunk_skip(oid, 2), n))
        {
-               if (oid_names[n].level >= i)
-               {
-                       n--;
-                       continue;
-               }
-               oid.ptr[--i + 2] = oid_names[n--].octet;
+               chunk_free(&oid);
+               return chunk_empty;
        }
-       while (i > 0);
-
        return oid;
 }
 
@@ -212,6 +231,25 @@ char *asn1_oid_to_string(chunk_t oid)
        return (val == 0) ? strdup(buf) : NULL;
 }
 
+/*
+ * Defined in header.
+ */
+char *asn1_known_oid_to_string(int oid)
+{
+       chunk_t buf;
+
+       if (oid < 0 || oid >= OID_MAX)
+       {
+               return NULL;
+       }
+       buf = chunk_alloca(oid_names[oid].level + 1);
+       if (!fill_known_oid(buf, oid))
+       {
+               return NULL;
+       }
+       return asn1_oid_to_string(buf);
+}
+
 /*
  * Defined in header.
  */
index a1d62538035edd311ab682d8488c9c368e241ae5..1e77487c704cf837b5011c0dff09a03f5742420b 100644 (file)
@@ -130,6 +130,14 @@ chunk_t asn1_oid_from_string(char *str);
  */
 char* asn1_oid_to_string(chunk_t oid);
 
+/**
+ * Converts a known OID index to a human readable string.
+ *
+ * @param oid          known OID index
+ * @return                     human readable OID string, allocated, NULL on error
+ */
+char* asn1_known_oid_to_string(int oid);
+
 /**
  * Returns the length of an ASN.1 object
  * The blob pointer is advanced past the tag length fields