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
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00111 CPKIFOID::CPKIFOID()
00112 :m_impl (new CPKIFOIDImpl)
00113 {
00114 m_impl->m_rawOID = NULL;
00115 }
00123
00124
00125
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
00135 return tmp;
00136 }
00157 CPKIFOID::CPKIFOID(
00159 const unsigned int oid[],
00161 const unsigned int size)
00162 :m_impl (new CPKIFOIDImpl)
00163 {
00164
00165 m_impl->m_rawOID = new ASN1OBJID;
00166 m_impl->m_rawOID->numids = size;
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
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
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
00335 if(m_impl->m_oidStr != NULL)
00336 return m_impl->m_oidStr->c_str();
00337
00338
00339 std::string* oidStr = new std::string();
00340
00341
00342
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
00392 if(m_impl->m_oidStr == (std::string*)NULL)
00393 {
00394
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
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
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 }