package asn1
import "encoding/asn1"
Package asn1 实现 DER 编码的 ASN.1 数据结构的解析, 其定义见 ITU-T Rec X.690。
另见 “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 = 1 TagInteger = 2 TagBitString = 3 TagOctetString = 4 TagNull = 5 TagOID = 6 TagEnum = 10 TagUTF8String = 12 TagSequence = 16 TagSet = 17 TagNumericString = 18 TagPrintableString = 19 TagT61String = 20 TagIA5String = 22 TagUTCTime = 23 TagGeneralizedTime = 24 TagGeneralString = 27 TagBMPString = 30 )
ASN.1 标签表示后续对象的类型。
const ( ClassUniversal = 0 ClassApplication = 1 ClassContextSpecific = 2 ClassPrivate = 3 )
ASN.1 类类型表示标签的命名空间。
Variables
var NullBytes = []byte{TagNull, 0}
NullBytes 包含表示 DER 编码的 ASN.1 NULL 类型的字节。
var NullRawValue = RawValue{Tag: TagNull}
NullRawValue 是一个 RawValue,其 Tag 被设置为 ASN.1 NULL 类型标签 (5)。
Functions
func Marshal
func Marshal(val any) ([]byte, error)
Marshal 返回 val 的 ASN.1 编码。
除 Unmarshal 可识别的结构体标签外,还可以使用以下标签:
ia5: 使字符串按 ASN.1 IA5String 值编组 omitempty: 使空切片被跳过 printable: 使字符串按 ASN.1 PrintableString 值编组 utf8: 使字符串按 ASN.1 UTF8String 值编组 numeric: 使字符串按 ASN.1 NumericString 值编组 utc: 使 time.Time 按 ASN.1 UTCTime 值编组 generalized: 使 time.Time 按 ASN.1 GeneralizedTime 值编组
func MarshalWithParams
func MarshalWithParams(val any, params string) ([]byte, error)
MarshalWithParams 允许为顶层元素指定字段参数。 params 的形式与字段标签相同。
func Unmarshal
func Unmarshal(b []byte, val any) (rest []byte, err error)
Unmarshal 解析 DER 编码的 ASN.1 数据结构 b, 并使用 reflect 包填充 val 所指向的任意值。 由于 Unmarshal 使用 reflect 包,被写入的结构体 必须使用大写字段名。如果 val 为 nil 或不是指针, Unmarshal 返回错误。
解析完 b 之后,任何剩余且未用于填充 val 的字节 将在 rest 中返回。当将一个 SEQUENCE 解析到结构体时, SEQUENCE 中任何在 val 里没有匹配字段的尾部元素 都不会包含在 rest 中,因为它们被视为 SEQUENCE 的合法元素而非尾部数据。
ASN.1 INTEGER 可以写入 int、int32、int64 或 *big.Int。 如果编码后的值无法放入该 Go 类型, Unmarshal 返回解析错误。
ASN.1 BIT STRING 可以写入 BitString。
ASN.1 OCTET STRING 可以写入 []byte。
ASN.1 OBJECT IDENTIFIER 可以写入 ObjectIdentifier。
ASN.1 ENUMERATED 可以写入 Enumerated。
ASN.1 UTCTIME 或 GENERALIZEDTIME 可以写入 time.Time。
ASN.1 PrintableString、IA5String 或 NumericString 可以写入 string。
上述任何 ASN.1 值都可以写入 interface{}。 存储在接口中的值具有对应的 Go 类型。 对于整数,该类型为 int64。
ASN.1 SEQUENCE OF x 或 SET OF x 可以写入 切片,前提是 x 可以写入该切片的元素类型。
ASN.1 SEQUENCE 或 SET 可以写入结构体, 前提是序列中的每个元素都可以 写入结构体中对应的元素。
结构体字段上的以下标签对 Unmarshal 有特殊含义:
application 指定使用 APPLICATION 标签 private 指定使用 PRIVATE 标签 default:x 为可选整数字段设置默认值(仅在同时存在 optional 时使用) explicit 指定用一个附加的显式标签包裹隐式标签 optional 将该字段标记为 ASN.1 OPTIONAL set 使期望的类型为 SET 而非 SEQUENCE tag:x 指定 ASN.1 标签编号;隐含 ASN.1 CONTEXT SPECIFIC
当将以 IMPLICIT 标签标记的 ASN.1 值解码到字符串字段时, Unmarshal 默认使用 PrintableString,它不支持 '@' 和 '&' 等字符。要强制使用其他编码,请使用以下 标签:
ia5 使字符串按 ASN.1 IA5String 值反编组 numeric 使字符串按 ASN.1 NumericString 值反编组 utf8 使字符串按 ASN.1 UTF8String 值反编组
当将以 IMPLICIT 标签标记的 ASN.1 值解码到 time.Time 字段时, Unmarshal 默认使用 UTCTime,它不支持时区或 小数秒。要强制使用 GeneralizedTime,请使用以下 标签:
generalized 使 time.Time 按 ASN.1 GeneralizedTime 值反编组
如果结构体第一个字段的类型是 RawContent,那么该结构体的原始 ASN1 内容将存储在其中。
如果切片类型的名称以 "SET" 结尾,那么它就被视为 设置了 "set" 标签。这导致将该类型解释为 SET OF x 而非 SEQUENCE OF x。这可用于嵌套切片 无法给出结构体标签的场景。
不支持其他 ASN.1 类型;如果遇到它们, Unmarshal 返回解析错误。
func UnmarshalWithParams
func UnmarshalWithParams(b []byte, val any, params string) (rest []byte, err error)
UnmarshalWithParams 允许为顶层元素指定字段参数。 params 的形式与字段标签相同。
Types
type BitString
type BitString struct { Bytes []byte // 打包成字节的位。 BitLength int // 以位为单位的长度。 }
BitString 是当你需要 ASN.1 BIT STRING 类型时要使用的结构体。 位字符串在内存中补齐到最近的字节,并记录有效位的数量。 填充位为零。
func (BitString) At
func (b BitString) At(i int) int
At 返回给定索引处的位。如果索引超出范围,它返回 0。
func (BitString) RightAlign
func (b BitString) RightAlign() []byte
RightAlign 返回一个将填充位移到开头的切片。 该切片可能与 BitString 共享内存。
type Enumerated
type Enumerated int
Enumerated 以普通的 int 表示。
type Flag
type Flag bool
Flag 接受任意数据,如果存在则被设置为 true。
type ObjectIdentifier
type ObjectIdentifier []int
ObjectIdentifier 表示一个 ASN.1 OBJECT IDENTIFIER。
func (ObjectIdentifier) Equal
func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool
Equal 报告 oi 和 other 是否表示相同的标识符。
func (ObjectIdentifier) String
func (oi ObjectIdentifier) String() string
type RawContent
type RawContent []byte
RawContent 用于表示需要为结构体保留未解码的 DER 数据。 要使用它,结构体的第一个字段必须是此类型。 其他任何字段使用此类型都是错误的。
type RawValue
type RawValue struct { Class, Tag int IsCompound bool Bytes []byte FullBytes []byte // 包含标签和长度 }
RawValue 表示一个未解码的 ASN.1 对象。
type StructuralError
type StructuralError struct { Msg string }
StructuralError 表明 ASN.1 数据是有效的,但接收它的 Go 类型不匹配。
func (StructuralError) Error
func (e StructuralError) Error() string
type SyntaxError
type SyntaxError struct { Msg string }
SyntaxError 表明 ASN.1 数据无效。
func (SyntaxError) Error
func (e SyntaxError) Error() string