Mixe for Privacy and Anonymity in the Internet
Defines | Functions | Variables
CAUtil.cpp File Reference
#include "StdAfx.h"
#include "CAUtil.hpp"
#include "CABase64.hpp"
#include "xml/DOM_Output.hpp"
#include "CASymCipher.hpp"
Include dependency graph for CAUtil.cpp:

Go to the source code of this file.

Defines

#define XML_ENCODE_KEY_TEMPLATE   "<EncryptedKey><EncryptionMethod Algorithm=\"RSA\"/><CipherData><CipherValue>%s</CipherValue></CipherData></EncryptedKey>"

Functions

UINT32 strtrim (UINT8 *s)
 Removes leading and ending whitespaces (chars<=32) from a zero terminated string.
UINT32 toLower (UINT8 *a_string)
UINT8bytes2hex (const void *bytes, UINT32 len)
 Converts the byte array to a hex string.
SINT32 memtrim (UINT8 *dest, const UINT8 *src, UINT32 size)
 Removes leading and ending whitespaces (chars<=32) from a byte array.
char * strins (const char *src, UINT32 pos, const char *ins)
 Inserts a String ins in a String src starting after pos chars.
char * strins (const char *src, const char *pos, const char *ins)
 Inserts a String ins in a String src starting at the char pos points to.
void logMemoryUsage ()
 Log information about the current memory (heap) usage.
SINT32 getcurrentTime (timespec &t)
 Gets the current Systemtime in milli seconds.
SINT32 getcurrentTimeMillis (UINT64 &u64Time)
 Gets the current Systemtime in milli seconds.
SINT32 getcurrentTimeMicros (UINT64 &u64Time)
 Gets the current Systemtime in micros seconds.
SINT32 initRandom ()
SINT32 compDate (struct tm *date1, struct tm *date2)
SINT32 getRandom (UINT32 *val)
 Gets 32 random bits.
SINT32 getRandom (UINT64 *val)
SINT32 getRandom (UINT8 *buff, UINT32 len)
 Gets some random bytes.
SINT32 msSleep (UINT32 ms)
 Sleeps ms milliseconds.
SINT32 sSleep (UINT32 sec)
 Sleeps sec Seconds.
UINT32 getMemoryUsage ()
SINT32 getDOMChildByName (const DOMNode *pNode, const char *const name, DOMElement *&child, bool deep)
SINT32 getDOMChildByName (const DOMNode *pNode, const XMLCh *const name, DOMNode *&a_child, bool deep)
SINT32 getDOMChildByName (const DOMNode *pNode, const char *const name, DOMNode *&a_child, bool deep)
SINT32 getSignatureElements (DOMNode *parent, DOMNode **signatureNodes, UINT32 *length)
SINT32 integrateDOMNode (const DOMNode *srcNode, DOMNode *dstNode, bool recursive, bool replace)
 integrates the source node in the destination Node.
bool equals (const XMLCh *const e1, const char *const e2)
void initDOMParser ()
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument (const UINT8 *const buff, UINT32 len)
 Parses a buffer containing an XML document and returns this document.
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument (const UINT8 *const pathOrURL)
 parses a file via path or URL
void releaseDOMParser ()
SINT32 getNodeName (const DOMNode *const pElem, UINT8 *value, UINT32 *valuelen)
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.
SINT32 getDOMElementAttribute (const DOMNode *const elem, const char *attrName, UINT8 *value, UINT32 *len)
SINT32 getDOMElementAttribute (const DOMNode *const elem, const char *attrName, SINT32 *value)
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.
DOMText * createDOMText (XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const text)
 Creates a new DOMText with the given value which belongs to the DOMDocument owernDoc.
SINT32 setDOMElementAttribute (DOMNode *pElem, const char *attrName, const UINT8 *value)
SINT32 setCurrentTimeMilliesAsDOMAttribute (DOMNode *pElem)
UINT8getTermsAndConditionsTemplateRefId (DOMNode *tcTemplateRoot)
DOMNodeList * getElementsByTagName (DOMElement *pElem, const char *const name)
SINT32 getLastDOMChildByName (const DOMNode *pNode, const char *const name, DOMElement *&a_child)
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument ()
 Creates an empty DOM DOcument.
SINT32 setDOMElementValue (DOMElement *pElem, UINT32 value)
SINT32 setDOMElementValue (DOMElement *pElem, SINT32 value)
SINT32 setDOMElementValue (DOMElement *pElem, double floatValue)
SINT32 setDOMElementValue (DOMElement *pElem, const UINT64 text)
 Sets the decimal text representation of a 64bit integer as node value TODO: implement this for non-64bit platforms.
SINT32 setDOMElementValue (DOMElement *pElem, const UINT8 *value)
SINT32 setDOMElementAttribute (DOMNode *pElem, const char *attrName, UINT64 value)
SINT32 setDOMElementAttribute (DOMNode *pElem, const char *attrName, SINT64 value)
SINT32 setDOMElementAttribute (DOMNode *pElem, const char *attrName, SINT32 value)
SINT32 setDOMElementAttribute (DOMNode *pElem, const char *attrName, UINT32 value)
SINT32 setDOMElementAttribute (DOMNode *pElem, const char *attrName, bool value)
SINT32 setDOMElementValue (DOMElement *pElem, bool value)
SINT32 getDOMElementAttribute (const DOMNode *const elem, const char *attrName, SINT64 &value)
SINT32 getDOMElementAttribute (const DOMNode *const elem, const char *attrName, UINT64 &value)
SINT32 getDOMElementAttribute (const DOMNode *const elem, const char *attrName, UINT32 &value)
SINT32 getDOMElementAttribute (const DOMNode *const elem, const char *attrName, bool &value)
SINT32 getLastDOMChildByName (const DOMNode *pNode, const char *const name, DOMNode *&a_child)
SINT32 getLastDOMChildByName (const DOMNode *pNode, const XMLCh *const name, DOMNode *&a_child)
SINT32 getDOMElementValue (const DOMElement *const pElem, UINT32 *value)
SINT32 getDOMElementValue (const DOMElement *const pElem, SINT32 *value)
SINT32 getDOMElementValue (const DOMElement *const pElem, double *value)
SINT32 getDOMElementValue (const DOMElement *pElem, UINT32 &value, UINT32 defaultValue)
 Gets the value from an DOM-Element as UINT32.
SINT32 getDOMElementValue (const DOMNode *const pElem, UINT64 &value)
SINT32 getDOMElementValue (const DOMElement *const pElem, SINT64 &value)
SINT32 getDOMElementValue (const DOMElement *const pElem, UINT16 *value)
void __encryptKey (UINT8 *key, UINT32 keylen, UINT8 *outBuff, UINT32 *outLen, CAASymCipher *pRSA)
SINT32 encodeXMLEncryptedKey (UINT8 *key, UINT32 keylen, UINT8 *xml, UINT32 *xmllen, CAASymCipher *pRSA)
SINT32 encodeXMLEncryptedKey (UINT8 *key, UINT32 keylen, DOMElement *&elemRootEncodedKey, XERCES_CPP_NAMESPACE::DOMDocument *docOwner, CAASymCipher *pRSA)
SINT32 decodeXMLEncryptedKey (UINT8 *key, UINT32 *keylen, const UINT8 *const xml, UINT32 xmllen, CAASymCipher *pRSA)
SINT32 decodeXMLEncryptedKey (UINT8 *key, UINT32 *keylen, const DOMNode *root, CAASymCipher *pRSA)
SINT32 encryptXMLElement (DOMNode *node, CAASymCipher *pRSA)
 The resulting encrypted xml struct is as follows:
UINT8encryptXMLElement (UINT8 *inbuff, UINT32 inlen, UINT32 &outlen, CAASymCipher *pRSA)
 Encrypts an XML-Element by wrapping it with:
SINT32 decryptXMLElement (DOMNode *node, CAASymCipher *pRSA)
 Replaces a DOM element with a deencrypted version of this element.
UINT8readFile (const UINT8 *const name, UINT32 *size)
SINT32 saveFile (const UINT8 *const name, const UINT8 *const buff, UINT32 buffSize)
SINT32 parseU64 (const UINT8 *str, UINT64 &value)
 Parses a 64bit unsigned integer.
SINT32 parseS64 (const UINT8 *str, SINT64 &value)
 Parses a 64bit signed integer.
SINT32 readPasswd (UINT8 *buff, UINT32 len)
 Read a passwd (i.e.

Variables

XercesDOMParser * theDOMParser = NULL
CAMutextheParseDOMDocumentLock = NULL

Define Documentation

#define XML_ENCODE_KEY_TEMPLATE   "<EncryptedKey><EncryptionMethod Algorithm=\"RSA\"/><CipherData><CipherValue>%s</CipherValue></CipherData></EncryptedKey>"

Referenced by encodeXMLEncryptedKey().


Function Documentation

void __encryptKey ( UINT8 key,
UINT32  keylen,
UINT8 outBuff,
UINT32 outLen,
CAASymCipher pRSA 
)

Definition at line 1191 of file CAUtil.cpp.

References CABase64::encode(), and CAASymCipher::encrypt().

Referenced by encodeXMLEncryptedKey().

{
  UINT8 tmpBuff[1024];
  memset(tmpBuff,0,sizeof(tmpBuff));
  memcpy(tmpBuff+128-keylen,key,keylen);
  pRSA->encrypt(tmpBuff,tmpBuff);
  CABase64::encode(tmpBuff,128,outBuff,outLen);
  outBuff[*outLen]=0;
}

Here is the call graph for this function:

UINT8* bytes2hex ( const void *  bytes,
UINT32  len 
)

Converts the byte array to a hex string.

the caller is responsible for deleting the string

Definition at line 90 of file CAUtil.cpp.

References len.

Referenced by CAChain::getPrintableChainId(), and CAThread::start().

  {
    if(bytes==NULL||len==0)
      return NULL;
    UINT8* buff=new UINT8[len*3+1];
    UINT32 aktInd=0;
    for(UINT32 i=0;i<len;i++)
      {
        UINT8 b1=((UINT8*)bytes)[i];
        UINT8 b=(b1>>4);
        if(b>9)
          b+=55;
        else
          b+=48;
        buff[aktInd++]=b;
        b=(b1&0x0F);
        if(b>9)
          b+=55;
        else
          b+=48;
        buff[aktInd++]=b;
        buff[aktInd++]=32;
      }
    buff[len*3]=0;
    return buff;
  }
SINT32 compDate ( struct tm *  date1,
struct tm *  date2 
)

Definition at line 321 of file CAUtil.cpp.

{
  //year
  if(date1->tm_year != date2->tm_year)
  {
    return (date1->tm_year < date2->tm_year) ? -1 : 1;
  }
  if(date1->tm_mon != date2->tm_mon)
  {
    return (date1->tm_mon < date2->tm_mon) ? -1 : 1;
  }
  if(date1->tm_mday != date2->tm_mday)
  {
    return (date1->tm_mday < date2->tm_mday) ? -1 : 1;
  }
  return 0;
}
XERCES_CPP_NAMESPACE::DOMDocument* createDOMDocument ( )
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 at line 779 of file CAUtil.cpp.

Referenced by CAASymCipher::addKeyPart(), CAMix::appendCompatibilityInfo(), CAMix::appendTermsAndConditionsExtension(), CACmdLnOptions::buildDefaultConfig(), CAFirstMix::doUserLogin_internal(), CACertStore::encode(), CACertificate::encode(), encodeXMLEncryptedKey(), encryptXMLElement(), CAInfoService::getCascadeHeloXMLAsString(), CACmdLnOptions::getMixXml(), CAASymCipher::getPublicKeyAsDOMElement(), CASignature::getSignKey(), CAAccountingInstance::handleAccountCertificate_internal(), CAFirstMix::handleTermsAndConditionsLogin(), CAMix::initMixCascadeInfo(), CAReplayCtrlChannelMsgProc::initTimestampsMessageTemplate(), CAAccountingInstance::makeCCRequest(), CAAccountingInstance::makeInitialCCRequest(), CAAccountingInstance::prepareCCRequest(), CAReplayCtrlChannelMsgProc::proccessGetTimestamp(), CAReplayCtrlChannelMsgProc::proccessGotTimestamp(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), CAFirstMix::processKeyExchange(), CACmdLnOptions::processXmlConfiguration(), CAAccountingInstance::sendAILoginConfirmation(), CAReplayCtrlChannelMsgProc::sendGetTimestamp(), CACmdLnOptions::setMixName(), CACmdLnOptions::setNextMix(), CACmdLnOptions::setPrepaidInterval(), CACmdLnOptions::setPrevMix(), CACmdLnOptions::setTargetInterfaces(), CAAccountingBIInterface::settleAll(), CAMultiSignature::signXML(), CAMix::termsAndConditionsInfoNode(), CAListenerInterface::toDOMElement(), CAXMLPriceCert::toXmlElement(), CAXMLBI::toXmlElement(), and CAXMLErrorMessage::toXmlElement().

  {
    XMLCh* n=XMLString::transcode(name);
    DOMElement* ret=pOwnerDoc->createElement(n);
    XMLString::release(&n);
    return ret;
  }
DOMText * createDOMText ( XERCES_CPP_NAMESPACE::DOMDocument *  pOwnerDoc,
const char *const  text 
)

Creates a new DOMText with the given value which belongs to the DOMDocument owernDoc.

Definition at line 787 of file CAUtil.cpp.

Referenced by CAASymCipher::addKeyPart().

  {
    XMLCh* t=XMLString::transcode(text);
    DOMText* ret= pOwnerDoc->createTextNode(t);
    XMLString::release(&t);
    return ret;
  }
SINT32 decodeXMLEncryptedKey ( UINT8 key,
UINT32 keylen,
const UINT8 *const  xml,
UINT32  xmllen,
CAASymCipher pRSA 
)

Definition at line 1233 of file CAUtil.cpp.

References decodeXMLEncryptedKey(), E_UNKNOWN, and parseDOMDocument().

Referenced by decodeXMLEncryptedKey(), CAMiddleMix::processKeyExchange(), and CALastMix::processKeyExchange().

{
  XERCES_CPP_NAMESPACE::DOMDocument* pDoc=parseDOMDocument(xml,xmllen);
  if(pDoc == NULL)
  {
    return E_UNKNOWN;
  }
  DOMElement* root=pDoc->getDocumentElement();
  if(root == NULL)
  {
    return E_UNKNOWN;
  }
  SINT32 ret=decodeXMLEncryptedKey(key,keylen,root,pRSA);
  if (pDoc != NULL)
  {
    pDoc->release();
    pDoc = NULL;
  }
  return ret;
}

Here is the call graph for this function:

SINT32 decodeXMLEncryptedKey ( UINT8 key,
UINT32 keylen,
const DOMNode *  root,
CAASymCipher pRSA 
)

Definition at line 1254 of file CAUtil.cpp.

References CABase64::decode(), CAASymCipher::decrypt(), E_SUCCESS, E_UNKNOWN, getDOMChildByName(), and getDOMElementValue().

{
  DOMNode* elemCipherValue=NULL;
  if(getDOMChildByName(root,"CipherValue",elemCipherValue,true)!=E_SUCCESS)
    return E_UNKNOWN;
  UINT8 buff[2048];
  UINT32 bufflen=2048;
  if(getDOMElementValue(elemCipherValue,buff,&bufflen)!=E_SUCCESS)
    return E_UNKNOWN;
  CABase64::decode(buff,bufflen,buff,&bufflen);
  pRSA->decrypt(buff,buff);
  for(SINT32 i=127;i>=0;i--)
    {
      if(buff[i]!=0)
        {
          if(i>32)
            *keylen=64;
          else if(i>16)
            *keylen=32;
          else
            *keylen=16;
        }
    }
  memcpy(key,buff+128-(*keylen),(*keylen));
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 decryptXMLElement ( DOMNode *  node,
CAASymCipher pRSA 
)

Replaces a DOM element with a deencrypted version of this element.

Definition at line 1437 of file CAUtil.cpp.

References CABase64::decode(), CASymCipher::decrypt1CBCwithPKCS7(), CAASymCipher::decryptOAEP(), E_SUCCESS, E_UNKNOWN, equals(), getDOMChildByName(), getDOMElementValue(), len, parseDOMDocument(), CASymCipher::setIVs(), and CASymCipher::setKey().

Referenced by CAFirstMix::doUserLogin_internal().

{
  XERCES_CPP_NAMESPACE::DOMDocument* doc=node->getOwnerDocument();
  if(! equals(node->getNodeName(),"EncryptedData"))
    return E_UNKNOWN;
  DOMNode* elemKeyInfo=NULL;
  getDOMChildByName(node,"ds:KeyInfo",elemKeyInfo,false);
  DOMNode* elemEncKey=NULL;
  getDOMChildByName(elemKeyInfo,"EncryptedKey",elemEncKey,false);
  DOMNode* elemCipherValue=NULL;
  getDOMChildByName(elemEncKey,"CipherValue",elemCipherValue,true);
  UINT8* cipherValue=new UINT8[1000];
  UINT32 len=1000;
  if(getDOMElementValue(elemCipherValue,cipherValue,&len)!=E_SUCCESS)
  {
    delete[] cipherValue;
    cipherValue = NULL;
    return E_UNKNOWN;
  }
  CABase64::decode(cipherValue,len,cipherValue,&len);
  if( pRSA->decryptOAEP(cipherValue,cipherValue,&len)!=E_SUCCESS||
      len!=32)
  {
    delete[] cipherValue;
    cipherValue = NULL;
    return E_UNKNOWN;
  }
  CASymCipher *pSymCipher=new CASymCipher();
  pSymCipher->setKey(cipherValue,false);
  pSymCipher->setIVs(cipherValue+16);

  DOMNode* elemCipherData=NULL;
  getDOMChildByName(node,"CipherData",elemCipherData,false);
  getDOMChildByName(elemCipherData,"CipherValue",elemCipherValue,false);
  len=1000;
  if(getDOMElementValue(elemCipherValue,cipherValue,&len)!=E_SUCCESS)
    {
      delete pSymCipher;
      pSymCipher = NULL;
      delete[] cipherValue;
      cipherValue = NULL;
      return E_UNKNOWN;
    }
  if(CABase64::decode(cipherValue,len,cipherValue,&len)!=E_SUCCESS)
    {
      delete pSymCipher;
      pSymCipher = NULL;
      delete[] cipherValue;
      cipherValue = NULL;
      return E_UNKNOWN;
    }
  SINT32 ret=pSymCipher->decrypt1CBCwithPKCS7(cipherValue,cipherValue,&len);
  delete pSymCipher;
  pSymCipher = NULL;
  if(ret!=E_SUCCESS)
    {
      delete[] cipherValue;
      cipherValue = NULL;
      return E_UNKNOWN;
    }
  //now the need to parse the plaintext...
  XERCES_CPP_NAMESPACE::DOMDocument* docPlain=parseDOMDocument(cipherValue,len);
  delete[] cipherValue;
  cipherValue = NULL;
  DOMNode* elemPlainRoot=NULL;
  if(docPlain==NULL)
    return E_UNKNOWN;
  if((elemPlainRoot=docPlain->getDocumentElement())==NULL)
    {
      if (docPlain != NULL)
      {
        docPlain->release();
        docPlain = NULL;
      }
      return E_UNKNOWN;
    }
  elemPlainRoot=doc->importNode(elemPlainRoot,true);
  DOMNode* parent=node->getParentNode();
  if(parent->getNodeType()==DOMNode::DOCUMENT_NODE)
    {
      DOMNode* n=parent->removeChild(node);
      if (n != NULL)
      {
        n->release();
        n = NULL;
      }
      parent->appendChild(elemPlainRoot);
    }
  else
    {
      DOMNode* n=parent->replaceChild(elemPlainRoot,node);
      if (n != NULL)
      {
        n->release();
        n = NULL;
      }
    }
  if (docPlain != NULL)
  {
    docPlain->release();
    docPlain = NULL;
  }
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 encodeXMLEncryptedKey ( UINT8 key,
UINT32  keylen,
UINT8 xml,
UINT32 xmllen,
CAASymCipher pRSA 
)

Definition at line 1201 of file CAUtil.cpp.

References __encryptKey(), E_SUCCESS, len, and XML_ENCODE_KEY_TEMPLATE.

Referenced by CAMiddleMix::processKeyExchange(), and CAFirstMix::processKeyExchange().

{
#define XML_ENCODE_KEY_TEMPLATE "<EncryptedKey><EncryptionMethod Algorithm=\"RSA\"/><CipherData><CipherValue>%s</CipherValue></CipherData></EncryptedKey>"
  UINT8 tmpBuff[1024];
  UINT32 len=1024;
  __encryptKey(key,keylen,tmpBuff,&len,pRSA);
  sprintf((char*)xml,XML_ENCODE_KEY_TEMPLATE,tmpBuff);
  *xmllen=strlen((char*)xml);
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 encodeXMLEncryptedKey ( UINT8 key,
UINT32  keylen,
DOMElement *&  elemRootEncodedKey,
XERCES_CPP_NAMESPACE::DOMDocument *  docOwner,
CAASymCipher pRSA 
)

Definition at line 1212 of file CAUtil.cpp.

References __encryptKey(), createDOMElement(), E_SUCCESS, setDOMElementAttribute(), and setDOMElementValue().

{
  elemRootEncodedKey=createDOMElement(docOwner,"EncryptedKey");
  DOMElement* elem1=createDOMElement(docOwner,"EncryptionMethod");
  setDOMElementAttribute(elem1,"Algorithm",(UINT8*)"RSA");
  elemRootEncodedKey->appendChild(elem1);
  DOMElement* elem2=createDOMElement(docOwner,"CipherData");
  elem1->appendChild(elem2);
  elem1=createDOMElement(docOwner,"CipherValue");
  elem2->appendChild(elem1);
  UINT8 tmpBuff[1024];
  UINT32 tmpLen=1024;
  __encryptKey(key,keylen,tmpBuff,&tmpLen,pRSA);
  setDOMElementValue(elem1,tmpBuff);
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 encryptXMLElement ( DOMNode *  node,
CAASymCipher pRSA 
)

The resulting encrypted xml struct is as follows:

Replaces a DOM element with an encrypted version of this element.

	* <EncryptedData Type='http://www.w3.org/2001/04/xmlenc#Element'>
	*		<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
	*   <ds:KeyInfo xmlns:ds='http://www.w3.org/2000/09/xmldsig#'>
	*			<EncryptedKey>
	*				<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
	*				<CipherData>
	*					<CipherValue>...</CipherValue>
	*				</CipherData>
	*			</EncryptedKey>
	*		</ds:KeyInfo>
	*		<CipherData>
	*			<CipherValue>...</CipherValue>
	*		</CipherData>
	*	</EncryptedData>
	*

Definition at line 1299 of file CAUtil.cpp.

References createDOMElement(), DOM_Output::dumpToMem(), E_SUCCESS, CABase64::encode(), CASymCipher::encrypt1CBCwithPKCS7(), CAASymCipher::encryptOAEP(), getRandom(), setDOMElementValue(), CASymCipher::setIVs(), and CASymCipher::setKey().

Referenced by CALocalProxy::processKeyExchange().

{
  XERCES_CPP_NAMESPACE::DOMDocument* doc=NULL;
  DOMNode* parent=NULL;
  if(node->getNodeType()==DOMNode::DOCUMENT_NODE)
    {
      doc=(XERCES_CPP_NAMESPACE::DOMDocument*)node;
      parent=doc;
      node=doc->getDocumentElement();
    }
  else
    {
      doc=node->getOwnerDocument();
      parent=node->getParentNode();
    }
  DOMElement* elemRoot=createDOMElement(doc,"EncryptedData");
  DOMElement* elemKeyInfo=createDOMElement(doc,"ds:KeyInfo");
  elemRoot->appendChild(elemKeyInfo);
  DOMElement* elemEncKey=createDOMElement(doc,"EncryptedKey");
  elemKeyInfo->appendChild(elemEncKey);
  DOMElement* elemCipherData=createDOMElement(doc,"CipherData");
  elemEncKey->appendChild(elemCipherData);
  DOMElement* elemCipherValue=createDOMElement(doc,"CipherValue");
  elemCipherData->appendChild(elemCipherValue);
  UINT8 key[32];
  getRandom(key,32);
  UINT8* pBuff=new UINT8[1000];
  UINT32 bufflen=255;
  pRSA->encryptOAEP(key,32,pBuff,&bufflen);
  UINT8* pOutBuff=new UINT8[1000];
  UINT32 outbufflen=255;
  CABase64::encode(pBuff,bufflen,pOutBuff,&outbufflen);
  pOutBuff[outbufflen]=0;
  setDOMElementValue(elemCipherValue,pOutBuff);
  delete[] pOutBuff;
  pOutBuff = NULL;
  delete[] pBuff;
  pBuff = NULL;
  CASymCipher *pSymCipher=new CASymCipher();
  pSymCipher->setKey(key,true);
  pSymCipher->setIVs(key+16);
  elemCipherData=createDOMElement(doc,"CipherData");
  elemRoot->appendChild(elemCipherData);
  elemCipherValue=createDOMElement(doc,"CipherValue");
  elemCipherData->appendChild(elemCipherValue);
  UINT8* b=DOM_Output::dumpToMem(node,&bufflen);
  outbufflen=bufflen+1000;
  pOutBuff=new UINT8[outbufflen];
  pSymCipher->encrypt1CBCwithPKCS7(b,bufflen,pOutBuff,&outbufflen);
  delete[] b;
  b = NULL;
  bufflen=outbufflen*3/2+1000;
  pBuff=new UINT8[bufflen];
  CABase64::encode(pOutBuff,outbufflen,pBuff,&bufflen);
  pBuff[bufflen]=0;
  setDOMElementValue(elemCipherValue,pBuff);
  delete[] pOutBuff;
  pOutBuff = NULL;
  delete[] pBuff;
  pBuff = NULL;
  if(parent->getNodeType()==DOMNode::DOCUMENT_NODE)
    {
      DOMNode* n=parent->removeChild(node);
      if (n != NULL)
      {
        n->release();
        n = NULL;
      }
      parent->appendChild(elemRoot);
    }
  else
    {
      DOMNode* n=parent->replaceChild(elemRoot,node);
      if (n != NULL)
      {
        n->release();
        n = NULL;
      }
    }
  return E_SUCCESS;
}

Here is the call graph for this function:

UINT8* encryptXMLElement ( UINT8 inbuff,
UINT32  inlen,
UINT32 outlen,
CAASymCipher pRSA 
)

Encrypts an XML-Element by wrapping it with:

	* \<EncryptedData Type='http://www.w3.org/2001/04/xmlenc#Element'>
	*		\<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
	*   \<ds:KeyInfo xmlns:ds='http://www.w3.org/2000/09/xmldsig#'>
	*			\<EncryptedKey>
	*				\<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
	*				\<CipherData\>
	*					\<CipherValue>...\</CipherValue>
	*				\</CipherData>
	*			\</EncryptedKey>
	*		\</ds:KeyInfo>
	*		\<CipherData>
	*			\<CipherValue>...\</CipherValue>
	*		\</CipherData>
	*	\</EncryptedData>
	*
Returns:
a buffer with the encyrpted element (the caller is responsible for freeing it), or NULL

Definition at line 1401 of file CAUtil.cpp.

References CABase64::encode(), CASymCipher::encrypt1CBCwithPKCS7(), CAASymCipher::encryptOAEP(), getRandom(), CASymCipher::setIVs(), and CASymCipher::setKey().

{
  const char* XML_ENC_TEMPLATE="<EncryptedData><ds:KeyInfo><EncryptedKey><CipherData><CipherValue>%s</CipherValue></CipherData></EncryptedKey></ds:KeyInfo><CipherData><CipherValue>%s</CipherValue></CipherData></EncryptedData>";
  UINT8 key[32];
  getRandom(key,32);
  UINT8 buff[1000];
  UINT32 bufflen=255;
  pRSA->encryptOAEP(key,32,buff,&bufflen);
  UINT8 keyoutbuff[1000];
  UINT32 keyoutbufflen=255;
  CABase64::encode(buff,bufflen,keyoutbuff,&keyoutbufflen);
  keyoutbuff[keyoutbufflen]=0;
  CASymCipher* pSymCipher=new CASymCipher();
  pSymCipher->setKey(key,true);
  pSymCipher->setIVs(key+16);
  UINT32 msgoutbufflen=inlen+1000;
  UINT8* msgoutbuff=new UINT8[msgoutbufflen];
  pSymCipher->encrypt1CBCwithPKCS7(inbuff,inlen,msgoutbuff,&msgoutbufflen);
  delete pSymCipher;
  pSymCipher = NULL;
  UINT32 encmsgoutbufflen=msgoutbufflen*3/2+1000;
  UINT8* encmsgoutbuff=new UINT8[encmsgoutbufflen];
  CABase64::encode(msgoutbuff,msgoutbufflen,encmsgoutbuff,&encmsgoutbufflen);
  delete[] msgoutbuff;
  msgoutbuff = NULL;
  encmsgoutbuff[encmsgoutbufflen]=0;
  msgoutbufflen=encmsgoutbufflen+1000;
  msgoutbuff=new UINT8[msgoutbufflen];
  sprintf((char*)msgoutbuff,XML_ENC_TEMPLATE,keyoutbuff,encmsgoutbuff);
  outlen=strlen((char*)msgoutbuff);
  delete[] encmsgoutbuff;
  encmsgoutbuff = NULL;
  return msgoutbuff;
}

Here is the call graph for this function:

bool equals ( const XMLCh *const  e1,
const char *const  e2 
)
SINT32 getcurrentTime ( timespec &  t)

Gets the current Systemtime in milli seconds.

Parameters:
bnTime- Big Number, in which the current time is placed
Return values:
E_UNSPECIFIED,ifbnTime was NULL
E_UNKNOWN,ifan error occurs
E_SUCCESS,otherwise

Definition at line 225 of file CAUtil.cpp.

References E_SUCCESS.

Referenced by CAChain::addChannel(), CAChain::forceImmediateResponsesInternal(), CAAccountingInstance::handleAccountCertificate_internal(), initRandom(), CAChain::processDownstream(), and CAConditionVariable::wait().

  {
    #ifdef _WIN32
      timeb timebuffer;
      ftime(&timebuffer);
      /* Hack what should be solved better...*/
      t.tv_sec=timebuffer.time;
      t.tv_nsec=timebuffer.millitm*1000000;
      /* end of hack..*/
      return E_SUCCESS;
    #else //we dont use ftime due to a bug in glibc2.0
    //we use gettimeofday() in order to get the millis...
      struct timeval tv;
      gettimeofday(&tv,NULL); //getting millis...
      t.tv_sec=tv.tv_sec;
      t.tv_nsec=tv.tv_usec*1000;
      return E_SUCCESS;
    #endif
  }

Gets the current Systemtime in micros seconds.

Depending on the operating system, the time may not be so accurat.

Parameters:
u64Time- 64 bit Integer, in which the current time is placed
Return values:
E_UNKNOWN,ifan error occurs
E_SUCCESS,otherwise

Definition at line 278 of file CAUtil.cpp.

References E_SUCCESS, and E_UNKNOWN.

Referenced by CAChain::CAChain(), fm_loopReadFromMix(), fm_loopSendToMix(), lm_loopReadFromMix(), lm_loopSendToMix(), CALastMixA::loop(), CAFirstMixB::loop(), CAFirstMixA::loop(), CALastMixB::loop(), CADatabase::measurePerformance(), CAReplayDatabase::measurePerformance(), mm_loopSendToMixAfter(), mm_loopSendToMixBefore(), CAFirstMixA::sendToUsers(), and CAChain::~CAChain().

  {
    #ifdef _WIN32
      timeb timebuffer;
      ftime(&timebuffer);
      /* Hack what should be solved better...*/
      u64Time=((UINT64)timebuffer.time)*1000000+((UINT64)timebuffer.millitm)*1000;
      /* end of hack..*/
      return E_SUCCESS;
    #else //we dont use ftime due to a bug in glibc2.0
    //we use gettimeofday() in order to get the millis...
      struct timeval tv;
      gettimeofday(&tv,NULL); //getting millis...
      #ifdef HAVE_NATIVE_UINT64
        u64Time=((UINT64)tv.tv_sec)*1000000+((UINT64)tv.tv_usec);
        return E_SUCCESS;
      #else
        return E_UNKNOWN;
      #endif
    #endif
  }

Gets the current Systemtime in milli seconds.

Parameters:
u64Time- 64 bit Integer, in which the current time is placed
Return values:
E_UNKNOWN,ifan error occurs
E_SUCCESS,otherwise

Definition at line 250 of file CAUtil.cpp.

References E_SUCCESS, and E_UNKNOWN.

Referenced by CAFirstMixChannelList::add(), CATempIPBlockList::checkIP(), CATempIPBlockList::cleanupThreadMainLoop(), CAFirstMixA::closeConnection(), fm_loopReadFromMix(), CAInfoService::getCascadeHeloXMLAsString(), CACmdLnOptions::getMixXml(), CAInfoService::getStatusXMLAsString(), CAAccountingInstance::handleCostConfirmation_internal(), CATempIPBlockList::insertIP(), lm_loopReadFromMix(), CALastMixA::loop(), CAFirstMixB::loop(), CAHttpClient::parseHTTPHeader(), CAMuxSocket::receive(), CASocket::receiveFullyT(), CASocket::receiveLine(), CAInfoService::sendCascadeHelo(), CASocket::sendFullyTimeOut(), CAInfoService::sendMixHelo(), CAInfoService::sendStatus(), setCurrentTimeMilliesAsDOMAttribute(), CAMix::start(), CAASymCipher::testSpeed(), and CASymCipher::testSpeed().

  {
    #ifdef _WIN32
      timeb timebuffer;
      ftime(&timebuffer);
      /* Hack what should be solved better...*/
      u64Time=((UINT64)timebuffer.time)*1000+((UINT64)timebuffer.millitm);
      /* end of hack..*/
      return E_SUCCESS;
    #else //we dont use ftime due to a bug in glibc2.0
    //we use gettimeofday() in order to get the millis...
      struct timeval tv;
      gettimeofday(&tv,NULL); //getting millis...
      #ifdef HAVE_NATIVE_UINT64
        u64Time=((UINT64)tv.tv_sec)*1000+((UINT64)tv.tv_usec)/1000;
        return E_SUCCESS;
      #else
        return E_UNKNOWN;
      #endif
    #endif
  }
SINT32 getDOMChildByName ( const DOMNode *  pNode,
const char *const  name,
DOMElement *&  child,
bool  deep 
)

Definition at line 430 of file CAUtil.cpp.

References getDOMChildByName().

Referenced by CACmdLnOptions::addVisibleAddresses(), CAMix::appendTermsAndConditionsExtension(), CAMix::checkCompatibility(), decodeXMLEncryptedKey(), decryptXMLElement(), CAFirstMix::doUserLogin_internal(), CAInfoService::getCascadeHeloXMLAsString(), getDOMChildByName(), CAListenerInterface::getInstance(), CACmdLnOptions::getMixXml(), CAAccountingInstance::handleAccountCertificate_internal(), CAAccountingInstance::handleChallengeResponse_internal(), CAFirstMix::handleKeyInfoExtensions(), CAFirstMix::handleTermsAndConditionsExtension(), CAFirstMix::handleTermsAndConditionsLogin(), CAMix::initMixCascadeInfo(), CAAccountingInstance::makeCCRequest(), CAAccountingInstance::makeInitialCCRequest(), CACmdLnOptions::parseInfoServices(), CASignature::parseSignKeyXML(), CAAccountingInstance::prepareCCRequest(), CAReplayCtrlChannelMsgProc::proccessGetTimestamp(), CALocalProxy::processKeyExchange(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), CAFirstMix::processKeyExchange(), CACmdLnOptions::processXmlConfiguration(), CAReplayControlChannel::processXMLMessage(), CAReplayCtrlChannelMsgProc::propagateCurrentReplayTimestamp(), CACmdLnOptions::setAccountingDatabase(), CACmdLnOptions::setAccountingHardLimit(), CACmdLnOptions::setAccountingOptions(), CACmdLnOptions::setAccountingSoftLimit(), CACmdLnOptions::setCascadeNameFromOptions(), CACmdLnOptions::setCertificateOptions(), CACmdLnOptions::setDaemonMode(), CACmdLnOptions::setDynamicMix(), CACmdLnOptions::setGeneralOptions(), CACmdLnOptions::setInfoServices(), CACmdLnOptions::setKeepAliveTraffic(), CACmdLnOptions::setListenerInterfaces(), CACmdLnOptions::setLoggingOptions(), CACmdLnOptions::setMaxUsers(), CACmdLnOptions::setMinCascadeLength(), CACmdLnOptions::setMixCertificateVerification(), CACmdLnOptions::setMixDescription(), CACmdLnOptions::setMixID(), CACmdLnOptions::setMixName(), CACmdLnOptions::setMixType(), CACmdLnOptions::setNetworkOptions(), CACmdLnOptions::setNextMix(), CACmdLnOptions::setNextMixCertificate(), CACmdLnOptions::setNrOfFileDescriptors(), CACmdLnOptions::setOwnCertificate(), CACmdLnOptions::setOwnOperatorCertificate(), CACmdLnOptions::setPaymentInstance(), CACmdLnOptions::setPaymentReminder(), CACmdLnOptions::setPrepaidInterval(), CACmdLnOptions::setPrevMix(), CACmdLnOptions::setPrevMixCertificate(), CACmdLnOptions::setPriceCertificate(), CACmdLnOptions::setRessourceOptions(), CACmdLnOptions::setServerMonitoring(), CACmdLnOptions::setSettleInterval(), CACmdLnOptions::setTargetInterfaces(), CACmdLnOptions::setTermsAndConditions(), CACmdLnOptions::setTermsAndConditionsList(), CACmdLnOptions::setTermsAndConditionsTemplates(), CACmdLnOptions::setTrustedRootCertificates(), CACmdLnOptions::setUserID(), CAXMLCostConfirmation::setValues(), CAXMLBI::setValues(), CAXMLErrorMessage::setValues(), CAXMLPriceCert::setValues(), CASignature::setVerifyKey(), CAMultiSignature::signXML(), CACertStore::verifyMixCert(), CAMultiSignature::verifyXML(), and CASignature::verifyXML().

  {
    return getDOMChildByName(pNode,name,(DOMNode*&)child,deep);
  }

Here is the call graph for this function:

SINT32 getDOMChildByName ( const DOMNode *  pNode,
const XMLCh *const  name,
DOMNode *&  a_child,
bool  deep 
)

Definition at line 434 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, equals(), and getDOMChildByName().

  {
    a_child=NULL;
    if(pNode==NULL)
      return E_UNKNOWN;
    DOMNode *pChild=pNode->getFirstChild();
    while(pChild!=NULL)
      {
        if(XMLString::equals(pChild->getNodeName(),name))
          {
            a_child=pChild;
            return E_SUCCESS;
          }
        if(deep)
          {
            if(getDOMChildByName(pChild,name,a_child,deep)==E_SUCCESS)
              return E_SUCCESS;
          }
        pChild=pChild->getNextSibling();
      }
    return E_UNKNOWN;
  }

Here is the call graph for this function:

SINT32 getDOMChildByName ( const DOMNode *  pNode,
const char *const  name,
DOMNode *&  a_child,
bool  deep 
)

Definition at line 457 of file CAUtil.cpp.

References E_UNKNOWN, and getDOMChildByName().

  {
    a_child=NULL;
    if(pNode==NULL)
      return E_UNKNOWN;
    XMLCh* tmpName=XMLString::transcode((const char * const)name);
    SINT32 ret=getDOMChildByName(pNode,tmpName,a_child,deep);
    XMLString::release(&tmpName);
    return ret;
  }

Here is the call graph for this function:

SINT32 getDOMElementAttribute ( const DOMNode *const  elem,
const char *  attrName,
UINT8 value,
UINT32 len 
)
SINT32 getDOMElementAttribute ( const DOMNode *const  elem,
const char *  attrName,
SINT32 value 
)

Definition at line 769 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, getDOMElementAttribute(), and len.

  {
    UINT8 val[50];
    UINT32 len=50;
    if(getDOMElementAttribute(elem,attrName,val,&len)!=E_SUCCESS)
      return E_UNKNOWN;
    *value=atol((char*)val);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 getDOMElementAttribute ( const DOMNode *const  elem,
const char *  attrName,
SINT64 value 
)

Definition at line 993 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, getDOMElementAttribute(), len, and parseS64().

{
  UINT8 val[50];
  UINT32 len=50;
  if(getDOMElementAttribute(elem,attrName,val,&len)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  if(parseS64(val,value)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementAttribute ( const DOMNode *const  elem,
const char *  attrName,
UINT64 value 
)

Definition at line 1008 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, getDOMElementAttribute(), len, and parseU64().

{
  UINT8 val[50];
  UINT32 len=50;
  if(getDOMElementAttribute(elem,attrName,val,&len)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  if(parseU64(val,value)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementAttribute ( const DOMNode *const  elem,
const char *  attrName,
UINT32 value 
)

Definition at line 1023 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, getDOMElementAttribute(), and len.

{
  UINT8 val[50];
  UINT32 len=50;
  if(getDOMElementAttribute(elem,attrName,val,&len)!=E_SUCCESS)
    return E_UNKNOWN;
  long l=atol((char*)val);
  if(l<0)
    return E_UNKNOWN;
  value=(UINT32)l;
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementAttribute ( const DOMNode *const  elem,
const char *  attrName,
bool &  value 
)

Definition at line 1036 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, E_UNSPECIFIED, getDOMElementAttribute(), len, STR_VALUE_FALSE, and STR_VALUE_TRUE.

{
  UINT8 val[50];
  UINT32 len=50;
  if(getDOMElementAttribute(elem,attrName,val,&len) != E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  SINT32 ret = E_UNSPECIFIED;
  if(strncasecmp((char*)val, STR_VALUE_TRUE, strlen(STR_VALUE_TRUE)) == 0)
  {
    value = true;
    ret = E_SUCCESS;
  }
  else if(strncasecmp((char*)val, STR_VALUE_FALSE, strlen(STR_VALUE_FALSE)) == 0)
  {
    value = false;
    ret = E_SUCCESS;
  }
  return ret;
}

Here is the call graph for this function:

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.

If there is no text node len is set to 0.

TODO: Why is elem a DOM_Node and not a DOM_Element here?

Parameters:
elemthe element which has a text node under it
valuea buffer that gets the text value
valuelenon call contains the buffer size, on return contains the number of bytes copied
Return values:
E_SPACEif the buffer is too small
E_UNKNOWNif the element is NULL
E_SUCCESSotherwise

Definition at line 715 of file CAUtil.cpp.

References ASSERT, E_SPACE, E_SUCCESS, and E_UNKNOWN.

Referenced by CACmdLnOptions::addVisibleAddresses(), CACertificate::decode(), decodeXMLEncryptedKey(), decryptXMLElement(), CAFirstMix::doUserLogin_internal(), getDOMElementValue(), CAListenerInterface::getInstance(), CAASymCipher::getKeyPart(), CAAccountingInstance::handleAccountCertificate_internal(), CAAccountingInstance::handleChallengeResponse_internal(), CACmdLnOptions::parseInfoServices(), CALocalProxy::processKeyExchange(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), CAFirstMix::processKeyExchange(), CACmdLnOptions::processXmlConfiguration(), CAReplayControlChannel::processXMLMessage(), CACmdLnOptions::setAccountingDatabase(), CACmdLnOptions::setAccountingHardLimit(), CACmdLnOptions::setAccountingSoftLimit(), CACmdLnOptions::setCascadeNameFromOptions(), CACmdLnOptions::setDaemonMode(), CACmdLnOptions::setDynamicMix(), CACmdLnOptions::setInfoServices(), CACmdLnOptions::setKeepAliveTraffic(), CACmdLnOptions::setLoggingOptions(), CACmdLnOptions::setMaxUsers(), CACmdLnOptions::setMixCertificateVerification(), CACmdLnOptions::setMixID(), CACmdLnOptions::setMixName(), CACmdLnOptions::setMixType(), CACmdLnOptions::setNrOfFileDescriptors(), CACmdLnOptions::setPaymentReminder(), CACmdLnOptions::setPrepaidInterval(), CACmdLnOptions::setRessourceOptions(), CACmdLnOptions::setServerMonitoring(), CACmdLnOptions::setSettleInterval(), CASignature::setSignKey(), CACmdLnOptions::setTargetInterfaces(), CACmdLnOptions::setTermsAndConditionsTemplates(), CACmdLnOptions::setUserID(), CAXMLCostConfirmation::setValues(), CAXMLBI::setValues(), CAXMLErrorMessage::setValues(), CAXMLPriceCert::setValues(), CASignature::setVerifyKey(), CAMultiSignature::verifyXML(), and CASignature::verifyXML().

  {
    ASSERT(value!=NULL,"Value is null");
    ASSERT(valuelen!=NULL,"ValueLen is null");
    ASSERT(pElem!=NULL,"Element is NULL");
    if(pElem==NULL)
      return E_UNKNOWN;
    DOMNode*  pText=pElem->getFirstChild();
    UINT32 spaceLeft=*valuelen;
    *valuelen=0;
    while(pText!=NULL)
      {
        if(pText->getNodeType()==DOMNode::TEXT_NODE)
          {
            const XMLCh* str=pText->getNodeValue();
            char* tmpStr=XMLString::transcode(str);
            UINT32 tmpStrLen=strlen(tmpStr);
            if(tmpStrLen>=spaceLeft)
              {
                *valuelen=tmpStrLen+1;
                XMLString::release(&tmpStr);
                return E_SPACE;
              }
            memcpy(value+(*valuelen),tmpStr,tmpStrLen);
            *valuelen+=tmpStrLen;
            spaceLeft-=tmpStrLen;
            XMLString::release(&tmpStr);
          }
        pText=pText->getNextSibling();
      }
    value[*valuelen]=0;
    return E_SUCCESS;
  }
SINT32 getDOMElementValue ( const DOMElement *const  pElem,
UINT32 value 
)

Definition at line 1093 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, E_UNKNOWN, and getDOMElementValue().

{
  ASSERT(value!=NULL,"Value is null");
  ASSERT(pElem!=NULL,"Element is NULL");
  UINT8 buff[255];
  UINT32 buffLen=255;
  if(getDOMElementValue(pElem,buff,&buffLen)!=E_SUCCESS)
    return E_UNKNOWN;
  *value=atol((char*)buff);

  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementValue ( const DOMElement *const  pElem,
SINT32 value 
)

Definition at line 1106 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, E_UNKNOWN, and getDOMElementValue().

{
  ASSERT(value!=NULL,"Value is null");
  ASSERT(pElem!=NULL,"Element is NULL");
  UINT8 buff[255];
  UINT32 buffLen=255;
  if(getDOMElementValue(pElem,buff,&buffLen)!=E_SUCCESS)
    return E_UNKNOWN;
  *value=atol((char*)buff);

  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementValue ( const DOMElement *const  pElem,
double *  value 
)

Definition at line 1119 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, E_UNKNOWN, and getDOMElementValue().

{
  ASSERT(value!=NULL,"Value is null");
  ASSERT(pElem!=NULL,"Element is NULL");
  UINT8 buff[255];
  UINT32 buffLen=255;
  if(getDOMElementValue(pElem,buff,&buffLen)!=E_SUCCESS)
    return E_UNKNOWN;
  *value=atof((char*)buff);

  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementValue ( const DOMElement *const  pElem,
UINT32 value,
UINT32  defaultValue 
)

Gets the value from an DOM-Element as UINT32.

If an error occurs, the default value is returned.

Definition at line 1133 of file CAUtil.cpp.

References E_SUCCESS, and getDOMElementValue().

{
  UINT32 v;
  if(getDOMElementValue(pElem,&v)!=E_SUCCESS)
    {
    value=defaultValue;
    }
  else
    value=v;
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementValue ( const DOMNode *const  pElem,
UINT64 value 
)

Definition at line 1145 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, E_UNKNOWN, getDOMElementValue(), and parseU64().

{
  ASSERT(pElem!=NULL, "Element is NULL");
  UINT8 buf[256];
  UINT32 bufLen = 256;
  if(getDOMElementValue(pElem,buf,&bufLen)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  if(parseU64(buf, value)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }

  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementValue ( const DOMElement *const  pElem,
SINT64 value 
)

Definition at line 1162 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, E_UNKNOWN, getDOMElementValue(), and parseS64().

{
  ASSERT(pElem!=NULL, "Element is NULL");
  UINT8 buf[256];
  UINT32 bufLen = 256;
  if(getDOMElementValue(pElem,buf,&bufLen)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  if(parseS64(buf, value)!=E_SUCCESS)
  {
    return E_UNKNOWN;
  }
  return E_SUCCESS;
}

Here is the call graph for this function:

SINT32 getDOMElementValue ( const DOMElement *const  pElem,
UINT16 value 
)

Definition at line 1179 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, and getDOMElementValue().

{
  UINT32 tmp;
  if(getDOMElementValue(pElem,&tmp)!=E_SUCCESS)
    return E_UNKNOWN;
  if(tmp>0xFFFF)
    return E_UNKNOWN;
  *value=(UINT16)tmp;
  return E_SUCCESS;
}

Here is the call graph for this function:

DOMNodeList* getElementsByTagName ( DOMElement *  pElem,
const char *const  name 
)
SINT32 getLastDOMChildByName ( const DOMNode *  pNode,
const char *const  name,
DOMElement *&  a_child 
)

Definition at line 876 of file CAUtil.cpp.

References getLastDOMChildByName().

Referenced by getLastDOMChildByName(), and CAMix::initMixCascadeInfo().

  {
    return getLastDOMChildByName(pNode,name,(DOMNode*&)a_child);
  }

Here is the call graph for this function:

SINT32 getLastDOMChildByName ( const DOMNode *  pNode,
const char *const  name,
DOMNode *&  a_child 
)

Definition at line 1058 of file CAUtil.cpp.

References E_UNKNOWN, and getLastDOMChildByName().

{
  a_child=NULL;
  if(pNode==NULL)
    return E_UNKNOWN;
  XMLCh* tmpName=XMLString::transcode((const char * const)name);
  SINT32 ret=getLastDOMChildByName(pNode,tmpName,a_child);
  XMLString::release(&tmpName);
  return ret;
}

Here is the call graph for this function:

SINT32 getLastDOMChildByName ( const DOMNode *  pNode,
const XMLCh *const  name,
DOMNode *&  a_child 
)

Definition at line 1069 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, and equals().

{
  a_child=NULL;
  if(pNode==NULL)
    {
      return E_UNKNOWN;
    }
  DOMNode* pChild;
  pChild=pNode->getLastChild();
  while(pChild!=NULL)
    {
      if(XMLString::equals(pChild->getNodeName(),name))
        {
          a_child = pChild; // found a child
          return E_SUCCESS;
        }
      pChild=pChild->getPreviousSibling();
    }
  return E_UNKNOWN;
}

Here is the call graph for this function:

Definition at line 415 of file CAUtil.cpp.

Referenced by CAFirstMix::clean(), and CAFirstMixB::loop().

  {
#ifndef _WIN32
    struct rusage usage_self;
    if(getrusage(RUSAGE_SELF,&usage_self)==-1)
      return 0;
    struct rusage usage_children;
    if(getrusage(RUSAGE_CHILDREN,&usage_children)==-1)
      return 0;
    return usage_self.ru_idrss+usage_children.ru_idrss;
#else
  return 0;
#endif
  }
SINT32 getNodeName ( const DOMNode *const  pElem,
UINT8 value,
UINT32 valuelen 
)

Definition at line 676 of file CAUtil.cpp.

References ASSERT, E_SPACE, E_SUCCESS, and E_UNKNOWN.

Referenced by CAMix::checkCompatibility().

{
  ASSERT(value!=NULL,"Value is null");
  ASSERT(valuelen!=NULL,"ValueLen is null");
  ASSERT(pElem!=NULL,"Element is NULL");
  if(pElem==NULL)
    return E_UNKNOWN;

  UINT32 spaceLeft=*valuelen;
  *valuelen=0;
  const XMLCh* str=pElem->getNodeName();
  char* tmpStr=XMLString::transcode(str);
  UINT32 tmpStrLen=strlen(tmpStr);
  if(tmpStrLen>=spaceLeft)
    {
      *valuelen=tmpStrLen+1;
      XMLString::release(&tmpStr);
      return E_SPACE;
    }
  memcpy(value+(*valuelen),tmpStr,tmpStrLen);
  *valuelen+=tmpStrLen;
  XMLString::release(&tmpStr);
  value[*valuelen] = 0;
  return E_SUCCESS;
}
SINT32 getRandom ( UINT32 val)

Gets 32 random bits.

Parameters:
val- on return the bits are random
Return values:
E_UNKNOWN,ifan error occured
E_SUCCESS,ifsuccessful

Definition at line 344 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, and E_UNKNOWN.

Referenced by CASocketList::add(), CAMiddleMixChannelList::add(), CAFirstMixChannelList::add(), CAFirstMixChannelList::addChannel(), CAIPList::CAIPList(), CAPool::CAPool(), CAFirstMixA::closeConnection(), CAChainTable::createEntry(), encryptXMLElement(), CADatabase::fill(), CAReplayDatabase::fill(), fm_loopReadFromMix(), fm_loopSendToMix(), CAAccountingInstance::handleAccountCertificate_internal(), lm_loopReadFromMix(), lm_loopSendToMix(), CALastMixA::loop(), CAFirstMixB::loop(), CAFirstMixA::loop(), CALastMixB::loop(), CALocalProxy::loop(), CADatabase::measurePerformance(), CAReplayDatabase::measurePerformance(), mm_loopReadFromMixAfter(), mm_loopReadFromMixBefore(), mm_loopSendToMixAfter(), mm_loopSendToMixBefore(), CAFirstMixA::notifyAllUserChannels(), CAMsg::openEncryptedLog(), CAPool::pool(), CAChain::processDownstream(), CALocalProxy::processKeyExchange(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), CAFirstMix::processKeyExchange(), CAReplayDatabase::test(), CAMiddleMixChannelList::test(), CAQueue::test(), CALastMixChannelList::test(), CAASymCipher::testSpeed(), and CASymCipher::testSpeed().

  {
    ASSERT(val!=NULL,"VAL should be not NULL");
    if(RAND_bytes((UINT8*)val,4)!=1&&
       RAND_pseudo_bytes((UINT8*)val,4)<0)
        return E_UNKNOWN;
    return E_SUCCESS;
  }
SINT32 getRandom ( UINT64 val)

Definition at line 353 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, and E_UNKNOWN.

  {
    ASSERT(val!=NULL,"VAL should be not NULL");
    if(RAND_bytes((UINT8*)val,sizeof(UINT64))!=1&&
       RAND_pseudo_bytes((UINT8*)val,sizeof(UINT64))<0)
        return E_UNKNOWN;
    return E_SUCCESS;
  }
SINT32 getRandom ( UINT8 buff,
UINT32  len 
)

Gets some random bytes.

Parameters:
buff- buff which is filled with randomness
len- number of bytes requested
Return values:
E_UNKNOWN,ifan error occured
E_SUCCESS,ifsuccessful

Definition at line 368 of file CAUtil.cpp.

References ASSERT, E_SUCCESS, and E_UNKNOWN.

  {
    ASSERT(buff!=NULL,"BUFF should be not NULL")
    if(RAND_bytes(buff,len)!=1&&
       RAND_pseudo_bytes(buff,len)<0)
      return E_UNKNOWN;
    return E_SUCCESS;
  }
SINT32 getSignatureElements ( DOMNode *  parent,
DOMNode **  signatureNodes,
UINT32 length 
)

Definition at line 468 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, and equals().

Referenced by CACertStore::verifyMixCert(), and CAMultiSignature::verifyXML().

{
  if(parent == NULL)
  {
    return E_UNKNOWN;
  }

  DOMNode* child = parent->getFirstChild();
  UINT32 count = 0;

  while(child != NULL)
  {
    if(XMLString::equals(child->getNodeName(), XMLString::transcode("Signature")))
    {
      if(count < *length)
      {
        signatureNodes[count] = child;
        count++;
      }
      else
      {
        return E_UNKNOWN;
      }
    }
    child = child->getNextSibling();
  }
  *length = count;

  return E_SUCCESS;
}

Here is the call graph for this function:

UINT8* getTermsAndConditionsTemplateRefId ( DOMNode *  tcTemplateRoot)

Definition at line 824 of file CAUtil.cpp.

References E_SUCCESS, getDOMElementAttribute(), TMP_BUFF_SIZE, TMP_DATE_SIZE, and TMP_LOCALE_SIZE.

Referenced by CAMix::appendTermsAndConditionsExtension(), CAFirstMix::getTermsAndConditionsTemplate(), and CACmdLnOptions::setTermsAndConditionsTemplates().

{
  UINT32 tmpTypeLen = TMP_BUFF_SIZE;
  UINT8 tmpType[TMP_BUFF_SIZE];

  UINT32 tmpLocaleLen = TMP_LOCALE_SIZE;
  UINT8 tmpLocale[TMP_LOCALE_SIZE];

  UINT32 tmpDateLen = TMP_DATE_SIZE;
  UINT8 tmpDate[TMP_DATE_SIZE];
  memset(tmpDate, 0, TMP_DATE_SIZE);
  memset(tmpLocale, 0, TMP_LOCALE_SIZE);
  memset(tmpType, 0, TMP_BUFF_SIZE);

  //TODO replace magic strings
  if(getDOMElementAttribute(tcTemplateRoot, "type", tmpType, &tmpTypeLen) != E_SUCCESS)
  {
    return NULL;
  }
  else if(getDOMElementAttribute(tcTemplateRoot, "locale", tmpLocale, &tmpLocaleLen) != E_SUCCESS)
  {
    return NULL;
  }
  else if(getDOMElementAttribute(tcTemplateRoot, "date", tmpDate, &tmpDateLen) != E_SUCCESS)
  {
    return NULL;
  }
  if( (tmpTypeLen == 0) ||
    (tmpLocaleLen == 0) ||
    (tmpDateLen) == 0)
  {
    return NULL;
  }
  //reserve 2 more chars for the both underlines between the fields ...
  size_t templateRefIdLen = tmpTypeLen+tmpLocaleLen+tmpDateLen+2;
  //... and 1 more for zero termination.
  char *templateRefId = new char[templateRefIdLen+1];
  memset(templateRefId, 0, templateRefIdLen+1);
  snprintf(templateRefId, templateRefIdLen+1, "%s_%s_%s", (char *) tmpType, (char *) tmpLocale, (char *) tmpDate);

  return (UINT8 *) templateRefId;
}

Here is the call graph for this function:

void initDOMParser ( )

Definition at line 628 of file CAUtil.cpp.

Referenced by CALibProxytest::init().

{
  theParseDOMDocumentLock = new CAMutex();
  theDOMParser = new XercesDOMParser();
}

Definition at line 300 of file CAUtil.cpp.

References E_SUCCESS, and getcurrentTime().

Referenced by CALibProxytest::init(), CADatabase::measurePerformance(), and CAReplayDatabase::measurePerformance().

  {
    #if _WIN32
      RAND_screen();
    #else
      #ifndef __linux
        unsigned char randbuff[255];
        getcurrentTime(*((timespec*)randbuff));
        RAND_seed(randbuff,sizeof(randbuff));
      #endif
    #endif
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 integrateDOMNode ( const DOMNode *  srcNode,
DOMNode *  dstNode,
bool  recursive,
bool  replace 
)

integrates the source node in the destination Node.

TODO 1. test for XERCES >= 3.0.1 2. specification

Definition at line 504 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, equals(), INTEGRATE_NOT_ALLOWED_POSITIONS, integrateDOMNode(), and CAMsg::printMsg().

Referenced by integrateDOMNode(), and CACmdLnOptions::setTermsAndConditionsList().

{
  if( (srcNode->getNodeType() != DOMNode::ELEMENT_NODE) ||
    (dstNode->getNodeType() != DOMNode::ELEMENT_NODE) )
  {
    return E_UNKNOWN;
  }

  DOMNodeList *srcList = srcNode->getChildNodes();
  XERCES_CPP_NAMESPACE::DOMDocument *srcOwnerDoc = srcNode->getOwnerDocument();
  XERCES_CPP_NAMESPACE::DOMDocument *dstOwnerDoc = dstNode->getOwnerDocument();

  short int pos =
#if _XERCES_VERSION >= 30001
    srcNode->compareDocumentPosition(dstNode);
#else
    srcNode->compareTreePosition(dstNode);
#endif
  if( (pos & INTEGRATE_NOT_ALLOWED_POSITIONS)  )
  {
    CAMsg::printMsg(LOG_ERR,"integrate impossible due to illegal tree positions, (pos: 0x%x)\n", pos);
    return E_UNKNOWN;
  }

  if(srcList->getLength() == 0)
  {
    return E_SUCCESS;
  }

  DOMElement *srcElem = (DOMElement *) srcNode;
  DOMElement *dstElem = (DOMElement *) dstNode;

  DOMNode *currSrcChild = NULL;
  XMLCh** nodeNames=new XMLCh*[srcList->getLength()];
  memset(nodeNames,0,sizeof(XMLCh*)*srcList->getLength());
  UINT32 nodeNamesIndex = 0;
  XMLCh *currSrcChildName = NULL;

  DOMNodeList *currSrcChildren = NULL;
  DOMNodeList *currDstChildren = NULL;
  bool nodeAlreadyFinished = false;

  for(XMLSize_t i = 0; i < srcList->getLength(); i++)
  {
    currSrcChild = srcList->item(i);
    if( currSrcChild->getNodeType() == DOMNode::ELEMENT_NODE )
    {
      nodeAlreadyFinished = false;
      currSrcChildName = (XMLCh *) ((DOMElement *) currSrcChild)->getTagName();
      /*UINT8 *tn = (UINT8 *) XMLString::transcode(currSrcChildName);
      CAMsg::printMsg(LOG_DEBUG,"handle %s\n", tn);
      XMLString::release(&tn);*/
      for(UINT32 i = 0; i < nodeNamesIndex; i++ )
      {
        if(XMLString::equals(currSrcChildName, nodeNames[i]))
        {
          nodeAlreadyFinished = true;
          break;
        }
      }

      if(nodeAlreadyFinished)
      {
        continue;
      }
      currDstChildren = dstElem->getElementsByTagName(currSrcChildName);
      currSrcChildren = srcElem->getElementsByTagName(currSrcChildName);

      for(XMLSize_t j = 0;
        j < currSrcChildren->getLength(); j++ )
      {
        if(j >= currDstChildren->getLength())
        {
          if( (dstOwnerDoc != NULL) && (srcOwnerDoc != dstOwnerDoc) )
          {
            dstNode->appendChild(dstOwnerDoc->importNode(currSrcChildren->item(j), true));
          }
          else
          {
            dstNode->appendChild(currSrcChildren->item(j)->cloneNode(true));
          }
        }
        else if(replace)
        {
          if( (dstOwnerDoc != NULL) && (srcOwnerDoc != dstOwnerDoc) )
          {
            dstElem->replaceChild(
              dstOwnerDoc->importNode(currSrcChildren->item(j),true),
              currDstChildren->item(j));
          }
          else
          {
            dstElem->replaceChild(
              dstOwnerDoc->cloneNode(currSrcChildren->item(j)),
              currDstChildren->item(j));
          }
          continue;
        }
        else if(recursive)
        {
          if(currSrcChildren->item(j)->hasChildNodes() )
          {
            integrateDOMNode(currSrcChildren->item(j), currDstChildren->item(j), true, false);
          }
        }
        nodeNames[nodeNamesIndex++] = currSrcChildName;
      }
    }
  }
  delete[] nodeNames;
  return E_SUCCESS;
}

Here is the call graph for this function:

void logMemoryUsage ( )

Log information about the current memory (heap) usage.

Definition at line 176 of file CAUtil.cpp.

References CAMsg::printMsg().

Referenced by fm_loopLog(), and lm_loopLog().

  {
#ifdef HAVE_SBRK
    CAMsg::printMsg(LOG_DEBUG,"Memory consumption reported by sbrk(): %u\n",(long)((char*)sbrk(0)-internal_sbrk_start));
#endif
#ifdef HAVE_MALLINFO
    struct mallinfo malli=mallinfo();
    //memset(&malli,0,sizeo(malli));
    CAMsg::printMsg(LOG_DEBUG,"Memory consumption reported by mallinfo():\n");
    CAMsg::printMsg(LOG_DEBUG,"\t Total size of memory allocated with sbrk() by malloc() [bytes]: %i\n",malli.arena);
    CAMsg::printMsg(LOG_DEBUG,"\t Number of chunks not in use: %i\n",malli.ordblks);
    CAMsg::printMsg(LOG_DEBUG,"\t Total number of chunks allocated with mmap(): %i\n",malli.hblks);
    CAMsg::printMsg(LOG_DEBUG,"\t Total size of memory allocated with mmap() [byte]: %i\n",malli.hblkhd);
    CAMsg::printMsg(LOG_DEBUG,"\t Total size of memory occupied by chunks handed out by malloc(): %i\n",malli.uordblks);
    CAMsg::printMsg(LOG_DEBUG,"\t Total size of memory occupied by free (not in use) chunks: %i\n",malli.fordblks);
    CAMsg::printMsg(LOG_DEBUG,"\t Size of the top-most releasable chunk that normally borders the end of the heap: %i\n",malli.keepcost);
#endif
  }

Here is the call graph for this function:

SINT32 memtrim ( UINT8 dest,
const UINT8 src,
UINT32  size 
)

Removes leading and ending whitespaces (chars<=32) from a byte array.

Parameters:
srcinput byte array
destoutput byte array
sizesize of the input byte array
Return values:
E_UNSPECIFIED,ifdest was NULL
Returns:
size of output otherwise
Todo:
replace UINT32 size with SINT32 size

Definition at line 126 of file CAUtil.cpp.

References E_UNSPECIFIED.

  {
    if(src==NULL||size==0)
      return 0;
    if(dest==NULL)
      return E_UNSPECIFIED;
    UINT32 start=0;
    UINT32 end=size-1;
    while(start<=end&&src[start]<=32)
      start++;
    if(start>end) //empty string....
      return 0;
    while(end>start&&src[end]<=32)
      end--;
    size=(end+1)-start;
    memmove(dest,src+start,size);
    return (SINT32)size;
  }

Sleeps ms milliseconds.

Definition at line 378 of file CAUtil.cpp.

References E_SUCCESS.

Referenced by fm_loopAcceptUsers(), fm_loopReadFromMix(), fml_loopDelayBuckets(), lm_loopReadFromMix(), CALastMixA::loop(), CAFirstMixB::loop(), CAFirstMixA::loop(), CALastMixB::loop(), main(), mm_loopReadFromMixAfter(), mm_loopReadFromMixBefore(), my_terminate(), CAHttpClient::parseHTTPHeader(), producer(), CAClientSocket::receiveFully(), and CASocket::sendTimeOut().

  {//Do not us usleep for this --> because it doesnt seam to work on irix, multithreaded
    #ifdef _WIN32
      Sleep(ms);
    #else
      struct timespec req;
      struct timespec rem;
      req.tv_sec=ms/1000;
      req.tv_nsec=(ms%1000)*1000000;
      while(nanosleep(&req,&rem)==-1)
        {
          req.tv_sec=rem.tv_sec;
          req.tv_nsec=rem.tv_nsec;
        }
    #endif
    return E_SUCCESS;
  }
XERCES_CPP_NAMESPACE::DOMDocument* parseDOMDocument ( const UINT8 *const  buff,
UINT32  len 
)
XERCES_CPP_NAMESPACE::DOMDocument* parseDOMDocument ( const UINT8 *const  pathOrURL)

parses a file via path or URL

Definition at line 649 of file CAUtil.cpp.

References CAMutex::lock(), and CAMutex::unlock().

{
  theParseDOMDocumentLock->lock();

  theDOMParser->parse((const char *const) pathOrURL);
  XERCES_CPP_NAMESPACE::DOMDocument* ret=NULL;
  if(theDOMParser->getErrorCount()==0)
  {
    ret=theDOMParser->adoptDocument();
  }
  theParseDOMDocumentLock->unlock();
  return ret;
}

Here is the call graph for this function:

SINT32 parseS64 ( const UINT8 str,
SINT64 value 
)

Parses a 64bit signed integer.

Note: If the value is out of range or not parseable an erro is returned.

Todo:
code if we do not have native UINT64

Definition at line 1617 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, and len.

Referenced by getDOMElementAttribute(), and getDOMElementValue().

{
  #ifdef  HAVE_ATOLL
    value = atoll((const char *) str);
    return E_SUCCESS;
  #else
    #ifdef HAVE_NATIVE_UINT64
        if (str == NULL)
        {
          return E_UNKNOWN;
        }
        UINT32 len=strlen((char*)str);
        if (len < 1)
        {
          return E_UNKNOWN;
        }
        SINT64 s64 = 0;
        for (UINT32 i = 0; i < len; i++)
        {
          UINT8 c=str[i];
          if (c >= '0' && c <= '9')
          {
            s64 *= 10;
            s64 += c - '0';
          }
          else if (i != 0 || str[i] != '+'||str[i]!='-')
          {
            return E_UNKNOWN;
          }
        }
        if(str[0]=='-')
          value=-s64;
        else
          value = s64;
        return E_SUCCESS;
    #else
      #warning parseS64() is not implemented for platforms without native INT64 support!!!

      return E_UNKNOWN;
    #endif
  #endif
}
SINT32 parseU64 ( const UINT8 str,
UINT64 value 
)

Parses a 64bit unsigned integer.

Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in milliseconds since the epoch.

Note: If the value is out of range or not parseable an erro is returned.

Todo:
code if we do not have native UINT64

Definition at line 1573 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, and len.

Referenced by getDOMElementAttribute(), and getDOMElementValue().

{
  #ifdef  HAVE_STRTOULL
    value = strtoull((const char *) str, NULL, 0);
    return E_SUCCESS;
  #else
    #ifdef HAVE_NATIVE_UINT64
        if (str == NULL)
        {
          return E_UNKNOWN;
        }
        UINT32 len=strlen((char*)str);
        if (len < 1)
        {
          return E_UNKNOWN;
        }
        UINT64 u64 = 0;
        for (UINT32 i = 0; i < len; i++)
        {
          UINT8 c=str[i];
          if (c >= '0' && c <= '9')
          {
            u64 *= 10;
            u64 += c - '0';
          }
          else if (i != 0 || str[i] != '+')
          {
            return E_UNKNOWN;
          }
        }
        value = u64;
        return E_SUCCESS;
    #else
      #warning parseU64() is not implemented for platforms without native UINT64 support!!!

      return E_UNKNOWN;
    #endif
  #endif
}
UINT8* readFile ( const UINT8 *const  name,
UINT32 size 
)

Definition at line 1543 of file CAUtil.cpp.

References filesize32().

Referenced by CAMiddleMix::init(), CALastMix::init(), and CAFirstMix::processKeyExchange().

{
  int handle=open((char*)name,O_BINARY|O_RDONLY);
  if(handle<0)
    return NULL;
  *size=filesize32(handle);
  UINT8* buff=new UINT8[*size];
  read(handle,buff,*size);
  close(handle);
  return buff;
}

Here is the call graph for this function:

SINT32 readPasswd ( UINT8 buff,
UINT32  len 
)

Read a passwd (i.e.

without echoing the chars typed in)

Definition at line 1660 of file CAUtil.cpp.

References E_SUCCESS, and flags.

Referenced by CACmdLnOptions::setOwnCertificate().

{
  if(len==0)
    return E_SUCCESS;

#ifndef _WIN32
  termios tmpTermios;
  UINT32 flags;
  bool bRestore=true;
  if(tcgetattr(STDIN_FILENO,&tmpTermios)!=0)
    {
      bRestore=false;
    }
  flags=tmpTermios.c_lflag;
  tmpTermios.c_lflag&=~(ECHO);
  if(bRestore)
    tcsetattr(STDIN_FILENO,TCSAFLUSH,&tmpTermios);
#endif

  UINT32 i=0;
  for(i=0;i<len-1;i++)
    {
#ifdef _WIN32
      int c=::getch();
#else
      int c=getchar();
#endif
      if(c<=0||c=='\r'||c=='\n')
        break;
      buff[i]=(UINT8)c;
    }
  buff[i]=0;

#ifndef _WIN32
  tmpTermios.c_lflag=flags;
  if(bRestore)
    tcsetattr(STDIN_FILENO,TCSAFLUSH,&tmpTermios);
#endif
  return E_SUCCESS;
}
void releaseDOMParser ( )

Definition at line 663 of file CAUtil.cpp.

References CAMutex::lock(), theDOMParser, theParseDOMDocumentLock, and CAMutex::unlock().

Referenced by CALibProxytest::cleanup().

Here is the call graph for this function:

SINT32 saveFile ( const UINT8 *const  name,
const UINT8 *const  buff,
UINT32  buffSize 
)

Definition at line 1555 of file CAUtil.cpp.

References E_SUCCESS, and E_UNKNOWN.

Referenced by CAMiddleMix::init(), CALastMix::init(), and CAFirstMix::processKeyExchange().

{
  int handle=open((char*)name,O_BINARY|O_WRONLY|O_CREAT);
  if(handle<0)
    return E_UNKNOWN;
  if(write(handle,buff,buffSize)!=buffSize)
    {
      close(handle);
      return E_UNKNOWN;
  }
  close(handle);
  return E_SUCCESS;
}

Definition at line 807 of file CAUtil.cpp.

References E_SUCCESS, E_UNKNOWN, getcurrentTimeMillis(), print64(), setDOMElementAttribute(), and UNIVERSAL_ATTRIBUTE_LAST_UPDATE.

Referenced by CAInfoService::getOperatorTnCsAsStrings().

{
    if( pElem == NULL || pElem->getNodeType() != DOMNode::ELEMENT_NODE )
    {
      return E_UNKNOWN;
    }
    UINT64 currentMillis;
    getcurrentTimeMillis(currentMillis);
    UINT8 tmpStrCurrentMillis[50];
    print64(tmpStrCurrentMillis,currentMillis);
    setDOMElementAttribute
      (pElem,UNIVERSAL_ATTRIBUTE_LAST_UPDATE, tmpStrCurrentMillis );
    return E_SUCCESS;

}

Here is the call graph for this function:

SINT32 setDOMElementAttribute ( DOMNode *  pElem,
const char *  attrName,
const UINT8 value 
)

Definition at line 795 of file CAUtil.cpp.

References E_SUCCESS, and E_UNKNOWN.

Referenced by CACmdLnOptions::addMixIdToMixInfo(), CAMix::appendCompatibilityInfo(), CAMix::appendTermsAndConditionsExtension(), CACmdLnOptions::buildDefaultConfig(), CAFirstMix::doUserLogin_internal(), encodeXMLEncryptedKey(), CAInfoService::getCascadeHeloXMLAsString(), CAInfoService::getMixHeloXMLAsString(), CAInfoService::getOperatorTnCsAsStrings(), CACmdLnOptions::getTermsAndConditions(), CAAccountingInstance::handleAccountCertificate_internal(), CALastMix::initMixCascadeInfo(), CAMix::initMixCascadeInfo(), CAReplayCtrlChannelMsgProc::initTimestampsMessageTemplate(), CAAccountingInstance::makeInitialCCRequest(), CAAccountingInstance::prepareCCRequest(), CAReplayCtrlChannelMsgProc::proccessGetTimestamp(), CAReplayCtrlChannelMsgProc::proccessGotTimestamp(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), CAFirstMix::processKeyExchange(), CAAccountingInstance::sendAILoginConfirmation(), CAReplayCtrlChannelMsgProc::sendGetTimestamp(), setCurrentTimeMilliesAsDOMAttribute(), setDOMElementAttribute(), CACmdLnOptions::setMixName(), CACmdLnOptions::setTargetInterfaces(), CAMultiSignature::signXML(), CAMix::termsAndConditionsInfoNode(), CAXMLPriceCert::toXmlElement(), CAXMLBI::toXmlElement(), and CAXMLErrorMessage::toXmlElement().

{
  if(pElem==NULL||pElem->getNodeType()!=DOMNode::ELEMENT_NODE||attrName==NULL||value==NULL)
    return E_UNKNOWN;
  XMLCh* name=XMLString::transcode(attrName);
  XMLCh* val=XMLString::transcode((const char*)value);
  ((DOMElement*)pElem)->setAttribute(name,val);
  XMLString::release(&name);
  XMLString::release(&val);
  return E_SUCCESS;
}
SINT32 setDOMElementAttribute ( DOMNode *  pElem,
const char *  attrName,
UINT64  value 
)

Definition at line 953 of file CAUtil.cpp.

References print64(), and setDOMElementAttribute().

{
  UINT8 tmp[50];
  print64(tmp, value);
  return setDOMElementAttribute(pElem, attrName, tmp);
}

Here is the call graph for this function:

SINT32 setDOMElementAttribute ( DOMNode *  pElem,
const char *  attrName,
SINT64  value 
)

Definition at line 960 of file CAUtil.cpp.

References setDOMElementAttribute().

{
  UINT8 tmp[50];
  memset(tmp, 0, 50);
  snprintf((char *) tmp, 50, "%lld", value);
  return setDOMElementAttribute(pElem, attrName, tmp);
}

Here is the call graph for this function:

SINT32 setDOMElementAttribute ( DOMNode *  pElem,
const char *  attrName,
SINT32  value 
)

Definition at line 968 of file CAUtil.cpp.

References setDOMElementAttribute().

{
  UINT8 tmp[10];
  sprintf((char*)tmp, "%i", value);
  return setDOMElementAttribute(pElem, attrName, tmp);
}

Here is the call graph for this function:

SINT32 setDOMElementAttribute ( DOMNode *  pElem,
const char *  attrName,
UINT32  value 
)

Definition at line 975 of file CAUtil.cpp.

References setDOMElementAttribute().

{
  UINT8 tmp[10];
  sprintf((char*)tmp, "%u", value);
  return setDOMElementAttribute(pElem, attrName, tmp);
}

Here is the call graph for this function:

SINT32 setDOMElementAttribute ( DOMNode *  pElem,
const char *  attrName,
bool  value 
)

Definition at line 982 of file CAUtil.cpp.

References setDOMElementAttribute(), STR_VALUE_FALSE, and STR_VALUE_TRUE.

{
  return setDOMElementAttribute(pElem, attrName,
      ((UINT8*) (value ? STR_VALUE_TRUE : STR_VALUE_FALSE)));
}

Here is the call graph for this function:

SINT32 setDOMElementValue ( DOMElement *  pElem,
UINT32  value 
)

Definition at line 887 of file CAUtil.cpp.

References E_SUCCESS, and setDOMElementValue().

Referenced by CACmdLnOptions::buildDefaultConfig(), CAFirstMix::doUserLogin_internal(), CACertificate::encode(), encodeXMLEncryptedKey(), encryptXMLElement(), CAInfoService::getCascadeHeloXMLAsString(), CACmdLnOptions::getMixXml(), CASignature::getSignKey(), CAAccountingInstance::handleAccountCertificate_internal(), CAMix::initMixCascadeInfo(), CAReplayCtrlChannelMsgProc::initTimestampsMessageTemplate(), CAAccountingInstance::makeCCRequest(), CAAccountingInstance::makeInitialCCRequest(), CAAccountingInstance::prepareCCRequest(), CAReplayCtrlChannelMsgProc::proccessGetTimestamp(), CAReplayCtrlChannelMsgProc::proccessGotTimestamp(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), CAFirstMix::processKeyExchange(), CACmdLnOptions::processXmlConfiguration(), CAReplayCtrlChannelMsgProc::propagateCurrentReplayTimestamp(), CAAccountingInstance::sendAILoginConfirmation(), setDOMElementValue(), CACmdLnOptions::setMixName(), CACmdLnOptions::setPrepaidInterval(), CACmdLnOptions::setTargetInterfaces(), CAMultiSignature::signXML(), CAListenerInterface::toDOMElement(), CAXMLPriceCert::toXmlElement(), CAXMLBI::toXmlElement(), and CAXMLErrorMessage::toXmlElement().

  {
    UINT8 tmp[11];
    memset(tmp, 0, 11);
    snprintf((char*)tmp, 10, "%u", value);
    setDOMElementValue(pElem,tmp);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 setDOMElementValue ( DOMElement *  pElem,
SINT32  value 
)

Definition at line 896 of file CAUtil.cpp.

References E_SUCCESS, and setDOMElementValue().

  {
    //One more char for the sign of negative numbers
    UINT8 tmp[12];
    memset(tmp, 0, 12);
    snprintf((char*)tmp, 11, "%d", value);
    setDOMElementValue(pElem,tmp);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 setDOMElementValue ( DOMElement *  pElem,
double  floatValue 
)

Definition at line 906 of file CAUtil.cpp.

References E_SUCCESS, and setDOMElementValue().

  {
    char tmp[400];
    snprintf(tmp,400, "%.2f", floatValue);
    setDOMElementValue(pElem,(UINT8 *)tmp);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 setDOMElementValue ( DOMElement *  pElem,
const UINT64  text 
)

Sets the decimal text representation of a 64bit integer as node value TODO: implement this for non-64bit platforms.

Returns the content of the text node under elem as 64bit unsigned integer.

Definition at line 919 of file CAUtil.cpp.

References E_SUCCESS, print64(), and setDOMElementValue().

  {
    UINT8 tmp[32];
    memset(tmp, 0, 32);
    print64(tmp,text);
    setDOMElementValue(pElem,tmp);
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 setDOMElementValue ( DOMElement *  pElem,
const UINT8 value 
)

Definition at line 929 of file CAUtil.cpp.

References E_SUCCESS.

  {
    XMLCh* val=XMLString::transcode((const char *)value);
    DOMText* pText=pElem->getOwnerDocument()->createTextNode(val);
    XMLString::release(&val);
    //Remove all "old" text Elements...
    DOMNode* pChild=pElem->getFirstChild();
    while(pChild!=NULL)
    {
      if(pChild->getNodeType()==DOMNode::TEXT_NODE)
        {
          DOMNode* n=pElem->removeChild(pChild);
          if (n != NULL)
          {
            n->release();
            n = NULL;
          }
        }
      pChild=pChild->getNextSibling();
    }
    pElem->appendChild(pText);
    return E_SUCCESS;
  }
SINT32 setDOMElementValue ( DOMElement *  pElem,
bool  value 
)

Definition at line 988 of file CAUtil.cpp.

References setDOMElementValue(), STR_VALUE_FALSE, and STR_VALUE_TRUE.

{
  return setDOMElementValue(pElem,((UINT8*) (value ? STR_VALUE_TRUE : STR_VALUE_FALSE)));
}

Here is the call graph for this function:

SINT32 sSleep ( UINT32  sec)

Sleeps sec Seconds.

Definition at line 397 of file CAUtil.cpp.

References E_SUCCESS.

Referenced by CATempIPBlockList::cleanupThreadMainLoop(), CASocket::connect(), CAMiddleMix::connectToNextMix(), CAFirstMix::connectToNextMix(), db_loopMaintenance(), fm_loopAcceptUsers(), fm_loopLog(), CAInfoService::InfoLoop(), iplist_loopDoLogCountries(), lm_loopLog(), CALocalProxy::loop(), replaydb_loopMaintenance(), rp_loopPropagateTimestamp(), signal_segv(), CALocalProxy::start(), and CAMix::start().

  {
    #ifdef _WIN32
      Sleep(sec*1000);
    #else
      struct timespec req;
      struct timespec rem;
      req.tv_sec=sec;
      req.tv_nsec=0;
      while(nanosleep(&req,&rem)==-1)
        {
          req.tv_sec=rem.tv_sec;
          req.tv_nsec=rem.tv_nsec;
        }
    #endif
    return E_SUCCESS;
  }
char* strins ( const char *  src,
UINT32  pos,
const char *  ins 
)

Inserts a String ins in a String src starting after pos chars.

Returns a newly allocated String which must be freed using delete.

Definition at line 148 of file CAUtil.cpp.

Referenced by strins().

  {
    if(src==NULL||ins==NULL)
      return NULL;
    UINT32 srcLen=strlen(src);
    if(pos>srcLen)
      return NULL;
    UINT32 insLen=strlen(ins);
    char* newString=new char[srcLen+insLen+1];
    if(newString==NULL)
      return NULL;
    memcpy(newString,src,pos);
    memcpy(newString+pos,ins,insLen);
    memcpy(newString+pos+insLen,src+pos,srcLen-pos+1); //copy includes the \0
    return newString;
  }
char* strins ( const char *  src,
const char *  pos,
const char *  ins 
)

Inserts a String ins in a String src starting at the char pos points to.

Returns a newly allocated String which must be freed using delete.

Definition at line 168 of file CAUtil.cpp.

References strins().

  {
    if(pos==NULL||pos<src)
      return NULL;
    return strins(src,pos-src,ins);
  }

Here is the call graph for this function:

UINT32 strtrim ( UINT8 s)

Removes leading and ending whitespaces (chars<=32) from a zero terminated string.

Parameters:
sinput string (null terminated)
Returns:
new size of string
Return values:
0means, that either s was NULL or that the new string has a zero length (that means, that the old string only contains whitespaces)

Definition at line 51 of file CAUtil.cpp.

Referenced by CAListenerInterface::getInstance(), CAChain::getPrintableChainId(), CAMultiSignature::getSKI(), CAAccountingInstance::prepareCCRequest(), CACmdLnOptions::setAccountingDatabase(), CACmdLnOptions::setLoggingOptions(), CACmdLnOptions::setMixID(), and CACmdLnOptions::setTargetInterfaces().

  {
    if(s==NULL)
      return 0;
    UINT32 end=strlen((char*)s);
    if(end==0)
      return 0;
    end--;
    UINT32 start=0;
    UINT32 size;
    while(start<=end&&s[start]<=32)
      start++;
    if(start>end) //empty string....
      return 0;
    while(end>start&&s[end]<=32)
      end--;
    size=(end+1)-start;
    memmove(s,s+start,size);
    s[size]=0;
    return size;
  }
UINT32 toLower ( UINT8 a_string)

Definition at line 73 of file CAUtil.cpp.

References E_SUCCESS.

Referenced by CACmdLnOptions::setLoggingOptions().

{
  int differ = 'A'-'a';
  char ch;
  int ii = strlen((char*)a_string);
  for (int i=0; i <ii;i++)
  {
    strncpy(&ch,(char*)a_string+i,1);
    if (ch>='A' && ch<='Z')
    {
      ch = ch-differ;
      memcpy(a_string+i,&ch,1);
    }
  }
  return E_SUCCESS;
}

Variable Documentation

XercesDOMParser* theDOMParser = NULL

Definition at line 625 of file CAUtil.cpp.

Referenced by releaseDOMParser().

Definition at line 626 of file CAUtil.cpp.

Referenced by releaseDOMParser().