00001
00010 #include "PKIFSCVP.h"
00011
00012 #include "ASN1Helper.h"
00013 #include "ToolkitUtils.h"
00014 #include "SCVP.h"
00015 #include "Buffer.h"
00016 #include "AlgorithmIdentifier.h"
00017 #include "PKIFMediators.h"
00018 #include "IPKIFHashContext.h"
00019 #include "IPKIFCryptoMisc.h"
00020 #include "PKIFCryptUtils.h"
00021 #include "PKIFTime.h"
00022 #include "Query.h"
00023 #include "PKIFX509Extensions2.h"
00024 #include "ResponseFlags.h"
00025 #include "Certificate.h"
00026 #include "CertReferences.h"
00027
00029
00030 struct CPKIFQueryImpl
00031 {
00032
00033 void populateExtensionsVector();
00034
00035 CPKIFTimePtr m_validationTime;
00036 CPKIFTimePtr m_producedAt;
00037 CPKIFCertificateListPtr m_intermidiateCerts;
00038 CPKIFBufferPtr m_hashedMessage;
00039 CPKIFResponseFlagsPtr m_responseFlags;
00040 CPKIFValidationPolicyPtr m_validationPolicy;
00041 CPKIFOIDListPtr m_checks;
00042 CPKIFOIDListPtr m_wantBack;
00043 CPKIFCertReferencesPtr m_certRefs;
00044 CPKIFBufferPtr m_serverContextInfo;
00045 CPKIFRevocationInfoListPtr m_revInfos;
00046
00047
00048 std::vector<CPKIFX509ExtensionPtr> m_exts;
00049
00050
00051 CPKIFASNWrapper<Query>* m_query;
00052
00053 void ClearAllMembers();
00054 void MakeSCVPQuery();
00055 void FreeSCVPQuery();
00056
00057
00058 void CallingAllGets();
00059
00060 CPKIFQuery * m_this;
00061 };
00062
00063
00071 void CPKIFQueryImpl::CallingAllGets()
00072 {
00073 LOG_STRING_DEBUG("CPKIFCVRequestImpl::CallingAllGets()", TOOLKIT_SCVP_ASN, 0, this);
00074
00075 populateExtensionsVector();
00076 FreeSCVPQuery();
00077 }
00078
00086 void CPKIFQueryImpl::MakeSCVPQuery()
00087 {
00088 LOG_STRING_DEBUG("CPKIFCVRequest::MakeSCVPR()", TOOLKIT_SCVP_ASN, 0, this);
00089
00090 FreeSCVPQuery();
00091 m_query = new CPKIFASNWrapper<Query>( BEREncQuery, BERDecQuery);
00092 }
00100 void CPKIFQueryImpl::FreeSCVPQuery()
00101 {
00102 LOG_STRING_DEBUG("CPKIFQueryImpl::MakeSCVPR()", TOOLKIT_SCVP_ASN, 0, this);
00103 if(NULL != m_query)
00104 delete m_query;
00105 m_query = NULL;
00106 }
00107
00116 void CPKIFQueryImpl::ClearAllMembers()
00117 {
00118 LOG_STRING_DEBUG("CPKIFQueryImpl::ClearAllMembers()", TOOLKIT_SCVP_ASN, 0, this);
00119
00120 FreeSCVPQuery();
00121 }
00122
00133 void CPKIFQueryImpl::populateExtensionsVector()
00134 {
00135 LOG_STRING_DEBUG("CPKIFQueryImpl::populateExtensionsVector()", TOOLKIT_SCVP_ASN, 0, this);
00136
00137 if(NULL == m_query || NULL == m_query->data())
00138 return;
00139
00140
00141 if(!m_exts.empty())
00142 return;
00143
00144
00145 if(0 == (*m_query)->m.queryExtensionsPresent)
00146 {
00147 m_exts.clear();
00148 return;
00149 }
00150
00151
00152
00153 CPKIFX509ExtensionMediator2 * mediator = CPKIFX509ExtensionMediator2::GetInstance();
00154
00155 m_this->IPKIFHasExtensions::GetExtensions (mediator, m_exts);
00156 }
00157
00159
00160
00168 CPKIFQuery::CPKIFQuery() :m_impl(new CPKIFQueryImpl)
00169 {
00170 LOG_STRING_DEBUG("CPKIFQuery::CPKIFQuery()", TOOLKIT_SCVP_ASN, 0, this);
00171
00172 m_impl->m_query = NULL;
00173
00174 CPKIFValidationPolicyPtr tmpVP(new CPKIFValidationPolicy);
00175 m_impl->m_validationPolicy = tmpVP;
00176
00177 CPKIFValidationPolRefPtr vpr(new CPKIFValidationPolRef(g_id_svp_defaultValPolicy));
00178 tmpVP->SetValidationPolRef(vpr);
00179 }
00180
00191 CPKIFQuery::CPKIFQuery(
00193 const CPKIFBufferPtr& name)
00194 :m_impl (new CPKIFQueryImpl)
00195 {
00196
00197
00198 if(name == (CPKIFBuffer*)NULL || 0 == name->GetLength())
00199 {
00200 throw CPKIFSCVPException(TOOLKIT_SCVP_ASN, COMMON_INVALID_INPUT);
00201 }
00202 m_impl->m_query = NULL;
00203 m_impl->MakeSCVPQuery();
00204 (*m_impl->m_query).Decode(name->GetBuffer(), name->GetLength());
00205
00206
00207
00208 CACASNWRAPPER_CREATE(CertReferences, objPDU);
00209 ASN1OpenType* data1 = objPDU.Encode(&(*m_impl->m_query)->queriedCerts);
00210 CPKIFBufferPtr tmpBuf1(new CPKIFBuffer(data1->data, data1->numocts));
00211 CPKIFCertReferencesPtr tmpCertRefs(new CPKIFCertReferences(tmpBuf1));
00212 if(NULL != data1)
00213 delete data1;
00214 m_impl->m_certRefs = tmpCertRefs;
00215
00216
00217
00218 CACASNWRAPPER_CREATE(ValidationPolicy, objPDU2);
00219 ASN1OpenType* data2 = objPDU2.Encode(&(*m_impl->m_query)->validationPolicy);
00220 CPKIFBufferPtr tmpBuf2(new CPKIFBuffer(data2->data, data2->numocts));
00221 CPKIFValidationPolicyPtr tmpValPol(new CPKIFValidationPolicy(tmpBuf2));
00222 if(NULL != data2)
00223 delete data2;
00224
00225 m_impl->m_validationPolicy = tmpValPol;
00226
00227
00228
00229 if(1 == (m_impl->m_query->data()->m.responseFlagsPresent)) {
00230 CACASNWRAPPER_CREATE(ResponseFlags, objPDU3);
00231 ASN1OpenType* data3 = objPDU3.Encode(&(*m_impl->m_query)->responseFlags);
00232 CPKIFBufferPtr tmpBuf3(new CPKIFBuffer(data3->data, data3->numocts));
00233 CPKIFResponseFlagsPtr tmpResponseFlags(new CPKIFResponseFlags(tmpBuf3));
00234 if(NULL != data3)
00235 delete data3;
00236
00237 m_impl->m_responseFlags = tmpResponseFlags;
00238 } else {
00239 m_impl->m_responseFlags = CPKIFResponseFlagsPtr(new CPKIFResponseFlags());
00240 }
00241
00242
00243
00244 CPKIFOIDListPtr tmpChecks(new CPKIFOIDList());
00245 DListNode* cur = (*m_impl->m_query)->checks.head;
00246 while(NULL != cur)
00247 {
00248 ASN1OBJID* tmp = (ASN1OBJID*)cur->data;
00249 CPKIFOIDPtr tmpOID(new CPKIFOID(tmp->subid, tmp->numids));
00250
00251 tmpChecks->push_back(tmpOID);
00252
00253 cur = cur->next;
00254 }
00255 m_impl->m_checks = tmpChecks;
00256
00257 if(0 < (*m_impl->m_query)->m.wantBackPresent)
00258 {
00259 CPKIFOIDListPtr tmpWantBack(new CPKIFOIDList());
00260 DListNode* cur = (*m_impl->m_query)->wantBack.head;
00261 while(NULL != cur)
00262 {
00263 ASN1OBJID* tmp = (ASN1OBJID*)cur->data;
00264 CPKIFOIDPtr tmpOID(new CPKIFOID(tmp->subid, tmp->numids));
00265
00266 tmpWantBack->push_back(tmpOID);
00267
00268 cur = cur->next;
00269 }
00270 m_impl->m_wantBack = tmpWantBack;
00271 }
00272
00273 if(0 < (*m_impl->m_query)->m.serverContextInfoPresent)
00274 {
00275 CPKIFBufferPtr tmpBuf(new CPKIFBuffer((*m_impl->m_query)->serverContextInfo.data, (*m_impl->m_query)->serverContextInfo.numocts));
00276
00277 m_impl->m_serverContextInfo = tmpBuf;
00278 }
00279 if(0 < (*m_impl->m_query)->m.validationTimePresent)
00280 {
00281 CPKIFTimePtr tmpTime(new CPKIFTime((*m_impl->m_query)->validationTime));
00282
00283 m_impl->m_validationTime = tmpTime;
00284 }
00285
00286 if(0 < (*m_impl->m_query)->m.producedAtPresent)
00287 {
00288 CPKIFTimePtr tmpTime(new CPKIFTime((*m_impl->m_query)->producedAt));
00289
00290 m_impl->m_producedAt = tmpTime;
00291 }
00292
00293 if(0 < (*m_impl->m_query)->m.intermediateCertsPresent)
00294 {
00295 CPKIFCertificateListPtr certList(new CPKIFCertificateList());
00296 DListNode* cur = (*m_impl->m_query)->intermediateCerts.head;
00297 while(NULL != cur)
00298 {
00299 ASN1OpenType* tmp = (ASN1OpenType*)cur->data;
00300 CPKIFCertificatePtr tmpCert(new CPKIFCertificate());
00301 tmpCert->Decode(tmp->data, tmp->numocts);
00302
00303 certList->push_back(tmpCert);
00304
00305 cur = cur->next;
00306 }
00307 m_impl->m_intermidiateCerts = certList;
00308 }
00309
00310
00311 if(false )
00312 {
00313 CPKIFRevocationInfoListPtr revInfoList(new CPKIFRevocationInfoList());
00314 DListNode* cur = (*m_impl->m_query)->revInfos.head;
00315 while(NULL != cur)
00316 {
00317 RevocationInfo* tmp = (RevocationInfo*)cur->data;
00318 CACASNWRAPPER_CREATE(RevocationInfo, objPDU);
00319 ASN1OpenType* data1 = objPDU.Encode(tmp);
00320 CPKIFBufferPtr tmpBuf(new CPKIFBuffer(data1->data, data1->numocts));
00321 CPKIFRevocationInfoPtr tmpRevInfo(new CPKIFRevocationInfo(tmpBuf));
00322
00323 if(NULL != data1)
00324 delete data1;
00325
00326
00327 revInfoList->push_back(tmpRevInfo);
00328
00329 cur = cur->next;
00330 }
00331 m_impl->m_revInfos = revInfoList;
00332 }
00333
00334 m_impl->populateExtensionsVector();
00335 }
00336
00344 CPKIFQuery::~CPKIFQuery()
00345 {
00346 LOG_STRING_DEBUG("CPKIFQuery::~CPKIFQuery()", TOOLKIT_SCVP_ASN, 0, this);
00347
00348 m_impl->FreeSCVPQuery();
00349
00350 if (m_impl) {
00351 delete m_impl;
00352 }
00353 }
00354
00362 void CPKIFQuery::GetEncodedExtensions (
00364 CPKIFBufferPtr& buf)
00365 {
00366 try
00367 {
00368 if ((*m_impl->m_query)->m.queryExtensionsPresent)
00369 {
00370 CACASNWRAPPER_CREATE(CACX509V3Extensions, extsWrapper);
00371 ASN1OpenType *data = extsWrapper.Encode (&(*m_impl->m_query)->queryExtensions);
00372 CPKIFBufferPtr tmp(new CPKIFBuffer(data->data, data->numocts));
00373 buf = tmp;
00374 delete data;
00375 return;
00376 }
00377 }
00378 catch (... )
00379 {
00380
00381 }
00382
00383 CPKIFBufferPtr nullExt;
00384 buf = nullExt;
00385 }
00386
00387
00396 void CPKIFQuery::GetExtensionByOID(
00398 const CPKIFOID& oid,
00400 CPKIFX509ExtensionPtr& ref)
00401 {
00402 if(m_impl->m_exts.empty() && 0 != (*m_impl->m_query)->m.queryExtensionsPresent)
00403 m_impl->populateExtensionsVector();
00404
00405 std::vector<CPKIFX509ExtensionPtr>::iterator pos;
00406 std::vector<CPKIFX509ExtensionPtr>::iterator end = m_impl->m_exts.end();
00407 for(pos = m_impl->m_exts.begin(); pos != end; ++pos)
00408 {
00409 if(oid == (*pos)->oid())
00410 {
00411 ref = *pos;
00412 return;
00413 }
00414 }
00415 }
00416
00425 bool CPKIFQuery::ExtensionsPresent() const
00426 {
00427 LOG_STRING_DEBUG("CPKIFQuery::ExtensionsPresent()", TOOLKIT_SCVP_ASN, 0, this);
00428
00429 if(NULL != m_impl->m_query && (*m_impl->m_query)->m.queryExtensionsPresent)
00430 return true;
00431 else
00432 return false;
00433 }
00434
00442 void CPKIFQuery::SetValidationTime(
00444 CPKIFTimePtr& validationTime)
00445 {
00446 m_impl->m_validationTime = validationTime;
00447 }
00455 const CPKIFTimePtr CPKIFQuery::GetValidationTime() const
00456 {
00457 return m_impl->m_validationTime;
00458 }
00466 void CPKIFQuery::SetProducedAt(
00468 CPKIFTimePtr& producedAt)
00469 {
00470 m_impl->m_producedAt = producedAt;
00471 }
00479 const CPKIFTimePtr CPKIFQuery::GetProducedAt() const
00480 {
00481 return m_impl->m_producedAt;
00482 }
00490 void CPKIFQuery::SetCertBundle(
00493 CPKIFCertificateListPtr & intermidiateCerts)
00494 {
00495 m_impl->m_intermidiateCerts = intermidiateCerts;
00496 }
00504 void CPKIFQuery::GetCertBundle(
00507 CPKIFCertificateListPtr& intermidiateCerts) const
00508 {
00509 intermidiateCerts = m_impl->m_intermidiateCerts;
00510 }
00511
00519 void CPKIFQuery::AddToCertBundle(CPKIFCertificatePtr& cert)
00520 {
00521 m_impl->m_intermidiateCerts->push_back(cert);
00522 }
00530 void CPKIFQuery::SetResponseFlags(
00532 CPKIFResponseFlagsPtr& responseFlags)
00533 {
00534 m_impl->m_responseFlags = responseFlags;
00535 }
00543 const CPKIFResponseFlagsPtr CPKIFQuery::GetResponseFlags() const
00544 {
00545 return m_impl->m_responseFlags;
00546 }
00554 void CPKIFQuery::SetValidationPolicy(
00556 CPKIFValidationPolicyPtr& validationPolicy)
00557 {
00558 m_impl->m_validationPolicy = validationPolicy;
00559 }
00567 const CPKIFValidationPolicyPtr CPKIFQuery::GetValidationPolicy() const
00568 {
00569 return m_impl->m_validationPolicy;
00570 }
00571
00580 void CPKIFQuery::SetChecks(
00582 CPKIFOIDListPtr& checks)
00583 {
00584 m_impl->m_checks = checks;
00585 }
00586
00594 void CPKIFQuery::GetChecks(
00597 CPKIFOIDListPtr& checks) const
00598 {
00599 checks = m_impl->m_checks;
00600 }
00601
00611 void CPKIFQuery::SetWantBack(
00613 CPKIFOIDListPtr& wantBack)
00614 {
00615 m_impl->m_wantBack = wantBack;
00616 }
00617
00627 void CPKIFQuery::GetWantBack(
00629 CPKIFOIDListPtr& wantBack) const
00630 {
00631 wantBack = m_impl->m_wantBack;
00632 }
00633
00642 void CPKIFQuery::SetCertRef(
00644 CPKIFCertReferencesPtr& certRefs)
00645 {
00646 m_impl->m_certRefs = certRefs;
00647 }
00655 void CPKIFQuery::GetCertRef(
00657 CPKIFCertReferencesPtr& certRefs) const
00658 {
00659 certRefs = m_impl->m_certRefs;
00660 }
00661
00669 void CPKIFQuery::SetServerContextInfo(
00671 CPKIFBufferPtr& serverContextInfo)
00672 {
00673 m_impl->m_serverContextInfo = serverContextInfo;
00674 }
00682 const CPKIFBufferPtr CPKIFQuery::GetServerContextInfo() const
00683 {
00684 return m_impl->m_serverContextInfo;
00685 }
00686
00696 void CPKIFQuery::SetRevInfos(
00698 CPKIFRevocationInfoListPtr& revInfos)
00699 {
00700 m_impl->m_revInfos = revInfos;
00701 }
00711 void CPKIFQuery::GetRevInfos(
00713 CPKIFRevocationInfoListPtr& revInfos) const
00714 {
00715 revInfos = m_impl->m_revInfos;
00716 }
00717
00725 bool CPKIFQuery::AreThereAnyUnprocessedCriticalExtensions()
00726 {
00727 LOG_STRING_DEBUG("CPKIFQuery::AreThereAnyUnprocessedCriticalExtensions()", TOOLKIT_OCSP_ASN, 0, this);
00728
00729 std::vector<CPKIFX509ExtensionPtr>::iterator pos;
00730 std::vector<CPKIFX509ExtensionPtr>::iterator end = m_impl->m_exts.end();
00731 for(pos = m_impl->m_exts.begin(); pos != end; ++pos)
00732 {
00733 if((*pos)->isCritical())
00734 return true;
00735 }
00736
00737 return false;
00738 }