LibJAD ASN1 Routines

#include "asn1.h"

Overview

The ASN1 routines provide very basic support for parsing and printing asn1 data files. More information about asn1 can be found at Wikipedia. In brief, ASN1 is a simple binary format for data, with each field being composed of a type and size data in addition to the actual data.

Data Structures

ASN1DATA

unsigned char typeThe asn1 type of this field.
size_t lengthLength of the data buffer.
unsigned char* dataData for this field.
struct ASN1DATA** partArray of subfields.

typedef struct ASN1DATA* asn1

The asn1 data type is a pointer type, pointing to a structure which describes an ASN1 data object. The type field, an unsigned char, contains the asn1 type. The length field contains the length of the data buffer stored in data. In the event that node is not a primitive type, the part field is a null terminated array of asn1 objects. These objects represent the fields of the composite type.

Functions

asn1* asn1_parse_data(unsigned char* buf, size_t len)

bufBuffer of asn1 data to parse.
lenLength of the buffer to parse.
returnAn array of asn1 structures.

Given a data buffer and the length of the buffer this routine will parse the buffer as an ASN1 data structure and return a null terminated array of asn1 data structures containing the information. Each element of the array contains one top level ASN1 data item. Note that this implemenation is not very efficient. A component type node contains both a buffer containing its complete payload, and a pointer to a set of child nodes containing a parsed version of this information.

void asn1_print_node(asn1 data, int depth)

dataAn asn1 node.
lenThe depth of this data in tree.

This routine will print an asn1 node and its meta information such as its type and length. If the node in question is a constructed type this function will recursively call itself to print out the component parts. The depth parameter is used to control level of indenting when printing out the parts of a constructed types. In most cases it should be set to 0, indicating a top level node.

asn1 allocated_asn1(size_t length)

lengthLength of the asn1 data to be held.
returnAn asn1 structure that will hold length bytes.

Allocates a new asn1 data structure. This function allocates sizeof(ASN1DATA+length) bytes. The asn1 data object returned points to the the start of this buffer, and the data field of this structure is pointed to the bytes following the asn1 data structure. The length field is set appropriate, but type is not set.

void asn1_free_tree(asn1* tree)

treeAn array of asn1 nodes.

This method frees a null terminiated array of asn1 data structures. For nodes of a component type, all sub-nodes are deleted in a recursive fashion.