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
57 void (*destroy)(ENGINE *);
58 const RSA_METHOD *rsa;
60 const RAND_METHOD *rand;
64 ENGINE_finish(ENGINE *engine)
66 if (engine->references-- <= 0)
68 if (engine->references > 0)
76 (*engine->destroy)(engine);
78 memset(engine, 0, sizeof(engine));
79 engine->references = -1;
87 ENGINE_up_ref(ENGINE *engine)
89 if (engine->references < 0)
96 ENGINE_set_id(ENGINE *engine, const char *id)
98 engine->id = strdup(id);
99 return (engine->id == NULL) ? 0 : 1;
103 ENGINE_set_name(ENGINE *engine, const char *name)
105 engine->name = strdup(name);
106 return (engine->name == NULL) ? 0 : 1;
110 ENGINE_set_RSA(ENGINE *engine, const RSA_METHOD *method)
112 engine->rsa = method;
117 ENGINE_set_DH(ENGINE *engine, const DH_METHOD *method)
124 ENGINE_set_destroy_function(ENGINE *e, void (*destroy)(ENGINE *))
126 e->destroy = destroy;
131 ENGINE_get_id(const ENGINE *engine)
137 ENGINE_get_name(const ENGINE *engine)
143 ENGINE_get_RSA(const ENGINE *engine)
149 ENGINE_get_DH(const ENGINE *engine)
155 ENGINE_get_RAND(const ENGINE *engine)
164 #define SG_default_engine(type) \
165 static ENGINE *type##_engine; \
167 ENGINE_set_default_##type(ENGINE *engine) \
170 ENGINE_finish(type##_engine); \
171 type##_engine = engine; \
173 ENGINE_up_ref(type##_engine); \
177 ENGINE_get_default_##type(void) \
180 ENGINE_up_ref(type##_engine); \
181 return type##_engine; \
184 SG_default_engine(RSA)
185 SG_default_engine(DH)
187 #undef SG_default_engine
193 static ENGINE **engines;
194 static unsigned int num_engines;
197 add_engine(ENGINE *engine)
201 dup = ENGINE_by_id(engine->id);
207 d = realloc(engines, (num_engines + 1) * sizeof(*engines));
211 engines[num_engines++] = engine;
217 ENGINE_load_builtin_engines(void)
222 engine = calloc(1, sizeof(*engine));
226 ENGINE_set_id(engine, "builtin");
227 ENGINE_set_name(engine,
228 "Heimdal crypto builtin engine version " PACKAGE_VERSION);
229 ENGINE_set_RSA(engine, RSA_imath_method());
230 ENGINE_set_DH(engine, DH_imath_method());
232 ret = add_engine(engine);
234 ENGINE_finish(engine);
238 ENGINE_by_dso(const char *path, const char *id)
245 engine = calloc(1, sizeof(*engine));
249 handle = dlopen(path, RTLD_NOW);
250 if (handle == NULL) {
251 /* printf("error: %s\n", dlerror()); */
257 unsigned long version;
258 openssl_v_check v_check;
260 v_check = (openssl_v_check)dlsym(handle, "v_check");
261 if (v_check == NULL) {
267 version = (*v_check)(OPENSSL_DYNAMIC_VERSION);
276 openssl_bind_engine bind_engine;
278 bind_engine = (openssl_bind_engine)dlsym(handle, "bind_engine");
279 if (bind_engine == NULL) {
285 ret = (*bind_engine)(engine, id, NULL); /* XXX fix third arg */
293 ENGINE_up_ref(engine);
295 ret = add_engine(engine);
298 ENGINE_finish(engine);
309 ENGINE_by_id(const char *id)
313 for (i = 0; i < num_engines; i++) {
314 if (strcmp(id, engines[i]->id) == 0) {
315 ENGINE_up_ref(engines[i]);
323 ENGINE_add_conf_module(void)