If we've not been inited don't deinit
[openssl.git] / crypto / init.c
index bce8438b9561d9b02515009e6223eb613606c001..3b55a4300f4fab572a38f46b4326ab601d73182d 100644 (file)
  *
  */
 
-#include <openssl/e_os2.h>
-
-#if defined(OPENSSL_SYS_WINDOWS) && !defined(_WIN32_WINNT)
-/*
- * We default to requiring Windows Vista, Windows Server 2008 or later. We can
- * support lower versions if _WIN32_WINNT is explicity defined to something
- * less
- */
-# define _WIN32_WINNT 0x0600
-#endif
-
 #include <internal/cryptlib_int.h>
 #include <openssl/err.h>
 #include <openssl/evp.h>
@@ -106,11 +95,19 @@ static void ossl_init_thread_stop_cleanup(void)
 }
 
 static struct thread_local_inits_st *local = NULL;
-void *ossl_init_get_thread_local(int alloc)
+static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
 {
+    static struct thread_local_inits_st *tmp;
+
+    tmp = local;
+
     if (local == NULL && alloc)
-        local = OPENSSL_zalloc(sizeof(*local));
-    return local;
+        tmp = local = OPENSSL_zalloc(sizeof(*local));
+
+    if (!alloc)
+        local = NULL;
+
+    return tmp;
 }
 
 #elif defined(OPENSSL_SYS_WINDOWS)
@@ -171,7 +168,7 @@ static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
 }
 # endif
 
-DWORD threadstopkey = TLS_OUT_OF_INDEXES;
+static DWORD threadstopkey = TLS_OUT_OF_INDEXES;
 
 static int ossl_init_setup_thread_stop(void)
 {
@@ -193,7 +190,7 @@ static void ossl_init_thread_stop_cleanup(void)
     }
 }
 
-void *ossl_init_get_thread_local(int alloc)
+static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
 {
     struct thread_local_inits_st *local = TlsGetValue(threadstopkey);
 
@@ -201,6 +198,9 @@ void *ossl_init_get_thread_local(int alloc)
         local = OPENSSL_zalloc(sizeof *local);
         TlsSetValue(threadstopkey, local);
     }
+    if (!alloc) {
+        TlsSetValue(threadstopkey, NULL);
+    }
 
     return local;
 }
@@ -208,7 +208,7 @@ void *ossl_init_get_thread_local(int alloc)
 #else /* pthreads */
 # include <pthread.h>
 
-pthread_key_t threadstopkey;
+static pthread_key_t threadstopkey;
 
 typedef pthread_once_t OPENSSL_INIT_ONCE;
 # define OPENSSL_INIT_ONCE_STATIC_INIT          PTHREAD_ONCE_INIT
@@ -238,7 +238,7 @@ static void ossl_init_thread_stop_cleanup(void)
 {
 }
 
-void *ossl_init_get_thread_local(int alloc)
+static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
 {
     struct thread_local_inits_st *local = pthread_getspecific(threadstopkey);
 
@@ -246,6 +246,9 @@ void *ossl_init_get_thread_local(int alloc)
         local = OPENSSL_zalloc(sizeof *local);
         pthread_setspecific(threadstopkey, local);
     }
+    if (!alloc) {
+        pthread_setspecific(threadstopkey, NULL);
+    }
 
     return local;
 }
@@ -534,6 +537,10 @@ void OPENSSL_INIT_library_stop(void)
 {
     OPENSSL_INIT_STOP *currhandler, *lasthandler;
 
+    /* If we've not been inited then no need to deinit */
+    if (!base_inited)
+        return;
+
     /*
      * Thread stop may not get automatically called by the thread library for
      * the very last thread in some situations, so call it directly.
@@ -610,24 +617,22 @@ void OPENSSL_INIT_library_stop(void)
         OPENSSL_INIT_ONCE_DYNAMIC_INIT(&load_crypto_strings);
     }
 
-    if (base_inited) {
 #ifdef OPENSSL_INIT_DEBUG
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
-                        "CRYPTO_cleanup_all_ex_data()\n");
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
-                        "EVP_cleanup()\n");
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
-                        "CONF_modules_free()\n");
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
-                        "RAND_cleanup()\n");
+    fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+                    "CRYPTO_cleanup_all_ex_data()\n");
+    fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+                    "EVP_cleanup()\n");
+    fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+                    "CONF_modules_free()\n");
+    fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+                    "RAND_cleanup()\n");
 #endif
-        CRYPTO_cleanup_all_ex_data();
-        EVP_cleanup();
-        CONF_modules_free();
-        RAND_cleanup();
-        base_inited = 0;
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&base);
-    }
+    CRYPTO_cleanup_all_ex_data();
+    EVP_cleanup();
+    CONF_modules_free();
+    RAND_cleanup();
+    base_inited = 0;
+    OPENSSL_INIT_ONCE_DYNAMIC_INIT(&base);
 }
 
 static const OPENSSL_INIT_SETTINGS *ossl_init_get_setting(
@@ -679,13 +684,17 @@ void OPENSSL_INIT_crypto_library_start(uint64_t opts,
     }
 
     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;
+            config_filename = (curr == NULL) ? NULL : curr->value.type_string;
+        } else {
+            config_filename = NULL;
         }
         ossl_init_once_run(&config, ossl_init_config);
+        CRYPTO_w_unlock(CRYPTO_LOCK_INIT);
     }
 
     if (opts & OPENSSL_INIT_ASYNC) {