1 // frmSSLCertificate.cpp - SSL Certificate form.
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 "frmSSLCertificate.h"
24 frmSSLCertificate::frmSSLCertificate( wxWindow* parent )
26 frmSSLCertificateADT( parent )
31 void frmSSLCertificate::SetupCerts(SSLCertCollection SSLCertsInc)
34 // Grab the certificates, start from the one requested and show.
36 lblValidityStatus->Hide();
38 std::map<int, struct SSLCertData>::iterator SCDIter;
39 SCDIter = SSLCertsInc.SSLCollection.find(StartFrom);
41 // Setup the summary tab.
43 // Split the subject data down.
45 std::map<wxString, wxString>::iterator SDIter = SCDIter->second.CertData.find(wxT("Subject"));
46 std::map<wxString, wxString> SubjectData = GetSubjectData(SDIter->second);
48 // Get the common name (CN).
50 std::map<wxString, wxString>::iterator CNiter = SubjectData.find(wxT("CN"));
52 lblCertName->SetLabel(CNiter->second);
54 // Split the issuer data down.
56 // Attempt to grab the organisation name first then
57 // the common name if it doesn't exist.
59 if (SubjectData.count(wxT("O")) == 1){
61 std::map<wxString, wxString>::iterator Oiter = SubjectData.find(wxT("O"));
62 lblIssuedToData->SetLabel(Oiter->second);
66 lblIssuedToData->SetLabel(CNiter->second);
70 // Setup the valid from date.
72 std::map<wxString, wxString>::iterator Validiter = SCDIter->second.CertData.find(wxT("Start date"));
74 wxDateTime ValidFromDate;
75 ValidFromDate.ParseDate(Validiter->second);
77 lblValidFromData->SetLabel(ValidFromDate.FormatDate());
79 // Setup the expiry date.
81 std::map<wxString, wxString>::iterator Expiryiter = SCDIter->second.CertData.find(wxT("Expire date"));
83 wxDateTime ExpiryDate;
84 ExpiryDate.ParseDate(Expiryiter->second);
86 lblValidUntilData->SetLabel(ExpiryDate.FormatDate());
88 // Setup the detail tab.
90 wxListItem ColumnDataName;
91 ColumnDataName.SetId(0);
92 ColumnDataName.SetText(_("Name"));
93 ColumnDataName.SetWidth(115);
94 lstValues->InsertColumn(0, ColumnDataName);
96 wxListItem ColumnDataValue;
97 ColumnDataValue.SetId(0);
98 ColumnDataValue.SetText(_("Value"));
99 ColumnDataValue.SetWidth(115);
100 lstValues->InsertColumn(0, ColumnDataValue);
104 for (std::map<wxString, wxString>::iterator iter = SCDIter->second.CertData.begin();
105 iter != SCDIter->second.CertData.end(); ++iter){
107 // Ignore certificate data.
109 if (iter->first == wxT("Cert")){
118 SSLInfo.SetText(_("Mooo"));
119 SSLIndex = lstValues->InsertItem(SSLInfo);
121 lstValues->SetItem(SSLIndex, 0, iter->first);
122 lstValues->SetItem(SSLIndex, 1, iter->second);
126 // Setup the hierachy tab.
128 wxTreeItemId CertTreeId;
129 wxTreeItemId PrevCertTreeId;
131 for (std::map<int, struct SSLCertData>::reverse_iterator iter = SSLCertsInc.SSLCollection.rbegin();
132 iter != SSLCertsInc.SSLCollection.rend(); ++iter){
134 if (iter->first < StartFrom){
140 // Get the common name of the certificate. (Fetch subject).
142 std::map<wxString, wxString>::iterator CSDIter = iter->second.CertData.find(wxT("Subject"));
143 std::map<wxString, wxString> CertSubjectData = GetSubjectData(CSDIter->second);
145 // Get the common name (CN).
147 std::map<wxString, wxString>::iterator CertCNiter = CertSubjectData.find(wxT("CN"));
149 // Add to the hierachy.
151 SSLCertItemData *SSLCertID = new SSLCertItemData((iter->first + 1));
153 CertTreeId = treHierarchy->AppendItem(PrevCertTreeId, CertCNiter->second, 0, -1, SSLCertID);
154 PrevCertTreeId = CertTreeId;
158 treHierarchy->ExpandAll();
160 SSLCertData = SSLCertsInc;
164 void frmSSLCertificate::SetupCertsString(SSLCertCollectionString SSLCertsInc)
167 // Grab the certificates, start from the one requested and show.
169 lblValidityStatus->Hide();
171 std::map<int, struct SSLCertDataString>::iterator SCDIter;
172 SCDIter = SSLCertsInc.SSLCollection.find(StartFrom);
174 // Setup the summary tab.
176 // Split the subject data down.
178 std::map<string, string>::iterator SDIter = SCDIter->second.CertData.find("Subject");
179 std::map<string, string> SubjectData = GetSubjectData(SDIter->second);
181 // Get the common name (CN).
183 std::map<string, string>::iterator CNiter = SubjectData.find("CN");
185 lblCertName->SetLabel(CNiter->second);
187 // Split the issuer data down.
189 // Attempt to grab the organisation name first then
190 // the common name if it doesn't exist.
192 if (SubjectData.find("O") != SubjectData.end()){
194 std::map<string, string>::iterator Oiter = SubjectData.find("O");
195 lblIssuedToData->SetLabel(Oiter->second);
199 lblIssuedToData->SetLabel(CNiter->second);
203 // Setup the valid from date.
205 std::map<string, string>::iterator Validiter = SCDIter->second.CertData.find("Start date");
207 wxDateTime ValidFromDate;
208 ValidFromDate.ParseDate(Validiter->second);
210 lblValidFromData->SetLabel(ValidFromDate.FormatDate());
212 // Setup the expiry date.
214 std::map<string, string>::iterator Expiryiter = SCDIter->second.CertData.find("Expire date");
216 wxDateTime ExpiryDate;
217 ExpiryDate.ParseDate(Expiryiter->second);
219 lblValidUntilData->SetLabel(ExpiryDate.FormatDate());
221 // Setup the detail tab.
223 wxListItem ColumnDataName;
224 ColumnDataName.SetId(0);
225 ColumnDataName.SetText(_("Name"));
226 ColumnDataName.SetWidth(115);
227 lstValues->InsertColumn(0, ColumnDataName);
229 wxListItem ColumnDataValue;
230 ColumnDataValue.SetId(0);
231 ColumnDataValue.SetText(_("Value"));
232 ColumnDataValue.SetWidth(115);
233 lstValues->InsertColumn(0, ColumnDataValue);
237 for (std::map<string, string>::iterator iter = SCDIter->second.CertData.begin();
238 iter != SCDIter->second.CertData.end(); ++iter){
240 // Ignore certificate data.
242 if (iter->first == wxT("Cert")){
251 SSLInfo.SetText(_("Mooo"));
252 SSLIndex = lstValues->InsertItem(SSLInfo);
254 lstValues->SetItem(SSLIndex, 0, iter->first);
255 lstValues->SetItem(SSLIndex, 1, iter->second);
259 // Setup the hierachy tab.
261 wxTreeItemId CertTreeId;
262 wxTreeItemId PrevCertTreeId;
264 for (std::map<int, struct SSLCertDataString>::reverse_iterator iter = SSLCertsInc.SSLCollection.rbegin();
265 iter != SSLCertsInc.SSLCollection.rend(); ++iter){
267 if (iter->first < StartFrom){
273 // Get the common name of the certificate. (Fetch subject).
275 std::map<string, string>::iterator CSDIter = iter->second.CertData.find("Subject");
276 std::map<string, string> CertSubjectData = GetSubjectData(CSDIter->second);
278 // Get the common name (CN).
280 std::map<string, string>::iterator CertCNiter = CertSubjectData.find("CN");
282 // Add to the hierachy.
284 SSLCertItemData *SSLCertID = new SSLCertItemData((iter->first + 1));
286 CertTreeId = treHierarchy->AppendItem(PrevCertTreeId, CertCNiter->second, 0, -1, SSLCertID);
287 PrevCertTreeId = CertTreeId;
291 treHierarchy->ExpandAll();
293 SSLCertDataString = SSLCertsInc;
297 std::map<wxString,wxString> frmSSLCertificate::GetSubjectData(wxString SubjectData)
300 // Get the subject data and put into a map<wxString,wxString>
302 std::map<wxString,wxString> SubjectDataFinal;
303 wxStringTokenizer SubjectDataToken(SubjectData, wxT(","));
305 wxString SettingName;
306 wxString SettingValue;
308 while(SubjectDataToken.HasMoreTokens())
311 StringData = SubjectDataToken.GetNextToken();
313 // Split the data down between the equals sign.
315 wxStringTokenizer DataSplitToken(StringData, wxT("="));
317 while (DataSplitToken.HasMoreTokens()){
319 SettingName = DataSplitToken.GetNextToken();
320 SettingValue = DataSplitToken.GetNextToken();
321 SettingName.Trim(TRUE);
322 SettingName.Trim(FALSE);
323 SettingValue.Trim(TRUE);
324 SettingValue.Trim(FALSE);
326 SubjectDataFinal.insert(std::make_pair(SettingName, SettingValue));
332 SettingValue.Clear();
336 return SubjectDataFinal;
340 std::map<string,string> frmSSLCertificate::GetSubjectData(std::string SubjectData)
343 // Get the subject data and put into a map<wxString,wxString>
345 std::map<string,string> SubjectDataFinal;
346 wxStringTokenizer SubjectDataToken(wxString(SubjectData.c_str(), wxConvUTF8), wxT(","));
348 wxString SettingName;
349 wxString SettingValue;
351 while(SubjectDataToken.HasMoreTokens())
354 StringData = SubjectDataToken.GetNextToken();
356 // Split the data down between the equals sign.
358 wxStringTokenizer DataSplitToken(StringData, wxT("="));
360 while (DataSplitToken.HasMoreTokens()){
362 SettingName = DataSplitToken.GetNextToken();
363 SettingValue = DataSplitToken.GetNextToken();
364 SettingName.Trim(TRUE);
365 SettingName.Trim(FALSE);
366 SettingValue.Trim(TRUE);
367 SettingValue.Trim(FALSE);
369 SubjectDataFinal.insert(std::make_pair(SettingName.ToStdString(), SettingValue.ToStdString()));
375 SettingValue.Clear();
379 return SubjectDataFinal;
383 void frmSSLCertificate::LoadValueData( wxListEvent& event )
386 // Get the value data and display it in the text at the bottom.
388 long intSelected = -1;
389 wxString SSLSettingValue;
391 intSelected = lstValues->GetNextItem(intSelected,
393 wxLIST_STATE_SELECTED);
395 SSLSettingValue = lstValues->GetItemText(intSelected, 1);
397 txtValueData->SetValue(SSLSettingValue);
401 void frmSSLCertificate::ViewCertificate( wxCommandEvent& event )
404 // Get the internal certificate ID number.
406 wxTreeItemId SelectedChild = treHierarchy->GetSelection();
408 SSLCertItemData *ActiveSSLData = (SSLCertItemData*)treHierarchy->GetItemData(SelectedChild);
410 if (ActiveSSLData->GetCertID() == (StartFrom + 1)){
416 frmSSLCertificate *frameSSLCert = new frmSSLCertificate ( this );
417 frameSSLCert->StartCertFrom((ActiveSSLData->GetCertID() - 1));
418 frameSSLCert->SetupCerts(SSLCertData);
419 frameSSLCert->ShowModal();
425 void frmSSLCertificate::CloseWindow( wxCommandEvent& event )
428 // Close this window.
433 void frmSSLCertificate::StartCertFrom(int StartFromInc){
435 // Start the certificate chain from the specified
436 // certificate number.
438 StartFrom = StartFromInc;
441 void frmSSLCertificate::CheckCertificate( wxTreeEvent& event ){
443 // Check the certificate.
445 wxTreeItemId SelectedChild = treHierarchy->GetSelection();
447 SSLCertItemData *ActiveSSLData = (SSLCertItemData*)treHierarchy->GetItemData(SelectedChild);