00001
00010 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && defined(PKIFRESOURCES_ALLOW_GCC_PRAGMA)
00011 #pragma implementation "SimpleCertAndCrlPanel.cpp"
00012 #endif
00013
00014
00015 #include "PKIFdll.h"
00016
00017 #include "wx/wxprec.h"
00018
00019 #ifdef __BORLANDC__
00020 #pragma hdrstop
00021 #endif
00022
00023 #if defined(WIN32) || defined(_WIN32)
00024 #include <shellapi.h>
00025 #endif
00026
00027 #include "SimpleCertAndCrlPanel.h"
00028 #include "CertificateDisplayNotebook.h"
00029 #include "CRLDisplayNotebook.h"
00030 #include "ResourceUtils.h"
00031
00032 #include <iterator>
00033 using namespace boost;
00034 using namespace std;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 BEGIN_EVENT_TABLE(CPKIFSimpleCertAndCrlPanel,wxPanel)
00045 EVT_BUTTON( ID_BUTTON_ADD_TA, CPKIFSimpleCertAndCrlPanel::OnAddTA )
00046 EVT_BUTTON( ID_BUTTON_VIEW_TA, CPKIFSimpleCertAndCrlPanel::OnViewTA )
00047 EVT_BUTTON( ID_BUTTON_REMOVE_TA, CPKIFSimpleCertAndCrlPanel::OnRemoveTA )
00048 EVT_BUTTON( ID_BUTTON_ADD_CRL, CPKIFSimpleCertAndCrlPanel::OnAddCrl )
00049 EVT_BUTTON( ID_BUTTON_VIEW_CRL, CPKIFSimpleCertAndCrlPanel::OnViewCrl )
00050 EVT_BUTTON( ID_BUTTON_REMOVE_CRL, CPKIFSimpleCertAndCrlPanel::OnRemoveCrl )
00051 EVT_BUTTON( ID_BUTTON_ADD_CERT, CPKIFSimpleCertAndCrlPanel::OnAddCert )
00052 EVT_BUTTON( ID_BUTTON_VIEW_CERT, CPKIFSimpleCertAndCrlPanel::OnViewCert )
00053 EVT_BUTTON( ID_BUTTON_REMOVE_CERT, CPKIFSimpleCertAndCrlPanel::OnRemoveCert )
00054 END_EVENT_TABLE()
00062 CPKIFSimpleCertAndCrlPanel::CPKIFSimpleCertAndCrlPanel( wxWindow *parent, wxWindowID id,
00063 const wxPoint &position, const wxSize& size, long style ) :
00064 wxPanel( parent, id, position, size, style )
00065 {
00066 #ifdef WIN32
00067 memset(m_tmpName, 0, MAX_PATH);
00068 #endif
00069
00070
00071 SimpleCertAndCrlPanel( this, TRUE );
00072
00073
00074
00075
00076
00077
00078
00079 wxListBox* lb1 = GetListboxCerts();
00080 wxASSERT(lb1);
00081 if(lb1->GetCount() == 0)
00082 {
00083 GetButtonViewCert()->Enable(false);
00084 }
00085
00086 wxListBox* lb2 = GetListboxCrls();
00087 wxASSERT(lb2);
00088
00089 if(lb2->GetCount() == 0)
00090 {
00091 GetButtonViewCrl()->Enable(false);
00092 }
00093
00094 wxListBox* lb3 = GetListboxTrustAnchors();
00095 wxASSERT(lb3);
00096
00097 if(lb3->GetCount() == 0)
00098 {
00099 GetButtonViewTa()->Enable(false);
00100 }
00101 }
00109 CPKIFSimpleCertAndCrlPanel::~CPKIFSimpleCertAndCrlPanel()
00110 {
00111 #ifdef WIN32
00112 if(0 != strlen(m_tmpName))
00113 remove(m_tmpName);
00114 #endif
00115 }
00123 bool CPKIFSimpleCertAndCrlPanel::Validate()
00124 {
00125 return TRUE;
00126 }
00127
00128
00136 void CPKIFSimpleCertAndCrlPanel::OnRemoveCert( wxCommandEvent &event )
00137 {
00138 wxListBox* lb = GetListboxCerts();
00139 wxASSERT(lb);
00140 wxArrayInt selections;
00141
00142 int count = lb->GetSelections(selections);
00143
00144 for(int jj = 0; jj < count; jj++)
00145 {
00146 int sel = selections.Item(jj);
00147 vector<CPKIFCertificatePtr>::iterator i;
00148 vector<CPKIFCertificatePtr>::iterator end = m_vCerts.end();
00149 for(i = m_vCerts.begin(); i != end; i++)
00150 {
00151
00152 wxString s = lb->GetString(sel);
00153 if(wxString((*i)->Subject()->ToString(),wxConvUTF8) == s)
00154 {
00155 m_vCerts.erase(i);
00156 break;
00157 }
00158 }
00159 }
00160 UpdateCerts();
00161 }
00169 void CPKIFSimpleCertAndCrlPanel::OnViewCert( wxCommandEvent &event )
00170 {
00171 wxListBox* lb = GetListboxCerts();
00172 wxASSERT(lb);
00173 wxArrayInt selections;
00174
00175 int count = lb->GetSelections(selections);
00176
00177 if(count == 0)
00178 {
00179 wxMessageBox("You must select a certificate.", _T("Warning"), wxICON_WARNING);
00180 return;
00181 }
00182 if(count > 1)
00183 {
00184 wxMessageBox("You must only select one certificate.", _T("Warning"), wxICON_WARNING);
00185 return;
00186 }
00187
00188 int sel = selections.Item(0);
00189 CPKIFCertificatePtr curCertDetails;
00190 if(-1 != sel && sel < m_vCerts.size())
00191 curCertDetails = m_vCerts[sel];
00192 else
00193 {
00194 wxMessageBox("You must make selection before viewing the certificate.", _T("Warning"), wxICON_WARNING);
00195 return;
00196 }
00197
00198 #if defined(WIN32) || defined(_WIN32)
00199 char tmppath[MAX_PATH];
00200 GetTempPath(MAX_PATH, tmppath);
00201
00202
00203 if(0 != strlen(m_tmpName))
00204 remove(m_tmpName);
00205
00206 GetTempFileName(tmppath, "pkif_", 0, m_tmpName);
00207
00208
00209 remove(m_tmpName);
00210
00211
00212 char* p = m_tmpName + (strlen(m_tmpName) - 3);
00213 strcpy(p, "cer");
00214
00215 CPKIFBufferPtr rawCert = curCertDetails->Encoded();
00216 FILE* f = fopen(m_tmpName, "wb+");
00217 fwrite(rawCert->GetBuffer(), rawCert->GetLength(), 1, f);
00218 fclose(f);
00219
00220 HINSTANCE h = ShellExecute(NULL, "open", m_tmpName, NULL, NULL, SW_SHOWNORMAL);
00221 if((int)h <= 32)
00222 {
00223 wxMessageBox("There is no default certificate viewer installed. Unable to display additional information.", _T("Error"), wxICON_ERROR);
00224 return;
00225 }
00226 #else
00227 CPKIFCertificateDisplayNotebook cdn(NULL,-1,wxT("Certificate"),wxDefaultPosition,wxDefaultSize,
00228 wxDEFAULT_DIALOG_STYLE);
00229 cdn.SetCertificate(curCertDetails);
00230 cdn.Center();
00231 cdn.ShowModal();
00232 #endif
00233 }
00241 void CPKIFSimpleCertAndCrlPanel::OnAddCert( wxCommandEvent &event )
00242 {
00243 static wxString filter(wxT("Certificate files (*.cer, *.der, *.crt)|*.cer;*.der;*.crt|All files (*.*)|*.*"));
00244 wxFileDialog dlg(this, wxString(wxT("Select one or more certiticates")), wxEmptyString, wxEmptyString, filter, wxOPEN | wxFILE_MUST_EXIST | wxMULTIPLE);
00245 if(wxID_OK == dlg.ShowModal())
00246 {
00247 wxArrayString filenames;
00248 dlg.GetPaths(filenames);
00249
00250 wxArrayString::iterator pos;
00251 wxArrayString::iterator end = filenames.end();
00252 for(pos = filenames.begin(); pos != end; ++pos)
00253 {
00254 CPKIFCertificatePtr cert = GetCert(*pos);
00255
00256 if(cert != (CPKIFCertificate*)NULL)
00257 {
00258 if(!cert->IsSelfSigned())
00259 m_vCerts.push_back(cert);
00260 else
00261 wxMessageBox("Self-signed certificates cannot be added to this store.", "ERROR", wxICON_ERROR);
00262 }
00263 }
00264 }
00265
00266 UpdateCerts();
00267 }
00275 void CPKIFSimpleCertAndCrlPanel::OnRemoveCrl( wxCommandEvent &event )
00276 {
00277 wxListBox* lb = GetListboxCrls();
00278 wxASSERT(lb);
00279 wxArrayInt selections;
00280
00281 int count = lb->GetSelections(selections);
00282
00283 for(int jj = 0; jj < count; jj++)
00284 {
00285 int sel = selections.Item(jj);
00286 vector<CPKIFCRLPtr>::iterator i;
00287 vector<CPKIFCRLPtr>::iterator end = m_vCrls.end();
00288 for(i = m_vCrls.begin(); i != end; i++)
00289 {
00290
00291 wxString s = lb->GetString(sel);
00292 if(wxString((*i)->Issuer()->ToString(),wxConvUTF8) == s)
00293 {
00294 m_vCrls.erase(i);
00295 break;
00296 }
00297 }
00298 }
00299 UpdateCrls();
00300 }
00308 void CPKIFSimpleCertAndCrlPanel::OnViewCrl( wxCommandEvent &event )
00309 {
00310 wxListBox* lb = GetListboxCrls();
00311 wxASSERT(lb);
00312 wxArrayInt selections;
00313
00314 int count = lb->GetSelections(selections);
00315
00316 if(count == 0)
00317 {
00318 wxMessageBox("You must select a CRL.", _T("Warning"), wxICON_WARNING);
00319 return;
00320 }
00321
00322 if(count > 1)
00323 {
00324 wxMessageBox("You must only select one CRL.", _T("Warning"), wxICON_WARNING);
00325 return;
00326 }
00327
00328 int sel = selections.Item(0);
00329 CPKIFCRLPtr curCrlDetails;
00330 if(-1 != sel && sel < m_vCrls.size())
00331 curCrlDetails = m_vCrls[sel];
00332 else
00333 {
00334 wxMessageBox("You must make selection before viewing the CRL.", _T("Warning"), wxICON_WARNING);
00335 return;
00336 }
00337
00338 #if defined(WIN32) || defined(_WIN32)
00339 char tmppath[MAX_PATH];
00340 GetTempPath(MAX_PATH, tmppath);
00341
00342
00343 if(0 != strlen(m_tmpName))
00344 remove(m_tmpName);
00345
00346 GetTempFileName(tmppath, "pkif_", 0, m_tmpName);
00347
00348
00349 remove(m_tmpName);
00350
00351
00352 char* p = m_tmpName + (strlen(m_tmpName) - 3);
00353 strcpy(p, "crl");
00354
00355 CPKIFBufferPtr rawCert = curCrlDetails->Encoded();
00356 FILE* f = fopen(m_tmpName, "wb+");
00357 fwrite(rawCert->GetBuffer(), rawCert->GetLength(), 1, f);
00358 fclose(f);
00359
00360 HINSTANCE h = ShellExecute(NULL, "open", m_tmpName, NULL, NULL, SW_SHOWNORMAL);
00361 if((int)h <= 32)
00362 {
00363 wxMessageBox("There is no default CRL viewer installed. Unable to display additional information.", _T("Error"), wxICON_ERROR);
00364 return;
00365 }
00366 #else
00367 CPKIFCRLDisplayNotebook cdn(NULL,-1,wxT("Certificate Revocation List"),wxDefaultPosition,wxDefaultSize,
00368 wxDEFAULT_DIALOG_STYLE);
00369 cdn.SetCRL(curCrlDetails);
00370 cdn.Center();
00371 cdn.ShowModal();
00372 #endif
00373 }
00381 void CPKIFSimpleCertAndCrlPanel::OnAddCrl( wxCommandEvent &event )
00382 {
00383 static wxString filter(wxT("CRL files (*.crl)|*.crl|All files (*.*)|*.*"));
00384 wxFileDialog dlg(this, wxString(wxT("Select one or more CRLs")), wxEmptyString, wxEmptyString, filter, wxOPEN | wxFILE_MUST_EXIST | wxMULTIPLE);
00385 if(wxID_OK == dlg.ShowModal())
00386 {
00387 wxArrayString filenames;
00388 dlg.GetPaths(filenames);
00389
00390 wxArrayString::iterator pos;
00391 wxArrayString::iterator end = filenames.end();
00392 for(pos = filenames.begin(); pos != end; ++pos)
00393 {
00394 CPKIFCRLPtr crl = GetCrl(*pos);
00395 if(crl != (CPKIFCRL*)NULL)
00396 m_vCrls.push_back(crl);
00397 }
00398 }
00399
00400 UpdateCrls();
00401 }
00409 void CPKIFSimpleCertAndCrlPanel::OnRemoveTA( wxCommandEvent &event )
00410 {
00411 wxListBox* lb = GetListboxTrustAnchors();
00412 wxASSERT(lb);
00413 wxArrayInt selections;
00414
00415 int count = lb->GetSelections(selections);
00416
00417 for(int jj = 0; jj < count; jj++)
00418 {
00419 int sel = selections.Item(jj);
00420 vector<CPKIFCertificatePtr>::iterator i;
00421 vector<CPKIFCertificatePtr>::iterator end = m_vTrustAnchors.end();
00422 for(i = m_vTrustAnchors.begin(); i != end; i++)
00423 {
00424
00425 wxString s = lb->GetString(sel);
00426 if(wxString((*i)->Subject()->ToString(),wxConvUTF8) == s)
00427 {
00428 m_vTrustAnchors.erase(i);
00429 break;
00430 }
00431 }
00432 }
00433 UpdateTrustAnchors();
00434 }
00442 void CPKIFSimpleCertAndCrlPanel::OnViewTA( wxCommandEvent &event )
00443 {
00444 wxListBox* lb = GetListboxTrustAnchors();
00445 wxASSERT(lb);
00446 wxArrayInt selections;
00447
00448 int count = lb->GetSelections(selections);
00449
00450 if(count == 0)
00451 {
00452 wxMessageBox("You must select a certificate.", _T("Warning"), wxICON_WARNING);
00453 return;
00454 }
00455
00456 if(count > 1)
00457 {
00458 wxMessageBox("You must only select one certificate.", _T("Warning"), wxICON_WARNING);
00459 return;
00460 }
00461
00462 int sel = selections.Item(0);
00463 CPKIFCertificatePtr curCertDetails;
00464 if(-1 != sel && sel < m_vTrustAnchors.size())
00465 curCertDetails = m_vTrustAnchors[sel];
00466 else
00467 {
00468 wxMessageBox("You must make selection before viewing the certificate.", _T("Warning"), wxICON_WARNING);
00469 return;
00470 }
00471
00472 #if defined(WIN32) || defined(_WIN32)
00473 char tmppath[MAX_PATH];
00474 GetTempPath(MAX_PATH, tmppath);
00475
00476
00477 if(0 != strlen(m_tmpName))
00478 remove(m_tmpName);
00479
00480 GetTempFileName(tmppath, "pkif_", 0, m_tmpName);
00481
00482
00483 remove(m_tmpName);
00484
00485
00486 char* p = m_tmpName + (strlen(m_tmpName) - 3);
00487 strcpy(p, "cer");
00488
00489
00490 CPKIFBufferPtr rawCert = curCertDetails->Encoded();
00491 FILE* f = fopen(m_tmpName, "wb+");
00492 fwrite(rawCert->GetBuffer(), rawCert->GetLength(), 1, f);
00493 fclose(f);
00494
00495 HINSTANCE h = ShellExecute(NULL, "open", m_tmpName, NULL, NULL, SW_SHOWNORMAL);
00496 if((int)h <= 32)
00497 {
00498 wxMessageBox("There is no default certificate viewer installed. Unable to display additional information.", _T("Error"), wxICON_ERROR);
00499 return;
00500 }
00501 #else
00502 CPKIFCertificateDisplayNotebook cdn(NULL,-1,wxT("Certificate"),wxDefaultPosition,wxDefaultSize,
00503 wxDEFAULT_DIALOG_STYLE);
00504 cdn.SetCertificate(curCertDetails);
00505 cdn.Center();
00506 cdn.ShowModal();
00507 #endif
00508 }
00516 void CPKIFSimpleCertAndCrlPanel::OnAddTA( wxCommandEvent &event )
00517 {
00518 static wxString filter(wxT("Certificate files (*.cer, *.der, *.crt)|*.cer;*.der;*.crt|All files (*.*)|*.*"));
00519 wxFileDialog dlg(this, wxString(wxT("Select one or more trust anchors")), wxEmptyString, wxEmptyString, filter, wxOPEN | wxFILE_MUST_EXIST | wxMULTIPLE);
00520 if(wxID_OK == dlg.ShowModal())
00521 {
00522 wxArrayString filenames;
00523 dlg.GetPaths(filenames);
00524
00525 wxArrayString::iterator pos;
00526 wxArrayString::iterator end = filenames.end();
00527 for(pos = filenames.begin(); pos != end; ++pos)
00528 {
00529 CPKIFCertificatePtr cert = GetCert(*pos);
00530 if(cert != (CPKIFCertificate*)NULL)
00531 m_vTrustAnchors.push_back(cert);
00532 }
00533 }
00534
00535 UpdateTrustAnchors();
00536 }
00544 void CPKIFSimpleCertAndCrlPanel::OnInitDialog(wxInitDialogEvent& event)
00545 {
00546 if(NULL == m_m) return;
00547
00548 CPKIFCacheMediator2* sm = m_m->GetMediator<CPKIFCacheMediator2>();
00549 if(NULL == sm) return;
00550
00551 std::vector<IPKIFColleaguePtr> c;
00552 sm->GetColleagues(c);
00553
00554 SimpleCertCachePtr simpleCert;
00555 SimpleCRLCachePtr simpleCRL;
00556 SimpleRootStorePtr simpleRoot;
00557
00558 std::vector<IPKIFColleaguePtr>::iterator pos;
00559 std::vector<IPKIFColleaguePtr>::iterator end = c.end();
00560 for(pos = c.begin(); pos != end; ++pos)
00561 {
00562 if(NULL != dynamic_pointer_cast<SimpleRootStore, IPKIFColleague>(*pos))
00563 {
00564 simpleRoot = dynamic_pointer_cast<SimpleRootStore, IPKIFColleague>(*pos);
00565 }
00566 else if(NULL != dynamic_pointer_cast<SimpleCertCache, IPKIFColleague>(*pos))
00567 {
00568 simpleCert = dynamic_pointer_cast<SimpleCertCache, IPKIFColleague>(*pos);
00569 }
00570 else if(NULL != dynamic_pointer_cast<SimpleCRLCache, IPKIFColleague>(*pos))
00571 {
00572 simpleCRL = dynamic_pointer_cast<SimpleCRLCache, IPKIFColleague>(*pos);
00573 }
00574 }
00575
00576 if(simpleCert)
00577 {
00578 std::vector<CPKIFCertificatePtr> certs;
00579 simpleCert->GetCertificates(certs);
00580
00581 int certCount = 0;
00582 std::vector<CPKIFCertificatePtr>::iterator pos;
00583 std::vector<CPKIFCertificatePtr>::iterator end = certs.end();
00584 for(pos = certs.begin(); pos != end; ++pos, ++certCount)
00585 {
00586 m_vCerts.push_back(*pos);
00587 }
00588 }
00589 if(simpleCRL)
00590 {
00591 std::vector<CPKIFCRLPtr> crls;
00592 simpleCRL->GetCRLs(crls);
00593
00594 int crlCount = 0;
00595 std::vector<CPKIFCRLPtr>::iterator pos;
00596 std::vector<CPKIFCRLPtr>::iterator end = crls.end();
00597 for(pos = crls.begin(); pos != end; ++pos, ++crlCount)
00598 {
00599 m_vCrls.push_back(*pos);
00600 }
00601 }
00602 if(simpleRoot)
00603 {
00604 IPKIFTrustAnchorList roots;
00605 simpleRoot->GetTrustRoots(roots);
00606
00607 int rootCount = 0;
00608 IPKIFTrustAnchorList::iterator pos;
00609 IPKIFTrustAnchorList::iterator end = roots.end();
00610 for(pos = roots.begin(); pos != end; ++pos, ++rootCount)
00611 {
00612
00613 CPKIFTrustRoot* ta = dynamic_cast<CPKIFTrustRoot*>(&(*(*pos)));
00614 if(ta != NULL)
00615 {
00616 CPKIFCertificatePtr cert;
00617 ta->GetCert(cert);
00618 m_vTrustAnchors.push_back(cert);
00619 }
00620 }
00621 }
00622
00623 UpdateCerts();
00624 UpdateTrustAnchors();
00625 UpdateCrls();
00626 }
00634 void CPKIFSimpleCertAndCrlPanel::SetInitialMediator(
00636 IPKIFMediatorPtr& m)
00637 {
00638 m_m = m;
00639 }
00647 void CPKIFSimpleCertAndCrlPanel::UpdateCerts()
00648 {
00649 wxListBox* lb = GetListboxCerts();
00650 wxASSERT(lb);
00651 lb->Clear();
00652
00653 std::vector<CPKIFCertificatePtr>::iterator pos;
00654 std::vector<CPKIFCertificatePtr>::iterator end = m_vCerts.end();
00655 for(pos = m_vCerts.begin(); pos != end; ++pos)
00656 {
00657 CPKIFNamePtr subName = (*pos)->Subject();
00658 wxString wxSubName(subName->ToString(),wxConvUTF8);
00659 lb->InsertItems(1, &wxSubName, lb->GetCount());
00660 }
00661
00662 if(lb->GetCount() > 0)
00663 {
00664 GetButtonViewCert()->Enable(true);
00665 }
00666 else
00667 {
00668 GetButtonViewCert()->Enable(false);
00669 }
00670 }
00678 void CPKIFSimpleCertAndCrlPanel::UpdateCrls()
00679 {
00680 wxListBox* lb = GetListboxCrls();
00681 wxASSERT(lb);
00682 lb->Clear();
00683
00684 std::vector<CPKIFCRLPtr>::iterator pos;
00685 std::vector<CPKIFCRLPtr>::iterator end = m_vCrls.end();
00686 for(pos = m_vCrls.begin(); pos != end; ++pos)
00687 {
00688 CPKIFNamePtr issName = (*pos)->Issuer();
00689 wxString wxIssName(issName->ToString(),wxConvUTF8);
00690 lb->InsertItems(1, &wxIssName, lb->GetCount());
00691 }
00692
00693 if(lb->GetCount() > 0)
00694 {
00695 GetButtonViewCrl()->Enable(true);
00696 }
00697 else
00698 {
00699 GetButtonViewCrl()->Enable(false);
00700 }
00701 }
00709 void CPKIFSimpleCertAndCrlPanel::UpdateTrustAnchors()
00710 {
00711 wxListBox* lb = GetListboxTrustAnchors();
00712 wxASSERT(lb);
00713 lb->Clear();
00714
00715 std::vector<CPKIFCertificatePtr>::iterator pos;
00716 std::vector<CPKIFCertificatePtr>::iterator end = m_vTrustAnchors.end();
00717 for(pos = m_vTrustAnchors.begin(); pos != end; ++pos)
00718 {
00719 CPKIFNamePtr subName = (*pos)->Subject();
00720 wxString wxSubName(subName->ToString(),wxConvUTF8);
00721 lb->InsertItems(1, &wxSubName, lb->GetCount());
00722 }
00723
00724 if(lb->GetCount() > 0)
00725 {
00726 GetButtonViewTa()->Enable(true);
00727 }
00728 else
00729 {
00730 GetButtonViewTa()->Enable(false);
00731 }
00732 }
00740 void CPKIFSimpleCertAndCrlPanel::GetTrustAnchors(
00742 std::vector<CPKIFCertificatePtr>& v)
00743 {
00744 copy(m_vTrustAnchors.begin(), m_vTrustAnchors.end(), back_inserter(v));
00745 }
00753 void CPKIFSimpleCertAndCrlPanel::GetCertificates(
00755 std::vector<CPKIFCertificatePtr>& v)
00756 {
00757 copy(m_vCerts.begin(), m_vCerts.end(), back_inserter(v));
00758 }
00766 void CPKIFSimpleCertAndCrlPanel::GetCrls(
00768 std::vector<CPKIFCRLPtr>& v)
00769 {
00770 copy(m_vCrls.begin(), m_vCrls.end(), back_inserter(v));
00771 }