1 /* $NetBSD: engine.c,v 1.3 2014/04/24 13:45:34 pettai Exp $ */
4 * Copyright (c) 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 void (*destroy
)(ENGINE
*);
56 const RSA_METHOD
*rsa
;
58 const RAND_METHOD
*rand
;
66 engine
= calloc(1, sizeof(*engine
));
67 engine
->references
= 1;
73 ENGINE_free(ENGINE
*engine
)
75 return ENGINE_finish(engine
);
79 ENGINE_finish(ENGINE
*engine
)
81 if (engine
->references
-- <= 0)
83 if (engine
->references
> 0)
91 (*engine
->destroy
)(engine
);
93 memset(engine
, 0, sizeof(*engine
));
94 engine
->references
= -1;
102 ENGINE_up_ref(ENGINE
*engine
)
104 if (engine
->references
< 0)
106 engine
->references
++;
111 ENGINE_set_id(ENGINE
*engine
, const char *id
)
113 engine
->id
= strdup(id
);
114 return (engine
->id
== NULL
) ? 0 : 1;
118 ENGINE_set_name(ENGINE
*engine
, const char *name
)
120 engine
->name
= strdup(name
);
121 return (engine
->name
== NULL
) ? 0 : 1;
125 ENGINE_set_RSA(ENGINE
*engine
, const RSA_METHOD
*method
)
127 engine
->rsa
= method
;
132 ENGINE_set_DH(ENGINE
*engine
, const DH_METHOD
*method
)
139 ENGINE_set_destroy_function(ENGINE
*e
, void (*destroy
)(ENGINE
*))
141 e
->destroy
= destroy
;
146 ENGINE_get_id(const ENGINE
*engine
)
152 ENGINE_get_name(const ENGINE
*engine
)
158 ENGINE_get_RSA(const ENGINE
*engine
)
164 ENGINE_get_DH(const ENGINE
*engine
)
170 ENGINE_get_RAND(const ENGINE
*engine
)
179 #define SG_default_engine(type) \
180 static ENGINE *type##_engine; \
182 ENGINE_set_default_##type(ENGINE *engine) \
185 ENGINE_finish(type##_engine); \
186 type##_engine = engine; \
188 ENGINE_up_ref(type##_engine); \
192 ENGINE_get_default_##type(void) \
195 ENGINE_up_ref(type##_engine); \
196 return type##_engine; \
199 SG_default_engine(RSA
)
200 SG_default_engine(DH
)
202 #undef SG_default_engine
208 static ENGINE
**engines
;
209 static unsigned int num_engines
;
212 add_engine(ENGINE
*engine
)
216 dup
= ENGINE_by_id(engine
->id
);
220 d
= realloc(engines
, (num_engines
+ 1) * sizeof(*engines
));
224 engines
[num_engines
++] = engine
;
230 ENGINE_load_builtin_engines(void)
235 engine
= ENGINE_new();
239 ENGINE_set_id(engine
, "builtin");
240 ENGINE_set_name(engine
,
241 "Heimdal crypto builtin (ltm) engine version " PACKAGE_VERSION
);
242 ENGINE_set_RSA(engine
, RSA_ltm_method());
243 ENGINE_set_DH(engine
, DH_ltm_method());
245 ret
= add_engine(engine
);
247 ENGINE_finish(engine
);
249 #ifdef USE_HCRYPTO_TFM
254 engine
= ENGINE_new();
258 ENGINE_set_id(engine
, "tfm");
259 ENGINE_set_name(engine
,
260 "Heimdal crypto tfm engine version " PACKAGE_VERSION
);
261 ENGINE_set_RSA(engine
, RSA_tfm_method());
262 ENGINE_set_DH(engine
, DH_tfm_method());
264 ret
= add_engine(engine
);
266 ENGINE_finish(engine
);
267 #endif /* USE_HCRYPTO_TFM */
269 #ifdef USE_HCRYPTO_LTM
274 engine
= ENGINE_new();
278 ENGINE_set_id(engine
, "ltm");
279 ENGINE_set_name(engine
,
280 "Heimdal crypto ltm engine version " PACKAGE_VERSION
);
281 ENGINE_set_RSA(engine
, RSA_ltm_method());
282 ENGINE_set_DH(engine
, DH_ltm_method());
284 ret
= add_engine(engine
);
286 ENGINE_finish(engine
);
294 engine
= ENGINE_new();
298 ENGINE_set_id(engine
, "gmp");
299 ENGINE_set_name(engine
,
300 "Heimdal crypto gmp engine version " PACKAGE_VERSION
);
301 ENGINE_set_RSA(engine
, RSA_gmp_method());
303 ret
= add_engine(engine
);
305 ENGINE_finish(engine
);
310 ENGINE_by_dso(const char *path
, const char *id
)
317 engine
= calloc(1, sizeof(*engine
));
321 handle
= dlopen(path
, RTLD_NOW
);
322 if (handle
== NULL
) {
323 /* printf("error: %s\n", dlerror()); */
329 unsigned long version
;
330 openssl_v_check v_check
;
332 v_check
= (openssl_v_check
)dlsym(handle
, "v_check");
333 if (v_check
== NULL
) {
339 version
= (*v_check
)(OPENSSL_DYNAMIC_VERSION
);
348 openssl_bind_engine bind_engine
;
350 bind_engine
= (openssl_bind_engine
)dlsym(handle
, "bind_engine");
351 if (bind_engine
== NULL
) {
357 ret
= (*bind_engine
)(engine
, id
, NULL
); /* XXX fix third arg */
365 ENGINE_up_ref(engine
);
367 ret
= add_engine(engine
);
370 ENGINE_finish(engine
);
381 ENGINE_by_id(const char *id
)
385 for (i
= 0; i
< num_engines
; i
++) {
386 if (strcmp(id
, engines
[i
]->id
) == 0) {
387 ENGINE_up_ref(engines
[i
]);
395 ENGINE_add_conf_module(void)