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