00001
00010 #include "CPKIFPathProcessingMediator2.h"
00011
00012 #include "boost/thread/recursive_mutex.hpp"
00013
00014 #include "ToolkitUtils.h"
00015
00016 #include "PKIFException.h"
00017 #include "PKIFPathException.h"
00018 #include "PKIFPathLogger.h"
00019 #include "PKIFErrors.h"
00020
00021 #include "PKIFPathValidator2.h"
00022 #include "PKIFPathBuilder2.h"
00023 #include "PKIFPathBuildAndValidate.h"
00024 #include "ScopeGuard.h"
00025 #include "PKIFLog.h"
00026 #include "PKIFPathSettings.h"
00027 #include "PKIFCertificatePath.h"
00028 #include <vector>
00029 using namespace std;
00030 using namespace boost;
00032 struct CPKIFPathProcessingMediator2Impl {
00033
00041 CPKIFPathProcessingMediator2Impl ():m_me()
00042 {
00043 };
00044
00045
00046 boost::recursive_mutex m_me;
00047
00048 CPKIFPathSettingsPtr m_defPathSettings;
00049
00050 std::vector<IPKIFColleaguePtr> m_vModules;
00051
00052 std::vector<IPKIFPathBuildPtr> m_vPathBuildModules;
00053 std::vector<IPKIFPathValidatePtr> m_vPathValidateModules;
00054 std::vector<IPKIFPathBuildAndValidatePtr> m_vPathBuildandValidateModules;
00055
00056 bool m_addDefaultColleagues;
00057 };
00059
00071 CPKIFPathProcessingMediator2::CPKIFPathProcessingMediator2(
00073 bool addDefaultColleagues)
00074 : m_impl (new CPKIFPathProcessingMediator2Impl)
00075 {
00076 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::CPKIFPathProcessingMediator2(void)", TOOLKIT_PATH_MEDIATOR, 0, this);
00077 m_impl->m_addDefaultColleagues = addDefaultColleagues;
00078 }
00086 CPKIFPathProcessingMediator2::~CPKIFPathProcessingMediator2(void)
00087 {
00088 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::~CPKIFPathProcessingMediator2(void)", TOOLKIT_PATH_MEDIATOR, 0, this);
00089
00090 Terminate();
00091
00092 delete m_impl;
00093 m_impl = '\0';
00094 }
00095
00105 void CPKIFPathProcessingMediator2::Terminate()
00106 {
00107 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::Terminate()", TOOLKIT_PATH_MEDIATOR, 0, this);
00108
00109
00110 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00111 try
00112 {
00113
00114 RemoveParentRelationships(m_impl->m_vModules, this);
00115
00116 RemoveMediatorAssociations();
00117
00118
00119
00120
00121
00122 IPKIFMediator::Terminate();
00123
00124 m_impl->m_vModules.clear();
00125 m_impl->m_vPathBuildandValidateModules.clear();
00126 m_impl->m_vPathBuildModules.clear();
00127 m_impl->m_vPathValidateModules.clear();
00128 }
00129 catch(CPKIFException& )
00130 {
00131
00132
00133 LOG_STRING_ERROR("CPKIFException encountered during CPKIFPathProcessingMediator2 mediator termination", thisComponent, COMMON_TERMINATION_ERROR, this);
00134
00135 _ASSERT(false);
00136 }
00137 catch(...)
00138 {
00139 LOG_STRING_ERROR("Unknown exception encountered during CPKIFPathProcessingMediator2 mediator termination", thisComponent, COMMON_TERMINATION_ERROR, this);
00140 _ASSERT(false);
00141 }
00142 }
00143
00155 void CPKIFPathProcessingMediator2::Initialize()
00156 {
00157 InitializeMediator(NULL);
00158 }
00159
00174 void CPKIFPathProcessingMediator2::InitializeMediator(
00176 std::vector<CPKIFException*>* errorInfo)
00177 {
00178 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::InitializeMediator(std::vector<CPKIFException*>* errorInfo)", TOOLKIT_PATH_MEDIATOR, 0, this);
00179
00180
00181 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00182
00183 LOG_STRING_DEBUG("Initializing CPKIFPathProcessingMediator2 mediator", thisComponent, 0, this);
00184
00185 if(!m_impl->m_vModules.empty())
00186 throw CPKIFPathException(thisComponent, COMMON_ALREADY_INITIALIZED, "This instance has already been initialized. Call Terminate prior to re-initializing.");
00187
00188 if(m_impl->m_addDefaultColleagues)
00189 {
00190 CPKIFPathValidator2Ptr x1(new CPKIFPathValidator2);
00191 CPKIFPathBuilder2Ptr x2(new CPKIFPathBuilder2);
00192 CPKIFPathBuildAndValidatePtr x3(new CPKIFPathBuildAndValidate);
00193
00194
00195 IPKIFColleaguePtr cp = dynamic_pointer_cast<IPKIFColleague,CPKIFPathValidator2>(x1);
00196 AddColleague(cp);
00197 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFPathBuilder2>(x2);
00198 AddColleague(cp);
00199 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFPathBuildAndValidate>(x3);
00200 AddColleague(cp);
00201 }
00202 }
00203
00216 void CPKIFPathProcessingMediator2::AddColleague(
00218 IPKIFColleaguePtr& module)
00219 {
00220 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::AddColleague(IPKIFColleague* module, bool transferOwnership)", TOOLKIT_PATH_MEDIATOR, 0, this);
00221
00222
00223 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00224 if(!module)
00225 return;
00226
00227
00228 module->Initialize();
00229 module->AddParent(this);
00230
00231 try
00232 {
00233
00234
00235
00236
00237
00238
00239
00240 m_impl->m_vModules.push_back(module);
00241 if(dynamic_pointer_cast<IPKIFPathBuild, IPKIFColleague>(module))
00242 m_impl->m_vPathBuildModules.push_back(dynamic_pointer_cast<IPKIFPathBuild, IPKIFColleague>(module));
00243 if(dynamic_pointer_cast<IPKIFPathValidate, IPKIFColleague>(module))
00244 m_impl->m_vPathValidateModules.push_back(dynamic_pointer_cast<IPKIFPathValidate, IPKIFColleague>(module));
00245 if(dynamic_pointer_cast<IPKIFPathBuildAndValidate, IPKIFColleague>(module))
00246 m_impl->m_vPathBuildandValidateModules.push_back(dynamic_pointer_cast<IPKIFPathBuildAndValidate, IPKIFColleague>(module));
00247
00248 }
00249 catch(...)
00250 {
00251 throw;
00252 }
00253 }
00254
00265 void CPKIFPathProcessingMediator2::SetDefaultPathSettings(
00268 CPKIFPathSettingsPtr& pathSettings)
00269 {
00270 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::SetDefaultPathSettings(CPKIFPathSettingsPtr& pathSettings)", TOOLKIT_PATH_MEDIATOR, 0, this);
00271
00272
00273 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00274 m_impl->m_defPathSettings = pathSettings;
00275 }
00276
00286 CPKIFPathSettingsPtr CPKIFPathProcessingMediator2::GetDefaultPathSettings()
00287 {
00288 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::GetDefaultPathSettings", TOOLKIT_PATH_MEDIATOR, 0, this);
00289
00290
00291 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00292
00293 if(m_impl->m_defPathSettings != (CPKIFPathSettings*)NULL)
00294 {
00295 CPKIFPathSettingsPtr ps(new CPKIFPathSettings(*m_impl->m_defPathSettings));
00296 return ps;
00297 }
00298 else
00299 {
00300
00301 CPKIFPathSettingsPtr tmp(new CPKIFPathSettings);
00302 return tmp;
00303 }
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00353 bool CPKIFPathProcessingMediator2::ValidatePath(
00355 CPKIFCertificatePath& path,
00357 CPKIFPathValidationResults& results,
00359 CPKIFFuncStoragePtr& thisCallOnlyFuncs)
00360 {
00361 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::ValidatePath", TOOLKIT_PATH_MEDIATOR, 0, this);
00362
00363
00364 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00365
00366
00367
00368 CPKIFPathSettingsPtr settings;
00369 path.GetPathSettings(settings);
00370 if(settings == (CPKIFPathSettings*)NULL)
00371 path.SetPathSettings(GetDefaultPathSettings());
00372
00373 bool retVal = false;
00374 vector<IPKIFPathValidatePtr>::iterator pos;
00375 vector<IPKIFPathValidatePtr>::iterator end = m_impl->m_vPathValidateModules.end();
00376 bool opAttempted = false;
00377 for(pos = m_impl->m_vPathValidateModules.begin(); pos != end; ++pos)
00378 {
00379 try
00380 {
00381 opAttempted = true;
00382 retVal = (*pos)->ValidatePath(path, results, thisCallOnlyFuncs);
00383 CPKIFPathLogger::LogValidationResults(results, path, "Logging path following completion of validation operation");
00384 AuditPathEvent(PO_VALIDATE, retVal, path);
00385
00386
00387
00388 return retVal;
00389 }
00390 catch(CPKIFException& e)
00391 {
00392 CPKIFPathLogger::LogPath(path, "Logging path following unexpected exception during validation operation");
00393
00394 std::string reason = "A path processing-related exception was thrown. ";
00395 reason.append(*e.print());
00396 AuditString(EVENTLOG_WARNING_TYPE, CAT_PKIF_PATH, PKIF_UNEXPECTED_EXCEPTION, reason.c_str(), thisComponent, COMMON_UNKNOWN_ERROR, this);
00397
00398 }
00399 }
00400
00401 if(!opAttempted)
00402 throw CPKIFPathException(thisComponent, COMMON_OPERATION_NOT_HANDLED);
00403
00404 return retVal;
00405 }
00406
00427 void CPKIFPathProcessingMediator2::SetAdditionalCertificateChecks(
00429 CPKIFFuncStoragePtr& funcs)
00430 {
00431 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::SetAdditionalCertificateChecks", TOOLKIT_PATH_MEDIATOR, 0, this);
00432
00433
00434 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00435 bool retVal = false;
00436 vector<IPKIFPathValidatePtr>::iterator pos;
00437 vector<IPKIFPathValidatePtr>::iterator end = m_impl->m_vPathValidateModules.end();
00438 bool opAttempted = false, opSucceeded = false;
00439 for(pos = m_impl->m_vPathValidateModules.begin(); pos != end; ++pos)
00440 {
00441 try
00442 {
00443 opAttempted = true;
00444 (*pos)->SetAdditionalCertificateChecks(funcs);
00445 opSucceeded = true;
00446 }
00447 catch(CPKIFException& e)
00448 {
00449 std::string reason = "A path processing-related exception was thrown. ";
00450 reason.append(*e.print());
00451 AuditString(EVENTLOG_WARNING_TYPE, CAT_PKIF_PATH, PKIF_UNEXPECTED_EXCEPTION, reason.c_str(), thisComponent, COMMON_UNKNOWN_ERROR, this);
00452
00453 }
00454 }
00455
00456 if(!opAttempted)
00457 throw CPKIFPathException(thisComponent, COMMON_OPERATION_NOT_HANDLED);
00458
00459 if(!opSucceeded)
00460 throw CPKIFPathException(thisComponent, COMMON_OPERATION_NOT_SUCCESSFUL);
00461 }
00462
00503 bool CPKIFPathProcessingMediator2::BuildPath(
00506 CPKIFCertificatePath& path)
00507 {
00508 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::BuildPath", TOOLKIT_PATH_MEDIATOR, 0, this);
00509
00510
00511 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00512
00513
00514
00515 CPKIFPathSettingsPtr settings;
00516 path.GetPathSettings(settings);
00517 if(settings == (CPKIFPathSettings*)NULL)
00518 path.SetPathSettings(GetDefaultPathSettings());
00519
00520 bool retVal = false;
00521 vector<IPKIFPathBuildPtr>::iterator pos;
00522 vector<IPKIFPathBuildPtr>::iterator end = m_impl->m_vPathBuildModules.end();
00523 bool opAttempted = false, opSucceeded = false;
00524 for(pos = m_impl->m_vPathBuildModules.begin(); pos != end; ++pos)
00525 {
00526 try
00527 {
00528 opAttempted = true;
00529 retVal = (*pos)->BuildPath(path);
00530 opSucceeded = true;
00531 AuditPathEvent(PO_BUILD, retVal, path);
00532
00533
00534 if(retVal)
00535 return retVal;
00536 }
00537 catch(CPKIFException& e)
00538 {
00539 std::string reason = "A path processing-related exception was thrown. ";
00540 reason.append(*e.print());
00541 AuditString(EVENTLOG_WARNING_TYPE, CAT_PKIF_PATH, PKIF_UNEXPECTED_EXCEPTION, reason.c_str(), thisComponent, COMMON_UNKNOWN_ERROR, this);
00542
00543 }
00544 }
00545
00546 if(!opAttempted)
00547 throw CPKIFPathException(thisComponent, COMMON_OPERATION_NOT_HANDLED);
00548
00549 if(!opSucceeded)
00550 throw CPKIFPathException(thisComponent, COMMON_OPERATION_NOT_SUCCESSFUL);
00551
00552 return retVal;
00553 }
00554
00582 bool CPKIFPathProcessingMediator2::BuildAndValidatePath(
00587 CPKIFCertificatePath& path,
00594 CPKIFPathValidationResults& results)
00595 {
00596 LOG_STRING_DEBUG("CPKIFPathProcessingMediator2::BuildAndValidatePath", TOOLKIT_PATH_MEDIATOR, 0, this);
00597
00598
00599 boost::recursive_mutex::scoped_lock(m_impl->m_me);
00600
00601
00602
00603 CPKIFPathSettingsPtr settings;
00604 path.GetPathSettings(settings);
00605 if(settings == (CPKIFPathSettings*)NULL)
00606 path.SetPathSettings(GetDefaultPathSettings());
00607
00608 bool retVal = false;
00609 vector<IPKIFPathBuildAndValidatePtr>::iterator pos;
00610 vector<IPKIFPathBuildAndValidatePtr>::iterator end = m_impl->m_vPathBuildandValidateModules.end();
00611 bool opAttempted = false;
00612 for(pos = m_impl->m_vPathBuildandValidateModules.begin(); pos != end; ++pos)
00613 {
00614 try
00615 {
00616 opAttempted = true;
00617 if((*pos)->BuildAndValidatePath(path, results))
00618 {
00619 retVal = true;
00620 break;
00621 }
00622 }
00623 catch(CPKIFException& e)
00624 {
00625 CPKIFPathLogger::LogPath(path, "Logging path following unexpected exception during build and validation operation");
00626
00627 std::string reason = "A path processing-related exception was thrown. ";
00628 reason.append(*e.print());
00629 AuditString(EVENTLOG_WARNING_TYPE, CAT_PKIF_PATH, PKIF_UNEXPECTED_EXCEPTION, reason.c_str(), thisComponent, COMMON_UNKNOWN_ERROR, this);
00630
00631 }
00632
00633 }
00634
00635 if(!opAttempted)
00636 throw CPKIFPathException(thisComponent, COMMON_OPERATION_NOT_HANDLED);
00637
00638 return retVal;
00639 }
00647 void CPKIFPathProcessingMediator2::GetColleagues(
00649 std::vector<IPKIFColleaguePtr>& v) const
00650 {
00651 copy(m_impl->m_vModules.begin(),m_impl->m_vModules.end(), back_inserter(v));
00652 }