HID SE reader消息模块的ANS.1 BER学习

本文深入介绍了ASN.1的基本编码规则(BER),包括三种编码方法:原始定长、构造定长和构造不定长。详细阐述了Tag、Length和Value的编码规则,并通过实例解析了不同类型如INTEGER、BOOLEAN、NULL等的编码过程。此外,还探讨了构造类型如SEQUENCE和SET的编码特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The iCLASS SE Reader Module messaging protocol consists of a Header and a BER encoded

ASN.1 message Payload


Abstract Syntax Notation One, abbreviated ASN.1, is anotation for describing abstract types and values.

1.BER传输语法的格式一直是TLV三元组<Type,Length, Value>.

TTagL是整个类型的长度,V是类型的Value,它还可以是TLVTLV组合

2.BER传输语法是基于八位组大端编码的,高八位在左。

3.Tag是一个或若干个八位组


There are four classes of tag:

Universal, for types whose meaning is the same in all applications; these types are only defined in X.208.

Application, for types whose meaning is specific to an application, such as X.500 directory services; types in two different applications may have the same application-specific tag and different meanings.

Private, for types whose meaning is specific to a given enterprise.

Context-specific, for types whose meaning is specific to a given structured type; context-specific tags are used to distinguish between component types with the same underlying tag within the context of a given structured type, and component types in two different structured types may have the same tag and different meanings.

(1).UniversalTag类型(值是0-30):


第七、六位指明Tag的类型,UniversalTag类型用00表示;第五位指明该类型以primitive方式编码还是constructed方式编码。

Tagvalue值是基本类型的Tag的值,例如INTEGERTag值是2,SEQUENCE型类Tag值是16:

ASN.1中定义的UNIVERSALTag

Tag

类型

0

BER保留

1

BOOLEAN

2

INTEGER

3

BIT STRING

4

OCTET STRING

5

NULL

6

OBJECT IDENTIFIER

7

ObjectDescripion

8

EXTERNAL,INSTANCE OF

9

REAL

10

ENUMERATED

11

EMBEDDED PDV

12

UFT8String

13

RELATIVE-OID

14

保留

15

保留

16

SEQUENCE,SEQUENCE OF

17

SET,SET OF

18

NumericString

19

PrintableString

20

TeletexString,T61String

21

VideotexString

22

IA5String

23

UTCTime

24

GeneralizedTime

25

GraphicString

26

VisibleString,ISO646String

27

GeneralString

28

UniversalString

29

CHARACTER STRING

30

BMPString

31

保留



(2).Tag大于30时,多个八位组中编码,第一个八位组后五位全部为1,其余的八位组最高位为1表示后续还有,为0表示Tag结束。


第一个八位组高二位的取值:00表示Universal01表示APPLICATION类型,10表示context-specific,11表示PRIVATE类型

2.BER编码中Length表示Value部分所占八位组的个数,有两大类:定长方式(DefiniteForm)和不定长方式(IndefiniteForm

(1).定长方式

定长方式中,按长度是否超过一个八位,又分为短、长两种形式

短:类型长度大于等于0个八位,小于等于127

长:类型长度大于等于127个八位,小于等于256^126-1


第一个八位组的低七位指明整个L所占用的八位组个数,后续八位组表示V的长度

(2).不定长方式

Length所在八位组固定编码为0x80,但在Value编码结束后以两个0x00结尾。这种方式使得可以在编码没有完全结束的情况下,可以先发送部分消息给对方。

.各类型的编码

UNIVERSALTag和短型Value为例,讨论各种类型的BER编码,重点关注Value部分。

1.BOOLEAN

只能以primitive方式编码,FALSE的编码为(n10表示10进制):


TRUE的编码(任何不是全0都可以)为:


或者:


2.NULL

只能以primitive方式编码,且只有一个值:


3.INTEGER

只能以primitive方式编码。

(1).对于正数,如果最高比特位为0则直接编码;如果为1,则在最高比特位之前增加一个全0的八位组。

最高位为0:

最高位为1:

(2).对于负数,先取绝对值,再取反,最后加1

4.ENUMERATED

按照整数值的规则编码。

5.REAL

(1).0


(2).正无穷大:


(3).
负无穷大:


(4).基于10进制且以10为底的指数方法:


NR有三个可选值:

NR1:在V中底6位用000001表示,表示不带小数和指数的简单10进制整数。

例如4902,#4902,##4902,+4902等,#表示空格,每个字符占一个8位组。

NR2:在V中底6位用000010表示,表示还小数点的10进制数

例如4902.00,#4902.0,1.0

NR3:在V中底6位用000011表示,在NR2的基础上扩展,用字符‘E’代表以10为底的指数

例如+0.56.0E2,0.2E-3

实数的表示方法在ISO6093标准文档中做了详细定义。

(5).基于2进制的编码方式

这种方式比较奇怪,先放一边

6.BIT STRING

可以primitive方式编码或者constructed方式编码。

(1).Primitive方式:

例如'1011011101011'B的编码


V中第一个八位取值0-7,表示在这个V后面补的0的个数

如果BITSTRING的值为空,则编码时,长度为1,补充的八位组为全0。


(2).constructed方式

'1011011101011'B的编码


L的最高位是1,说明是不定长方式编码,V中嵌套两个TLV,Primitive方式表示这个值的高八位和低八位,最后以两个全为0的八位结尾

7.OCTET STRING

BITSTRING类似,但是不需要增加表征补充位个数的八位组。

8.OBJECT IDENTIFIER

只能以primitive方式编码。编码时,第一个八位组采用公式:first_arc* 40+second_arc如果一个数大于127,就采用多个8位表示,最高位用1表示后续还有octet,0表示后续没有。

例:对{isomember-body f(250) type-org(1) ft(16) asn1-book(9)}的编码为


注意对250的编码方式,1×128+122

9.RELATIVE-OID

OBJECTIDENTIFIER类似,但是不需要对注册树前两段进行特殊处理。


10.字符串和日期

OCTETSTRING编码类似,只是Tag不同。

11.SEQUENCE只能用constructed形式。对每个成员均以TLV方式编码,且顺序要与定义的一致。

v SEQUENCE { age INTEGER, single BOOLEAN } ::={age 24, single TRUE }的编码为:


注意BOOLEAN类型,非0表示真。

忽略扩展符'...',对扩展的内容则按顺序编码。

12.SET

SEQUENCE类似,但是成员顺序有发送者决定。

13.SEQUENCEOF

SEQUENCEOfTagSEQUENCE相同,编码规则也相同。

14.SETOF

SEQUENCEOf类似。

15.CHOICE

严格说CHOICE类型在编码中并不存在,只是在描述中体现一种关系。编码时,是按照具体被选择的成员编码规则编码的。

例如amousCHOICE { name VisibleString, nobody NULL } ::= name:"Perec"

的编码为:


Tag26,上选定成员的类型的tag

如果CHOICE类型显式(EXPLICIT)指定了Tag,那么该Tag应当以constructed方式编码。

忽略扩展符'...',对扩展的内容则按顺序编码。

16.Tag对编码的影响

(1).如果Tag是隐式的,或者在模块定义中声明了IMPLICITTAGS或者AUTOMATICTAGS,则只有关键字IMPLICIT左侧的Tag才会被编码。如:

v [1]IMPLICIT INTEGER ::= -38的编码为:


(2).如果一个类型的Tag是显式(EXPLICIT)的(或者在模块定义中声明了EXPLICITTAGS),则要以constructed方式编码三元组系列。

如v[APPLICATION 0] EXPLICIT INTEGER ::= 38的编码为:


17.子类型约束

子类型约束是在BER编码规则之后被引入ASN.1的,在编码规则中不能体现约束。

18.EXTERNAL

该类型不推荐使用,略。

19.INSTANCEOF

定义为:

vINSTANCE OF TYPE-IDENTIFIER ::=

{

type-id{iso member-body f(250) type-org(1) ft(16) asn1-book(9)

chapter18(5)integer- type(0)},


valueINTEGER:5

}

的编码应当和如下一个SEQUENCE类型的值类似:

{

direct-reference{iso member-body f(250) type-org(1) ft(16) asn1-book(9)

chapter18(5)integer-type(0)},

encodingsingle-ASN1-type:INTEGER:5

}

编码为:


为什么valueINTEGER:5的编码采用constructed方式,处层TLVTag为上下文类型,值为0

20.EMBEDDEDPDV

EMBEDDEDPDV的编码和其等效的SEQUENCE结构编码类似。其嵌入部分的编码,应该是遵循identification成员指定的规则。

21.CHARACTERSTRING

CHARACTERSTRING的编码和其等效的SEQUENCE结构编码类似。其嵌入字符串部分的编码,应该是遵循identification成员指定的规则。

22.InformationObjects and Object Sets

信息对象和对象集合永远都不编码。如前所述,传递他们所包含信息的途径是在值定义或者类型定义中引用他们。这样的结果,不是在编码中出现了这些信息,而是ASN.1编译器在生成编解码器时,会按照这些信息生成相应的约束表。

23.ValueSet

值集合的编码按照对应类型的编码规则进行。

.BER编码规则的属性

1.BER编码规则是机器无关

2.BER的传输语法是十分冗长。,TL很多情况下都是可以省略,但是这种冗余信息能很好的保藏抽象语法结构。

3.BER传输语法能容易升级而且向上兼容。如每个类型都可以用CHOICE来代替

以下面的命令为例子

samCommandGetVersionInfo (0x02)
Description: Requests the version string from the SIO Processor.
Valid States: Issue this command only when the iCLASS SE Reader Module is in the IDLE
state. See Section 2.3 Internal States of Operation, page 13.
State Change: None.
6.3.1  Request
A0 02 82 00
Value  Description  ASN.1 Encoding Notes
0xA0  Tag = SamCommand (0x00)   Constructed Type => 0xA0
0x02  Len = 2
0x82  Tag = GetVersionInfo (0x02)  Primitive Type => 0x82
0x00  Len = 0
VALUE = NULL ( No Parameters)



3. Basic Encoding Rules

The Basic Encoding Rules for ASN.1, abbreviated BER, giveone or more ways to represent any ASN.1 value as an octetstring. (There are certainly other ways to represent ASN.1values, but BER is the standard for interchanging suchvalues in OSI.)

There are three methods to encode an ASN.1 value under BER,the choice of which depends on the type of value and whetherthe length of the value is known. The three methods areprimitive, definite-length encoding; constructed, definite-length encoding; and constructed, indefinite-lengthencoding. Simple non-string types employ the primitive,definite-length method; structured types employ either ofthe constructed methods; and simple string types employ anyof the methods, depending on whether the length of the valueis known. Types derived by implicit tagging employ themethod of the underlying type and types derived by explicittagging employ the constructed methods.

In each method, the BER encoding has three or four parts:

Identifier octets. These identify the class and tag number of the ASN.1 value, and indicate whether the method is primitive or constructed.

Length octets. For the definite-length methods, these give the number of contents octets. For the constructed, indefinite-length method, these indicate that the length is indefinite.

Contents octets. For the primitive, definite-length method, these give a concrete representation of the value. For the constructed methods, these give the concatenation of the BER encodings of the components of the value.

End-of-contents octets. For the constructed, indefinite- length method, these denote the end of the contents. For the other methods, these are absent.

The three methods of encoding are described in the followingsections.

3.1 Primitive, definite-length method

This method applies to simple types and types derived fromsimple types by implicit tagging. It requires that thelength of the value be known in advance. The parts of theBER encoding are as follows:

Identifier octets. There are two forms: low tag number (fortag numbers between 0 and 30) and high tag number (for tagnumbers 31 and greater).

Low-tag-number form. One octet. Bits 8 and 7 specify the class (see Table 2), bit 6 has value "0," indicating that the encoding is primitive, and bits 5-1 give the tag number.

ClassBit 8Bit 7
universal00
application01
context-specific10
private11

Table 2. Class encoding in identifier octets.

High-tag-number form. Two or more octets. First octet is as in low-tag-number form, except that bits 5-1 all have value "1." Second and following octets give the tag number, base 128, most significant digit first, with as few digits as possible, and with the bit 8 of each octet except the last set to "1."

Length octets. There are two forms: short (for lengthsbetween 0 and 127), and long definite (for lengths between 0and 2^1008 -1).

Short form. One octet. Bit 8 has value "0" and bits 7-1 give the length.

Long form. Two to 127 octets. Bit 8 of first octet has value "1" and bits 7-1 give the number of additional length octets. Second and following octets give the length, base 256, most significant digit first.

Contents octets. These give a concrete representation of thevalue (or the value of the underlying type, if the type isderived by implicit tagging). Details for particular typesare given in Section 5.

3.2 Constructed, definite-length method

This method applies to simple string types, structuredtypes, types derived simple string types and structuredtypes by implicit tagging, and types derived from anythingby explicit tagging. It requires that the length of thevalue be known in advance. The parts of the BER encoding areas follows:

Identifier octets. As described in Section 3.1, except thatbit 6 has value "1," indicating that the encoding isconstructed.

Length octets. As described in Section 3.1.

Contents octets. The concatenation of the BER encodings ofthe components of the value:

  • For simple string types and types derived from them by implicit tagging, the concatenation of the BER encodings of consecutive substrings of the value (underlying value for implicit tagging).

  • For structured types and types derived from them by implicit tagging, the concatenation of the BER encodings of components of the value (underlying value for implicit tagging).

  • For types derived from anything by explicit tagging, the BER encoding of the underlying value.

Details for particular types are given in Section 5.

3.3 Constructed, indefinite-length method

This method applies to simple string types, structuredtypes, types derived simple string types and structuredtypes by implicit tagging, and types derived from anythingby explicit tagging. It does not require that the length ofthe value be known in advance. The parts of the BER encodingare as follows:

Identifier octets. As described in Section 3.2.

Length octets. One octet, 80.

Contents octets. As described in Section 3.2.

End-of-contents octets. Two octets, 00 00.

Since the end-of-contents octets appear where an ordinaryBER encoding might be expected (e.g., in the contents octetsof a sequence value), the 00 and 00 appear as identifier andlength octets, respectively. Thus the end-of-contents octetsis really the primitive, definite-length encoding of a valuewith universal class, tag number 0, and length 0.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值