asn1
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
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,”http://luca.ntop.org/Teaching/Appunti/asn1.html.
Index¶
- Constants
- Variables
- func Marshal(val any) ([]byte, error)
- func MarshalWithParams(val any, params string) ([]byte, error)
- func Unmarshal(b []byte, val any) (rest []byte, err error)
- func UnmarshalWithParams(b []byte, val any, params string) (rest []byte, err error)
- type BitString
- type Enumerated
- type Flag
- type ObjectIdentifier
- type RawContent
- type RawValue
- type StructuralError
- type SyntaxError
Constants¶
const (TagBoolean = 1TagInteger = 2TagBitString = 3TagOctetString = 4TagNull = 5TagOID = 6TagEnum = 10TagUTF8String = 12TagSequence = 16TagSet = 17TagNumericString = 18TagPrintableString = 19TagT61String = 20TagIA5String = 22TagUTCTime = 23TagGeneralizedTime = 24TagGeneralString = 27TagBMPString = 30)
ASN.1 tags represent the type of the following object.
const (ClassUniversal = 0ClassApplication = 1ClassContextSpecific = 2ClassPrivate = 3)
ASN.1 class types represent the namespace of the tag.
Variables¶
var NullBytes = []byte{TagNull, 0}
NullBytes contains bytes representing the DER-encoded ASN.1 NULL type.
var NullRawValue =RawValue{Tag:TagNull}
NullRawValue is aRawValue with its Tag set to the ASN.1 NULL type tag (5).
Functions¶
funcMarshal¶
Marshal returns the ASN.1 encoding of val.
In addition to the struct tags recognized by Unmarshal, the following can beused:
ia5: causes strings to be marshaled as ASN.1, IA5String valuesomitempty: causes empty slices to be skippedprintable: causes strings to be marshaled as ASN.1, PrintableString valuesutf8: causes strings to be marshaled as ASN.1, UTF8String valuesnumeric: causes strings to be marshaled as ASN.1, NumericString valuesutc: causes time.Time to be marshaled as ASN.1, UTCTime valuesgeneralized: causes time.Time to be marshaled as ASN.1, GeneralizedTime values
funcMarshalWithParams¶added ingo1.10
MarshalWithParams allows field parameters to be specified for thetop-level element. The form of the params is the same as the field tags.
funcUnmarshal¶
Unmarshal parses the DER-encoded ASN.1 data structure band uses the reflect package to fill in an arbitrary value pointed at by val.Because Unmarshal uses the reflect package, the structsbeing written to must use upper case field names. If valis nil or not a pointer, Unmarshal returns an error.
After parsing b, any bytes that were leftover and not used to fillval will be returned in rest. When parsing a SEQUENCE into a struct,any trailing elements of the SEQUENCE that do not have matchingfields in val will not be included in rest, as these are consideredvalid elements of the SEQUENCE and not trailing data.
An ASN.1 INTEGER can be written to an int, int32, int64,or *big.Int.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 aBitString.
An ASN.1 OCTET STRING can be written to a []byte.
An ASN.1 OBJECT IDENTIFIER can be written to anObjectIdentifier.
An ASN.1 ENUMERATED can be written to anEnumerated.
An ASN.1 UTCTIME or GENERALIZEDTIME can be written to atime.Time.
An ASN.1 PrintableString, IA5String, or NumericString can be written to a string.
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 writtento 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 structif each of the elements in the sequence can bewritten to the corresponding element in the struct.
The following tags on struct fields have special meaning to Unmarshal:
application specifies that an APPLICATION tag is usedprivate specifies that a PRIVATE tag is useddefault:x sets the default value for optional integer fields (only used if optional is also present)explicit specifies that an additional, explicit tag wraps the implicit oneoptional marks the field as ASN.1 OPTIONALset causes a SET, rather than a SEQUENCE type to be expectedtag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
When decoding an ASN.1 value with an IMPLICIT tag into a string field,Unmarshal will default to a PrintableString, which doesn't supportcharacters such as '@' and '&'. To force other encodings, use the followingtags:
ia5 causes strings to be unmarshaled as ASN.1 IA5String valuesnumeric causes strings to be unmarshaled as ASN.1 NumericString valuesutf8 causes strings to be unmarshaled as ASN.1 UTF8String values
When decoding an ASN.1 value with an IMPLICIT tag into a time.Time field,Unmarshal will default to a UTCTime, which doesn't support time zones orfractional seconds. To force usage of GeneralizedTime, use the followingtag:
generalized causes time.Times to be unmarshaled as ASN.1 GeneralizedTime values
If the type of the first field of a structure is RawContent then the rawASN1 contents of the struct will be stored in it.
If the name of a slice type ends with "SET" then it's treated as ifthe "set" tag was set on it. This results in interpreting the type as aSET OF x rather than a SEQUENCE OF x. This can be used with nested sliceswhere a struct tag cannot be given.
Other ASN.1 types are not supported; if it encounters them,Unmarshal returns a parse error.
Types¶
typeBitString¶
BitString is the structure to use when you want an ASN.1 BIT STRING type. Abit string is padded up to the nearest byte in memory and the number ofvalid bits is recorded. Padding bits will be zero.
func (BitString)RightAlign¶
RightAlign returns a slice where the padding bits are at the beginning. Theslice may share memory with the BitString.
typeObjectIdentifier¶
type ObjectIdentifier []int
An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
func (ObjectIdentifier)Equal¶
func (oiObjectIdentifier) Equal(otherObjectIdentifier)bool
Equal reports whether oi and other represent the same identifier.
func (ObjectIdentifier)String¶added ingo1.3
func (oiObjectIdentifier) String()string
typeRawContent¶
type RawContent []byte
RawContent is used to signal that the undecoded, DER data needs to bepreserved for a struct. To use it, the first field of the struct must havethis type. It's an error for any of the other fields to have this type.
typeRawValue¶
type RawValue struct {Class, TagintIsCompoundboolBytes []byteFullBytes []byte// includes the tag and length}
A RawValue represents an undecoded ASN.1 object.
typeStructuralError¶
type StructuralError struct {Msgstring}
A StructuralError suggests that the ASN.1 data is valid, but the Go typewhich is receiving it doesn't match.
func (StructuralError)Error¶
func (eStructuralError) Error()string
typeSyntaxError¶
type SyntaxError struct {Msgstring}
A SyntaxError suggests that the ASN.1 data is invalid.
func (SyntaxError)Error¶
func (eSyntaxError) Error()string