• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in
69  *    the documentation and/or other materials provided with the
70  *    distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  *    software must display the following acknowledgment:
74  *    "This product includes software developed by the OpenSSL Project
75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  *    endorse or promote products derived from this software without
79  *    prior written permission. For written permission, please contact
80  *    openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  *    nor may "OpenSSL" appear in their names without prior written
84  *    permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com).  This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com). */
108 
109 #ifndef OPENSSL_HEADER_EX_DATA_H
110 #define OPENSSL_HEADER_EX_DATA_H
111 
112 #include <openssl/base.h>
113 
114 #include <openssl/stack.h>
115 
116 #if defined(__cplusplus)
117 extern "C" {
118 #endif
119 
120 
121 /* ex_data is a mechanism for associating arbitrary extra data with objects.
122  * The different types of objects which can have data associated with them are
123  * called "classes" and there are predefined classes for all the OpenSSL
124  * objects that support ex_data.
125  *
126  * Within a given class, different users can be assigned indexes in which to
127  * store their data. Each index has callback functions that are called when a
128  * new object of that type is created, freed and duplicated. */
129 
130 
131 typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
132 
133 /* CRYPTO_EX_new is the type of a callback function that is called whenever a
134  * new object of a given class is created. For example, if this callback has
135  * been passed to |CRYPTO_get_ex_new_index| with a |class| of
136  * |CRYPTO_EX_INDEX_SSL| then it'll be called each time an SSL* is created.
137  *
138  * The callback is passed the new object (i.e. the SSL*) in |parent|. The
139  * arguments |argl| and |argp| contain opaque values that were given to
140  * |CRYPTO_get_ex_new_index|. The callback should return one on success, but
141  * the value is ignored.
142  *
143  * TODO(fork): the |ptr| argument is always NULL, no? */
144 typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
145                           int index, long argl, void *argp);
146 
147 /* CRYPTO_EX_free is a callback function that is called when an object of the
148  * class is being destroyed. See |CRYPTO_EX_new| for a discussion of the
149  * arguments.
150  *
151  * If |CRYPTO_get_ex_new_index| was called after the creation of objects of the
152  * class that this applies to then, when those those objects are destroyed,
153  * this callback will be called with a NULL value for |ptr|. */
154 typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
155                             int index, long argl, void *argp);
156 
157 /* CRYPTO_EX_dup is a callback function that is called when an object of the
158  * class is being copied and thus the ex_data linked to it also needs to be
159  * copied. On entry, |*from_d| points to the data for this index from the
160  * original object. When the callback returns, |*from_d| will be set as the
161  * data for this index in |to|.
162  *
163  * If |CRYPTO_get_ex_new_index| was called after the creation of objects of the
164  * class that this applies to then, when those those objects are copies, this
165  * callback will be called with a NULL value for |*from_d|. */
166 typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
167                           void **from_d, int index, long argl, void *argp);
168 
169 /* CRYPTO_get_ex_new_index allocates a new index for ex_data linked with
170  * objects of the given |class|. This should not be called directly, rather
171  * each class of object should provide a wrapper function that sets
172  * |class_value| correctly.
173  *
174  * The |class_value| argument should be one of |CRYPTO_EX_INDEX_*| or a
175  * user-defined class value returned from |CRYPTO_ex_data_new_class|.
176  *
177  * See the descriptions of the callback typedefs for details of when they are
178  * called. Any of the callback arguments may be NULL. The |argl| and |argp|
179  * arguments are opaque values that are passed to the callbacks.
180  *
181  * It returns the new index, or a negative number on error.
182  *
183  * TODO(fork): this should follow the standard calling convention.
184  *
185  * TODO(fork): replace the class_value with a pointer to EX_CLASS_ITEM. Saves
186  * having that hash table and some of the lock-bouncing. Maybe have every
187  * module have a private global EX_CLASS_ITEM somewhere and any direct callers
188  * of CRYPTO_{get,set}_ex_data{,_index} would have to always call the
189  * wrappers. */
190 OPENSSL_EXPORT int CRYPTO_get_ex_new_index(int class_value, long argl,
191                                            void *argp, CRYPTO_EX_new *new_func,
192                                            CRYPTO_EX_dup *dup_func,
193                                            CRYPTO_EX_free *free_func);
194 
195 /* CRYPTO_set_ex_data sets an extra data pointer on a given object. This should
196  * not be called directly, rather each class of object should provide a wrapper
197  * function.
198  *
199  * The |index| argument should have been returned from a previous call to
200  * |CRYPTO_get_ex_new_index|. */
201 OPENSSL_EXPORT int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val);
202 
203 /* CRYPTO_set_ex_data return an extra data pointer for a given object, or NULL
204  * if no such index exists. This should not be called directly, rather each
205  * class of object should provide a wrapper function.
206  *
207  * The |index| argument should have been returned from a previous call to
208  * |CRYPTO_get_ex_new_index|. */
209 OPENSSL_EXPORT void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int index);
210 
211 /* CRYPTO_EX_INDEX_* are the built-in classes of objects.
212  *
213  * User defined classes start at 100.
214  *
215  * TODO(fork): WARNING: these are called "INDEX", but they aren't! */
216 #define CRYPTO_EX_INDEX_BIO 0
217 #define CRYPTO_EX_INDEX_SSL 1
218 #define CRYPTO_EX_INDEX_SSL_CTX 2
219 #define CRYPTO_EX_INDEX_SSL_SESSION 3
220 #define CRYPTO_EX_INDEX_X509_STORE 4
221 #define CRYPTO_EX_INDEX_X509_STORE_CTX 5
222 #define CRYPTO_EX_INDEX_RSA 6
223 #define CRYPTO_EX_INDEX_DSA 7
224 #define CRYPTO_EX_INDEX_DH 8
225 #define CRYPTO_EX_INDEX_ENGINE 9
226 #define CRYPTO_EX_INDEX_X509 10
227 #define CRYPTO_EX_INDEX_UI 11
228 #define CRYPTO_EX_INDEX_EC_KEY 12
229 #define CRYPTO_EX_INDEX_EC_GROUP 13
230 #define CRYPTO_EX_INDEX_COMP 14
231 #define CRYPTO_EX_INDEX_STORE 15
232 
233 
234 /* User-defined classes of objects.
235  *
236  * Core OpenSSL code has predefined class values given above (the
237  * |CRYPTO_EX_INDEX_*| values). It's possible to get dynamic class values
238  * assigned for user-defined objects. */
239 
240 /* CRYPTO_ex_data_new_class returns a fresh class value for a user-defined type
241  * that wishes to use ex_data.
242  *
243  * TODO(fork): hopefully remove this. */
244 OPENSSL_EXPORT int CRYPTO_ex_data_new_class(void);
245 
246 
247 /* Embedding, allocating and freeing |CRYPTO_EX_DATA| structures for objects
248  * that embed them. */
249 
250 /* CRYPTO_new_ex_data initialises a newly allocated |CRYPTO_EX_DATA| which is
251  * embedded inside of |obj| which is of class |class_value|. Returns one on
252  * success and zero otherwise. */
253 OPENSSL_EXPORT int CRYPTO_new_ex_data(int class_value, void *obj,
254                                       CRYPTO_EX_DATA *ad);
255 
256 /* CRYPTO_dup_ex_data duplicates |from| into a freshly allocated
257  * |CRYPTO_EX_DATA|, |to|. Both of which are inside objects of the given
258  * class. It returns one on success and zero otherwise. */
259 OPENSSL_EXPORT int CRYPTO_dup_ex_data(int class_value, CRYPTO_EX_DATA *to,
260                                       const CRYPTO_EX_DATA *from);
261 
262 /* CRYPTO_free_ex_data frees |ad|, which is embedded inside |obj|, which is an
263  * object of the given class. */
264 OPENSSL_EXPORT void CRYPTO_free_ex_data(int class_value, void *obj,
265                                         CRYPTO_EX_DATA *ad);
266 
267 
268 /* Handling different ex_data implementations. */
269 
270 /* CRYPTO_EX_DATA_IMPL is the opaque type of an implementation of ex_data. */
271 typedef struct st_CRYPTO_EX_DATA_IMPL CRYPTO_EX_DATA_IMPL;
272 
273 /* CRYPTO_get_ex_data_implementation returns the current implementation of
274  * ex_data. */
275 OPENSSL_EXPORT const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(
276     void);
277 
278 /* CRYPTO_set_ex_data_implementation sets the implementation of ex_data to use,
279  * unless ex_data has already been used and the default implementation
280  * installed. It returns one on success and zero otherwise. */
281 OPENSSL_EXPORT int CRYPTO_set_ex_data_implementation(
282     const CRYPTO_EX_DATA_IMPL *impl);
283 
284 
285 /* Private functions. */
286 
287 /* CRYPTO_cleanup_all_ex_data cleans up all ex_data state. It assumes that no
288  * other threads are executing code that might call ex_data functions. */
289 OPENSSL_EXPORT void CRYPTO_cleanup_all_ex_data(void);
290 
291 struct crypto_ex_data_st {
292   STACK_OF(void) *sk;
293 };
294 
295 
296 #if defined(__cplusplus)
297 }  /* extern C */
298 #endif
299 
300 #endif  /* OPENSSL_HEADER_EX_DATA_H */
301