NULL the thread_local_inits_st pointer after use
[openssl.git] / crypto / init.c
index 8950ff679740cbde2165b47e4daf797e529bb3da..f23227e60a6c97dd998e3e61aad304807539e855 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>
-#if 0
 #include <internal/evp_int.h>
 #include <internal/conf.h>
 #include <internal/async.h>
 #include <internal/engine.h>
-#endif
-#include <openssl/conf.h>
-#include <openssl/async.h>
-#include <openssl/engine.h>
 #include <openssl/comp.h>
-#if 0
 #include <internal/err.h>
-#endif
 #include <stdlib.h>
 
+static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
+
 /* Implement "once" functionality */
 #if !defined(OPENSSL_THREADS)
 typedef int OPENSSL_INIT_ONCE;
@@ -111,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)
@@ -198,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);
 
@@ -206,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;
 }
@@ -243,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);
 
@@ -251,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;
 }
@@ -287,14 +285,16 @@ static void ossl_init_no_load_crypto_strings(void)
 
 static void ossl_init_load_crypto_strings(void)
 {
-#ifndef OPENSSL_NO_ERR
+    /*
+     * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
+     * pulling in all the error strings during static linking
+     */
+#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
 # ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
                     "err_load_crypto_strings_intern()\n");
 # endif
-#if 0
     err_load_crypto_strings_intern();
-#endif
 #endif
     load_crypto_strings_inited = 1;
 }
@@ -311,9 +311,7 @@ static void ossl_init_add_all_ciphers(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
                     "openssl_add_all_ciphers_internal()\n");
 # endif
-#if 0
     openssl_add_all_ciphers_internal();
-#endif
 # ifndef OPENSSL_NO_ENGINE
 #  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
     ENGINE_setup_bsd_cryptodev();
@@ -334,9 +332,7 @@ static void ossl_init_add_all_digests(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
                     "openssl_add_all_digests_internal()\n");
 # endif
-#if 0
     openssl_add_all_digests_internal();
-#endif
 # ifndef OPENSSL_NO_ENGINE
 #  if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)
     ENGINE_setup_bsd_cryptodev();
@@ -361,9 +357,7 @@ static void ossl_init_config(void)
             "OPENSSL_INIT: ossl_init_config: openssl_config_internal(%s)\n",
             config_filename==NULL?"NULL":config_filename);
 #endif
-#if 0
     openssl_config_internal(config_filename);
-#endif
     config_inited = 1;
 }
 static void ossl_init_no_config(void)
@@ -372,9 +366,7 @@ static void ossl_init_no_config(void)
     fprintf(stderr,
             "OPENSSL_INIT: ossl_init_config: openssl_no_config_internal()\n");
 #endif
-#if 0
     openssl_no_config_internal();
-#endif
     config_inited = 1;
 }
 
@@ -385,9 +377,7 @@ static void ossl_init_async(void)
 #ifdef OPENSSL_INIT_DEBUG
     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
 #endif
-#if 0
     async_init();
-#endif
     async_inited = 1;
 }
 
@@ -400,9 +390,7 @@ static void ossl_init_engine_openssl(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
                     "engine_load_openssl_internal()\n");
 # endif
-#if 0
     engine_load_openssl_internal();
-#endif
     engine_inited = 1;
 }
 # if !defined(OPENSSL_NO_HW) && \
@@ -414,9 +402,7 @@ static void ossl_init_engine_cryptodev(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
                     "engine_load_cryptodev_internal()\n");
 #  endif
-#if 0
     engine_load_cryptodev_internal();
-#endif
     engine_inited = 1;
 }
 # endif
@@ -429,9 +415,7 @@ static void ossl_init_engine_rdrand(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
                     "engine_load_rdrand_internal()\n");
 #  endif
-#if 0
     engine_load_rdrand_internal();
-#endif
     engine_inited = 1;
 }
 # endif
@@ -442,9 +426,7 @@ static void ossl_init_engine_dynamic(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
                     "engine_load_dynamic_internal()\n");
 # endif
-#if 0
     engine_load_dynamic_internal();
-#endif
     engine_inited = 1;
 }
 # ifndef OPENSSL_NO_STATIC_ENGINE
@@ -456,9 +438,7 @@ static void ossl_init_engine_padlock(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
                     "engine_load_padlock_internal()\n");
 #   endif
-#if 0
     engine_load_padlock_internal();
-#endif
     engine_inited = 1;
 }
 #  endif
@@ -470,9 +450,7 @@ static void ossl_init_engine_capi(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
                     "engine_load_capi_internal()\n");
 #   endif
-#if 0
     engine_load_capi_internal();
-#endif
     engine_inited = 1;
 }
 #  endif
@@ -483,9 +461,7 @@ static void ossl_init_engine_dasync(void)
     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
                     "engine_load_dasync_internal()\n");
 # endif
-#if 0
     engine_load_dasync_internal();
-#endif
     engine_inited = 1;
 }
 # endif
@@ -499,7 +475,7 @@ static void ossl_init_zlib(void)
     zlib_inited = 1;
 }
 
-void ossl_init_thread_stop(struct thread_local_inits_st *locals)
+static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
 {
     /* Can't do much about this */
     if (locals == NULL)
@@ -525,6 +501,12 @@ void ossl_init_thread_stop(struct thread_local_inits_st *locals)
     ossl_init_thread_stop_cleanup();
 }
 
+void OPENSSL_INIT_thread_stop(void)
+{
+    ossl_init_thread_stop(
+        (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
+}
+
 int ossl_init_thread_start(uint64_t opts)
 {
     struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
@@ -700,13 +682,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) {