Mixe for Privacy and Anonymity in the Internet
CAXMLPriceCert.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team 
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification, 
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice, 
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice, 
00011     this list of conditions and the following disclaimer in the documentation and/or 
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 
00015     may be used to endorse or promote products derived from this software without specific 
00016     prior written permission. 
00017 
00018   
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #include "StdAfx.h"
00029 #ifndef ONLY_LOCAL_PROXY
00030 #ifdef PAYMENT
00031 #include "CAXMLPriceCert.hpp"
00032 #include "CAUtil.hpp"
00033 #include "CAMsg.hpp"
00034 
00035 const char* const CAXMLPriceCert::ms_pStrElemName="PriceCertificate";
00036 
00037 CAXMLPriceCert::CAXMLPriceCert()
00038   {
00039     m_StrSubjectKeyIdentifier = NULL;
00040     m_StrSignatureTime = NULL;
00041     m_StrBiID = NULL;
00042     m_domDocument = NULL; 
00043   }
00044 
00045 CAXMLPriceCert::~CAXMLPriceCert()
00046   {
00047     delete[] m_StrSubjectKeyIdentifier;
00048     m_StrSubjectKeyIdentifier = NULL;
00049     
00050     delete[] m_StrSignatureTime;
00051     m_StrSignatureTime = NULL;
00052     
00053     delete[] m_StrBiID;
00054     m_StrBiID = NULL;
00055     
00056     if(m_domDocument != NULL)
00057     {
00058       //CAMsg::printMsg(LOG_DEBUG, "cleaning up internal PriceCert document 0x%x.\n",
00059       //          m_domDocument);
00060       m_domDocument->release();
00061       m_domDocument=NULL; 
00062     }   
00063   }
00064 
00065 CAXMLPriceCert* CAXMLPriceCert::getInstance(const UINT8 * strXmlData,UINT32 strXmlDataLen)
00066 {
00067   // parse XML
00068   if(strXmlData==NULL)
00069     return NULL;
00070   CAXMLPriceCert* pPC=new CAXMLPriceCert();
00071   pPC->m_domDocument = parseDOMDocument(strXmlData, strXmlDataLen);
00072   if(pPC->setValues()!=E_SUCCESS)
00073     {
00074       delete pPC;
00075       pPC = NULL;
00076     }
00077   return pPC; 
00078 }
00079 
00080 CAXMLPriceCert* CAXMLPriceCert::getInstance(DOMElement* elemRoot)
00081   {
00082     if(elemRoot==NULL)
00083       {
00084         CAMsg::printMsg(LOG_DEBUG,"CAXMLPriceCert::getInstance: root element is null\n");
00085         return NULL;
00086       } 
00087     CAXMLPriceCert* pPC=new CAXMLPriceCert();
00088     pPC->m_domDocument=createDOMDocument();
00089     pPC->m_domDocument->appendChild(pPC->m_domDocument->importNode(elemRoot,true));
00090     if(pPC->setValues()!=E_SUCCESS)
00091       {
00092         delete pPC;
00093         pPC = NULL;
00094         CAMsg::printMsg(LOG_DEBUG,"CAXMLPriceCert::getInstance.setValues() FAILED \n");
00095       }
00096     return pPC; 
00097   }
00098 
00099 SINT32 CAXMLPriceCert::toXmlElement(XERCES_CPP_NAMESPACE::DOMDocument* a_doc, DOMElement* &elemRoot)
00100   {
00101     elemRoot = createDOMElement(a_doc,"PriceCertificate");
00102   
00103     setDOMElementAttribute(elemRoot,"version",(UINT8*)"1.1"); 
00104 
00105     DOMElement* elemHashOfMixCert = createDOMElement(a_doc,"SubjectKeyIdentifier");
00106     setDOMElementValue(elemHashOfMixCert,m_StrSubjectKeyIdentifier);
00107     elemRoot->appendChild(elemHashOfMixCert);
00108   
00109     DOMElement* elemRate = createDOMElement(a_doc,"Rate");
00110     setDOMElementValue(elemRate,m_lRate);
00111     elemRoot->appendChild(elemRate);
00112   
00113     DOMElement* elemCreationTime = createDOMElement(a_doc,"SignatureTime");
00114     setDOMElementValue(elemCreationTime,m_StrSignatureTime);
00115     elemRoot->appendChild(elemCreationTime);
00116   
00117     DOMElement* elemBiID = createDOMElement(a_doc,"BiID");
00118     setDOMElementValue(elemBiID,m_StrBiID);
00119     elemRoot->appendChild(elemBiID);
00120   
00121     //append signature node
00122     if (m_signature != NULL)
00123     {     
00124       elemRoot->appendChild(a_doc->importNode(m_signature,true));
00125     }
00126     else
00127       {
00128         CAMsg::printMsg(LOG_DEBUG,"Could not import PI signature node!\n");
00129       }
00130     return E_SUCCESS;
00131   }
00132 
00133 SINT32 CAXMLPriceCert::setValues() 
00134 {
00135   CAMsg::printMsg(LOG_DEBUG,"Parsing price certificate.\n");
00136 
00137   if(m_domDocument==NULL)
00138   {
00139     CAMsg::printMsg(LOG_CRIT,"Price certificate is no valid document!\n");
00140     return E_UNKNOWN;
00141   }
00142   DOMElement* elemRoot=m_domDocument->getDocumentElement();
00143   DOMElement* elem=NULL;
00144   
00145   if(!equals(elemRoot->getTagName(),ms_pStrElemName))
00146     {
00147       CAMsg::printMsg(LOG_CRIT,"Failed to get root elem tagname of price certificate!\n");
00148       return E_UNKNOWN;
00149     }
00150   //TODO: parsing Strings could be generalized instead of copy-and-paste code for each element
00151   UINT8 strGeneral[512];
00152   UINT32 strGeneralLen = 512;
00153   // parse subjectkeyidentifier(UINT8*)
00154   delete[] m_StrSubjectKeyIdentifier;
00155   m_StrSubjectKeyIdentifier=NULL;
00156 
00157   getDOMChildByName(elemRoot, "SubjectKeyIdentifier", elem, false);
00158   if(getDOMElementValue(elem, strGeneral, &strGeneralLen)==E_SUCCESS)
00159     {
00160       m_StrSubjectKeyIdentifier = new UINT8[strGeneralLen+1];
00161       memcpy(m_StrSubjectKeyIdentifier, strGeneral,strGeneralLen+1);
00162       //CAMsg::printMsg(LOG_DEBUG,"setValues(): parsing subjectkeyidentifier OK\n");
00163     }
00164   else
00165     {
00166       delete[] m_StrSubjectKeyIdentifier;
00167       m_StrSubjectKeyIdentifier=NULL;
00168       CAMsg::printMsg(LOG_CRIT,"Failed to parse subjectkeyidentifier of price certificate!\n");
00169       return E_UNKNOWN;
00170     }
00171   
00172   // parse rate (double)
00173   getDOMChildByName(elemRoot, "Rate", elem, false);
00174   if(getDOMElementValue(elem, &m_lRate)!=E_SUCCESS)
00175   {
00176     CAMsg::printMsg(LOG_CRIT,"Could not parse rate of price certificate!\n");
00177     return E_UNKNOWN;
00178   }
00179   
00180 
00181 
00182 
00183   // parse creation time (UINT8*)
00184   delete[] m_StrSignatureTime;
00185   m_StrSignatureTime=NULL;
00186   getDOMChildByName(elemRoot, "SignatureTime", elem, false);
00187   
00188   if(getDOMElementValue(elem, strGeneral, &strGeneralLen)==E_SUCCESS)
00189     {
00190       m_StrSignatureTime = new UINT8[strGeneralLen+1];
00191       memcpy(m_StrSignatureTime, strGeneral,strGeneralLen+1);
00192     }
00193   else
00194     {
00195       delete[] m_StrSignatureTime;
00196       m_StrSignatureTime=NULL;
00197       CAMsg::printMsg(LOG_CRIT,"Could not parse SignatureTime of price certificate!\n");
00198       return E_UNKNOWN;
00199     }
00200   
00201   UINT8 strGeneral2[512];
00202   UINT32 strGeneralLen2 = 512;  
00203   
00204   // parse BiID (UINT8*)
00205   delete[] m_StrBiID;
00206   m_StrBiID=NULL;
00207   getDOMChildByName(elemRoot, "BiID", elem, false);
00208   if(getDOMElementValue(elem, strGeneral2, &strGeneralLen2)==E_SUCCESS)
00209     {
00210       m_StrBiID = new UINT8[strGeneralLen2+1];
00211       memcpy(m_StrBiID, strGeneral2,strGeneralLen2+1);
00212     }
00213   else
00214     {
00215       delete[] m_StrBiID;
00216       m_StrBiID=NULL;
00217       CAMsg::printMsg(LOG_CRIT,"Could not parse parse BiID of price certificate!\n");
00218       return E_UNKNOWN;
00219     }
00220     
00221     
00222   //if present, store signature node as element 
00223   getDOMChildByName(elemRoot, "Signature", m_signature, true);
00224   //returns E_UNKNOWN if no signature node present, right now we dont care 
00225   
00226   
00227   
00228     
00229   
00230   //CAMsg::printMsg(LOG_DEBUG,"setValues(): finished\n");
00231   return E_SUCCESS;
00232 }
00233 
00234 
00235 #endif //PAYMENT
00236 #endif //ONLY_LOCAL_PROXY