00001
00010 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && defined(PKIFRESOURCES_ALLOW_GCC_PRAGMA)
00011 #pragma implementation "NameConstraintsPanel.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 #include "NameConstraintsPanel.h"
00024 #include "NameConstraintsEntryDlg.h"
00025 #include "OID.h"
00026 #include "PRStringUtils.h"
00027
00028
00029 #include "boost/asio/ip/address_v4.hpp"
00030 #include "boost/asio/ip/address_v6.hpp"
00031
00032
00033 using boost::asio::ip::address_v4;
00034 using boost::asio::ip::address_v6;
00035
00036 using namespace boost;
00037 using namespace boost::asio;
00038 using namespace std;
00039
00040 void UpdateList(wxListBox* lb, CPKIFGeneralSubtreePtr& ns);
00041 string DecodeIP(CPKIFBufferPtr& encodedIP);
00042
00043
00044
00045
00046
00047
00048
00049
00050 BEGIN_EVENT_TABLE(CPKIFNameConstraintsPanel,wxPanel)
00051 EVT_BUTTON( ID_BUTTON_ADD_PERNC, CPKIFNameConstraintsPanel::OnAddPermNC )
00052
00053 EVT_BUTTON( ID_BUTTON_REMOVE_PERNC, CPKIFNameConstraintsPanel::OnRemovePermNC )
00054 EVT_BUTTON( ID_BUTTON_ADD_EXCLNC, CPKIFNameConstraintsPanel::OnAddExclNC )
00055
00056 EVT_BUTTON( ID_BUTTON_REMOVE_EXCLNC, CPKIFNameConstraintsPanel::OnRemoveExclNC )
00057 END_EVENT_TABLE()
00065 CPKIFNameConstraintsPanel::CPKIFNameConstraintsPanel( wxWindow *parent, wxWindowID id,
00066 const wxPoint &position, const wxSize& size, long style ) :
00067 wxPanel( parent, id, position, size, style )
00068 {
00069
00070 NameConstraintsPanel( this, TRUE );
00071 }
00072
00073
00074
00082 void CPKIFNameConstraintsPanel::OnRemovePermNC( wxCommandEvent &event )
00083 {
00084 wxListBox* lb = GetListboxPermNC();
00085 wxASSERT(lb);
00086 int sel = lb->GetSelection();
00087 if(wxNOT_FOUND != sel)
00088 lb->Delete(sel);
00089 }
00097 void CPKIFNameConstraintsPanel::OnAddPermNC( wxCommandEvent &event )
00098 {
00099 CPKIFNameConstraintsEntryDlg cpeDlg(this, -1, wxT("Enter a General Name"));
00100 cpeDlg.Centre();
00101 if(wxID_OK == cpeDlg.ShowModal())
00102 {
00103 wxListBox* lb = GetListboxPermNC();
00104 wxASSERT(lb);
00105
00106 CPKIFGeneralSubtreePtr ns = cpeDlg.GetNameConstraint();
00107 if(!ns) {
00108 wxLogError(wxT("Invalid name constraint specified."));
00109 wxLog::FlushActive();
00110 } else {
00111 m_permNCList.push_back(ns);
00112
00113 UpdateList(lb, ns);
00114 }
00115 }
00116 }
00124 void CPKIFNameConstraintsPanel::OnRemoveExclNC( wxCommandEvent &event )
00125 {
00126 wxListBox* lb = GetListboxExclNC();
00127 wxASSERT(lb);
00128 int sel = lb->GetSelection();
00129 if(wxNOT_FOUND != sel)
00130 lb->Delete(sel);
00131 }
00139 void CPKIFNameConstraintsPanel::OnAddExclNC( wxCommandEvent &event )
00140 {
00141 CPKIFNameConstraintsEntryDlg cpeDlg(this, -1, wxT("Enter a General Name"));
00142 cpeDlg.Centre();
00143 if(wxID_OK == cpeDlg.ShowModal())
00144 {
00145 wxListBox* lb = GetListboxExclNC();
00146 wxASSERT(lb);
00147
00148 CPKIFGeneralSubtreePtr ns = cpeDlg.GetNameConstraint();
00149 if(!ns) {
00150 wxLogError(wxT("Invalid name constraint specified."));
00151 wxLog::FlushActive();
00152 } else {
00153 m_exclNCList.push_back(ns);
00154
00155 UpdateList(lb, ns);
00156 }
00157
00158 }
00159 }
00167 void CPKIFNameConstraintsPanel::SetInitialPathSettings(CPKIFPathSettingsPtr& ps)
00168 {
00169 m_initialPathSettings = ps;
00170 }
00178 void CPKIFNameConstraintsPanel::OnInitDialog(wxInitDialogEvent& event)
00179 {
00180
00181 if(m_initialPathSettings != (CPKIFPathSettings*)NULL)
00182 {
00183 wxListBox* lbPermNC = GetListboxPermNC();
00184 wxASSERT(lbPermNC);
00185
00186 CPKIFGeneralSubtreeListPtr initPermNC;
00187 m_initialPathSettings->GetInitialPermSubtrees(initPermNC);
00188
00189 if(initPermNC != (CPKIFGeneralSubtreeList*)NULL)
00190 {
00191
00192 CPKIFGeneralSubtreeList::iterator pos1 = initPermNC->begin();
00193 CPKIFGeneralSubtreeList::iterator end1 = initPermNC->end();
00194
00195 for(int ii = 0; pos1 != end1; ++ii, ++pos1)
00196 {
00197 UpdateList(lbPermNC, (*pos1));
00198 }
00199
00200 }
00201 wxListBox* lbExclNC = GetListboxExclNC();
00202 wxASSERT(lbExclNC);
00203
00204 CPKIFGeneralSubtreeListPtr initExclNC;
00205 m_initialPathSettings->GetInitialExclSubtrees(initExclNC);
00206
00207 if(initExclNC != (CPKIFGeneralSubtreeList*)NULL)
00208 {
00209
00210 CPKIFGeneralSubtreeList::iterator pos2 = initExclNC->begin();
00211 CPKIFGeneralSubtreeList::iterator end2 = initExclNC->end();
00212
00213 for(int ii = 0; pos2 != end2; ++ii, ++pos2)
00214 {
00215 UpdateList(lbExclNC, (*pos2));
00216 }
00217 }
00218 }
00219 }
00220
00228 void UpdateList(wxListBox* lb, CPKIFGeneralSubtreePtr& ns)
00229 {
00230 switch(ns->GetBase()->GetType())
00231 {
00232 case CPKIFGeneralName::RFC822:
00233 {
00234 wxString prefix(wxT("RFC822 Name: "));
00235 wxString wxNS = prefix + WXCASTCHARS(ns->GetBase()->rfc822Name());
00236 lb->InsertItems(1, &wxNS, 0);
00237 lb->SetClientData(0, (void*)ns.get());
00238 }
00239 break;
00240 case CPKIFGeneralName::DNSNAME:
00241 {
00242 wxString prefix(wxT("DNS Name: "));
00243 wxString wxNS = prefix + WXCASTCHARS(ns->GetBase()->dnsName());
00244 lb->InsertItems(1, &wxNS, 0);
00245 lb->SetClientData(0, (void*)ns.get());
00246 }
00247 break;
00248 case CPKIFGeneralName::DIRECTORYNAME:
00249 {
00250 wxString prefix(wxT("Directory Name: "));
00251 wxString wxNS = prefix + WXCASTCHARS(ns->GetBase()->directoryName()->ToString());
00252 lb->InsertItems(1, &wxNS, 0);
00253 lb->SetClientData(0, (void*)ns.get());
00254 }
00255 break;
00256 case CPKIFGeneralName::URI:
00257 {
00258 wxString prefix(wxT("URI: "));
00259 wxString wxNS = prefix + WXCASTCHARS(ns->GetBase()->uri());
00260 lb->InsertItems(1, &wxNS, 0);
00261 lb->SetClientData(0, (void*)ns.get());
00262 }
00263 break;
00264 case CPKIFGeneralName::IPADDRESS:
00265 {
00266 wxString prefix(wxT("IP Address: "));
00267 try {
00268 CPKIFBufferPtr buff = ns->GetBase()->ipAddress();
00269 wxString wxNS = prefix + WXCASTCHARS(DecodeIP(buff).c_str());
00270 lb->InsertItems(1, &wxNS, 0);
00271 lb->SetClientData(0, (void*)ns.get());
00272 } catch(std::exception &) {
00273 wxLogError(wxT("An unsupported IP address was encountered. This name constraint will not be preserved. IPv6 needs to be installed in order to edit IPv6 names."));
00274 wxLog::FlushActive();
00275 }
00276 }
00277 break;
00278 }
00279 }
00287 CPKIFGeneralSubtreeListPtr CPKIFNameConstraintsPanel::GetExcludedNameConstraints()
00288 {
00289 CPKIFGeneralSubtreeListPtr exclNCList(new CPKIFGeneralSubtreeList);
00290
00291 for(int i = 0; i < GetListboxExclNC()->GetCount(); ++i)
00292 {
00293
00294 CPKIFGeneralSubtree* data = (CPKIFGeneralSubtree*) GetListboxExclNC()->GetClientData(i);
00295
00296 CPKIFGeneralNamePtr tmpGN(new CPKIFGeneralName(data->GetBase()->Encoded()));
00297
00298 CPKIFGeneralSubtreePtr tmpGS(new CPKIFGeneralSubtree());
00299 tmpGS->SetBase(tmpGN);
00300 tmpGS->SetMin(0);
00301
00302 exclNCList->push_back(tmpGS);
00303 }
00304
00305 return exclNCList;
00306 }
00314 CPKIFGeneralSubtreeListPtr CPKIFNameConstraintsPanel::GetPermittedNameConstraints()
00315 {
00316 CPKIFGeneralSubtreeListPtr permNCList(new CPKIFGeneralSubtreeList);
00317
00318 for(int i = 0; i < GetListboxPermNC()->GetCount(); ++i)
00319 {
00320
00321 CPKIFGeneralSubtree* data = (CPKIFGeneralSubtree*) GetListboxPermNC()->GetClientData(i);
00322
00323 CPKIFGeneralNamePtr tmpGN(new CPKIFGeneralName(data->GetBase()->Encoded()));
00324
00325 CPKIFGeneralSubtreePtr tmpGS(new CPKIFGeneralSubtree());
00326 tmpGS->SetBase(tmpGN);
00327 tmpGS->SetMin(0);
00328
00329 permNCList->push_back(tmpGS);
00330 }
00331
00332 return permNCList;
00333 }
00341 string DecodeIP(
00343 CPKIFBufferPtr& encodedIP)
00344 {
00345 string rv = "";
00346 if(encodedIP->GetLength() == 32)
00347 {
00348 try {
00349 ip::address_v6::bytes_type bytesAddr;
00350 memcpy(bytesAddr.c_array(), encodedIP->GetBuffer(),encodedIP->GetLength()/2);
00351 ip::address_v6 addr(bytesAddr);
00352 rv = addr.to_string();
00353
00354 rv+="/";
00355
00356 ip::address_v6::bytes_type bytesMask;
00357 memcpy(bytesMask.c_array(), encodedIP->GetBuffer()+encodedIP->GetLength()/2,encodedIP->GetLength()/2);
00358 ip::address_v6 mask(bytesMask);
00359 string sMask = mask.to_string();
00360
00361 rv +=sMask;
00362 } catch(std::exception &) {
00363 wxLogError(wxT("An attempt was made to decode an IPv6 address."));
00364 throw;
00365 }
00366
00367 }
00368 else if(encodedIP->GetLength() == 8)
00369 {
00370 ip::address_v4::bytes_type bytesAddr;
00371 memcpy(bytesAddr.c_array(), encodedIP->GetBuffer(),encodedIP->GetLength()/2);
00372 ip::address_v4 addr(bytesAddr);
00373 rv = addr.to_string();
00374
00375 rv+="/";
00376
00377 ip::address_v4::bytes_type bytesMask;
00378 memcpy(bytesMask.c_array(), encodedIP->GetBuffer()+encodedIP->GetLength()/2,encodedIP->GetLength()/2);
00379 ip::address_v4 mask(bytesMask);
00380 string sMask = mask.to_string();
00381
00382 rv +=sMask;
00383 }
00384 return rv;
00385
00386 }