1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos ASN1_EXTERN_FUNCS, ASN1_ex_d2i, ASN1_ex_d2i_ex, ASN1_ex_i2d, ASN1_ex_new_func, 6 1.1 christos ASN1_ex_new_ex_func, ASN1_ex_free_func, ASN1_ex_print_func, 7 1.1 christos IMPLEMENT_EXTERN_ASN1 8 1.1 christos - ASN.1 external function support 9 1.1 christos 10 1.1 christos =head1 SYNOPSIS 11 1.1 christos 12 1.1 christos #include <openssl/asn1t.h> 13 1.1 christos 14 1.1 christos typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 15 1.1 christos const ASN1_ITEM *it, int tag, int aclass, char opt, 16 1.1 christos ASN1_TLC *ctx); 17 1.1 christos typedef int ASN1_ex_d2i_ex(ASN1_VALUE **pval, const unsigned char **in, long len, 18 1.1 christos const ASN1_ITEM *it, int tag, int aclass, char opt, 19 1.1 christos ASN1_TLC *ctx, OSSL_LIB_CTX *libctx, 20 1.1 christos const char *propq); 21 1.1 christos typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out, 22 1.1 christos const ASN1_ITEM *it, int tag, int aclass); 23 1.1 christos typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 24 1.1 christos typedef int ASN1_ex_new_ex_func(ASN1_VALUE **pval, const ASN1_ITEM *it, 25 1.1 christos OSSL_LIB_CTX *libctx, const char *propq); 26 1.1 christos typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 27 1.1 christos typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval, 28 1.1 christos int indent, const char *fname, 29 1.1 christos const ASN1_PCTX *pctx); 30 1.1 christos 31 1.1 christos struct ASN1_EXTERN_FUNCS_st { 32 1.1 christos void *app_data; 33 1.1 christos ASN1_ex_new_func *asn1_ex_new; 34 1.1 christos ASN1_ex_free_func *asn1_ex_free; 35 1.1 christos ASN1_ex_free_func *asn1_ex_clear; 36 1.1 christos ASN1_ex_d2i *asn1_ex_d2i; 37 1.1 christos ASN1_ex_i2d *asn1_ex_i2d; 38 1.1 christos ASN1_ex_print_func *asn1_ex_print; 39 1.1 christos ASN1_ex_new_ex_func *asn1_ex_new_ex; 40 1.1 christos ASN1_ex_d2i_ex *asn1_ex_d2i_ex; 41 1.1 christos }; 42 1.1 christos typedef struct ASN1_EXTERN_FUNCS_st ASN1_EXTERN_FUNCS; 43 1.1 christos 44 1.1 christos #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) 45 1.1 christos 46 1.1 christos =head1 DESCRIPTION 47 1.1 christos 48 1.1 christos ASN.1 data structures templates are typically defined in OpenSSL using a series 49 1.1 christos of macros such as ASN1_SEQUENCE(), ASN1_SEQUENCE_END() and so on. Instead 50 1.1 christos templates can also be defined based entirely on external functions. These 51 1.1 christos external functions are called to perform operations such as creating a new 52 1.1 christos B<ASN1_VALUE> or converting an B<ASN1_VALUE> to or from DER encoding. 53 1.1 christos 54 1.1 christos The macro IMPLEMENT_EXTERN_ASN1() can be used to create such an externally 55 1.1 christos defined structure. The name of the structure should be supplied in the I<sname> 56 1.1 christos parameter. The tag for the structure (e.g. typically B<V_ASN1_SEQUENCE>) should 57 1.1 christos be supplied in the I<tag> parameter. Finally a pointer to an 58 1.1 christos B<ASN1_EXTERN_FUNCS> structure should be supplied in the I<fptrs> parameter. 59 1.1 christos 60 1.1 christos The B<ASN1_EXTERN_FUNCS> structure has the following entries. 61 1.1 christos 62 1.1 christos =over 4 63 1.1 christos 64 1.1 christos =item I<app_data> 65 1.1 christos 66 1.1 christos A pointer to arbitrary application specific data. 67 1.1 christos 68 1.1 christos =item I<asn1_ex_new> 69 1.1 christos 70 1.1 christos A "new" function responsible for constructing a new B<ASN1_VALUE> object. The 71 1.1 christos newly constructed value should be stored in I<*pval>. The I<it> parameter is a 72 1.1 christos pointer to the B<ASN1_ITEM> template object created via the 73 1.1 christos IMPLEMENT_EXTERN_ASN1() macro. 74 1.1 christos 75 1.1 christos Returns a positive value on success or 0 on error. 76 1.1 christos 77 1.1 christos =item I<asn1_ex_free> 78 1.1 christos 79 1.1 christos A "free" function responsible for freeing the B<ASN1_VALUE> passed in I<*pval> 80 1.1 christos that was previously allocated via a "new" function. The I<it> parameter is a 81 1.1 christos pointer to the B<ASN1_ITEM> template object created via the 82 1.1 christos IMPLEMENT_EXTERN_ASN1() macro. 83 1.1 christos 84 1.1 christos =item I<asn1_ex_clear> 85 1.1 christos 86 1.1 christos A "clear" function responsible for clearing any data in the B<ASN1_VALUE> passed 87 1.1 christos in I<*pval> and making it suitable for reuse. The I<it> parameter is a pointer 88 1.1 christos to the B<ASN1_ITEM> template object created via the IMPLEMENT_EXTERN_ASN1() 89 1.1 christos macro. 90 1.1 christos 91 1.1 christos =item I<asn1_ex_d2i> 92 1.1 christos 93 1.1 christos A "d2i" function responsible for converting DER data with the tag I<tag> and 94 1.1 christos class I<class> into an B<ASN1_VALUE>. If I<*pval> is non-NULL then the 95 1.1 christos B<ASN_VALUE> it points to should be reused. Otherwise a new B<ASN1_VALUE> 96 1.1 christos should be allocated and stored in I<*pval>. I<*in> points to the DER data to be 97 1.1 christos decoded and I<len> is the length of that data. After decoding I<*in> should be 98 1.1 christos updated to point at the next byte after the decoded data. If the B<ASN1_VALUE> 99 1.1 christos is considered optional in this context then I<opt> will be nonzero. Otherwise 100 1.1 christos it will be zero. The I<it> parameter is a pointer to the B<ASN1_ITEM> template 101 1.1 christos object created via the IMPLEMENT_EXTERN_ASN1() macro. A pointer to the current 102 1.1 christos B<ASN1_TLC> context (which may be required for other ASN1 function calls) is 103 1.1 christos passed in the I<ctx> parameter. 104 1.1 christos 105 1.1 christos The I<asn1_ex_d2i> entry may be NULL if I<asn1_ex_d2i_ex> has been specified 106 1.1 christos instead. 107 1.1 christos 108 1.1 christos Returns <= 0 on error or a positive value on success. 109 1.1 christos 110 1.1 christos =item I<asn1_ex_i2d> 111 1.1 christos 112 1.1 christos An "i2d" function responsible for converting an B<ASN1_VALUE> into DER encoding. 113 1.1 christos On entry I<*pval> will contain the B<ASN1_VALUE> to be encoded. If default 114 1.1 christos tagging is to be used then I<tag> will be -1 on entry. Otherwise if implicit 115 1.1 christos tagging should be used then I<tag> and I<aclass> will be the tag and associated 116 1.1 christos class. 117 1.1 christos 118 1.1 christos If I<out> is not NULL then this function should write the DER encoded data to 119 1.1 christos the buffer in I<*out>, and then increment I<*out> to point to immediately after 120 1.1 christos the data just written. 121 1.1 christos 122 1.1 christos If I<out> is NULL then no data should be written but the length calculated and 123 1.1 christos returned as if it were. 124 1.1 christos 125 1.1 christos The I<asn1_ex_i2d> entry may be NULL if I<asn1_ex_i2d_ex> has been specified 126 1.1 christos instead. 127 1.1 christos 128 1.1 christos The return value should be negative if a fatal error occurred, or 0 if a 129 1.1 christos non-fatal error occurred. Otherwise it should return the length of the encoded 130 1.1 christos data. 131 1.1 christos 132 1.1 christos =item I<asn1_ex_print> 133 1.1 christos 134 1.1 christos A "print" function. I<out> is the BIO to print the output to. I<*pval> is the 135 1.1 christos B<ASN1_VALUE> to be printed. I<indent> is the number of spaces of indenting to 136 1.1 christos be printed before any data is printed. I<fname> is currently unused and is 137 1.1 christos always "". I<pctx> is a pointer to the B<ASN1_PCTX> for the print operation. 138 1.1 christos 139 1.1 christos Returns 0 on error or a positive value on success. If the return value is 2 then 140 1.1 christos an additional newline will be printed after the data printed by this function. 141 1.1 christos 142 1.1 christos =item I<asn1_ex_new_ex> 143 1.1 christos 144 1.1 christos This is the same as I<asn1_ex_new> except that it is additionally passed the 145 1.1 christos OSSL_LIB_CTX to be used in I<libctx> and any property query string to be used 146 1.1 christos for algorithm fetching in the I<propq> parameter. See 147 1.1 christos L<crypto(7)/ALGORITHM FETCHING> for further details. If I<asn1_ex_new_ex> is 148 1.1 christos non NULL, then it will always be called in preference to I<asn1_ex_new>. 149 1.1 christos 150 1.1 christos =item I<asn1_ex_d2i_ex> 151 1.1 christos 152 1.1 christos This is the same as I<asn1_ex_d2i> except that it is additionally passed the 153 1.1 christos OSSL_LIB_CTX to be used in I<libctx> and any property query string to be used 154 1.1 christos for algorithm fetching in the I<propq> parameter. See 155 1.1 christos L<crypto(7)/ALGORITHM FETCHING> for further details. If I<asn1_ex_d2i_ex> is 156 1.1 christos non NULL, then it will always be called in preference to I<asn1_ex_d2i>. 157 1.1 christos 158 1.1 christos =back 159 1.1 christos 160 1.1 christos =head1 RETURN VALUES 161 1.1 christos 162 1.1 christos Return values for the various callbacks are as described above. 163 1.1 christos 164 1.1 christos =head1 SEE ALSO 165 1.1 christos 166 1.1 christos L<ASN1_item_new_ex(3)> 167 1.1 christos 168 1.1 christos =head1 HISTORY 169 1.1 christos 170 1.1 christos The I<asn1_ex_new_ex> and I<asn1_ex_d2i_ex> callbacks were added in OpenSSL 3.0. 171 1.1 christos 172 1.1 christos =head1 COPYRIGHT 173 1.1 christos 174 1.1 christos Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. 175 1.1 christos 176 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 177 1.1 christos this file except in compliance with the License. You can obtain a copy 178 1.1 christos in the file LICENSE in the source distribution or at 179 1.1 christos L<https://www.openssl.org/source/license.html>. 180 1.1 christos 181 1.1 christos =cut 182