Ensure Async is deinited properly
[openssl.git] / crypto / init.c
index cb9d65a4ddd1835a2bccfda30b176fa0891a43e4..613606ebdc8e52e4086fcfefd05999656ca0ab51 100644 (file)
 
 #include <internal/cryptlib_int.h>
 #include <openssl/err.h>
+#include <openssl/rand.h>
 #include <openssl/evp.h>
 #include <internal/evp_int.h>
 #include <internal/conf.h>
 #include <internal/async.h>
+#ifndef OPENSSL_NO_ENGINE
 #include <internal/engine.h>
+#endif
 #include <openssl/comp.h>
 #include <internal/err.h>
 #include <stdlib.h>
@@ -253,6 +256,7 @@ static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
 
 #endif
 
+typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
 struct ossl_init_stop_st {
     void (*handler)(void);
     OPENSSL_INIT_STOP *next;
@@ -268,7 +272,9 @@ static void ossl_init_base(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
 #endif
     ossl_init_setup_thread_stop();
+#ifndef OPENSSL_SYS_UEFI
     atexit(OPENSSL_cleanup);
+#endif
     OPENSSL_cpuid_setup();
     base_inited = 1;
 }
@@ -368,6 +374,7 @@ static void ossl_init_no_config(void)
     config_inited = 1;
 }
 
+#ifndef OPENSSL_NO_ASYNC
 static OPENSSL_INIT_ONCE async = OPENSSL_INIT_ONCE_STATIC_INIT;
 static int async_inited = 0;
 static void ossl_init_async(void)
@@ -378,6 +385,7 @@ static void ossl_init_async(void)
     async_init();
     async_inited = 1;
 }
+#endif
 
 #ifndef OPENSSL_NO_ENGINE
 static int engine_inited = 0;
@@ -479,6 +487,7 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
     if (locals == NULL)
         return;
 
+#ifndef OPENSSL_NO_ASYNC
     if (locals->async) {
 #ifdef OPENSSL_INIT_DEBUG
         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
@@ -486,6 +495,7 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
 #endif
         ASYNC_cleanup_thread();
     }
+#endif
 
     if (locals->err_state) {
 #ifdef OPENSSL_INIT_DEBUG
@@ -496,7 +506,6 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
     }
 
     OPENSSL_free(locals);
-    ossl_init_thread_stop_cleanup();
 }
 
 void OPENSSL_thread_stop(void)
@@ -571,6 +580,16 @@ void OPENSSL_cleanup(void)
         COMP_zlib_cleanup();
     }
 
+#ifndef OPENSSL_NO_ASYNC
+    if (async_inited) {
+# ifdef OPENSSL_INIT_DEBUG
+        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
+                        "async_deinit()\n");
+# endif
+        async_deinit();
+    }
+#endif
+
 #ifndef OPENSSL_NO_ENGINE
     if (engine_inited) {
 # ifdef OPENSSL_INIT_DEBUG
@@ -589,6 +608,8 @@ void OPENSSL_cleanup(void)
         ERR_free_strings();
     }
 
+    ossl_init_thread_stop_cleanup();
+
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
                     "CRYPTO_cleanup_all_ex_data()\n");
@@ -606,21 +627,6 @@ void OPENSSL_cleanup(void)
     base_inited = 0;
 }
 
-static const OPENSSL_INIT_SETTINGS *ossl_init_get_setting(
-        const OPENSSL_INIT_SETTINGS *settings, int name)
-{
-    if (settings == NULL)
-        return NULL;
-
-    while (settings->name != OPENSSL_INIT_SET_END) {
-        if (settings->name == name)
-            return settings;
-        settings++;
-    }
-
-    return NULL;
-}
-
 /*
  * If this function is called with a non NULL settings value then it must be
  * called prior to any threads making calls to any OpenSSL functions,
@@ -638,8 +644,7 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
              * sets an error etc
              */
             stoperrset = 1;
-            CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO_LIBRARY_START,
-                      ERR_R_INIT_FAIL);
+            CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
         }
         return 0;
     }
@@ -671,22 +676,16 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
 
     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
         CRYPTO_w_lock(CRYPTO_LOCK_INIT);
-        if (settings != NULL) {
-            const OPENSSL_INIT_SETTINGS *curr;
-            curr = ossl_init_get_setting(settings,
-                                         OPENSSL_INIT_SET_CONF_FILENAME);
-            config_filename = (curr == NULL) ? NULL : curr->value.type_string;
-        } else {
-            config_filename = NULL;
-        }
+        config_filename = (settings == NULL) ? NULL : settings->config_name;
         ossl_init_once_run(&config, ossl_init_config);
         CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
     }
 
+#ifndef OPENSSL_NO_ASYNC
     if (opts & OPENSSL_INIT_ASYNC) {
         ossl_init_once_run(&async, ossl_init_async);
     }
-
+#endif
 #ifndef OPENSSL_NO_ENGINE
     if (opts & OPENSSL_INIT_ENGINE_OPENSSL) {
         ossl_init_once_run(&engine_openssl, ossl_init_engine_openssl);