Overview

Package asn1 implements parsing of DER-encoded ASN.1 data structures, as defined
in ITU-T Rec X.690.

See also ``A Layman’s Guide to a Subset of ASN.1, BER, and DER,’’
.

Index

common.go

Constants

ASN.1 tags represent the type of the following object.

  1. const (
  2. ClassUniversal = 0
  3. ClassApplication = 1
  4. ClassContextSpecific = 2
  5. ClassPrivate = 3
  6. )

ASN.1 class types represent the namespace of the tag.

Variables

  1. var NullBytes = []byte{, 0}

NullBytes contains bytes representing the DER-encoded ASN.1 NULL type.

  1. var NullRawValue = RawValue{: TagNull}

NullRawValue is a RawValue with its Tag set to the ASN.1 NULL type tag (5).

  1. func Marshal(val interface{}) ([], error)

Marshal returns the ASN.1 encoding of val.

In addition to the struct tags recognised by Unmarshal, the following can be
used:

  1. ia5: causes strings to be marshaled as ASN.1, IA5String values
  2. printable: causes strings to be marshaled as ASN.1, PrintableString values
  3. utc: causes time.Time to be marshaled as ASN.1, UTCTime values

func

  1. func MarshalWithParams(val interface{}, params ) ([]byte, )

MarshalWithParams allows field parameters to be specified for the top-level
element. The form of the params is the same as the field tags.

func Unmarshal

  1. func Unmarshal(b []byte, val interface{}) (rest [], err error)

Unmarshal parses the DER-encoded ASN.1 data structure b and uses the reflect
package to fill in an arbitrary value pointed at by val. Because Unmarshal uses
the reflect package, the structs being written to must use upper case field
names.

An ASN.1 INTEGER can be written to an int, int32, int64, or *big.Int (from the
math/big package). If the encoded value does not fit in the Go type, Unmarshal
returns a parse error.

An ASN.1 BIT STRING can be written to a BitString.

An ASN.1 OCTET STRING can be written to a []byte.

An ASN.1 OBJECT IDENTIFIER can be written to an ObjectIdentifier.

An ASN.1 ENUMERATED can be written to an Enumerated.

An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a time.Time.

Any of the above ASN.1 values can be written to an interface{}. The value stored
in the interface has the corresponding Go type. For integers, that type is
int64.

An ASN.1 SEQUENCE OF x or SET OF x can be written to a slice if an x can be
written to the slice’s element type.

An ASN.1 SEQUENCE or SET can be written to a struct if each of the elements in
the sequence can be written to the corresponding element in the struct.

The following tags on struct fields have special meaning to Unmarshal:

If the type of the first field of a structure is RawContent then the raw ASN1
contents of the struct will be stored in it.

If the type name of a slice element ends with “SET” then it’s treated as if the
“set” tag was set on it. This can be used with nested slices where a struct tag
cannot be given.

Other ASN.1 types are not supported; if it encounters them, Unmarshal returns a
parse error.

func

  1. func UnmarshalWithParams(b [], val interface{}, params string) (rest [], err error)

UnmarshalWithParams allows field parameters to be specified for the top-level
element. The form of the params is the same as the field tags.

type

  1. type BitString struct {
  2. Bytes [] // bits packed into bytes.
  3. }

BitString is the structure to use when you want an ASN.1 BIT STRING type. A bit
string is padded up to the nearest byte in memory and the number of valid bits
is recorded. Padding bits will be zero.

func (BitString) At

  1. func (b BitString) At(i ) int

At returns the bit at the given index. If the index is out of range it returns
false.

  1. func (b ) RightAlign() []byte

RightAlign returns a slice where the padding bits are at the beginning. The
slice may share memory with the BitString.

  1. type Enumerated

An Enumerated is represented as a plain int.

type Flag

  1. type Flag bool

type

  1. type ObjectIdentifier []

An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.

func (ObjectIdentifier) Equal

Equal reports whether oi and other represent the same identifier.

  1. func (oi ObjectIdentifier) String()

type RawContent

  1. type RawContent []byte

RawContent is used to signal that the undecoded, DER data needs to be preserved
for a struct. To use it, the first field of the struct must have this type. It’s
an error for any of the other fields to have this type.

type

  1. type RawValue struct {
  2. Class, Tag
  3. IsCompound bool
  4. Bytes []
  5. FullBytes []byte // includes the tag and length
  6. }

A RawValue represents an undecoded ASN.1 object.

  1. type StructuralError struct {
  2. Msg
  3. }

A StructuralError suggests that the ASN.1 data is valid, but the Go type which
is receiving it doesn’t match.

func (StructuralError) Error

  1. func (e StructuralError) Error()

type SyntaxError

  1. type SyntaxError struct {
  2. Msg string
  3. }

A SyntaxError suggests that the ASN.1 data is invalid.