Terminology

ASN.1 : Abstract Syntax Notation 1. For some information about ASN.1 and Apache Directory Server, see: ASN.1 Codec

BER : Basic Encoding Rules.

FSM : Finite State Machine

PDU : Protocol Data Unit. An array of byte representing the encoded data, using one specific encoding (BER, PER, ...)

TLV : Abreviation for Type/Length/Value. The Length represent the number of bytes necessary to represent the Value. It can be 0. Not all encoding rules use TLV. BER does.

PDU

A BER encoded PDU is a byte array, which contains elements called TLV.

Checking a PDU 

A PDU is considered as correct if it met the two conditions:

  1. The ASN.1 grammar definition is respected
  2. The Length of each TLV is correct

The first condition is checked with a state machine. The second condition is checked on the fly, accordingly to these rules:

  1. the TLV is terminal (ie : the Value is not itslef another TLV): the L must be equals to zero or to the V length
  2. the TLV is not terminal : the L should be either zero with no V, or the L must be equals to V length, which should be computed recusively

We are using the following algorithm to control that the TLV lengths are correct :

checkTLV(<T, L, V>) --> boolean

==>

if checkTLVLength(<T,L,V) == length(<T>) + L
  then return true
  else return false

checkTLVLength(<T, L, V>) --> int 

==>

if <T> is terminal
  then
    if <L> == 0
      then return 1 + length(<T>)
      else
        if <L> == length(<V>)
          then return <L> + length(<L>) + length(<T>)
          else error
  else
    if checkTLVList( <V> ) == <L>
      then return <L> + length(<L>) + length(<T>)
      else error

checkTLVList( <TLV>*) --> int 

==>

if <TLV> is empty
  then return 0
  else return checkTLVLength( first( <TLV> ) ) + checkTLVList( last( <TLV> ) )

Decoding PDU

To decode a PDU, we will use a FSM, which seems te be the best soltuion as we have a perfectly deterministic way to decode the bytes we have received, and because it allows us to use a statefull decoder. (Notions about using a FSM to decode an ASN.1 encoded PDU are given in the following page: Finite State Machine for an ASN.1 codec

  • No labels