Movatterモバイル変換


[0]ホーム

URL:


asn1

packagestandard library
go1.25.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:13Imported by:16,991

Details

Repository

cs.opensource.google/go/go

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

View Source
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.

View Source
const (ClassUniversal       = 0ClassApplication     = 1ClassContextSpecific = 2ClassPrivate         = 3)

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

Variables

View Source
var NullBytes = []byte{TagNull, 0}

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

View Source
var NullRawValue =RawValue{Tag:TagNull}

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

Functions

funcMarshal

func Marshal(valany) ([]byte,error)

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

funcMarshalWithParamsadded ingo1.10

func MarshalWithParams(valany, paramsstring) ([]byte,error)

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

funcUnmarshal

func Unmarshal(b []byte, valany) (rest []byte, errerror)

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.

funcUnmarshalWithParams

func UnmarshalWithParams(b []byte, valany, paramsstring) (rest []byte, errerror)

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

Types

typeBitString

type BitString struct {Bytes     []byte// bits packed into bytes.BitLengthint// length in bits.}

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)At

func (bBitString) At(iint)int

At returns the bit at the given index. If the index is out of range itreturns 0.

func (BitString)RightAlign

func (bBitString) RightAlign() []byte

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

typeEnumerated

type Enumeratedint

An Enumerated is represented as a plain int.

typeFlag

type Flagbool

A Flag accepts any data and is set to true if present.

typeObjectIdentifier

type ObjectIdentifier []int

An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.

func (ObjectIdentifier)Equal

Equal reports whether oi and other represent the same identifier.

func (ObjectIdentifier)Stringadded 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

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp