D
.github
Bump actions/setup-go from 6 to 7 (#55)
tests
Harden and modernize the BER decoder (#52)
.gitignore
Harden and modernize the BER decoder (#52)
.golangci.yml
Harden and modernize the BER decoder (#52)
ber_test.go
fix: reject long-form definite lengths that wrap negative (#54)
ber.go
Harden and modernize the BER decoder (#52)
CHANGELOG.md
Harden and modernize the BER decoder (#52)
constructors_test.go
Harden and modernize the BER decoder (#52)
content_int.go
lint fixes (#28)
doc.go
Harden and modernize the BER decoder (#52)
example_test.go
Harden and modernize the BER decoder (#52)
fuzz_test.go
BUGFIX: memory was allocated based on encoded length instead of the length of the io.Reader, causing potential DOS due to unexpected high memory usage (max MaxPacketLengthBytes) (#42)
generalizedTime_test.go
Gofumpt (#38)
generalizedTime.go
29 generalized time bug (#30)
go.mod
Harden and modernize the BER decoder (#52)
header_test.go
Harden and modernize the BER decoder (#52)
header.go
lint fixes (#28)
identifier_test.go
Harden and modernize the BER decoder (#52)
identifier.go
return EOF when reading first byte of an ASN.1 packet (#35)
lazy_test.go
Harden and modernize the BER decoder (#52)
length_test.go
fix: reject long-form definite lengths that wrap negative (#54)
length.go
Harden and modernize the BER decoder (#52)
LICENSE
Retain @mmitton's license
README.md
Harden and modernize the BER decoder (#52)
real_test.go
Harden and modernize the BER decoder (#52)
real.go
Harden and modernize the BER decoder (#52)
SECURITY.md
Harden and modernize the BER decoder (#52)
string_test.go
lint fixes (#28)
suite_test.go
Harden and modernize the BER decoder (#52)
util.go
Harden and modernize the BER decoder (#52)

asn1-ber

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.