Question

I have a problem with encoding/decoding a structure types containing a CHOICE-within-a-CHOICE construction. Simplest example is below.

ASN1 File:

TEST {}
DEFINITIONS IMPLICIT TAGS ::=
BEGIN

Test_ch1 ::= CHOICE {
 t1 INTEGER
}

Test_ch2 ::= CHOICE {
 t2 Test_ch1
}

END

Source code:

#include <stdio.h>
#include <libtasn1.h>

int main()
{
 int result = 0;
 asn1_node definitions = NULL, node = NULL;
 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
 const char * treefile = ASN1_FILE;

 char data[1024];
 int data_size = sizeof(data);

 asn1_parser2tree (treefile, &definitions, errorDescription);
 asn1_create_element (definitions, "TEST.Test_ch2", &node );
 asn1_write_value (node, "", "t2", 1);
 asn1_write_value (node, "t2", "t1", 1);
 asn1_write_value (node, "t2.t1", "1234", 0);
 asn1_der_coding (node, "", data, &data_size, errorDescription);

 asn1_create_element (definitions, "TEST.Test_ch2", &node );
 result = asn1_der_decoding (&node, data, data_size, errorDescription);
 if(result != ASN1_SUCCESS) { asn1_perror (result); printf("Decoding error = \"%s\"", errorDescription); return -1; }

 return 0;
}

Encoding is fine, encoded data is: 02 02 04 d2 for t1 = 1234. But decoding fails.

The result of asn1_der_decoding(...) is ASN1_DER_ERROR, errorDescription is empty.

Exactly same error does appear when construction like Test_ch2 is inside other structure (SET, SEQUENCE or whatever), for example a GeneralName from RFC 5280 (in case of Name choice).

Why does it reject its own encoded data? What is wrong here?

Was it helpful?

Solution 2

Good News, Everyone! This problem finally proved to be a bug as they just fixed this in their git repository. And note: in less than a year. :)

Thanks to Nikos Mavrogiannopoulos.

OTHER TIPS

Consider using lionet.info/asn1c

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top