一,、介紹 Abstract Syntax Notation One (ASN.1)是一種獨立于機器的描述語言,,用于描述在網(wǎng)絡上傳遞的消息 標準包括: ISO 8824-1 | ITU-T X.680: Specification of basic notation, ISO 8824-2 | ITU-T X.681: Information object specification, ISO 8824-3 | ITU-T X.682: Constraint specification, ISO 8824-4 | ITU-T X.683: Parameterization of ASN.1 ASN.1特別適合表示現(xiàn)代通信應用中那些復雜的,、變化的及可擴展的數(shù)據(jù)結構 ASN.1 可分為兩個部分 語法規(guī)則:從數(shù)據(jù)類型,、內容順序或結構等方面來描述消息的內容 編碼規(guī)則:如何編碼實際消息中的數(shù)據(jù) 特點: 表達簡單和復雜類型的能力 可對類型根據(jù)大小及(或)數(shù)值進行約束 也可以施加更強的約束 字段可標記為 OPTIONAL 大寫開頭表示類型名,,小寫開頭的表示變量名/字段名 二,、ASN.1術語 1.抽象語法(Abstract Syntax) 描述通用數(shù)據(jù)結構 允許定義數(shù)據(jù)類型和值 2.數(shù)據(jù)類型(Data Type) 值的集合,可以是簡單類型或結構類型 可以對數(shù)據(jù)類型命名 3.編碼(Encoding) 用于表示數(shù)據(jù)值的字節(jié)序列 4.編碼規(guī)則(Encoding Rules) 給出從一種語法到另一種的映射方法 5.傳輸語法(Transfer Syntax) 位模式(Bits pattern) 描述數(shù)據(jù)是在傳輸時是如何表示的 三,、ASN.1模塊定義 模塊(module):ASN.1規(guī)范中的基本構造塊 模塊定義格式如下:
<modulereference> DEFINITIONS ::= BEGIN EXPORTS IMPORTS AssignmentList END 其中:EXPORTS 這個模塊中的定義可能被其他模塊引入 IMPORTS 定義由其他模塊引入 AssignmentList 這個模塊中將定義類型分配,、值分配及宏定義
四、ASN.1簡單類型 1. 基本類型:BOOLEAN,,INTEGER,ENUMERATED,,REAL,,BIT STRING,OCTET STRING 2. 字符串類型(ISO10646-1的子集) NumericString (0-9,<space>) PrintableString (0-9,A-Z,a-z,<space>,<s[ecial> VisibleString GraphicString UTF8String IA5String (ASCII) 3. 對象類型 OBJECT IDENTIFIER ObjectDescriptor 對象標識符:一個任意長的非負整數(shù)序列,,用于標記對象(如算法等) 4. 其它類型 NULL 空值 UTCTime yymmdd hhmm[ss] <local offset from UTC> GeneralizedTime yyyymmdd hhmm[ss] <local offset from UTC> 強制從2050年開始
五,、ASN.1類型定義 語法: <type name> ::= <type> 示例: Counter ::= INTEGER IpAddress ::= OCTET STRING Months ::= ENUMERATED { january (1), february (2), march (3), april (4), may (5), june (6), july (7), august (8), september (9), october (10), november (11), december(12) } 六、ASN.1子類型定義 語法: <subtype name> ::= <type> (<constraint>) 示例: Counter ::= INTEGER (0..65536) IpAddress ::= OCTET STRING ( SIZE(4) ) Spring ::= Months (march | april | may) Summer ::= Months (june | july | august ) SmallPrime ::= INTEGER ( 2 | 3 | 5 | 7 | 11 ) ExportKey ::= BIT STRING ( SIZE(40) )
七,、ASN.1賦值 語法: <value name> <type> ::= <value> 示例: ipInReceives Counter ::= 2450 ipRouteMask IpAddress ::= ‘FFFFFF00’H currentMonth Months ::= july currentTime UTCTime ::= “030708094018+0800” name VisibleString ::= “John” married BOOLEAN ::= TRUE faxMessage BIT STRING ::= ‘01100001101’B
internet OBJECT IDENTIFIER ::= { iso(1) org(3) dod(6) 1 } private OBJECT IDENTIFIER ::= { internet 4 }
八,、ASN.1結構類型 SEQUENCE 對應于C語言中的struct 類型定義 UserAccount ::= SEQUENCE { username PrintableString, password PrintableString, accountNr INTEGER } 賦值 myAccount UserAccount ::= { username “tly”, password “guesswhat”, accountNr 2345 } SEQUENCE OF 對應于C語言中的數(shù)組 類型定義 MemberCountries ::= SEQUENCE OF PrintableString AccountRegistry ::= SEQUENCE OF UserAccount 賦值 eastAsia MemberCountries ::= { “China”, “Japan”, “Korean”, “DPR” } SET 類似于SEQUENCE,但不考慮分量順序 類型定義 UserAccount ::= SET { username [0] PrintableString, password [1] PrintableString, accountNr [2] INTEGER } 賦值 myAccount UserAccount ::= { accountNr 2345, username “tly”, password “guesswhat” } SET OF 集合類型,,每一分量類型相同,,不考慮順序 類型定義 Keywords ::= SET OF PrintableString 賦值 someASN1Keywords Keywords ::= { “INTEGER”, “BOOLEAN”, “REAL” }
|