Hello,
I try to encode in der format an ASN1 CHOICE, so I wrote test program.
In my header file the choice is defined as follow :
/* GMP ::= CHOICE */
/* none INTEGER */
/* supported OCTET_STRING */
typedef struct {
int type;
union {
ASN1_INTEGER *none;
ASN1_OCTET_STRING *supported;
}d;
} GSPT_GMP;
DECLARE_ASN1_FUNCTIONS(GSPT_GMP)
DECLARE_STACK_OF(GSPT_GMP)
Below, how I implemented it in my .c file :
ASN1_CHOICE(GSPT_GMP) = {
ASN1_SIMPLE(GSPT_GMP, d.none, ASN1_INTEGER),
ASN1_SIMPLE(GSPT_GMP, d.supported,ASN1_OCTET_STRING)
} ASN1_CHOICE_END(GSPT_GMP)
IMPLEMENT_STACK_OF(GSPT_GMP)
IMPLEMENT_ASN1_FUNCTIONS(GSPT_GMP)
Then I wrote functions to create and encode der the GSPT_GMP choice :
GSPT_GMP *GSPT_GMP_create(int x, int c, char *s)
{
GSPT_GMP * tGMP = NULL;
tGMP = GSPT_GMP_new();
if(tGMP == NULL){
/* error trap */
}
switch(x){
case V_ASN1_INTEGER:
tGMP->type = V_ASN1_INTEGER;
tGMP->d.none = ASN1_INTEGER_new();
if(tGMP->d.none == NULL){
/* error trap */
}
ASN1_INTEGER_set(tGMP->d.none,c);
break;
case V_ASN1_OCTET_STRING:
tGMP->type = V_ASN1_OCTET_STRING;
tGMP->d.supported = ASN1_OCTET_STRING_new();
if(tGMP->d.supported == NULL){
/* error trap */
}
ASN1_OCTET_STRING_set(tGMP->d.supported, (const unsigned char *)s,
strlen(s));
break;
}
return tGMP;
}
It works fine.
My problems come during the encoding process. Indeed, I have no problem
for this process when it occurs on ASN1 SEQUENCE structure, but for the
CHOICE troubles come.
Below my code to der encoding :
unsigned char *GSPT_GMP_i2d(GSPT_GMP *gmp, long *len)
{
unsigned char *buf = NULL, *next;
int total =0;
*len = i2d_GSPT_GMP(gmp, NULL);
buf = next = (unsigned char *)malloc(*len){ /* error trap */}
*len = i2d_GSPT_GMP(gmp, &next);
return buf;
}
Then in my calling program, I record this encoded buffer in a file and I
read it with openssl asn1parse command.
But it doesn't work because the buffer is incorrect, it contains
nothing, its size is 0.
If I process the same procedure belong a SEQUENCE there is no problems I
can read it with the asn1parse tool. If the choice is encapsulated in
SEQUENCE that contains others SEQUENCE or STACK_OF SEQUENCE, only CHOICE
is unreadable.
int main(void)
{
GSPT_GMP *gmp = NULL;
long len = 0;
unsigned char *der = NULL;
int fd = 0;
gmp = GSPT_GMP_create(V_ASN1_OCTET_STRING,1,"string");
der = GSPT_GMP_i2d(gmp, &len);
fd = open("./proto.der", O_RDWR | O_TRUNC);
if(fd < 0){/* error trap */ }
write(fd,(const void *)der, len);
close(fd);
return 0;
}
Output for the choice (the size file produced is 0):
$ openssl asn1parse -inform der -in proto.der
Error: offset too large
The Same test with a ASN1 SEQUENCE encoding and recorded in file :
$ openssl asn1parse -inform der -in proto.der
0:d=0 hl=2 l= 14 cons: SEQUENCE
2:d=1 hl=2 l= 4 prim: OBJECT :1.0.0.2.3
8:d=1 hl=2 l= 6 prim: OCTET STRING :TEST
I don't see what's wrong, may be someone can help me to diagnose the
troubles.
Best regards
Franck
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openssl-users@openssl.org
Automated List Manager majord...@openssl.org