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 const wxString validFromDateToParse = wxString(Validiter->second);
209 wxString::const_iterator validFromDateToParseEnd = validFromDateToParse.end();
210 ValidFromDate.ParseDateTime(validFromDateToParse, &validFromDateToParseEnd);
212 lblValidFromData->SetLabel(ValidFromDate.FormatDate());
214 // Setup the expiry date.
216 std::map<string, string>::iterator Expiryiter = SCDIter->second.CertData.find("Expire date");
218 wxDateTime ExpiryDate;
219 ExpiryDate.ParseDate(Expiryiter->second);
221 lblValidUntilData->SetLabel(ExpiryDate.FormatDate());
223 // Setup the detail tab.
225 wxListItem ColumnDataName;
226 ColumnDataName.SetId(0);
227 ColumnDataName.SetText(_("Name"));
228 ColumnDataName.SetWidth(115);
229 lstValues->InsertColumn(0, ColumnDataName);
231 wxListItem ColumnDataValue;
232 ColumnDataValue.SetId(0);
233 ColumnDataValue.SetText(_("Value"));
234 ColumnDataValue.SetWidth(115);
235 lstValues->InsertColumn(0, ColumnDataValue);
239 for (std::map<string, string>::iterator iter = SCDIter->second.CertData.begin();
240 iter != SCDIter->second.CertData.end(); ++iter){
242 // Ignore certificate data.
244 if (iter->first == wxT("Cert")){
253 SSLInfo.SetText(_("Mooo"));
254 SSLIndex = lstValues->InsertItem(SSLInfo);
256 lstValues->SetItem(SSLIndex, 0, iter->first);
257 lstValues->SetItem(SSLIndex, 1, iter->second);
261 // Setup the hierachy tab.
263 wxTreeItemId CertTreeId;
264 wxTreeItemId PrevCertTreeId;
266 for (std::map<int, struct SSLCertDataString>::reverse_iterator iter = SSLCertsInc.SSLCollection.rbegin();
267 iter != SSLCertsInc.SSLCollection.rend(); ++iter){
269 if (iter->first < StartFrom){
275 // Get the common name of the certificate. (Fetch subject).
277 std::map<string, string>::iterator CSDIter = iter->second.CertData.find("Subject");
278 std::map<string, string> CertSubjectData = GetSubjectData(CSDIter->second);
280 // Get the common name (CN).
282 std::map<string, string>::iterator CertCNiter = CertSubjectData.find("CN");
284 // Add to the hierachy.
286 SSLCertItemData *SSLCertID = new SSLCertItemData((iter->first + 1));
288 CertTreeId = treHierarchy->AppendItem(PrevCertTreeId, CertCNiter->second, 0, -1, SSLCertID);
289 PrevCertTreeId = CertTreeId;
293 treHierarchy->ExpandAll();
295 SSLCertDataString = SSLCertsInc;
299 std::map<wxString,wxString> frmSSLCertificate::GetSubjectData(wxString SubjectData)
302 // Get the subject data and put into a map<wxString,wxString>
304 std::map<wxString,wxString> SubjectDataFinal;
305 wxStringTokenizer SubjectDataToken(SubjectData, wxT(","));
307 wxString SettingName;
308 wxString SettingValue;
310 while(SubjectDataToken.HasMoreTokens())
313 StringData = SubjectDataToken.GetNextToken();
315 // Split the data down between the equals sign.
317 wxStringTokenizer DataSplitToken(StringData, wxT("="));
319 while (DataSplitToken.HasMoreTokens()){
321 SettingName = DataSplitToken.GetNextToken();
322 SettingValue = DataSplitToken.GetNextToken();
323 SettingName.Trim(TRUE);
324 SettingName.Trim(FALSE);
325 SettingValue.Trim(TRUE);
326 SettingValue.Trim(FALSE);
328 SubjectDataFinal.insert(std::make_pair(SettingName, SettingValue));
334 SettingValue.Clear();
338 return SubjectDataFinal;
342 std::map<string,string> frmSSLCertificate::GetSubjectData(std::string SubjectData)
345 // Get the subject data and put into a map<wxString,wxString>
347 std::map<string,string> SubjectDataFinal;
348 wxStringTokenizer SubjectDataToken(wxString(SubjectData.c_str(), wxConvUTF8), wxT(","));
350 wxString SettingName;
351 wxString SettingValue;
353 while(SubjectDataToken.HasMoreTokens())
356 StringData = SubjectDataToken.GetNextToken();
358 // Split the data down between the equals sign.
360 wxStringTokenizer DataSplitToken(StringData, wxT("="));
362 while (DataSplitToken.HasMoreTokens()){
364 SettingName = DataSplitToken.GetNextToken();
365 SettingValue = DataSplitToken.GetNextToken();
366 SettingName.Trim(TRUE);
367 SettingName.Trim(FALSE);
368 SettingValue.Trim(TRUE);
369 SettingValue.Trim(FALSE);
371 SubjectDataFinal.insert(std::make_pair(SettingName.ToStdString(), SettingValue.ToStdString()));
377 SettingValue.Clear();
381 return SubjectDataFinal;
385 void frmSSLCertificate::LoadValueData( wxListEvent& event )
388 // Get the value data and display it in the text at the bottom.
390 long intSelected = -1;
391 wxString SSLSettingValue;
393 intSelected = lstValues->GetNextItem(intSelected,
395 wxLIST_STATE_SELECTED);
397 SSLSettingValue = lstValues->GetItemText(intSelected, 1);
399 txtValueData->SetValue(SSLSettingValue);
403 void frmSSLCertificate::ViewCertificate( wxCommandEvent& event )
406 // Get the internal certificate ID number.
408 wxTreeItemId SelectedChild = treHierarchy->GetSelection();
410 SSLCertItemData *ActiveSSLData = (SSLCertItemData*)treHierarchy->GetItemData(SelectedChild);
412 if (ActiveSSLData->GetCertID() == (StartFrom + 1)){
418 frmSSLCertificate *frameSSLCert = new frmSSLCertificate ( this );
419 frameSSLCert->StartCertFrom((ActiveSSLData->GetCertID() - 1));
420 frameSSLCert->SetupCerts(SSLCertData);
421 frameSSLCert->ShowModal();
427 void frmSSLCertificate::CloseWindow( wxCommandEvent& event )
430 // Close this window.
435 void frmSSLCertificate::StartCertFrom(int StartFromInc){
437 // Start the certificate chain from the specified
438 // certificate number.
440 StartFrom = StartFromInc;
443 void frmSSLCertificate::CheckCertificate( wxTreeEvent& event ){
445 // Check the certificate.
447 wxTreeItemId SelectedChild = treHierarchy->GetSelection();
449 SSLCertItemData *ActiveSSLData = (SSLCertItemData*)treHierarchy->GetItemData(SelectedChild);