Search code examples
parsinggsmasn.1ss7

How to decode GSM-TCAP messages using asn1c generated code


I am using the c code generated by asn1c from the TCAP protocol specification (i.e., the corresponding ASN1 files). I can successfully encode TCAP packets by the generated code. However, trying to "decode" related byte streams fails.

A sample code is as follows.

// A real byte stream of a TCAP message:
unsigned char packet_bytes[] = {
  0x62, 0x43, 0x48, 0x04, 0x00, 0x18, 0x02, 0x78,
  0x6b, 0x1a, 0x28, 0x18, 0x06, 0x07, 0x00, 0x11,
  0x86, 0x05, 0x01, 0x01, 0x01, 0xa0, 0x0d, 0x60,
  0x0b, 0xa1, 0x09, 0x06, 0x07, 0x04, 0x00, 0x00,
  0x01, 0x00, 0x14, 0x03, 0x6c, 0x1f, 0xa1, 0x1d,
  0x02, 0x01, 0x00, 0x02, 0x01, 0x2d, 0x30, 0x15,
  0x80, 0x07, 0x91, 0x64, 0x21, 0x92, 0x05, 0x31,
  0x74, 0x81, 0x01, 0x01, 0x82, 0x07, 0x91, 0x64,
  0x21, 0x00, 0x00, 0x90, 0x02
};
// Initializing ...
TCAP_TCMessage_t _pdu, *pdu = &_pdu;
memset(pdu, 0, sizeof(*pdu));    

// Decoding:
asn_dec_rval_t dec_ret = ber_decode(NULL, &asn_DEF_TCAP_TCMessage, (void **) &pdu, packet_bytes, sizeof(packet_bytes));

While the message type ("Begin", in this case), is correctly detected, but other paramters are not parsed.

Using other encoding rules, i.e., aper_decode() and uper_decode(), also fails. I would be thankful if anyone can describe how to use the auto-generated c code for decoding (parsing) a byte string of TCAP messages.


Solution

  • @Vasil, thank you very much for your answer.

    Which asn1c are you using (git commit id) and where do you get it from as there are quite a log of forks out there?

    I use the mouse07410's branch.

    How do you know that Begin is correctly detected?

    From the field present of the pdu variable that is evaluated by ber_decode (you can see the pdu type in the sample code). From the "Wireshark" output for this byte stream, I know that the correct type of the message is Begin.

    You could try compiling with -DASN_EMIT_DEBUG=1 in CFLAGS (or -DEMIT_ASN_DEBUG=1 depending on the asn1c version you are using) to get some more debug messages.

    Thanks for providing the hint; it was helpful.


    The problem was related to the asn1 files I was using. I used osmocom asn1 files and compiled them by

    ASN=../asn
    asn1c $ASN/DialoguePDUs.asn $ASN/tcap.asn $ASN/UnidialoguePDUs.asn
    

    in which, DialoguePortion is defined as follows (note that the first definition is commented):

    --DialoguePortion ::= [APPLICATION 11] EXPLICIT EXTERNAL
    
    -- WS adaptation
    DialoguePortion ::= [APPLICATION  11] IMPLICIT DialogueOC
    DialogueOC ::= OCTET STRING    
    

    To be able to decode TCAP messages, one needs to use the former definition (as is in the standard), i.e., DialoguePortion should be defined as

    DialoguePortion ::= [APPLICATION 11] EXPLICIT EXTERNAL
    

    When using this latter definition in the asn1 file, and recompiling the asn1 files, the problem solved.

    P.S.: This question is also related to my problem.