Mixe for Privacy and Anonymity in the Internet
CAXMLBI.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 #if !defined(ONLY_LOCAL_PROXY) && defined (PAYMENT)
30 #include "CAXMLBI.hpp"
31 #include "CAMsg.hpp"
32 
33 const char* const CAXMLBI::ms_pXmlElemName="PaymentInstance";
34 
35 CAXMLBI::CAXMLBI() : CAAbstractXMLEncodable()
36  {
37  m_pVeryfire = NULL;
38  m_pBiID = NULL;
39  m_pHostName = NULL;
40  m_pCert = NULL;
41  }
42 
43 CAXMLBI* CAXMLBI::getInstance(const UINT8 * biID, const UINT8 * hostName, const int portNumber, CACertificate * pCert)
44  {
45  if(biID==NULL||hostName==NULL)
46  {
47  return NULL;
48  }
49  CAXMLBI* pBI = new CAXMLBI();
50  if(pCert!=NULL)
51  {
52  pBI->m_pCert = pCert->clone();
53  }
54  pBI->m_pBiID = new UINT8[strlen((char*)biID)+1];
55  strcpy((char*)pBI->m_pBiID, (char*)biID);
56  memset(pBI->m_pBiID, 0, (strlen((char*)biID)+1));
57  pBI->m_pHostName = new UINT8[strlen((char*)hostName)+1];
58  strcpy((char*)pBI->m_pHostName, (char*)hostName);
59  pBI->m_iPortNumber = portNumber;
60  pBI->m_pVeryfire = NULL;
61  return pBI;
62  }
63 
64 CAXMLBI* CAXMLBI::getInstance(DOMElement* elemRoot)
65  {
66  if (elemRoot == NULL)
67  {
68  return NULL;
69  }
70  CAXMLBI* pPI = new CAXMLBI();
71  if(pPI->setValues(elemRoot)!=E_SUCCESS)
72  {
73  delete pPI;
74  return NULL;
75  }
76  return pPI;
77  }
78 
79 CAXMLBI::~CAXMLBI()
80  {
81  delete m_pCert;
82  m_pCert = NULL;
83 
84  delete m_pVeryfire;
85  m_pVeryfire = NULL;
86 
87  delete[] m_pBiID;
88  m_pBiID = NULL;
89 
90  delete m_pHostName;
91  m_pHostName = NULL;
92  }
93 
94 SINT32 CAXMLBI::setValues(DOMElement* elemRoot)
95  {
96  DOMElement* elem=NULL;
97  UINT8 strGeneral[256];
98  UINT32 strGeneralLen = 255;
99 
100  if(!equals(elemRoot->getTagName(),CAXMLBI::getXMLElementName()))
101  {
102  return E_UNKNOWN;
103  }
104 
105  //Parse ID
106  if(getDOMElementAttribute(elemRoot, "id", strGeneral, &strGeneralLen)==E_SUCCESS)
107  {
108  m_pBiID = new UINT8[strGeneralLen+1];
109  memset(m_pBiID, 0, (strGeneralLen+1));
110  memcpy(m_pBiID,strGeneral,strGeneralLen);
111  }
112  else
113  {
114  return E_UNKNOWN;
115  }
116 
117  //Parse PI Certificate
118  DOMElement* elemCert=NULL;
119  getDOMChildByName(elemRoot, "Certificate", elem, false);
120  getDOMChildByName(elem, "X509Certificate", elemCert, false);
121  CACertificate *pPICert = CACertificate::decode(elemCert, CERT_X509CERTIFICATE, NULL);
122  if (pPICert != NULL)
123  {
124  m_pCert = pPICert;
125  }
126  else
127  {
128  CAMsg::printMsg(LOG_CRIT,"No certificate for payment instance available!\n");
129  return E_UNKNOWN;
130  }
131 
132  //Parse PI Host
133  DOMElement* elemNet=NULL;
134  DOMElement* elemListeners=NULL;
135  DOMElement* elemListener=NULL;
136  DOMElement* elemHost=NULL;
137  DOMElement* elemPort=NULL;
138  getDOMChildByName(elemRoot, "Network", elemNet, false);
139  getDOMChildByName(elemNet, "ListenerInterfaces", elemListeners, false);
140  getDOMChildByName(elemListeners, "ListenerInterface", elemListener, false);
141  getDOMChildByName(elemListener, "Host", elemHost, false);
142  getDOMChildByName(elemListener, "Port", elemPort, false);
143  strGeneralLen=255;
144  //Parse PI Host and Port
145  if( getDOMElementValue(elemHost, strGeneral, &strGeneralLen)!=E_SUCCESS||
146  getDOMElementValue(elemPort, &m_iPortNumber)!=E_SUCCESS)
147  {
148  delete [] m_pBiID;
149  m_pBiID=NULL;
150  delete m_pCert;
151  m_pCert=NULL;
152  return E_UNKNOWN;
153  }
154  m_pHostName = new UINT8[strGeneralLen+1];
155  strcpy((char*)m_pHostName, (char*)strGeneral);
156  return E_SUCCESS;
157  }
158 
159 SINT32 CAXMLBI::toXmlElement(XERCES_CPP_NAMESPACE::DOMDocument *a_doc, DOMElement* & elemRoot)
160  {
161  elemRoot = createDOMElement(a_doc,getXMLElementName());
162  setDOMElementAttribute(elemRoot, "id", m_pBiID);
163 
164  //Set network settings
165  DOMElement* elemNet = createDOMElement(a_doc,"Network");
166  elemRoot->appendChild(elemNet);
167  DOMElement* elemListeners = createDOMElement(a_doc,"ListenerInterfaces");
168  elemNet->appendChild(elemListeners);
169  DOMElement* elemListener = createDOMElement(a_doc,"ListenerInterface");
170  elemListeners->appendChild(elemListener);
171  //Set Hostname
172  DOMElement* elemHost = createDOMElement(a_doc,"Host");
173  elemListener->appendChild(elemHost);
174  setDOMElementValue(elemHost, m_pHostName);
175  //Set Port
176  DOMElement* elemPort = createDOMElement(a_doc,"Port");
177  elemListener->appendChild(elemPort);
178  setDOMElementValue(elemPort, m_iPortNumber);
179  //Set Cert
180  if(m_pCert!=NULL)
181  {
182  DOMElement* elemCert = createDOMElement(a_doc,"Certificate");
183  elemRoot->appendChild(elemCert);
184 
185  DOMElement* tmpElem=NULL;
186  m_pCert->encode(tmpElem, a_doc);
187  elemCert->appendChild(tmpElem);
188  }
189 
190  return E_SUCCESS;
191  }
192 #endif //ONLY_LOCAL_PROXY
#define CERT_X509CERTIFICATE
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
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
SINT32 getDOMElementAttribute(const DOMNode *const elem, const char *attrName, UINT8 *value, UINT32 *len)
Definition: CAUtil.cpp:780
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
Abstract base class for classes which can be converted to an XML structure.
CACertificate * clone() const
static CACertificate * decode(const UINT8 *const buff, UINT32 bufflen, UINT32 type, const char *const passwd=NULL)
Extracts a certificate from an encoded (DER,XML) form.
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