Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Bring up Invalid SSL certificate dialog when creating a new account (still incomplete)
authorKiri <sbrokenshire@xestia.co.uk>
Sun, 6 Sep 2015 20:21:31 +0000 (21:21 +0100)
committerKiri <sbrokenshire@xestia.co.uk>
Sun, 6 Sep 2015 20:21:31 +0000 (21:21 +0100)
source/carddav/carddav.cpp
source/carddav/carddav.h

index 095ab95..4b08fbb 100644 (file)
@@ -74,6 +74,7 @@ wxString CardDAV::PageData;
 CURLcode CardDAV::claconncode;\r
 int CardDAV::HTTPErrorCode;\r
 wxString CardDAV::ErrorMessage;\r
+SSLCertCollection CardDAV::VerifyCertCollection;\r
 \r
 CardDAV::CardDAV(){\r
        ServerPort = 8080;\r
@@ -224,6 +225,137 @@ void CardDAV::Abort(){
 \r
 }\r
 \r
+CURLcode CardDAV::SSLVerifyTest(){\r
+\r
+       PageData.Clear();\r
+       PageHeader.Clear();\r
+\r
+       SSLStatus = TRUE;\r
+       AuthPassed = TRUE;\r
+       AbortConnection = FALSE;\r
+\r
+       CURL *conn;\r
+       CURL *connssldata;\r
+       CURLcode conncode;\r
+       wxString ServerAddressURL;\r
+       wxString ServerAuth;\r
+       wxString ServerAddressSSL;\r
+       wxString ServerAddressNormal;\r
+\r
+       conn = curl_easy_init();\r
+\r
+       /*struct CardDAVCURLPasser {\r
+\r
+               CardDAV *Data;\r
+               bool HeaderMode = TRUE;\r
+\r
+       } CardDAVHeader, CardDAVFooter;\r
+\r
+       CardDAVHeader.Data = this;\r
+       CardDAVHeader.HeaderMode = TRUE;\r
+\r
+       CardDAVFooter.Data = this;\r
+       CardDAVFooter.HeaderMode = FALSE;*/\r
+\r
+       wxString Data1;\r
+       wxString Data2;\r
+\r
+       ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");\r
+       ServerAddressSSL = wxT("https://") + ServerAddressURL;\r
+\r
+       if (ServerSSL){\r
+\r
+               union {\r
+                       struct curl_slist       *certdata;\r
+                       struct curl_certinfo    *certinfo;\r
+               } ptr;\r
+\r
+               ptr.certdata = NULL;\r
+\r
+               // Setup two initial connections and attempt to get the certificate data.\r
+\r
+               curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));\r
+               curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);\r
+               curl_easy_setopt(conn, CURLOPT_VERBOSE, 1L);\r
+               //curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, FALSE);\r
+               //curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, FALSE);\r
+               curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, curlerrbuffer);\r
+               curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);\r
+               curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);\r
+               curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);\r
+\r
+               conncode = (curl_easy_perform(conn));\r
+\r
+               // Check if the SSL certificate is valid or self-signed or some other\r
+               // error occured.\r
+\r
+               if (conncode == CURLE_OK){\r
+\r
+                       // Connection is OK. Do nothing.\r
+\r
+                       *ServerResult = TRUE;\r
+\r
+               } else if (conncode == CURLE_SSL_CACERT || conncode == CURLE_SSL_CONNECT_ERROR){\r
+\r
+                       connssldata = curl_easy_init();\r
+\r
+                       // Retry but get the certificates without peer/host verification.\r
+\r
+                       curl_easy_setopt(connssldata, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));\r
+                       curl_easy_setopt(connssldata, CURLOPT_CERTINFO, 1);\r
+                       curl_easy_setopt(connssldata, CURLOPT_VERBOSE, 1L);\r
+                       //curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, FALSE);\r
+                       //curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, FALSE);\r
+                       curl_easy_setopt(connssldata, CURLOPT_ERRORBUFFER, curlerrbuffer);\r
+                       curl_easy_setopt(connssldata, CURLOPT_WRITEFUNCTION, WritebackFunc);\r
+                       curl_easy_setopt(connssldata, CURLOPT_WRITEDATA, &PageData);\r
+                       curl_easy_setopt(connssldata, CURLOPT_WRITEHEADER, &PageHeader);\r
+                       //curl_easy_setopt(connssldata, CURLOPT_SSL_VERIFYPEER, FALSE);\r
+                       //curl_easy_setopt(connssldata, CURLOPT_SSL_VERIFYHOST, FALSE);\r
+\r
+                       CURLcode certfetchcode;\r
+\r
+                       certfetchcode = (curl_easy_perform(connssldata));\r
+\r
+                       VerifyCertCollection = BuildSSLCollection(connssldata);\r
+\r
+                       if (certfetchcode == CURLE_OK){\r
+                               \r
+                               curl_easy_getinfo(connssldata, CURLINFO_CERTINFO, &ptr.certdata);\r
+\r
+                               VerifyCertCollection = BuildSSLCollection(connssldata);\r
+\r
+                       } else {\r
+\r
+                               conncode = certfetchcode;\r
+\r
+                       }\r
+\r
+                       *ServerResult = FALSE;\r
+\r
+               } else {\r
+\r
+                       fprintf(stderr, "curl_easy_perform() failed: %s\n",\r
+                               curl_easy_strerror(conncode));\r
+\r
+                       ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));\r
+\r
+                       *ServerResult = FALSE;\r
+\r
+               }\r
+\r
+       }\r
+\r
+       curl_easy_cleanup(conn);\r
+\r
+       return conncode;\r
+\r
+}\r
+\r
+SSLCertCollection CardDAV::GetSSLVerifyResults(){\r
+       return VerifyCertCollection;\r
+}\r
+\r
 bool CardDAV::Connect(){\r
 \r
        PageData.Clear();\r
@@ -233,15 +365,15 @@ bool CardDAV::Connect(){
        AuthPassed = TRUE;\r
        AbortConnection = FALSE;\r
 \r
-        CURL *conn;\r
-        CURLcode conncode;\r
+    CURL *conn;\r
+    CURLcode conncode;\r
        wxString ServerAddressURL;\r
        wxString ServerAuth;\r
        wxString ServerAddressSSL;\r
        wxString ServerAddressNormal;   \r
 \r
         conn = curl_easy_init();\r
-       \r
+\r
        struct CardDAVCURLPasser {\r
        \r
                CardDAV *Data;\r
@@ -284,7 +416,47 @@ bool CardDAV::Connect(){
        \r
        if (ServerSSL){\r
 \r
+               union {\r
+                       struct curl_slist       *certdata;\r
+                       struct curl_certinfo    *certinfo;\r
+               } ptr;\r
+\r
+               ptr.certdata = NULL;\r
+\r
+               // Setup two initial connections and attempt to get the certificate data.\r
+\r
                curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));\r
+               curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);\r
+\r
+               conncode = (curl_easy_perform(conn));\r
+\r
+               // Check if the SSL certificate is valid or self-signed or some other\r
+               // error occured.\r
+\r
+               if (conncode == CURLE_OK){\r
+\r
+                       // Connection is OK. Do nothing.\r
+\r
+               } else if (conncode == CURLE_SSL_CACERT){\r
+\r
+                       // Post message saying SSL certificate is invalid and \r
+\r
+\r
+                       curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);\r
+\r
+               } else {\r
+\r
+                       fprintf(stderr, "curl_easy_perform() failed: %s\n",\r
+                               curl_easy_strerror(conncode));\r
+\r
+                       ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));\r
+\r
+                       *ServerResult = FALSE;\r
+                       return FALSE;\r
+\r
+               }\r
+\r
+               /*curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));\r
                curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);\r
                curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);\r
                curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);\r
@@ -310,6 +482,31 @@ bool CardDAV::Connect(){
                \r
                if (conncode == CURLE_OK){\r
 \r
+                       // Process the server header response and look for\r
+                       // 'addressbook' within the DAV header.\r
+\r
+                       wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));\r
+                       wxString wxSHeaderLine;\r
+                       std::map<int, wxString> DAVHeaderLines;\r
+\r
+                       while (wxSHeaderLines.HasMoreTokens()){\r
+\r
+                               wxSHeaderLine = wxSHeaderLines.GetNextToken();\r
+\r
+                               if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){\r
+\r
+                                       // Look for address book in the line.\r
+\r
+                                       if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){\r
+\r
+                                               HasCalDAVSupport = TRUE;\r
+\r
+                                       }\r
+\r
+                               }\r
+\r
+                       }\r
+\r
                        *ServerResult = TRUE;\r
                        AuthPassed = TRUE;\r
                        SSLStatus = TRUE;\r
index fd1068a..aff2138 100644 (file)
@@ -79,6 +79,12 @@ class CardDAV
                static SSLCertCollection BuildSSLCollection(CURL *conn);
                wxString ETagValueResult();
                wxString GetErrorMessage();
+       
+               // SSL Verification tests when connecting.
+
+               static CURLcode SSLVerifyTest();
+               static SSLCertCollection GetSSLVerifyResults();
+
                //size_t WritebackFunc(char *ptr, size_t size, size_t nmemb, FILE *userdata);
        private:
                static wxString ServerAddress;
@@ -110,6 +116,7 @@ class CardDAV
                static char curlerrbuffer[CURL_ERROR_SIZE];
                static SSLCertCollection SSLCertCol;
                static wxString ErrorMessage;
+               static wxString ErrorBufferMessage;
        protected:
                static int SSLErrorCode;
                static int ConnectionErrorCode;
@@ -117,6 +124,7 @@ class CardDAV
                static wxString PageData;
                static CURLcode claconncode;
                static int HTTPErrorCode;
+               static SSLCertCollection VerifyCertCollection;
                                
 };
 
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy