PolicyInformationSet.cpp

Go to the documentation of this file.
00001 
00009 #include "PolicyInformationSet.h"
00010 #include "PolicyInformation.h"
00011 #include "PolicyQualifier.h"
00012 
00013 #include "OID.h"
00014 #include "Buffer.h"
00015 
00016 #include "ASN1Helper.h"
00017 #include "ToolkitUtils.h"
00018 #include "PKIX1Implicit88.h"
00019 #include "PKIX1Explicit88.h"
00020 
00021 #include <iostream>
00022 #include <boost/scoped_array.hpp>
00023 using std::endl;
00024 
00026 
00027 struct CPKIFPolicyInformationSetImpl
00028 {
00029     CPKIFPolicyInformationListPtr m_policyList;
00030     CPKIFBufferPtr m_value;
00031     bool m_extModified;
00032 };
00033 
00035 
00036 char CPKIFPolicyInformationSet::extOID[] =      "2.5.29.32";
00037 
00038 void FreeList(DList* asn1rtdlist);
00039 
00047 CPKIFPolicyInformationSet::CPKIFPolicyInformationSet()
00048   :m_impl (new CPKIFPolicyInformationSetImpl)
00049 {
00050 }
00051 //commented out - 8/21/2004
00052 //CPKIFPolicyInformationSet::CPKIFPolicyInformationSet(const CPKIFPolicyInformationSet& ext)
00053 //{
00054 //}
00055 
00064 CPKIFPolicyInformationListPtr CPKIFPolicyInformationSet::GetPolicySet() const 
00065 {
00066     return m_impl->m_policyList;
00067 }
00068 
00069 
00077 void CPKIFPolicyInformationSet::SetPolicySet(CPKIFPolicyInformationListPtr &polSet) 
00078 {
00079     m_impl->m_policyList = polSet;
00080     m_impl->m_extModified = true;
00081 }
00082 
00093 //CPKIFPolicyInformationSet::CPKIFPolicyInformationSet(
00094 //  //! [in] Reference to an internal structure representation of the extension containing 
00095 //  //! information used to construct the object
00096 //  const CACX509V3Extension& ext) 
00097 //  : CPKIFX509Extension(ext), m_impl (new CPKIFPolicyInformationSetImpl)
00098 //{
00099 //  CACASNWRAPPER_CREATE(CACX509V3CertificatePolicies, objPDU);
00100 //  objPDU.Decode(ext.extnValue.data, ext.extnValue.numocts);
00101 //
00102 //  DListNode* cur = objPDU->head;
00103 //  if(NULL != cur)
00104 //  {
00105 //      CPKIFPolicyInformationListPtr piList(new CPKIFPolicyInformationList);
00106 //      m_impl->m_policyList = piList;
00107 //  }
00108 //  while(NULL != cur)
00109 //  {
00110 //      CACASNWRAPPER_CREATE(CACX509V3PolicyInformation, extsWrapper);
00111 //      ASN1OpenType *data = extsWrapper.Encode ((CACX509V3PolicyInformation*)cur->data);
00112 //      CPKIFBufferPtr tempBuffer(new CPKIFBuffer(data->data, data->numocts));
00113 //      //CPKIFPolicyInformationPtr pi(new CPKIFPolicyInformation((CACX509V3PolicyInformation*)cur->data));
00114 //      CPKIFPolicyInformationPtr pi(new CPKIFPolicyInformation(tempBuffer));
00115 //      m_impl->m_policyList->push_back(pi);
00116 //      cur = cur->next;
00117 //  }
00118 //}
00119 
00130 CPKIFPolicyInformationSet::CPKIFPolicyInformationSet(
00131     const bool& criticality,
00132     const CPKIFBufferPtr& ext) 
00133   : CPKIFX509Extension (criticality, ext), m_impl (new CPKIFPolicyInformationSetImpl)
00134 {
00135     CACASNWRAPPER_CREATE(CACX509V3CertificatePolicies, objPDU);
00136     objPDU.Decode(ext->GetBuffer(), ext->GetLength());
00137 
00138     DListNode* cur = objPDU->head;
00139     if(NULL != cur)
00140     {
00141         CPKIFPolicyInformationListPtr piList(new CPKIFPolicyInformationList);
00142         m_impl->m_policyList = piList;
00143     }
00144     while(NULL != cur)
00145     {
00146         //CACASNWRAPPER_CREATE(CACX509V3PolicyInformation, extsWrapper);
00147         //ASN1OpenType *data = extsWrapper.Encode ((CACX509V3PolicyInformation*)cur->data);
00148         //CPKIFBufferPtr tempBuffer(new CPKIFBuffer(data->data, data->numocts));
00149 
00150         ASN1OpenType* openType = (ASN1OpenType*)cur->data;
00151         CPKIFBufferPtr tmpBuffer(new CPKIFBuffer(openType->data, openType->numocts));
00152         CPKIFPolicyInformationPtr pi(new CPKIFPolicyInformation(tmpBuffer));
00153         m_impl->m_policyList->push_back(pi);
00154         cur = cur->next;
00155     }
00156 
00157     m_impl->m_value = ext;
00158     m_impl->m_extModified = false;
00159 }
00167 CPKIFPolicyInformationSet::~CPKIFPolicyInformationSet()
00168 {
00169     if(m_impl)
00170     {
00171         delete m_impl;
00172         m_impl = 0;
00173     }
00174 }
00175 
00184 const CPKIFOIDPtr CPKIFPolicyInformationSet::oid() const
00185 {
00186     //added static variable for copying instead of string creation each call
00187     static CPKIFOID staticOID(extOID);
00188     //CPKIFOIDPtr tmp(new CPKIFOID(new std::string(extOID)));
00189     static CPKIFOIDPtr tmp(new CPKIFOID(staticOID));
00190     return tmp;
00191 }
00192 
00200 CPKIFBufferPtr CPKIFPolicyInformationSet::value() const 
00201 {
00202     if(m_impl->m_policyList && !m_impl->m_policyList->empty() && (m_impl->m_value == (CPKIFBuffer*)NULL || m_impl->m_extModified))
00203     {
00204         CACASNWRAPPER_CREATE(CACX509V3CertificatePolicies, objPDU);
00205         CACX509V3CertificatePolicies policies; 
00206         memset(&policies, 0, sizeof(CACX509V3CertificatePolicies));
00207 
00208         DListNode* cur = NULL;
00209         CPKIFPolicyInformationList::iterator pos;
00210         CPKIFPolicyInformationList::iterator end = m_impl->m_policyList->end();
00211         for(pos = m_impl->m_policyList->begin(); pos != end; ++pos)
00212         {
00213             
00214             if(NULL == cur)
00215             {
00216                 NEW_NODE(cur)
00217             }
00218             else
00219             {
00220                 NEW_NEXT_AND_ADVANCE(cur)
00221             }
00222 
00223             CACASNWRAPPER_CREATE(CACX509V3PolicyInformation, objPolInfo);
00224             CACX509V3PolicyInformation policyInfo;
00225             memset(&policyInfo, 0, sizeof(CACX509V3PolicyInformation));
00226             policyInfo.policyQualifiers.count = 0;
00227             policyInfo.policyQualifiers.head = NULL;
00228             policyInfo.policyQualifiers.tail = NULL;
00229 
00230             CPKIFStringPtr str(new std::string((*pos)->PolicyOID()->ToString())); 
00231             ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
00232             CopyOID(&policyInfo.policyIdentifier, tmpOid);
00233             delete tmpOid;
00234 
00235             CPKIFPolicyQualifierListPtr polQuals = (*pos)->Qualifiers();
00236 
00237             if(polQuals != (CPKIFPolicyQualifierList*)NULL)
00238             {
00239                 policyInfo.m.policyQualifiersPresent = 1;
00240                 DListNode* polQualsCurNode = NULL;
00241                 CPKIFPolicyQualifierList::iterator polQualsPos;
00242                 CPKIFPolicyQualifierList::iterator polQualsEnd = polQuals->end();
00243                 for(polQualsPos = polQuals->begin(); polQualsPos != polQualsEnd; ++polQualsPos)
00244                 {
00245                     CACASNWRAPPER_CREATE(CACX509V3PolicyQualifierInfo, objPDU2);
00246                     CACX509V3PolicyQualifierInfo policyQualifier;
00247                     if(NULL == polQualsCurNode)
00248                     {
00249                         NEW_NODE(polQualsCurNode)
00250                     }
00251                     else
00252                     {
00253                         NEW_NEXT_AND_ADVANCE(polQualsCurNode)
00254                     }
00255 
00256                     CPKIFStringPtr str2(new std::string((*polQualsPos)->QualifierID()->ToString())); 
00257                     ASN1OBJID* tmpOid2 = ConvertStringToASN1OBJID(str2);
00258                     policyQualifier.policyQualifierId = *tmpOid2;
00259 
00260                     policyQualifier.qualifier.numocts = (*polQualsPos)->Qualifier()->GetLength();
00261                     policyQualifier.qualifier.data = new unsigned char[(*polQualsPos)->Qualifier()->GetLength()];
00262                     memcpy((void*)policyQualifier.qualifier.data, (*polQualsPos)->Qualifier()->GetBuffer(), (*polQualsPos)->Qualifier()->GetLength());
00263         
00264                     ASN1OpenType* data1 = objPDU2.Encode(&policyQualifier);
00265 
00266                     if(policyQualifier.qualifier.data)
00267                         delete[] policyQualifier.qualifier.data;
00268 
00269                     polQualsCurNode->data = new ASN1OpenType;
00270                     ((ASN1OpenType*)polQualsCurNode->data)->data = new unsigned char[data1->numocts];
00271                     memcpy((void*)((ASN1OpenType*)polQualsCurNode->data)->data, data1->data, data1->numocts);
00272                     ((ASN1OpenType*)polQualsCurNode->data)->numocts = data1->numocts;
00273 
00274                     SET_HEAD_TAIL_INCREMENT(policyInfo.policyQualifiers, polQualsCurNode);
00275 
00276                     delete data1;
00277                 }
00278             }
00279 
00280             ASN1OpenType* data2 = objPolInfo.Encode(&policyInfo);
00281 
00282             FreeList(&policyInfo.policyQualifiers);
00283 
00284             cur->data = new ASN1OpenType;
00285             ((ASN1OpenType*)cur->data)->data = new unsigned char[data2->numocts];
00286             memcpy((void*)((ASN1OpenType*)cur->data)->data, data2->data, data2->numocts);
00287             ((ASN1OpenType*)cur->data)->numocts = data2->numocts;
00288 
00289             SET_HEAD_TAIL_INCREMENT(policies, cur)
00290 
00291             delete data2;
00292         }
00293 
00294         
00295         ASN1OpenType* extEncoded = objPDU.Encode(&policies);
00296         CPKIFBufferPtr tmp(new CPKIFBuffer(extEncoded->data, extEncoded->numocts));
00297 
00298         m_impl->m_value = tmp;
00299         m_impl->m_extModified = false;
00300 
00301         FreeList(&policies);
00302         delete extEncoded;
00303     }
00304 
00305     return m_impl->m_value;
00306 }
00307 
00315 void FreeOpenType(
00317     ASN1OpenType* asn1ot)
00318 {
00319     if(NULL != asn1ot)
00320     {
00321         if(NULL != asn1ot->data)
00322         {
00323             delete asn1ot->data;
00324         }
00325         delete asn1ot;
00326     }
00327 }
00328 
00336 void FreeList(
00338     DList* asn1rtdlist)
00339 {
00340     if(NULL != asn1rtdlist)
00341     {
00342         if(NULL != asn1rtdlist->head)
00343         {
00344             DListNode* cur = NULL, *tmp = NULL;
00345             cur = asn1rtdlist->head;
00346             while(NULL != cur)
00347             {
00348                 FreeOpenType((ASN1OpenType*)cur->data);
00349                 tmp = cur->next;
00350                 delete cur;
00351                 cur = tmp;
00352             }
00353         }
00354     }
00355 }
00356 
00357 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFPolicyInformationSetPtr & extension)
00358 {
00359     return operator<<(os,*extension);
00360 }
00361 
00362 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFPolicyInformationSet & extension)
00363 {
00364     CPKIFPolicyInformationListPtr polSet = extension.GetPolicySet();
00365     if(!polSet)
00366         return os;
00367 
00368     CPKIFPolicyInformationList::iterator pos = polSet->begin();
00369     CPKIFPolicyInformationList::iterator end = polSet->end();
00370 
00371     for(int count = 1; pos != end; ++count, ++pos)
00372     {
00373         if(count != 1) os << endl;
00374         os << "[" << count << "] Certificate Policy:" << endl;
00375         os << "\tPolicy Identifier=" << (*pos)->PolicyOID()->ToString();
00376 
00377         CPKIFPolicyQualifierListPtr qList = (*pos)->Qualifiers();
00378         if(qList != (CPKIFPolicyQualifierList*)NULL && !qList->empty())
00379         {
00380 
00381             CPKIFPolicyQualifierList::iterator qpos = qList->begin();
00382             CPKIFPolicyQualifierList::iterator qend = qList->end();
00383 
00384             for(int qcount = 1; qpos != qend; qcount++, ++qpos)
00385             {
00386                 os << endl;
00387                 os << "\t[" << qcount << "] Policy Qualifier Info:" << endl;
00388                 CPKIFOIDPtr qualifierOID = (*qpos)->QualifierID();
00389                 CPKIFBufferPtr qualifier = (*qpos)->Qualifier();
00390 
00391                 if(0 == strcmp(qualifierOID->ToString(), "1.3.6.1.5.5.7.2.1"))
00392                 {
00393                     os << "\t\tPolicy Qualifier Id: User Notice" << endl;
00394                     os << "\t\t\tQualifier:" << endl;
00395                     if(qualifier->GetLength() > 257)
00396                     {
00397                         std::string ql((char*)qualifier->GetBuffer(),3,qualifier->GetLength()-3);
00398                         os << "\t\t\t\t" << ql;
00399                     }
00400                     else
00401                     {
00402                         std::string ql((char*)qualifier->GetBuffer(),2,qualifier->GetLength()-2);
00403                         os << "\t\t\t\t" << ql;
00404                     }
00405                 }
00406                 else if(0 == strcmp(qualifierOID->ToString(), "1.3.6.1.5.5.7.2.2"))
00407                 {
00408                     // can we do better than dumping hex-ascii? this is what resources code was doing.
00409                     os << "\t\tPolicy Qualifier Id: CPS" << endl;
00410                     os << "\t\t\tQualifier:" << endl;
00411 
00412                     boost::scoped_array<char> buf(new char[2*qualifier->GetLength()+1]);
00413                     btoa((char*)qualifier->GetBuffer(), buf.get(), qualifier->GetLength());
00414                     os << "\t\t\t\t" << buf.get();
00415                 }
00416                 else 
00417                 {
00418                     os << "\t\tPolicy Qualifier Id: " << qualifierOID->ToString() << endl;
00419                     os << "\t\t\tQualifier:" << endl;
00420 
00421                     boost::scoped_array<char> buf(new char[2*qualifier->GetLength()+1]);
00422                     btoa((char*)qualifier->GetBuffer(), buf.get(), qualifier->GetLength());
00423                     os << "\t\t\t\t" << buf.get();
00424                 }
00425             }
00426 
00427         }
00428     }
00429     return os;
00430 }

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