| 1 |
/* |
| 2 |
* Copyright (c) 2006 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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. |
| 16 |
* |
| 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. |
| 20 |
* |
| 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 |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
#include <roken.h> |
| 36 |
|
| 37 |
#include <engine.h> |
| 38 |
|
| 39 |
#ifdef HAVE_DLFCN_H |
| 40 |
#include <dlfcn.h> |
| 41 |
#ifndef RTLD_NOW |
| 42 |
#define RTLD_NOW 0 |
| 43 |
#endif |
| 44 |
#endif |
| 45 |
|
| 46 |
struct hc_engine { |
| 47 |
int references; |
| 48 |
char *name; |
| 49 |
char *id; |
| 50 |
void (*destroy)(ENGINE *); |
| 51 |
const RSA_METHOD *rsa; |
| 52 |
const DH_METHOD *dh; |
| 53 |
const RAND_METHOD *rand; |
| 54 |
}; |
| 55 |
|
| 56 |
ENGINE * |
| 57 |
ENGINE_new(void) |
| 58 |
{ |
| 59 |
ENGINE *engine; |
| 60 |
|
| 61 |
engine = calloc(1, sizeof(*engine)); |
| 62 |
engine->references = 1; |
| 63 |
|
| 64 |
return engine; |
| 65 |
} |
| 66 |
|
| 67 |
int |
| 68 |
ENGINE_free(ENGINE *engine) |
| 69 |
{ |
| 70 |
return ENGINE_finish(engine); |
| 71 |
} |
| 72 |
|
| 73 |
int |
| 74 |
ENGINE_finish(ENGINE *engine) |
| 75 |
{ |
| 76 |
if (engine->references-- <= 0) |
| 77 |
abort(); |
| 78 |
if (engine->references > 0) |
| 79 |
return 1; |
| 80 |
|
| 81 |
if (engine->name) |
| 82 |
free(engine->name); |
| 83 |
if (engine->id) |
| 84 |
free(engine->id); |
| 85 |
if(engine->destroy) |
| 86 |
(*engine->destroy)(engine); |
| 87 |
|
| 88 |
memset(engine, 0, sizeof(*engine)); |
| 89 |
engine->references = -1; |
| 90 |
|
| 91 |
|
| 92 |
free(engine); |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
int |
| 97 |
ENGINE_up_ref(ENGINE *engine) |
| 98 |
{ |
| 99 |
if (engine->references < 0) |
| 100 |
abort(); |
| 101 |
engine->references++; |
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
int |
| 106 |
ENGINE_set_id(ENGINE *engine, const char *id) |
| 107 |
{ |
| 108 |
engine->id = strdup(id); |
| 109 |
return (engine->id == NULL) ? 0 : 1; |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
ENGINE_set_name(ENGINE *engine, const char *name) |
| 114 |
{ |
| 115 |
engine->name = strdup(name); |
| 116 |
return (engine->name == NULL) ? 0 : 1; |
| 117 |
} |
| 118 |
|
| 119 |
int |
| 120 |
ENGINE_set_RSA(ENGINE *engine, const RSA_METHOD *method) |
| 121 |
{ |
| 122 |
engine->rsa = method; |
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
int |
| 127 |
ENGINE_set_DH(ENGINE *engine, const DH_METHOD *method) |
| 128 |
{ |
| 129 |
engine->dh = method; |
| 130 |
return 1; |
| 131 |
} |
| 132 |
|
| 133 |
int |
| 134 |
ENGINE_set_destroy_function(ENGINE *e, void (*destroy)(ENGINE *)) |
| 135 |
{ |
| 136 |
e->destroy = destroy; |
| 137 |
return 1; |
| 138 |
} |
| 139 |
|
| 140 |
const char * |
| 141 |
ENGINE_get_id(const ENGINE *engine) |
| 142 |
{ |
| 143 |
return engine->id; |
| 144 |
} |
| 145 |
|
| 146 |
const char * |
| 147 |
ENGINE_get_name(const ENGINE *engine) |
| 148 |
{ |
| 149 |
return engine->name; |
| 150 |
} |
| 151 |
|
| 152 |
const RSA_METHOD * |
| 153 |
ENGINE_get_RSA(const ENGINE *engine) |
| 154 |
{ |
| 155 |
return engine->rsa; |
| 156 |
} |
| 157 |
|
| 158 |
const DH_METHOD * |
| 159 |
ENGINE_get_DH(const ENGINE *engine) |
| 160 |
{ |
| 161 |
return engine->dh; |
| 162 |
} |
| 163 |
|
| 164 |
const RAND_METHOD * |
| 165 |
ENGINE_get_RAND(const ENGINE *engine) |
| 166 |
{ |
| 167 |
return engine->rand; |
| 168 |
} |
| 169 |
|
| 170 |
/* |
| 171 |
* |
| 172 |
*/ |
| 173 |
|
| 174 |
#define SG_default_engine(type) \ |
| 175 |
static ENGINE *type##_engine; \ |
| 176 |
int \ |
| 177 |
ENGINE_set_default_##type(ENGINE *engine) \ |
| 178 |
{ \ |
| 179 |
if (type##_engine) \ |
| 180 |
ENGINE_finish(type##_engine); \ |
| 181 |
type##_engine = engine; \ |
| 182 |
if (type##_engine) \ |
| 183 |
ENGINE_up_ref(type##_engine); \ |
| 184 |
return 1; \ |
| 185 |
} \ |
| 186 |
ENGINE * \ |
| 187 |
ENGINE_get_default_##type(void) \ |
| 188 |
{ \ |
| 189 |
if (type##_engine) \ |
| 190 |
ENGINE_up_ref(type##_engine); \ |
| 191 |
return type##_engine; \ |
| 192 |
} |
| 193 |
|
| 194 |
SG_default_engine(RSA) |
| 195 |
SG_default_engine(DH) |
| 196 |
|
| 197 |
#undef SG_default_engine |
| 198 |
|
| 199 |
/* |
| 200 |
* |
| 201 |
*/ |
| 202 |
|
| 203 |
static ENGINE **engines; |
| 204 |
static unsigned int num_engines; |
| 205 |
|
| 206 |
static int |
| 207 |
add_engine(ENGINE *engine) |
| 208 |
{ |
| 209 |
ENGINE **d, *dup; |
| 210 |
|
| 211 |
dup = ENGINE_by_id(engine->id); |
| 212 |
if (dup) |
| 213 |
return 0; |
| 214 |
|
| 215 |
d = realloc(engines, (num_engines + 1) * sizeof(*engines)); |
| 216 |
if (d == NULL) |
| 217 |
return 1; |
| 218 |
engines = d; |
| 219 |
engines[num_engines++] = engine; |
| 220 |
|
| 221 |
return 1; |
| 222 |
} |
| 223 |
|
| 224 |
void |
| 225 |
ENGINE_load_builtin_engines(void) |
| 226 |
{ |
| 227 |
ENGINE *engine; |
| 228 |
int ret; |
| 229 |
|
| 230 |
engine = ENGINE_new(); |
| 231 |
if (engine == NULL) |
| 232 |
return; |
| 233 |
|
| 234 |
ENGINE_set_id(engine, "builtin"); |
| 235 |
ENGINE_set_name(engine, |
| 236 |
"Heimdal crypto builtin (ltm) engine version " PACKAGE_VERSION); |
| 237 |
ENGINE_set_RSA(engine, RSA_ltm_method()); |
| 238 |
ENGINE_set_DH(engine, DH_ltm_method()); |
| 239 |
|
| 240 |
ret = add_engine(engine); |
| 241 |
if (ret != 1) |
| 242 |
ENGINE_finish(engine); |
| 243 |
|
| 244 |
#ifdef USE_HCRYPTO_TFM |
| 245 |
/* |
| 246 |
* TFM |
| 247 |
*/ |
| 248 |
|
| 249 |
engine = ENGINE_new(); |
| 250 |
if (engine == NULL) |
| 251 |
return; |
| 252 |
|
| 253 |
ENGINE_set_id(engine, "tfm"); |
| 254 |
ENGINE_set_name(engine, |
| 255 |
"Heimdal crypto tfm engine version " PACKAGE_VERSION); |
| 256 |
ENGINE_set_RSA(engine, RSA_tfm_method()); |
| 257 |
ENGINE_set_DH(engine, DH_tfm_method()); |
| 258 |
|
| 259 |
ret = add_engine(engine); |
| 260 |
if (ret != 1) |
| 261 |
ENGINE_finish(engine); |
| 262 |
#endif /* USE_HCRYPTO_TFM */ |
| 263 |
|
| 264 |
#ifdef USE_HCRYPTO_LTM |
| 265 |
/* |
| 266 |
* ltm |
| 267 |
*/ |
| 268 |
|
| 269 |
engine = ENGINE_new(); |
| 270 |
if (engine == NULL) |
| 271 |
return; |
| 272 |
|
| 273 |
ENGINE_set_id(engine, "ltm"); |
| 274 |
ENGINE_set_name(engine, |
| 275 |
"Heimdal crypto ltm engine version " PACKAGE_VERSION); |
| 276 |
ENGINE_set_RSA(engine, RSA_ltm_method()); |
| 277 |
ENGINE_set_DH(engine, DH_ltm_method()); |
| 278 |
|
| 279 |
ret = add_engine(engine); |
| 280 |
if (ret != 1) |
| 281 |
ENGINE_finish(engine); |
| 282 |
#endif |
| 283 |
|
| 284 |
#ifdef HAVE_GMP |
| 285 |
/* |
| 286 |
* gmp |
| 287 |
*/ |
| 288 |
|
| 289 |
engine = ENGINE_new(); |
| 290 |
if (engine == NULL) |
| 291 |
return; |
| 292 |
|
| 293 |
ENGINE_set_id(engine, "gmp"); |
| 294 |
ENGINE_set_name(engine, |
| 295 |
"Heimdal crypto gmp engine version " PACKAGE_VERSION); |
| 296 |
ENGINE_set_RSA(engine, RSA_gmp_method()); |
| 297 |
|
| 298 |
ret = add_engine(engine); |
| 299 |
if (ret != 1) |
| 300 |
ENGINE_finish(engine); |
| 301 |
#endif |
| 302 |
} |
| 303 |
|
| 304 |
ENGINE * |
| 305 |
ENGINE_by_dso(const char *path, const char *id) |
| 306 |
{ |
| 307 |
#ifdef HAVE_DLOPEN |
| 308 |
ENGINE *engine; |
| 309 |
void *handle; |
| 310 |
int ret; |
| 311 |
|
| 312 |
engine = calloc(1, sizeof(*engine)); |
| 313 |
if (engine == NULL) |
| 314 |
return NULL; |
| 315 |
|
| 316 |
handle = dlopen(path, RTLD_NOW); |
| 317 |
if (handle == NULL) { |
| 318 |
/* printf("error: %s\n", dlerror()); */ |
| 319 |
free(engine); |
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
{ |
| 324 |
unsigned long version; |
| 325 |
openssl_v_check v_check; |
| 326 |
|
| 327 |
v_check = (openssl_v_check)dlsym(handle, "v_check"); |
| 328 |
if (v_check == NULL) { |
| 329 |
dlclose(handle); |
| 330 |
free(engine); |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
|
| 334 |
version = (*v_check)(OPENSSL_DYNAMIC_VERSION); |
| 335 |
if (version == 0) { |
| 336 |
dlclose(handle); |
| 337 |
free(engine); |
| 338 |
return NULL; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
{ |
| 343 |
openssl_bind_engine bind_engine; |
| 344 |
|
| 345 |
bind_engine = (openssl_bind_engine)dlsym(handle, "bind_engine"); |
| 346 |
if (bind_engine == NULL) { |
| 347 |
dlclose(handle); |
| 348 |
free(engine); |
| 349 |
return NULL; |
| 350 |
} |
| 351 |
|
| 352 |
ret = (*bind_engine)(engine, id, NULL); /* XXX fix third arg */ |
| 353 |
if (ret != 1) { |
| 354 |
dlclose(handle); |
| 355 |
free(engine); |
| 356 |
return NULL; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
ENGINE_up_ref(engine); |
| 361 |
|
| 362 |
ret = add_engine(engine); |
| 363 |
if (ret != 1) { |
| 364 |
dlclose(handle); |
| 365 |
ENGINE_finish(engine); |
| 366 |
return NULL; |
| 367 |
} |
| 368 |
|
| 369 |
return engine; |
| 370 |
#else |
| 371 |
return NULL; |
| 372 |
#endif |
| 373 |
} |
| 374 |
|
| 375 |
ENGINE * |
| 376 |
ENGINE_by_id(const char *id) |
| 377 |
{ |
| 378 |
int i; |
| 379 |
|
| 380 |
for (i = 0; i < num_engines; i++) { |
| 381 |
if (strcmp(id, engines[i]->id) == 0) { |
| 382 |
ENGINE_up_ref(engines[i]); |
| 383 |
return engines[i]; |
| 384 |
} |
| 385 |
} |
| 386 |
return NULL; |
| 387 |
} |
| 388 |
|
| 389 |
void |
| 390 |
ENGINE_add_conf_module(void) |
| 391 |
{ |
| 392 |
} |