changeset 452:f8b480b08532

Factor out polarssl error handling and start new sslhelp file
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 23 Apr 2014 10:33:40 +0000
parents 94613c91a3d4
children 6dec8101793c
files ui/CMakeLists.txt ui/sslconnection.cpp ui/sslhelp.cpp ui/sslhelp.h
diffstat 4 files changed, 51 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/ui/CMakeLists.txt	Wed Apr 23 12:34:37 2014 +0200
+++ b/ui/CMakeLists.txt	Wed Apr 23 10:33:40 2014 +0000
@@ -20,6 +20,7 @@
 set(DOWNLOADER_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/downloader.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/sslconnection.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/sslhelp.cpp
 )
 
 set(TRUSTBRIDGE_SOURCES
@@ -43,6 +44,7 @@
     ${CMAKE_CURRENT_SOURCE_DIR}/createinstallerdialog.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/createcertlistdialog.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/aboutdialog.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/sslhelp.cpp
     ${CERTIFICATELIST_SOURCES}
 )
 
--- a/ui/sslconnection.cpp	Wed Apr 23 12:34:37 2014 +0200
+++ b/ui/sslconnection.cpp	Wed Apr 23 10:33:40 2014 +0000
@@ -8,6 +8,7 @@
 /* TODO: Wrap ssl_session in a class for reuse.
  * see programs/ssl/ssl_client2.c for example of session reuse */
 #include "sslconnection.h"
+#include "sslhelp.h"
 
 #include <QFile>
 #include <QUuid>
@@ -24,14 +25,6 @@
 }
 #endif
 
-QString getErrorMsg(int ret)
-{
-    char errbuf[255];
-    polarssl_strerror(ret, errbuf, 255);
-    errbuf[254] = '\0'; /* Just to be sure */
-    return QString::fromLatin1(errbuf);
-}
-
 SSLConnection::SSLConnection(const QString& url,
                              const QByteArray& certificate):
     mUrl(url),
@@ -57,7 +50,7 @@
     if (ret == 0) {
         mInitialized = true;
     } else {
-        qDebug() << "Initialization error: " + getErrorMsg(ret);
+        qDebug() << "Initialization error: " + getPolarSSLErrorMsg(ret);
     }
 }
 
@@ -152,7 +145,7 @@
                   mUrl.port(443));
 
     if (ret != 0) {
-        qDebug() << "Connect failed: " << getErrorMsg(ret);
+        qDebug() << "Connect failed: " << getPolarSSLErrorMsg(ret);
         mErrorState = NoConnection;
         return ret;
     }
@@ -163,7 +156,7 @@
     while ((ret = ssl_handshake(&mSSL)) != 0) {
         if (ret != POLARSSL_ERR_NET_WANT_READ &&
                 ret != POLARSSL_ERR_NET_WANT_WRITE) {
-            qDebug() << "SSL Handshake failed: " << getErrorMsg(ret);
+            qDebug() << "SSL Handshake failed: " << getPolarSSLErrorMsg(ret);
             mErrorState = SSLHandshakeFailed;
             return ret;
         }
@@ -171,7 +164,7 @@
 
     ret = ssl_get_session(&mSSL, &mSavedSession);
     if (ret != 0) {
-        qDebug() << "SSL get session failed: " << getErrorMsg(ret);
+        qDebug() << "SSL get session failed: " << getPolarSSLErrorMsg(ret);
 
         mErrorState = NoConnection;
         return ret;
@@ -257,7 +250,7 @@
     if (mNeedsReset) {
         ret = reset();
         if (ret != 0) {
-            qDebug() << "Reset failed: " << getErrorMsg(ret);
+            qDebug() << "Reset failed: " << getPolarSSLErrorMsg(ret);
             return ret;
         }
     }
@@ -301,7 +294,7 @@
     if (ret != 0)
     {
         qDebug() << "SSL Connection reset failed: "
-                 << getErrorMsg(ret);
+                 << getPolarSSLErrorMsg(ret);
         return ret;
     }
 
@@ -312,7 +305,7 @@
 
     if (ret != 0) {
         mErrorState = NoConnection;
-        qDebug() << "Connection failed." << getErrorMsg(ret);
+        qDebug() << "Connection failed." << getPolarSSLErrorMsg(ret);
         return ret;
     }
 
@@ -320,7 +313,7 @@
         if (ret != POLARSSL_ERR_NET_WANT_READ &&
                 ret != POLARSSL_ERR_NET_WANT_WRITE) {
             qDebug() << "SSL Handshake failed: "
-                 << getErrorMsg(ret);
+                 << getPolarSSLErrorMsg(ret);
             mErrorState = SSLHandshakeFailed;
             return ret;
         }
@@ -356,7 +349,7 @@
             tries++;
         }
         if (ret <= 0) {
-            qDebug() << "Read failed: " << getErrorMsg(ret);
+            qDebug() << "Read failed: " << getPolarSSLErrorMsg(ret);
             return QByteArray();
         }
         if (len < (len - (unsigned int) ret)) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/sslhelp.cpp	Wed Apr 23 10:33:40 2014 +0000
@@ -0,0 +1,16 @@
+/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=2)
+ * and comes with ABSOLUTELY NO WARRANTY!
+ * See LICENSE.txt for details.
+ */
+#include "sslhelp.h"
+
+QString getPolarSSLErrorMsg(int ret)
+{
+    char errbuf[1020];
+    polarssl_strerror(ret, errbuf, 1020);
+    errbuf[1020] = '\0'; /* Just to be sure */
+    return QString::fromLatin1(errbuf);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/sslhelp.h	Wed Apr 23 10:33:40 2014 +0000
@@ -0,0 +1,23 @@
+/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=2)
+ * and comes with ABSOLUTELY NO WARRANTY!
+ * See LICENSE.txt for details.
+ */
+
+
+/**
+ * @file sslhelp.h
+ * @brief Helper functions to combine Qt with Polarssl
+ */
+#include <polarssl/error.h>
+
+#include <QString>
+
+/** @brief get a human readable error message for a polarssl return code
+ *
+ * @param [in] ret A polarssl error code
+ * @returns A QString representation of that error
+ */
+QString getPolarSSLErrorMsg(int ret);

http://wald.intevation.org/projects/trustbridge/