[EXTERNAL] ASN.1 macros for composite structures.

Erwann Abalea erwann.abalea at docusign.com
Thu Jan 26 19:08:15 UTC 2023


Bonjour,

Taking your example, the following works here (Mac, OpenSSL 3.0.5).

$ cat myobj.h
#pragma once
#include <openssl/asn1.h>

#define ASN1_OBJECT_dup(x)
ASN1_dup_of(ASN1_OBJECT,i2d_ASN1_OBJECT,d2i_ASN1_OBJECT,x)

/*
 * MyObj ::= SEQUENCE {
 *   a OBJECT IDENTIFIER,
 *   b OBJECT IDENTIFIER }
 */

typedef struct {
  ASN1_OBJECT *a;
  ASN1_OBJECT *b;
} MYOBJ;

DECLARE_ASN1_FUNCTIONS(MYOBJ);
DECLARE_ASN1_DUP_FUNCTION(MYOBJ);
DECLARE_ASN1_PRINT_FUNCTION(MYOBJ);
#define d2i_MYOBJ_bio(bp,p) ASN1_d2i_bio_of(MYOBJ,MYOBJ_new,d2i_MYOBJ,bp,p)
#define i2d_MYOBJ_bio(bp,o) ASN1_i2d_bio_of(MYOBJ,i2d_MYOBJ,bp,o)
#define d2i_MYOBJ_fp(fp,p) ASN1_d2i_fp_of(MYOBJ,MYOBJ_new,d2i_MYOBJ,fp,p)
#define i2d_MYOBJ_fp(fp,p) ASN1_i2d_fp_of(MYOBJ,i2d_MYOBJ,fp,p)


$ cat myobj.c
#include "myobj.h"
#include <openssl/asn1t.h>

/* MyObj */
ASN1_SEQUENCE(MYOBJ) = {
  ASN1_SIMPLE(MYOBJ, a, ASN1_OBJECT),
  ASN1_SIMPLE(MYOBJ, b, ASN1_OBJECT)
} ASN1_SEQUENCE_END(MYOBJ)

IMPLEMENT_ASN1_FUNCTIONS(MYOBJ);
IMPLEMENT_ASN1_DUP_FUNCTION(MYOBJ);
IMPLEMENT_ASN1_PRINT_FUNCTION(MYOBJ);


$ cat myprg.c
#include "myobj.h"
#include <stdio.h>
#include <stdlib.h>
#include <openssl/err.h>
#include <openssl/objects.h>
#include <openssl/bio.h>

int main(void)
{
  FILE *fout = NULL;
  BIO *bout = NULL;
  MYOBJ *myobj = NULL;

  printf("Openssl Version: %s\n", OPENSSL_VERSION_STR);

  myobj = MYOBJ_new();
  myobj->a = OBJ_nid2obj(NID_commonName);
  myobj->b = OBJ_nid2obj(NID_countryName);

  bout = BIO_new_fp(stdout, BIO_NOCLOSE);
  MYOBJ_print_ctx(bout, myobj, 0, NULL);
  BIO_flush(bout);
  BIO_free(bout);

  fout = fopen("myobj.der", "wb");
  i2d_MYOBJ_fp(fout, myobj);
  fclose(fout);

  ERR_print_errors_fp(stderr);
  return 0;
}

$ gcc -o myprg -I /opt/local/include -L /opt/local/lib myprg.c myobj.c
-lcrypto

$ ./myprg
Openssl Version: 3.0.5
MYOBJ:
  a: commonName (2.5.4.3)
  b: countryName (2.5.4.6)

And there's a file myobj.der created in the current directory.


On Thu, Jan 26, 2023 at 6:57 PM Ladd, Watson via openssl-users <
openssl-users at openssl.org> wrote:

> Dear OpenSSL Users,
>
> I have a structure of the form
>
> typdef struct {
>       ASN1_OBJECT *a;
>        ASN1_OBJECT *b;
> } example;
>
> and I cannot figure out how to make the ASN.1 macros serialize it
> correctly.
>
> When I tried to use an ASN1_SEQUENCE with two ASN1_SIMPLEs the code failed
> to serialize the structure.
> I've been trying to use various ASN1_TYPE, but it seems those only work
> when ASN1_OPTS are used or some variant.
>
> Any suggestions/pointers to documentation on how to do this? It seems like
> the resources that used to exist for the very old
> macros don't exist anymore, and the newer more explicit stuff is only used
> inside of providers and not exposed elsewhere.
>
> Sincerely,
> Watson Ladd



-- 
Cordialement,
Erwann Abalea.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mta.openssl.org/pipermail/openssl-users/attachments/20230126/a02d6421/attachment.htm>


More information about the openssl-users mailing list