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 <di...@webweaving.org> 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);

};

Reply via email to