]> git.ipfire.org Git - thirdparty/openssl.git/blobdiff - crypto/trace.c
Move e_os.h to include/internal
[thirdparty/openssl.git] / crypto / trace.c
index d9524da1a6878e0370cb01e94f4ff0ef84e76d2a..a9a097f1a3142754d74a85be9bf084468341ee5b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
 #include <stdio.h>
 #include <string.h>
 
+#include "internal/thread_once.h"
 #include <openssl/bio.h>
 #include <openssl/crypto.h>
 #include <openssl/trace.h>
 #include "internal/bio.h"
 #include "internal/nelem.h"
-#include "internal/cryptlib_int.h"
+#include "internal/refcount.h"
+#include "crypto/cryptlib.h"
 
-#include "e_os.h"                /* strcasecmp for Windows */
+#include "internal/e_os.h"                /* strcasecmp for Windows */
 
 #ifndef OPENSSL_NO_TRACE
 
@@ -125,13 +127,20 @@ static const struct trace_category_st trace_categories[] = {
     TRACE_CATEGORY_(TLS),
     TRACE_CATEGORY_(TLS_CIPHER),
     TRACE_CATEGORY_(CONF),
+#ifndef OPENSSL_NO_ENGINE
     TRACE_CATEGORY_(ENGINE_TABLE),
     TRACE_CATEGORY_(ENGINE_REF_COUNT),
+#endif
     TRACE_CATEGORY_(PKCS5V2),
     TRACE_CATEGORY_(PKCS12_KEYGEN),
     TRACE_CATEGORY_(PKCS12_DECRYPT),
     TRACE_CATEGORY_(X509V3_POLICY),
     TRACE_CATEGORY_(BN_CTX),
+    TRACE_CATEGORY_(CMP),
+    TRACE_CATEGORY_(STORE),
+    TRACE_CATEGORY_(DECODER),
+    TRACE_CATEGORY_(ENCODER),
+    TRACE_CATEGORY_(REF_COUNT)
 };
 
 const char *OSSL_trace_get_category_name(int num)
@@ -218,6 +227,13 @@ static int trace_detach_cb(int category, int type, const void *data)
     return 1;
 }
 
+static int do_ossl_trace_init(void);
+static CRYPTO_ONCE trace_inited = CRYPTO_ONCE_STATIC_INIT;
+DEFINE_RUN_ONCE_STATIC(ossl_trace_init)
+{
+    return do_ossl_trace_init();
+}
+
 static int set_trace_data(int category, int type, BIO **channel,
                           const char **prefix, const char **suffix,
                           int (*attach_cb)(int, int, const void *),
@@ -227,8 +243,9 @@ static int set_trace_data(int category, int type, BIO **channel,
     char *curr_prefix = NULL;
     char *curr_suffix = NULL;
 
-    /* Ensure ossl_trace_init() is called */
-    OPENSSL_init_crypto(0, NULL);
+    /* Ensure do_ossl_trace_init() is called once */
+    if (!RUN_ONCE(&trace_inited, ossl_trace_init))
+        return 0;
 
     curr_channel = trace_channels[category].bio;
     curr_prefix = trace_channels[category].prefix;
@@ -297,19 +314,15 @@ static int set_trace_data(int category, int type, BIO **channel,
 
     return 1;
 }
-#endif
 
-int ossl_trace_init(void)
+static int do_ossl_trace_init(void)
 {
-#ifndef OPENSSL_NO_TRACE
     trace_lock = CRYPTO_THREAD_lock_new();
-    if (trace_lock == NULL)
-        return 0;
-#endif
-
-    return 1;
+    return trace_lock != NULL;
 }
 
+#endif
+
 void ossl_trace_cleanup(void)
 {
 #ifndef OPENSSL_NO_TRACE
@@ -458,7 +471,8 @@ BIO *OSSL_trace_begin(int category)
     prefix = trace_channels[category].prefix;
 
     if (channel != NULL) {
-        CRYPTO_THREAD_write_lock(trace_lock);
+        if (!CRYPTO_THREAD_write_lock(trace_lock))
+            return NULL;
         current_channel = channel;
         switch (trace_channels[category].type) {
         case SIMPLE_CHANNEL:
@@ -483,6 +497,8 @@ void OSSL_trace_end(int category, BIO * channel)
     char *suffix = NULL;
 
     category = ossl_trace_get_category(category);
+    if (category < 0)
+        return;
     suffix = trace_channels[category].suffix;
     if (channel != NULL
         && ossl_assert(channel == current_channel)) {