From cc1d5f41a42cd292bf44b1187e78386e397d761d Mon Sep 17 00:00:00 2001
From: Alexander Stein <alexander.stein@informatik.tu-chemnitz.de>
Date: Fri, 10 Jul 2009 17:55:10 +0200
Subject: [PATCH 2/2] Support some basic status messages if ssl is not possible due to invalid keys

* Now it's possible to see if the core has:
 * an expired certificate
 * a certificate not valid yet

Signed-off-by: Alexander Stein <alexander.stein@informatik.tu-chemnitz.de>
---
 src/client/clientsyncer.cpp |    8 +++++++-
 src/core/core.cpp           |    8 ++++++++
 src/core/sslserver.cpp      |    6 +++++-
 src/core/sslserver.h        |    3 +++
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/client/clientsyncer.cpp b/src/client/clientsyncer.cpp
index 64d909f..81d2d03 100644
--- a/src/client/clientsyncer.cpp
+++ b/src/client/clientsyncer.cpp
@@ -216,7 +216,13 @@ void ClientSyncer::clientInitAck(const QVariantMap &msg) {
 
       sslSocket->startClientEncryption();
     } else {
-      emit connectionError(tr("<b>The Quassel Core you are trying to connect to does not support SSL!</b><br />If you want to connect anyways, disable the usage of SSL in the account settings."));
+      if (msg["SslStatusMsg"].toString() == "Unsupported") {
+	emit connectionError(tr("<b>The Quassel Core you are trying to connect to does not support SSL!</b><br />If you want to connect anyways, disable the usage of SSL in the account settings."));
+      } else if (msg["SslStatusMsg"].toString() == "CertExpired") {
+	emit connectionError(tr("<b>The Quassel Core you are trying to connect has an expired certificate!</b><br />If you want to connect anyways, disable the usage of SSL in the account settings."));
+      } else if (msg["SslStatusMsg"].toString() == "CertNotValidYet") {
+	emit connectionError(tr("<b>The Quassel Core you are trying to connect has a certificate which is not valid yet!</b><br />If you want to connect anyways, disable the usage of SSL in the account settings."));
+      }
       disconnectFromCore();
     }
     return;
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 18f022b..f992221 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -511,8 +511,15 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     SslServer *sslServer = qobject_cast<SslServer *>(&_server);
     QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
     bool supportSsl = (bool)sslServer && (bool)sslSocket && sslServer->isCertValid();
+    QString sslStatusMsg;
+    if ((bool)sslServer) {
+      sslStatusMsg = sslServer->getSslStatusMsg();
+    } else {
+      sslStatusMsg = "Unsupported";
+    }
 #else
     bool supportSsl = false;
+    QString sslStatusMsg = "Unsupported";
 #endif
 
 #ifndef QT_NO_COMPRESS
@@ -522,6 +529,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
 #endif
 
     reply["SupportSsl"] = supportSsl;
+    reply["SslStatusMsg"] = sslStatusMsg;
     reply["SupportsCompression"] = supportsCompression;
     // switch to ssl/compression after client has been informed about our capabilities (see below)
 
diff --git a/src/core/sslserver.cpp b/src/core/sslserver.cpp
index ede37d0..fa7f226 100644
--- a/src/core/sslserver.cpp
+++ b/src/core/sslserver.cpp
@@ -34,7 +34,8 @@
 
 SslServer::SslServer(QObject *parent)
   : QTcpServer(parent),
-  _isCertValid(false)
+  _isCertValid(false),
+  _sslStatusMsg("Unsupported")
 {
   static bool sslWarningShown = false;
   if(!setCertificate(Quassel::configDirPath() + "quasselCert.pem")) {
@@ -106,10 +107,12 @@ bool SslServer::setCertificate(const QString &path) {
 
     if (currentTime > _cert.expiryDate()) {
       quWarning() << "SslServer: certificate expired";
+      _sslStatusMsg = "CertExpired";
       return false;
     }
     if (currentTime < _cert.effectiveDate()) {
       quWarning() << "SslServer: certificate is not valid yet";
+      _sslStatusMsg = "CertNotValidYet";
       return false;
     }
     quWarning() << "SslServer: Invalid certificate (unknown reason)";
@@ -120,6 +123,7 @@ bool SslServer::setCertificate(const QString &path) {
     return false;
   }
 
+  _sslStatusMsg = "Supported";
   _isCertValid = true;
 
   return _isCertValid;
diff --git a/src/core/sslserver.h b/src/core/sslserver.h
index 306bd8e..b0ce9b8 100644
--- a/src/core/sslserver.h
+++ b/src/core/sslserver.h
@@ -27,6 +27,7 @@
 #include <QSslKey>
 #include <QTcpServer>
 #include <QLinkedList>
+#include <QString>
 
 class SslServer : public QTcpServer {
   Q_OBJECT
@@ -40,6 +41,7 @@ public:
   virtual inline const QSslCertificate &certificate() const { return _cert; }
   virtual inline const QSslKey &key() const { return _key; }
   virtual inline bool isCertValid() const { return _isCertValid; }
+  virtual inline QString getSslStatusMsg() const { return _sslStatusMsg; }
 
 protected:
   virtual void incomingConnection(int socketDescriptor);
@@ -50,6 +52,7 @@ private:
   QSslCertificate _cert;
   QSslKey _key;
   bool _isCertValid;
+  QString _sslStatusMsg;
 };
 
 #endif //HAVE_SSL
-- 
1.6.3.3

