1109998Smarkm/* crypto/engine/eng_lib.c */ 2280304Sjkim/* 3280304Sjkim * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project 4280304Sjkim * 2000. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * licensing@OpenSSL.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60109998Smarkm#include "eng_int.h" 61160814Ssimon#include <openssl/rand.h> 62109998Smarkm 63109998Smarkm/* The "new"/"free" stuff first */ 64109998Smarkm 65109998SmarkmENGINE *ENGINE_new(void) 66280304Sjkim{ 67280304Sjkim ENGINE *ret; 68109998Smarkm 69280304Sjkim ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE)); 70280304Sjkim if (ret == NULL) { 71280304Sjkim ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE); 72280304Sjkim return NULL; 73280304Sjkim } 74280304Sjkim memset(ret, 0, sizeof(ENGINE)); 75280304Sjkim ret->struct_ref = 1; 76280304Sjkim engine_ref_debug(ret, 0, 1) 77280304Sjkim CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data); 78280304Sjkim return ret; 79280304Sjkim} 80109998Smarkm 81280304Sjkim/* 82280304Sjkim * Placed here (close proximity to ENGINE_new) so that modifications to the 83109998Smarkm * elements of the ENGINE structure are more likely to be caught and changed 84280304Sjkim * here. 85280304Sjkim */ 86109998Smarkmvoid engine_set_all_null(ENGINE *e) 87280304Sjkim{ 88280304Sjkim e->id = NULL; 89280304Sjkim e->name = NULL; 90280304Sjkim e->rsa_meth = NULL; 91280304Sjkim e->dsa_meth = NULL; 92280304Sjkim e->dh_meth = NULL; 93280304Sjkim e->rand_meth = NULL; 94280304Sjkim e->store_meth = NULL; 95280304Sjkim e->ciphers = NULL; 96280304Sjkim e->digests = NULL; 97280304Sjkim e->destroy = NULL; 98280304Sjkim e->init = NULL; 99280304Sjkim e->finish = NULL; 100280304Sjkim e->ctrl = NULL; 101280304Sjkim e->load_privkey = NULL; 102280304Sjkim e->load_pubkey = NULL; 103280304Sjkim e->cmd_defns = NULL; 104280304Sjkim e->flags = 0; 105280304Sjkim} 106109998Smarkm 107109998Smarkmint engine_free_util(ENGINE *e, int locked) 108280304Sjkim{ 109280304Sjkim int i; 110109998Smarkm 111280304Sjkim if (e == NULL) { 112280304Sjkim ENGINEerr(ENGINE_F_ENGINE_FREE_UTIL, ERR_R_PASSED_NULL_PARAMETER); 113280304Sjkim return 0; 114280304Sjkim } 115280304Sjkim if (locked) 116280304Sjkim i = CRYPTO_add(&e->struct_ref, -1, CRYPTO_LOCK_ENGINE); 117280304Sjkim else 118280304Sjkim i = --e->struct_ref; 119280304Sjkim engine_ref_debug(e, 0, -1) 120280304Sjkim if (i > 0) 121280304Sjkim return 1; 122109998Smarkm#ifdef REF_CHECK 123280304Sjkim if (i < 0) { 124280304Sjkim fprintf(stderr, "ENGINE_free, bad structural reference count\n"); 125280304Sjkim abort(); 126280304Sjkim } 127109998Smarkm#endif 128280304Sjkim /* Free up any dynamically allocated public key methods */ 129280304Sjkim engine_pkey_meths_free(e); 130280304Sjkim engine_pkey_asn1_meths_free(e); 131280304Sjkim /* 132280304Sjkim * Give the ENGINE a chance to do any structural cleanup corresponding to 133280304Sjkim * allocation it did in its constructor (eg. unload error strings) 134280304Sjkim */ 135280304Sjkim if (e->destroy) 136280304Sjkim e->destroy(e); 137280304Sjkim CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data); 138280304Sjkim OPENSSL_free(e); 139280304Sjkim return 1; 140280304Sjkim} 141109998Smarkm 142109998Smarkmint ENGINE_free(ENGINE *e) 143280304Sjkim{ 144280304Sjkim return engine_free_util(e, 1); 145280304Sjkim} 146109998Smarkm 147109998Smarkm/* Cleanup stuff */ 148109998Smarkm 149280304Sjkim/* 150280304Sjkim * ENGINE_cleanup() is coded such that anything that does work that will need 151280304Sjkim * cleanup can register a "cleanup" callback here. That way we don't get 152280304Sjkim * linker bloat by referring to all *possible* cleanups, but any linker bloat 153280304Sjkim * into code "X" will cause X's cleanup function to end up here. 154280304Sjkim */ 155109998Smarkmstatic STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL; 156109998Smarkmstatic int int_cleanup_check(int create) 157280304Sjkim{ 158280304Sjkim if (cleanup_stack) 159280304Sjkim return 1; 160280304Sjkim if (!create) 161280304Sjkim return 0; 162280304Sjkim cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null(); 163280304Sjkim return (cleanup_stack ? 1 : 0); 164280304Sjkim} 165280304Sjkim 166109998Smarkmstatic ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb) 167280304Sjkim{ 168280304Sjkim ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof(ENGINE_CLEANUP_ITEM)); 169280304Sjkim if (!item) 170280304Sjkim return NULL; 171280304Sjkim item->cb = cb; 172280304Sjkim return item; 173280304Sjkim} 174280304Sjkim 175109998Smarkmvoid engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb) 176280304Sjkim{ 177280304Sjkim ENGINE_CLEANUP_ITEM *item; 178280304Sjkim if (!int_cleanup_check(1)) 179280304Sjkim return; 180280304Sjkim item = int_cleanup_item(cb); 181280304Sjkim if (item) 182280304Sjkim sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0); 183280304Sjkim} 184280304Sjkim 185109998Smarkmvoid engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb) 186280304Sjkim{ 187280304Sjkim ENGINE_CLEANUP_ITEM *item; 188280304Sjkim if (!int_cleanup_check(1)) 189280304Sjkim return; 190280304Sjkim item = int_cleanup_item(cb); 191280304Sjkim if (item) 192280304Sjkim sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item); 193280304Sjkim} 194280304Sjkim 195109998Smarkm/* The API function that performs all cleanup */ 196109998Smarkmstatic void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item) 197280304Sjkim{ 198280304Sjkim (*(item->cb)) (); 199280304Sjkim OPENSSL_free(item); 200280304Sjkim} 201280304Sjkim 202109998Smarkmvoid ENGINE_cleanup(void) 203280304Sjkim{ 204280304Sjkim if (int_cleanup_check(0)) { 205280304Sjkim sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack, 206280304Sjkim engine_cleanup_cb_free); 207280304Sjkim cleanup_stack = NULL; 208280304Sjkim } 209280304Sjkim /* 210280304Sjkim * FIXME: This should be handled (somehow) through RAND, eg. by it 211280304Sjkim * registering a cleanup callback. 212280304Sjkim */ 213280304Sjkim RAND_set_rand_method(NULL); 214280304Sjkim} 215109998Smarkm 216109998Smarkm/* Now the "ex_data" support */ 217109998Smarkm 218109998Smarkmint ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 219280304Sjkim CRYPTO_EX_dup *dup_func, 220280304Sjkim CRYPTO_EX_free *free_func) 221280304Sjkim{ 222280304Sjkim return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp, 223280304Sjkim new_func, dup_func, free_func); 224280304Sjkim} 225109998Smarkm 226109998Smarkmint ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) 227280304Sjkim{ 228280304Sjkim return (CRYPTO_set_ex_data(&e->ex_data, idx, arg)); 229280304Sjkim} 230109998Smarkm 231109998Smarkmvoid *ENGINE_get_ex_data(const ENGINE *e, int idx) 232280304Sjkim{ 233280304Sjkim return (CRYPTO_get_ex_data(&e->ex_data, idx)); 234280304Sjkim} 235109998Smarkm 236280304Sjkim/* 237280304Sjkim * Functions to get/set an ENGINE's elements - mainly to avoid exposing the 238280304Sjkim * ENGINE structure itself. 239280304Sjkim */ 240109998Smarkm 241109998Smarkmint ENGINE_set_id(ENGINE *e, const char *id) 242280304Sjkim{ 243280304Sjkim if (id == NULL) { 244280304Sjkim ENGINEerr(ENGINE_F_ENGINE_SET_ID, ERR_R_PASSED_NULL_PARAMETER); 245280304Sjkim return 0; 246280304Sjkim } 247280304Sjkim e->id = id; 248280304Sjkim return 1; 249280304Sjkim} 250109998Smarkm 251109998Smarkmint ENGINE_set_name(ENGINE *e, const char *name) 252280304Sjkim{ 253280304Sjkim if (name == NULL) { 254280304Sjkim ENGINEerr(ENGINE_F_ENGINE_SET_NAME, ERR_R_PASSED_NULL_PARAMETER); 255280304Sjkim return 0; 256280304Sjkim } 257280304Sjkim e->name = name; 258280304Sjkim return 1; 259280304Sjkim} 260109998Smarkm 261109998Smarkmint ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f) 262280304Sjkim{ 263280304Sjkim e->destroy = destroy_f; 264280304Sjkim return 1; 265280304Sjkim} 266109998Smarkm 267109998Smarkmint ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f) 268280304Sjkim{ 269280304Sjkim e->init = init_f; 270280304Sjkim return 1; 271280304Sjkim} 272109998Smarkm 273109998Smarkmint ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f) 274280304Sjkim{ 275280304Sjkim e->finish = finish_f; 276280304Sjkim return 1; 277280304Sjkim} 278109998Smarkm 279109998Smarkmint ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f) 280280304Sjkim{ 281280304Sjkim e->ctrl = ctrl_f; 282280304Sjkim return 1; 283280304Sjkim} 284109998Smarkm 285109998Smarkmint ENGINE_set_flags(ENGINE *e, int flags) 286280304Sjkim{ 287280304Sjkim e->flags = flags; 288280304Sjkim return 1; 289280304Sjkim} 290109998Smarkm 291109998Smarkmint ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns) 292280304Sjkim{ 293280304Sjkim e->cmd_defns = defns; 294280304Sjkim return 1; 295280304Sjkim} 296109998Smarkm 297109998Smarkmconst char *ENGINE_get_id(const ENGINE *e) 298280304Sjkim{ 299280304Sjkim return e->id; 300280304Sjkim} 301109998Smarkm 302109998Smarkmconst char *ENGINE_get_name(const ENGINE *e) 303280304Sjkim{ 304280304Sjkim return e->name; 305280304Sjkim} 306109998Smarkm 307109998SmarkmENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e) 308280304Sjkim{ 309280304Sjkim return e->destroy; 310280304Sjkim} 311109998Smarkm 312109998SmarkmENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e) 313280304Sjkim{ 314280304Sjkim return e->init; 315280304Sjkim} 316109998Smarkm 317109998SmarkmENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e) 318280304Sjkim{ 319280304Sjkim return e->finish; 320280304Sjkim} 321109998Smarkm 322109998SmarkmENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e) 323280304Sjkim{ 324280304Sjkim return e->ctrl; 325280304Sjkim} 326109998Smarkm 327109998Smarkmint ENGINE_get_flags(const ENGINE *e) 328280304Sjkim{ 329280304Sjkim return e->flags; 330280304Sjkim} 331109998Smarkm 332109998Smarkmconst ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e) 333280304Sjkim{ 334280304Sjkim return e->cmd_defns; 335280304Sjkim} 336160814Ssimon 337280304Sjkim/* 338280304Sjkim * eng_lib.o is pretty much linked into anything that touches ENGINE already, 339280304Sjkim * so put the "static_state" hack here. 340280304Sjkim */ 341160814Ssimon 342160814Ssimonstatic int internal_static_hack = 0; 343160814Ssimon 344160814Ssimonvoid *ENGINE_get_static_state(void) 345280304Sjkim{ 346280304Sjkim return &internal_static_hack; 347280304Sjkim} 348