Re: asn.1 choice decoding problem

2005-03-20 Thread Dr. Stephen Henson
On Sun, Mar 20, 2005, Eric Alata wrote:

 Hi,
 
 I have made the change. Now, the CHOICE looks like:
 
 1  ASN1_CHOICE(G) = {
 2ASN1_EX_TYPE(ASN1_TFLG_EXPTAG|ASN1_TFLG_APPLICATION, 0, G, 
 value.g_1, H_BODY),
 3ASN1_EX_TYPE(ASN1_TFLG_EXPTAG|ASN1_TFLG_APPLICATION, 1, G, 
 value.g_2, I_BODY)
 4  } ASN1_CHOICE_END(G)
 
 It works fine. But, if I understand, everywhere I want to use H, I must use 
 the
 ASN1_EX_TYPE definition?

Yes, you could use a #define if you want to use a shorter form. Although it
looks complicated that ASN1_EX_TYPE macro is similar to the expansion of other
macros like ASN1_EXP(): its just there isn't a convenient short form for that.

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


asn.1 choice decoding problem

2005-03-19 Thread Eric Alata
Hello everyone.

I would like to implement these types with openssl/asn.1:

1   H ::= [APPLICATION 0] INTEGER
2
3   I ::= [APPLICATION 1] OCTET STRING
4
5   G ::= CHOICE {
6   g_1 H,
7   g_2 I }

To do so, I have created a test program (at the end of this message).
Now, all functions but d2i_G are correct. I wanted to solve this problem
by myself, so I have read  some files of openssl to understand,
principally tasn_dec.c.

When a CHOICE (G) is encourted in SN1_item_ex_d2i, the attribute
OPTIONAL is set to every sub types because only one of them is needed
but not all of them. Let us admit that the encapsulated type is H.
Then, ASN1_item_ex_d2i is called recursively. During the test of H,
which is Primitive (I think), the test :
if ((tag != -1) || opt) {
seems to block the d2i_G function.

CHOICE is a 'simple' structure, so I must have made a mistake.
Could you please have a look to my program?

Thanks,
Eric

Here are the output and the test program:

 -

60:0110   3:0011   2:0010   1:0001
7b:0011
Error

 -

#include string.h
#include openssl/asn1.h
#include openssl/asn1t.h


#define H_BODY ASN1_INTEGER
#define I_BODY ASN1_OCTET_STRING


typedef struct _G {
int type;
union {
H_BODY *g_1;
I_BODY *g_2;
} value;
} G;


#define G_1 0
#define G_2 1


ASN1_ITEM_TEMPLATE(I) =
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_EXPTAG|ASN1_TFLG_APPLICATION, 1, I, 
I_BODY)
ASN1_ITEM_TEMPLATE_END(I)


ASN1_ITEM_TEMPLATE(H) =
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_EXPTAG|ASN1_TFLG_APPLICATION, 0, H, 
H_BODY)
ASN1_ITEM_TEMPLATE_END(H)


ASN1_CHOICE(G) = {
ASN1_SIMPLE(G, value.g_1, H),
ASN1_SIMPLE(G, value.g_2, I)
} ASN1_CHOICE_END(G)


IMPLEMENT_ASN1_FUNCTIONS(G)


void hex2bin(int bin, char *str) {
unsigned int mask;

mask = 0x80;
while (mask) {
if (bin  mask)
*str = '1';
else
*str = '0';
str++;
mask = 1;
}
*str = 0;
}



int main(void) {
unsigned char *buffer;
unsigned char string[8];
int j;
int i;
G *f;
G *g;

g = G_new();
g-type = G_1;
g-value.g_1 = ASN1_INTEGER_new();
ASN1_INTEGER_set(g-value.g_1, 123);

buffer = NULL;
i = i2d_G(g, buffer);

for (j = 0; j  i; j++) {
int n = buffer[j]  0xFF;
if (j % 4 == 0) {
printf(\n);
}
hex2bin(n, string);
printf(%2x:%s  , n, string);
}
printf(\n);

f = d2i_G(NULL, buffer, i);

if (f == NULL) {
printf(Error\n);
} else {
G_free(g);
}

return 0;
}
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]