DEC C on VMS is pedantic by definition.
[openssl.git] / crypto / ex_data.c
1 /* crypto/ex_data.c */
2
3 /*
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),
8  * where new_func, dup_func, and free_func all are 0, and in
9  * hwcrhk_init (crypto/engine/hw_ncipher.c), which is hopefully only
10  * ever used during program initialization.
11  *
12  * Any multi-threaded application crazy enough to use ex_data for its own
13  * purposes had better make sure that SSL_get_ex_data_X509_STORE_CTX_idx
14  * is called once before multiple threads are created.
15  */
16
17 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
18  * All rights reserved.
19  *
20  * This package is an SSL implementation written
21  * by Eric Young (eay@cryptsoft.com).
22  * The implementation was written so as to conform with Netscapes SSL.
23  * 
24  * This library is free for commercial and non-commercial use as long as
25  * the following conditions are aheared to.  The following conditions
26  * apply to all code found in this distribution, be it the RC4, RSA,
27  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
28  * included with this distribution is covered by the same copyright terms
29  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
30  * 
31  * Copyright remains Eric Young's, and as such any Copyright notices in
32  * the code are not to be removed.
33  * If this package is used in a product, Eric Young should be given attribution
34  * as the author of the parts of the library used.
35  * This can be in the form of a textual message at program startup or
36  * in documentation (online or textual) provided with the package.
37  * 
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *    "This product includes cryptographic software written by
49  *     Eric Young (eay@cryptsoft.com)"
50  *    The word 'cryptographic' can be left out if the rouines from the library
51  *    being used are not cryptographic related :-).
52  * 4. If you include any Windows specific code (or a derivative thereof) from 
53  *    the apps directory (application code) you must include an acknowledgement:
54  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
55  * 
56  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  * 
68  * The licence and distribution terms for any publically available version or
69  * derivative of this code cannot be changed.  i.e. this code cannot simply be
70  * copied and put under another distribution licence
71  * [including the GNU Public Licence.]
72  */
73
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <openssl/buffer.h>
77 #include <openssl/bio.h>
78 #include <openssl/lhash.h>
79 #include "cryptlib.h"
80
81 int CRYPTO_get_ex_new_index(int idx, STACK_OF(CRYPTO_EX_DATA_FUNCS) **skp, long argl, void *argp,
82              CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
83         {
84         int ret= -1;
85         CRYPTO_EX_DATA_FUNCS *a;
86
87         MemCheck_off();
88         if (*skp == NULL)
89                 *skp=sk_CRYPTO_EX_DATA_FUNCS_new_null();
90         if (*skp == NULL)
91                 {
92                 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
93                 goto err;
94                 }
95         a=(CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
96         if (a == NULL)
97                 {
98                 CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
99                 goto err;
100                 }
101         a->argl=argl;
102         a->argp=argp;
103         a->new_func=new_func;
104         a->dup_func=dup_func;
105         a->free_func=free_func;
106         while (sk_CRYPTO_EX_DATA_FUNCS_num(*skp) <= idx)
107                 {
108                 if (!sk_CRYPTO_EX_DATA_FUNCS_push(*skp,NULL))
109                         {
110                         CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX,ERR_R_MALLOC_FAILURE);
111                         OPENSSL_free(a);
112                         goto err;
113                         }
114                 }
115         sk_CRYPTO_EX_DATA_FUNCS_set(*skp,idx, a);
116         ret=idx;
117 err:
118         MemCheck_on();
119         return(ret);
120         }
121
122 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val)
123         {
124         int i;
125
126         if (ad->sk == NULL)
127                 {
128                 if ((ad->sk=sk_new_null()) == NULL)
129                         {
130                         CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
131                         return(0);
132                         }
133                 }
134         i=sk_num(ad->sk);
135
136         while (i <= idx)
137                 {
138                 if (!sk_push(ad->sk,NULL))
139                         {
140                         CRYPTOerr(CRYPTO_F_CRYPTO_SET_EX_DATA,ERR_R_MALLOC_FAILURE);
141                         return(0);
142                         }
143                 i++;
144                 }
145         sk_set(ad->sk,idx,val);
146         return(1);
147         }
148
149 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)
150         {
151         if (ad->sk == NULL)
152                 return(0);
153         else if (idx >= sk_num(ad->sk))
154                 return(0);
155         else
156                 return(sk_value(ad->sk,idx));
157         }
158
159 /* The callback is called with the 'object', which is the original data object
160  * being duplicated, a pointer to the
161  * 'new' object to be inserted, the index, and the argi/argp
162  */
163 int CRYPTO_dup_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, CRYPTO_EX_DATA *to,
164              CRYPTO_EX_DATA *from)
165         {
166         int i,j,m,r;
167         CRYPTO_EX_DATA_FUNCS *mm;
168         char *from_d;
169
170         if (meth == NULL) return(1);
171         if (from->sk == NULL) return(1);
172         m=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
173         j=sk_num(from->sk);
174         for (i=0; i<j; i++)
175                 {
176                 from_d=CRYPTO_get_ex_data(from,i);
177                 if (i < m)
178                         {
179                         mm=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
180                         if (mm->dup_func != NULL)
181                                 r=mm->dup_func(to,from,(char **)&from_d,i,
182                                         mm->argl,mm->argp);
183                         }
184                 CRYPTO_set_ex_data(to,i,from_d);
185                 }
186         return(1);
187         }
188
189 /* Call each free callback */
190 void CRYPTO_free_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, void *obj, CRYPTO_EX_DATA *ad)
191         {
192         CRYPTO_EX_DATA_FUNCS *m;
193         void *ptr;
194         int i,max;
195
196         if (meth != NULL)
197                 {
198                 max=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
199                 for (i=0; i<max; i++)
200                         {
201                         m=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
202                         if ((m != NULL) && (m->free_func != NULL))
203                                 {
204                                 ptr=CRYPTO_get_ex_data(ad,i);
205                                 m->free_func(obj,ptr,ad,i,m->argl,m->argp);
206                                 }
207                         }
208                 }
209         if (ad->sk != NULL)
210                 {
211                 sk_free(ad->sk);
212                 ad->sk=NULL;
213                 }
214         }
215
216 void CRYPTO_new_ex_data(STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth, void *obj, CRYPTO_EX_DATA *ad)
217         {
218         CRYPTO_EX_DATA_FUNCS *m;
219         void *ptr;
220         int i,max;
221
222         ad->sk=NULL;
223         if (meth != NULL)
224                 {
225                 max=sk_CRYPTO_EX_DATA_FUNCS_num(meth);
226                 for (i=0; i<max; i++)
227                         {
228                         m=sk_CRYPTO_EX_DATA_FUNCS_value(meth,i);
229                         if ((m != NULL) && (m->new_func != NULL))
230                                 {
231                                 ptr=CRYPTO_get_ex_data(ad,i);
232                                 m->new_func(obj,ptr,ad,i,m->argl,m->argp);
233                                 }
234                         }
235                 }
236         }
237
238 IMPLEMENT_STACK_OF(CRYPTO_EX_DATA_FUNCS)