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 via email to