Browse Source

Refactoring OpenSSL error reporting

Source commit: d11398667377b71f4dd18c9223e52ee9d978b25f
Martin Prikryl 1 year ago
parent
commit
b46fe1542e
2 changed files with 29 additions and 33 deletions
  1. 28 33
      source/filezilla/AsyncSslSocketLayer.cpp
  2. 1 0
      source/filezilla/AsyncSslSocketLayer.h

+ 28 - 33
source/filezilla/AsyncSslSocketLayer.cpp

@@ -1088,6 +1088,22 @@ BOOL CAsyncSslSocketLayer::ShutDownComplete()
   }
 }
 
+void CAsyncSslSocketLayer::LogSslError(const SSL *s, const char * str, const char * fmt, int nMessageType, char * debug)
+{
+  USES_CONVERSION;
+  char * buffer = new char[4096 + ((debug != NULL) ? strlen(debug) : 0)];
+  sprintf(buffer, fmt,
+      str,
+      SSL_state_string_long(s));
+  if (debug != NULL)
+  {
+    sprintf(buffer + strlen(buffer), " [%s]", debug);
+    OPENSSL_free(debug);
+  }
+  LogSocketMessageRaw(nMessageType, A2T(buffer));
+  delete[] buffer;
+}
+
 void CAsyncSslSocketLayer::apps_ssl_info_callback(const SSL *s, int where, int ret)
 {
   USES_CONVERSION;
@@ -1136,17 +1152,7 @@ void CAsyncSslSocketLayer::apps_ssl_info_callback(const SSL *s, int where, int r
     {
       debug = reinterpret_cast<char*>(ret);
     }
-    char *buffer = new char[4096 + ((debug != NULL) ? strlen(debug) : 0)];
-    sprintf(buffer, "%s: %s",
-        str,
-        SSL_state_string_long(s));
-    if (debug != NULL)
-    {
-      sprintf(buffer + strlen(buffer), " [%s]", debug);
-      OPENSSL_free(debug);
-    }
-    pLayer->LogSocketMessageRaw(FZ_LOG_INFO, A2T(buffer));
-    delete[] buffer;
+    pLayer->LogSslError(s, str, "%s: %s", FZ_LOG_INFO, debug);
   }
   else if (where & SSL_CB_ALERT)
   {
@@ -1172,39 +1178,28 @@ void CAsyncSslSocketLayer::apps_ssl_info_callback(const SSL *s, int where, int r
 
   else if (where & SSL_CB_EXIT)
   {
+    bool SendFailure = false;
     if (ret == 0)
     {
-      char *buffer = new char[4096];
-      sprintf(buffer, "%s: failed in %s",
-          str,
-          SSL_state_string_long(s));
-      pLayer->LogSocketMessageRaw(FZ_LOG_WARNING, A2T(buffer));
+      pLayer->LogSslError(s, str, "%s: failed in %s", FZ_LOG_WARNING);
       pLayer->PrintLastErrorMsg();
-      delete [] buffer;
-      if (!pLayer->m_bFailureSent)
-      {
-        pLayer->m_bFailureSent=TRUE;
-        pLayer->DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_FAILURE, pLayer->m_bSslEstablished ? SSL_FAILURE_UNKNOWN : SSL_FAILURE_ESTABLISH);
-      }
+      SendFailure = true;
     }
     else if (ret < 0)
     {
       int error = SSL_get_error(s,ret);
       if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE)
       {
-        char *buffer = new char[4096];
-        sprintf(buffer, "%s: error in %s",
-            str,
-            SSL_state_string_long(s));
-        pLayer->LogSocketMessageRaw(FZ_LOG_WARNING, A2T(buffer));
-        delete [] buffer;
-        if (!pLayer->m_bFailureSent)
-        {
-          pLayer->m_bFailureSent=TRUE;
-          pLayer->DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_FAILURE, pLayer->m_bSslEstablished ? SSL_FAILURE_UNKNOWN : SSL_FAILURE_ESTABLISH);
-        }
+        pLayer->LogSslError(s, str, "%s: error in %s", FZ_LOG_WARNING);
+        SendFailure = true;
       }
     }
+
+    if (SendFailure && !pLayer->m_bFailureSent)
+    {
+      pLayer->m_bFailureSent = TRUE;
+      pLayer->DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_FAILURE, pLayer->m_bSslEstablished ? SSL_FAILURE_UNKNOWN : SSL_FAILURE_ESTABLISH);
+    }
   }
   if (where & SSL_CB_HANDSHAKE_DONE)
   {

+ 1 - 0
source/filezilla/AsyncSslSocketLayer.h

@@ -163,6 +163,7 @@ private:
   int ProcessSendBuffer();
 
   void TriggerEvents();
+  void LogSslError(const SSL *s, const char * str, const char * fmt, int nMessageType, char * debug = NULL);
 
   // Will be called from the OpenSSL library
   static void apps_ssl_info_callback(const SSL * s, int where, int ret);