ResourceUtils.cpp

Go to the documentation of this file.
00001 
00010 #include "ResourceUtils.h"
00011 #include "boost/filesystem/operations.hpp"
00012 #include <boost/tokenizer.hpp>
00013 namespace fs = boost::filesystem;
00014 #include <fstream>
00015 
00016 using namespace std;
00024 CPKIFCertificatePtr GetCert(
00026     wxString& filename)
00027 {
00028     if(fs::path::default_name_check_writable()) boost::filesystem::path::default_name_check(boost::filesystem::native);
00029 
00030     boost::filesystem::path p(filename.mb_str());
00031     int len = boost::filesystem::file_size(p);
00032     CPKIFBufferPtr buf(new CPKIFBuffer);
00033     buf->AllocateBuffer(len);
00034 
00035     std::ifstream i(filename.mb_str(), ios::in | ios::binary);
00036     i.read((char*)buf->GetBuffer(), len);
00037     i.close();
00038 
00039     CPKIFCertificatePtr cert(new CPKIFCertificate);
00040     try
00041     {
00042         cert->Decode(buf->GetBuffer(), len);
00043     }catch(CPKIFException&)
00044     {
00045         wxMessageBox(wxT("Please specify a Valid certificate file"),wxT("Error"),wxICON_ERROR | wxOK);
00046         CPKIFCertificatePtr nullPtr;
00047         return nullPtr;
00048     }
00049         return cert;
00050 }
00058 CPKIFCRLPtr GetCrl(
00060     wxString& filename)
00061 {
00062     if(fs::path::default_name_check_writable()) boost::filesystem::path::default_name_check(boost::filesystem::native);
00063 
00064     boost::filesystem::path p(filename.mb_str());
00065     int len = boost::filesystem::file_size(p);
00066     CPKIFBufferPtr buf(new CPKIFBuffer);
00067     buf->AllocateBuffer(len);
00068 
00069     std::ifstream i(filename.mb_str(), ios::in | ios::binary);
00070     i.read((char*)buf->GetBuffer(), len);
00071     i.close();
00072 
00073     CPKIFCRLPtr crl(new CPKIFCRL);
00074     try
00075     {
00076         crl->Decode(buf->GetBuffer(), len);
00077     }catch(CPKIFException&)
00078     {
00079         wxMessageBox(wxT("Please specify a Valid CRL file"),wxT("Error"),wxICON_ERROR | wxOK);
00080         CPKIFCRLPtr nullPtr;
00081         return nullPtr;
00082     }
00083     return crl;
00084 }
00092 void StringSplit(
00094     const string & text,
00096     string & separators,
00098     vector<string> & words)
00099  {
00100     size_t n = text.length();
00101     size_t start, stop;
00102 
00103     start = text.find_first_not_of(separators);
00104     while ((start >= 0) && (start < n)) {
00105        stop = text.find_first_of(separators, start);
00106        if ((stop < 0) || (stop > n)) stop = n;
00107        words.push_back(text.substr(start, stop - start));
00108        start = text.find_first_not_of(separators, stop+1);
00109        }
00110  }
00118 bool ValidateOID(const wxString& attrVal)
00119 {
00120     if(attrVal.First(wxT(".")) == 0 || attrVal.Last() == wxT('.') || attrVal.Find(wxT("..")) != -1)
00121         return false;
00122     vector<string> parts;
00123     string separator(".");
00124     StringSplit(string(attrVal.mb_str()), separator, parts);
00125 
00126 
00127     if(parts[0] != "0" && parts[0] != "1" && parts[0] != "2" )
00128         return false;
00129 
00130     return true;
00131 }
00132 
00140 bool ValidateOIDComponents(const wxString& attrVal)
00141 {
00142     std::string tmpStdString(attrVal.ToAscii());
00143 
00144     boost::char_separator<char> period(".");
00145     boost::tokenizer<boost::char_separator<char> > tokenizer(tmpStdString, period);
00146     boost::tokenizer<boost::char_separator<char> >::iterator pos;
00147     boost::tokenizer<boost::char_separator<char> >::iterator end = tokenizer.end();
00148     for(pos = tokenizer.begin(); pos != end; ++pos)
00149     {
00150         int subNum = atoi((*pos).c_str());
00151         
00152         if(subNum == 2147483647)
00153         {
00154             return false;
00155         }
00156     }
00157 
00158     return true;
00159 }
00167 string GetOIDString(const CPKIFOIDPtr &oid)
00168 {
00169     string rv = "Unknown";
00170     if(oid == g_md2WithRSAEncryption)
00171     {
00172         rv = "md2WithRSAEncryption";
00173     }
00174     else if(*oid == *g_md5WithRSAEncryption)
00175     {
00176         rv = "md5WithRSAEncryption";
00177     }
00178     else if(*oid == *g_sha1WithRSAEncryption)
00179     {
00180         rv = "sha1WithRSAEncryption";
00181     }
00182     else if(*oid == *g_sha224WithRSAEncryption)
00183     {
00184         rv = "sha224WithRSAEncryption";
00185     }
00186     else if(*oid == *g_sha256WithRSAEncryption)
00187     {
00188         rv = "sha256WithRSAEncryption";
00189     }
00190     else if(*oid == *g_sha384WithRSAEncryption)
00191     {
00192         rv = "sha384WithRSAEncryption";
00193     }
00194     else if(*oid == *g_sha512WithRSAEncryption)
00195     {
00196         rv = "sha512WithRSAEncryption";
00197     }
00198     else if(*oid == *g_dsaWithSHA1)
00199     {
00200         rv = "dsaWithSHA1";
00201     }
00202     else if(*oid == *g_dsaWithSHA1Alternative)
00203     {
00204         rv = "dsaWithSHA1Alternative";
00205     }
00206     else if(*oid == *g_rsa)
00207     {
00208         rv = "RSA";
00209     }
00210     else if(*oid == *g_dsa)
00211     {
00212         rv = "DSA";
00213     }
00214     else if(*oid == *g_ecc)
00215     {
00216         rv = "ECC";
00217     }
00218     else if(*oid == *g_ecdsa_sha1)
00219     {
00220         rv = "ecdsa_sha1";
00221     }
00222     else if(*oid == *g_ecdsa_sha224)
00223     {
00224         rv = "ecdsa_sha224";
00225     }
00226     else if(*oid == *g_ecdsa_sha256)
00227     {
00228         rv = "ecdsa_sha256";
00229     }
00230     else if(*oid == *g_ecdsa_sha384)
00231     {
00232         rv = "ecdsa_sha384";
00233     }
00234     else if(*oid == *g_ecdsa_sha512)
00235     {
00236         rv = "ecdsa_sha512";
00237     }
00238     else if(*oid == *g_sha1)
00239     {
00240         rv = "sha1";
00241     }
00242     else if(*oid == *g_sha224)
00243     {
00244         rv = "sha244";
00245     }
00246     else if(*oid == *g_sha256)
00247     {
00248         rv = "sha256";
00249     }
00250     else if(*oid == *g_sha384)
00251     {
00252         rv = "sha384";
00253     }
00254     else if(*oid == *g_sha512)
00255     {
00256         rv = "sha512";
00257     }
00258     else if(0 == strcmp(oid->ToString(), "1.3.6.1.5.5.7.2.1"))
00259     {
00260         rv = "CPS";
00261     }
00262     else if(0 == strcmp(oid->ToString(), "1.3.6.1.5.5.7.2.2"))
00263     {
00264         rv = "User Notice";
00265     }
00266     return rv;
00267 }
00275 const wxString& GetBufAsAscii(
00277     const CPKIFBufferPtr& in,
00279     wxString& out)
00280 {
00281     size_t outLen = in->GetLength()*2+1;
00282     char * outChars = new char[outLen];
00283     memset(outChars,0x00,outLen);
00284     btoa((char*)in->GetBuffer(), outChars, in->GetLength());
00285     out = wxString(outChars,wxConvUTF8);
00286     delete[] outChars;
00287     return out;
00288 }

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