1*0Sstevel@tonic-gate /* crypto/engine/eng_lib.c */ 2*0Sstevel@tonic-gate /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 3*0Sstevel@tonic-gate * project 2000. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate /* ==================================================================== 6*0Sstevel@tonic-gate * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 9*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 10*0Sstevel@tonic-gate * are met: 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 14*0Sstevel@tonic-gate * 15*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 16*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 17*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 18*0Sstevel@tonic-gate * distribution. 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 21*0Sstevel@tonic-gate * software must display the following acknowledgment: 22*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 23*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24*0Sstevel@tonic-gate * 25*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26*0Sstevel@tonic-gate * endorse or promote products derived from this software without 27*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 28*0Sstevel@tonic-gate * licensing@OpenSSL.org. 29*0Sstevel@tonic-gate * 30*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 31*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 32*0Sstevel@tonic-gate * permission of the OpenSSL Project. 33*0Sstevel@tonic-gate * 34*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 35*0Sstevel@tonic-gate * acknowledgment: 36*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 37*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38*0Sstevel@tonic-gate * 39*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 51*0Sstevel@tonic-gate * ==================================================================== 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 54*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 55*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 56*0Sstevel@tonic-gate * 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #include <openssl/crypto.h> 60*0Sstevel@tonic-gate #include "cryptlib.h" 61*0Sstevel@tonic-gate #include "eng_int.h" 62*0Sstevel@tonic-gate #include <openssl/rand.h> /* FIXME: This shouldn't be needed */ 63*0Sstevel@tonic-gate #include <openssl/engine.h> 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate /* The "new"/"free" stuff first */ 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate ENGINE *ENGINE_new(void) 68*0Sstevel@tonic-gate { 69*0Sstevel@tonic-gate ENGINE *ret; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE)); 72*0Sstevel@tonic-gate if(ret == NULL) 73*0Sstevel@tonic-gate { 74*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE); 75*0Sstevel@tonic-gate return NULL; 76*0Sstevel@tonic-gate } 77*0Sstevel@tonic-gate memset(ret, 0, sizeof(ENGINE)); 78*0Sstevel@tonic-gate ret->struct_ref = 1; 79*0Sstevel@tonic-gate engine_ref_debug(ret, 0, 1) 80*0Sstevel@tonic-gate CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data); 81*0Sstevel@tonic-gate return ret; 82*0Sstevel@tonic-gate } 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate /* Placed here (close proximity to ENGINE_new) so that modifications to the 85*0Sstevel@tonic-gate * elements of the ENGINE structure are more likely to be caught and changed 86*0Sstevel@tonic-gate * here. */ 87*0Sstevel@tonic-gate void engine_set_all_null(ENGINE *e) 88*0Sstevel@tonic-gate { 89*0Sstevel@tonic-gate e->id = NULL; 90*0Sstevel@tonic-gate e->name = NULL; 91*0Sstevel@tonic-gate e->rsa_meth = NULL; 92*0Sstevel@tonic-gate e->dsa_meth = NULL; 93*0Sstevel@tonic-gate e->dh_meth = NULL; 94*0Sstevel@tonic-gate e->rand_meth = NULL; 95*0Sstevel@tonic-gate e->ciphers = NULL; 96*0Sstevel@tonic-gate e->digests = NULL; 97*0Sstevel@tonic-gate e->destroy = NULL; 98*0Sstevel@tonic-gate e->init = NULL; 99*0Sstevel@tonic-gate e->finish = NULL; 100*0Sstevel@tonic-gate e->ctrl = NULL; 101*0Sstevel@tonic-gate e->load_privkey = NULL; 102*0Sstevel@tonic-gate e->load_pubkey = NULL; 103*0Sstevel@tonic-gate e->cmd_defns = NULL; 104*0Sstevel@tonic-gate e->flags = 0; 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate int engine_free_util(ENGINE *e, int locked) 108*0Sstevel@tonic-gate { 109*0Sstevel@tonic-gate int i; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate if(e == NULL) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_ENGINE_FREE, 114*0Sstevel@tonic-gate ERR_R_PASSED_NULL_PARAMETER); 115*0Sstevel@tonic-gate return 0; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate if(locked) 118*0Sstevel@tonic-gate i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE); 119*0Sstevel@tonic-gate else 120*0Sstevel@tonic-gate i = --e->struct_ref; 121*0Sstevel@tonic-gate engine_ref_debug(e, 0, -1) 122*0Sstevel@tonic-gate if (i > 0) return 1; 123*0Sstevel@tonic-gate #ifdef REF_CHECK 124*0Sstevel@tonic-gate if (i < 0) 125*0Sstevel@tonic-gate { 126*0Sstevel@tonic-gate fprintf(stderr,"ENGINE_free, bad structural reference count\n"); 127*0Sstevel@tonic-gate abort(); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate #endif 130*0Sstevel@tonic-gate /* Give the ENGINE a chance to do any structural cleanup corresponding 131*0Sstevel@tonic-gate * to allocation it did in its constructor (eg. unload error strings) */ 132*0Sstevel@tonic-gate if(e->destroy) 133*0Sstevel@tonic-gate e->destroy(e); 134*0Sstevel@tonic-gate CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data); 135*0Sstevel@tonic-gate OPENSSL_free(e); 136*0Sstevel@tonic-gate return 1; 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate int ENGINE_free(ENGINE *e) 140*0Sstevel@tonic-gate { 141*0Sstevel@tonic-gate return engine_free_util(e, 1); 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /* Cleanup stuff */ 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate /* ENGINE_cleanup() is coded such that anything that does work that will need 147*0Sstevel@tonic-gate * cleanup can register a "cleanup" callback here. That way we don't get linker 148*0Sstevel@tonic-gate * bloat by referring to all *possible* cleanups, but any linker bloat into code 149*0Sstevel@tonic-gate * "X" will cause X's cleanup function to end up here. */ 150*0Sstevel@tonic-gate static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL; 151*0Sstevel@tonic-gate static int int_cleanup_check(int create) 152*0Sstevel@tonic-gate { 153*0Sstevel@tonic-gate if(cleanup_stack) return 1; 154*0Sstevel@tonic-gate if(!create) return 0; 155*0Sstevel@tonic-gate cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null(); 156*0Sstevel@tonic-gate return (cleanup_stack ? 1 : 0); 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb) 159*0Sstevel@tonic-gate { 160*0Sstevel@tonic-gate ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof( 161*0Sstevel@tonic-gate ENGINE_CLEANUP_ITEM)); 162*0Sstevel@tonic-gate if(!item) return NULL; 163*0Sstevel@tonic-gate item->cb = cb; 164*0Sstevel@tonic-gate return item; 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb) 167*0Sstevel@tonic-gate { 168*0Sstevel@tonic-gate ENGINE_CLEANUP_ITEM *item; 169*0Sstevel@tonic-gate if(!int_cleanup_check(1)) return; 170*0Sstevel@tonic-gate item = int_cleanup_item(cb); 171*0Sstevel@tonic-gate if(item) 172*0Sstevel@tonic-gate sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0); 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb) 175*0Sstevel@tonic-gate { 176*0Sstevel@tonic-gate ENGINE_CLEANUP_ITEM *item; 177*0Sstevel@tonic-gate if(!int_cleanup_check(1)) return; 178*0Sstevel@tonic-gate item = int_cleanup_item(cb); 179*0Sstevel@tonic-gate if(item) 180*0Sstevel@tonic-gate sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item); 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate /* The API function that performs all cleanup */ 183*0Sstevel@tonic-gate static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item) 184*0Sstevel@tonic-gate { 185*0Sstevel@tonic-gate (*(item->cb))(); 186*0Sstevel@tonic-gate OPENSSL_free(item); 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate void ENGINE_cleanup(void) 189*0Sstevel@tonic-gate { 190*0Sstevel@tonic-gate if(int_cleanup_check(0)) 191*0Sstevel@tonic-gate { 192*0Sstevel@tonic-gate sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack, 193*0Sstevel@tonic-gate engine_cleanup_cb_free); 194*0Sstevel@tonic-gate cleanup_stack = NULL; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate /* FIXME: This should be handled (somehow) through RAND, eg. by it 197*0Sstevel@tonic-gate * registering a cleanup callback. */ 198*0Sstevel@tonic-gate RAND_set_rand_method(NULL); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* Now the "ex_data" support */ 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 204*0Sstevel@tonic-gate CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 205*0Sstevel@tonic-gate { 206*0Sstevel@tonic-gate return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp, 207*0Sstevel@tonic-gate new_func, dup_func, free_func); 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) 211*0Sstevel@tonic-gate { 212*0Sstevel@tonic-gate return(CRYPTO_set_ex_data(&e->ex_data, idx, arg)); 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate void *ENGINE_get_ex_data(const ENGINE *e, int idx) 216*0Sstevel@tonic-gate { 217*0Sstevel@tonic-gate return(CRYPTO_get_ex_data(&e->ex_data, idx)); 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate /* Functions to get/set an ENGINE's elements - mainly to avoid exposing the 221*0Sstevel@tonic-gate * ENGINE structure itself. */ 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate int ENGINE_set_id(ENGINE *e, const char *id) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate if(id == NULL) 226*0Sstevel@tonic-gate { 227*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_ENGINE_SET_ID, 228*0Sstevel@tonic-gate ERR_R_PASSED_NULL_PARAMETER); 229*0Sstevel@tonic-gate return 0; 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate e->id = id; 232*0Sstevel@tonic-gate return 1; 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate int ENGINE_set_name(ENGINE *e, const char *name) 236*0Sstevel@tonic-gate { 237*0Sstevel@tonic-gate if(name == NULL) 238*0Sstevel@tonic-gate { 239*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_ENGINE_SET_NAME, 240*0Sstevel@tonic-gate ERR_R_PASSED_NULL_PARAMETER); 241*0Sstevel@tonic-gate return 0; 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate e->name = name; 244*0Sstevel@tonic-gate return 1; 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f) 248*0Sstevel@tonic-gate { 249*0Sstevel@tonic-gate e->destroy = destroy_f; 250*0Sstevel@tonic-gate return 1; 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f) 254*0Sstevel@tonic-gate { 255*0Sstevel@tonic-gate e->init = init_f; 256*0Sstevel@tonic-gate return 1; 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f) 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate e->finish = finish_f; 262*0Sstevel@tonic-gate return 1; 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f) 266*0Sstevel@tonic-gate { 267*0Sstevel@tonic-gate e->ctrl = ctrl_f; 268*0Sstevel@tonic-gate return 1; 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate int ENGINE_set_flags(ENGINE *e, int flags) 272*0Sstevel@tonic-gate { 273*0Sstevel@tonic-gate e->flags = flags; 274*0Sstevel@tonic-gate return 1; 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns) 278*0Sstevel@tonic-gate { 279*0Sstevel@tonic-gate e->cmd_defns = defns; 280*0Sstevel@tonic-gate return 1; 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate const char *ENGINE_get_id(const ENGINE *e) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate return e->id; 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate const char *ENGINE_get_name(const ENGINE *e) 289*0Sstevel@tonic-gate { 290*0Sstevel@tonic-gate return e->name; 291*0Sstevel@tonic-gate } 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e) 294*0Sstevel@tonic-gate { 295*0Sstevel@tonic-gate return e->destroy; 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e) 299*0Sstevel@tonic-gate { 300*0Sstevel@tonic-gate return e->init; 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e) 304*0Sstevel@tonic-gate { 305*0Sstevel@tonic-gate return e->finish; 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e) 309*0Sstevel@tonic-gate { 310*0Sstevel@tonic-gate return e->ctrl; 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate int ENGINE_get_flags(const ENGINE *e) 314*0Sstevel@tonic-gate { 315*0Sstevel@tonic-gate return e->flags; 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e) 319*0Sstevel@tonic-gate { 320*0Sstevel@tonic-gate return e->cmd_defns; 321*0Sstevel@tonic-gate } 322