ASN.1 (Abstract Syntax Notation One) DER (Distinguished Encoding Rules) is a standard-backed serialization scheme used in many different use-cases. Particularly popular usage scenarios are X.509 certificates and some telecommunication / networking protocols.
DER is self-describing encoding scheme which allows representation of simple, atomic data elements, such as strings and numbers, and complex objects, such as sequences of other elements.
DER is a subset of BER (Basic Encoding Rules), with an emphasis on being non-ambiguous: there's always exactly one canonical way to encode a data structure defined in terms of ASN.1 using DER.
This spec allows full parsing of format syntax, but to understand the semantics, one would typically require a dictionary of Object Identifiers (OIDs), to match OID bodies against some human-readable list of constants. OIDs are covered by many different standards, so typically it's simpler to use a pre-compiled list of them, such as:
This page hosts a formal specification of ASN.1 DER (Abstract Syntax Notation One, Distinguished Encoding Rules) using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.
All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.
For C++, the easiest way is to clone the runtime library sources and build them along with your project.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
std::istream
). One can open local file for that, or use existing std::string
or char*
buffer.
#include <fstream>
std::ifstream is("path/to/local/file.der", std::ifstream::binary);
#include <sstream>
std::istringstream is(str);
#include <sstream>
const char buf[] = { ... };
std::string str(buf, sizeof buf);
std::istringstream is(str);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
asn1_der_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.type_tag() // => get type tag
#ifndef ASN1_DER_H_
#define ASN1_DER_H_
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>
#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif
/**
* ASN.1 (Abstract Syntax Notation One) DER (Distinguished Encoding
* Rules) is a standard-backed serialization scheme used in many
* different use-cases. Particularly popular usage scenarios are X.509
* certificates and some telecommunication / networking protocols.
*
* DER is self-describing encoding scheme which allows representation
* of simple, atomic data elements, such as strings and numbers, and
* complex objects, such as sequences of other elements.
*
* DER is a subset of BER (Basic Encoding Rules), with an emphasis on
* being non-ambiguous: there's always exactly one canonical way to
* encode a data structure defined in terms of ASN.1 using DER.
*
* This spec allows full parsing of format syntax, but to understand
* the semantics, one would typically require a dictionary of Object
* Identifiers (OIDs), to match OID bodies against some human-readable
* list of constants. OIDs are covered by many different standards,
* so typically it's simpler to use a pre-compiled list of them, such
* as:
*
* * <https://www.cs.auckland.ac.nz/~pgut001/dumpasn1.cfg>
* * <http://oid-info.com/>
* * <https://www.alvestrand.no/objectid/top.html>
* \sa https://www.itu.int/itu-t/recommendations/rec.aspx?rec=12483&lang=en Source
*/
class asn1_der_t : public kaitai::kstruct {
public:
class body_sequence_t;
class body_utf8string_t;
class body_object_id_t;
class len_encoded_t;
class body_printable_string_t;
enum type_tag_t {
TYPE_TAG_END_OF_CONTENT = 0,
TYPE_TAG_BOOLEAN = 1,
TYPE_TAG_INTEGER = 2,
TYPE_TAG_BIT_STRING = 3,
TYPE_TAG_OCTET_STRING = 4,
TYPE_TAG_NULL_VALUE = 5,
TYPE_TAG_OBJECT_ID = 6,
TYPE_TAG_OBJECT_DESCRIPTOR = 7,
TYPE_TAG_EXTERNAL = 8,
TYPE_TAG_REAL = 9,
TYPE_TAG_ENUMERATED = 10,
TYPE_TAG_EMBEDDED_PDV = 11,
TYPE_TAG_UTF8STRING = 12,
TYPE_TAG_RELATIVE_OID = 13,
TYPE_TAG_SEQUENCE_10 = 16,
TYPE_TAG_PRINTABLE_STRING = 19,
TYPE_TAG_IA5STRING = 22,
TYPE_TAG_SEQUENCE_30 = 48,
TYPE_TAG_SET = 49
};
asn1_der_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, asn1_der_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~asn1_der_t();
class body_sequence_t : public kaitai::kstruct {
public:
body_sequence_t(kaitai::kstream* p__io, asn1_der_t* p__parent = 0, asn1_der_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~body_sequence_t();
private:
std::vector<asn1_der_t*>* m_entries;
asn1_der_t* m__root;
asn1_der_t* m__parent;
public:
std::vector<asn1_der_t*>* entries() const { return m_entries; }
asn1_der_t* _root() const { return m__root; }
asn1_der_t* _parent() const { return m__parent; }
};
class body_utf8string_t : public kaitai::kstruct {
public:
body_utf8string_t(kaitai::kstream* p__io, asn1_der_t* p__parent = 0, asn1_der_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~body_utf8string_t();
private:
std::string m_str;
asn1_der_t* m__root;
asn1_der_t* m__parent;
public:
std::string str() const { return m_str; }
asn1_der_t* _root() const { return m__root; }
asn1_der_t* _parent() const { return m__parent; }
};
/**
* \sa https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-object-identifier Source
*/
class body_object_id_t : public kaitai::kstruct {
public:
body_object_id_t(kaitai::kstream* p__io, asn1_der_t* p__parent = 0, asn1_der_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~body_object_id_t();
private:
bool f_first;
int32_t m_first;
public:
int32_t first();
private:
bool f_second;
int32_t m_second;
public:
int32_t second();
private:
uint8_t m_first_and_second;
std::string m_rest;
asn1_der_t* m__root;
asn1_der_t* m__parent;
public:
uint8_t first_and_second() const { return m_first_and_second; }
std::string rest() const { return m_rest; }
asn1_der_t* _root() const { return m__root; }
asn1_der_t* _parent() const { return m__parent; }
};
class len_encoded_t : public kaitai::kstruct {
public:
len_encoded_t(kaitai::kstream* p__io, asn1_der_t* p__parent = 0, asn1_der_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~len_encoded_t();
private:
bool f_result;
uint16_t m_result;
public:
uint16_t result();
private:
uint8_t m_b1;
uint16_t m_int2;
bool n_int2;
public:
bool _is_null_int2() { int2(); return n_int2; };
private:
uint8_t m_int1;
bool n_int1;
public:
bool _is_null_int1() { int1(); return n_int1; };
private:
asn1_der_t* m__root;
asn1_der_t* m__parent;
public:
uint8_t b1() const { return m_b1; }
uint16_t int2() const { return m_int2; }
uint8_t int1() const { return m_int1; }
asn1_der_t* _root() const { return m__root; }
asn1_der_t* _parent() const { return m__parent; }
};
class body_printable_string_t : public kaitai::kstruct {
public:
body_printable_string_t(kaitai::kstream* p__io, asn1_der_t* p__parent = 0, asn1_der_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~body_printable_string_t();
private:
std::string m_str;
asn1_der_t* m__root;
asn1_der_t* m__parent;
public:
std::string str() const { return m_str; }
asn1_der_t* _root() const { return m__root; }
asn1_der_t* _parent() const { return m__parent; }
};
private:
type_tag_t m_type_tag;
len_encoded_t* m_len;
kaitai::kstruct* m_body;
bool n_body;
public:
bool _is_null_body() { body(); return n_body; };
private:
asn1_der_t* m__root;
kaitai::kstruct* m__parent;
std::string m__raw_body;
kaitai::kstream* m__io__raw_body;
public:
type_tag_t type_tag() const { return m_type_tag; }
len_encoded_t* len() const { return m_len; }
kaitai::kstruct* body() const { return m_body; }
asn1_der_t* _root() const { return m__root; }
kaitai::kstruct* _parent() const { return m__parent; }
std::string _raw_body() const { return m__raw_body; }
kaitai::kstream* _io__raw_body() const { return m__io__raw_body; }
};
#endif // ASN1_DER_H_
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "asn1_der.h"
asn1_der_t::asn1_der_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, asn1_der_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = this;
m_len = 0;
m__io__raw_body = 0;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void asn1_der_t::_read() {
m_type_tag = static_cast<asn1_der_t::type_tag_t>(m__io->read_u1());
m_len = new len_encoded_t(m__io, this, m__root);
n_body = true;
switch (type_tag()) {
case asn1_der_t::TYPE_TAG_PRINTABLE_STRING: {
n_body = false;
m__raw_body = m__io->read_bytes(len()->result());
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new body_printable_string_t(m__io__raw_body, this, m__root);
break;
}
case asn1_der_t::TYPE_TAG_SEQUENCE_10: {
n_body = false;
m__raw_body = m__io->read_bytes(len()->result());
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new body_sequence_t(m__io__raw_body, this, m__root);
break;
}
case asn1_der_t::TYPE_TAG_SET: {
n_body = false;
m__raw_body = m__io->read_bytes(len()->result());
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new body_sequence_t(m__io__raw_body, this, m__root);
break;
}
case asn1_der_t::TYPE_TAG_SEQUENCE_30: {
n_body = false;
m__raw_body = m__io->read_bytes(len()->result());
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new body_sequence_t(m__io__raw_body, this, m__root);
break;
}
case asn1_der_t::TYPE_TAG_UTF8STRING: {
n_body = false;
m__raw_body = m__io->read_bytes(len()->result());
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new body_utf8string_t(m__io__raw_body, this, m__root);
break;
}
case asn1_der_t::TYPE_TAG_OBJECT_ID: {
n_body = false;
m__raw_body = m__io->read_bytes(len()->result());
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new body_object_id_t(m__io__raw_body, this, m__root);
break;
}
default: {
m__raw_body = m__io->read_bytes(len()->result());
break;
}
}
}
asn1_der_t::~asn1_der_t() {
_clean_up();
}
void asn1_der_t::_clean_up() {
if (m_len) {
delete m_len; m_len = 0;
}
if (!n_body) {
if (m__io__raw_body) {
delete m__io__raw_body; m__io__raw_body = 0;
}
if (m_body) {
delete m_body; m_body = 0;
}
}
}
asn1_der_t::body_sequence_t::body_sequence_t(kaitai::kstream* p__io, asn1_der_t* p__parent, asn1_der_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
m_entries = 0;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void asn1_der_t::body_sequence_t::_read() {
m_entries = new std::vector<asn1_der_t*>();
{
int i = 0;
while (!m__io->is_eof()) {
m_entries->push_back(new asn1_der_t(m__io));
i++;
}
}
}
asn1_der_t::body_sequence_t::~body_sequence_t() {
_clean_up();
}
void asn1_der_t::body_sequence_t::_clean_up() {
if (m_entries) {
for (std::vector<asn1_der_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
delete *it;
}
delete m_entries; m_entries = 0;
}
}
asn1_der_t::body_utf8string_t::body_utf8string_t(kaitai::kstream* p__io, asn1_der_t* p__parent, asn1_der_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void asn1_der_t::body_utf8string_t::_read() {
m_str = kaitai::kstream::bytes_to_str(m__io->read_bytes_full(), std::string("UTF-8"));
}
asn1_der_t::body_utf8string_t::~body_utf8string_t() {
_clean_up();
}
void asn1_der_t::body_utf8string_t::_clean_up() {
}
asn1_der_t::body_object_id_t::body_object_id_t(kaitai::kstream* p__io, asn1_der_t* p__parent, asn1_der_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
f_first = false;
f_second = false;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void asn1_der_t::body_object_id_t::_read() {
m_first_and_second = m__io->read_u1();
m_rest = m__io->read_bytes_full();
}
asn1_der_t::body_object_id_t::~body_object_id_t() {
_clean_up();
}
void asn1_der_t::body_object_id_t::_clean_up() {
}
int32_t asn1_der_t::body_object_id_t::first() {
if (f_first)
return m_first;
m_first = (first_and_second() / 40);
f_first = true;
return m_first;
}
int32_t asn1_der_t::body_object_id_t::second() {
if (f_second)
return m_second;
m_second = kaitai::kstream::mod(first_and_second(), 40);
f_second = true;
return m_second;
}
asn1_der_t::len_encoded_t::len_encoded_t(kaitai::kstream* p__io, asn1_der_t* p__parent, asn1_der_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
f_result = false;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void asn1_der_t::len_encoded_t::_read() {
m_b1 = m__io->read_u1();
n_int2 = true;
if (b1() == 130) {
n_int2 = false;
m_int2 = m__io->read_u2be();
}
n_int1 = true;
if (b1() == 129) {
n_int1 = false;
m_int1 = m__io->read_u1();
}
}
asn1_der_t::len_encoded_t::~len_encoded_t() {
_clean_up();
}
void asn1_der_t::len_encoded_t::_clean_up() {
if (!n_int2) {
}
if (!n_int1) {
}
}
uint16_t asn1_der_t::len_encoded_t::result() {
if (f_result)
return m_result;
m_result = ((b1() == 129) ? (int1()) : (((b1() == 130) ? (int2()) : (b1()))));
f_result = true;
return m_result;
}
asn1_der_t::body_printable_string_t::body_printable_string_t(kaitai::kstream* p__io, asn1_der_t* p__parent, asn1_der_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void asn1_der_t::body_printable_string_t::_read() {
m_str = kaitai::kstream::bytes_to_str(m__io->read_bytes_full(), std::string("ASCII"));
}
asn1_der_t::body_printable_string_t::~body_printable_string_t() {
_clean_up();
}
void asn1_der_t::body_printable_string_t::_clean_up() {
}