2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 void (*destroy
)(ENGINE
*);
44 const RSA_METHOD
*rsa
;
46 const RAND_METHOD
*rand
;
55 engine
= calloc(1, sizeof(*engine
));
58 engine
->references
= 1;
62 engine
->dso_handle
= 0;
68 ENGINE_free(ENGINE
*engine
)
70 return ENGINE_finish(engine
);
74 ENGINE_finish(ENGINE
*engine
)
76 if (engine
->references
-- <= 0)
78 if (engine
->references
> 0)
86 (*engine
->destroy
)(engine
);
87 if (engine
->dso_handle
)
88 dlclose(engine
->dso_handle
);
90 memset(engine
, 0, sizeof(*engine
));
91 engine
->references
= -1;
99 ENGINE_up_ref(ENGINE
*engine
)
101 if (engine
->references
< 0)
103 engine
->references
++;
108 ENGINE_set_id(ENGINE
*engine
, const char *id
)
110 engine
->id
= strdup(id
);
111 return (engine
->id
== NULL
) ? 0 : 1;
115 ENGINE_set_name(ENGINE
*engine
, const char *name
)
117 engine
->name
= strdup(name
);
118 return (engine
->name
== NULL
) ? 0 : 1;
122 ENGINE_set_RSA(ENGINE
*engine
, const RSA_METHOD
*method
)
124 engine
->rsa
= method
;
129 ENGINE_set_DH(ENGINE
*engine
, const DH_METHOD
*method
)
136 ENGINE_set_destroy_function(ENGINE
*e
, void (*destroy
)(ENGINE
*))
138 e
->destroy
= destroy
;
143 ENGINE_get_id(const ENGINE
*engine
)
149 ENGINE_get_name(const ENGINE
*engine
)
155 ENGINE_get_RSA(const ENGINE
*engine
)
161 ENGINE_get_DH(const ENGINE
*engine
)
167 ENGINE_get_RAND(const ENGINE
*engine
)
176 #define SG_default_engine(type) \
177 static ENGINE *type##_engine; \
179 ENGINE_set_default_##type(ENGINE *engine) \
182 ENGINE_finish(type##_engine); \
183 type##_engine = engine; \
185 ENGINE_up_ref(type##_engine); \
189 ENGINE_get_default_##type(void) \
192 ENGINE_up_ref(type##_engine); \
193 return type##_engine; \
196 SG_default_engine(RSA
)
197 SG_default_engine(DH
)
199 #undef SG_default_engine
205 static ENGINE
**engines
;
206 static unsigned int num_engines
;
209 add_engine(ENGINE
*engine
)
213 dup
= ENGINE_by_id(engine
->id
);
217 d
= realloc(engines
, (num_engines
+ 1) * sizeof(*engines
));
221 engines
[num_engines
++] = engine
;
227 ENGINE_load_builtin_engines(void)
232 engine
= ENGINE_new();
236 ENGINE_set_id(engine
, "builtin");
237 ENGINE_set_name(engine
,
238 "Heimdal crypto builtin (ltm) engine version " PACKAGE_VERSION
);
239 ENGINE_set_RSA(engine
, RSA_ltm_method());
240 ENGINE_set_DH(engine
, DH_ltm_method());
242 ret
= add_engine(engine
);
244 ENGINE_finish(engine
);
246 #ifdef USE_HCRYPTO_TFM
251 engine
= ENGINE_new();
255 ENGINE_set_id(engine
, "tfm");
256 ENGINE_set_name(engine
,
257 "Heimdal crypto tfm engine version " PACKAGE_VERSION
);
258 ENGINE_set_RSA(engine
, RSA_tfm_method());
259 ENGINE_set_DH(engine
, DH_tfm_method());
261 ret
= add_engine(engine
);
263 ENGINE_finish(engine
);
264 #endif /* USE_HCRYPTO_TFM */
266 #ifdef USE_HCRYPTO_LTM
271 engine
= ENGINE_new();
275 ENGINE_set_id(engine
, "ltm");
276 ENGINE_set_name(engine
,
277 "Heimdal crypto ltm engine version " PACKAGE_VERSION
);
278 ENGINE_set_RSA(engine
, RSA_ltm_method());
279 ENGINE_set_DH(engine
, DH_ltm_method());
281 ret
= add_engine(engine
);
283 ENGINE_finish(engine
);
291 engine
= ENGINE_new();
295 ENGINE_set_id(engine
, "gmp");
296 ENGINE_set_name(engine
,
297 "Heimdal crypto gmp engine version " PACKAGE_VERSION
);
298 ENGINE_set_RSA(engine
, RSA_gmp_method());
300 ret
= add_engine(engine
);
302 ENGINE_finish(engine
);
307 ENGINE_by_dso(const char *path
, const char *id
)
313 engine
= calloc(1, sizeof(*engine
));
316 engine
->references
= 0; /* ref will be added below */
320 engine
->dso_handle
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
| RTLD_GROUP
);
321 if (engine
->dso_handle
== NULL
) {
322 /* printf("error: %s\n", dlerror()); */
328 unsigned long version
;
329 openssl_v_check v_check
;
331 v_check
= (openssl_v_check
)dlsym(engine
->dso_handle
, "v_check");
332 if (v_check
== NULL
) {
333 dlclose(engine
->dso_handle
);
338 version
= (*v_check
)(OPENSSL_DYNAMIC_VERSION
);
340 dlclose(engine
->dso_handle
);
347 openssl_bind_engine bind_engine
;
350 (openssl_bind_engine
)dlsym(engine
->dso_handle
, "bind_engine");
351 if (bind_engine
== NULL
) {
352 dlclose(engine
->dso_handle
);
357 ret
= (*bind_engine
)(engine
, id
, NULL
); /* XXX fix third arg */
359 dlclose(engine
->dso_handle
);
365 ENGINE_up_ref(engine
);
367 ret
= add_engine(engine
);
369 ENGINE_finish(engine
);
380 ENGINE_by_id(const char *id
)
384 for (i
= 0; i
< num_engines
; i
++) {
385 if (strcmp(id
, engines
[i
]->id
) == 0) {
386 ENGINE_up_ref(engines
[i
]);
394 ENGINE_add_conf_module(void)