Yate
ASNLib Class Reference

Class containing functions for decoding/encoding ASN.1 basic data types. More...

#include <yateasn.h>

Public Types

enum  TypeTag {
  UNIVERSAL = 0x00 , BOOLEAN = 0x01 , INTEGER = 0x02 , BIT_STRING = 0x03 ,
  OCTET_STRING = 0x04 , NULL_ID = 0x05 , OBJECT_ID = 0x06 , REAL = 0x09 ,
  UTF8_STR = 0x0c , SEQUENCE = 0x30 , SET = 0x31 , NUMERIC_STR = 0x12 ,
  PRINTABLE_STR = 0x13 , IA5_STR = 0x16 , UTC_TIME = 0x17 , GENERALIZED_TIME = 0x18 ,
  VISIBLE_STR = 0x1a , GENERAL_STR = 0x1b , UNIVERSAL_STR = 0x1c , CHARACTER_STR = 0x1d ,
  BMP_STR = 0x1e , CHOICE = 0x1f , DEFINED = 0x2d
}
 
enum  Error {
  InvalidLengthOrTag = -1 , ConstraintBreakError = -2 , ParseError = -3 , InvalidContentsError = -4 ,
  IndefiniteForm = -5
}
 

Public Member Functions

 ASNLib ()
 
 ~ASNLib ()
 

Static Public Member Functions

static int decodeLength (DataBlock &data)
 
static int decodeBoolean (DataBlock &data, bool *val, bool tagCheck)
 
static int decodeInteger (DataBlock &data, u_int64_t &intVal, unsigned int bytes, bool tagCheck)
 
static int decodeUINT8 (DataBlock &data, u_int8_t *intVal, bool tagCheck)
 
static int decodeUINT16 (DataBlock &data, u_int16_t *intVal, bool tagCheck)
 
static int decodeUINT32 (DataBlock &data, u_int32_t *intVal, bool tagCheck)
 
static int decodeUINT64 (DataBlock &data, u_int64_t *intVal, bool tagCheck)
 
static int decodeINT8 (DataBlock &data, int8_t *intVal, bool tagCheck)
 
static int decodeINT16 (DataBlock &data, int16_t *intVal, bool tagCheck)
 
static int decodeINT32 (DataBlock &data, int32_t *intVal, bool tagCheck)
 
static int decodeINT64 (DataBlock &data, int64_t *intVal, bool tagCheck)
 
static int decodeBitString (DataBlock &data, String *val, bool tagCheck)
 
static int decodeOctetString (DataBlock &data, OctetString *strVal, bool tagCheck)
 
static int decodeNull (DataBlock &data, bool tagCheck)
 
static int decodeOID (DataBlock &data, ASNObjId *obj, bool tagCheck)
 
static int decodeReal (DataBlock &data, float *realVal, bool tagCheck)
 
static int decodeString (DataBlock &data, String *str, int *type, bool tagCheck)
 
static int decodeUtf8 (DataBlock &data, String *str, bool tagCheck)
 
static int decodeGenTime (DataBlock &data, unsigned int *time, unsigned int *fractions, bool *utc, bool tagCheck)
 
static int decodeUTCTime (DataBlock &data, unsigned int *time, bool tagCheck)
 
static int decodeAny (DataBlock data, DataBlock *val, bool tagCheck)
 
static int decodeSequence (DataBlock &data, bool tagCheck)
 
static int decodeSet (DataBlock &data, bool tagCheck)
 
static DataBlock buildLength (DataBlock &data)
 
static DataBlock encodeBoolean (bool val, bool tagCheck)
 
static DataBlock encodeInteger (u_int64_t intVal, bool tagCheck)
 
static DataBlock encodeOctetString (OctetString strVal, bool tagCheck)
 
static DataBlock encodeNull (bool tagCheck)
 
static DataBlock encodeBitString (String val, bool tagCheck)
 
static DataBlock encodeOID (ASNObjId obj, bool tagCheck)
 
static DataBlock encodeReal (float val, bool tagCheck)
 
static DataBlock encodeString (String str, int type, bool tagCheck)
 
static DataBlock encodeUtf8 (String str, bool tagCheck)
 
static DataBlock encodeGenTime (unsigned int time, unsigned int fractions, bool tagCheck)
 
static DataBlock encodeUTCTime (unsigned int time, bool tagCheck)
 
static DataBlock encodeAny (DataBlock data, bool tagCheck)
 
static int encodeSequence (DataBlock &data, bool tagCheck)
 
static int encodeSet (DataBlock &data, bool tagCheck)
 
static int matchEOC (DataBlock &data)
 
static int parseUntilEoC (DataBlock &data, int length=0)
 

Detailed Description

Class containing functions for decoding/encoding ASN.1 basic data types.

Class ASNLib

Member Enumeration Documentation

◆ Error

enum Error

Error types

◆ TypeTag

enum TypeTag

ASN.1 Type tags

Constructor & Destructor Documentation

◆ ASNLib()

ASNLib ( )

Constructor

◆ ~ASNLib()

~ASNLib ( )

Destructor

Member Function Documentation

◆ buildLength()

static DataBlock buildLength ( DataBlock & data)
static

Encode the length of the given data

Parameters
dataThe data for which the length should be encoded
Returns
The data block which now contains the length encoding

◆ decodeAny()

static int decodeAny ( DataBlock data,
DataBlock * val,
bool tagCheck )
static

Decode a block of arbitrary data

Parameters
dataInput block from which the value should be extracted
valDataBlock in which the data shoulb be copied
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeBitString()

static int decodeBitString ( DataBlock & data,
String * val,
bool tagCheck )
static

Decode a bitstring value from the encoded data

Parameters
dataInput block from which the bitstring value should be extracted
valString to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x03) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeBoolean()

static int decodeBoolean ( DataBlock & data,
bool * val,
bool tagCheck )
static

Decode a boolean value from the encoded data

Parameters
dataInput block from which the boolean value should be extracted
valPointer to a boolean to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for boolean (0x01) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the boolean value could not be decoded

◆ decodeGenTime()

static int decodeGenTime ( DataBlock & data,
unsigned int * time,
unsigned int * fractions,
bool * utc,
bool tagCheck )
static

Decode a GeneralizedTime value from the encoded data

Parameters
dataInput block from which the value should be extracted
timeInteger to be filled with time in seconds since epoch
fractionsInteger to be filled with fractions of a second
utcFlag indicating if the decode time value represent local time or UTC time
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag (0x18) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeINT16()

static int decodeINT16 ( DataBlock & data,
int16_t * intVal,
bool tagCheck )
static

Decode an integer value from the encoded data - helper function for casting from u_int64_t to int16_t in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeINT32()

static int decodeINT32 ( DataBlock & data,
int32_t * intVal,
bool tagCheck )
static

Decode an integer value from the encoded data - helper function for casting from u_int64_t to int32_t in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeINT64()

static int decodeINT64 ( DataBlock & data,
int64_t * intVal,
bool tagCheck )
static

Decode an integer value from the encoded data - helper function for casting in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeINT8()

static int decodeINT8 ( DataBlock & data,
int8_t * intVal,
bool tagCheck )
static

Decode an integer value from the encoded data - helper function for casting from u_int64_t to int8_t in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeInteger()

static int decodeInteger ( DataBlock & data,
u_int64_t & intVal,
unsigned int bytes,
bool tagCheck )
static

Decode an integer value from the encoded data

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
bytesWidth of the decoded integer field
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeLength()

static int decodeLength ( DataBlock & data)
static

Decode the length of the block data containing the ASN.1 type data

Parameters
dataInput block from which to extract the length
Returns
The length of the data block containing data, -1 if it couldn't be decoded

◆ decodeNull()

static int decodeNull ( DataBlock & data,
bool tagCheck )
static

Decode a null value from the encoded data

Parameters
dataInput block from which the null value should be extracted
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x05) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeOctetString()

static int decodeOctetString ( DataBlock & data,
OctetString * strVal,
bool tagCheck )
static

Decode a string value from the encoded data

Parameters
dataInput block from which the octet string value should be extracted
strValString to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x04) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeOID()

static int decodeOID ( DataBlock & data,
ASNObjId * obj,
bool tagCheck )
static

Decode an object id value from the encoded data

Parameters
dataInput block from which the OID value should be extracted
objASNObjId to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x06) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeReal()

static int decodeReal ( DataBlock & data,
float * realVal,
bool tagCheck )
static

Decode a real value from the encoded data - not implemented

Parameters
dataInput block from which the real value should be extracted
realValFloat to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x09) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeSequence()

static int decodeSequence ( DataBlock & data,
bool tagCheck )
static

Decode the header of an ASN.1 sequence ( decodes the tag and the length of the sequence)

Parameters
dataInput block from which the header should be extracted
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 (0x30) should be verified
Returns
Length of data consumed from the input data it the decoding was succesful, -1 if the integer value could not be decoded

◆ decodeSet()

static int decodeSet ( DataBlock & data,
bool tagCheck )
static

Decode the header of an ASN.1 set ( decodes the tag and the length of the sequence)

Parameters
dataInput block from which the header should be extracted
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 (0x31) should be verified
Returns
Length of data consumed from the input data it the decoding was succesful, -1 if the integer value could not be decoded

◆ decodeString()

static int decodeString ( DataBlock & data,
String * str,
int * type,
bool tagCheck )
static

Decode other types of ASN.1 strings from the encoded data (NumericString, PrintableString, VisibleString, IA5String)

Parameters
dataInput block from which the string value should be extracted
strString to be filled with the decoded value
typeInteger to be filled with the value indicating which type of string has been decoded
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeUINT16()

static int decodeUINT16 ( DataBlock & data,
u_int16_t * intVal,
bool tagCheck )
static

Decode an unsigned integer value from the encoded data - helper function for casting from u_int64_t to u_int16_t in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeUINT32()

static int decodeUINT32 ( DataBlock & data,
u_int32_t * intVal,
bool tagCheck )
static

Decode an unsigned integer value from the encoded data - helper function for casting from u_int64_t to u_int32_t in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeUINT64()

static int decodeUINT64 ( DataBlock & data,
u_int64_t * intVal,
bool tagCheck )
static

Decode an unsigned integer value from the encoded data - helper function for casting in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeUINT8()

static int decodeUINT8 ( DataBlock & data,
u_int8_t * intVal,
bool tagCheck )
static

Decode an unsigned integer value from the encoded data - helper function for casting from u_int64_t to u_int8_t in case of size constraints

Parameters
dataInput block from which the integer value should be extracted
intValInteger to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag for integer (0x02) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeUTCTime()

static int decodeUTCTime ( DataBlock & data,
unsigned int * time,
bool tagCheck )
static

Decode a UTC time value from the encoded data

Parameters
dataInput block from which the value should be extracted
timeInteger to be filled with time in seconds since epoch
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag (0x17) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ decodeUtf8()

static int decodeUtf8 ( DataBlock & data,
String * str,
bool tagCheck )
static

Decode an UTF8 string from the encoded data

Parameters
dataInput block from which the string value should be extracted
strString to be filled with the decoded value
tagCheckFlag for indicating if in the process of decoding the value the presence of the ASN.1 tag (0x0c) should be verified
Returns
Length of data consumed from the input data it the decoding was successful, -1 if the integer value could not be decoded

◆ encodeAny()

static DataBlock encodeAny ( DataBlock data,
bool tagCheck )
static

Encode an arbitrary block a data

Parameters
datadata
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeBitString()

static DataBlock encodeBitString ( String val,
bool tagCheck )
static

Encode the given bitstring value

Parameters
valThe bitstring value to encode
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeBoolean()

static DataBlock encodeBoolean ( bool val,
bool tagCheck )
static

Encode the given boolean value

Parameters
valThe boolean value to encode
tagCheckFlag to specify if the boolean type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeGenTime()

static DataBlock encodeGenTime ( unsigned int time,
unsigned int fractions,
bool tagCheck )
static

Encode the given time value into a GeneralizedTime format

Parameters
timeTime in seconds since epoch to encode
fractionsFractions of a seconds to encode
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeInteger()

static DataBlock encodeInteger ( u_int64_t intVal,
bool tagCheck )
static

Encode the given integer value

Parameters
intValThe integer value to encode
tagCheckFlag to specify if the integer type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeNull()

static DataBlock encodeNull ( bool tagCheck)
static

Encode a null value

Parameters
tagCheckFlag to specify if the null tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeOctetString()

static DataBlock encodeOctetString ( OctetString strVal,
bool tagCheck )
static

Encode the given octet string value

Parameters
strValThe octet string value to encode
tagCheckFlag to specify if the octet string type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeOID()

static DataBlock encodeOID ( ASNObjId obj,
bool tagCheck )
static

Encode the given OID value

Parameters
objThe OID value to encode
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeReal()

static DataBlock encodeReal ( float val,
bool tagCheck )
static

Encode the given real value - not implemented

Parameters
valThe real value to encode
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeSequence()

static int encodeSequence ( DataBlock & data,
bool tagCheck )
static

Encode the header for a sequence

Parameters
dataSequence data for which the header is encoded
tagCheckFlag to specify if the ype tag should be inserted in the encoding
Returns
The length of the data block length encoding

◆ encodeSet()

static int encodeSet ( DataBlock & data,
bool tagCheck )
static

Encode the header for a set

Parameters
dataSequence data for which the header is encoded
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The length of the data block length encoding

◆ encodeString()

static DataBlock encodeString ( String str,
int type,
bool tagCheck )
static

Encode the given string value to NumericString, PrintableString, IA5String, VisibleString

Parameters
strThe string value to encode
typeThe type of the encoding
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeUTCTime()

static DataBlock encodeUTCTime ( unsigned int time,
bool tagCheck )
static

Encode the given time value into an UTCTime format

Parameters
timeTime in seconds since epoch to encode
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ encodeUtf8()

static DataBlock encodeUtf8 ( String str,
bool tagCheck )
static

Encode the UTF8 string value

Parameters
strThe string value to encode
tagCheckFlag to specify if the type tag should be inserted in the encoding
Returns
The data block encoding of the value

◆ matchEOC()

static int matchEOC ( DataBlock & data)
static

Verify the data for End Of Contents presence

Parameters
dataInput block to verify
Returns
Length of data consumed from the input data it the decoding was succesful, it should be 2 in case of success, -1 if the data doesn't match EoC

◆ parseUntilEoC()

static int parseUntilEoC ( DataBlock & data,
int length = 0 )
static

Extract length until a End Of Contents is found.

Parameters
dataInput block for which to determine the length to End Of Contents
lengthLength to which to add determined length
Returns
Length until End Of Contents

The documentation for this class was generated from the following file: