Basic Encoding Rules (BER)

Welcome to the BER library documentation. This is a simple library for parsing data encoed using the Basic Encoding Rules (BER) of ASN.1. Since the Distinguished Encoding Rules (DER) is a restricted subset of BER, it should also work on DER-encoded data.

Example

This is a basic example of how to use it:

FILE* fp = fopen("myfile.ber", "rb");
struct ber_element element;
struct ber_event event;
struct ber_parser* parser = ber_parser_new();

while (ber_parser_parser(&event, &element, parser) == 0 && event != BER_NO_EVENT) {
  printf("Tag: %d\n", element.tag);

  printf("Data: ");
  for (int i = 0; i < element.length; i++) {
    printf("%02x", element.data[i]);
  }
  printf("\n");
}

API

enum ber_event
enumerator BER_NO_EVENT
enumerator BER_PRIMITIVE_EVENT
enumerator BER_CONSTRUCTED_EVENT
enumerator BER_EOC_EVENT
enum ber_tag_class
enumerator BER_TAG_CLASS_UNIVERSAL
enumerator BER_TAG_CLASS_APPLICATION
enumerator BER_TAG_CLASS_CONTEXT_SPECIFIC
enumerator BER_TAG_CLASS_PRIVATE
struct ber_element
enum tag_class tag_class

The class of the tag

int constructed

Whether the element is constructed

size_t tag

The tag of the element

int definite

Whether the element has a definite (has a pre-defined length)

size_t length

The length of the value

char *value

The value encoded

struct ber_parser

A BER parser object that holds the state of the parsing.

const char *ber_get_version()

Gets the version string

Returns

A pointer to the static version string

struct ber_parser *ber_parser_new()

Creates a new BER parser.

Returns

An instance of ber_parser

int ber_parser_parse(enum ber_event *event, ber_element *element, struct ber_parser *parser)
Returns

0 on success, or -1 on error

Warning

This is not thread-safe.

void ber_parser_free(struct ber_parser *parser)

Frees the parsero bject