asn1

Imports

Imports #

"bytes"
"errors"
"fmt"
"math/big"
"reflect"
"slices"
"time"
"unicode/utf8"
"errors"
"fmt"
"math"
"math/big"
"reflect"
"slices"
"strconv"
"strings"
"time"
"unicode/utf16"
"unicode/utf8"
"reflect"
"strconv"
"strings"

Constants & Variables

ClassApplication const #

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

const ClassApplication = 1

ClassContextSpecific const #

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

const ClassContextSpecific = 2

ClassPrivate const #

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

const ClassPrivate = 3

ClassUniversal const #

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

const ClassUniversal = 0

NullBytes var #

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

var NullBytes = []byte{...}

NullRawValue var #

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

var NullRawValue = RawValue{...}

TagBMPString const #

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

const TagBMPString = 30

TagBitString const #

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

const TagBitString = 3

TagBoolean const #

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

const TagBoolean = 1

TagEnum const #

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

const TagEnum = 10

TagGeneralString const #

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

const TagGeneralString = 27

TagGeneralizedTime const #

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

const TagGeneralizedTime = 24

TagIA5String const #

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

const TagIA5String = 22

TagInteger const #

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

const TagInteger = 2

TagNull const #

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

const TagNull = 5

TagNumericString const #

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

const TagNumericString = 18

TagOID const #

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

const TagOID = 6

TagOctetString const #

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

const TagOctetString = 4

TagPrintableString const #

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

const TagPrintableString = 19

TagSequence const #

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

const TagSequence = 16

TagSet const #

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

const TagSet = 17

TagT61String const #

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

const TagT61String = 20

TagUTCTime const #

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

const TagUTCTime = 23

TagUTF8String const #

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

const TagUTF8String = 12

allowAmpersand const #

const allowAmpersand ampersandFlag = true

allowAsterisk const #

const allowAsterisk asteriskFlag = true

bigIntType var #

var bigIntType = *ast.CallExpr

bigOne var #

var bigOne = *ast.CallExpr

bitStringType var #

var bitStringType = *ast.CallExpr

byte00Encoder var #

var byte00Encoder encoder = *ast.CallExpr

byteFFEncoder var #

var byteFFEncoder encoder = *ast.CallExpr

enumeratedType var #

var enumeratedType = *ast.CallExpr

flagType var #

var flagType = *ast.CallExpr

objectIdentifierType var #

var objectIdentifierType = *ast.CallExpr

rawContentsType var #

var rawContentsType = *ast.CallExpr

rawValueType var #

var rawValueType = *ast.CallExpr

rejectAmpersand const #

const rejectAmpersand ampersandFlag = false

rejectAsterisk const #

const rejectAsterisk asteriskFlag = false

timeType var #

var timeType = *ast.CallExpr

Type Aliases

Enumerated type #

An Enumerated is represented as a plain int.

type Enumerated int

Flag type #

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

type Flag bool

ObjectIdentifier type #

An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.

type ObjectIdentifier []int

RawContent type #

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 RawContent []byte

ampersandFlag type #

type ampersandFlag bool

asteriskFlag type #

type asteriskFlag bool

bitStringEncoder type #

type bitStringEncoder BitString

byteEncoder type #

type byteEncoder byte

bytesEncoder type #

type bytesEncoder []byte

int64Encoder type #

type int64Encoder int64

multiEncoder type #

type multiEncoder []encoder

oidEncoder type #

type oidEncoder []int

setEncoder type #

type setEncoder []encoder

stringEncoder type #

type stringEncoder string

Interfaces

encoder interface #

encoder represents an ASN.1 element that is waiting to be marshaled.

type encoder interface {
Len() int
Encode(dst []byte)
}

Structs

BitString struct #

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.

type BitString struct {
Bytes []byte
BitLength int
}

RawValue struct #

A RawValue represents an undecoded ASN.1 object.

type RawValue struct {
Class int
Tag int
IsCompound bool
Bytes []byte
FullBytes []byte
}

StructuralError struct #

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

type StructuralError struct {
Msg string
}

SyntaxError struct #

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

type SyntaxError struct {
Msg string
}

fieldParameters struct #

fieldParameters is the parsed representation of tag string from a structure field.

type fieldParameters struct {
optional bool
explicit bool
application bool
private bool
defaultValue *int64
tag *int
stringType int
timeType int
set bool
omitEmpty bool
}

invalidUnmarshalError struct #

An invalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)

type invalidUnmarshalError struct {
Type reflect.Type
}

tagAndLength struct #

type tagAndLength struct {
class int
tag int
length int
isCompound bool
}

taggedEncoder struct #

type taggedEncoder struct {
scratch [8]byte
tag encoder
body encoder
}

Functions

At method #

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

func (b BitString) At(i int) int

Encode method #

func (s setEncoder) Encode(dst []byte)

Encode method #

func (b bytesEncoder) Encode(dst []byte)

Encode method #

func (s stringEncoder) Encode(dst []byte)

Encode method #

func (m multiEncoder) Encode(dst []byte)

Encode method #

func (b bitStringEncoder) Encode(dst []byte)

Encode method #

func (oid oidEncoder) Encode(dst []byte)

Encode method #

func (t *taggedEncoder) Encode(dst []byte)

Encode method #

func (c byteEncoder) Encode(dst []byte)

Encode method #

func (i int64Encoder) Encode(dst []byte)

Equal method #

Equal reports whether oi and other represent the same identifier.

func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool

Error method #

func (e SyntaxError) Error() string

Error method #

func (e StructuralError) Error() string

Error method #

func (e *invalidUnmarshalError) Error() string

Len method #

func (oid oidEncoder) Len() int

Len method #

func (c byteEncoder) Len() int

Len method #

func (b bytesEncoder) Len() int

Len method #

func (s stringEncoder) Len() int

Len method #

func (m multiEncoder) Len() int

Len method #

func (s setEncoder) Len() int

Len method #

func (b bitStringEncoder) Len() int

Len method #

func (t *taggedEncoder) Len() int

Len method #

func (i int64Encoder) Len() int

Marshal function #

Marshal returns the ASN.1 encoding of val. In addition to the struct tags recognized 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

func Marshal(val any) ([]byte, error)

MarshalWithParams function #

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 MarshalWithParams(val any, params string) ([]byte, error)

RightAlign method #

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

func (b BitString) RightAlign() []byte

String method #

func (oi ObjectIdentifier) String() string

Unmarshal function #

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]. 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.

func Unmarshal(b []byte, val any) (rest []byte, err error)

UnmarshalWithParams function #

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

func UnmarshalWithParams(b []byte, val any, params string) (rest []byte, err error)

appendBase128Int function #

func appendBase128Int(dst []byte, n int64) []byte

appendFourDigits function #

func appendFourDigits(dst []byte, v int) []byte

appendGeneralizedTime function #

func appendGeneralizedTime(dst []byte, t time.Time) (ret []byte, err error)

appendLength function #

func appendLength(dst []byte, i int) []byte

appendTagAndLength function #

func appendTagAndLength(dst []byte, t tagAndLength) []byte

appendTimeCommon function #

func appendTimeCommon(dst []byte, t time.Time) []byte

appendTwoDigits function #

func appendTwoDigits(dst []byte, v int) []byte

appendUTCTime function #

func appendUTCTime(dst []byte, t time.Time) (ret []byte, err error)

base128IntLength function #

func base128IntLength(n int64) int

canHaveDefaultValue function #

canHaveDefaultValue reports whether k is a Kind that we will set a default value for. (A signed integer, essentially.)

func canHaveDefaultValue(k reflect.Kind) bool

checkInteger function #

checkInteger returns nil if the given bytes are a valid DER-encoded INTEGER and an error otherwise.

func checkInteger(bytes []byte) error

getUniversalType function #

Given a reflected Go type, getUniversalType returns the default tag number and expected compound flag.

func getUniversalType(t reflect.Type) (matchAny bool, tagNumber int, isCompound bool, ok bool)

invalidLength function #

invalidLength reports whether offset + length > sliceLength, or if the addition would overflow.

func invalidLength(offset int, length int, sliceLength int) bool

isNumeric function #

isNumeric reports whether the given b is in the ASN.1 NumericString set.

func isNumeric(b byte) bool

isPrintable function #

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.

func isPrintable(b byte, asterisk asteriskFlag, ampersand ampersandFlag) bool

lengthLength function #

func lengthLength(i int) (numBytes int)

makeBigInt function #

func makeBigInt(n *big.Int) (encoder, error)

makeBody function #

func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error)

makeField function #

func makeField(v reflect.Value, params fieldParameters) (e encoder, err error)

makeGeneralizedTime function #

func makeGeneralizedTime(t time.Time) (e encoder, err error)

makeIA5String function #

func makeIA5String(s string) (e encoder, err error)

makeNumericString function #

func makeNumericString(s string) (e encoder, err error)

makeObjectIdentifier function #

func makeObjectIdentifier(oid []int) (e encoder, err error)

makePrintableString function #

func makePrintableString(s string) (e encoder, err error)

makeUTCTime function #

func makeUTCTime(t time.Time) (e encoder, err error)

makeUTF8String function #

func makeUTF8String(s string) encoder

outsideUTCRange function #

func outsideUTCRange(t time.Time) bool

parseBMPString function #

parseBMPString parses an ASN.1 BMPString (Basic Multilingual Plane of ISO/IEC/ITU 10646-1) from the given byte slice and returns it.

func parseBMPString(bmpString []byte) (string, error)

parseBase128Int function #

parseBase128Int parses a base-128 encoded int from the given offset in the given byte slice. It returns the value and the new offset.

func parseBase128Int(bytes []byte, initOffset int) (ret int, offset int, err error)

parseBigInt function #

parseBigInt treats the given bytes as a big-endian, signed integer and returns the result.

func parseBigInt(bytes []byte) (*big.Int, error)

parseBitString function #

parseBitString parses an ASN.1 bit string from the given byte slice and returns it.

func parseBitString(bytes []byte) (ret BitString, err error)

parseBool function #

func parseBool(bytes []byte) (ret bool, err error)

parseField function #

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.

func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error)

parseFieldParameters function #

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.

func parseFieldParameters(str string) (ret fieldParameters)

parseGeneralizedTime function #

parseGeneralizedTime parses the GeneralizedTime from the given byte slice and returns the resulting time.

func parseGeneralizedTime(bytes []byte) (ret time.Time, err error)

parseIA5String function #

parseIA5String parses an ASN.1 IA5String (ASCII string) from the given byte slice and returns it.

func parseIA5String(bytes []byte) (ret string, err error)

parseInt32 function #

parseInt32 treats the given bytes as a big-endian, signed integer and returns the result.

func parseInt32(bytes []byte) (int32, error)

parseInt64 function #

parseInt64 treats the given bytes as a big-endian, signed integer and returns the result.

func parseInt64(bytes []byte) (ret int64, err error)

parseNumericString function #

parseNumericString parses an ASN.1 NumericString from the given byte array and returns it.

func parseNumericString(bytes []byte) (ret string, err error)

parseObjectIdentifier function #

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.

func parseObjectIdentifier(bytes []byte) (s ObjectIdentifier, err error)

parsePrintableString function #

parsePrintableString parses an ASN.1 PrintableString from the given byte array and returns it.

func parsePrintableString(bytes []byte) (ret string, err error)

parseSequenceOf function #

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.

func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type) (ret reflect.Value, err error)

parseT61String function #

parseT61String parses an ASN.1 T61String (8-bit clean string) from the given byte slice and returns it.

func parseT61String(bytes []byte) (ret string, err error)

parseTagAndLength function #

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.

func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err error)

parseUTCTime function #

func parseUTCTime(bytes []byte) (ret time.Time, err error)

parseUTF8String function #

parseUTF8String parses an ASN.1 UTF8String (raw UTF-8) from the given byte array and returns it.

func parseUTF8String(bytes []byte) (ret string, err error)

setDefaultValue function #

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.

func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool)

stripTagAndLength function #

func stripTagAndLength(in []byte) []byte

Generated with Arrow