ASN1_EXTERN_FUNCS(3ossl)

✨ NAME ✨

ASN1_EXTERN_FUNCS, ASN1_ex_d2i, ASN1_ex_d2i_ex, ASN1_ex_i2d, ASN1_ex_new_func, ASN1_ex_new_ex_func, ASN1_ex_free_func, ASN1_ex_print_func, IMPLEMENT_EXTERN_ASN1 β€” it's giving ASN.1 external function support πŸ¦‹

okay bestie, this is the ASN1_EXTERN_FUNCS page from the OpenSSL manual, and yes we're doing it in group-chat voice, no cap πŸ’…


🌸 SYNOPSIS 🌸

#include <openssl/asn1t.h>

typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
                        const ASN1_ITEM *it, int tag, int aclass, char opt,
                        ASN1_TLC *ctx);
typedef int ASN1_ex_d2i_ex(ASN1_VALUE **pval, const unsigned char **in, long len,
                           const ASN1_ITEM *it, int tag, int aclass, char opt,
                           ASN1_TLC *ctx, OSSL_LIB_CTX *libctx,
                           const char *propq);
typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
                        const ASN1_ITEM *it, int tag, int aclass);
typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
typedef int ASN1_ex_new_ex_func(ASN1_VALUE **pval, const ASN1_ITEM *it,
                                OSSL_LIB_CTX *libctx, const char *propq);
typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval,
                               int indent, const char *fname,
                               const ASN1_PCTX *pctx);

struct ASN1_EXTERN_FUNCS_st {
   void *app_data;
   ASN1_ex_new_func *asn1_ex_new;
   ASN1_ex_free_func *asn1_ex_free;
   ASN1_ex_free_func *asn1_ex_clear;
   ASN1_ex_d2i *asn1_ex_d2i;
   ASN1_ex_i2d *asn1_ex_i2d;
   ASN1_ex_print_func *asn1_ex_print;
   ASN1_ex_new_ex_func *asn1_ex_new_ex;
   ASN1_ex_d2i_ex *asn1_ex_d2i_ex;
};
typedef struct ASN1_EXTERN_FUNCS_st ASN1_EXTERN_FUNCS;

#define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs)

like. look at that struct. it's a whole vibe. πŸ’–


πŸŽ€ DESCRIPTION πŸŽ€

so normally, when OpenSSL defines ASN.1 data structure templates, it uses a lil series of macros β€” think ASN1_SEQUENCE(), ASN1_SEQUENCE_END() and friends, the usual suspects ✨. But β€” plot twist β€” templates can also be defined entirely on external functions instead. these external functions get called to do the stuff like creating a new ASN1_VALUE, or converting an ASN1_VALUE to or from DER encoding. main character energy, honestly. πŸ‘‘

the macro IMPLEMENT_EXTERN_ASN1() is what you use to create one of these externally defined structures. here's the rundown:

  • the name of the structure goes in the sname parameter
  • the tag for the structure (e.g. typically V_ASN1_SEQUENCE) goes in the tag parameter
  • and finally, a pointer to an ASN1_EXTERN_FUNCS structure goes in the fptrs parameter

πŸ’œ the ASN1_EXTERN_FUNCS structure, entry by entry

it has the following entries. buckle up, bestie, there's nine of them and we're doing ALL of them 🫢

app_data

a pointer to arbitrary application specific data. that's it. that's the entry. it's the little pocket where you keep your stuff. πŸ“

asn1_ex_new

a "new" function, responsible for constructing a new ASN1_VALUE object. the newly constructed value should be stored in *pval. the it parameter is a pointer to the ASN1_ITEM template object created via the IMPLEMENT_EXTERN_ASN1() macro.

returns a positive value on success, or 0 on error. 😭

asn1_ex_free

a "free" function, responsible for freeing the ASN1_VALUE passed in *pval that was previously allocated via a "new" function. the it parameter is a pointer to the ASN1_ITEM template object created via the IMPLEMENT_EXTERN_ASN1() macro.

asn1_ex_clear

a "clear" function, responsible for clearing any data in the ASN1_VALUE passed in *pval and making it suitable for reuse. the it parameter is a pointer to the ASN1_ITEM template object created via the IMPLEMENT_EXTERN_ASN1() macro.

asn1_ex_d2i

a "d2i" function, responsible for converting DER data with the tag tag and class class into an ASN1_VALUE. the deets:

  • if *pval is non-NULL, then the ASN_VALUE it points to should be reused. otherwise a new ASN1_VALUE should be allocated and stored in *pval.
  • *in points to the DER data to be decoded, and len is the length of that data.
  • after decoding, *in should be updated to point at the next byte after the decoded data.
  • if the ASN1_VALUE is considered optional in this context, then opt will be nonzero. otherwise it will be zero.
  • the it parameter is a pointer to the ASN1_ITEM template object created via the IMPLEMENT_EXTERN_ASN1() macro.
  • a pointer to the current ASN1_TLC context (which may be required for other ASN1 function calls) is passed in the ctx parameter.

the asn1_ex_d2i entry may be NULL if asn1_ex_d2i_ex has been specified instead.

returns <= 0 on error, or a positive value on success. 🫑

asn1_ex_i2d

an "i2d" function, responsible for converting an ASN1_VALUE into DER encoding. on entry *pval will contain the ASN1_VALUE to be encoded.

  • if default tagging is to be used, then tag will be -1 on entry. otherwise, if implicit tagging should be used, then tag and aclass will be the tag and associated class.
  • if out is not NULL, then this function should write the DER encoded data to the buffer in *out, and then increment *out to point to immediately after the data just written. ✍️
  • if out is NULL, then no data should be written β€” but the length should be calculated and returned as if it were.

the asn1_ex_i2d entry may be NULL if asn1_ex_i2d_ex has been specified instead.

the return value should be negative if a fatal error occurred, or 0 if a non-fatal error occurred. otherwise it should return the length of the encoded data. πŸ’€ (dramatic but accurate)

asn1_ex_print

a "print" function. here's the map:

  • out is the BIO to print the output to.
  • *pval is the ASN1_VALUE to be printed.
  • indent is the number of spaces of indenting to be printed before any data is printed.
  • fname is currently unused and is always "".
  • pctx is a pointer to the ASN1_PCTX for the print operation.

returns 0 on error, or a positive value on success. and ooh, here's a cute one: if the return value is 2, then an additional newline will be printed after the data printed by this function. ✨

asn1_ex_new_ex

this is the same as asn1_ex_new, except that it is additionally passed the OSSL_LIB_CTX to be used in libctx, and any property query string to be used for algorithm fetching in the propq parameter. see "ALGORITHM FETCHING" in crypto(7) for further details.

if asn1_ex_new_ex is non NULL, then it will always be called in preference to asn1_ex_new. no debate, it wins, periodt. πŸ‘‘

asn1_ex_d2i_ex

this is the same as asn1_ex_d2i, except that it is additionally passed the OSSL_LIB_CTX to be used in libctx, and any property query string to be used for algorithm fetching in the propq parameter. see "ALGORITHM FETCHING" in crypto(7) for further details.

if asn1_ex_d2i_ex is non NULL, then it will always be called in preference to asn1_ex_d2i. same energy, different function. πŸ¦‹


🌈 RETURN VALUES 🌈

return values for the various callbacks are as described above. yes bestie, you have to scroll back up, they're all up there in the descriptions πŸ’–


🌟 SEE ALSO 🌟


πŸ“œ HISTORY πŸ“œ

the asn1_ex_new_ex and asn1_ex_d2i_ex callbacks were added in OpenSSL 3.0. so they're the newer girlies of the group chat. πŸ’…


copyright 2021 The OpenSSL Project Authors. All Rights Reserved.

licensed under the Apache License 2.0 (the "License"). you may not use this file except in compliance with the License. you can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html.


and that's the whole page, bestie!! πŸŽ€ written by the OpenSSL girlies, version 3.5.9, dated 2026-09-29 β€” mwah, slay, go read ASN1_item_new_ex(3) next, ily 🫢✨

πŸ“¦ where this page comes from