ValPolRequest.cpp

Go to the documentation of this file.
00001 
00010 #include "ValPolRequest.h"
00011 #include "components.h"
00012 #include "ToolkitUtils.h"
00013 #include "Buffer.h"
00014 #include "PKIFSCVPErrors.h"
00015 #include "SCVPException.h"
00016 #include "ASN1Helper.h"
00017 #include "SCVP.h"
00018 #include "private/PKIFSCVPMemoryHelper.h"
00019 #include "IPKIFColleague.h"
00020 #include "IPKIFCryptoMisc.h"
00021 #include "PKIFCryptUtils.h"
00022 #include "PKIFMessageErrors.h"
00023 
00025 
00026 struct CPKIFValPolRequestImpl 
00027 {
00028     CPKIFValPolRequest *m_parent;
00029     
00030     int m_version;
00031     CPKIFBufferPtr m_nonce;
00032 
00033     //convenience goo for auto-nonce generation
00034     bool m_bGenerateNonce;
00035     void GenerateNonce() const; //logical const
00036     IPKIFMediatorPtr m_mediator;
00037 
00038     //member for decoding
00039     CPKIFASNWrapper<ValPolRequest>* m_scvpValPolReq;
00040 
00041     void ClearAllMembers();
00042     void MakeSCVPValPolReq();
00043     void FreeSCVPValPolReq();
00044     void CallingAllGets();
00045 };
00046 
00054 void CPKIFValPolRequestImpl::CallingAllGets()
00055 {
00056     LOG_STRING_DEBUG("CPKIFValPolRequestImpl::CallingAllGets()", TOOLKIT_SCVP_ASN, 0, this);
00057 
00058     m_parent->GetNonce();
00059     FreeSCVPValPolReq();
00060 }
00061 
00069 void CPKIFValPolRequestImpl::MakeSCVPValPolReq()
00070 {
00071     LOG_STRING_DEBUG("CPKIFValPolRequestImpl::MakeSCVPR()", TOOLKIT_SCVP_ASN, 0, this);
00072 
00073     FreeSCVPValPolReq();
00074     m_scvpValPolReq = new CPKIFASNWrapper<ValPolRequest>( BEREncValPolRequest, BERDecValPolRequest );
00075 }
00083 void CPKIFValPolRequestImpl::FreeSCVPValPolReq()
00084 {
00085     LOG_STRING_DEBUG("CPKIFValPolRequestImpl::FreeSCVPR()", TOOLKIT_SCVP_ASN, 0, this);
00086     if(NULL != m_scvpValPolReq)
00087         delete m_scvpValPolReq;
00088     m_scvpValPolReq = NULL;
00089 }
00090 
00099 void CPKIFValPolRequestImpl::ClearAllMembers()
00100 {
00101     m_version = 1;
00102     m_bGenerateNonce = false;
00103 
00104     IPKIFMediatorPtr tmp;
00105     m_mediator = tmp;
00106     
00107     CPKIFBufferPtr tmpNonce;
00108     m_nonce = tmpNonce;
00109 
00110     FreeSCVPValPolReq();
00111 }
00112 
00120 void CPKIFValPolRequestImpl::GenerateNonce() const
00121 {
00122     LOG_STRING_DEBUG("CPKIFValPolRequestImpl::GenerateNonce()", TOOLKIT_SCVP_ASN, 0, this);
00123 
00124     IPKIFCryptoMisc* iMisc = NULL;
00125     if(NULL != m_mediator)
00126         iMisc = m_mediator->GetMediator<IPKIFCryptoMisc>();
00127 
00128     unsigned char buf[20];
00129     memset(buf, 0, 20); //avoid (unlikely) case of sending random memory to TSA
00130 
00131     if(NULL != iMisc)
00132     {
00133         //if we can get a pointer to an externally provided interface use it
00134         iMisc->GenRandom(buf, 20);
00135     }
00136     else
00137     {
00138         //otherwise create a temp object to generate the nonce
00139  
00140         IPKIFCryptoMisc *raw = GetPlatformCryptoMisc();
00141         raw->GenRandom(buf, 20);
00142     }
00143 
00144 
00145     //strip leading zeroes
00146     const unsigned char* bufEnd = buf + 20;
00147     unsigned char* bufP = buf;
00148     int nonZeroLen = 20;
00149     while(*bufP == 0x00)
00150     {
00151         ++bufP;
00152         --nonZeroLen;
00153     }
00154     CPKIFBufferPtr tmpNonce(new CPKIFBuffer(bufP, nonZeroLen));
00155 
00156     CPKIFValPolRequestImpl* nonConst = const_cast<CPKIFValPolRequestImpl*>(this);
00157     nonConst->m_nonce = tmpNonce;
00158 }
00160 
00169 CPKIFValPolRequest::CPKIFValPolRequest() :m_impl(new CPKIFValPolRequestImpl)
00170 {
00171     LOG_STRING_DEBUG("CPKIFValPolRequest::CPKIFValPolRequest()", TOOLKIT_SCVP_ASN, 0, this);
00172 
00173     m_impl->m_version = 1;
00174     m_impl->m_bGenerateNonce = false;
00175 
00176     m_impl->m_scvpValPolReq = NULL;
00177 
00178 }
00179 
00187 CPKIFValPolRequest::~CPKIFValPolRequest()
00188 {
00189     LOG_STRING_DEBUG("CPKIFValPolRequest::~CPKIFValPolRequest()", TOOLKIT_SCVP_ASN, 0, this);
00190 
00191     m_impl->FreeSCVPValPolReq();
00192     if (m_impl) {
00193       delete m_impl;
00194     }   
00195 }
00203 void CPKIFValPolRequest::SetNonce(
00206     CPKIFBufferPtr& nonce)
00207 {
00208     LOG_STRING_DEBUG("CPKIFValPolRequest::SetNonce(CPKIFStringPtr& nonce)", TOOLKIT_SCVP_ASN, 0, this);
00209 
00210     m_impl->m_nonce = nonce;
00211 }
00219 const CPKIFBufferPtr CPKIFValPolRequest::GetNonce() const
00220 {
00221     if(m_impl->m_nonce == NULL)
00222     {
00223         CPKIFBufferPtr t;
00224         return t;
00225     }
00226     else
00227         return m_impl->m_nonce;
00228 }
00229 
00237 void CPKIFValPolRequest::SetGenerateNonce(
00239     bool generateNonce)
00240 {
00241     LOG_STRING_DEBUG("CPKIFValPolRequest::SetGenerateNonce(bool generateNonce, IPKIFMediatorPtr m)", TOOLKIT_SCVP_ASN, 0, this);
00242 
00243     m_impl->m_bGenerateNonce = generateNonce;
00244 }
00245 
00255 void CPKIFValPolRequest::SetGenerateNonce(
00257     bool generateNonce, 
00259     IPKIFMediatorPtr& m)
00260 {
00261     LOG_STRING_DEBUG("CPKIFValPolRequest::SetGenerateNonce(bool generateNonce, IPKIFMediatorPtr m)", TOOLKIT_SCVP_ASN, 0, this);
00262 
00263     m_impl->CallingAllGets();
00264     m_impl->m_bGenerateNonce = generateNonce;
00265     m_impl->m_mediator = m;
00266 }
00274 bool CPKIFValPolRequest::GetGenerateNonce() const
00275 {
00276     return m_impl->m_bGenerateNonce;
00277 }
00278 
00289 void CPKIFValPolRequest::Decode(
00291     CPKIFBufferPtr& msg)
00292 {
00293     m_impl->ClearAllMembers();
00294 
00295     //if the input is empty - fail now
00296     if(msg == (CPKIFBuffer*)NULL || 0 == msg->GetLength())
00297     {
00298         throw CPKIFSCVPException(TOOLKIT_SCVP_ASN, COMMON_INVALID_INPUT);
00299     }
00300 
00301     m_impl->MakeSCVPValPolReq();
00302 
00303     try
00304     {
00305         //otherwise try to parse it into the m_impl->m_scvpr member
00306         const unsigned char* tmpP = msg->GetBuffer();
00307         (*m_impl->m_scvpValPolReq).Decode(msg->GetBuffer(), msg->GetLength());
00308 
00309         m_impl->m_version = (*m_impl->m_scvpValPolReq)->vpRequestVersion;
00310 
00311         CPKIFBufferPtr algOID(new CPKIFBuffer((*m_impl->m_scvpValPolReq)->requestNonce.data, (*m_impl->m_scvpValPolReq)->requestNonce.numocts));
00312     
00313         m_impl->m_nonce = algOID;
00314 
00315         m_impl->m_bGenerateNonce = false;
00316     }
00317     catch(CPKIFException&)
00318     {
00319         throw CPKIFSCVPException(TOOLKIT_SCVP_ASN, MSG_DECODE_FAILED);
00320     }
00321 }
00329 int CPKIFValPolRequest::GetVersion() const
00330 {
00331     return m_impl->m_version;
00332 }
00333 
00341 void CPKIFValPolRequest::SetVersion(int v)
00342 {
00343     m_impl->m_version = v;
00344 }
00356 CPKIFBufferPtr CPKIFValPolRequest::Encode() const
00357 {
00358     PKIFSCVPMemoryHelper mhSCVPValPolRequest;
00359     mhSCVPValPolRequest.pValPolRequest = new ValPolRequest;
00360     memset(mhSCVPValPolRequest.pValPolRequest, 0, sizeof(ValPolRequest));
00361     //nonce
00362     if(m_impl->m_bGenerateNonce)
00363     {
00364         m_impl->GenerateNonce();
00365     }
00366 
00367     const CPKIFBufferPtr nonce = GetNonce();
00368     if(m_impl->m_nonce != (CPKIFBuffer*)NULL)
00369     {
00370         mhSCVPValPolRequest.pValPolRequest->requestNonce.data = m_impl->m_nonce->GetBuffer();
00371         mhSCVPValPolRequest.pValPolRequest->requestNonce.numocts = m_impl->m_nonce->GetLength();
00372 
00373     }
00374     else
00375     {
00376         throw CPKIFSCVPException(TOOLKIT_SCVP, SCVP_MISSING_REQUIRED_FIELD, "ValPolRequest nonce value not specified.");
00377     }
00378 
00379     mhSCVPValPolRequest.pValPolRequest->vpRequestVersion = m_impl->m_version;
00380 
00381     CACASNWRAPPER_CREATE(ValPolRequest, objPDU);
00382     ASN1OpenType* data1 = objPDU.Encode(mhSCVPValPolRequest.pValPolRequest);
00383     CPKIFBufferPtr tmp(new CPKIFBuffer(data1->data, data1->numocts));
00384     delete data1;
00385 
00386     return tmp;
00387 }

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