Moved log message for unexpected ASN.1 objects to level 2.
[strongswan.git] / src / libstrongswan / asn1 / asn1_parser.c
index bc4c0b5..40e11b3 100644 (file)
@@ -19,7 +19,6 @@
 #include <string.h>
 #include <time.h>
 
 #include <string.h>
 #include <time.h>
 
-#include <utils.h>
 #include <debug.h>
 
 #include "asn1.h"
 #include <debug.h>
 
 #include "asn1.h"
@@ -54,7 +53,7 @@ struct private_asn1_parser_t {
        bool success;
 
        /**
        bool success;
 
        /**
-        * Declare object data as private - use debug level 4 to log it 
+        * Declare object data as private - use debug level 4 to log it
         */
        bool private;
 
         */
        bool private;
 
@@ -79,16 +78,14 @@ struct private_asn1_parser_t {
        chunk_t blobs[ASN1_MAX_LEVEL + 2];
 };
 
        chunk_t blobs[ASN1_MAX_LEVEL + 2];
 };
 
-/**
- * Implementation of asn1_parser_t.iterate
- */
-static bool iterate(private_asn1_parser_t *this, int *objectID, chunk_t *object)
+METHOD(asn1_parser_t, iterate, bool,
+       private_asn1_parser_t *this, int *objectID, chunk_t *object)
 {
        chunk_t *blob, *blob1;
        u_char *start_ptr;
        u_int level;
        asn1Object_t obj;
 {
        chunk_t *blob, *blob1;
        u_char *start_ptr;
        u_int level;
        asn1Object_t obj;
-       
+
        *object = chunk_empty;
 
        /* Advance to the next object syntax definition line */
        *object = chunk_empty;
 
        /* Advance to the next object syntax definition line */
@@ -99,7 +96,7 @@ static bool iterate(private_asn1_parser_t *this, int *objectID, chunk_t *object)
        {
                return FALSE;
        }
        {
                return FALSE;
        }
-               
+
        if (obj.flags & ASN1_END)  /* end of loop or option found */
        {
                if (this->loopAddr[obj.level] && this->blobs[obj.level+1].len > 0)
        if (obj.flags & ASN1_END)  /* end of loop or option found */
        {
                if (this->loopAddr[obj.level] && this->blobs[obj.level+1].len > 0)
@@ -109,30 +106,30 @@ static bool iterate(private_asn1_parser_t *this, int *objectID, chunk_t *object)
                }
                else
                {
                }
                else
                {
-                       this->loopAddr[obj.level] = 0;         /* exit loop or option*/
+                       this->loopAddr[obj.level] = 0;           /* exit loop or option*/
                        goto end;
                }
        }
                        goto end;
                }
        }
-       
+
        level = this->level0 + obj.level;
        blob = this->blobs + obj.level;
        blob1 = blob + 1;
        start_ptr = blob->ptr;
        level = this->level0 + obj.level;
        blob = this->blobs + obj.level;
        blob1 = blob + 1;
        start_ptr = blob->ptr;
-       
+
        /* handle ASN.1 defaults values */
        if ((obj.flags & ASN1_DEF) && (blob->len == 0 || *start_ptr != obj.type) )
        {
                /* field is missing */
        /* handle ASN.1 defaults values */
        if ((obj.flags & ASN1_DEF) && (blob->len == 0 || *start_ptr != obj.type) )
        {
                /* field is missing */
-               DBG2("L%d - %s:", level, obj.name);
+               DBG2(DBG_ASN, "L%d - %s:", level, obj.name);
                if (obj.type & ASN1_CONSTRUCTED)
                {
                        this->line++ ;  /* skip context-specific tag */
                }
                goto end;
        }
                if (obj.type & ASN1_CONSTRUCTED)
                {
                        this->line++ ;  /* skip context-specific tag */
                }
                goto end;
        }
-       
+
        /* handle ASN.1 options */
        /* handle ASN.1 options */
-       
+
        if ((obj.flags & ASN1_OPT)
                        && (blob->len == 0 || *start_ptr != obj.type))
        {
        if ((obj.flags & ASN1_OPT)
                        && (blob->len == 0 || *start_ptr != obj.type))
        {
@@ -145,35 +142,35 @@ static bool iterate(private_asn1_parser_t *this, int *objectID, chunk_t *object)
                                 (this->objects[this->line].level == obj.level)));
                goto end;
        }
                                 (this->objects[this->line].level == obj.level)));
                goto end;
        }
-               
+
        /* an ASN.1 object must possess at least a tag and length field */
        /* an ASN.1 object must possess at least a tag and length field */
-       
+
        if (blob->len < 2)
        {
        if (blob->len < 2)
        {
-               DBG1("L%d - %s:  ASN.1 object smaller than 2 octets",
+               DBG1(DBG_ASN, "L%d - %s:  ASN.1 object smaller than 2 octets",
                                        level, obj.name);
                this->success = FALSE;
                goto end;
        }
                                        level, obj.name);
                this->success = FALSE;
                goto end;
        }
-       
+
        blob1->len = asn1_length(blob);
        blob1->len = asn1_length(blob);
-       
+
        if (blob1->len == ASN1_INVALID_LENGTH)
        {
        if (blob1->len == ASN1_INVALID_LENGTH)
        {
-               DBG1("L%d - %s:  length of ASN.1 object invalid or too large", 
+               DBG1(DBG_ASN, "L%d - %s:  length of ASN.1 object invalid or too large",
                                        level, obj.name);
                this->success = FALSE;
        }
                                        level, obj.name);
                this->success = FALSE;
        }
-       
+
        blob1->ptr = blob->ptr;
        blob->ptr += blob1->len;
        blob->len -= blob1->len;
        blob1->ptr = blob->ptr;
        blob->ptr += blob1->len;
        blob->len -= blob1->len;
-       
+
        /* return raw ASN.1 object without prior type checking */
        /* return raw ASN.1 object without prior type checking */
-       
+
        if (obj.flags & ASN1_RAW)
        {
        if (obj.flags & ASN1_RAW)
        {
-               DBG2("L%d - %s:", level, obj.name);
+               DBG2(DBG_ASN, "L%d - %s:", level, obj.name);
                object->ptr = start_ptr;
                object->len = (size_t)(blob->ptr - start_ptr);
                goto end;
                object->ptr = start_ptr;
                object->len = (size_t)(blob->ptr - start_ptr);
                goto end;
@@ -181,16 +178,16 @@ static bool iterate(private_asn1_parser_t *this, int *objectID, chunk_t *object)
 
        if (*start_ptr != obj.type && !(this->implicit && this->line == 0))
        {
 
        if (*start_ptr != obj.type && !(this->implicit && this->line == 0))
        {
-               DBG1("L%d - %s: ASN1 tag 0x%02x expected, but is 0x%02x",
+               DBG2(DBG_ASN, "L%d - %s: ASN1 tag 0x%02x expected, but is 0x%02x",
                                        level, obj.name, obj.type, *start_ptr);
                                        level, obj.name, obj.type, *start_ptr);
-               DBG3("%b", start_ptr, (u_int)(blob->ptr - start_ptr));
+               DBG3(DBG_ASN, "%b", start_ptr, (u_int)(blob->ptr - start_ptr));
                this->success = FALSE;
                goto end;
        }
                this->success = FALSE;
                goto end;
        }
-       
-       DBG2("L%d - %s:", level, obj.name);
-       
-       /* In case of "SEQUENCE OF" or "SET OF" start a loop */ 
+
+       DBG2(DBG_ASN, "L%d - %s:", level, obj.name);
+
+       /* In case of "SEQUENCE OF" or "SET OF" start a loop */
        if (obj.flags & ASN1_LOOP)
        {
                if (blob1->len > 0)
        if (obj.flags & ASN1_LOOP)
        {
                if (blob1->len > 0)
@@ -217,11 +214,11 @@ static bool iterate(private_asn1_parser_t *this, int *objectID, chunk_t *object)
                object->len = (size_t)(blob->ptr - start_ptr);
                if (this->private)
                {
                object->len = (size_t)(blob->ptr - start_ptr);
                if (this->private)
                {
-                       DBG4("%B", object);
+                       DBG4(DBG_ASN, "%B", object);
                }
                else
                {
                }
                else
                {
-                       DBG3("%B", object);
+                       DBG3(DBG_ASN, "%B", object);
                }
        }
        else if (obj.flags & ASN1_BODY)
                }
        }
        else if (obj.flags & ASN1_BODY)
@@ -235,43 +232,33 @@ end:
        return this->success;
 }
 
        return this->success;
 }
 
-/**
- * Implementation of asn1_parser_t.get_level
- */
-static u_int get_level(private_asn1_parser_t *this)
+METHOD(asn1_parser_t, get_level, u_int,
+private_asn1_parser_t *this)
 {
        return this->level0 + this->objects[this->line].level;
 }
 
 {
        return this->level0 + this->objects[this->line].level;
 }
 
-/**
- * Implementation of asn1_parser_t.set_top_level
- */
-static void set_top_level(private_asn1_parser_t *this, u_int level0)
+METHOD(asn1_parser_t, set_top_level, void,
+       private_asn1_parser_t *this, u_int level0)
 {
        this->level0 = level0;
 }
 
 {
        this->level0 = level0;
 }
 
-/**
- * Implementation of asn1_parser_t.set_flags
- */
-static void set_flags(private_asn1_parser_t *this, bool implicit, bool private)
+METHOD(asn1_parser_t, set_flags, void,
+       private_asn1_parser_t *this, bool implicit, bool private)
 {
        this->implicit = implicit;
        this->private = private;
 }
 
 {
        this->implicit = implicit;
        this->private = private;
 }
 
-/**
- * Implementation of asn1_parser_t.success
- */
-static bool success(private_asn1_parser_t *this)
+METHOD(asn1_parser_t, success, bool,
+       private_asn1_parser_t *this)
 {
        return this->success;
 }
 
 {
        return this->success;
 }
 
-/**
- * Implementation of asn1_parser_t.destroy
- */
-static void destroy(private_asn1_parser_t *this)
+METHOD(asn1_parser_t, destroy, void,
+       private_asn1_parser_t *this)
 {
        free(this);
 }
 {
        free(this);
 }
@@ -281,20 +268,22 @@ static void destroy(private_asn1_parser_t *this)
  */
 asn1_parser_t* asn1_parser_create(asn1Object_t const *objects, chunk_t blob)
 {
  */
 asn1_parser_t* asn1_parser_create(asn1Object_t const *objects, chunk_t blob)
 {
-       private_asn1_parser_t *this = malloc_thing(private_asn1_parser_t);
-
-       memset(this, '\0', sizeof(private_asn1_parser_t));
-       this->objects = objects;
-       this->blobs[0] = blob;
-       this->line = -1;
-       this->success = TRUE;
-
-       this->public.iterate = (bool (*)(asn1_parser_t*, int*, chunk_t*))iterate;
-       this->public.get_level = (u_int (*)(asn1_parser_t*))get_level;
-       this->public.set_top_level = (void (*)(asn1_parser_t*, u_int))set_top_level;
-       this->public.set_flags = (void (*)(asn1_parser_t*, bool, bool))set_flags;
-       this->public.success = (bool (*)(asn1_parser_t*))success;
-       this->public.destroy = (void (*)(asn1_parser_t*))destroy;
+       private_asn1_parser_t *this;
+
+       INIT(this,
+               .public = {
+                       .iterate = _iterate,
+                       .get_level = _get_level,
+                       .set_top_level = _set_top_level,
+                       .set_flags = _set_flags,
+                       .success = _success,
+                       .destroy = _destroy,
+               },
+               .objects = objects,
+               .blobs[0] = blob,
+               .line = -1,
+               .success = TRUE,
+       );
 
        return &this->public;
 }
 
        return &this->public;
 }