Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.8 KiB

  1. /// hdrutil.h
  2. // Macros and types to support encode/decode of linearized headers
  3. #ifndef _LHUTIL_H
  4. #define _LHUTIL_H
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #include <awrt.h>
  9. #include <lineariz.h>
  10. #include <sosmapi.h>
  11. #include <awsec.h>
  12. #include <lhprot.h>
  13. ///////////////////////////////////////////////////////////////////////////
  14. /// Extern variables
  15. ///
  16. // DEBUG
  17. #ifdef DEBUG
  18. extern DBGPARAM dpCurSettings;
  19. #define ZONE_LINEARIZER (0x0001 & dpCurSettings.ulZoneMask)
  20. #define ZONE_LIN_VERBOSE (0x0002 & dpCurSettings.ulZoneMask)
  21. extern WORD wHashSize;
  22. #endif
  23. ///////////////////////////////////////////////////////////////////////////
  24. /// Errors
  25. ///
  26. #ifndef NO_ERROR
  27. # define NO_ERROR (0)
  28. #endif
  29. #define ERR_MEMORY_ERROR (1)
  30. #define ERR_INVALID_OCTETS (2)
  31. #define ERR_UNEXPECTED_OCTETS (3)
  32. #define ERR_UNEXPECTED_LENGTH (4)
  33. #define ERR_DATASTREAM_ERROR (5)
  34. // Define this small because we only use it on the header !!
  35. #define MAX_OCTETBLOCK_SIZE 256
  36. ///////////////////////////////////////////////////////////////////////////
  37. /// Types
  38. ///
  39. typedef unsigned char OCTET;
  40. typedef DWORD OCTETSINK;
  41. typedef DWORD OCTETSOURCE;
  42. typedef DWORD OCTETFILE;
  43. typedef struct _LININST {
  44. DWORD octets; // Source/sink for linearized format. This
  45. // is used to get at all the data.
  46. DWORD dwGlobalError; // Error for this job
  47. LPDATACB lpfnData; // Callback to read/write source/sink
  48. DWORD dwHashInstance; // Hashing instance ...
  49. BYTE rgb[MAX_OCTETBLOCK_SIZE]; // For decoding memory ...
  50. } LININST, FAR *LPLININST, NEAR *NPLININST;
  51. //------------------------------------------------------------------------
  52. // This def relies on the fact that there is a variable called
  53. // lininst in the calling function which pts to the instance
  54. #define AllocLinMemMacro(lpRet, uSize) lpRet = lplinst->rgb;
  55. // Nothing to do since its in the instance
  56. #define FreeLinMemMacro(lpVal) (VOID)(0)
  57. #define OctetInMacro(lplinst, pOctet) OctetDataMacro(GPM_GETDATA, lplinst, (LPBYTE)pOctet, 1)
  58. #define OctetOutMacro(lplinst, Octet) \
  59. { \
  60. OCTET oct = Octet; \
  61. OctetDataMacro(GPM_PUTDATA, lplinst, (LPBYTE)&oct, 1); \
  62. }
  63. #define OctetBlockInMacro(lplinst, lpb, size) OctetDataMacro(GPM_GETDATA, lplinst, lpb, size)
  64. #define OctetBlockOutMacro(lplinst, lpb, size) OctetDataMacro(GPM_PUTDATA, lplinst, lpb, size)
  65. #define OctetDataMacro(gpm, lplinst, lpb, size) \
  66. do { \
  67. if ((*lplinst->lpfnData)(gpm, lplinst->octets, (LPBYTE)(lpb), size, NULL)!=size) \
  68. { \
  69. DEBUGMSG(1, (THIS_FUNCTION ":Octet read/write Failed !!\r\n")); \
  70. lplinst->dwGlobalError = ERR_DATASTREAM_ERROR; \
  71. goto error; \
  72. } \
  73. /* Hash it if necessary */ \
  74. if (lplinst->dwHashInstance) \
  75. { \
  76. DEBUGSTMT(wHashSize+=size); \
  77. DEBUGCHK(size <= 0xffff); \
  78. SendBFTHash(lplinst->dwHashInstance, (LPBYTE)(lpb), (WORD)size); \
  79. } \
  80. } \
  81. while (0)
  82. /// Utility functions to encode / decode a header
  83. #define WriteHdrOpen(lplinst) \
  84. { \
  85. OctetOutMacro(lplinst, TAG_MESSAGE_HEADER); \
  86. ASN1EncodeIndefiniteLengthMacro(lplinst); \
  87. }
  88. #define WriteHdrClose(lplinst) \
  89. { \
  90. ASN1EncodeEndOfContentsOctetsMacro(lplinst); \
  91. WriteHashMacro(lplinst); \
  92. }
  93. #define WriteOrigOpen(lplinst) \
  94. OctetOutMacro (lplinst, TAG_FROM); \
  95. ASN1EncodeIndefiniteLengthMacro(lplinst); \
  96. OctetOutMacro (lplinst, TAG_RECIP_DESC); \
  97. ASN1EncodeIndefiniteLengthMacro(lplinst);
  98. #define WriteOrigClose(lplinst) \
  99. ASN1EncodeEndOfContentsOctetsMacro(lplinst); \
  100. ASN1EncodeEndOfContentsOctetsMacro(lplinst);
  101. #define WriteRecipOpen(lplinst) \
  102. OctetOutMacro (lplinst, TAG_TO); \
  103. ASN1EncodeIndefiniteLengthMacro(lplinst); \
  104. OctetOutMacro (lplinst, TAG_RECIP_DESC); \
  105. ASN1EncodeIndefiniteLengthMacro(lplinst);
  106. #define WriteRecipClose(lplinst) WriteOrigClose(lplinst)
  107. #define WriteString(lplinst, tag, string) \
  108. ASN1EncodePrimitiveStringImplicitMacro(lplinst, tag, string, lstrlen(string)+1)
  109. // Hashing macros
  110. #define StartHashMacro(lplinst) \
  111. { \
  112. if (!(lplinst->dwHashInstance = StartBFTHash())) \
  113. { \
  114. DEBUGMSG(1, (THIS_FUNCTION ":Error getting hash instance!\n\r")); \
  115. lplinst->dwGlobalError = ERR_NOMEMORY; \
  116. goto error; \
  117. } \
  118. DEBUGSTMT(wHashSize=0); \
  119. }
  120. #define WriteHashMacro(lplinst) \
  121. { \
  122. BYTE bHash[16]; \
  123. BYTE bSalt[3]; \
  124. _fmemset(bHash, 0, sizeof(bHash)); \
  125. _fmemset(bSalt, 0, sizeof(bSalt)); \
  126. /* Complete the hash if we initialized it */ \
  127. if (lplinst->dwHashInstance) \
  128. { \
  129. DoneBFTHash(lplinst->dwHashInstance, bSalt, bHash); \
  130. lplinst->dwHashInstance = 0; \
  131. } \
  132. OctetBlockOutMacro(lplinst, bSalt, sizeof(bSalt)); \
  133. OctetBlockOutMacro(lplinst, bHash, sizeof(bHash)); \
  134. DEBUGMSG(ZONE_LINEARIZER, ("Lhutil: Hdr Hash Size %u\r\n", wHashSize)); \
  135. }
  136. #define CheckHashMacro(lplinst) \
  137. { \
  138. BYTE rgbMsgHash[19], rgbNewHash[16]; \
  139. DWORD dwInstance = lplinst->dwHashInstance; \
  140. /* Reset instance so we dont hash the msg hash */ \
  141. lplinst->dwHashInstance = 0; \
  142. OctetBlockInMacro(lplinst, rgbMsgHash, sizeof(rgbMsgHash)); \
  143. /* Generate current hash using same salt */ \
  144. DoneBFTHash(dwInstance, rgbMsgHash, rgbNewHash); \
  145. /* Compare them */ \
  146. if (! BinComp(sizeof(rgbNewHash), rgbMsgHash+3, \
  147. sizeof(rgbNewHash), rgbNewHash) != 0) \
  148. { \
  149. DEBUGMSG(1, ("ERROR: Hash Does not match !!!\n\r")); \
  150. lplinst->dwGlobalError = ERR_GENERAL_FAILURE; \
  151. goto error; \
  152. } \
  153. }
  154. ///////////////////////////////////////////////////////////////////////////
  155. /// Internal funcion prototypes
  156. ///
  157. BOOL __fastcall BinComp(DWORD cbA, PVOID lpA, DWORD cbB, PVOID lpB);
  158. #ifdef __cplusplus
  159. } // extern "C"
  160. #endif
  161. #endif // hdrutil_inc