4 * This is not thread-safe, nor can it be changed to become thread-safe
5 * without changing various function prototypes and using a lot of locking.
6 * Luckily, it's not really used anywhere except in ssl_verify_cert_chain
7 * via SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c), where
8 * new_func, dup_func, and free_func all are 0.
10 * Any multi-threaded application crazy enough to use ex_data for its own
11 * purposes had better make sure that SSL_get_ex_data_X509_STORE_CTX_idx
12 * is called once before multiple threads are created.
15 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
16 * All rights reserved.
18 * This package is an SSL implementation written
19 * by Eric Young (eay@cryptsoft.com).
20 * The implementation was written so as to conform with Netscapes SSL.
22 * This library is free for commercial and non-commercial use as long as
23 * the following conditions are aheared to. The following conditions
24 * apply to all code found in this distribution, be it the RC4, RSA,
25 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
26 * included with this distribution is covered by the same copyright terms
27 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
29 * Copyright remains Eric Young's, and as such any Copyright notices in
30 * the code are not to be removed.
31 * If this package is used in a product, Eric Young should be given attribution
32 * as the author of the parts of the library used.
33 * This can be in the form of a textual message at program startup or
34 * in documentation (online or textual) provided with the package.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * "This product includes cryptographic software written by
47 * Eric Young (eay@cryptsoft.com)"
48 * The word 'cryptographic' can be left out if the rouines from the library
49 * being used are not cryptographic related :-).
50 * 4. If you include any Windows specific code (or a derivative thereof) from
51 * the apps directory (application code) you must include an acknowledgement:
52 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
54 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * The licence and distribution terms for any publically available version or
67 * derivative of this code cannot be changed. i.e. this code cannot simply be
68 * copied and put under another distribution licence
69 * [including the GNU Public Licence.]
74 #include <openssl/buffer.h>
75 #include <openssl/bio.h>
76 #include <openssl/lhash.h>
79 int CRYPTO_get_ex_new_index(int idx
, STACK_OF(CRYPTO_EX_DATA_FUNCS
) **skp
, long argl
, void *argp
,
80 CRYPTO_EX_new
*new_func
, CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
83 CRYPTO_EX_DATA_FUNCS
*a
;
87 *skp
=sk_CRYPTO_EX_DATA_FUNCS_new_null();
90 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX
,ERR_R_MALLOC_FAILURE
);
93 a
=(CRYPTO_EX_DATA_FUNCS
*)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS
));
96 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX
,ERR_R_MALLOC_FAILURE
);
101 a
->new_func
=new_func
;
102 a
->dup_func
=dup_func
;
103 a
->free_func
=free_func
;
104 while (sk_CRYPTO_EX_DATA_FUNCS_num(*skp
) <= idx
)
106 if (!sk_CRYPTO_EX_DATA_FUNCS_push(*skp
,NULL
))
108 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX
,ERR_R_MALLOC_FAILURE
);
113 sk_CRYPTO_EX_DATA_FUNCS_set(*skp
,idx
, a
);
120 int CRYPTO_set_ex_data(CRYPTO_EX_DATA
*ad
, int idx
, void *val
)
126 if ((ad
->sk
=sk_new_null()) == NULL
)
128 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA
,ERR_R_MALLOC_FAILURE
);
136 if (!sk_push(ad
->sk
,NULL
))
138 CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA
,ERR_R_MALLOC_FAILURE
);
143 sk_set(ad
->sk
,idx
,val
);
147 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA
*ad
, int idx
)
151 else if (idx
>= sk_num(ad
->sk
))
154 return(sk_value(ad
->sk
,idx
));
157 /* The callback is called with the 'object', which is the original data object
158 * being duplicated, a pointer to the
159 * 'new' object to be inserted, the index, and the argi/argp
161 int CRYPTO_dup_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS
) *meth
, CRYPTO_EX_DATA
*to
,
162 CRYPTO_EX_DATA
*from
)
165 CRYPTO_EX_DATA_FUNCS
*mm
;
168 if (meth
== NULL
) return(1);
169 if (from
->sk
== NULL
) return(1);
170 m
=sk_CRYPTO_EX_DATA_FUNCS_num(meth
);
174 from_d
=CRYPTO_get_ex_data(from
,i
);
177 mm
=sk_CRYPTO_EX_DATA_FUNCS_value(meth
,i
);
178 if (mm
->dup_func
!= NULL
)
179 r
=mm
->dup_func(to
,from
,(char **)&from_d
,i
,
182 CRYPTO_set_ex_data(to
,i
,from_d
);
187 /* Call each free callback */
188 void CRYPTO_free_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS
) *meth
, void *obj
, CRYPTO_EX_DATA
*ad
)
190 CRYPTO_EX_DATA_FUNCS
*m
;
196 max
=sk_CRYPTO_EX_DATA_FUNCS_num(meth
);
197 for (i
=0; i
<max
; i
++)
199 m
=sk_CRYPTO_EX_DATA_FUNCS_value(meth
,i
);
200 if ((m
!= NULL
) && (m
->free_func
!= NULL
))
202 ptr
=CRYPTO_get_ex_data(ad
,i
);
203 m
->free_func(obj
,ptr
,ad
,i
,m
->argl
,m
->argp
);
214 void CRYPTO_new_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS
) *meth
, void *obj
, CRYPTO_EX_DATA
*ad
)
216 CRYPTO_EX_DATA_FUNCS
*m
;
223 max
=sk_CRYPTO_EX_DATA_FUNCS_num(meth
);
224 for (i
=0; i
<max
; i
++)
226 m
=sk_CRYPTO_EX_DATA_FUNCS_value(meth
,i
);
227 if ((m
!= NULL
) && (m
->new_func
!= NULL
))
229 ptr
=CRYPTO_get_ex_data(ad
,i
);
230 m
->new_func(obj
,ptr
,ad
,i
,m
->argl
,m
->argp
);
236 IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS
)