Mixe for Privacy and Anonymity in the Internet
CAUtil.hpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification,
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice,
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice,
00011     this list of conditions and the following disclaimer in the documentation and/or
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
00015     may be used to endorse or promote products derived from this software without specific
00016     prior written permission.
00017 
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #ifndef __CA_UTIL__
00029 #define __CA_UTIL__
00030 #include "CAASymCipher.hpp"
00031 
00032 #define UNIVERSAL_ATTRIBUTE_LAST_UPDATE "lastUpdate"
00033 #define UNIVERSAL_NODE_LAST_UPDATE "LastUpdate"
00034 
00035 #define STR_VALUE_TRUE "true"
00036 #define STR_VALUE_FALSE "false"
00037 
00038 #define TMP_BUFF_SIZE 255
00039 #define TMP_LOCALE_SIZE 3
00040 #define TMP_DATE_SIZE 9
00041 
00042 #define TEMPLATE_REFID_MAXLEN ((TMP_BUFF_SIZE) + (TMP_LOCALE_SIZE) + (TMP_DATE_SIZE) + 2)
00043 
00044 #if _XERCES_VERSION >= 30001
00045   #define INTEGRATE_NOT_ALLOWED_POSITIONS \
00046     (DOMNode::DOCUMENT_POSITION_CONTAINED_BY | DOMNode::DOCUMENT_POSITION_CONTAINS )
00047 #else
00048   #define INTEGRATE_NOT_ALLOWED_POSITIONS \
00049     (DOMNode::TREE_POSITION_ANCESTOR   | DOMNode::TREE_POSITION_DESCENDANT | \
00050     DOMNode::TREE_POSITION_EQUIVALENT | DOMNode::TREE_POSITION_SAME_NODE )
00051 #endif
00052 
00053 UINT32 strtrim(UINT8*);
00054 UINT32 toLower(UINT8* a_string);
00055 
00056 SINT32 memtrim(UINT8* out,const UINT8* in,UINT32 len);
00057 
00059 UINT8* bytes2hex(const void* bytes,UINT32 len);
00060 
00061 char* strins(const char* src,UINT32 pos,const char* ins);
00062 char* strins(const char* src,const char * pos,const char* ins);
00063 
00064 SINT32 getcurrentTime(timespec& t);
00065 SINT32 getcurrentTimeMillis(UINT64& u64Time);
00066 SINT32 getcurrentTimeMicros(UINT64& u64Time);
00067 
00068 SINT32 compDate(struct tm *date1, struct tm *date2);
00069 
00070 SINT32 initRandom();
00071 SINT32 getRandom(UINT8* buff,UINT32 len);
00072 
00073 SINT32 getRandom(UINT32* val);
00074 
00075 SINT32 getRandom(UINT64* val);
00076 
00077 SINT32 msSleep(UINT32 ms);
00078 
00079 SINT32 sSleep(UINT32 sec);
00080 
00081 UINT32 getMemoryUsage();
00082 
00083 inline SINT64 filesize64(int handle)
00084   {
00085     #ifdef _WIN32
00086       return _filelengthi64(handle);
00087     #elif defined (HAVE_FSTAT64)
00088       struct stat64 info;
00089       if(fstat64(handle, &info) != 0)
00090         return E_UNKNOWN;
00091       return info.st_size;
00092     #else
00093       struct stat info;
00094       if(fstat(handle, &info) != 0)
00095         return E_UNKNOWN;
00096       return info.st_size;
00097     #endif
00098   }
00099 
00100 inline SINT32 filesize32(int handle)
00101   {
00102     #ifdef _WIN32
00103       return _filelength(handle);
00104     #else
00105       struct stat info;
00106       if(fstat(handle, &info) != 0)
00107         return E_UNKNOWN;
00108       return info.st_size;
00109     #endif
00110   }
00111 
00115 XERCES_CPP_NAMESPACE::DOMDocument* parseDOMDocument(const UINT8* const buff, UINT32 len);
00119 XERCES_CPP_NAMESPACE::DOMDocument* parseDOMDocument(const UINT8* const pathOrURL);
00120 void initDOMParser();
00121 void releaseDOMParser();
00122 
00123 SINT32 getDOMChildByName(const DOMNode* pNode,const XMLCh* const name,DOMNode* & child,bool deep=false);
00124 SINT32 getDOMChildByName(const DOMNode* pNode,const char * const name,DOMNode* & child,bool deep=false);
00125 SINT32 getDOMChildByName(const DOMNode* pNode,const char * const name,DOMElement* & child,bool deep=false);
00126 SINT32 getSignatureElements(DOMNode* parent, DOMNode** signatureNodes, UINT32* length);
00127 
00138 SINT32 getDOMElementValue(const DOMNode * const pElem,UINT8* value,UINT32* len);
00139 
00140 SINT32 setDOMElementAttribute(DOMNode* pElem,const char* attrName,const UINT8* value);
00141 
00142 bool equals(const XMLCh* const e1,const char* const e2);
00143 
00144 SINT32 getDOMElementAttribute(const DOMNode * const pElem,const char* attrName,SINT32* value);
00145 
00148 DOMElement* createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument* pOwnerDoc,const char * const name);
00149 
00152 DOMText* createDOMText(XERCES_CPP_NAMESPACE::DOMDocument* pOwnerDoc,const char * const text);
00153 
00154 #ifndef ONLY_LOCAL_PROXY
00155 
00157 XERCES_CPP_NAMESPACE::DOMDocument* createDOMDocument();
00158 
00161 DOMText* createDOMText(XERCES_CPP_NAMESPACE::DOMDocument* pOwnerDoc,const char * const text);
00162 
00163 
00164 SINT32 setDOMElementValue(DOMElement* pElem, UINT32 value);
00165 
00166 SINT32 setDOMElementValue(DOMElement* pElem, SINT32 value);
00176 SINT32 setDOMElementValue(DOMElement* pElem, const UINT64 text);
00177 SINT32 setDOMElementValue(DOMElement* pElem, const SINT64 text);
00178 
00179 SINT32 getNodeName(const DOMNode * const pElem, UINT8* value,UINT32* valuelen);
00180 
00181 SINT32 getDOMElementValue(const DOMNode * const pElem, UINT64 &value);
00182 SINT32 getDOMElementValue(const DOMElement * const pElem, SINT64 &value);
00183 
00184 SINT32 getDOMElementValue(const DOMElement * const pElem,UINT32* value);
00185 SINT32 getDOMElementValue(const DOMElement * const pElem,SINT32* value);
00188 SINT32 getDOMElementValue(const DOMElement * const pElem,UINT32& value,UINT32 defaultValue);
00189 
00190 SINT32 getDOMElementValue(const DOMElement * const pElem,UINT16* value);
00191 
00192 SINT32 setDOMElementValue(DOMElement* pElem,const UINT8* value);
00193 
00194 
00195 SINT32 getDOMElementValue(const DOMElement * const pElem,double* value);
00196 
00197 SINT32 setDOMElementAttribute(DOMNode* pElem,const char* attrName, bool value);
00198 SINT32 setDOMElementAttribute(DOMNode* pElem,const char* attrName, SINT32 value);
00199 SINT32 setDOMElementAttribute(DOMNode* pElem,const char* attrName, UINT32 value);
00200 SINT32 setDOMElementAttribute(DOMNode* pElem, const char* attrName, UINT64 value);
00201 SINT32 setDOMElementAttribute(DOMNode* pElem, const char* attrName, SINT64 value);
00202 
00203 SINT32 setDOMElementValue(DOMElement* pElem,double floatValue);
00204 SINT32 setDOMElementValue(DOMElement* pElem, bool value);
00205 
00206 SINT32 getDOMElementAttribute(const DOMNode * const pElem,const char* attrName,SINT64& value);
00207 SINT32 getDOMElementAttribute(const DOMNode * const pElem,const char* attrName,UINT32& value);
00208 SINT32 getDOMElementAttribute(const DOMNode * const pElem,const char* attrName,bool& value);
00209 SINT32 getDOMElementAttribute(const DOMNode * const pElem,const char* attrName,UINT8* value,UINT32* len);
00210 
00211 DOMNodeList* getElementsByTagName(DOMElement* pElem,const char* const name);
00212 
00213 SINT32 getLastDOMChildByName(const DOMNode* pNode,const XMLCh* const name,DOMNode* & a_child);
00214 SINT32 getLastDOMChildByName(const DOMNode* pNode,const char * const name,DOMNode* & a_child);
00215 SINT32 getLastDOMChildByName(const DOMNode* pNode,const char * const name,DOMElement* & a_child);
00216 
00217 SINT32 setCurrentTimeMilliesAsDOMAttribute(DOMNode *pElem);
00218 
00219 //if not null the returned char pointer must be explicitely freed by the caller with 'delete []'
00220 UINT8 *getTermsAndConditionsTemplateRefId(DOMNode *tcTemplateRoot);
00221 
00222 SINT32 encodeXMLEncryptedKey(UINT8* key,UINT32 keylen, UINT8* xml, UINT32* xmllen,CAASymCipher* pRSA);
00223 SINT32 encodeXMLEncryptedKey(UINT8* key,UINT32 keylen, DOMElement* & elemRootEncodedKey,XERCES_CPP_NAMESPACE::DOMDocument* docOwner,CAASymCipher* pRSA);
00224 SINT32 decodeXMLEncryptedKey(UINT8* key,UINT32* keylen, const UINT8* const xml, UINT32 xmllen,CAASymCipher* pRSA);
00225 SINT32 decodeXMLEncryptedKey(UINT8* key,UINT32* keylen, const DOMNode* pRoot,CAASymCipher* pRSA);
00226 
00227 SINT32 integrateDOMNode(const DOMNode *srcNode, DOMNode *dstNode, bool recursive, bool replace);
00228 
00230 SINT32 encryptXMLElement(DOMNode* pElem , CAASymCipher* pRSA);
00231 
00233 SINT32 decryptXMLElement(DOMNode* pelem , CAASymCipher* pRSA);
00234 
00235 #endif //ONLY_LOCAL_PROXY
00236 
00237 UINT8* encryptXMLElement(UINT8* inbuff,UINT32 inlen,UINT32& outlen,CAASymCipher* pRSA);
00238 
00239 inline void set64(UINT64& op1,UINT32 op2)
00240   {
00241 #if !defined(HAVE_NATIVE_UINT64)
00242     op1.low=op2;
00243     op1.high=0;
00244 #else
00245     op1=op2;
00246 #endif
00247   }
00248 
00251 inline void set64(UINT64& dst,UINT64 src)
00252   {
00253 #if !defined(HAVE_NATIVE_UINT64)
00254     dst.low=src.low;
00255     dst.high=src.high;
00256 #else
00257     dst=src;
00258 #endif
00259   }
00260 
00263 inline void set64(SINT64& dst,SINT64 src)
00264   {
00265 #if !defined(HAVE_NATIVE_UINT64)
00266     dst.low=src.low;
00267     dst.high=src.high;
00268 #else
00269     dst=src;
00270 #endif
00271   }
00272 
00273 inline void setZero64(UINT64& op1)
00274   {
00275 #if !defined(HAVE_NATIVE_UINT64)
00276     op1.low=0;
00277     op1.high=0;
00278 #else
00279     op1=0;
00280 #endif
00281   }
00282 
00283 inline void setZero64(SINT64& op1)
00284   {
00285 #if !defined(HAVE_NATIVE_UINT64)
00286     op1.low=0;
00287     op1.high=0;
00288 #else
00289     op1=0;
00290 #endif
00291   }
00292 
00293 inline void add64(UINT64& op1,UINT32 op2)
00294   {
00295 #if !defined(HAVE_NATIVE_UINT64)
00296     UINT32 t=op1.low;
00297     op1.low+=op2;
00298     if(op1.low<t)
00299       op1.high++;
00300 #else
00301     op1+=op2;
00302 #endif
00303   }
00304 
00305 inline void inc64(UINT64& op1)
00306   {
00307 #if !defined(HAVE_NATIVE_UINT64)
00308   op1.low++;
00309   if(op1.low==0)
00310     op1.high++;
00311 #else
00312     op1++;
00313 #endif
00314   }
00315 
00316 inline UINT32 diff64(const UINT64& bigop,const UINT64& smallop)
00317   {
00318     #if !defined(HAVE_NATIVE_UINT64)
00319       return (UINT32) -1; //TODO!!!
00320     #else
00321       return (UINT32)(bigop-smallop);
00322     #endif
00323   }
00324 
00325 inline UINT32 div64(UINT64& op1,UINT32 op2)
00326   {
00327 #if !defined(HAVE_NATIVE_UINT64)
00328     return (UINT32) -1; //TODO!!!
00329 #else
00330     return (UINT32)(op1/op2);
00331 #endif
00332   }
00333 
00334 inline bool isGreater64(UINT64& op1,UINT64& op2)
00335   {
00336 #if !defined(HAVE_NATIVE_UINT64)
00337     if(op1.high>op2.high)
00338       return true;
00339     if(op1.high==op2.high)
00340       return op1.low>op2.low;
00341     return false;
00342 #else
00343     return op1>op2;
00344 #endif
00345   }
00346 
00347 inline bool isGreater64(SINT64 op1,SINT64 op2)
00348   {
00349 #if !defined(HAVE_NATIVE_UINT64)
00350     if(op1.high>op2.high)
00351       return true;
00352     if(op1.high==op2.high)
00353       return op1.low>op2.low;
00354     return false;
00355 #else
00356     return op1>op2;
00357 #endif
00358   }
00359 
00360 inline bool isLesser64(UINT64& smallOp1,UINT64& bigOp2)
00361   {
00362 #if !defined(HAVE_NATIVE_UINT64)
00363     if(smallOp1.high<bigOp2.high)
00364       return true;
00365     if(smallOp1.high==bigOp2.high)
00366       return smallOp1.low<bigOp2.low;
00367     return false;
00368 #else
00369     return smallOp1<bigOp2;
00370 #endif
00371   }
00372 
00373 inline bool isEqual64(UINT64& op1,UINT64& op2)
00374   {
00375 #if !defined(HAVE_NATIVE_UINT64)
00376     return (op1.high==op2.high)&&op1.low==op2.low;
00377 #else
00378     return op1==op2;
00379 #endif
00380   }
00381 
00382 inline bool isZero64(UINT64& op1)
00383   {
00384 #if !defined(HAVE_NATIVE_UINT64)
00385     return (op1.high==0)&&op1.low==0;
00386 #else
00387     return op1==0;
00388 #endif
00389   }
00390 
00391 inline bool isZero64(SINT64& op1)
00392   {
00393 #if !defined(HAVE_NATIVE_UINT64)
00394     return (op1.high==0)&&op1.low==0;
00395 #else
00396     return op1==0;
00397 #endif
00398   }
00399 
00400 inline void print64(UINT8* buff,UINT64 num)
00401   {
00402     #ifdef HAVE_NATIVE_UINT64
00403       if(num==0)
00404         {
00405           buff[0]='0';
00406           buff[1]=0;
00407           return;
00408         }
00409       UINT64 mask=10000000000000000000ULL;
00410       UINT digit;
00411       UINT32 index=0;
00412       bool bprintZero=false;
00413       if(num>=mask)
00414         {
00415           buff[index++]='1';
00416           num-=mask;
00417           bprintZero=true;
00418         }
00419       while(mask>1)
00420         {
00421           mask/=10;
00422           digit=(UINT)(num/mask);
00423           if(digit>0||bprintZero)
00424             {
00425               buff[index++]=(UINT8)(digit+'0');
00426               num%=mask;
00427               bprintZero=true;
00428             }
00429         }
00430       buff[index]=0;
00431     #else //no native UINT_64
00432       sprintf((char*)buff,"(%lu:%lu)",op.high,op.low);
00433     #endif
00434   }
00435 
00436 
00437 UINT8* readFile(const UINT8* const name,UINT32* size);
00438 SINT32 saveFile(const UINT8* const name,const UINT8* const buff,UINT32 buffSize);
00439 
00447 //SINT32 parseJdbcTimestamp(const UINT8 * strTimestamp, SINT32& seconds);
00448 
00449 
00458 //SINT32 formatJdbcTimestamp(const SINT32 seconds, UINT8 * strTimestamp, const UINT32 len);
00459 
00460 
00464 SINT32 parseU64(const UINT8 * str, UINT64& value);
00465 
00469 SINT32 parseS64(const UINT8 * str, SINT64& value);
00470 
00473 SINT32 readPasswd(UINT8* buff,UINT32 len);
00474 
00475 void logMemoryUsage();
00476 
00477 
00478 #ifndef ONLY_LOCAL_PROXY
00479 
00482 inline DSA* DSA_clone(DSA* dsa)
00483   {
00484     if(dsa==NULL)
00485       return NULL;
00486     DSA* tmpDSA=DSA_new();
00487     tmpDSA->g=BN_dup(dsa->g);
00488     tmpDSA->p=BN_dup(dsa->p);
00489     tmpDSA->q=BN_dup(dsa->q);
00490     tmpDSA->pub_key=BN_dup(dsa->pub_key);
00491     if(dsa->priv_key!=NULL)
00492       tmpDSA->priv_key=BN_dup(dsa->priv_key);
00493     return tmpDSA;
00494   }
00495 
00499 inline RSA* RSA_clone(RSA* rsa)
00500   {
00501     if(rsa == NULL)
00502     {
00503       return NULL;
00504     }
00505     RSA* tmpRSA = RSA_new();
00506     tmpRSA->n = BN_dup(rsa->n);
00507     tmpRSA->e = BN_dup(rsa->e);
00508     if(rsa->d != NULL)
00509     { //we have a private key
00510       tmpRSA->d = BN_dup(rsa->d);
00511       if(tmpRSA->p != NULL)
00512       {
00513         tmpRSA->p = BN_dup(rsa->p);
00514       }
00515       if(tmpRSA->q != NULL)
00516       {
00517         tmpRSA->q = BN_dup(rsa->q);
00518       }
00519     }
00520     if(tmpRSA->dmp1 != NULL)
00521     {
00522       tmpRSA->dmp1 = BN_dup(rsa->dmp1);
00523     }
00524     if(tmpRSA->dmq1 != NULL)
00525     {
00526       tmpRSA->dmq1 = BN_dup(rsa->dmq1);
00527     }
00528     if(tmpRSA->iqmp != NULL)
00529     {
00530       tmpRSA->iqmp = BN_dup(rsa->iqmp);
00531     }
00532     return tmpRSA;
00533   }
00534 
00535 #endif //ONLY_LOCAL_PROXY
00536 #endif