1*0Sstevel@tonic-gate /* crypto/engine/eng_dyn.c */ 2*0Sstevel@tonic-gate /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 3*0Sstevel@tonic-gate * project 2001. 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 60*0Sstevel@tonic-gate #include <stdio.h> 61*0Sstevel@tonic-gate #include <openssl/crypto.h> 62*0Sstevel@tonic-gate #include "cryptlib.h" 63*0Sstevel@tonic-gate #include "eng_int.h" 64*0Sstevel@tonic-gate #include <openssl/engine.h> 65*0Sstevel@tonic-gate #include <openssl/dso.h> 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate /* Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE loader 68*0Sstevel@tonic-gate * should implement the hook-up functions with the following prototypes. */ 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate /* Our ENGINE handlers */ 71*0Sstevel@tonic-gate static int dynamic_init(ENGINE *e); 72*0Sstevel@tonic-gate static int dynamic_finish(ENGINE *e); 73*0Sstevel@tonic-gate static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); 74*0Sstevel@tonic-gate /* Predeclare our context type */ 75*0Sstevel@tonic-gate typedef struct st_dynamic_data_ctx dynamic_data_ctx; 76*0Sstevel@tonic-gate /* The implementation for the important control command */ 77*0Sstevel@tonic-gate static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx); 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate #define DYNAMIC_CMD_SO_PATH ENGINE_CMD_BASE 80*0Sstevel@tonic-gate #define DYNAMIC_CMD_NO_VCHECK (ENGINE_CMD_BASE + 1) 81*0Sstevel@tonic-gate #define DYNAMIC_CMD_ID (ENGINE_CMD_BASE + 2) 82*0Sstevel@tonic-gate #define DYNAMIC_CMD_LIST_ADD (ENGINE_CMD_BASE + 3) 83*0Sstevel@tonic-gate #define DYNAMIC_CMD_LOAD (ENGINE_CMD_BASE + 4) 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate /* The constants used when creating the ENGINE */ 86*0Sstevel@tonic-gate static const char *engine_dynamic_id = "dynamic"; 87*0Sstevel@tonic-gate static const char *engine_dynamic_name = "Dynamic engine loading support"; 88*0Sstevel@tonic-gate static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = { 89*0Sstevel@tonic-gate {DYNAMIC_CMD_SO_PATH, 90*0Sstevel@tonic-gate "SO_PATH", 91*0Sstevel@tonic-gate "Specifies the path to the new ENGINE shared library", 92*0Sstevel@tonic-gate ENGINE_CMD_FLAG_STRING}, 93*0Sstevel@tonic-gate {DYNAMIC_CMD_NO_VCHECK, 94*0Sstevel@tonic-gate "NO_VCHECK", 95*0Sstevel@tonic-gate "Specifies to continue even if version checking fails (boolean)", 96*0Sstevel@tonic-gate ENGINE_CMD_FLAG_NUMERIC}, 97*0Sstevel@tonic-gate {DYNAMIC_CMD_ID, 98*0Sstevel@tonic-gate "ID", 99*0Sstevel@tonic-gate "Specifies an ENGINE id name for loading", 100*0Sstevel@tonic-gate ENGINE_CMD_FLAG_STRING}, 101*0Sstevel@tonic-gate {DYNAMIC_CMD_LIST_ADD, 102*0Sstevel@tonic-gate "LIST_ADD", 103*0Sstevel@tonic-gate "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)", 104*0Sstevel@tonic-gate ENGINE_CMD_FLAG_NUMERIC}, 105*0Sstevel@tonic-gate {DYNAMIC_CMD_LOAD, 106*0Sstevel@tonic-gate "LOAD", 107*0Sstevel@tonic-gate "Load up the ENGINE specified by other settings", 108*0Sstevel@tonic-gate ENGINE_CMD_FLAG_NO_INPUT}, 109*0Sstevel@tonic-gate {0, NULL, NULL, 0} 110*0Sstevel@tonic-gate }; 111*0Sstevel@tonic-gate static const ENGINE_CMD_DEFN dynamic_cmd_defns_empty[] = { 112*0Sstevel@tonic-gate {0, NULL, NULL, 0} 113*0Sstevel@tonic-gate }; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate /* Loading code stores state inside the ENGINE structure via the "ex_data" 116*0Sstevel@tonic-gate * element. We load all our state into a single structure and use that as a 117*0Sstevel@tonic-gate * single context in the "ex_data" stack. */ 118*0Sstevel@tonic-gate struct st_dynamic_data_ctx 119*0Sstevel@tonic-gate { 120*0Sstevel@tonic-gate /* The DSO object we load that supplies the ENGINE code */ 121*0Sstevel@tonic-gate DSO *dynamic_dso; 122*0Sstevel@tonic-gate /* The function pointer to the version checking shared library function */ 123*0Sstevel@tonic-gate dynamic_v_check_fn v_check; 124*0Sstevel@tonic-gate /* The function pointer to the engine-binding shared library function */ 125*0Sstevel@tonic-gate dynamic_bind_engine bind_engine; 126*0Sstevel@tonic-gate /* The default name/path for loading the shared library */ 127*0Sstevel@tonic-gate const char *DYNAMIC_LIBNAME; 128*0Sstevel@tonic-gate /* Whether to continue loading on a version check failure */ 129*0Sstevel@tonic-gate int no_vcheck; 130*0Sstevel@tonic-gate /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */ 131*0Sstevel@tonic-gate const char *engine_id; 132*0Sstevel@tonic-gate /* If non-zero, a successfully loaded ENGINE should be added to the internal 133*0Sstevel@tonic-gate * ENGINE list. If 2, the add must succeed or the entire load should fail. */ 134*0Sstevel@tonic-gate int list_add_value; 135*0Sstevel@tonic-gate /* The symbol name for the version checking function */ 136*0Sstevel@tonic-gate const char *DYNAMIC_F1; 137*0Sstevel@tonic-gate /* The symbol name for the "initialise ENGINE structure" function */ 138*0Sstevel@tonic-gate const char *DYNAMIC_F2; 139*0Sstevel@tonic-gate }; 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate /* This is the "ex_data" index we obtain and reserve for use with our context 142*0Sstevel@tonic-gate * structure. */ 143*0Sstevel@tonic-gate static int dynamic_ex_data_idx = -1; 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* Because our ex_data element may or may not get allocated depending on whether 146*0Sstevel@tonic-gate * a "first-use" occurs before the ENGINE is freed, we have a memory leak 147*0Sstevel@tonic-gate * problem to solve. We can't declare a "new" handler for the ex_data as we 148*0Sstevel@tonic-gate * don't want a dynamic_data_ctx in *all* ENGINE structures of all types (this 149*0Sstevel@tonic-gate * is a bug in the design of CRYPTO_EX_DATA). As such, we just declare a "free" 150*0Sstevel@tonic-gate * handler and that will get called if an ENGINE is being destroyed and there 151*0Sstevel@tonic-gate * was an ex_data element corresponding to our context type. */ 152*0Sstevel@tonic-gate static void dynamic_data_ctx_free_func(void *parent, void *ptr, 153*0Sstevel@tonic-gate CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) 154*0Sstevel@tonic-gate { 155*0Sstevel@tonic-gate if(ptr) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr; 158*0Sstevel@tonic-gate if(ctx->dynamic_dso) 159*0Sstevel@tonic-gate DSO_free(ctx->dynamic_dso); 160*0Sstevel@tonic-gate if(ctx->DYNAMIC_LIBNAME) 161*0Sstevel@tonic-gate OPENSSL_free((void*)ctx->DYNAMIC_LIBNAME); 162*0Sstevel@tonic-gate if(ctx->engine_id) 163*0Sstevel@tonic-gate OPENSSL_free((void*)ctx->engine_id); 164*0Sstevel@tonic-gate OPENSSL_free(ctx); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* Construct the per-ENGINE context. We create it blindly and then use a lock to 169*0Sstevel@tonic-gate * check for a race - if so, all but one of the threads "racing" will have 170*0Sstevel@tonic-gate * wasted their time. The alternative involves creating everything inside the 171*0Sstevel@tonic-gate * lock which is far worse. */ 172*0Sstevel@tonic-gate static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx) 173*0Sstevel@tonic-gate { 174*0Sstevel@tonic-gate dynamic_data_ctx *c; 175*0Sstevel@tonic-gate c = OPENSSL_malloc(sizeof(dynamic_data_ctx)); 176*0Sstevel@tonic-gate if(!c) 177*0Sstevel@tonic-gate { 178*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_SET_DATA_CTX,ERR_R_MALLOC_FAILURE); 179*0Sstevel@tonic-gate return 0; 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate memset(c, 0, sizeof(dynamic_data_ctx)); 182*0Sstevel@tonic-gate c->dynamic_dso = NULL; 183*0Sstevel@tonic-gate c->v_check = NULL; 184*0Sstevel@tonic-gate c->bind_engine = NULL; 185*0Sstevel@tonic-gate c->DYNAMIC_LIBNAME = NULL; 186*0Sstevel@tonic-gate c->no_vcheck = 0; 187*0Sstevel@tonic-gate c->engine_id = NULL; 188*0Sstevel@tonic-gate c->list_add_value = 0; 189*0Sstevel@tonic-gate c->DYNAMIC_F1 = "v_check"; 190*0Sstevel@tonic-gate c->DYNAMIC_F2 = "bind_engine"; 191*0Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 192*0Sstevel@tonic-gate if((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, 193*0Sstevel@tonic-gate dynamic_ex_data_idx)) == NULL) 194*0Sstevel@tonic-gate { 195*0Sstevel@tonic-gate /* Good, we're the first */ 196*0Sstevel@tonic-gate ENGINE_set_ex_data(e, dynamic_ex_data_idx, c); 197*0Sstevel@tonic-gate *ctx = c; 198*0Sstevel@tonic-gate c = NULL; 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 201*0Sstevel@tonic-gate /* If we lost the race to set the context, c is non-NULL and *ctx is the 202*0Sstevel@tonic-gate * context of the thread that won. */ 203*0Sstevel@tonic-gate if(c) 204*0Sstevel@tonic-gate OPENSSL_free(c); 205*0Sstevel@tonic-gate return 1; 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate /* This function retrieves the context structure from an ENGINE's "ex_data", or 209*0Sstevel@tonic-gate * if it doesn't exist yet, sets it up. */ 210*0Sstevel@tonic-gate static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e) 211*0Sstevel@tonic-gate { 212*0Sstevel@tonic-gate dynamic_data_ctx *ctx; 213*0Sstevel@tonic-gate if(dynamic_ex_data_idx < 0) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate /* Create and register the ENGINE ex_data, and associate our 216*0Sstevel@tonic-gate * "free" function with it to ensure any allocated contexts get 217*0Sstevel@tonic-gate * freed when an ENGINE goes underground. */ 218*0Sstevel@tonic-gate int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 219*0Sstevel@tonic-gate dynamic_data_ctx_free_func); 220*0Sstevel@tonic-gate if(new_idx == -1) 221*0Sstevel@tonic-gate { 222*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX,ENGINE_R_NO_INDEX); 223*0Sstevel@tonic-gate return NULL; 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 226*0Sstevel@tonic-gate /* Avoid a race by checking again inside this lock */ 227*0Sstevel@tonic-gate if(dynamic_ex_data_idx < 0) 228*0Sstevel@tonic-gate { 229*0Sstevel@tonic-gate /* Good, someone didn't beat us to it */ 230*0Sstevel@tonic-gate dynamic_ex_data_idx = new_idx; 231*0Sstevel@tonic-gate new_idx = -1; 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 234*0Sstevel@tonic-gate /* In theory we could "give back" the index here if 235*0Sstevel@tonic-gate * (new_idx>-1), but it's not possible and wouldn't gain us much 236*0Sstevel@tonic-gate * if it were. */ 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx); 239*0Sstevel@tonic-gate /* Check if the context needs to be created */ 240*0Sstevel@tonic-gate if((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx)) 241*0Sstevel@tonic-gate /* "set_data" will set errors if necessary */ 242*0Sstevel@tonic-gate return NULL; 243*0Sstevel@tonic-gate return ctx; 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate static ENGINE *engine_dynamic(void) 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate ENGINE *ret = ENGINE_new(); 249*0Sstevel@tonic-gate if(!ret) 250*0Sstevel@tonic-gate return NULL; 251*0Sstevel@tonic-gate if(!ENGINE_set_id(ret, engine_dynamic_id) || 252*0Sstevel@tonic-gate !ENGINE_set_name(ret, engine_dynamic_name) || 253*0Sstevel@tonic-gate !ENGINE_set_init_function(ret, dynamic_init) || 254*0Sstevel@tonic-gate !ENGINE_set_finish_function(ret, dynamic_finish) || 255*0Sstevel@tonic-gate !ENGINE_set_ctrl_function(ret, dynamic_ctrl) || 256*0Sstevel@tonic-gate !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) || 257*0Sstevel@tonic-gate !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) 258*0Sstevel@tonic-gate { 259*0Sstevel@tonic-gate ENGINE_free(ret); 260*0Sstevel@tonic-gate return NULL; 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate return ret; 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate void ENGINE_load_dynamic(void) 266*0Sstevel@tonic-gate { 267*0Sstevel@tonic-gate ENGINE *toadd = engine_dynamic(); 268*0Sstevel@tonic-gate if(!toadd) return; 269*0Sstevel@tonic-gate ENGINE_add(toadd); 270*0Sstevel@tonic-gate /* If the "add" worked, it gets a structural reference. So either way, 271*0Sstevel@tonic-gate * we release our just-created reference. */ 272*0Sstevel@tonic-gate ENGINE_free(toadd); 273*0Sstevel@tonic-gate /* If the "add" didn't work, it was probably a conflict because it was 274*0Sstevel@tonic-gate * already added (eg. someone calling ENGINE_load_blah then calling 275*0Sstevel@tonic-gate * ENGINE_load_builtin_engines() perhaps). */ 276*0Sstevel@tonic-gate ERR_clear_error(); 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate static int dynamic_init(ENGINE *e) 280*0Sstevel@tonic-gate { 281*0Sstevel@tonic-gate /* We always return failure - the "dyanamic" engine itself can't be used 282*0Sstevel@tonic-gate * for anything. */ 283*0Sstevel@tonic-gate return 0; 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate static int dynamic_finish(ENGINE *e) 287*0Sstevel@tonic-gate { 288*0Sstevel@tonic-gate /* This should never be called on account of "dynamic_init" always 289*0Sstevel@tonic-gate * failing. */ 290*0Sstevel@tonic-gate return 0; 291*0Sstevel@tonic-gate } 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()) 294*0Sstevel@tonic-gate { 295*0Sstevel@tonic-gate dynamic_data_ctx *ctx = dynamic_get_data_ctx(e); 296*0Sstevel@tonic-gate int initialised; 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate if(!ctx) 299*0Sstevel@tonic-gate { 300*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_CTRL,ENGINE_R_NOT_LOADED); 301*0Sstevel@tonic-gate return 0; 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1); 304*0Sstevel@tonic-gate /* All our control commands require the ENGINE to be uninitialised */ 305*0Sstevel@tonic-gate if(initialised) 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_CTRL, 308*0Sstevel@tonic-gate ENGINE_R_ALREADY_LOADED); 309*0Sstevel@tonic-gate return 0; 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate switch(cmd) 312*0Sstevel@tonic-gate { 313*0Sstevel@tonic-gate case DYNAMIC_CMD_SO_PATH: 314*0Sstevel@tonic-gate /* a NULL 'p' or a string of zero-length is the same thing */ 315*0Sstevel@tonic-gate if(p && (strlen((const char *)p) < 1)) 316*0Sstevel@tonic-gate p = NULL; 317*0Sstevel@tonic-gate if(ctx->DYNAMIC_LIBNAME) 318*0Sstevel@tonic-gate OPENSSL_free((void*)ctx->DYNAMIC_LIBNAME); 319*0Sstevel@tonic-gate if(p) 320*0Sstevel@tonic-gate ctx->DYNAMIC_LIBNAME = BUF_strdup(p); 321*0Sstevel@tonic-gate else 322*0Sstevel@tonic-gate ctx->DYNAMIC_LIBNAME = NULL; 323*0Sstevel@tonic-gate return (ctx->DYNAMIC_LIBNAME ? 1 : 0); 324*0Sstevel@tonic-gate case DYNAMIC_CMD_NO_VCHECK: 325*0Sstevel@tonic-gate ctx->no_vcheck = ((i == 0) ? 0 : 1); 326*0Sstevel@tonic-gate return 1; 327*0Sstevel@tonic-gate case DYNAMIC_CMD_ID: 328*0Sstevel@tonic-gate /* a NULL 'p' or a string of zero-length is the same thing */ 329*0Sstevel@tonic-gate if(p && (strlen((const char *)p) < 1)) 330*0Sstevel@tonic-gate p = NULL; 331*0Sstevel@tonic-gate if(ctx->engine_id) 332*0Sstevel@tonic-gate OPENSSL_free((void*)ctx->engine_id); 333*0Sstevel@tonic-gate if(p) 334*0Sstevel@tonic-gate ctx->engine_id = BUF_strdup(p); 335*0Sstevel@tonic-gate else 336*0Sstevel@tonic-gate ctx->engine_id = NULL; 337*0Sstevel@tonic-gate return (ctx->engine_id ? 1 : 0); 338*0Sstevel@tonic-gate case DYNAMIC_CMD_LIST_ADD: 339*0Sstevel@tonic-gate if((i < 0) || (i > 2)) 340*0Sstevel@tonic-gate { 341*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_CTRL, 342*0Sstevel@tonic-gate ENGINE_R_INVALID_ARGUMENT); 343*0Sstevel@tonic-gate return 0; 344*0Sstevel@tonic-gate } 345*0Sstevel@tonic-gate ctx->list_add_value = (int)i; 346*0Sstevel@tonic-gate return 1; 347*0Sstevel@tonic-gate case DYNAMIC_CMD_LOAD: 348*0Sstevel@tonic-gate return dynamic_load(e, ctx); 349*0Sstevel@tonic-gate default: 350*0Sstevel@tonic-gate break; 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED); 353*0Sstevel@tonic-gate return 0; 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx) 357*0Sstevel@tonic-gate { 358*0Sstevel@tonic-gate ENGINE cpy; 359*0Sstevel@tonic-gate dynamic_fns fns; 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate if(!ctx->DYNAMIC_LIBNAME || ((ctx->dynamic_dso = DSO_load(NULL, 362*0Sstevel@tonic-gate ctx->DYNAMIC_LIBNAME, NULL, 0)) == NULL)) 363*0Sstevel@tonic-gate { 364*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_LOAD, 365*0Sstevel@tonic-gate ENGINE_R_DSO_NOT_FOUND); 366*0Sstevel@tonic-gate return 0; 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate /* We have to find a bind function otherwise it'll always end badly */ 369*0Sstevel@tonic-gate if(!(ctx->bind_engine = (dynamic_bind_engine)DSO_bind_func( 370*0Sstevel@tonic-gate ctx->dynamic_dso, ctx->DYNAMIC_F2))) 371*0Sstevel@tonic-gate { 372*0Sstevel@tonic-gate ctx->bind_engine = NULL; 373*0Sstevel@tonic-gate DSO_free(ctx->dynamic_dso); 374*0Sstevel@tonic-gate ctx->dynamic_dso = NULL; 375*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_LOAD, 376*0Sstevel@tonic-gate ENGINE_R_DSO_FAILURE); 377*0Sstevel@tonic-gate return 0; 378*0Sstevel@tonic-gate } 379*0Sstevel@tonic-gate /* Do we perform version checking? */ 380*0Sstevel@tonic-gate if(!ctx->no_vcheck) 381*0Sstevel@tonic-gate { 382*0Sstevel@tonic-gate unsigned long vcheck_res = 0; 383*0Sstevel@tonic-gate /* Now we try to find a version checking function and decide how 384*0Sstevel@tonic-gate * to cope with failure if/when it fails. */ 385*0Sstevel@tonic-gate ctx->v_check = (dynamic_v_check_fn)DSO_bind_func( 386*0Sstevel@tonic-gate ctx->dynamic_dso, ctx->DYNAMIC_F1); 387*0Sstevel@tonic-gate if(ctx->v_check) 388*0Sstevel@tonic-gate vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION); 389*0Sstevel@tonic-gate /* We fail if the version checker veto'd the load *or* if it is 390*0Sstevel@tonic-gate * deferring to us (by returning its version) and we think it is 391*0Sstevel@tonic-gate * too old. */ 392*0Sstevel@tonic-gate if(vcheck_res < OSSL_DYNAMIC_OLDEST) 393*0Sstevel@tonic-gate { 394*0Sstevel@tonic-gate /* Fail */ 395*0Sstevel@tonic-gate ctx->bind_engine = NULL; 396*0Sstevel@tonic-gate ctx->v_check = NULL; 397*0Sstevel@tonic-gate DSO_free(ctx->dynamic_dso); 398*0Sstevel@tonic-gate ctx->dynamic_dso = NULL; 399*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_LOAD, 400*0Sstevel@tonic-gate ENGINE_R_VERSION_INCOMPATIBILITY); 401*0Sstevel@tonic-gate return 0; 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate } 404*0Sstevel@tonic-gate /* First binary copy the ENGINE structure so that we can roll back if 405*0Sstevel@tonic-gate * the hand-over fails */ 406*0Sstevel@tonic-gate memcpy(&cpy, e, sizeof(ENGINE)); 407*0Sstevel@tonic-gate /* Provide the ERR, "ex_data", memory, and locking callbacks so the 408*0Sstevel@tonic-gate * loaded library uses our state rather than its own. FIXME: As noted in 409*0Sstevel@tonic-gate * engine.h, much of this would be simplified if each area of code 410*0Sstevel@tonic-gate * provided its own "summary" structure of all related callbacks. It 411*0Sstevel@tonic-gate * would also increase opaqueness. */ 412*0Sstevel@tonic-gate fns.err_fns = ERR_get_implementation(); 413*0Sstevel@tonic-gate fns.ex_data_fns = CRYPTO_get_ex_data_implementation(); 414*0Sstevel@tonic-gate CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb, 415*0Sstevel@tonic-gate &fns.mem_fns.realloc_cb, 416*0Sstevel@tonic-gate &fns.mem_fns.free_cb); 417*0Sstevel@tonic-gate fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback(); 418*0Sstevel@tonic-gate fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback(); 419*0Sstevel@tonic-gate fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback(); 420*0Sstevel@tonic-gate fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback(); 421*0Sstevel@tonic-gate fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback(); 422*0Sstevel@tonic-gate /* Now that we've loaded the dynamic engine, make sure no "dynamic" 423*0Sstevel@tonic-gate * ENGINE elements will show through. */ 424*0Sstevel@tonic-gate engine_set_all_null(e); 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate /* Try to bind the ENGINE onto our own ENGINE structure */ 427*0Sstevel@tonic-gate if(!ctx->bind_engine(e, ctx->engine_id, &fns)) 428*0Sstevel@tonic-gate { 429*0Sstevel@tonic-gate ctx->bind_engine = NULL; 430*0Sstevel@tonic-gate ctx->v_check = NULL; 431*0Sstevel@tonic-gate DSO_free(ctx->dynamic_dso); 432*0Sstevel@tonic-gate ctx->dynamic_dso = NULL; 433*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_LOAD,ENGINE_R_INIT_FAILED); 434*0Sstevel@tonic-gate /* Copy the original ENGINE structure back */ 435*0Sstevel@tonic-gate memcpy(e, &cpy, sizeof(ENGINE)); 436*0Sstevel@tonic-gate return 0; 437*0Sstevel@tonic-gate } 438*0Sstevel@tonic-gate /* Do we try to add this ENGINE to the internal list too? */ 439*0Sstevel@tonic-gate if(ctx->list_add_value > 0) 440*0Sstevel@tonic-gate { 441*0Sstevel@tonic-gate if(!ENGINE_add(e)) 442*0Sstevel@tonic-gate { 443*0Sstevel@tonic-gate /* Do we tolerate this or fail? */ 444*0Sstevel@tonic-gate if(ctx->list_add_value > 1) 445*0Sstevel@tonic-gate { 446*0Sstevel@tonic-gate /* Fail - NB: By this time, it's too late to 447*0Sstevel@tonic-gate * rollback, and trying to do so allows the 448*0Sstevel@tonic-gate * bind_engine() code to have created leaks. We 449*0Sstevel@tonic-gate * just have to fail where we are, after the 450*0Sstevel@tonic-gate * ENGINE has changed. */ 451*0Sstevel@tonic-gate ENGINEerr(ENGINE_F_DYNAMIC_LOAD, 452*0Sstevel@tonic-gate ENGINE_R_CONFLICTING_ENGINE_ID); 453*0Sstevel@tonic-gate return 0; 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate /* Tolerate */ 456*0Sstevel@tonic-gate ERR_clear_error(); 457*0Sstevel@tonic-gate } 458*0Sstevel@tonic-gate } 459*0Sstevel@tonic-gate return 1; 460*0Sstevel@tonic-gate } 461