PKIFCMSAttributeMediator2.cpp

Go to the documentation of this file.
00001 
00009 #include "PKIFCMSAttributeMediator2.h"
00010 #include "ToolkitUtils.h"
00011 #include "PKIFMessageException.h"
00012 #include "ASN1Helper.h"
00013 #include "ContentTypeAttributeFactory.h"
00014 #include "MessageDigestAttributeFactory.h"
00015 #include "SigningTimeAttributeFactory.h"
00016 #include "CountersignatureAttributeFactory.h"
00017 #include "TimestampAttributeFactory.h"
00018 #include "BinarySigningTimeAttributeFactory.h"
00019 #include "SigningCertificateAttributeFactory.h"
00020 #include "OID.h"
00021 #include "Buffer.h"
00022 #include <map>
00023 #include "ScopeGuard.h"
00024 #include "boost/thread/recursive_mutex.hpp"
00025 
00026 #include "CryptographicMessageSyntax2004.h"
00027 #include "ContentCollectionModule.h"
00028 
00029 using namespace std;
00030 
00038 static void CMSFreeAdditionalModules(
00040     std::vector<IPKIFColleague*>& modules, 
00042     IPKIFMediator* mediator)
00043 {
00044     vector<IPKIFColleague*>::iterator pos;
00045     vector<IPKIFColleague*>::iterator end = modules.end();
00046     for(pos = modules.begin(); pos != end; ++pos)
00047     {
00048         try
00049         {
00050             (*pos)->RemoveParent(mediator);
00051             delete (*pos);
00052         }
00053         catch(...)
00054         {
00055 //          _ASSERT(false);
00056         }
00057     }
00058 
00059     modules.clear();//added 04/21/2003 CRW
00060 }
00061 
00062 CPKIFCMSAttributeMediator2 * CPKIFCMSAttributeMediator2::m_hInstance = 0;
00064 struct CPKIFCMSAttributeMediator2Impl
00065 {
00066     enum {thisComponent=TOOLKIT_MESSAGE_ATTR_MEDIATOR};
00067 
00068     //T m_modules;
00069     std::vector<IPKIFColleague*> m_vModules;
00070     std::vector<IPKIFColleague*> m_vModulesToDelete;
00071     boost::recursive_mutex m_me;
00072 };
00074 
00081 CPKIFCMSAttributeMediator2 * CPKIFCMSAttributeMediator2::GetInstance(void)
00082 {
00083     if(m_hInstance) return m_hInstance;
00084     m_hInstance = new CPKIFCMSAttributeMediator2();
00085     m_hInstance->InitializeMediator(0);
00086     m_hInstance->Initialize(0);
00087     return m_hInstance;
00088 }
00089 
00099 CPKIFCMSAttributeMediator2::CPKIFCMSAttributeMediator2(void)
00100     :m_impl (new CPKIFCMSAttributeMediator2Impl)
00101 {
00102     LOG_STRING_DEBUG("CPKIFCMSAttributeMediator2::CPKIFCMSAttributeMediator2", TOOLKIT_CRYPTO_MISC, 0, this);
00103 }
00104 
00113 CPKIFCMSAttributeMediator2::~CPKIFCMSAttributeMediator2(void)
00114 {
00115     LOG_STRING_DEBUG("CPKIFCMSAttributeMediator2::~CPKIFCMSAttributeMediator2", TOOLKIT_CRYPTO_MISC, 0, this);
00116     try
00117     {
00118         //clean up any modules that were added using AddColleague
00119         Terminate();
00120     }
00121     catch(...)
00122     {
00123         _ASSERT(false);
00124     }
00125 
00126     delete m_impl;
00127     m_impl = NULL;
00128 }
00129 
00140 void CPKIFCMSAttributeMediator2::Terminate()
00141 {
00142     m_impl->m_vModules.clear();
00143     CMSFreeAdditionalModules(m_impl->m_vModulesToDelete, this);
00144 }
00145 
00146 #define CMS_ATTRIBUTE_MEDIATOR_ALREADY_INITIALIZED 1
00147 
00160 void CPKIFCMSAttributeMediator2::Initialize(
00163     std::vector<CPKIFException*>* errorInfo)
00164 {
00165     LOG_STRING_DEBUG("CPKIFCMSAttributeMediator2::Initialize", TOOLKIT_CRYPTO_MISC, 0, this);
00166 
00167     if(!m_impl->m_vModules.empty())
00168         throw CPKIFMessageException(TOOLKIT_MESSAGE_ATTR_MEDIATOR, COMMON_ALREADY_INITIALIZED, "This instance has already been initialized.  Call Terminate prior to re-initializing.");
00169 
00170     //populate the m_vModules vector with any modules that were configured at compile-time
00171     //error information will be collected in the errorInfo parameter if desired
00172     /*if(NULL != errorInfo)
00173         PopulateVectorFunc(m_modules, this, m_vModules, *errorInfo);
00174     else
00175     {
00176         std::vector<CPKIFException*> ei;
00177         PopulateVectorFunc(m_modules, this, m_vModules, ei);
00178     }*/
00179 
00180     IPKIFColleague* x1 = new CPKIFContentTypeAttributeFactory();
00181     x1->Initialize();
00182     x1->AddParent(this);
00183     m_impl->m_vModules.push_back(x1);
00184     m_impl->m_vModulesToDelete.push_back(x1);
00185 
00186     IPKIFColleague* x2 = new CPKIFMessageDigestAttributeFactory();
00187     x2->Initialize();
00188     x2->AddParent(this);
00189     m_impl->m_vModules.push_back(x2);
00190     m_impl->m_vModulesToDelete.push_back(x2);
00191 
00192     IPKIFColleague* x3 = new CPKIFSigningTimeAttributeFactory();
00193     x3->Initialize();
00194     x3->AddParent(this);
00195     m_impl->m_vModules.push_back(x3);
00196     m_impl->m_vModulesToDelete.push_back(x3);
00197 
00198     IPKIFColleague* x4 = new CPKIFCountersignatureAttributeFactory();
00199     x4->Initialize();
00200     x4->AddParent(this);
00201     m_impl->m_vModules.push_back(x4);
00202     m_impl->m_vModulesToDelete.push_back(x4);
00203 
00204     IPKIFColleague* x5 = new CPKIFTimestampAttributeFactory();
00205     x5->Initialize();
00206     x5->AddParent(this);
00207     m_impl->m_vModules.push_back(x5);
00208     m_impl->m_vModulesToDelete.push_back(x5);
00209 
00210     IPKIFColleague* x6 = new CPKIFBinarySigningTimeAttributeFactory();
00211     x6->Initialize();
00212     x6->AddParent(this);
00213     m_impl->m_vModules.push_back(x6);
00214     m_impl->m_vModulesToDelete.push_back(x6);
00215 
00216     IPKIFColleague* x7 = new CPKIFSigningCertificateAttributeFactory();
00217     x7->Initialize();
00218     x7->AddParent(this);
00219     m_impl->m_vModules.push_back(x7);
00220     m_impl->m_vModulesToDelete.push_back(x7);
00221 }
00222 
00223 
00235 /*
00236 
00237 CPKIFAttributePtr CPKIFCMSAttributeMediator2::getAttribute(
00239     CACCMSAttribute& refAttribute)
00240 {
00241     LOG_STRING_DEBUG("CPKIFCMSAttributeMediator2::getAttribute", TOOLKIT_CRYPTO_MISC, 0, this);
00242 
00243     CPKIFOID oid(refAttribute.attrType);
00244 
00245     vector<IPKIFColleague*>::iterator pos;
00246     vector<IPKIFColleague*>::iterator end = m_impl->m_vModules.end();
00247     IPKIFCMSAttributeFactory* ext = NULL;
00248     for(pos = m_impl->m_vModules.begin(); pos != end; ++pos)
00249     {
00250         ext = dynamic_cast<IPKIFCMSAttributeFactory*>(*pos);
00251         if(NULL != ext && oid == ext->refOID())
00252         {
00253             try
00254             {
00255                 CPKIFAttributePtr tmp = ext->create(refAttribute); 
00256                 return tmp;
00257             }
00258             catch(...)
00259             {
00260                 //break out and try to create the attribute in a plain empty shell
00261                 //if that fails let the exception get thrown to the caller
00262                 break;
00263             }
00264         }
00265     }
00266 
00267     CPKIFAttributePtr tmpAttrPtr(new CPKIFAttribute(refAttribute));
00268     return tmpAttrPtr;
00269 }
00270 */
00271 
00282 CPKIFAttributePtr CPKIFCMSAttributeMediator2::getAttribute(
00284     CPKIFOIDPtr oid,
00286     CPKIFBufferPtr buf)
00287 {
00288     LOG_STRING_DEBUG("CPKIFCMSAttributeMediator2::getAttribute", TOOLKIT_CRYPTO_MISC, 0, this);
00289 
00290     //CPKIFOID oid(refAttribute.attrType);
00291 
00292     vector<IPKIFColleague*>::iterator pos;
00293     vector<IPKIFColleague*>::iterator end = m_impl->m_vModules.end();
00294     IPKIFCMSAttributeFactory* ext = NULL;
00295     for(pos = m_impl->m_vModules.begin(); pos != end; ++pos)
00296     {
00297         ext = dynamic_cast<IPKIFCMSAttributeFactory*>(*pos);
00298         if(NULL != ext && *oid == ext->refOID()) //oid == ext->refOID())
00299         {
00300             try
00301             {
00302                 CPKIFAttributePtr tmp = ext->create(buf); 
00303                 return tmp;
00304             }
00305             catch(...)
00306             {
00307                 //break out and try to create the attribute in a plain empty shell
00308                 //if that fails let the exception get thrown to the caller
00309                 break;
00310             }
00311         }
00312     }
00313 
00314     CPKIFAttributePtr tmpAttrPtr(new CPKIFAttribute(oid, buf));
00315     return tmpAttrPtr;
00316 }
00317 
00318 
00328 void CPKIFCMSAttributeMediator2::ParseAttributes (
00330      CPKIFBufferPtr& buf, 
00332      std::vector<CPKIFAttributePtr>& attrVector)
00333 {
00334   if (!buf || buf->GetLength() == 0)
00335     return;
00336 
00337   // Signed, unsigned, auth, unauthed, unprotected attributes
00338   // are typedef as DLists of CACCMSAttribute, so we should
00339   // be able to use the following wrapper object for all attribute types.
00340   CACASNWRAPPER_CREATE(CACCMSSignedAttributes, attrWrapper);
00341   CACCMSSignedAttributes *attrList = attrWrapper.Decode(buf->GetBuffer(), buf->GetLength());
00342 
00343   if (attrList->count == 0)
00344     return;
00345   
00346   // Build a colleague look-up table using OIDs as keys
00347   std::map<std::string, IPKIFCMSAttributeFactory*> OIDMap;
00348   std::string *OIDStrings = new std::string[m_impl->m_vModules.size ()];    
00349   int index = 0;
00350 
00351   vector<IPKIFColleague*>::iterator pos;
00352   vector<IPKIFColleague*>::iterator end = m_impl->m_vModules.end();
00353   for(pos = m_impl->m_vModules.begin(); pos != end; ++pos)
00354   {
00355     IPKIFCMSAttributeFactory *attrFactory = dynamic_cast<IPKIFCMSAttributeFactory*>(*pos);
00356     
00357     if (attrFactory)
00358     {
00359       OIDStrings[index] = attrFactory->refOID();
00360       OIDMap[OIDStrings[index]] = attrFactory;
00361       ++index;
00362     }
00363   }
00364  
00365   //iterate over the list of attributes and populate the attributes vector
00366   DListNode* cur = attrList->head;
00367   for(unsigned int i = 0; i < attrList->count; ++i)
00368   {
00369     CACCMSAttribute *attr = static_cast<CACCMSAttribute*>(cur->data);
00370     CPKIFOIDPtr cpkifOIDPtr (new CPKIFOID (attr->attrType.subid, attr->attrType.numids));
00371     std::string OID = cpkifOIDPtr->ToString();
00372     std::map<std::string, IPKIFCMSAttributeFactory*>::iterator itr = OIDMap.find (OID);
00373 
00374     CPKIFBufferPtr anAttrBuf (new CPKIFBuffer (attr->attrValues.data, attr->attrValues.numocts));
00375 
00376     if (itr != OIDMap.end ()) 
00377     {
00378       // Use the colleague to parse the extension
00379       CPKIFAttributePtr anAttrPtr = (*itr).second->create (anAttrBuf);
00380       attrVector.push_back (anAttrPtr);
00381     } 
00382     else 
00383     { 
00384       // instantiate default Attribute
00385       CPKIFAttributePtr anAttrPtr(new CPKIFAttribute(anAttrBuf));
00386       anAttrPtr->SetOID(cpkifOIDPtr);
00387       attrVector.push_back (anAttrPtr); 
00388     }
00389 
00390     cur = cur->next;
00391   }
00392 
00393   delete [] OIDStrings;
00394 }
00404 void CPKIFCMSAttributeMediator2::ParseAttributes2 (
00406      CPKIFBufferPtr& buf, 
00408      std::vector<CPKIFAttributePtr>& attrVector)
00409 {
00410   if (!buf || buf->GetLength() == 0)
00411     return;
00412 
00413   // Signed, unsigned, auth, unauthed, unprotected attributes
00414   // are typedef as DLists of CACCMSAttribute, so we should
00415   // be able to use the following wrapper object for all attribute types.
00416   CACASNWRAPPER_CREATE(ContentWithAttributes_attrs, attrWrapper);
00417   ContentWithAttributes_attrs *attrList = attrWrapper.Decode(buf->GetBuffer(), buf->GetLength());
00418 
00419   if (attrList->count == 0)
00420     return;
00421   
00422   // Build a colleague look-up table using OIDs as keys
00423   std::map<std::string, IPKIFCMSAttributeFactory*> OIDMap;
00424   std::string *OIDStrings = new std::string[m_impl->m_vModules.size ()];    
00425   int index = 0;
00426 
00427   vector<IPKIFColleague*>::iterator pos;
00428   vector<IPKIFColleague*>::iterator end = m_impl->m_vModules.end();
00429   for(pos = m_impl->m_vModules.begin(); pos != end; ++pos)
00430   {
00431     IPKIFCMSAttributeFactory *attrFactory = dynamic_cast<IPKIFCMSAttributeFactory*>(*pos);
00432     
00433     if (attrFactory)
00434     {
00435       OIDStrings[index] = attrFactory->refOID();
00436       OIDMap[OIDStrings[index]] = attrFactory;
00437       ++index;
00438     }
00439   }
00440  
00441   //iterate over the list of attributes and populate the attributes vector
00442   DListNode* cur = attrList->head;
00443   for(unsigned int i = 0; i < attrList->count; ++i)
00444   {
00445     CACCMSAttribute *attr = static_cast<CACCMSAttribute*>(cur->data);
00446     CPKIFOIDPtr cpkifOIDPtr (new CPKIFOID (attr->attrType.subid, attr->attrType.numids));
00447     std::string OID = cpkifOIDPtr->ToString();
00448     std::map<std::string, IPKIFCMSAttributeFactory*>::iterator itr = OIDMap.find (OID);
00449 
00450     CPKIFBufferPtr anAttrBuf (new CPKIFBuffer (attr->attrValues.data, attr->attrValues.numocts));
00451 
00452     if (itr != OIDMap.end ()) 
00453     {
00454       // Use the colleague to parse the extension
00455       CPKIFAttributePtr anAttrPtr = (*itr).second->create (anAttrBuf);
00456       attrVector.push_back (anAttrPtr);
00457     } 
00458     else 
00459     { 
00460       // instantiate default Attribute
00461       CPKIFAttributePtr anAttrPtr(new CPKIFAttribute(anAttrBuf));
00462       anAttrPtr->SetOID(cpkifOIDPtr);
00463       attrVector.push_back (anAttrPtr); 
00464     }
00465 
00466     cur = cur->next;
00467   }
00468 
00469   delete [] OIDStrings;
00470 }
00478 void CPKIFCMSAttributeMediator2::AddColleague(
00480     IPKIFColleague* module,
00482     bool transferOwnership)
00483 {
00484     LOG_STRING_DEBUG("CPKIFX509ExtensionMediator2::AddColleague(IPKIFColleague* module, bool transferOwnership)", TOOLKIT_PATH_MEDIATOR, 0, this);
00485 
00486     //CCACSynchronizedObject so(m_impl->m_md);
00487     boost::recursive_mutex::scoped_lock(m_impl->m_me);
00488     if(NULL == module)
00489         return;
00490 
00491     //if the module throws an exception let the caller catch it
00492     module->Initialize();
00493     module->AddParent(this);//added 3/12/2003 CRW
00494 
00495     try
00496     {
00497         //set upa n array of pointers that we will need to delete
00498         if(transferOwnership)
00499             m_impl->m_vModulesToDelete.push_back(module);
00500 
00501         //create a guard on the vector so if the push onto the primary vector fails we can
00502         //pop the one off of m_impl->m_vModulesToDelete
00503         ScopeGuard guard = MakeObjGuard(m_impl->m_vModulesToDelete, &vector<IPKIFColleague*>::pop_back);
00504         m_impl->m_vModules.push_back(module);
00505         guard.Dismiss();
00506     }
00507     catch(...)
00508     {
00509         throw;
00510     }
00511 }

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