CredentialSelector.cpp

Go to the documentation of this file.
00001 
00010 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && defined(PKIFRESOURCES_ALLOW_GCC_PRAGMA)
00011     #pragma implementation "CredentialSelector.cpp"
00012 #endif
00013 
00014 //Pick up PKIF windows SDK configuration macros instead of WX's 
00015 #include "PKIFdll.h"
00016 // For compilers that support precompilation, includes "wx/wx.h".
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 #include "CredentialSelectorDlg.h"
00027 
00028 using namespace std;
00029 
00030 // WDR: class implementations
00031 
00032 //----------------------------------------------------------------------------
00033 // CPKIFCredentialSelectorDlg
00034 //----------------------------------------------------------------------------
00035 
00036 // WDR: event table for CPKIFCredentialSelectorDlg
00037 
00038 BEGIN_EVENT_TABLE(CPKIFCredentialSelectorDlg,wxDialog)
00039     EVT_BUTTON( wxID_OK, CPKIFCredentialSelectorDlg::OnOK )
00040     EVT_BUTTON( ID_MOINFO, CPKIFCredentialSelectorDlg::OnViewCertificate )
00041     EVT_BUTTON( wxID_CANCEL, CPKIFCredentialSelectorDlg::OnClose )
00042     EVT_INIT_DIALOG( CPKIFCredentialSelectorDlg::OnInitDialog )
00043     EVT_LISTBOX_DCLICK( ID_LIST_OF_CREDENTIALS, CPKIFCredentialSelectorDlg::OnCredListDoubleClick )
00044 END_EVENT_TABLE()
00045 
00053 CPKIFCredentialSelectorDlg::CPKIFCredentialSelectorDlg( wxWindow *parent, wxWindowID id, const wxString &title,
00054     const wxPoint &position, const wxSize& size, long style ) :
00055     wxDialog( parent, id, title, position, size, style )
00056 {
00057 #ifdef WIN32
00058     memset(m_tmpName, 0, MAX_PATH);
00059 #endif
00060 
00061     m_ku = PKIFCRYPTO::DigitalSignature | PKIFCRYPTO::NonRepudiation | PKIFCRYPTO::KeyEncipherment;
00062 
00063     // WDR: dialog function CredentialSelectorDlg for CPKIFCredentialSelectorDlg
00064     CredentialSelectorDlg( this, TRUE ); 
00065 }
00066 
00074 CPKIFCredentialSelectorDlg::~CPKIFCredentialSelectorDlg()
00075 {
00076 #ifdef WIN32
00077     if(0 != strlen(m_tmpName))
00078         remove(m_tmpName);
00079 #endif
00080 
00081 }
00082 
00083 // WDR: handler implementations for CPKIFCredentialSelectorDlg
00084 
00092 void CPKIFCredentialSelectorDlg::OnCredListDoubleClick( wxCommandEvent &event )
00093 {
00094     OnOK(event);
00095 }
00103 void CPKIFCredentialSelectorDlg::OnClose( wxCommandEvent &event )
00104 {
00105     event.Skip();
00106 }
00107 
00117 void CPKIFCredentialSelectorDlg::OnViewCertificate( wxCommandEvent &event )
00118 {
00119 #ifdef WIN32
00120     wxListBox* credList = GetListOfCredentials();
00121     wxASSERT(credList);
00122     CPKIFCredentialPtr curCred;
00123     size_t sel = credList->GetSelection();
00124     if(-1 != sel && sel < m_creds.size())
00125         curCred = m_creds[sel];
00126     else
00127     {
00128         wxMessageBox("You must make selection before viewing the certificate.", _T("Warning"), wxICON_WARNING);
00129         return;
00130     }
00131 
00132     char tmppath[MAX_PATH];
00133     GetTempPath(MAX_PATH, tmppath);
00134 
00135     //if we've already done this - remove the previously created file
00136     if(0 != strlen(m_tmpName))
00137         remove(m_tmpName);
00138 
00139     GetTempFileName(tmppath, "pkif_", 0, m_tmpName);
00140     
00141     //we just wanted a unique file name - not a file
00142     remove(m_tmpName);
00143 
00144     //we want .cer not .tmp
00145     char* p = m_tmpName + (strlen(m_tmpName) - 3);
00146     strcpy(p, "cer");
00147 
00148     CPKIFCertificatePtr cert = curCred->GetCertificate();
00149     CPKIFBufferPtr rawCert = cert->Encoded();
00150     FILE* f = fopen(m_tmpName, "wb+");
00151     fwrite(rawCert->GetBuffer(), rawCert->GetLength(), 1, f);
00152     fclose(f);
00153 
00154     HINSTANCE h = ShellExecute(NULL, "open", m_tmpName, NULL, NULL, SW_SHOWNORMAL);
00155     if((int)h <= 32)
00156     {
00157         wxMessageBox("There is no default certificate viewer installed.  Unable to display additional information.", _T("Error"), wxICON_ERROR);
00158         return;
00159     }
00160 #endif    
00161 }
00162 
00172 void CPKIFCredentialSelectorDlg::OnOK( wxCommandEvent &event )
00173 {
00174     wxListBox* credList = GetListOfCredentials();
00175     wxASSERT(credList);
00176 
00177     long sel = credList->GetSelection();
00178     if(-1 != sel && static_cast<size_t>(sel) < m_creds.size())
00179     {
00180         m_selCred = m_creds[sel];
00181         EndModal(wxID_OK);
00182     }
00183     else
00184     {
00185         wxMessageBox(wxT("You must select a credential or click Cancel to dismiss this dialog."));
00186     }
00187 }
00188 
00210 bool CPKIFCredentialSelectorDlg::SetMediator(
00212     IPKIFMediatorPtr& m)
00213 {
00214     IPKIFMediatorPtr emptyMediator;
00215     m_mediator = emptyMediator;
00216 
00217     if(NULL != m)
00218     {
00219         IPKIFCryptoKeyIDOperations* iCKIO = m->GetMediator<IPKIFCryptoKeyIDOperations>();
00220         if(NULL == iCKIO)
00221             return false;
00222     }
00223     m_mediator = m;
00224     return true;
00225 }
00226 
00250 void CPKIFCredentialSelectorDlg::SetKeyUsages(
00252     bitset<9>& ku)
00253 {
00254     m_ku = ku;
00255 }
00256 
00265 CPKIFCredentialPtr CPKIFCredentialSelectorDlg::GetSelectedCredential() const
00266 {
00267     return m_selCred;
00268 }
00269 
00287 void CPKIFCredentialSelectorDlg::OnInitDialog(wxInitDialogEvent& event)
00288 {
00289 #ifndef WIN32
00290     wxButton* moInfo = GetMoInfo();
00291     moInfo->Disable();
00292 #endif
00293 
00294     if(m_mediator == (IPKIFMediator*)NULL)
00295     {
00296         m_mediator = MakeDefaultMediator();
00297     }
00298 
00299     if(m_mediator == (IPKIFMediator*)NULL)
00300     {
00301         wxMessageBox(wxT("No cryptographic module has been specified.  Unable to list available credentials."), wxT("Error"), wxICON_ERROR);
00302         return;
00303     }
00304 
00305     wxListBox* credList = GetListOfCredentials();
00306     wxASSERT(credList);
00307 
00308     //get a pointer to the key ID interface from the mediator
00309     IPKIFCryptoKeyIDOperations* iCKIO = m_mediator->GetMediator<IPKIFCryptoKeyIDOperations>();
00310     if(NULL == iCKIO)
00311     {
00312         wxMessageBox(wxT("No acceptable cryptographic interface was found.  Unable to list available credentials."), wxT("Error"), wxICON_ERROR);
00313         return;
00314     }
00315 
00316     //empty anything present in the key ID vector
00317     m_creds.clear();
00318 
00319     //get the list of keys
00320     iCKIO->GetKeyList(m_creds, &m_ku);
00321 
00322     //iterate over the list and populate the list box
00323     int ii = 0;
00324     CPKIFCredentialList::iterator pos;
00325     CPKIFCredentialList::iterator end = m_creds.end();
00326     for(pos = m_creds.begin(); pos != end; ++pos, ++ii)
00327     {
00328         wxString credName((*pos)->Name(),wxConvUTF8);
00329         credList->InsertItems(1, &credName, ii);
00330     }
00331 }

Generated on Mon Nov 15 11:15:50 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6