Involved Source Files 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.common.gomarshal.go
Package-Level Type Names (total 23, in which 8 are exported)
/* sort exporteds by: | */
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. // length in bits. // bits packed into bytes. At returns the bit at the given index. If the index is out of range it
returns 0. RightAlign returns a slice where the padding bits are at the beginning. The
slice may share memory with the BitString.
func parseBitString(bytes []byte) (ret BitString, err error)
func vendor/golang.org/x/crypto/cryptobyte.(*String).ReadASN1BitString(out *BitString) bool
An Enumerated is represented as a plain int.
A Flag accepts any data and is set to true if present.
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.
A StructuralError suggests that the ASN.1 data is valid, but the Go type
which is receiving it doesn't match.Msgstring( StructuralError) Error() string
StructuralError : error
A SyntaxError suggests that the ASN.1 data is invalid.Msgstring( SyntaxError) Error() string
SyntaxError : error
fieldParameters is the parsed representation of tag string from a structure field. // true iff an APPLICATION tag is in use. // a default value for INTEGER typed fields (maybe nil). // true iff an EXPLICIT tag is in use. // true iff this should be omitted if empty when marshaling. // true iff the field is OPTIONAL // true iff a PRIVATE tag is in use. // true iff this should be encoded as a SET // the string tag to use when marshaling. // the EXPLICIT or IMPLICIT tag (maybe nil). // the time tag to use when marshaling.
func parseFieldParameters(str string) (ret fieldParameters)
func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error)
func makeField(v reflect.Value, params fieldParameters) (e encoder, err error)
func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error)
func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool)
An invalidUnmarshalError describes an invalid argument passed to Unmarshal.
(The argument to Unmarshal must be a non-nil pointer.)Typereflect.Type(*invalidUnmarshalError) Error() string
*invalidUnmarshalError : error
bodyencoder scratch contains temporary space for encoding the tag and length of
an element in order to avoid extra allocations.tagencoder(*taggedEncoder) Encode(dst []byte)(*taggedEncoder) Len() int
*taggedEncoder : encoder
Package-Level Functions (total 52, in which 4 are exported)
Marshal returns the ASN.1 encoding of val.
In addition to the struct tags recognised by Unmarshal, the following can be
used:
ia5: causes strings to be marshaled as ASN.1, IA5String values
omitempty: causes empty slices to be skipped
printable: causes strings to be marshaled as ASN.1, PrintableString values
utf8: causes strings to be marshaled as ASN.1, UTF8String values
utc: causes time.Time to be marshaled as ASN.1, UTCTime values
generalized: causes time.Time to be marshaled as ASN.1, GeneralizedTime values
MarshalWithParams allows field parameters to be specified for the
top-level element. The form of the params is the same as the field tags.
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. If val
is nil or not a pointer, Unmarshal returns an error.
After parsing b, any bytes that were leftover and not used to fill
val will be returned in rest. When parsing a SEQUENCE into a struct,
any trailing elements of the SEQUENCE that do not have matching
fields in val will not be included in rest, as these are considered
valid elements of the SEQUENCE and not trailing data.
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.
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 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:
application specifies that an APPLICATION tag is used
private specifies that a PRIVATE tag is used
default: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 one
optional marks the field as ASN.1 OPTIONAL
set causes a SET, rather than a SEQUENCE type to be expected
tag: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 support
characters such as '@' and '&'. To force other encodings, use the following
tags:
ia5 causes strings to be unmarshaled as ASN.1 IA5String values
numeric causes strings to be unmarshaled as ASN.1 NumericString values
utf8 causes strings to be unmarshaled as ASN.1 UTF8String values
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 name of a slice type ends with "SET" then it's treated as if
the "set" tag was set on it. This results in interpreting the type as a
SET OF x rather than a SEQUENCE OF x. 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.
UnmarshalWithParams allows field parameters to be specified for the
top-level element. The form of the params is the same as the field tags.
canHaveDefaultValue reports whether k is a Kind that we will set a default
value for. (A signed integer, essentially.)
checkInteger returns nil if the given bytes are a valid DER-encoded
INTEGER and an error otherwise.
Given a reflected Go type, getUniversalType returns the default tag number
and expected compound flag.
invalidLength reports whether offset + length > sliceLength, or if the
addition would overflow.
isNumeric reports whether the given b is in the ASN.1 NumericString set.
isPrintable reports whether the given b is in the ASN.1 PrintableString set.
If asterisk is allowAsterisk then '*' is also allowed, reflecting existing
practice. If ampersand is allowAmpersand then '&' is allowed as well.
parseField is the main parsing function. Given a byte slice and an offset
into the array, it will try to parse a suitable ASN.1 value out and store it
in the given Value.
Given a tag string with the format specified in the package comment,
parseFieldParameters will parse it into a fieldParameters structure,
ignoring unknown parts of the string.
parseGeneralizedTime parses the GeneralizedTime from the given byte slice
and returns the resulting time.
parseIA5String parses an ASN.1 IA5String (ASCII string) from the given
byte slice and returns it.
parseInt32 treats the given bytes as a big-endian, signed integer and returns
the result.
parseInt64 treats the given bytes as a big-endian, signed integer and
returns the result.
parseNumericString parses an ASN.1 NumericString from the given byte array
and returns it.
parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and
returns it. An object identifier is a sequence of variable length integers
that are assigned in a hierarchy.
parsePrintableString parses an ASN.1 PrintableString from the given byte
array and returns it.
parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse
a number of ASN.1 values from the given byte slice and returns them as a
slice of Go values of the given type.
parseT61String parses an ASN.1 T61String (8-bit clean string) from the given
byte slice and returns it.
parseTagAndLength parses an ASN.1 tag and length pair from the given offset
into a byte slice. It returns the parsed data and the new offset. SET and
SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we
don't distinguish between ordered and unordered objects in this code.
parseUTF8String parses an ASN.1 UTF8String (raw UTF-8) from the given byte
array and returns it.
setDefaultValue is used to install a default value, from a tag string, into
a Value. It is successful if the field was optional, even if a default value
wasn't provided or it failed to install it into the Value.
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.