Changes
Page history
wiki asn1rt: add info on how to generate binary structures
authored
Nov 16, 2017
by
mitshell
Show whitespace changes
Inline
Side-by-side
Using-the-pycrate-asn1-runtime.md
View page @
8363d5d4
...
...
@@ -63,6 +63,283 @@ by the runtime:
-
EXTERNAL, EMBEDDED PDV and CHARACTER STRING are just defined like SEQUENCE objects
## Generating binary structures
While encoding and decoding ASN.1 values, we are converting abstract values to bytes'
buffer and vice versa: we will see in the several examples below how to use the
*to_[bcdp]er()*
and
*from_[bcdp]er()*
methods to make such encoding, respectively decoding.
There is also the possibility to generate the exact binary structures corresponding
to those encoding and decoding, making use of basic pycrate core objects (mostly Int,
Uint, Buf and Envelope). This is done by adding the
*_ws*
suffixes to encoding and decoding
methods:
*to_[bcdp]er_ws()*
and
*from_[bcdp]er()*
.
This will generate a master Envelope object under the attribute
*_struct*
of the encoded
or decoded object, which will demonstrate the exact structure of the transfer syntax.
Here is an example with a TCAP-MAP case:
>>> from pycrate_asn1dir import TCAP_MAP
>>> M = TCAP_MAP.TCAP_MAP_Messages.TCAP_MAP_Message
>>> M
<TCAP-MAP-Message ([TCMessage] CHOICE)>
>>> from binascii import hexlify, unhexlify
>>> # this is the way to go for a simple decoding:
>>> M.from_ber(unhexlify('626a48042f3b46026b3a2838060700118605010101a02d602b80020780a109060704000001001302be1a2818060704000001010101a00da00b80099656051124006913f66c26a12402010102013b301c04010f040eaa180da682dd6c31192d36bbdd468007917267415827f2'))
>>> # this is the way to go if we want to additionnally generate the binary structure:
>>> M.from_ber_ws(unhexlify('626a48042f3b46026b3a2838060700118605010101a02d602b80020780a109060704000001001302be1a2818060704000001010101a00da00b80099656051124006913f66c26a12402010102013b301c04010f040eaa180da682dd6c31192d36bbdd468007917267415827f2'))
>>> M() # we have the decoded value, as usual
(u'begin', {u'dialoguePortion': {u'direct-reference': (0, 0, 17, 773, 1, 1, 1), u'encoding': (u'single-ASN1-type', ('DialoguePDU', (u'dialogueRequest', {u'user-information': [{u'direct-reference': (0, 4, 0, 0, 1, 1, 1, 1), u'encoding': (u'single-ASN1-type', ('MAP-DialoguePDU', (u'map-open', {u'destinationReference': '\x96V\x05\x11$\x00i\x13\xf6'})))}], u'application-context-name': (0, 4, 0, 0, 1, 0, 19, 2), u'protocol-version': (1, 1)})))}, u'components': [(u'basicROS', (u'invoke', {u'opcode': (u'local', 59), u'argument': ('USSD-Arg', {u'ussd-DataCodingScheme': '\x0f', u'msisdn': "\x91rgAX'\xf2", u'ussd-String': '\xaa\x18\r\xa6\x82\xddl1\x19-6\xbb\xddF'}), u'invokeId': (u'present', 1)}))], u'otid': '/;F\x02'})
>>> print(M.to_asn1())
begin : {
otid '2F3B4602'H,
dialoguePortion {
direct-reference {0 0 17 773 1 1 1} -- dialogue-as-id --,
encoding single-ASN1-type : DialoguePDU: dialogueRequest : {
protocol-version '1'B -- version1 --,
application-context-name {0 4 0 0 1 0 19 2} -- networkUnstructuredSsContext-v2 --,
user-information {
{
direct-reference {0 4 0 0 1 1 1 1} -- map-DialogueAS --,
encoding single-ASN1-type : MAP-DialoguePDU: map-open : {
destinationReference '9656051124006913F6'H
}
}
}
}
},
components {
basicROS : invoke : {
invokeId present : 1,
opcode local : 59,
argument USSD-Arg: {
ussd-DataCodingScheme '0F'H,
ussd-String 'AA180DA682DD6C31192D36BBDD46'H,
msisdn '917267415827F2'H
}
}
}
}
>>> M._struct # we have additionnally the binary structure
<TCAP-MAP-Message : <T : <Class : 1 (APPLICATION)> [...] >
>>> print(M._struct.show())
### TCAP-MAP-Message ###
### T ###
<Class : 1 (APPLICATION)>
<PC : 1 (constructed)>
<Val : 2>
### L ###
<Form : 0 (short)>
<Val : 106>
### V ###
### otid ###
### T ###
<Class : 1 (APPLICATION)>
<PC : 0 (primitive)>
<Val : 8>
### L ###
<Form : 0 (short)>
<Val : 4>
<V : 0x2f3b4602>
### dialoguePortion ###
### T ###
<Class : 1 (APPLICATION)>
<PC : 1 (constructed)>
<Val : 11>
### L ###
<Form : 0 (short)>
<Val : 58>
### V ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 1 (constructed)>
<Val : 8 (INSTANCE OF, EXTERNAL)>
### L ###
<Form : 0 (short)>
<Val : 56>
### V ###
### direct-reference ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 6 (OBJECT IDENTIFIER)>
### L ###
<Form : 0 (short)>
<Val : 7>
<V : '\x00\x11\x86\x05\x01\x01\x01'>
### encoding ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 1 (constructed)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 45>
### Type ###
### T ###
<Class : 1 (APPLICATION)>
<PC : 1 (constructed)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 43>
### V ###
### protocol-version ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 0 (primitive)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 2>
### V ###
<BU : 7>
<BS : 0x80>
### application-context-name ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 1 (constructed)>
<Val : 1>
### L ###
<Form : 0 (short)>
<Val : 9>
### V ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 6 (OBJECT IDENTIFIER)>
### L ###
<Form : 0 (short)>
<Val : 7>
<V : '\x04\x00\x00\x01\x00\x13\x02'>
### user-information ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 1 (constructed)>
<Val : 30>
### L ###
<Form : 0 (short)>
<Val : 26>
### V ###
### _item_ ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 1 (constructed)>
<Val : 8 (INSTANCE OF, EXTERNAL)>
### L ###
<Form : 0 (short)>
<Val : 24>
### V ###
### direct-reference ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 6 (OBJECT IDENTIFIER)>
### L ###
<Form : 0 (short)>
<Val : 7>
<V : '\x04\x00\x00\x01\x01\x01\x01'>
### encoding ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 1 (constructed)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 13>
### Type ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 1 (constructed)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 11>
### V ###
### destinationReference ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 0 (primitive)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 9>
<V : 0x9656051124006913f6>
### components ###
### T ###
<Class : 1 (APPLICATION)>
<PC : 1 (constructed)>
<Val : 12>
### L ###
<Form : 0 (short)>
<Val : 38>
### V ###
### _item_ ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 1 (constructed)>
<Val : 1>
### L ###
<Form : 0 (short)>
<Val : 36>
### V ###
### invokeId ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 2 (INTEGER)>
### L ###
<Form : 0 (short)>
<Val : 1>
<V : 1>
### opcode ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 2 (INTEGER)>
### L ###
<Form : 0 (short)>
<Val : 1>
<V : 59>
### argument ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 1 (constructed)>
<Val : 16 (SEQUENCE, SEQUENCE OF)>
### L ###
<Form : 0 (short)>
<Val : 28>
### V ###
### ussd-DataCodingScheme ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 4 (OCTET STRING)>
### L ###
<Form : 0 (short)>
<Val : 1>
<V : 0x0f>
### ussd-String ###
### T ###
<Class : 0 (UNIVERSAL)>
<PC : 0 (primitive)>
<Val : 4 (OCTET STRING)>
### L ###
<Form : 0 (short)>
<Val : 14>
<V : 0xaa180da682dd6c31192d36bbdd46>
### msisdn ###
### T ###
<Class : 2 (CONTEXT-SPECIFIC)>
<PC : 0 (primitive)>
<Val : 0>
### L ###
<Form : 0 (short)>
<Val : 7>
<V : 0x917267415827f2>
You should not use those extended methods for standard ASN.1 processing, but only
for debugging purpose, as they render the encoding and decoding process much slower.
You have been warned !
## The UMTS and LTE RRC protocols
### ASN.1 definition and modules
...
...
...
...