asn1

package
v1.23.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

パッケージasn1は、ITU-T Rec X.690で定義されたDERエンコードされたASN.1データ構造の解析を実装します。 また、「ASN.1、BER、およびDERのサブセットの素人向けガイド」も参照してください。 http://luca.ntop.org/Teaching/Appunti/asn1.html

Index

Constants

View Source
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のタグは、次のオブジェクトのタイプを表します。

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

ASN.1クラスタイプは、タグの名前空間を表します。

Variables

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

NullBytesには、DERエンコードされたASN.1 NULLタイプを表すバイトが含まれています。

View Source
var NullRawValue = RawValue{Tag: TagNull}

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

Functions

func Marshal

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

MarshalはvalのASN.1エンコーディングを返します。

In addition to the struct tags recognized by Unmarshal, the following can be used:

ia5:         文字列をASN.1のIA5String値としてエンコードします。
omitempty:   空のスライスをスキップします。
printable:   文字列をASN.1のPrintableString値としてエンコードします。
utf8:        文字列をASN.1のUTF8String値としてエンコードします。
utc:         time.TimeをASN.1のUTCTime値としてエンコードします。
generalized: time.TimeをASN.1のGeneralizedTime値としてエンコードします。

func MarshalWithParams added in v1.10.0

func MarshalWithParams(val any, params string) ([]byte, error)

MarshalWithParamsは、トップレベルの要素にフィールドパラメータを指定することを可能にします。パラメータの形式は、フィールドタグと同じです。

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の解析時、valにマッチするフィールドを持たないトレーリング要素は、 トレーリングデータではなくSEQUENCEの有効な要素と見なされないため、restには含まれません。

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

構造体フィールドに対する以下のタグにはUnmarshalに特別な意味があります。

applicationはAPPLICATIONタグが使用されていることを指定します
privateはPRIVATEタグが使用されていることを指定します
default:xはオプションの整数フィールドのデフォルト値を設定します(オプションも指定されている場合のみ使用)
explicitは暗黙のタグを追加の明示的なタグでラップすることを指定します
optionalはフィールドをASN.1 OPTIONALとしてマークします
setはSEQUENCEではなくSET型を期待します
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値として復元します

構造体の最初のフィールドの型がRawContentの場合、構造体の生のASN1コンテンツがそれに保存されます。

スライスの型名が"SET"で終わる場合、これは"set"タグが設定されたように扱われます。これにより、 タイプがSEQUENCEではなくSET OF xと解釈されます。これは、 構造体タグが付けられないネストしたスライスで使用することができます。

他のASN.1の型はサポートされていません; 遭遇すると、 Unmarshalは解析エラーを返します。

func UnmarshalWithParams

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

UnmarshalWithParamsでは、トップレベルの要素にフィールドパラメータを指定することができます。パラメータの形式は、フィールドタグと同じです。

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

フラグは任意のデータを受け入れ、存在する場合にはtrueに設定されます。

type ObjectIdentifier

type ObjectIdentifier []int

ObjectIdentifierは、ASN.1オブジェクト識別子を表します。

func (ObjectIdentifier) Equal

func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool

Equalはoiとotherが同じ識別子を表しているかどうかを報告します。

func (ObjectIdentifier) String added in v1.3.0

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL