Hi,

I'm trying to use the custom ASN1 facilities of openssl, but lack
understanding of some aspects. At the end of I've included some
test source code which attempts to encode and write out a custom
ASN1 structure. Regarding the code I have a few questions:

1. Does symmKeyInfo_new(), where symmKeyInfo is the custom ASN1
   struct, allocate each field of the struct? Additionally, if I
   used nested sequences, will it recursively allocate?

   Looking through debugging sessions, this appears to be what is
   going on, but it's a little unclear.

2. symmKeyInfo has the ASN1_OBJECT field 'usage'. If allocated using
   symmKeyInfo_new(), how can I set the field?

   For example, 'payload' which is an ASN1_OCTET_STRING has available
   ASN1_OCTET_STRING_set, which sets the field, reallocating 'payload'
   if necessary.

   However digging through the openssl source I can't seem to find a
   similar function for ASN1_OBJECT type.

Any answers to the above would be appreciated.

Thanks,
Naveen

--- BEGIN CODE ---

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

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

typedef struct symmKeyInfo_st
{
    ASN1_OBJECT *usage;
    ASN1_OCTET_STRING *payload;
} symmKeyInfo;

// declares ASN1 new, free, i2d, and d2i function stubs
DECLARE_ASN1_FUNCTIONS(symmKeyInfo)

// declares the iterator struct used to imeplement the above function stubs
ASN1_SEQUENCE(symmKeyInfo) = {
    ASN1_SIMPLE(symmKeyInfo, usage, ASN1_OBJECT),
    ASN1_SIMPLE(symmKeyInfo, payload, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(symmKeyInfo)

// defines the functions from above
IMPLEMENT_ASN1_FUNCTIONS(symmKeyInfo)

int main(int argc, char **argv)
{
    const char *fake_usage_oid = "1.3.6.1.5.5.7.13.99";
    const char *fake_payload = "abcdefg";
    ASN1_OBJECT *usage_oid = NULL;
    ASN1_OCTET_STRING *payload = NULL;
    symmKeyInfo *skinfo = NULL;
    int der_len = 0;
    char *der_buf = NULL;
    char *p = NULL;

    if (!(skinfo = symmKeyInfo_new()))
    {
        fprintf(stderr, "couldnt allocate for symmKeyInfo_new()\n");
        exit(1);
    }

    // set the key payload
    if (!(ASN1_OCTET_STRING_set(skinfo->payload, fake_payload, 
strlen(fake_payload))))
    {
        fprintf(stderr, "couldnt set skinfo->payload\n");
        exit(1);
    }

    // prepare the usage OID as an ASN1_OBJECT type
    if (!(usage_oid = OBJ_txt2obj(fake_usage_oid, 1)))
    {
        fprintf(stderr, "couldnt translate %s via OBJ_txt2obj\n", 
fake_usage_oid);
        exit(1);
    }

    // set the usage OID, uncomment this line and get a segfault,
    // since skinfo->usage has already been allocated
    //skinfo->usage = usage_oid;

    der_len = i2d_symmKeyInfo(skinfo, NULL);
    der_buf = malloc(der_len);
    p = der_buf;
    if (der_len != (i2d_symmKeyInfo(skinfo, &p)))
    {
        fprintf(stderr, "failed to encode skinfo to der\n");
        exit(1);
    }


    // write DER encoded data to stdout
    fwrite(der_buf, der_len, 1, stdout);

    
    symmKeyInfo_free(skinfo);

    return (0);
}

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to