After loading a dynamic engine, reset the command definitions to the
[openssl.git] / crypto / engine / eng_dyn.c
index 57f7696726cc2e1782c064cdc19a9394fa909c52..bac5e712022ca0f31400d9034b519a6bd32e4570 100644 (file)
@@ -1,9 +1,9 @@
-/* crypto/engine/engine_dyn.c */
+/* crypto/engine/eng_dyn.c */
 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
- * project 2000.
+ * project 2001.
  */
 /* ====================================================================
- * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -108,6 +108,9 @@ static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
                ENGINE_CMD_FLAG_NO_INPUT},
        {0, NULL, NULL, 0}
        };
+static const ENGINE_CMD_DEFN dynamic_cmd_defns_empty[] = {
+       {0, NULL, NULL, 0}
+       };
 
 /* Loading code stores state inside the ENGINE structure via the "ex_data"
  * element. We load all our state into a single structure and use that as a
@@ -236,9 +239,7 @@ static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
        return ctx;
        }
 
-/* As this is only ever called once, there's no need for locking
- * (indeed - the lock will already be held by our caller!!!) */
-ENGINE *ENGINE_dynamic(void)
+static ENGINE *engine_dynamic(void)
        {
        ENGINE *ret = ENGINE_new();
        if(!ret)
@@ -257,6 +258,20 @@ ENGINE *ENGINE_dynamic(void)
        return ret;
        }
 
+void ENGINE_load_dynamic(void)
+       {
+       ENGINE *toadd = engine_dynamic();
+       if(!toadd) return;
+       ENGINE_add(toadd);
+       /* If the "add" worked, it gets a structural reference. So either way,
+        * we release our just-created reference. */
+       ENGINE_free(toadd);
+       /* If the "add" didn't work, it was probably a conflict because it was
+        * already added (eg. someone calling ENGINE_load_blah then calling
+        * ENGINE_load_builtin_engines() perhaps). */
+       ERR_clear_error();
+       }
+
 static int dynamic_init(ENGINE *e)
        {
        /* We always return failure - the "dyanamic" engine itself can't be used
@@ -390,6 +405,10 @@ static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
        fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
        fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
        fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
+       /* Now that we've loaded the dynamic engine, initialise the command
+          array to contain none */
+       ENGINE_set_cmd_defns(e, dynamic_cmd_defns_empty);
+
        /* Try to bind the ENGINE onto our own ENGINE structure */
        if(!ctx->bind_engine(e, ctx->engine_id, &fns))
                {