PathSettingsSerializer.cpp

Go to the documentation of this file.
00001 
00010 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && defined(PKIFRESOURCES_ALLOW_GCC_PRAGMA)
00011     #pragma interface "PathSettingsSerializer.cpp"
00012 #endif
00013 
00014 
00015 #include "PathSettingsSerializer.h"
00016 #include "wx/config.h" 
00017 #include "wx/fileconf.h"
00018 #include "pkif.h"
00019 #include "PKIFBase64.h"
00020 #ifdef _WIN32
00021 #include "pkifregconf.h"
00022 #endif
00023 #include "wx/log.h"
00024 
00025 #include "PRStringUtils.h"
00026 
00027 #define PKIFRESOURCES_PATH_SERIALIZER           TOOLKIT_APP + 2
00028 
00029 
00030 void SaveSettings_wx(CPKIFPathSettingsPtr& settingsToSave, wxConfigBase* conf, const char* localFile, bool useFile, bool bAllUsers, const char* pathRelToCompanyApp);
00031 
00039 CPKIFPathSettingsSerializer::CPKIFPathSettingsSerializer()
00040 {
00041 }
00042 
00043 
00051 CPKIFPathSettingsSerializer::~CPKIFPathSettingsSerializer()
00052 {
00053 }
00054 
00070 void CPKIFPathSettingsSerializer::SaveSettings(
00072     CPKIFPathSettingsPtr& settingsToSave, 
00074     const char* application,
00076     const char* company, 
00078     const char* localFile,
00080     bool useFile, 
00081     bool bAllUsers, 
00082     const char* pathRelToCompanyApp)
00083 {
00084     const char* pCompany = "Orion Security Solutions";
00085     const char* pApp = "PKIFv2";
00086     const char* pLocalFile = "";
00087     if(company)
00088         pCompany = company;
00089     if(application)
00090         pApp = application;
00091     if(localFile)
00092         pLocalFile = localFile;
00093 
00094     wxString empty;
00095 
00096     //wxConfig * conf = 0;
00097     wxConfigBase * conf = 0;
00098     if(useFile)
00099     { 
00100         conf = new wxFileConfig(wxString(pApp,wxConvUTF8), wxString(pCompany,wxConvUTF8), wxString(pLocalFile,wxConvUTF8), empty, wxCONFIG_USE_LOCAL_FILE);
00101     }
00102     else
00103     {
00104 #ifdef _WIN32
00105         conf = new wxPkifRegConfig(wxString(pApp,wxConvUTF8),wxString(pCompany,wxConvUTF8), wxEmptyString, wxEmptyString, bAllUsers ? wxCONFIG_USE_GLOBAL_FILE : 0, pathRelToCompanyApp);
00106 #else
00107         conf = new wxConfig(wxString(pApp,wxConvUTF8),wxString(pCompany,wxConvUTF8));
00108 #endif
00109     }
00110 
00111 
00112     try
00113     {
00114         SaveSettings_wx(settingsToSave, conf, localFile, useFile, bAllUsers, pathRelToCompanyApp);
00115         conf->Flush();
00116         if(conf) delete conf;
00117     }
00118     catch(...)
00119     {
00120         if(conf) delete conf;
00121     }
00122 }
00123     
00139 void SaveSettings_wx(
00141     CPKIFPathSettingsPtr& settingsToSave, 
00143     wxConfigBase* conf,
00145     const char* localFile,
00147     bool useFile, 
00148     bool bAllUsers, 
00149     const char* pathRelToCompanyApp)
00150 {
00151 
00152     wxString wxOldPath = conf->GetPath();
00153     if(pathRelToCompanyApp)
00154     {
00155         wxString wxPathRelToCompanyApp(pathRelToCompanyApp,wxConvUTF8);
00156         conf->SetPath(wxPathRelToCompanyApp);
00157     }
00158 
00159     //conf->DeleteAll();
00160     conf->DeleteGroup(wxT("Path Settings"));
00161 
00162     if(NULL == settingsToSave)
00163     {
00164         conf->SetPath(wxOldPath);
00165         return;
00166     }
00167 
00168 
00169     try
00170     {
00171         if(!conf->Write(wxT("Path Settings/CheckRevocationStatus"), settingsToSave->GetCheckRevocationStatus()))
00172             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing CheckRevocationStatus");
00173         if(!conf->Write(wxT("Path Settings/PathDepth"),  settingsToSave->GetDepth()))
00174             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing PathDepth");
00175         if(!conf->Write(wxT("Path Settings/RequireExplicitPolicy"), settingsToSave->GetInitialExplicitPolicyIndicator()))
00176             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing RequireExplicitPolicy");
00177         if(!conf->Write(wxT("Path Settings/InhibitPolicyMapping"), settingsToSave->GetInitialPolicyMappingInhibitIndicator()))
00178             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing InhibitPolicyMapping");
00179         if(!conf->Write(wxT("Path Settings/InhibitAnyPolicy"), settingsToSave->GetInitialInhibitAnyPolicyIndicator()))
00180             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing InhibitAnyPolicy");
00181         if(!conf->Write(wxT("Path Settings/RequireFreshRevData"), settingsToSave->GetRequireFreshRevocationData()))
00182             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing RequireFreshRevData");
00183         if(!conf->Write(wxT("Path Settings/RequireRecentRevData"), settingsToSave->GetRequireSufficientlyRecent()))
00184             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing RequireRecentRevData");
00185         if(!conf->Write(wxT("Path Settings/UseValidatorFilterWhenBuilding"), settingsToSave->GetUseValidatorFilterWhenBuilding()))
00186             throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing UseValidatorFilterWhenBuilding");
00187 
00188         if(settingsToSave->GetRequireSufficientlyRecent())
00189         {
00190             CPKIFDurationPtr duration = settingsToSave->GetDuration();
00191             if(duration != (CPKIFDuration*)NULL && !conf->Write(wxT("Path Settings/Duration"), duration->asSeconds()))
00192                 throw CPKIFException(PKIFRESOURCES_PATH_SERIALIZER, COMMON_UNKNOWN_ERROR, "Settings serialization failed while writing Duration");
00193         }
00194 
00195         //policy
00196         wxString group(wxT("Path Settings/CertificatePolicies"));
00197 
00198         CPKIFPolicyInformationListPtr initPolSet;
00199         settingsToSave->GetInitialPolicySet(initPolSet);
00200 
00201         CPKIFPolicyInformationList::iterator pos = initPolSet->begin();
00202         CPKIFPolicyInformationList::iterator end = initPolSet->end();
00203 
00204         for(int ii = 0; pos != end; ++ii, ++pos)
00205         {
00206             wxString key;
00207             key.Printf(wxT("%s/%02d"),WXCASTCHARS(group.c_str()),ii);
00208             conf->Write(key, WXCASTCHARS((*pos)->PolicyOID()->ToString()));
00209         }
00210 
00211         //ExcludedNameConstraints
00212         wxString groupExcludedNameConstraints(wxT("Path Settings/ExcludedNameConstraints"));
00213 
00214         CPKIFGeneralSubtreeListPtr exclNC;
00215         settingsToSave->GetInitialExclSubtrees(exclNC);
00216 
00217         if(exclNC != (CPKIFGeneralSubtreeList*)NULL)
00218         {
00219             CPKIFGeneralSubtreeList::iterator pos2 = exclNC->begin();
00220             CPKIFGeneralSubtreeList::iterator end2 = exclNC->end();
00221 
00222             for(int ii = 0; pos2 != end2; ++ii, ++pos2)
00223             {
00224                 CPKIFBufferPtr buff = (*pos2)->GetBase()->Encoded();
00225                 char * encoded = NULL;
00226                 B64Encode(buff->GetBuffer(), buff->GetLength(), &encoded);
00227                 wxString key;
00228                 key.Printf(wxT("%s/%02d"),groupExcludedNameConstraints.c_str(),ii);
00229                 conf->Write(key, WXCASTCHARS(encoded));
00230                 if(encoded) {
00231                     delete encoded;
00232                 }
00233             }
00234         }
00235 
00236         //PermittedNameConstraints
00237         wxString groupPermittedNameConstraints(wxT("Path Settings/PermittedNameConstraints"));
00238 
00239         CPKIFGeneralSubtreeListPtr permNC;
00240         settingsToSave->GetInitialPermSubtrees(permNC);
00241 
00242         if(permNC != (CPKIFGeneralSubtreeList*)NULL)
00243         {
00244             CPKIFGeneralSubtreeList::iterator pos3 = permNC->begin();
00245             CPKIFGeneralSubtreeList::iterator end3 = permNC->end();
00246 
00247             for(int ii = 0; pos3 != end3; ++ii, ++pos3)
00248             {
00249                 CPKIFBufferPtr buff = (*pos3)->GetBase()->Encoded();
00250                 char * encoded = NULL;
00251                 B64Encode(buff->GetBuffer(), buff->GetLength(), &encoded);
00252                 wxString key;
00253                 key.Printf(wxT("%s/%02d"),groupPermittedNameConstraints.c_str(),ii);
00254                 conf->Write(key, WXCASTCHARS(encoded));
00255                 if(encoded) {
00256                     delete encoded;
00257                 }
00258             }
00259         }
00260 
00261         
00262     }
00263     catch(...)
00264     {
00265         throw;
00266     }
00267 
00268     conf->SetPath(wxOldPath);
00269     // on UNIX, sometimes settings don't get saved unless you call this explicitly
00270     conf->Flush();
00271 }
00272 
00279 CPKIFPathSettingsPtr CPKIFPathSettingsSerializer::LoadSettings(
00281     const char* application,
00283     const char* company, 
00285     const char* localFile, 
00287     bool useFile,
00288     bool bAllUsers,
00289     const char* pathRelToCompanyApp)
00290 
00291 {
00292     CPKIFPathSettingsPtr ps(new CPKIFPathSettings);
00293     bool b = false;
00294     int i = 0;
00295 
00296     wxString empty;
00297 
00298     const char* pCompany = "Orion Security Solutions";
00299     const char* pApp = "PKIFv2";
00300     const char* pLocalFile = "";
00301     if(company)
00302         pCompany = company;
00303     if(application)
00304         pApp = application;
00305     if(localFile)
00306         pLocalFile = localFile;
00307 
00308     wxLogNull ln;
00309 
00310     //wxConfig * conf = 0;
00311     wxConfigBase * conf = 0;
00312     if(useFile)
00313     { 
00314         conf = new wxFileConfig(wxString(pApp,wxConvUTF8), wxString(pCompany,wxConvUTF8), wxString(pLocalFile,wxConvUTF8), empty, wxCONFIG_USE_LOCAL_FILE);
00315     }
00316     else
00317     {
00318 #ifdef _WIN32
00319         conf = new wxPkifRegConfig(wxString(pApp,wxConvUTF8),wxString(pCompany,wxConvUTF8), wxEmptyString, wxEmptyString, bAllUsers ? wxCONFIG_USE_GLOBAL_FILE : 0, pathRelToCompanyApp);
00320 #else
00321         conf = new wxConfig(wxString(pApp,wxConvUTF8),wxString(pCompany,wxConvUTF8));
00322 #endif
00323     }
00324     
00325     if(pathRelToCompanyApp)
00326     {
00327         wxString wxPathRelToCompanyApp(pathRelToCompanyApp,wxConvUTF8);
00328         conf->SetPath(wxPathRelToCompanyApp);
00329     }
00330 
00331     try
00332     {
00333         if(conf->Read(wxT("Path Settings/CheckRevocationStatus"), &b, true))
00334             ps->SetCheckRevocationStatus(b);
00335 
00336         if(conf->Read(wxT("Path Settings/PathDepth"), &i, true))
00337             ps->SetDepth(i);
00338 
00339         if(conf->Read(wxT("Path Settings/RequireExplicitPolicy"), &b, true))
00340             ps->SetInitialExplicitPolicyIndicator(b);
00341 
00342         if(conf->Read(wxT("Path Settings/InhibitPolicyMapping"), &b, true))
00343             ps->SetInitialPolicyMappingInhibitIndicator(b);
00344 
00345         if(conf->Read(wxT("Path Settings/InhibitAnyPolicy"), &b, true))
00346             ps->SetInitialInhibitAnyPolicyIndicator(b);
00347 
00348         if(conf->Read(wxT("Path Settings/RequireFreshRevData"), &b, true))
00349             ps->SetRequireFreshRevocationData(b);
00350 
00351         if(conf->Read(wxT("Path Settings/RequireRecentRevData"), &b, true))
00352             ps->SetRequireSufficientlyRecent(b);
00353 
00354         if(conf->Read(wxT("Path Settings/UseValidatorFilterWhenBuilding"), &b, true))
00355             ps->SetUseValidatorFilterWhenBuilding(b);
00356 
00357         if(ps->GetRequireSufficientlyRecent())
00358         {
00359             double d = 0.0;
00360             if(conf->Read(wxT("Path Settings/Duration"), &d))
00361                 ps->SetSufficientlyRecent(d);
00362 
00363             
00364         }
00365 
00366         //policy
00367         CPKIFPolicyInformationListPtr polList(new CPKIFPolicyInformationList);
00368         wxString group(wxT("Path Settings/CertificatePolicies"));
00369         
00370         long ii = 0;
00371         unsigned long curr = 0;
00372         wxString oldPath = conf->GetPath();
00373         wxString path(wxT("Path Settings/CertificatePolicies"));
00374         conf->SetPath(path);
00375         while(conf->GetNextEntry(path,ii))
00376         {
00377             wxString key, val;
00378             key = wxString::Format(wxT("%02d"),curr);
00379             //key.Printf(wxT("%s/%02d"),oldPath.c_str(),curr);
00380             if(!conf->Read(key, &val))
00381                 continue;
00382 
00383             std::string strOID((const char *)val.mb_str());
00384             CPKIFOIDPtr polOID(new CPKIFOID(strOID));
00385             CPKIFPolicyInformationPtr pol(new CPKIFPolicyInformation(polOID));
00386             polList->push_back(pol);
00387             curr++;
00388         }
00389         conf->SetPath(oldPath);
00390         if(curr > 0)
00391             ps->SetInitialPolicySet(polList);
00392 
00393         //ExclNameConstraints
00394         CPKIFGeneralSubtreeListPtr exclNCList(new CPKIFGeneralSubtreeList);
00395         
00396         long ii2 = 0;
00397         unsigned long curr2 = 0;
00398         wxString path2(wxT("Path Settings/ExcludedNameConstraints"));
00399         wxString oldPath2 = conf->GetPath();
00400         conf->SetPath(path2);
00401         while(conf->GetNextEntry(path2,ii2))
00402         {
00403             wxString key, val;
00404             key = wxString::Format(wxT("%02d"),curr2 );
00405             if(!conf->Read(key, &val))
00406                 continue;
00407 
00408             if(!val.empty())
00409             {
00410                 unsigned char * decodedNC = NULL;
00411                 unsigned long decodedNCLen;
00412                 wxASCIIHelper vala(val);
00413                 bool b = B64Decode(vala.to_chars(), &decodedNC, &decodedNCLen);
00414                 if(b)
00415                 {   
00416                     try
00417                     {
00418                         //stick the decoded name in a buffer object (which can handle the cleanup for us)
00419                         CPKIFBufferPtr buf(new CPKIFBuffer(true, decodedNC, decodedNCLen));
00420 
00421                         CPKIFGeneralNamePtr gn(new CPKIFGeneralName(buf));  
00422                         CPKIFGeneralSubtreePtr subtree(new CPKIFGeneralSubtree());
00423                         subtree->SetBase(gn);
00424                         subtree->SetMin(0);
00425                         exclNCList->push_back(subtree);
00426                     }
00427                     catch(...)
00428                     {
00429                     }
00430                 }
00431             }
00432             curr2++;
00433         }
00434         conf->SetPath(oldPath2);
00435         if(curr2 > 0)
00436             ps->SetInitialExclSubtrees(exclNCList);
00437 
00438         //PermittedNameConstraints
00439         CPKIFGeneralSubtreeListPtr permNCList(new CPKIFGeneralSubtreeList);
00440         
00441         long ii3 = 0;
00442         unsigned long curr3 = 0;
00443         wxString path3(wxT("Path Settings/PermittedNameConstraints"));
00444         wxString oldPath3 = conf->GetPath();
00445         conf->SetPath(path3);
00446         while(conf->GetNextEntry(path3,ii3))
00447         {
00448             wxString key, val;
00449             key = wxString::Format(wxT("%02d"),curr3);
00450             if(!conf->Read(key, &val))
00451                 continue;
00452 
00453             if(!val.empty())
00454             {
00455                 unsigned char * decodedNC = NULL;
00456                 unsigned long decodedNCLen;
00457                 wxASCIIHelper vala(val);
00458                 bool b = B64Decode(vala.to_chars(), &decodedNC, &decodedNCLen);
00459                 if(b)
00460                 {   
00461                     try
00462                     {
00463                         //stick the decoded name in a buffer object (which can handle the cleanup for us)
00464                         CPKIFBufferPtr buf(new CPKIFBuffer(true, decodedNC, decodedNCLen));
00465 
00466                         CPKIFGeneralNamePtr gn(new CPKIFGeneralName(buf));  
00467                         CPKIFGeneralSubtreePtr subtree(new CPKIFGeneralSubtree());
00468                         subtree->SetBase(gn);   
00469                         subtree->SetMin(0);             
00470                         permNCList->push_back(subtree);
00471                     }
00472                     catch(...)
00473                     {
00474                     }
00475                 }
00476             }
00477             curr3++;
00478         }
00479         conf->SetPath(oldPath3);
00480         if(curr3 > 0)
00481             ps->SetInitialPermSubtrees(permNCList);
00482     }
00483     catch(...)
00484     {
00485         if(conf) delete conf;
00486         throw;
00487     }
00488     if(conf) delete conf;
00489     return ps;
00490 }

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