Skip to content
/ bertlv Public

Golang implementation of parsing and building BER-TLV.

License

Notifications You must be signed in to change notification settings

skythen/bertlv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BER-TLV

Build Status Coverage Status GoDoc Go Report Card

Package bertlv implements parsing and building of BER-TLV structures.

Please note that this is not a complete implementation of the X.690 standard as it is agnostic about classes (Universal, Application, Context-specific, Private) and therefore does not check for correct encoding of tags/values.

go get github.com/skythen/bertlv

Parse

You can parse BER-TLV encoded data from bytes:

b := []byte{0x71, 0x10, 0xB0, 0x0E, 0x0F, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0E, 0x05, 0x05, 0x04, 0x03, 0x02, 0x01}
bertlvs, err := Parse(b)

Constructed objects

You can check if a BerTLV is constructed and get first or all children or filter child objects by tag:

if bertlvs[0].Tag.IsConstructed() {
    child := bertlvs[0].FirstChild(NewOneByteTag(0x0F))
}

Create

You can create single BER-TLVs with NewBerTLV:

val := []byte{0xB0, 0x0E, 0x0F, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0E, 0x05, 0x05, 0x04, 0x03, 0x02, 0x01}
bertlv, err := NewBerTLV(NewOneByteTag(0x71), val)

If you want to create complex constructed objects you use the Builder:

builder := Builder{}
nestedBuilder := Builder{}
anotherNestedBuilder := Builder{}

berTlvs, err := builder.AddBytes(NewOneByteTag(0x71), 			     // first level, constructed object
    nestedBuilder.AddBytes(NewOneByteTag(0xB0), 		             // second level, constructed object
        anotherNestedBuilder.
            AddBytes(NewOneByteTag(0x0F), []byte{0x01, 0x02, 0x03, 0x04, 0x05}). // third level primitive object
            AddBytes(NewOneByteTag(0x0E), []byte{0x05, 0x04, 0x03, 0x02, 0x01}). // third level primitive object
        Bytes()).
    Bytes()).
BuildBerTLVs()

You can also use

  • builder.AddEmpty() to add objects without value
  • builder.AddByte() to add objects with a value that consists of a single byte
  • builder.AddRaw() to add raw bytes