ASN1_EXTERN_FUNCS(3ossl)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

ASN1_EXTERN_FUNCS from OpenSSLโ€™s 3ossl manual is here, bestie ๐Ÿ’…โœจ โ€” itโ€™s all about ASN.1 external function support, and yes weโ€™re getting into the callback weeds together.

๐ŸŒธ 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 โ€” ASN.1 external function support. Thatโ€™s the whole crew, no cap.

โœจ SYNOPSIS โœจ

Bestie, hereโ€™s the include and the exact type lore:

        #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)

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Okay so normally ASN.1 data structures templates in OpenSSL get defined using a series of macros like ASN1_SEQUENCE(), ASN1_SEQUENCE_END() and so on. But plot twist: templates can also be defined based entirely on external functions. These external functions are called to perform operations such as creating a new ASN1_VALUE or converting an ASN1_VALUE to or from DER encoding. Itโ€™s giving โ€œcustom engine, but make it ASN.1.โ€

The macro IMPLEMENT_EXTERN_ASN1() can be used to create such an externally defined structure. Supply the name of the structure in the sname parameter. Supply the tag for the structure (e.g. typically V_ASN1_SEQUENCE) in the tag parameter. Finally, supply a pointer to an ASN1_EXTERN_FUNCS structure in the fptrs parameter. Thatโ€™s the whole recipe, periodt.

The ASN1_EXTERN_FUNCS structure has the following entries. Buckle up, bestie.

๐Ÿ’… app_data

A pointer to arbitrary application specific data. Literally whatever you need it to point to.

โœจ 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. 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 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.

๐Ÿ’œ asn1_ex_print

A โ€œprintโ€ function. 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. 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. Like, itโ€™s the main character version.

๐Ÿ”ฅ 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.

๐Ÿซถ RETURN VALUES ๐Ÿซถ

Return values for the various callbacks are as described above. No extra secret returns, bestie.

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

ASN1_item_new_ex(3)

๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

The asn1_ex_new_ex and asn1_ex_d2i_ex callbacks were added in OpenSSL 3.0.

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>.


Signed with maximum main-character energy by OpenSSL 3.5.6 on 2026-04-07 โ€” ASN1_EXTERN_FUNCS(3ossl) out ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from