simple ASN1 sequence - not quite understanding what goes wrong

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

simple ASN1 sequence - not quite understanding what goes wrong

Dirk-Willem van Gulik
I've got a very simple sequence of to integers that I am trying to convert to DER.

Bt I am getting an error or segfault in the final i2d step (lengt -1 for i2d_X9_62).

Any advice on what is going wrong here ?

With kind regards,

Dw.


#include <openssl/bn.h>
#include <openssl/ec.h>

#include <openssl/asn1.h>
#include <openssl/asn1t.h>

#include <err.h>
#include <assert.h>
#include <stdio.h>

typedef struct X9_62_st {
        ASN1_INTEGER *p;
        ASN1_INTEGER *q;
} X9_62;

ASN1_SEQUENCE(X_9_62) =
{
        ASN1_SIMPLE(X9_62, p, ASN1_INTEGER),
        ASN1_SIMPLE(X9_62, q, ASN1_INTEGER)
};
const ASN1_ITEM X9_62_it;

DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62)
DECLARE_ASN1_FUNCTIONS(X9_62)
IMPLEMENT_ASN1_FUNCTIONS(X9_62)

int main(int argc, char **argv)
{

        const unsigned char pbin[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7,
                8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
        const unsigned char qbin[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7,
                8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};

        assert(sizeof(pbin) == 32);
        assert(sizeof(qbin) == 32);

        X9_62          *x962 = X9_62_new();

        BIGNUM * p = BN_bin2bn(pbin, sizeof(pbin), NULL);
        assert(p);
        fprintf(stderr,"P: %s\n",BN_bn2hex(p));

        BIGNUM * q = BN_bin2bn(qbin, sizeof(qbin), NULL);
        assert(q);
        fprintf(stderr,"Q: %s\n",BN_bn2hex(q));

        x962->p = BN_to_ASN1_INTEGER(p, NULL);
        assert(x962->p);

        x962->q = BN_to_ASN1_INTEGER(q, NULL);
        assert(x962->q);

        unsigned char buff [32 * 1024];
        unsigned char           *outp = buff;

        int len = i2d_X9_62(x962, NULL);
        assert(len >=0 && len < sizeof(buff);

        len = i2d_X9_62(x962, outp);

        for (size_t i = 0; i < len; i++)
                putchar(buff[i]);

        X9_62_free(x962);

        return (0);
};

Reply | Threaded
Open this post in threaded view
|

Re: simple ASN1 sequence - not quite understanding what goes wrong

Dirk-Willem van Gulik
Answering my own question - I forgot the END of sequence in the marco.

Functional code below.

Dw.

On 28 Aug 2020, at 15:49, Dirk-Willem van Gulik <[hidden email]> wrote:

I've got a very simple sequence of to integers that I am trying to convert to DER.

Bt I am getting an error or segfault in the final i2d step (lengt -1 for i2d_X9_62).

Any advice on what is going wrong here ?

With kind regards,

Dw.

#include <openssl/bn.h>
#include <openssl/ec.h>

#include <openssl/asn1.h>
#include <openssl/asn1t.h>

#include <err.h>
#include <assert.h>
#include <stdio.h>

typedef struct X962_st {
ASN1_INTEGER *p;
ASN1_INTEGER *q;
} X962;

DECLARE_ASN1_FUNCTIONS(X962)

ASN1_SEQUENCE(X962) =
{
ASN1_SIMPLE(X962, p, ASN1_INTEGER),
ASN1_SIMPLE(X962, q, ASN1_INTEGER)
}ASN1_SEQUENCE_END(X962);

DECLARE_ASN1_ALLOC_FUNCTIONS(X962)
IMPLEMENT_ASN1_FUNCTIONS(X962)

int main(int argc, char **argv)
{
const unsigned char pbin[] = {1, 2, 3, 4, 5, 6, 7, 8, 
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
const unsigned char qbin[] = {0, 0, 0, 0, 0, 0, 0, 0, 
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
assert(sizeof(pbin) == 32);
assert(sizeof(qbin) == 32);

X962          *x962 = X962_new();

BIGNUM * p = BN_bin2bn(pbin, sizeof(pbin), NULL);
assert(p);

x962->p = BN_to_ASN1_INTEGER(p, NULL);
fprintf(stderr,"P: %s\n",BN_bn2hex(p));
assert(x962->p);

BIGNUM * q = BN_bin2bn(qbin, sizeof(qbin), NULL);
assert(q);

x962->q = BN_to_ASN1_INTEGER(q, NULL);
fprintf(stderr,"Q: %s\n",BN_bn2hex(q));
assert(x962->q);

int len = i2d_X962(x962, NULL);
assert(len>0 && len < 1000);

unsigned char buff[32 * 1024];
unsigned char           *outp = buff;

len = i2d_X962(x962, &outp );

for (size_t i = 0; i < len; i++)
putchar(buff[i]);

X962_free(x962);

return (0);

};