an introduction to abstract syntax notation 1 (asn.1)

28
SDS Foil no 1 An Introduction to Abstract Syntax Notation 1 (ASN.1) Steinar Andresen/Rolv Bræk/Finn Arve Aagesen Norwegian University of Science and Technology, Trondheim

Upload: abdalla

Post on 18-Jan-2016

54 views

Category:

Documents


1 download

DESCRIPTION

An Introduction to Abstract Syntax Notation 1 (ASN.1). Steinar Andresen/Rolv Bræk/Finn Arve Aagesen Norwegian University of Science and Technology, Trondheim. The need of a “common language” In order to cooperate the following needs arise. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 1

An Introduction to Abstract Syntax Notation 1 (ASN.1)

Steinar Andresen/Rolv Bræk/Finn Arve Aagesen

Norwegian University of Science and Technology, Trondheim

Page 2: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 2

The need of a “common language”In order to cooperate the following needs arise

A shared interpretation of what the data means and a common protocol for interchange

•A shared view on data structure The ASN.1 Notation

•Transfer coding rulesThe ASN.1 Encoding schemes: BER, PER, ..

A shared interpretation of what the data means and a common protocol for interchange

•A shared view on data structure The ASN.1 Notation

•Transfer coding rulesThe ASN.1 Encoding schemes: BER, PER, ..

LAN - Org A

LAN Org C

LAN Org B

PUBLIC

WAN

Page 3: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 3

The history of ASN.1

General purpose notation and encoding scheme: • Developed to be applied to the MHS protocols (X.400) (“born”

in 1982)

• Generalised as general tools ISO 8824 Notation and ISO 8825 Encoding rules in 1990, Revised in 1995

• General use in many application fields today

General purpose notation and encoding scheme: • Developed to be applied to the MHS protocols (X.400) (“born”

in 1982)

• Generalised as general tools ISO 8824 Notation and ISO 8825 Encoding rules in 1990, Revised in 1995

• General use in many application fields today

Page 4: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 4

The original “realm” of ASN

• Applied first to the Presentation layer in the “Open Systems Interconnection”.

• Soon used to define the protocols of the Applications layer:FTAM, ROSE, MHS, etc.

• Also used for lower layers

• Applied first to the Presentation layer in the “Open Systems Interconnection”.

• Soon used to define the protocols of the Applications layer:FTAM, ROSE, MHS, etc.

• Also used for lower layers

Application

Presentation

Session

Transport

Network

Datalink

Physical

OSI Protocol Stack

Page 5: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 5

Why ASN.1 and BER?

Applicat ion

Presentation

Session

Transport

Network

Datalink

Physical

OSI Protocol Stack

on

s

Applicat ion

Presentation

Session

Transport

Network

Datalink

Physical

OSI Protocol Stack

ASN.1for the data syntax

BERfor the (sequential)transfer syntax

PDU PDU

Page 6: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 6

ASN.1 Use

ASN.1

BER

PDU PDU

Encode Decode

ASN.1

A PDU can be a complex element (letter, document, …) and:

• specified using datatypes of SDL, LOTOS, UML, ...

• implemented using datatypes of CHILL, C++, Java, ...

ASN.1 provides a language independent syntax and ASN.1 compilers take care of the mapping

A PDU can be a complex element (letter, document, …) and:

• specified using datatypes of SDL, LOTOS, UML, ...

• implemented using datatypes of CHILL, C++, Java, ...

ASN.1 provides a language independent syntax and ASN.1 compilers take care of the mapping

0 1 | 0 | 1 | 0 | 0 | 1 | 1

C++, Java, SDL...

C++, Java,...

C++, Java, SDL...

C++, Java,...

Page 7: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 7

ASN.1 Module

• A module is a set of ASN.1 definitions assembled for a specific purpose.

• The structure of a module is:

ModuleDefinition::= ModuleIdentifier DEFINITIONSDEFINITIONS::= BEGIN

ModuleBodyEND

• A module is a set of ASN.1 definitions assembled for a specific purpose.

• The structure of a module is:

ModuleDefinition::= ModuleIdentifier DEFINITIONSDEFINITIONS::= BEGIN

ModuleBodyEND

Page 8: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 8

ASN.1 Module Identifier

• ModuleIdentifer is an element of type Object Identifiers

• Object Identifiers are adminstrered by ISO, ITU-T, etc. A Module Identifier represents an official reference to the Module.

• ModuleIdentifer is an element of type Object Identifiers

• Object Identifiers are adminstrered by ISO, ITU-T, etc. A Module Identifier represents an official reference to the Module.

Page 9: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 9

Module Body

ModuleBody::= Exports -- definitions that may be exported to other modules Imports -- definitions that are imported from other modules AssignmentList --this modules definitions

| -- “|” means “or”

empty

ModuleBody::= Exports -- definitions that may be exported to other modules Imports -- definitions that are imported from other modules AssignmentList --this modules definitions

| -- “|” means “or”

empty

Page 10: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 10

Assignments

WheatherReport ::= SEQUENCE{ ......}

Name of a type reference to defined type

sampleWReport WheatherReport::= { ......}

Name of a value The type of this value Actual value spec.

Type assignment

Value assignment

Page 11: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 11

Type assignment

A type assignment has three syntactic elements:

• the type reference (the name allocated to it),

• the symbol “::=“ (means defined as) and

• the appropriate type notation

A type assignment has three syntactic elements:

• the type reference (the name allocated to it),

• the symbol “::=“ (means defined as) and

• the appropriate type notation

WheatherReport ::= SEQUENCE

{

stationNumber INTEGER {1..99999}

timeOfReport UTCTime

.........

}

Page 12: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 12

Value assignment

A value assignment has four syntactic elements: • the value reference (the name allocated),

• the type to which the value belongs

• the symbol “::=“ (means defined as) and

• the appropriate value notation

A value assignment has four syntactic elements: • the value reference (the name allocated),

• the type to which the value belongs

• the symbol “::=“ (means defined as) and

• the appropriate value notation

Sample value assignment:

sampleReport WheatherReport ::=

{

stationNumber 73290

timeOfReport “900102125703Z”

.........

}

Page 13: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 13

Sequence

Letter ::= SEQUENCE {

opening OCTETSTRING,

body OCTETSTRING,

closing OCTETSTRING,

address AddressType

}

AddressType ::= SEQUENCE {

name OCTETSTRING,

number INTEGER,

street OCTETSTRING,

postOffice OCTETSTRING,

state OCTETSTRING,

zipCode INTEGER

}

Letter ::= SEQUENCE {

opening OCTETSTRING,

body OCTETSTRING,

closing OCTETSTRING,

address AddressType

}

AddressType ::= SEQUENCE {

name OCTETSTRING,

number INTEGER,

street OCTETSTRING,

postOffice OCTETSTRING,

state OCTETSTRING,

zipCode INTEGER

}

Page 14: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 14

Optional

AddressType ::= SEQUENCE {

name OCTETSTRING,

number INTEGER,

street OCTETSTRING,

apartNumber INTEGER OPTIONAL,

postOffice OCTETSTRING,

state OCTETSTRING,

zipCode INTEGER

}

AddressType ::= SEQUENCE {

name OCTETSTRING,

number INTEGER,

street OCTETSTRING,

apartNumber INTEGER OPTIONAL,

postOffice OCTETSTRING,

state OCTETSTRING,

zipCode INTEGER

}

Page 15: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 15

Tags

Letter ::= SEQUENCE {

opening OCTETSTRING,

body OCTETSTRING,

closing OCTETSTRING,

receiverAddr AddressType OPTIONAL,

senderAddr AddressType OPTIONAL

}

Letter ::= SEQUENCE {

opening OCTETSTRING,

body OCTETSTRING,

closing OCTETSTRING,

receiverAddr [0] AddressType OPTIONAL,

senderAddr [1] AddressType OPTIONAL

}

Letter ::= SEQUENCE {

opening OCTETSTRING,

body OCTETSTRING,

closing OCTETSTRING,

receiverAddr AddressType OPTIONAL,

senderAddr AddressType OPTIONAL

}

Letter ::= SEQUENCE {

opening OCTETSTRING,

body OCTETSTRING,

closing OCTETSTRING,

receiverAddr [0] AddressType OPTIONAL,

senderAddr [1] AddressType OPTIONAL

}

Page 16: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 16

Spelling Conventions

• Reserved words: only CAPITAL letters (INTEGER, CHOICE,…)

• Types: first letter Capital (TransactionID)

• Value reference: first letter Lowercase (application(0))

• Identifier: first letter Lowercase (destination, responder)

• Macro: only CAPITAL letters

• Reserved words: only CAPITAL letters (INTEGER, CHOICE,…)

• Types: first letter Capital (TransactionID)

• Value reference: first letter Lowercase (application(0))

• Identifier: first letter Lowercase (destination, responder)

• Macro: only CAPITAL letters

Page 17: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 17

ASN.1 Tagged Types

Four classes:

1. UNIVERSAL (ASN.1 In-build types)

2. Application specific types (non-ambiguous within module)

3. Context specific types (non-ambiguous within construct)

3. Private (non-ambiguous within some ”enterprise”)

UNIVERSAL:

• Simple types (BOOLEAN, INTEGER, ENUMERATED, …)

• Constructor types (SEQUENCE, SEQUENCE OF, SET, SETOF)

• Additional predefined (NumericString, Teletxstring, GeneralizedTime, …)

• OBJECT IDENTIFIER

• Tagging is needed to recognise the types in the transfer syntaxt (when decoding a received PDU)

Four classes:

1. UNIVERSAL (ASN.1 In-build types)

2. Application specific types (non-ambiguous within module)

3. Context specific types (non-ambiguous within construct)

3. Private (non-ambiguous within some ”enterprise”)

UNIVERSAL:

• Simple types (BOOLEAN, INTEGER, ENUMERATED, …)

• Constructor types (SEQUENCE, SEQUENCE OF, SET, SETOF)

• Additional predefined (NumericString, Teletxstring, GeneralizedTime, …)

• OBJECT IDENTIFIER

• Tagging is needed to recognise the types in the transfer syntaxt (when decoding a received PDU)

Page 18: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 18

Universal Types

Universal Tag ASN.1 Type

• 1 BOOLEAN

• 2 INTEGER

• 3 BITSTRING

• 4 OCTETSTRING

• 5 NULL

• 6 OBJECTIDENTIFIER

• 7 ObjectDescriptor

• 8 EXTERNAL

• 9 REAL

• 10 ENUMERATED

• 11-15 reserved for addenda

• 16 SEQUENCE, SEQUENCE OF

• 17 SET, SET OF

• … ...

Universal Tag ASN.1 Type

• 1 BOOLEAN

• 2 INTEGER

• 3 BITSTRING

• 4 OCTETSTRING

• 5 NULL

• 6 OBJECTIDENTIFIER

• 7 ObjectDescriptor

• 8 EXTERNAL

• 9 REAL

• 10 ENUMERATED

• 11-15 reserved for addenda

• 16 SEQUENCE, SEQUENCE OF

• 17 SET, SET OF

• … ...

Page 19: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 19

ExampleExample DEFINITIONS::= BEGIN

TransactionID ::= INTEGER

Component ::= INTEGER {application(0), networkManager(1),systemManager(2)}

Request ::= PrintableString

EntityIdentifier ::= [PRIVATE 1] IMPLICIT OCTETSTRING

RequestPDU ::= SEQUENCE {responseRequired BOOLEAN, TransactionID, requestor EntityIdentifier,request CHOICE {

command [0] IMPLICIT SET {destination Component, Request}

shutdown [1] IMPLICIT NULL}}

ResponsePDU ::= SEQUENCE {TransactionID, responder EntityIdentifier OPTIONAL, result INTEGER {success(0), failure(1)}}

END

Example DEFINITIONS::= BEGIN

TransactionID ::= INTEGER

Component ::= INTEGER {application(0), networkManager(1),systemManager(2)}

Request ::= PrintableString

EntityIdentifier ::= [PRIVATE 1] IMPLICIT OCTETSTRING

RequestPDU ::= SEQUENCE {responseRequired BOOLEAN, TransactionID, requestor EntityIdentifier,request CHOICE {

command [0] IMPLICIT SET {destination Component, Request}

shutdown [1] IMPLICIT NULL}}

ResponsePDU ::= SEQUENCE {TransactionID, responder EntityIdentifier OPTIONAL, result INTEGER {success(0), failure(1)}}

END

Page 20: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 20

A Value assignment example

aRequestPDU RequestPDU ::= {ResponseRequired TRUE, 45, requestor “MySystem”,request {

command {destination systemManager, ‘status’}

}}

aResponsePDU ResponsePDU::= {45, result INTEGER {success}}

aRequestPDU RequestPDU ::= {ResponseRequired TRUE, 45, requestor “MySystem”,request {

command {destination systemManager, ‘status’}

}}

aResponsePDU ResponsePDU::= {45, result INTEGER {success}}

Page 21: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 21

ASN.1 Transfer Syntax

• Basic Encoding Rules BER

• Packed Encoding Rules PER

• Canonical Encoding Rules CER

• Disting. Encoding Rules DER

• Basic Encoding Rules BER

• Packed Encoding Rules PER

• Canonical Encoding Rules CER

• Disting. Encoding Rules DER

• Defines how to encode the values before sending over the line

• Basically it is a Type, Length, Value encoding scheme with one or more octets for each of the fields.

• Defines how to encode the values before sending over the line

• Basically it is a Type, Length, Value encoding scheme with one or more octets for each of the fields.

Type Length Value

identified by tagging

Page 22: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 22

TLV encoding scheme

Octet 1 Octet 2 Octet 3 Octet n...

bit8 bit 7 ...bit1

TypeLengthTypeLengthValueTypeLengthValue…

DataElement

DataElement

DataElement

Page 23: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 23

ASN.1 Transfer Syntax

T A G F I E L D (=Type identifier field)

| 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |

f numberclass

Class:

• 00 UNIVERSAL

• 01 APPLICATION

• 10 Context specific

• 11 Private

f:

• 0 primitive encoding

• 1 constructed encoding

Class:

• 00 UNIVERSAL

• 01 APPLICATION

• 10 Context specific

• 11 Private

f:

• 0 primitive encoding

• 1 constructed encoding

number:

• <31 directly

• >=31 as below:

number:

• <31 directly

• >=31 as below:

class f 11111 1 … 1 … 0 …

last

...

Page 24: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 24

Examples

• BOOLEAN: UNIVERSAL 1 = 00000001 hex

• INTEGER: UNIVERSAL 2 = 00000002 hex

• SEQUENCE: UNIVERSAL 16 constructed = 00110000 = 30 hex

• [PRIVATE 35] IMPLICIT INTEGER = 11011111 00100011

• BOOLEAN: UNIVERSAL 1 = 00000001 hex

• INTEGER: UNIVERSAL 2 = 00000002 hex

• SEQUENCE: UNIVERSAL 16 constructed = 00110000 = 30 hex

• [PRIVATE 35] IMPLICIT INTEGER = 11011111 00100011

|c la s s | f | n u m b e r |

0 | 0 0 0 | 0 | 0 | 0 | 1

B O O L E A N

|c la s s | f | n u m b e r |

0 | 0 0 0 | 0 | 0 | 1 | 0

I N T E G E R

Page 25: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 25

Length field

Definite form

• short: length < 128

• long: length > 128

Indefinite form: only for

constructed types

Definite form

• short: length < 128

• long: length > 128

Indefinite form: only for

constructed types

0 <length>

1 <length of length field>

<length field> <length field>...

10000000 <value> 00000000 00000000... <value>

Page 26: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 26

VALUE FIELDS

| contents |

0 | 0 | 0 | 0 | 0 |0 | 0 | 0

| length |

0 | 0 | 0 | 0 |0 | 0 | 0 | 1

|class| f | number |

0 | 0 0 0 | 0 | 0 | 0 | 1

BOOLEAN

Ex. FALSE

(TRUE with content value different from 0)

| contents |

0 | 1 | 1 | 0 | 0 |1 | 0 | 0

| length |

0 | 0 | 0 | 0 |0 | 0 | 0 | 1

|class| f | number |

0 | 0 0 0 | 0 | 0 | 1 | 0

INTEGER

Ex. 100 (decimal)

Page 27: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 27

How to encode

RequestPDU ::= SEQUENCE {responseRequired BOOLEAN, TransactionID, requestor EntityIdentifier,request CHOICE {

command [0] IMPLICIT SET {destination Component, Request}

shutdown [1] IMPLICIT NULL}}

where:

ResponseRequired = FALSE, TransactionID = 10, requestor = “MySystem”,request = command {

destination = networkManager,

Request = ‘status’}

??

RequestPDU ::= SEQUENCE {responseRequired BOOLEAN, TransactionID, requestor EntityIdentifier,request CHOICE {

command [0] IMPLICIT SET {destination Component, Request}

shutdown [1] IMPLICIT NULL}}

where:

ResponseRequired = FALSE, TransactionID = 10, requestor = “MySystem”,request = command {

destination = networkManager,

Request = ‘status’}

??

Page 28: An Introduction to Abstract Syntax Notation 1 (ASN.1)

SDSFoil no 28

Encoding summary

Universal Tag ASN.1 Type

• 1 BOOLEAN

• 2 INTEGER

• 3 BITSTRING

• 4 OCTETSTRING

• 5 NULL

• 6OBJECTIDENTIIER

• 7 ObjectDescriptor

• 8 EXTERNAL

• 9 REAL

• 10 ENUMERATED

• 11-15 reserved for addenda

• 16 SEQUENCE, SEQUENCE OF

• 17 SET, SET OF

• … ...

Universal Tag ASN.1 Type

• 1 BOOLEAN

• 2 INTEGER

• 3 BITSTRING

• 4 OCTETSTRING

• 5 NULL

• 6OBJECTIDENTIIER

• 7 ObjectDescriptor

• 8 EXTERNAL

• 9 REAL

• 10 ENUMERATED

• 11-15 reserved for addenda

• 16 SEQUENCE, SEQUENCE OF

• 17 SET, SET OF

• … ...

Class:

• 00 UNIVERSAL

• 01 APPLICATION

• 10 Context specific

• 11 Private

f:

• 0 primitive encoding

• 1 constructed encoding

Class:

• 00 UNIVERSAL

• 01 APPLICATION

• 10 Context specific

• 11 Private

f:

• 0 primitive encoding

• 1 constructed encoding

| 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |

f numberclass

number:

• <31 directly

• >=31 as below:

number:

• <31 directly

• >=31 as below:

class f 11111 1 … 1 … 0 … ...