Skip to content

asn1 ​

原文: https://finproducts.atlassian.net/wiki/spaces/JIDD3/pages/34793652251/FIN+5.3.1+asn1 中文整理基于 FIN 5.3.1

导读: Asn 提供了用于创建 AsnObj 的实用工具。

以下为 API 参考,保留英文以便对照源码;目录已列出(英文标题便于检索);概述已尽量中译。


目录(导读) ​

  • classes
  • enums
  • errs
  • Slot Details
    • Null
    • asnEnum
    • bits
    • bool
    • genTime
    • int
    • octets
    • oid
    • seq
    • set
    • str
    • tag
    • utc
    • utf8
    • builder
    • get
    • isEmpty
    • isSeq
    • isSet
    • make
    • size
    • valStr
    • vals
    • add
    • item
    • toSeq
    • toSet
    • equals
    • hash
    • name
    • val
    • bigInt
    • buf
    • coll
    • effectiveTags
    • isBool
    • isInt
    • isNull
    • isOcts
    • isOid
    • isPrimitive
    • push
    • tags
    • toStr
    • ts
    • univTag
    • valEquals
    • valHash
    • asnNull
    • compare
    • getRange
    • ids
    • oidStr
    • makeUniv
    • app
    • cls
    • context
    • id
    • mode
    • priv
    • strictEquals
    • univ
    • univBits
    • univBool
    • univEnum
    • univGenTime
    • univGraphStr
    • univIa5Str
    • univInt
    • univNull
    • univOcts
    • univOid
    • univPrintStr
    • univReal
    • univSeq
    • univSet
    • univUtcTime
    • univUtf8
    • univVisStr
    • explicit
    • implicit
    • fromStr
    • isApp
    • isContext
    • isPriv
    • isUniv
    • mask
    • close
    • readObj
    • toBuf
    • write

API 正文(英文) ​

errs ​

AsnErrA general ASN.1 error

Asn ​

const class Asn : Obj

Asn provides utilities for creating AsnObj.

Fields

NullSingleton for universal Null

Methods

asnEnumConvenience to create a universal Enumerated value
bitsConvenience to create a universal Bit String
boolConvenience to create a universal Boolean
genTimeConvenience to create a universal GeneralizedTime
intConvenience to create a universal Integer.
octetsConvenience to create a universal Octet String
oidCreate an ASN.1 Object Identifier value (OID).
seqConvenience to create a universal SEQUENCE
setConvenience to create a universal SET
strConvenience to create one of the ASN.1 string types.
tagCreate an object builder and add the given tag if it is not null.
utcConvenience to create a universal UTCTime
utf8Convenience to create a universal Utf8String

Slot Details ​


Null ​

const static AsnObj Null := AsnObj.<ctor>([AsnTag.univNull], null)

Singleton for universal Null


asnEnum ​

static AsnObj asnEnum(Int val)

Convenience to create a universal Enumerated value


bits ​

static AsnObj bits(Buf bits)

Convenience to create a universal Bit String

See AsnObjBuilder.bits


bool ​

static AsnObj bool(Bool val)

Convenience to create a universal Boolean


genTime ​

static AsnObj genTime(DateTime ts)

Convenience to create a universal GeneralizedTime


int ​

static AsnObj int(Obj val)

Convenience to create a universal Integer.

See AsnObjBuilder.int


octets ​

static AsnObj octets(Obj val)

Convenience to create a universal Octet String

See AsnObjBuilder.octets


oid ​

static AsnOid oid(Obj val)

Create an ASN.1 Object Identifier value (OID).

See AsnObjBuilder.oid


seq ​

static AsnSeq seq(Obj items)

Convenience to create a universal SEQUENCE

See AsnObjBuilder.seq


set ​

static AsnSet set(Obj items)

Convenience to create a universal SET

The items parameter may be any of the values accepted by seq.


str ​

static AsnObj str(Str val, AsnTag univ)

Convenience to create one of the ASN.1 string types.

See AsnObjBuilder.str

See utf8 to easily create UTF-8 strings.


tag ​

static AsnObjBuilder tag(AsnTag? tag)

Create an object builder and add the given tag if it is not null.


utc ​

static AsnObj utc(DateTime ts)

Convenience to create a universal UTCTime


utf8 ​

static AsnObj utf8(Str val)

Convenience to create a universal Utf8String


AsnColl ​

abstract const class AsnColl : AsnObj

Base class for ASN.1 collection types.

Constructors

make

Methods

builderGet a collection builder
getGet an item value from the collection.
isEmptyIs the collection empty
isSeqIs this a SEQUENCE
isSetIs this a SET
sizeGet the number of items in the collection
valStr
valsGet the raw AsnObj values in the collection

Slot Details ​


builder ​

static AsnCollBuilder builder()

Get a collection builder


get ​

AsnObj? get(Obj key)

Get an item value from the collection.

  • If key is a Str then get the named item.
  • If key is an Int, then get the item at that zero-based index.

isEmpty ​

Bool isEmpty()

Is the collection empty


isSeq ​

Bool isSeq()

Is this a SEQUENCE


isSet ​

Bool isSet()

Is this a SET


make ​

new make(AsnTag[] tags, Obj val)


size ​

Int size()

Get the number of items in the collection


valStr ​

protected virtual override Str valStr()


vals ​

AsnObj[] vals()

Get the raw AsnObj values in the collection


AsnCollBuilder ​

class AsnCollBuilder : Obj

AsnColl builder.

Constructors

make

Methods

addConvenience to add an AsnItem with the given value and name
itemAdd an AsnItem to the collection
toSeqBuild an ASN.1 sequence
toSetBuild an ASN.1 set

Slot Details ​


add ​

This add(AsnObj val, Str? name := null)

Convenience to add an AsnItem with the given value and name


item ​

This item(AsnItem item)

Add an AsnItem to the collection


make ​

new make()


toSeq ​

AsnColl toSeq(AsnTag? tag := null)

Build an ASN.1 sequence


toSet ​

AsnColl toSet(AsnTag? tag := null)

Build an ASN.1 set


AsnErr ​

const class AsnErr : Err

A general ASN.1 error

Constructors

make

Slot Details ​


make ​

new make(Str msg := "", Err? cause := null)


AsnItem ​

const class AsnItem : Obj

An item in an ASN.1 collection. An item has a value, and an optional name associated with that value. When comparing items, only the values are compared; the name is ignored.

Constructors

make

Fields

name
val

Methods

equals
hash

Slot Details ​


equals ​

virtual override Bool equals(Obj? obj)


hash ​

virtual override Int hash()


make ​

new make(AsnObj val, Str? name := null)


name ​

const Str? name


val ​

const AsnObj val


AsnObj ​

const class AsnObj : Obj

A tagged ASN.1 value

Constructors

make

Fields

tagsThe tags for this object.
valThe value for this object.

Methods

bigIntGet the value as a BigInt.
boolGet the value as a Bool
bufGet any of the binary values as a Buf.
collGet this object as an AsnColl
effectiveTagsApply rules for EXPLICIT and IMPLICIT tags to obtain the set of effective tags for encoding this object.
equals
hash
intGet the value as an Int.
isBoolIs this object's universal tag a Boolean
isIntIs this object's universal tag an Integer
isNullIs this an ASN.1 Null value
isOctsIs this object's universal tag an Octet String
isOidIs this object's universal tag an Object Identifier
isPrimitiveIs this a primitive type?
oidGet this object as an AsnOid
pushPush a tag to the front of the tag chain for this value.
seqGet this object as an AsnSeq
strGet the value as a Str
tagGet the single effective tag for this object.
toStr
tsGet the value as a DateTime timestamp
univTagGet the univ tag for this object
valEquals
valHash
valStr

Slot Details ​


bigInt ​

BigInt bigInt()

Get the value as a BigInt.


bool ​

Bool bool()

Get the value as a Bool


buf ​

virtual Buf buf()

Get any of the binary values as a Buf. The Buf will be a safe copy that can be modified. Throws AsnErr if the value is not a binary value.


coll ​

AsnColl coll()

Get this object as an AsnColl


effectiveTags ​

AsnTag[] effectiveTags()

Apply rules for EXPLICIT and IMPLICIT tags to obtain the set of effective tags for encoding this object.


equals ​

override Bool equals(Obj? obj)


hash ​

override Int hash()


int ​

Int int()

Get the value as an Int. If the value is a BigInt you may lose both precision and sign. Use bigInt to get the value explicitly as a BigInt.


isBool ​

Bool isBool()

Is this object's universal tag a Boolean


isInt ​

Bool isInt()

Is this object's universal tag an Integer


isNull ​

Bool isNull()

Is this an ASN.1 Null value


isOcts ​

Bool isOcts()

Is this object's universal tag an Octet String


isOid ​

Bool isOid()

Is this object's universal tag an Object Identifier


isPrimitive ​

Bool isPrimitive()

Is this a primitive type?


make ​

new make(AsnTag[] tags, Obj? val)


oid ​

AsnOid oid()

Get this object as an AsnOid


push ​

virtual AsnObj push(AsnTag tag)

Push a tag to the front of the tag chain for this value. Returns a new instance of this object with the current value.

AsnObj.int(123).tag(AsnTag.implicit(TagClass.context, 0))
  => [0] IMPLICIT [UNIVERSAL 2]
AsnObj.int(123).tag(AsnTag.explicit(TagClass.app, 1))
  => [APPLICATION 1] EXPLICIT [UNIVERSAL 2]

seq ​

AsnSeq seq()

Get this object as an AsnSeq


str ​

Str str()

Get the value as a Str


tag ​

AsnTag tag()

Get the single effective tag for this object. Throws an error if there are multiple effective tags


tags ​

const AsnTag[] tags

The tags for this object.


toStr ​

virtual override Str toStr()


ts ​

DateTime ts()

Get the value as a DateTime timestamp


univTag ​

AsnTag univTag()

Get the univ tag for this object


val ​

const Obj? val

The value for this object.


valEquals ​

protected virtual Bool valEquals(AsnObj that)


valHash ​

protected virtual Int valHash()


valStr ​

protected virtual Str valStr()


AsnObjBuilder ​

class AsnObjBuilder : Obj

Utility to build an AsnObj

Constructors

make

Methods

asnEnumBuild an ASN.1 Enumerated value.
asnNullBuild an ASN.1 Null value
bitsBuild an ASN.1 Bit String value.
boolBuild an ASN.1 Boolean value
genTimeBuild an ASN.1 GeneralizedTime value.
intBuild an ASN.1 Integer value.
octetsBuild an ASN.1 Octet String value.
oidBuild an ASN.1 Object Identifier value (OID).
seqBuild an ASN.1 SEQUENCE value The items parameter may be
setCreate an ASN.1 SET value The items parameter may be any of the values accepted by seq.
strBuild one of the ASN.1 string types.
tagAdd a tag to the object builder.
utcBuild an ASN.1 UTCTime value
utf8Build an ASN.1 Utf8String value.

Slot Details ​


asnEnum ​

AsnObj asnEnum(Int val)

Build an ASN.1 Enumerated value.


asnNull ​

AsnObj asnNull()

Build an ASN.1 Null value


bits ​

AsnObj bits(Buf bits)

Build an ASN.1 Bit String value. The bits in the bit string are numbered from left to right. For example, bits 0-7 are in the first byte of the bits buffer.


bool ​

AsnObj bool(Bool val)

Build an ASN.1 Boolean value


genTime ​

AsnObj genTime(DateTime ts)

Build an ASN.1 GeneralizedTime value.


int ​

AsnObj int(Obj val)

Build an ASN.1 Integer value. The val may be either an Int or a BigInt, but is always normalized to BigInt.


make ​

new make()


octets ​

AsnObj octets(Obj val)

Build an ASN.1 Octet String value. The val may be:

  • a Str - it will be converted to a Buf as ((Str)val).toBuf
  • a Buf containing the raw octets

oid ​

AsnOid oid(Obj val)

Build an ASN.1 Object Identifier value (OID). The val may be:

  1. an Int[] where each element of the list is a part of the oid.
  2. a Str where each part of the oid is separated by ..
Asn.oid([1,2,3])
Asn.oid("1.2.3")

seq ​

AsnSeq seq(Obj items)

Build an ASN.1 SEQUENCE value The items parameter may be:

  • An AsnItem[] of raw items to add to the collection
  • An AsnObj[]
  • A Str:AsnObj - if the order of the sequence is important, you should ensure the map is ordered.

set ​

AsnSet set(Obj items)

Create an ASN.1 SET value The items parameter may be any of the values accepted by seq.


str ​

AsnObj str(Str val, AsnTag univ)

Build one of the ASN.1 string types. The univ parameter must be one of:

  • AsnTag.univUtf8
  • AsnTag.univPrintStr
  • AsnTag.univIa5Str
  • AsnTag.univVisStr

See utf8 to easily create UTF-8 strings.


tag ​

This tag(AsnTag? tag)

Add a tag to the object builder. Tags should be added in ther order they are specified in an ASN.1 type declaration. If the tag is null, then this is a no-op.

Whenever a concrete AsnObj is built, the builder will clear all tags.

// [0] [1 APPLICATION] Boolean
obj := AsnObjBuilder()
   .tag(AsnTag.context(0).implicit)
   .tag(AsnTag.app(1).implicit)
   .bool(true)

utc ​

AsnObj utc(DateTime ts)

Build an ASN.1 UTCTime value


utf8 ​

AsnObj utf8(Str val)

Build an ASN.1 Utf8String value.


AsnOid ​

const class AsnOid : AsnObj

Models an ASN.1 OBJECT IDENTIFIER type.

Constructors

make

Methods

compareOid is ordered by comparing its sub-identifier parts numerically.
getRangeGet a new Oid based on the specified range.
idsConvenience to get the value as a list of its Int identifiers.
oidStrConvenience to get a Str where the sub-identifiers are joined with a .
valStr

Slot Details ​


compare ​

virtual override Int compare(Obj that)

Oid is ordered by comparing its sub-identifier parts numerically.


getRange ​

@Operator AsnOid getRange(Range range)

Get a new Oid based on the specified range. This Oid is guaranteed to be in the universal tag class (i.e. - the tag is not preservered).

Throw IndexErr if the range is illegal.


ids ​

Int[] ids()

Convenience to get the value as a list of its Int identifiers.


make ​

new make(AsnTag[] tags, Int[] val)


oidStr ​

Str oidStr()

Convenience to get a Str where the sub-identifiers are joined with a .

Asn.oid("1.2.3").oidStr == "1.2.3"

valStr ​

virtual override Str valStr()


AsnSeq ​

const class AsnSeq : AsnColl

Models an ASN.1 SEQUENCE

Constructors

make
makeUniv

Slot Details ​


make ​

new make(AsnTag[] tags, Obj val)


makeUniv ​

new makeUniv(Obj val)


AsnSet ​

const class AsnSet : AsnColl

Models an ASN.1 SET

Constructors

make
makeUniv

Slot Details ​


make ​

new make(AsnTag[] tags, Obj val)


makeUniv ​

new makeUniv(Obj val)


AsnTag ​

const class AsnTag : Obj

ASN.1 Tag

Constructors

make

Fields

clsThe tag class
idThe tag id
modeThe tag mode
univBits
univBool
univEnum
univGenTime
univGraphStr
univIa5Str
univInt
univNull
univOcts
univOid
univPrintStr
univReal
univSeq
univSet
univUtcTime
univUtf8
univVisStr

Methods

appGet a builder for an application tag with the given id
contextGet a builder for a context tag with the given id
equalsTag equality is based only the class and id.
hash
privGet a builder for a private tag with the given id
strictEquals
toStr
univGet a builder for a universal tag with the given id.

Slot Details ​


app ​

static AsnTagBuilder app(Int id)

Get a builder for an application tag with the given id


cls ​

const AsnTagClass cls

The tag class


context ​

static AsnTagBuilder context(Int id)

Get a builder for a context tag with the given id


equals ​

virtual override Bool equals(Obj? obj)

Tag equality is based only the class and id. The mode is ignored for eqality purposes.


hash ​

virtual override Int hash()


id ​

const Int id

The tag id


make ​

new make(AsnTagClass cls, Int id, AsnTagMode mode)


mode ​

const AsnTagMode mode

The tag mode


priv ​

static AsnTagBuilder priv(Int id)

Get a builder for a private tag with the given id


strictEquals ​

Bool strictEquals(Obj? obj)


toStr ​

virtual override Str toStr()


univ ​

static AsnTagBuilder univ(Int id)

Get a builder for a universal tag with the given id.


univBits ​

const static AsnTag univBits := AsnTag.univ(3).explicit


univBool ​

const static AsnTag univBool := AsnTag.univ(1).explicit


univEnum ​

const static AsnTag univEnum := AsnTag.univ(10).explicit


univGenTime ​

const static AsnTag univGenTime := AsnTag.univ(24).explicit


univGraphStr ​

const static AsnTag univGraphStr := AsnTag.univ(25).explicit


univIa5Str ​

const static AsnTag univIa5Str := AsnTag.univ(22).explicit


univInt ​

const static AsnTag univInt := AsnTag.univ(2).explicit


univNull ​

const static AsnTag univNull := AsnTag.univ(5).explicit


univOcts ​

const static AsnTag univOcts := AsnTag.univ(4).explicit


univOid ​

const static AsnTag univOid := AsnTag.univ(6).explicit


univPrintStr ​

const static AsnTag univPrintStr := AsnTag.univ(19).explicit


univReal ​

const static AsnTag univReal := AsnTag.univ(9).explicit


univSeq ​

const static AsnTag univSeq := AsnTag.univ(16).explicit


univSet ​

const static AsnTag univSet := AsnTag.univ(17).explicit


univUtcTime ​

const static AsnTag univUtcTime := AsnTag.univ(23).explicit


univUtf8 ​

const static AsnTag univUtf8 := AsnTag.univ(12).explicit


univVisStr ​

const static AsnTag univVisStr := AsnTag.univ(26).explicit


AsnTagBuilder ​

class AsnTagBuilder : Obj

Utility to build an AsnTag.

See:

  • AsnTag.univ
  • AsnTag.context
  • AsnTag.app
  • AsnTag.priv

Constructors

makeCreate an unconfigured builder

Methods

appSet the tag class to application
contextSet the tag class to context
explicitBuild the tag with explicit mode
idSet the tag identifier
implicitBuild the tag with implicit mode
privSet the tag class to private
univSet the tag class to universal

Slot Details ​


app ​

This app()

Set the tag class to application


context ​

This context()

Set the tag class to context


explicit ​

AsnTag explicit()

Build the tag with explicit mode


id ​

This id(Int id)

Set the tag identifier


implicit ​

AsnTag implicit()

Build the tag with implicit mode


make ​

new make()

Create an unconfigured builder


priv ​

This priv()

Set the tag class to private


univ ​

This univ()

Set the tag class to universal


AsnTagClass ​

@Serializable { simple=true } enum class AsnTagClass : Enum

The tag class for an AsnTag

Enum

univ
app
context
priv

Constructors

fromStrReturn the AsnTagClass instance for the specified name.

Fields

mask
valsList of AsnTagClass values indexed by ordinal

Methods

isAppIs this the APPLICATION class
isContextIs this the CONTEXT class
isPrivIs this the PRIVATE class
isUnivIs this the UNIVERSAL class

Slot Details ​


app ​

const static AsnTagClass app := ...


context ​

const static AsnTagClass context := ...


fromStr ​

static new fromStr(Str name, Bool checked := true)

Return the AsnTagClass instance for the specified name. If not a valid name and checked is false return null, otherwise throw ParseErr.


isApp ​

Bool isApp()

Is this the APPLICATION class


isContext ​

Bool isContext()

Is this the CONTEXT class


isPriv ​

Bool isPriv()

Is this the PRIVATE class


isUniv ​

Bool isUniv()

Is this the UNIVERSAL class


mask ​

const Int mask


priv ​

const static AsnTagClass priv := ...


univ ​

const static AsnTagClass univ := ...


vals ​

const static AsnTagClass[] vals := ...

List of AsnTagClass values indexed by ordinal


AsnTagMode ​

@Serializable { simple=true } enum class AsnTagMode : Enum

The tag mode for a AsnTag

Enum

explicit
implicit

Constructors

fromStrReturn the AsnTagMode instance for the specified name.

Fields

valsList of AsnTagMode values indexed by ordinal

Slot Details ​


explicit ​

const static AsnTagMode explicit := ...


fromStr ​

static new fromStr(Str name, Bool checked := true)

Return the AsnTagMode instance for the specified name. If not a valid name and checked is false return null, otherwise throw ParseErr.


implicit ​

const static AsnTagMode implicit := ...


vals ​

const static AsnTagMode[] vals := ...

List of AsnTagMode values indexed by ordinal


BerReader ​

class BerReader : Obj

BerReader decodes ASN.1 objects using the Basic Encoding Rules

Constructors

make

Methods

close
readObj

Slot Details ​


close ​

Bool close()


make ​

new make(InStream in)


readObj ​

AsnObj readObj(AsnObj? spec := null)


BerWriter ​

class BerWriter : Obj

BerWriter encodes ASN.1 objects using the Basic Encoding Rules.

Constructors

make

Methods

close
toBuf
write

Slot Details ​


close ​

Bool close()


make ​

new make(OutStream out)


toBuf ​

static Buf toBuf(AsnObj obj, Buf buf := Buf.<ctor>())


write ​

This write(AsnObj obj)