Mixe for Privacy and Anonymity in the Internet
CAXMLPriceCert.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #include "StdAfx.h"
29 #ifndef ONLY_LOCAL_PROXY
30 #ifdef PAYMENT
31 #include "CAXMLPriceCert.hpp"
32 #include "CAUtil.hpp"
33 #include "CAMsg.hpp"
34 
35 const char* const CAXMLPriceCert::ms_pStrElemName="PriceCertificate";
36 
37 CAXMLPriceCert::CAXMLPriceCert()
38  {
39  m_StrSubjectKeyIdentifier = NULL;
40  m_StrSignatureTime = NULL;
41  m_StrBiID = NULL;
42  m_domDocument = NULL;
43  }
44 
45 CAXMLPriceCert::~CAXMLPriceCert()
46  {
47  delete[] m_StrSubjectKeyIdentifier;
48  m_StrSubjectKeyIdentifier = NULL;
49 
50  delete[] m_StrSignatureTime;
51  m_StrSignatureTime = NULL;
52 
53  delete[] m_StrBiID;
54  m_StrBiID = NULL;
55 
56  if(m_domDocument != NULL)
57  {
58  //CAMsg::printMsg(LOG_DEBUG, "cleaning up internal PriceCert document 0x%x.\n",
59  // m_domDocument);
60  m_domDocument->release();
61  m_domDocument=NULL;
62  }
63  }
64 
65 CAXMLPriceCert* CAXMLPriceCert::getInstance(const UINT8 * strXmlData,UINT32 strXmlDataLen)
66 {
67  // parse XML
68  if(strXmlData==NULL)
69  return NULL;
70  CAXMLPriceCert* pPC=new CAXMLPriceCert();
71  pPC->m_domDocument = parseDOMDocument(strXmlData, strXmlDataLen);
72  if(pPC->setValues()!=E_SUCCESS)
73  {
74  delete pPC;
75  pPC = NULL;
76  }
77  return pPC;
78 }
79 
80 CAXMLPriceCert* CAXMLPriceCert::getInstance(DOMElement* elemRoot)
81  {
82  if(elemRoot==NULL)
83  {
84  CAMsg::printMsg(LOG_DEBUG,"CAXMLPriceCert::getInstance: root element is null\n");
85  return NULL;
86  }
87  CAXMLPriceCert* pPC=new CAXMLPriceCert();
88  pPC->m_domDocument=createDOMDocument();
89  pPC->m_domDocument->appendChild(pPC->m_domDocument->importNode(elemRoot,true));
90  if(pPC->setValues()!=E_SUCCESS)
91  {
92  delete pPC;
93  pPC = NULL;
94  CAMsg::printMsg(LOG_DEBUG,"CAXMLPriceCert::getInstance.setValues() FAILED \n");
95  }
96  return pPC;
97  }
98 
99 SINT32 CAXMLPriceCert::toXmlElement(XERCES_CPP_NAMESPACE::DOMDocument* a_doc, DOMElement* &elemRoot)
100  {
101  elemRoot = createDOMElement(a_doc,"PriceCertificate");
102 
103  setDOMElementAttribute(elemRoot,"version",(UINT8*)"1.1");
104 
105  DOMElement* elemHashOfMixCert = createDOMElement(a_doc,"SubjectKeyIdentifier");
106  setDOMElementValue(elemHashOfMixCert,m_StrSubjectKeyIdentifier);
107  elemRoot->appendChild(elemHashOfMixCert);
108 
109  DOMElement* elemRate = createDOMElement(a_doc,"Rate");
110  setDOMElementValue(elemRate,m_lRate);
111  elemRoot->appendChild(elemRate);
112 
113  DOMElement* elemCreationTime = createDOMElement(a_doc,"SignatureTime");
114  setDOMElementValue(elemCreationTime,m_StrSignatureTime);
115  elemRoot->appendChild(elemCreationTime);
116 
117  DOMElement* elemBiID = createDOMElement(a_doc,"BiID");
118  setDOMElementValue(elemBiID,m_StrBiID);
119  elemRoot->appendChild(elemBiID);
120 
121  //append signature node
122  if (m_signature != NULL)
123  {
124  elemRoot->appendChild(a_doc->importNode(m_signature,true));
125  }
126  else
127  {
128  CAMsg::printMsg(LOG_DEBUG,"Could not import PI signature node!\n");
129  }
130  return E_SUCCESS;
131  }
132 
133 SINT32 CAXMLPriceCert::setValues()
134 {
135  CAMsg::printMsg(LOG_DEBUG,"Parsing price certificate.\n");
136 
137  if(m_domDocument==NULL)
138  {
139  CAMsg::printMsg(LOG_CRIT,"Price certificate is no valid document!\n");
140  return E_UNKNOWN;
141  }
142  DOMElement* elemRoot=m_domDocument->getDocumentElement();
143  DOMElement* elem=NULL;
144 
145  if(!equals(elemRoot->getTagName(),ms_pStrElemName))
146  {
147  CAMsg::printMsg(LOG_CRIT,"Failed to get root elem tagname of price certificate!\n");
148  return E_UNKNOWN;
149  }
150  //TODO: parsing Strings could be generalized instead of copy-and-paste code for each element
151  UINT8 strGeneral[512];
152  UINT32 strGeneralLen = 512;
153  // parse subjectkeyidentifier(UINT8*)
154  delete[] m_StrSubjectKeyIdentifier;
155  m_StrSubjectKeyIdentifier=NULL;
156 
157  getDOMChildByName(elemRoot, "SubjectKeyIdentifier", elem, false);
158  if(getDOMElementValue(elem, strGeneral, &strGeneralLen)==E_SUCCESS)
159  {
160  m_StrSubjectKeyIdentifier = new UINT8[strGeneralLen+1];
161  memcpy(m_StrSubjectKeyIdentifier, strGeneral,strGeneralLen+1);
162  //CAMsg::printMsg(LOG_DEBUG,"setValues(): parsing subjectkeyidentifier OK\n");
163  }
164  else
165  {
166  delete[] m_StrSubjectKeyIdentifier;
167  m_StrSubjectKeyIdentifier=NULL;
168  CAMsg::printMsg(LOG_CRIT,"Failed to parse subjectkeyidentifier of price certificate!\n");
169  return E_UNKNOWN;
170  }
171 
172  // parse rate (double)
173  getDOMChildByName(elemRoot, "Rate", elem, false);
174  if(getDOMElementValue(elem, &m_lRate)!=E_SUCCESS)
175  {
176  CAMsg::printMsg(LOG_CRIT,"Could not parse rate of price certificate!\n");
177  return E_UNKNOWN;
178  }
179 
180 
181 
182 
183  // parse creation time (UINT8*)
184  delete[] m_StrSignatureTime;
185  m_StrSignatureTime=NULL;
186  getDOMChildByName(elemRoot, "SignatureTime", elem, false);
187 
188  if(getDOMElementValue(elem, strGeneral, &strGeneralLen)==E_SUCCESS)
189  {
190  m_StrSignatureTime = new UINT8[strGeneralLen+1];
191  memcpy(m_StrSignatureTime, strGeneral,strGeneralLen+1);
192  }
193  else
194  {
195  delete[] m_StrSignatureTime;
196  m_StrSignatureTime=NULL;
197  CAMsg::printMsg(LOG_CRIT,"Could not parse SignatureTime of price certificate!\n");
198  return E_UNKNOWN;
199  }
200 
201  UINT8 strGeneral2[512];
202  UINT32 strGeneralLen2 = 512;
203 
204  // parse BiID (UINT8*)
205  delete[] m_StrBiID;
206  m_StrBiID=NULL;
207  getDOMChildByName(elemRoot, "BiID", elem, false);
208  if(getDOMElementValue(elem, strGeneral2, &strGeneralLen2)==E_SUCCESS)
209  {
210  m_StrBiID = new UINT8[strGeneralLen2+1];
211  memcpy(m_StrBiID, strGeneral2,strGeneralLen2+1);
212  }
213  else
214  {
215  delete[] m_StrBiID;
216  m_StrBiID=NULL;
217  CAMsg::printMsg(LOG_CRIT,"Could not parse parse BiID of price certificate!\n");
218  return E_UNKNOWN;
219  }
220 
221 
222  //if present, store signature node as element
223  getDOMChildByName(elemRoot, "Signature", m_signature, true);
224  //returns E_UNKNOWN if no signature node present, right now we dont care
225 
226 
227 
228 
229 
230  //CAMsg::printMsg(LOG_DEBUG,"setValues(): finished\n");
231  return E_SUCCESS;
232 }
233 
234 
235 #endif //PAYMENT
236 #endif //ONLY_LOCAL_PROXY
SINT32 setDOMElementAttribute(DOMNode *pElem, const char *attrName, const char *value)
Definition: CAUtil.cpp:831
SINT32 getDOMElementValue(const DOMNode *const pElem, UINT8 *value, UINT32 *valuelen)
Returns the content of the text node(s) under elem as null-terminated C String.
Definition: CAUtil.cpp:746
SINT32 setDOMElementValue(DOMElement *pElem, SINT32 value)
Definition: CAUtil.cpp:939
bool equals(const XMLCh *const e1, const char *const e2)
Definition: CAUtil.cpp:645
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument(const UINT8 *const buff, UINT32 len)
Parses a buffer containing an XML document and returns this document.
Definition: CAUtil.cpp:663
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument()
Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in...
Definition: CAUtil.cpp:1568
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
Definition: CAUtil.cpp:814
SINT32 getDOMChildByName(const DOMNode *pNode, const char *const name, DOMElement *&child, bool deep)
Definition: CAUtil.cpp:458
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3