Skip to content

Latest commit

 

History

97 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

asn1-ber

Go Reference PR Lint

ASN.1 BER encoding and decoding for Go, with no external dependencies. This is the BER layer used by go-ldap; it implements the subset of BER/DER needed for LDAP (RFC 4511), including integers, booleans, strings, object identifiers, REAL numbers, and generalized time.

Install

go get github.com/go-asn1-ber/asn1-ber

Requires Go 1.22 or newer.

Usage

package main

import (
	"fmt"

	ber "github.com/go-asn1-ber/asn1-ber"
)

func main() {
	// Encode a SEQUENCE containing a single OCTET STRING.
	seq := ber.NewSequence("greeting")
	seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "Hello, world", ""))

	// Serialize, then decode back into a packet tree.
	packet, err := ber.DecodePacketErr(seq.Bytes())
	if err != nil {
		panic(err)
	}

	fmt.Println(packet.Children[0].Value) // Hello, world
}

DecodePacketErr (and ReadPacket, which reads from an io.Reader) return an error on malformed input. DecodePacket is an older variant that returns nil instead; prefer the Err form. When decoding untrusted data, the MaxPacketLengthBytes and MaxNestingDepth package variables bound memory and recursion.

See the reference documentation for the full API.

Development

go test -race ./...                       # unit tests + conformance suite
go test -run='^$' -fuzz=FuzzDecodePacket  # fuzz the decoder
golangci-lint run                         # lint

The conformance suite in tests/ is built from the Strozhevsky ASN.1 test suite; see tests/README.md.

License

MIT. See LICENSE.

About

ASN1 BER Encoding / Decoding Library for the GO programming language.

Resources

Security policy

Stars

73 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages