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.

302 lines
6.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: mscat32.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // Functions: DllMain
  12. // DllRegisterServer
  13. // DllUnregisterServer
  14. //
  15. // *** local functions ***
  16. // CatalogNew
  17. // CatalogFreeMember
  18. // CatalogFreeAttribute
  19. // CatalogCheckForDuplicateMember
  20. //
  21. // History: 25-Apr-1997 pberkman created
  22. //
  23. //--------------------------------------------------------------------------
  24. #include "global.hxx"
  25. #include "mscat32.h"
  26. CRITICAL_SECTION MSCAT_CriticalSection;
  27. HINSTANCE hInst;
  28. //////////////////////////////////////////////////////////////////////////////////////
  29. //
  30. // standard DLL exports ...
  31. //
  32. //
  33. BOOL WINAPI mscat32DllMain(HANDLE hInstDLL,DWORD fdwReason,LPVOID lpvReserved)
  34. {
  35. BOOL fRet;
  36. BOOL fCritSecInitialized = FALSE;
  37. switch (fdwReason)
  38. {
  39. case DLL_PROCESS_ATTACH:
  40. hInst = (HINSTANCE)hInstDLL;
  41. __try
  42. {
  43. InitializeCriticalSection(&MSCAT_CriticalSection);
  44. fCritSecInitialized = TRUE;
  45. }
  46. __except(EXCEPTION_EXECUTE_HANDLER)
  47. {
  48. fRet = FALSE;
  49. goto Return;
  50. }
  51. break;
  52. case DLL_PROCESS_DETACH:
  53. DeleteCriticalSection(&MSCAT_CriticalSection);
  54. break;
  55. }
  56. fRet = CatAdminDllMain(hInstDLL, fdwReason, lpvReserved);
  57. if (!fRet && fCritSecInitialized)
  58. {
  59. DeleteCriticalSection(&MSCAT_CriticalSection);
  60. }
  61. Return:
  62. return fRet;
  63. }
  64. STDAPI mscat32DllRegisterServer(void)
  65. {
  66. return(S_OK);
  67. }
  68. STDAPI mscat32DllUnregisterServer(void)
  69. {
  70. return(S_OK);
  71. }
  72. //////////////////////////////////////////////////////////////////////////////////////
  73. //
  74. // local utility functions
  75. //
  76. //
  77. void *CatalogNew(DWORD cbSize)
  78. {
  79. void *pvRet;
  80. pvRet = (void *)new BYTE[cbSize];
  81. if (!(pvRet))
  82. {
  83. assert(pvRet);
  84. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  85. }
  86. return(pvRet);
  87. }
  88. BOOL CatalogFreeAttribute(CRYPTCATATTRIBUTE *pCatAttr)
  89. {
  90. if (!(pCatAttr) ||
  91. (pCatAttr->cbStruct != sizeof(CRYPTCATATTRIBUTE)))
  92. {
  93. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  94. return(FALSE);
  95. }
  96. DELETE_OBJECT(pCatAttr->pwszReferenceTag);
  97. DELETE_OBJECT(pCatAttr->pbValue);
  98. pCatAttr->cbValue = 0;
  99. pCatAttr->dwAttrTypeAndAction = 0;
  100. pCatAttr->dwReserved = 0;
  101. return(TRUE);
  102. }
  103. BOOL CatalogFreeMember(CRYPTCATMEMBER *pCatMember)
  104. {
  105. if (!(pCatMember) ||
  106. (pCatMember->cbStruct != sizeof(CRYPTCATMEMBER)))
  107. {
  108. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  109. return(FALSE);
  110. }
  111. //
  112. // reference tag
  113. //
  114. DELETE_OBJECT(pCatMember->pwszReferenceTag);
  115. //
  116. // file name
  117. //
  118. DELETE_OBJECT(pCatMember->pwszFileName);
  119. //
  120. // free indirect data
  121. //
  122. if (pCatMember->pIndirectData)
  123. {
  124. // Data.pszObjId
  125. DELETE_OBJECT(pCatMember->pIndirectData->Data.pszObjId);
  126. // Data.Value.pbData
  127. DELETE_OBJECT(pCatMember->pIndirectData->Data.Value.pbData);
  128. // DigestAlgorithm.pszObjId
  129. DELETE_OBJECT(pCatMember->pIndirectData->DigestAlgorithm.pszObjId);
  130. // Digest.pbData
  131. DELETE_OBJECT(pCatMember->pIndirectData->Digest.pbData);
  132. // the structure itself!
  133. DELETE_OBJECT(pCatMember->pIndirectData);
  134. }
  135. //
  136. // free encoded indirect data
  137. //
  138. DELETE_OBJECT(pCatMember->sEncodedIndirectData.pbData);
  139. pCatMember->sEncodedIndirectData.cbData = 0;
  140. //
  141. // free encoded member info
  142. //
  143. DELETE_OBJECT(pCatMember->sEncodedMemberInfo.pbData);
  144. pCatMember->sEncodedMemberInfo.cbData = 0;
  145. //
  146. // free attribute data
  147. //
  148. if (pCatMember->hReserved)
  149. {
  150. Stack_ *ps;
  151. DWORD cStack;
  152. CRYPTCATATTRIBUTE *pAttr;
  153. ps = (Stack_ *)pCatMember->hReserved;
  154. cStack = 0;
  155. while (pAttr = (CRYPTCATATTRIBUTE *)ps->Get(cStack))
  156. {
  157. CatalogFreeAttribute(pAttr);
  158. cStack++;
  159. }
  160. DELETE_OBJECT(ps);
  161. pCatMember->hReserved = NULL;
  162. }
  163. return(TRUE);
  164. }
  165. BOOL CatalogCheckForDuplicateMember(Stack_ *pMembers, WCHAR *pwszReferenceTag)
  166. {
  167. CRYPTCATMEMBER *pMem;
  168. DWORD iCur;
  169. DWORD ccRT;
  170. ccRT = wcslen(pwszReferenceTag);
  171. iCur = 0;
  172. while (iCur < pMembers->Count())
  173. {
  174. pMem = (CRYPTCATMEMBER *)pMembers->Get(iCur);
  175. if (pMem)
  176. {
  177. if (ccRT == (DWORD)wcslen(pMem->pwszReferenceTag))
  178. {
  179. if (wcscmp(pwszReferenceTag, pMem->pwszReferenceTag) == 0)
  180. {
  181. return(TRUE);
  182. }
  183. }
  184. }
  185. //
  186. // increment our index!
  187. //
  188. iCur++;
  189. }
  190. return(FALSE);
  191. }
  192. WCHAR aHexDigit[] = {
  193. L'0',
  194. L'1',
  195. L'2',
  196. L'3',
  197. L'4',
  198. L'5',
  199. L'6',
  200. L'7',
  201. L'8',
  202. L'9',
  203. L'A',
  204. L'B',
  205. L'C',
  206. L'D',
  207. L'E',
  208. L'F'
  209. };
  210. VOID ByteToWHex (IN LPBYTE pbDigest, IN DWORD iByte, OUT LPWSTR pwszHashTag)
  211. {
  212. DWORD iTag;
  213. DWORD iHexDigit1;
  214. DWORD iHexDigit2;
  215. iTag = iByte * 2;
  216. iHexDigit1 = ( pbDigest[ iByte ] & 0xF0 ) >> 4;
  217. iHexDigit2 = ( pbDigest[ iByte ] & 0x0F );
  218. pwszHashTag[ iTag ] = aHexDigit[ iHexDigit1 ];
  219. pwszHashTag[ iTag + 1 ] = aHexDigit[ iHexDigit2 ];
  220. }
  221. BOOL MsCatConstructHashTag (IN DWORD cbDigest, IN LPBYTE pbDigest, OUT LPWSTR* ppwszHashTag)
  222. {
  223. DWORD cwTag;
  224. LPWSTR pwszHashTag;
  225. DWORD cCount;
  226. cwTag = ( ( cbDigest * 2 ) + 1 );
  227. pwszHashTag = (LPWSTR)CatalogNew( cwTag * sizeof( WCHAR ) );
  228. if ( pwszHashTag == NULL )
  229. {
  230. SetLastError( E_OUTOFMEMORY );
  231. return( FALSE );
  232. }
  233. for ( cCount = 0; cCount < cbDigest; cCount++ )
  234. {
  235. ByteToWHex( pbDigest, cCount, pwszHashTag );
  236. }
  237. pwszHashTag[ cwTag - 1 ] = L'\0';
  238. *ppwszHashTag = pwszHashTag;
  239. return( TRUE );
  240. }
  241. VOID MsCatFreeHashTag (IN LPWSTR pwszHashTag)
  242. {
  243. DELETE_OBJECT(pwszHashTag);
  244. }