OID.cpp

Go to the documentation of this file.
00001 
00009 #include "OID.h"
00010 #include "components.h"
00011 #include "ASN1Errors.h"
00012 #include "PKIFException.h"
00013 #include "ASN1Helper.h"
00014 
00015 #include "boost/lexical_cast.hpp"
00016 
00017 #include <iostream>
00018 #include <sstream>
00019 
00020 using namespace std;
00021 
00023 
00024 struct CPKIFOIDImpl
00025 {
00026   CPKIFStringPtr m_oidStr;
00027   ASN1OBJID* m_rawOID;
00028 };
00029 
00031 
00033 //@b Interface: Subsystem
00034 //
00035 //This is a helper function that extracts a string up to a delimeter
00036 //
00037 //@return None
00038 //*/
00039 //void stringtok( 
00040 //  //![in] Input string           
00041 //  std::string &input, 
00042 //  //![in] String that contains the delimiters
00043 //  const std::string &delims, 
00044 //  //![out] String that contains the result
00045 //  std::string &result)
00046 //{
00047 //    std::string::size_type split(input.find_first_of(delims));
00048 //  if(std::string::npos != split)
00049 //  {
00050 //      result = input.substr(0, split);
00051 //
00052 //      std::string::size_type next(input.find_first_not_of(delims, split));
00053 //      if(std::string::npos != next)
00054 //          input.erase(0, next);
00055 //  }
00056 //  else
00057 //  {
00058 //      result = input;
00059 //      input.clear();
00060 //  }
00061 //    //return result;
00062 //}
00063 
00066 //@b Interface: Subsystem
00067 //
00068 //This is a helper function that converts a string representation of an oid into ASN1OBJID object
00069 //
00070 //@return A pointer to ASN1OBJID object corresponding to the string form provided in the str parameter
00071 //*/
00072 //ASN1OBJID* ConvertStringToASN1OBJID(
00073 //  //![in] This is a smart poiter to a CPKIFString object containing the dot form of OID that will be converted to ASN1OBJID object 
00074 //  CPKIFStringPtr str)
00075 //{
00076 //  if(str == NULL || str->empty())
00077 //      return NULL;
00078 //
00079 //  //get a copy of the string in a non-reference counted form (we will be modifying this copy)
00080 //  std::string oidStr = *str;
00081 //
00082 //  //see if there;s any stuff in the str passed in that isn't valid for an OID
00083 //  if(std::string::npos != oidStr.find_first_not_of("0123456789."))
00084 //      return NULL;
00085 //
00086 //  //create a new OID...
00087 //  ASN1OBJID* theOid = new ASN1OBJID;
00088 //  int numIds = 0;
00089 //  
00090 //  //and fill it
00091 //  std::string result;
00092 //  
00093 //  stringtok(oidStr, ".", result);
00094 //  while (!result.empty())
00095 //  {
00096 //      theOid->subid[numIds] = atoi(result.c_str());
00097 //      numIds++;
00098 //      stringtok(oidStr, ".", result);
00099 //  }
00100 //
00101 //  theOid->numids = numIds;
00102 //  return theOid;
00103 //}
00111 CPKIFOID::CPKIFOID()
00112   :m_impl (new CPKIFOIDImpl)
00113 {
00114     m_impl->m_rawOID = NULL;
00115 }
00123 //ASN1OBJID* CPKIFOID::raw() const 
00124 //{
00125 //  return m_impl->m_rawOID;
00126 //}
00127 CPKIFBufferPtr CPKIFOID::rawOID() const 
00128 {
00129     CACASNWRAPPER_CREATE(CACX509V3CertPolicyId, objPDU);
00130     ASN1OpenType* data1 = objPDU.Encode(m_impl->m_rawOID);
00131     CPKIFBufferPtr tmp(new CPKIFBuffer(data1->data, data1->numocts));
00132     if(data1)
00133         delete data1;
00134     //CPKIFBufferPtr tmp;
00135     return tmp;
00136 }
00157 CPKIFOID::CPKIFOID(
00159     const unsigned int oid[],
00161     const unsigned int size)
00162   :m_impl (new CPKIFOIDImpl)
00163 {
00164     //copy OID contents into new ASN1OBJID object
00165     m_impl->m_rawOID = new ASN1OBJID;
00166     m_impl->m_rawOID->numids = size;//oid.numids;
00167     for(unsigned int ii = 0; ii < size; ++ii)
00168         m_impl->m_rawOID->subid[ii] = oid[ii];
00169 }
00190 CPKIFOID::CPKIFOID(
00192     const CPKIFOID& alg)
00193   :m_impl (new CPKIFOIDImpl)
00194 {
00195     if(NULL == alg.m_impl->m_rawOID)
00196     {
00197         m_impl->m_rawOID = NULL;
00198 
00199         if(alg.m_impl->m_oidStr != NULL)
00200             m_impl->m_rawOID = ConvertStringToASN1OBJID(alg.m_impl->m_oidStr);
00201     }
00202     else
00203     {
00204         m_impl->m_rawOID = new ASN1OBJID;
00205         m_impl->m_rawOID->numids = alg.m_impl->m_rawOID->numids;
00206         for(unsigned int ii = 0; ii < alg.m_impl->m_rawOID->numids; ++ii)
00207             m_impl->m_rawOID->subid[ii] = alg.m_impl->m_rawOID->subid[ii];
00208     }
00209 }
00232 CPKIFOID::CPKIFOID(
00234     const CPKIFStringPtr& oidStr)
00235   :m_impl (new CPKIFOIDImpl)
00236 {
00237     m_impl->m_rawOID = ConvertStringToASN1OBJID(oidStr);
00238 
00239     if(!m_impl->m_rawOID)
00240     {
00241         //added 9/13/03
00242         throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_OID);
00243     }
00244 }
00268 CPKIFOID::CPKIFOID(
00270     const std::string& oidStr)
00271   :m_impl (new CPKIFOIDImpl)
00272 {
00273     CPKIFStringPtr strPtr(new std::string(oidStr));
00274     m_impl->m_rawOID = ConvertStringToASN1OBJID(strPtr);
00275     if(!m_impl->m_rawOID)
00276     {
00277         //added 9/13/03
00278         throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_OID);
00279     }
00280 }
00290 CPKIFOID::CPKIFOID(
00292     const CPKIFBufferPtr& oidBuff)
00293   :m_impl (new CPKIFOIDImpl)
00294 {
00295     CACASNWRAPPER_CREATE(CACX509V3CertPolicyId, objPDU);
00296     objPDU.Decode(oidBuff->GetBuffer(), oidBuff->GetLength());
00297 
00298     m_impl->m_rawOID = new ASN1OBJID;
00299     CopyOID(m_impl->m_rawOID, objPDU.data());
00300 
00301 }
00309 CPKIFOID::~CPKIFOID()
00310 {
00311     if(m_impl)
00312     {
00313         if(NULL != m_impl->m_rawOID)
00314         {
00315             delete m_impl->m_rawOID; m_impl->m_rawOID = NULL;
00316         }
00317 
00318         delete m_impl;
00319         m_impl = 0;
00320     }
00321 }
00332 const char* CPKIFOID::ToString() const
00333 {
00334     //if we've already produced a string version return it
00335     if(m_impl->m_oidStr != NULL)
00336         return m_impl->m_oidStr->c_str();
00337 
00338     //otherwise create a new one
00339     std::string* oidStr = new std::string();
00340     //char numbuf[25];
00341     
00342     //added check for NULL 7/12/2003
00343     if(NULL == m_impl->m_rawOID)
00344     {
00345         throw CPKIFException(TOOLKIT_ASN, ASN1_INVALID_OID);
00346     }
00347 
00348     ostringstream os;
00349     for (unsigned int curId = 0; curId < m_impl->m_rawOID->numids; ++curId)
00350     {
00351         
00352         if (curId == 0)
00353             os << m_impl->m_rawOID->subid[curId];
00354         else
00355         {
00356             os << "." << m_impl->m_rawOID->subid[curId];
00357         }
00358         
00359     }
00360     *oidStr = os.str();
00361     CPKIFStringPtr tmpOIDStr(oidStr);
00362     CPKIFOID* nonConstCACOID = const_cast<CPKIFOID*>(this);
00363     nonConstCACOID->m_impl->m_oidStr = tmpOIDStr;
00364     return m_impl->m_oidStr->c_str();
00365 }
00374 bool CPKIFOID::operator ==(
00376     const CPKIFOIDPtr& oid) const
00377 {
00378     return *this == *oid;
00379 }
00387 bool CPKIFOID::operator ==(
00389     const char* strOID) const
00390 {
00391     //added check to avoid conversion is a string is available - 8/26/2004
00392     if(m_impl->m_oidStr == (std::string*)NULL)
00393     {
00394         //CPKIFOID oid(std::string(strOID));
00395         std::string sOID(strOID);
00396         CPKIFOID rhs( (std::string(strOID)) );
00397         return *this == rhs;
00398     }
00399     else
00400     {
00401         return 0 == strcmp(m_impl->m_oidStr->c_str(), strOID);
00402     }
00403 }
00411 bool CPKIFOID::operator ==(
00413     const CPKIFOID& oid) const
00414 {
00415     if(NULL == oid.m_impl->m_rawOID)
00416         return false;
00417 
00418     if(m_impl->m_rawOID->numids != oid.m_impl->m_rawOID->numids)
00419         return false;
00420 
00421     //changed to start search at end of OID
00422     for(int ii = m_impl->m_rawOID->numids - 1; ii >= 0; --ii) 
00423         if(m_impl->m_rawOID->subid[ii] != oid.m_impl->m_rawOID->subid[ii])
00424             return false;
00425 
00426     return true;
00427 }
00435 bool CPKIFOID::operator !=(
00437     const CPKIFOID& oid) const
00438 {
00439     return !(this->operator ==(oid));
00440 }
00441 
00442 //changed to & - 7/28/2004
00450 bool CPKIFOID::operator !=(
00452     CPKIFOIDPtr& oid) const
00453 {
00454     return !(this->operator ==(oid));
00455 }
00463 bool CPKIFOID::operator !=(
00465     const char* strOID) const
00466 {
00467     return !(this->operator ==(strOID));
00468 }

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