1 // frmNewAccount.cpp - New Account form (CardDAV2 account section).
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmNewAccount.h"
22 void frmNewAccount::RunCardDAV2Test( wxCommandEvent& event ){
24 NewAccountResult *ResultData = new NewAccountResult;
26 lblServerConnResult->SetLabel(_("Testing..."));
27 lblCardDAVSupportResult->SetLabel(wxT(""));
28 lblServerResponse->SetLabel(wxT(""));
29 lblServerSSLResult->SetLabel(wxT(""));
30 lblServerSSLValid->SetLabel(wxT(""));
31 lblAbleToLoginResult->SetLabel(wxT(""));
32 bool UsingSSLBypass = false;
34 // Setup a CardDAV2 connection object for testing.
36 CardDAV2 TestConnection(txtServerAddress->GetValue().ToStdString(),
37 wxAtoi(txtServerPort->GetValue()),
38 txtUsername->GetValue().ToStdString(),
39 txtPassword->GetValue().ToStdString(),
40 chkUseSSL->GetValue() ? true : false);
42 // Test the connection.
44 TestConnection.SetupConnectionObject();
45 COConnectResult TestConnectionResult = TestConnection.Connect(false);
47 // If server is using SSL, verify that the SSL connection is valid.
49 if (TestConnection.SSLVerify() == COSSL_UNABLETOVERIFY){
50 #if defined(__APPLE__)
52 TestConnection.BypassSSLVerification(true);
54 COConnectResult TestConnectionResult = TestConnection.Connect(false);
56 TestConnection.BypassSSLVerification(false);
58 int SSLResult = DisplayTrustPanel(&TestConnection);
60 if (SSLResult != NSOKButton){
62 lblServerConnResult->SetLabel(_("Failed"));
63 lblServerResponse->SetLabel(_("Not applicable"));
64 lblServerSSLResult->SetLabel(_("Used"));
65 lblServerSSLValid->SetLabel(_("No"));
66 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage());
67 btnPrevious->Enable(true);
72 // Evalulate the trust object.
74 SecTrustResultType EvalResult = ProcessResultType(&TestConnection);
77 case kSecTrustResultProceed:
78 lblServerSSLValid->SetLabel(_("Verified"));
80 case kSecTrustResultConfirm:
81 lblServerSSLValid->SetLabel(_("Verified (user)"));
84 lblServerSSLValid->SetLabel(_("Unable to verify"));
87 lblServerResponse->SetLabel(_("Not applicable"));
88 lblServerSSLResult->SetLabel(_("Used"));
90 if (EvalResult != kSecTrustResultProceed){
96 #elif defined(__WIN32__)
98 TestConnection.BypassSSLVerification(true);
100 COConnectResult TestConnectionResult = TestConnection.Connect(false);
102 TestConnection.BypassSSLVerification(false);
104 BOOL ModifiedCertificateData = false;
106 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateDialogData = BuildCertificateData(&TestConnection, (HWND)this->GetHandle());
108 if (!CryptUIDlgViewCertificate(&CertificateDialogData, &ModifiedCertificateData)){
109 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
112 if (ModifiedCertificateData == false){
114 lblServerConnResult->SetLabel(_("Failed"));
115 lblServerResponse->SetLabel(_("Not applicable"));
116 lblServerSSLResult->SetLabel(_("Used"));
117 lblServerSSLValid->SetLabel(_("No"));
118 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage());
119 btnPrevious->Enable(true);
126 // Connect again and fetch SSL certificate information.
128 TestConnection.BypassSSLVerification(true);
130 COConnectResult TestConnectionResult = TestConnection.Connect(false);
132 TestConnection.BypassSSLVerification(false);
134 SSLCertCollectionString CertData = TestConnection.BuildSSLCollection();
135 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
137 frmICPtr->LoadDataNew(CertData, txtServerAddress->GetValue().ToStdString());
138 frmICPtr->ShowModal();
140 int SSLResult = frmICPtr->GetResult();
142 // Clean up before processing response.
147 // Process the response from the user.
151 // Accept the Certificate.
153 UsingSSLBypass = true;
154 TestConnection.BypassSSLVerification(true);
156 COConnectResult TestConnectionResult = TestConnection.Connect(true);
158 TestConnection.BypassSSLVerification(false);
160 } else if (SSLResult == 2){
162 // Reject the certificate, abort the task and mark as failed.
164 // TODO: Integrate into the code.
166 //lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
173 TestConnectionResult = TestConnection.Connect(true);
175 // Get the server prefix if the connection was successful.
177 if (TestConnectionResult == COCONNECT_OK){
179 if (UsingSSLBypass == true){
180 TestConnection.BypassSSLVerification(true);
183 std::string ReceivedServerPrefix;
184 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
185 ServerPrefix = ReceivedServerPrefix;
187 if (UsingSSLBypass == true){
188 TestConnection.BypassSSLVerification(true);
193 TestConnectionResult == COCONNECT_OK ? ResultData->Connected = true : ResultData->Connected = false;
195 ResultData->SSLStatus = TestConnection.CanDoSSL();
196 ResultData->SSLVerified = TestConnection.SSLVerify();
197 ResultData->ValidResponse = TestConnection.HasValidResponse();
198 ResultData->AuthPassed = TestConnection.AbleToLogin();
199 ResultData->CanProcess = TestConnection.CanDoProcessing();
200 ResultData->ErrorMessage = TestConnection.GetErrorMessage();
202 // Post event back confirming the tests.
204 wxCommandEvent ResultsEvent(UPDATERESULTS);
205 ResultsEvent.SetClientData(ResultData);
206 wxPostEvent(this, ResultsEvent);