]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dso/dso_lib.c
rsautl.c requires RSA.
[thirdparty/openssl.git] / crypto / dso / dso_lib.c
CommitLineData
8f4fac7f
GT
1/* dso_lib.c */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <openssl/crypto.h>
61#include "cryptlib.h"
62#include <openssl/dso.h>
63
64static DSO_METHOD *default_DSO_meth = NULL;
65
66DSO *DSO_new(void)
67 {
68 return(DSO_new_method(NULL));
69 }
70
71void DSO_set_default_method(DSO_METHOD *meth)
72 {
73 default_DSO_meth = meth;
74 }
75
76DSO_METHOD *DSO_get_default_method(void)
77 {
78 return(default_DSO_meth);
79 }
80
81DSO_METHOD *DSO_get_method(DSO *dso)
82 {
83 return(dso->meth);
84 }
85
86DSO_METHOD *DSO_set_method(DSO *dso, DSO_METHOD *meth)
87 {
88 DSO_METHOD *mtmp;
89 mtmp = dso->meth;
90 dso->meth = meth;
91 return(mtmp);
92 }
93
94DSO *DSO_new_method(DSO_METHOD *meth)
95 {
96 DSO *ret;
97
98 if(default_DSO_meth == NULL)
99 /* We default to DSO_METH_openssl() which in turn defaults
100 * to stealing the "best available" method. Will fallback
101 * to DSO_METH_null() in the worst case. */
102 default_DSO_meth = DSO_METHOD_openssl();
26a3a48d 103 ret = (DSO *)OPENSSL_malloc(sizeof(DSO));
8f4fac7f
GT
104 if(ret == NULL)
105 {
106 DSOerr(DSO_F_DSO_NEW_METHOD,ERR_R_MALLOC_FAILURE);
107 return(NULL);
108 }
109 memset(ret, 0, sizeof(DSO));
62324627
RL
110 ret->meth_data = sk_new_null();
111 if((ret->meth_data = sk_new_null()) == NULL)
8f4fac7f
GT
112 {
113 /* sk_new doesn't generate any errors so we do */
114 DSOerr(DSO_F_DSO_NEW_METHOD,ERR_R_MALLOC_FAILURE);
26a3a48d 115 OPENSSL_free(ret);
8f4fac7f
GT
116 return(NULL);
117 }
118 if(meth == NULL)
119 ret->meth = default_DSO_meth;
120 else
121 ret->meth = meth;
122 ret->references = 1;
123 if((ret->meth->init != NULL) && !ret->meth->init(ret))
124 {
26a3a48d 125 OPENSSL_free(ret);
8f4fac7f
GT
126 ret=NULL;
127 }
128 return(ret);
129 }
130
131int DSO_free(DSO *dso)
132 {
133 int i;
134
135 if(dso == NULL)
136 {
137 DSOerr(DSO_F_DSO_FREE,ERR_R_PASSED_NULL_PARAMETER);
138 return(0);
139 }
140
141 i=CRYPTO_add(&dso->references,-1,CRYPTO_LOCK_DSO);
142#ifdef REF_PRINT
143 REF_PRINT("DSO",dso);
144#endif
145 if(i > 0) return(1);
146#ifdef REF_CHECK
147 if(i < 0)
148 {
149 fprintf(stderr,"DSO_free, bad reference count\n");
150 abort();
151 }
152#endif
153
154 if((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso))
155 {
156 DSOerr(DSO_F_DSO_FREE,DSO_R_UNLOAD_FAILED);
157 return(0);
158 }
159
160 if((dso->meth->finish != NULL) && !dso->meth->finish(dso))
161 {
162 DSOerr(DSO_F_DSO_FREE,DSO_R_FINISH_FAILED);
163 return(0);
164 }
165
166 sk_free(dso->meth_data);
167
26a3a48d 168 OPENSSL_free(dso);
8f4fac7f
GT
169 return(1);
170 }
171
172int DSO_flags(DSO *dso)
173 {
174 return((dso == NULL) ? 0 : dso->flags);
175 }
176
177
178int DSO_up(DSO *dso)
179 {
180 if (dso == NULL)
181 {
182 DSOerr(DSO_F_DSO_UP,ERR_R_PASSED_NULL_PARAMETER);
183 return(0);
184 }
185
186 CRYPTO_add(&dso->references,1,CRYPTO_LOCK_DSO);
187 return(1);
188 }
189
b9e63915 190DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
8f4fac7f
GT
191 {
192 DSO *ret;
193 int allocated = 0;
194
195 if(filename == NULL)
196 {
197 DSOerr(DSO_F_DSO_LOAD,ERR_R_PASSED_NULL_PARAMETER);
198 return(NULL);
199 }
200 if(dso == NULL)
201 {
202 ret = DSO_new_method(meth);
203 if(ret == NULL)
204 {
205 DSOerr(DSO_F_DSO_LOAD,ERR_R_MALLOC_FAILURE);
206 return(NULL);
207 }
208 allocated = 1;
209 }
210 else
211 ret = dso;
b9e63915
GT
212 /* Bleurgh ... have to check for negative return values for
213 * errors. <grimace> */
214 if(DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0)
215 {
216 DSOerr(DSO_F_DSO_LOAD,DSO_R_CTRL_FAILED);
217 if(allocated)
218 DSO_free(ret);
219 return(NULL);
220 }
8f4fac7f
GT
221 if(ret->meth->dso_load == NULL)
222 {
223 DSOerr(DSO_F_DSO_LOAD,DSO_R_UNSUPPORTED);
224 if(allocated)
225 DSO_free(ret);
226 return(NULL);
227 }
228 if(!ret->meth->dso_load(ret, filename))
229 {
230 DSOerr(DSO_F_DSO_LOAD,DSO_R_LOAD_FAILED);
231 if(allocated)
232 DSO_free(ret);
233 return(NULL);
234 }
235 /* Load succeeded */
236 return(ret);
237 }
238
e9a68cfb 239void *DSO_bind_var(DSO *dso, const char *symname)
8f4fac7f
GT
240 {
241 void *ret = NULL;
242
243 if((dso == NULL) || (symname == NULL))
244 {
e9a68cfb 245 DSOerr(DSO_F_DSO_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
8f4fac7f
GT
246 return(NULL);
247 }
e9a68cfb 248 if(dso->meth->dso_bind_var == NULL)
8f4fac7f 249 {
e9a68cfb 250 DSOerr(DSO_F_DSO_BIND_VAR,DSO_R_UNSUPPORTED);
8f4fac7f
GT
251 return(NULL);
252 }
e9a68cfb 253 if((ret = dso->meth->dso_bind_var(dso, symname)) == NULL)
8f4fac7f 254 {
e9a68cfb
GT
255 DSOerr(DSO_F_DSO_BIND_VAR,DSO_R_SYM_FAILURE);
256 return(NULL);
257 }
258 /* Success */
259 return(ret);
260 }
261
262DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
263 {
264 DSO_FUNC_TYPE ret = NULL;
265
266 if((dso == NULL) || (symname == NULL))
267 {
268 DSOerr(DSO_F_DSO_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
269 return(NULL);
270 }
271 if(dso->meth->dso_bind_func == NULL)
272 {
273 DSOerr(DSO_F_DSO_BIND_FUNC,DSO_R_UNSUPPORTED);
274 return(NULL);
275 }
276 if((ret = dso->meth->dso_bind_func(dso, symname)) == NULL)
277 {
278 DSOerr(DSO_F_DSO_BIND_FUNC,DSO_R_SYM_FAILURE);
8f4fac7f
GT
279 return(NULL);
280 }
281 /* Success */
282 return(ret);
283 }
b9e63915
GT
284
285/* I don't really like these *_ctrl functions very much to be perfectly
286 * honest. For one thing, I think I have to return a negative value for
287 * any error because possible DSO_ctrl() commands may return values
288 * such as "size"s that can legitimately be zero (making the standard
289 * "if(DSO_cmd(...))" form that works almost everywhere else fail at
290 * odd times. I'd prefer "output" values to be passed by reference and
291 * the return value as success/failure like usual ... but we conform
292 * when we must... :-) */
293long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
294 {
295 if(dso == NULL)
296 {
297 DSOerr(DSO_F_DSO_CTRL,ERR_R_PASSED_NULL_PARAMETER);
298 return(-1);
299 }
5b0a0544
GT
300 /* We should intercept certain generic commands and only pass control
301 * to the method-specific ctrl() function if it's something we don't
302 * handle. */
303 switch(cmd)
304 {
305 case DSO_CTRL_GET_FLAGS:
306 return dso->flags;
307 case DSO_CTRL_SET_FLAGS:
308 dso->flags = (int)larg;
309 return(0);
310 case DSO_CTRL_OR_FLAGS:
311 dso->flags |= (int)larg;
312 return(0);
313 default:
314 break;
315 }
b9e63915
GT
316 if((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL))
317 {
318 DSOerr(DSO_F_DSO_CTRL,DSO_R_UNSUPPORTED);
319 return(-1);
320 }
321 return(dso->meth->dso_ctrl(dso,cmd,larg,parg));
322 }