From 74f3b0f9b31a7573c18a003bf008b0cc2819f424 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Wed, 28 Sep 2016 21:19:48 +0100 Subject: [PATCH] Updated CardDAV2/ConnectionObject implementation --- source/carddav2/carddav2.cpp | 62 +++++++++++++++++----------- source/carddav2/carddav2.h | 9 +++- source/connobject/ConnectionObject.h | 10 ++++- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/source/carddav2/carddav2.cpp b/source/carddav2/carddav2.cpp index 37bbdf9..d7c7707 100644 --- a/source/carddav2/carddav2.cpp +++ b/source/carddav2/carddav2.cpp @@ -72,11 +72,13 @@ size_t CardDAV2::WritebackFuncImplementation(char *ptr, size_t size, size_t nmem const struct curl_tlssessioninfo *TLSInfo; CURLcode TLSCode; - CURL *Connection = GetConnectionObject(); - TLSCode = curl_easy_getinfo(Connection, CURLINFO_TLS_SSL_PTR, &TLSInfo); + TLSCode = curl_easy_getinfo(data->ConnectionSessionObject, CURLINFO_TLS_SSL_PTR, &TLSInfo); + SecTrustRef CertificateData; + if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) { - SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &SecTrustObject); + SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &CertificateData); + data->SSLContext = CertificateData; } #elif defined(__WIN32__) @@ -136,6 +138,12 @@ CardDAV2::~CardDAV2(){ #if defined(__APPLE__) +SecTrustRef CardDAV2::BuildSSLCollection(){ + + return CertificateData; + +} + #elif defined(__WIN32__) PCCERT_CONTEXT CardDAV2::BuildSSLCollection(){ @@ -254,7 +262,15 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){ // Set the certificate data (if required). -#if defined(__WIN32__) +#if defined(__APPLE__) + + if (ServerSSL) { + + CertificateData = PageHeaderObject.SSLContext; + + } + +#elif defined(__WIN32__) if (ServerSSL) { @@ -1285,8 +1301,6 @@ COContactList CardDAV2::GetContactList(std::string SyncToken){ string ServerAddressURL = BuildURL(ServerPrefix); - std::cout << SyncData << std::endl; - curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, SyncData.c_str()); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(SyncData.c_str())); @@ -1398,9 +1412,9 @@ void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){ PageHeaderObject.ConnectionSessionObject = ConnectionSession; PageHeaderObject.DataSetting = &PageHeader; PageHeaderObject.ServerUsingSSL = false; - + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddress.c_str()); - curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC); curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); @@ -1408,7 +1422,7 @@ void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){ curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageDataObject); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeaderObject); - curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); @@ -1438,20 +1452,20 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ PageHeaderObject.ConnectionSessionObject = ConnectionSession; PageHeaderObject.DataSetting = &PageHeader; PageHeaderObject.ServerUsingSSL = true; - + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC); curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); - curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); + curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, 0L); curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageDataObject); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeaderObject); curl_easy_setopt(ConnectionSession, CURLOPT_ERRORBUFFER, SessionErrorBuffer); - curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1); - curl_easy_setopt(ConnectionSession, CURLOPT_VERBOSE, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1L); + curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1L); + curl_easy_setopt(ConnectionSession, CURLOPT_VERBOSE, 2L); curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); @@ -1463,17 +1477,17 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, NULL); } -#if !defined(__APPLE__) - if (EnableSSLBypass == true){ - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 0); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYSTATUS, 0L); } else { - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 2); - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 2L); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 1L); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYSTATUS, 1L); } - -#endif + +#if !defined(__APPLE__) || defined(__WIN32__) if (TestMode == false && ServerAccount.size() > 0){ @@ -1488,6 +1502,8 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ } } + +#endif } @@ -1998,4 +2014,4 @@ void CardDAV2::ProcessContactData(COContactList *ContactList){ return; -} \ No newline at end of file +} diff --git a/source/carddav2/carddav2.h b/source/carddav2/carddav2.h index 143b61f..6156dfe 100644 --- a/source/carddav2/carddav2.h +++ b/source/carddav2/carddav2.h @@ -69,6 +69,9 @@ class CardDAV2 : public ConnectionObject { std::string GetErrorMessage(); #if defined(__APPLE__) + + SecTrustRef BuildSSLCollection(); + #elif defined(__WIN32__) PCCERT_CONTEXT BuildSSLCollection(); @@ -86,6 +89,7 @@ class CardDAV2 : public ConnectionObject { bool ServerUsingSSL = false; CURL *ConnectionSessionObject = nullptr; #if defined(__APPLE__) + SecTrustRef SSLContext = nullptr; #elif defined(__WIN32__) PCCERT_CONTEXT SSLContext = nullptr; #endif @@ -125,6 +129,9 @@ class CardDAV2 : public ConnectionObject { void ProcessContactData(COContactList *ContactList); #if defined(__APPLE__) + + SecTrustRef CertificateData = nullptr; + #elif defined(__WIN32__) PCCERT_CONTEXT CertificateData = nullptr; @@ -138,4 +145,4 @@ class CardDAV2 : public ConnectionObject { }; -#endif \ No newline at end of file +#endif diff --git a/source/connobject/ConnectionObject.h b/source/connobject/ConnectionObject.h index d2ccf5c..3ad9d19 100644 --- a/source/connobject/ConnectionObject.h +++ b/source/connobject/ConnectionObject.h @@ -24,6 +24,11 @@ #include #include "../common/sslcertstructs.h" +#if defined (__APPLE__) +#import +#import +#endif + #if defined (__WIN32__) #include #include @@ -121,6 +126,9 @@ class ConnectionObject{ // OS specific functions. #if defined(__APPLE__) + + virtual SecTrustRef BuildSSLCollection() { return nullptr; }; + #elif defined(__WIN32__) virtual PCCERT_CONTEXT BuildSSLCollection() { return nullptr; }; @@ -160,4 +168,4 @@ class ConnectionObject{ }; -#endif \ No newline at end of file +#endif -- 2.39.2