xref: /freebsd/crypto/openssl/doc/man3/CRYPTO_get_ex_new_index.pod (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimCRYPTO_EX_new, CRYPTO_EX_free, CRYPTO_EX_dup,
6*b077aed3SPierre ProncheryCRYPTO_free_ex_index, CRYPTO_get_ex_new_index,
7*b077aed3SPierre ProncheryCRYPTO_alloc_ex_data, CRYPTO_set_ex_data, CRYPTO_get_ex_data,
8*b077aed3SPierre ProncheryCRYPTO_free_ex_data, CRYPTO_new_ex_data
9e71b7053SJung-uk Kim- functions supporting application-specific data
10e71b7053SJung-uk Kim
11e71b7053SJung-uk Kim=head1 SYNOPSIS
12e71b7053SJung-uk Kim
13e71b7053SJung-uk Kim #include <openssl/crypto.h>
14e71b7053SJung-uk Kim
15e71b7053SJung-uk Kim int CRYPTO_get_ex_new_index(int class_index,
16e71b7053SJung-uk Kim                             long argl, void *argp,
17e71b7053SJung-uk Kim                             CRYPTO_EX_new *new_func,
18e71b7053SJung-uk Kim                             CRYPTO_EX_dup *dup_func,
19e71b7053SJung-uk Kim                             CRYPTO_EX_free *free_func);
20e71b7053SJung-uk Kim
21e71b7053SJung-uk Kim typedef void CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
22e71b7053SJung-uk Kim                            int idx, long argl, void *argp);
23e71b7053SJung-uk Kim typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
24e71b7053SJung-uk Kim                             int idx, long argl, void *argp);
25e71b7053SJung-uk Kim typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
26*b077aed3SPierre Pronchery                           void **from_d, int idx, long argl, void *argp);
27e71b7053SJung-uk Kim
28*b077aed3SPierre Pronchery int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
29*b077aed3SPierre Pronchery
30*b077aed3SPierre Pronchery int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
31*b077aed3SPierre Pronchery                          int idx);
32e71b7053SJung-uk Kim
33e71b7053SJung-uk Kim int CRYPTO_set_ex_data(CRYPTO_EX_DATA *r, int idx, void *arg);
34e71b7053SJung-uk Kim
35*b077aed3SPierre Pronchery void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *r, int idx);
36e71b7053SJung-uk Kim
37e71b7053SJung-uk Kim void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *r);
38e71b7053SJung-uk Kim
39e71b7053SJung-uk Kim int CRYPTO_free_ex_index(int class_index, int idx);
40e71b7053SJung-uk Kim
41e71b7053SJung-uk Kim=head1 DESCRIPTION
42e71b7053SJung-uk Kim
43e71b7053SJung-uk KimSeveral OpenSSL structures can have application-specific data attached to them,
44e71b7053SJung-uk Kimknown as "exdata."
45e71b7053SJung-uk KimThe specific structures are:
46e71b7053SJung-uk Kim
47e71b7053SJung-uk Kim    BIO
48e71b7053SJung-uk Kim    DH
49e71b7053SJung-uk Kim    DSA
50e71b7053SJung-uk Kim    EC_KEY
51e71b7053SJung-uk Kim    ENGINE
52*b077aed3SPierre Pronchery    EVP_PKEY
53e71b7053SJung-uk Kim    RSA
54e71b7053SJung-uk Kim    SSL
55e71b7053SJung-uk Kim    SSL_CTX
56e71b7053SJung-uk Kim    SSL_SESSION
57e71b7053SJung-uk Kim    UI
58e71b7053SJung-uk Kim    UI_METHOD
59e71b7053SJung-uk Kim    X509
60e71b7053SJung-uk Kim    X509_STORE
61e71b7053SJung-uk Kim    X509_STORE_CTX
62e71b7053SJung-uk Kim
63*b077aed3SPierre ProncheryIn addition, the B<APP> name is reserved for use by application code.
64*b077aed3SPierre Pronchery
65*b077aed3SPierre ProncheryEach is identified by an B<CRYPTO_EX_INDEX_xxx> define in the header file
66*b077aed3SPierre ProncheryF<< <openssl/crypto.h> >>.  In addition, B<CRYPTO_EX_INDEX_APP> is reserved for
67e71b7053SJung-uk Kimapplications to use this facility for their own structures.
68e71b7053SJung-uk Kim
69e71b7053SJung-uk KimThe API described here is used by OpenSSL to manipulate exdata for specific
70e71b7053SJung-uk Kimstructures.  Since the application data can be anything at all it is passed
71e71b7053SJung-uk Kimand retrieved as a B<void *> type.
72e71b7053SJung-uk Kim
73e71b7053SJung-uk KimThe B<CRYPTO_EX_DATA> type is opaque.  To initialize the exdata part of
74e71b7053SJung-uk Kima structure, call CRYPTO_new_ex_data(). This is only necessary for
75e71b7053SJung-uk KimB<CRYPTO_EX_INDEX_APP> objects.
76e71b7053SJung-uk Kim
77e71b7053SJung-uk KimExdata types are identified by an B<index>, an integer guaranteed to be
78e71b7053SJung-uk Kimunique within structures for the lifetime of the program.  Applications
79e71b7053SJung-uk Kimusing exdata typically call B<CRYPTO_get_ex_new_index> at startup, and
80e71b7053SJung-uk Kimstore the result in a global variable, or write a wrapper function to
81e71b7053SJung-uk Kimprovide lazy evaluation.  The B<class_index> should be one of the
82e71b7053SJung-uk KimB<CRYPTO_EX_INDEX_xxx> values. The B<argl> and B<argp> parameters are saved
83e71b7053SJung-uk Kimto be passed to the callbacks but are otherwise not used.  In order to
84e71b7053SJung-uk Kimtransparently manipulate exdata, three callbacks must be provided. The
85e71b7053SJung-uk Kimsemantics of those callbacks are described below.
86e71b7053SJung-uk Kim
87e71b7053SJung-uk KimWhen copying or releasing objects with exdata, the callback functions
88e71b7053SJung-uk Kimare called in increasing order of their B<index> value.
89e71b7053SJung-uk Kim
90e71b7053SJung-uk KimIf a dynamic library can be unloaded, it should call CRYPTO_free_ex_index()
91e71b7053SJung-uk Kimwhen this is done.
92e71b7053SJung-uk KimThis will replace the callbacks with no-ops
93e71b7053SJung-uk Kimso that applications don't crash.  Any existing exdata will be leaked.
94e71b7053SJung-uk Kim
95e71b7053SJung-uk KimTo set or get the exdata on an object, the appropriate type-specific
96e71b7053SJung-uk Kimroutine must be used.  This is because the containing structure is opaque
97e71b7053SJung-uk Kimand the B<CRYPTO_EX_DATA> field is not accessible.  In both API's, the
98e71b7053SJung-uk KimB<idx> parameter should be an already-created index value.
99e71b7053SJung-uk Kim
100e71b7053SJung-uk KimWhen setting exdata, the pointer specified with a particular index is saved,
101e71b7053SJung-uk Kimand returned on a subsequent "get" call.  If the application is going to
102e71b7053SJung-uk Kimrelease the data, it must make sure to set a B<NULL> value at the index,
103e71b7053SJung-uk Kimto avoid likely double-free crashes.
104e71b7053SJung-uk Kim
105e71b7053SJung-uk KimThe function B<CRYPTO_free_ex_data> is used to free all exdata attached
106e71b7053SJung-uk Kimto a structure. The appropriate type-specific routine must be used.
107e71b7053SJung-uk KimThe B<class_index> identifies the structure type, the B<obj> is
1086935a639SJung-uk Kima pointer to the actual structure, and B<r> is a pointer to the
109e71b7053SJung-uk Kimstructure's exdata field.
110e71b7053SJung-uk Kim
111e71b7053SJung-uk Kim=head2 Callback Functions
112e71b7053SJung-uk Kim
113e71b7053SJung-uk KimThis section describes how the callback functions are used. Applications
114e71b7053SJung-uk Kimthat are defining their own exdata using B<CYPRTO_EX_INDEX_APP> must
115e71b7053SJung-uk Kimcall them as described here.
116e71b7053SJung-uk Kim
117e71b7053SJung-uk KimWhen a structure is initially allocated (such as RSA_new()) then the
118e71b7053SJung-uk Kimnew_func() is called for every defined index. There is no requirement
119e71b7053SJung-uk Kimthat the entire parent, or containing, structure has been set up.
120e71b7053SJung-uk KimThe new_func() is typically used only to allocate memory to store the
121e71b7053SJung-uk Kimexdata, and perhaps an "initialized" flag within that memory.
122*b077aed3SPierre ProncheryThe exdata value may be allocated later on with CRYPTO_alloc_ex_data(),
123*b077aed3SPierre Proncheryor may be set by calling CRYPTO_set_ex_data().
124e71b7053SJung-uk Kim
125e71b7053SJung-uk KimWhen a structure is free'd (such as SSL_CTX_free()) then the
126e71b7053SJung-uk Kimfree_func() is called for every defined index.  Again, the state of the
127e71b7053SJung-uk Kimparent structure is not guaranteed.  The free_func() may be called with a
128e71b7053SJung-uk KimNULL pointer.
129e71b7053SJung-uk Kim
130e71b7053SJung-uk KimBoth new_func() and free_func() take the same parameters.
131e71b7053SJung-uk KimThe B<parent> is the pointer to the structure that contains the exdata.
132e71b7053SJung-uk KimThe B<ptr> is the current exdata item; for new_func() this will typically
133e71b7053SJung-uk Kimbe NULL.  The B<r> parameter is a pointer to the exdata field of the object.
134e71b7053SJung-uk KimThe B<idx> is the index and is the value returned when the callbacks were
135e71b7053SJung-uk Kiminitially registered via CRYPTO_get_ex_new_index() and can be used if
136e71b7053SJung-uk Kimthe same callback handles different types of exdata.
137e71b7053SJung-uk Kim
138e71b7053SJung-uk Kimdup_func() is called when a structure is being copied.  This is only done
139e71b7053SJung-uk Kimfor B<SSL>, B<SSL_SESSION>, B<EC_KEY> objects and B<BIO> chains via
140e71b7053SJung-uk KimBIO_dup_chain().  The B<to> and B<from> parameters
141e71b7053SJung-uk Kimare pointers to the destination and source B<CRYPTO_EX_DATA> structures,
142*b077aed3SPierre Proncheryrespectively.  The B<*from_d> parameter is a pointer to the source exdata.
143*b077aed3SPierre ProncheryWhen the dup_func() returns, the value in B<*from_d> is copied to the
144e71b7053SJung-uk Kimdestination ex_data.  If the pointer contained in B<*pptr> is not modified
145e71b7053SJung-uk Kimby the dup_func(), then both B<to> and B<from> will point to the same data.
146e71b7053SJung-uk KimThe B<idx>, B<argl> and B<argp> parameters are as described for the other
147e71b7053SJung-uk Kimtwo callbacks.  If the dup_func() returns B<0> the whole CRYPTO_dup_ex_data()
148e71b7053SJung-uk Kimwill fail.
149e71b7053SJung-uk Kim
150e71b7053SJung-uk Kim=head1 RETURN VALUES
151e71b7053SJung-uk Kim
152e71b7053SJung-uk KimCRYPTO_get_ex_new_index() returns a new index or -1 on failure.
153e71b7053SJung-uk Kim
154*b077aed3SPierre ProncheryCRYPTO_free_ex_index(), CRYPTO_alloc_ex_data() and CRYPTO_set_ex_data()
155*b077aed3SPierre Proncheryreturn 1 on success or 0 on failure.
156e71b7053SJung-uk Kim
157e71b7053SJung-uk KimCRYPTO_get_ex_data() returns the application data or NULL on failure;
158e71b7053SJung-uk Kimnote that NULL may be a valid value.
159e71b7053SJung-uk Kim
160e71b7053SJung-uk Kimdup_func() should return 0 for failure and 1 for success.
161e71b7053SJung-uk Kim
162*b077aed3SPierre Pronchery=head1 HISTORY
163*b077aed3SPierre Pronchery
164*b077aed3SPierre ProncheryCRYPTO_alloc_ex_data() was added in OpenSSL 3.0.
165*b077aed3SPierre Pronchery
166*b077aed3SPierre ProncheryThe signature of the dup_func() callback was changed in OpenSSL 3.0 to use the
167*b077aed3SPierre Proncherytype B<void **> for B<from_d>.  Previously this parameter was of type B<void *>.
168*b077aed3SPierre Pronchery
169*b077aed3SPierre ProncherySupport for ENGINE "exdata" was deprecated in OpenSSL 3.0.
170*b077aed3SPierre Pronchery
171e71b7053SJung-uk Kim=head1 COPYRIGHT
172e71b7053SJung-uk Kim
173*b077aed3SPierre ProncheryCopyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
174e71b7053SJung-uk Kim
175*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
176e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
177e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
178e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
179e71b7053SJung-uk Kim
180e71b7053SJung-uk Kim=cut
181