RT4310: Fix varous no-XXX builds
[openssl.git] / crypto / init.c
index 53ed235699a1543c8874724aa2fe000fc3c9bf86..8775b82a228315640e536f0b03904fb0db9c2273 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/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>
+#include <assert.h>
+
+static int stopped = 0;
+
+static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
 
 /* Implement "once" functionality */
 #if !defined(OPENSSL_THREADS)
 typedef int OPENSSL_INIT_ONCE;
 # define OPENSSL_INIT_ONCE_STATIC_INIT          0
-# define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once)   (*(once) = 0)
 
 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
 {
@@ -104,11 +100,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)
 {
+    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)
@@ -123,7 +127,6 @@ void *ossl_init_get_thread_local(int alloc)
  */
 typedef LONG OPENSSL_INIT_ONCE;
 #  define OPENSSL_INIT_ONCE_STATIC_INIT          0
-#  define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once)   (*(once) = 0)
 
 #  define ONCE_UNINITED     0
 #  define ONCE_ININIT       1
@@ -151,8 +154,6 @@ static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
 
 typedef INIT_ONCE OPENSSL_INIT_ONCE;
 #  define OPENSSL_INIT_ONCE_STATIC_INIT          INIT_ONCE_STATIC_INIT
-#  define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once) \
-                InitOnceInitialize((PINIT_ONCE)(once))
 
 static BOOL CALLBACK once_cb(PINIT_ONCE once, PVOID initfp, PVOID *unused)
 {
@@ -169,7 +170,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)
 {
@@ -191,7 +192,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);
 
@@ -199,6 +200,9 @@ void *ossl_init_get_thread_local(int alloc)
         local = OPENSSL_zalloc(sizeof *local);
         TlsSetValue(threadstopkey, local);
     }
+    if (!alloc) {
+        TlsSetValue(threadstopkey, NULL);
+    }
 
     return local;
 }
@@ -206,11 +210,10 @@ 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
-# define OPENSSL_INIT_ONCE_DYNAMIC_INIT(once)   (*(once) = PTHREAD_ONCE_INIT)
 
 static void ossl_init_once_run(OPENSSL_INIT_ONCE *once, void (*init)(void))
 {
@@ -236,7 +239,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);
 
@@ -244,12 +247,16 @@ 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;
 }
 
 #endif
 
+typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
 struct ossl_init_stop_st {
     void (*handler)(void);
     OPENSSL_INIT_STOP *next;
@@ -265,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();
-    atexit(OPENSSL_INIT_library_stop);
+#ifndef OPENSSL_SYS_UEFI
+    atexit(OPENSSL_cleanup);
+#endif
     OPENSSL_cpuid_setup();
     base_inited = 1;
 }
@@ -280,7 +289,11 @@ 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");
@@ -361,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)
@@ -371,6 +385,7 @@ static void ossl_init_async(void)
     async_init();
     async_inited = 1;
 }
+#endif
 
 #ifndef OPENSSL_NO_ENGINE
 static int engine_inited = 0;
@@ -466,12 +481,13 @@ 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)
         return;
 
+#ifndef OPENSSL_NO_ASYNC
     if (locals->async) {
 #ifdef OPENSSL_INIT_DEBUG
         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
@@ -479,6 +495,7 @@ void ossl_init_thread_stop(struct thread_local_inits_st *locals)
 #endif
         ASYNC_cleanup_thread();
     }
+#endif
 
     if (locals->err_state) {
 #ifdef OPENSSL_INIT_DEBUG
@@ -492,6 +509,12 @@ void ossl_init_thread_stop(struct thread_local_inits_st *locals)
     ossl_init_thread_stop_cleanup();
 }
 
+void OPENSSL_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);
@@ -518,10 +541,19 @@ int ossl_init_thread_start(uint64_t opts)
     return 1;
 }
 
-void OPENSSL_INIT_library_stop(void)
+void OPENSSL_cleanup(void)
 {
     OPENSSL_INIT_STOP *currhandler, *lasthandler;
 
+    /* If we've not been inited then no need to deinit */
+    if (!base_inited)
+        return;
+
+    /* Might be explicitly called and also by atexit */
+    if (stopped)
+        return;
+    stopped = 1;
+
     /*
      * Thread stop may not get automatically called by the thread library for
      * the very last thread in some situations, so call it directly.
@@ -543,94 +575,45 @@ void OPENSSL_INIT_library_stop(void)
 
     if (zlib_inited) {
 #ifdef OPENSSL_INIT_DEBUG
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
                         "COMP_zlib_cleanup()\n");
 #endif
         COMP_zlib_cleanup();
-        zlib_inited = 0;
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&zlib);
     }
 
 #ifndef OPENSSL_NO_ENGINE
     if (engine_inited) {
 # ifdef OPENSSL_INIT_DEBUG
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
                         "ENGINE_cleanup()\n");
 # endif
         ENGINE_cleanup();
-        engine_inited = 0;
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_openssl);
-# if !defined(OPENSSL_NO_HW) && \
-    (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_cryptodev);
-# endif
-# ifndef OPENSSL_NO_RDRAND
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_rdrand);
-# endif
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_dynamic);
-# ifndef OPENSSL_NO_STATIC_ENGINE
-#  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_padlock);
-#  endif
-#  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_capi);
-#  endif
-        OPENSSL_INIT_ONCE_DYNAMIC_INIT(&engine_dasync);
-# endif
     }
 #endif
 
-    async_inited = 0;
-    OPENSSL_INIT_ONCE_DYNAMIC_INIT(&async);
-
-    config_inited = 0;
-    OPENSSL_INIT_ONCE_DYNAMIC_INIT(&config);
-    OPENSSL_INIT_ONCE_DYNAMIC_INIT(&add_all_ciphers);
-    OPENSSL_INIT_ONCE_DYNAMIC_INIT(&add_all_digests);
-
     if (load_crypto_strings_inited) {
 #ifdef OPENSSL_INIT_DEBUG
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
+        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
                         "ERR_free_strings()\n");
 #endif
         ERR_free_strings();
-        load_crypto_strings_inited = 0;
-        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);
-    }
-}
-
-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;
+    CRYPTO_cleanup_all_ex_data();
+    EVP_cleanup();
+    CONF_modules_free();
+    RAND_cleanup();
+    base_inited = 0;
 }
 
 /*
@@ -638,9 +621,23 @@ static const OPENSSL_INIT_SETTINGS *ossl_init_get_setting(
  * called prior to any threads making calls to any OpenSSL functions,
  * i.e. passing a non-null settings value is assumed to be single-threaded.
  */
-void OPENSSL_INIT_crypto_library_start(uint64_t opts,
-                                    const OPENSSL_INIT_SETTINGS *settings)
-{
+int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
+{
+    static int stoperrset = 0;
+
+    if (stopped) {
+        if (!stoperrset) {
+            /*
+             * We only ever set this once to avoid getting into an infinite
+             * loop where the error system keeps trying to init and fails so
+             * sets an error etc
+             */
+            stoperrset = 1;
+            CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
+        }
+        return 0;
+    }
+
     ossl_init_once_run(&base, ossl_init_base);
 
     if (opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
@@ -667,19 +664,17 @@ void OPENSSL_INIT_crypto_library_start(uint64_t opts,
     }
 
     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
-        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;
-        }
+        CRYPTO_w_lock(CRYPTO_LOCK_INIT);
+        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);
@@ -722,9 +717,11 @@ void OPENSSL_INIT_crypto_library_start(uint64_t opts,
     if (opts & OPENSSL_INIT_ZLIB) {
         ossl_init_once_run(&zlib, ossl_init_zlib);
     }
+
+    return 1;
 }
 
-int OPENSSL_INIT_register_stop_handler(void (*handler)(void))
+int OPENSSL_atexit(void (*handler)(void))
 {
     OPENSSL_INIT_STOP *newhand;