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:01100000   3:00000011   2:00000010   1:00000001
7b:01111011
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 List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to