Leaked source code of windows server 2003
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.

496 lines
13 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: wvtstrss.cpp
  8. //
  9. // Contents: WinVerifyTrust Stress
  10. //
  11. // History: 13-Aug-1997 pberkman created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #define STRING_SEPERATOR L'*'
  16. typedef struct STATEINFO_
  17. {
  18. WCHAR wszCatalogFile[MAX_PATH];
  19. HANDLE hState;
  20. } STATEINFO;
  21. void _StripQuotes(WCHAR *pwszIn);
  22. void _Add2CatDB(WCHAR *pwszCatFile, WCHAR *pwszMemberTag, WCHAR *pwszMemberFile, DWORD dwExpectedReturn);
  23. void _VerifyMember(WCHAR *pwszMemberTag, WCHAR *pwszMemberFile, DWORD dwExpectedReturn);
  24. void _CloseWVTHandles(void);
  25. STATEINFO * _FindStateHandle(WCHAR *pwszCatalogFile);
  26. void _ToLower(WCHAR *pwszInOut);
  27. Stack_ *pStateHandles = NULL;
  28. GUID gAction = DRIVER_ACTION_VERIFY;
  29. GUID gSS = DRIVER_ACTION_VERIFY;
  30. HCATADMIN hCatAdmin = NULL;
  31. BOOL fCatalogAdded = FALSE;
  32. BOOL fVerbose;
  33. DWORD dwTotalCatalogs = 0;
  34. DWORD dwTotalErrors = 0;
  35. HWND hWnd = NULL;
  36. extern "C" int __cdecl wmain(int argc, WCHAR **wargv)
  37. {
  38. WCHAR *pwszLoopFile;
  39. WCHAR *pwszCatFile;
  40. WCHAR *pwszMemFile;
  41. WCHAR *pwsz;
  42. DWORD dwExpectedReturn;
  43. DWORD dwCount;
  44. DWORD dwTotalFiles;
  45. BOOL fVerbose;
  46. int iRet;
  47. cWArgv_ *pArgs;
  48. fParse_ *pLoopFile;
  49. BOOL fFailed;
  50. COleDateTime tStart;
  51. COleDateTime tEnd;
  52. COleDateTimeSpan tsTotal;
  53. pLoopFile = NULL;
  54. dwTotalFiles = 0;
  55. dwCount = 1;
  56. iRet = 0;
  57. dwExpectedReturn = S_OK;
  58. hWnd = GetDesktopWindow();
  59. if (!(pArgs = new cWArgv_((HINSTANCE)GetModuleHandle(NULL), &fFailed)))
  60. {
  61. goto MemoryError;
  62. }
  63. if (fFailed)
  64. {
  65. goto MemoryError;
  66. }
  67. if (!(pStateHandles = new Stack_(NULL)))
  68. {
  69. goto MemoryError;
  70. }
  71. pArgs->AddUsageText(IDS_USAGETEXT_USAGE, IDS_USAGETEXT_OPTIONS,
  72. IDS_USAGETEXT_CMDFILE, IDS_USAGETEXT_ADD,
  73. IDS_USAGETEXT_OPTPARAM);
  74. pArgs->Add2List(IDS_PARAM_HELP, IDS_PARAMTEXT_HELP, WARGV_VALUETYPE_BOOL, (void *)FALSE);
  75. pArgs->Add2List(IDS_PARAM_VERBOSE, IDS_PARAMTEXT_VERBOSE, WARGV_VALUETYPE_BOOL, (void *)FALSE);
  76. pArgs->Add2List(IDS_PARAM_SSGUID, IDS_PARAMTEXT_SSGUID, WARGV_VALUETYPE_WCHAR, NULL);
  77. pArgs->Add2List(IDS_PARAM_ADD2DB, IDS_PARAMTEXT_ADD2DB, WARGV_VALUETYPE_WCHAR, NULL);
  78. pArgs->Fill(argc, wargv);
  79. if (pArgs->GetValue(IDS_PARAM_HELP))
  80. {
  81. wprintf(L"%s\n", pArgs->GetUsageString());
  82. goto NeededHelp;
  83. }
  84. if (!(pwszLoopFile = pArgs->GetFileName()))
  85. {
  86. wprintf(L"%s\n", pArgs->GetUsageString());
  87. goto ParamError;
  88. }
  89. if (!(pLoopFile = new fParse_(pwszLoopFile, &fFailed, MAX_PATH * 2)) ||
  90. fFailed)
  91. {
  92. goto MemoryError;
  93. }
  94. pLoopFile->Reset();
  95. fVerbose = (BOOL)((DWORD_PTR)pArgs->GetValue(IDS_PARAM_VERBOSE));
  96. if (pArgs->GetValue(IDS_PARAM_SSGUID))
  97. {
  98. if (!(wstr2guid((WCHAR *)pArgs->GetValue(IDS_PARAM_SSGUID), &gSS)))
  99. {
  100. wprintf(L"%s\n", pArgs->GetUsageString());
  101. goto ParamError;
  102. }
  103. }
  104. pwszCatFile = (WCHAR *)pArgs->GetValue(IDS_PARAM_ADD2DB);
  105. //
  106. // start our timer
  107. //
  108. tStart = COleDateTime::GetCurrentTime();
  109. if (!(CryptCATAdminAcquireContext(&hCatAdmin, (pwszCatFile) ? &gSS : NULL, 0)))
  110. {
  111. if (GetLastError() != dwExpectedReturn)
  112. {
  113. printf("\nERROR: unable to aquire CatAdminContext: return 0x%08X\n", GetLastError());
  114. }
  115. goto MSCATError;
  116. }
  117. while (pLoopFile->GetNextLine())
  118. {
  119. pLoopFile->EOLRemove();
  120. //
  121. // format:
  122. // catalog member tag^catalog member file^expected return code
  123. //
  124. if (!(pwszMemFile = wcschr(pLoopFile->GetCurrentLine(), STRING_SEPERATOR)))
  125. {
  126. if (fVerbose)
  127. {
  128. wprintf(L" parse error at line: %s\n", pLoopFile->GetCurrentLine());
  129. }
  130. continue;
  131. }
  132. *pwszMemFile = NULL;
  133. pwszMemFile++;
  134. if (!(pwsz = wcschr(pwszMemFile, STRING_SEPERATOR)))
  135. {
  136. if (fVerbose)
  137. {
  138. pwszMemFile--;
  139. *pwszMemFile = STRING_SEPERATOR;
  140. wprintf(L" parse error at line: %s\n", pLoopFile->GetCurrentLine());
  141. }
  142. continue;
  143. }
  144. *pwsz = NULL;
  145. pwsz++;
  146. dwExpectedReturn = (DWORD)_wtol(pwsz);
  147. _StripQuotes(pwszMemFile);
  148. _StripQuotes(pLoopFile->GetCurrentLine());
  149. if (pwszCatFile)
  150. {
  151. //
  152. // we're adding
  153. //
  154. _Add2CatDB(pwszCatFile, pLoopFile->GetCurrentLine(), pwszMemFile, dwExpectedReturn);
  155. }
  156. else
  157. {
  158. //
  159. // we're verifying
  160. //
  161. _VerifyMember(pLoopFile->GetCurrentLine(), pwszMemFile, dwExpectedReturn);
  162. }
  163. if (fVerbose)
  164. {
  165. wprintf(L"processed: %s\n", pwszMemFile);
  166. }
  167. dwTotalFiles++;
  168. }
  169. tEnd = COleDateTime::GetCurrentTime();
  170. tsTotal = tEnd - tStart;
  171. printf("\n");
  172. printf("\nTotal files processed: %ld", dwTotalFiles);
  173. printf("\nTotal Catalogs loaded: %ld", dwTotalCatalogs);
  174. printf("\nTotal errors: %ld", dwTotalErrors);
  175. printf("\nProcessing time: %s", (LPCSTR)tsTotal.Format("%D:%H:%M:%S"));
  176. printf("\nAverage seconds per file: %f", (double)tsTotal.GetTotalSeconds() / (double)dwTotalFiles);
  177. printf("\n");
  178. CommonReturn:
  179. _CloseWVTHandles();
  180. if (hCatAdmin)
  181. {
  182. CryptCATAdminReleaseContext(hCatAdmin, 0);
  183. }
  184. DELETE_OBJECT(pArgs);
  185. DELETE_OBJECT(pStateHandles);
  186. DELETE_OBJECT(pLoopFile);
  187. return(iRet);
  188. ErrorReturn:
  189. iRet = 1;
  190. goto CommonReturn;
  191. TRACE_ERROR_EX(DBG_SS_APP, MSCATError);
  192. TRACE_ERROR_EX(DBG_SS_APP, MemoryError);
  193. TRACE_ERROR_EX(DBG_SS_APP, ParamError);
  194. TRACE_ERROR_EX(DBG_SS_APP, NeededHelp);
  195. }
  196. void _Add2CatDB(WCHAR *pwszCatFile, WCHAR *pwszMemberTag, WCHAR *pwszMemberFile, DWORD dwExpectedReturn)
  197. {
  198. if (!(fCatalogAdded))
  199. {
  200. HCATINFO hCatInfo;
  201. if (!(hCatInfo = CryptCATAdminAddCatalog(hCatAdmin, pwszCatFile, NULL, 0)))
  202. {
  203. if (GetLastError() != dwExpectedReturn)
  204. {
  205. wprintf(L"\nERROR: unable to add catalog: %s: return 0x%08X\n", pwszCatFile, GetLastError());
  206. dwTotalErrors++;
  207. }
  208. return;
  209. }
  210. CryptCATAdminReleaseCatalogContext(hCatAdmin, hCatInfo, 0);
  211. fCatalogAdded = TRUE;
  212. }
  213. }
  214. void _VerifyMember(WCHAR *pwszMemberTag, WCHAR *pwszMemberFile, DWORD dwExpectedReturn)
  215. {
  216. HCATINFO hCatInfo;
  217. CATALOG_INFO sCatInfo;
  218. WCHAR wszCatFile[MAX_PATH];
  219. BYTE bHash[40];
  220. BYTE *pbHash;
  221. DWORD cbHash;
  222. HANDLE hFile;
  223. SetLastError(0);
  224. hCatInfo = NULL;
  225. hFile = INVALID_HANDLE_VALUE;
  226. if ((hFile = CreateFileU(pwszMemberFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  227. NULL)) == INVALID_HANDLE_VALUE)
  228. {
  229. goto FailedOpenFile;
  230. }
  231. cbHash = 40;
  232. pbHash = &bHash[0];
  233. if (!(CryptCATAdminCalcHashFromFileHandle(hFile, &cbHash, pbHash, 0)))
  234. {
  235. goto FailedHashCalc;
  236. }
  237. if (!(hCatInfo = CryptCATAdminEnumCatalogFromHash(hCatAdmin, pbHash, cbHash, 0, NULL)))
  238. {
  239. goto FailedEnumCatalog;
  240. }
  241. memset(&sCatInfo, 0x00, sizeof(CATALOG_INFO));
  242. sCatInfo.cbStruct = sizeof(CATALOG_INFO);
  243. if (!(CryptCATCatalogInfoFromContext(hCatInfo, &sCatInfo, 0)))
  244. {
  245. goto FailedEnumCatalog;
  246. }
  247. wcscpy(&wszCatFile[0], sCatInfo.wszCatalogFile);
  248. pwszMemberTag = wcsrchr(pwszMemberFile, L'\\');
  249. if (pwszMemberTag)
  250. {
  251. pwszMemberTag++;
  252. }
  253. else
  254. {
  255. pwszMemberTag = pwszMemberFile;
  256. }
  257. _ToLower(pwszMemberTag);
  258. WINTRUST_DATA sWTD;
  259. WINTRUST_CATALOG_INFO sWTCI;
  260. STATEINFO *psState;
  261. HRESULT hr;
  262. if (!(psState = _FindStateHandle(&wszCatFile[0])))
  263. {
  264. goto MemoryError;
  265. }
  266. memset(&sWTD, 0x00, sizeof(WINTRUST_DATA));
  267. sWTD.cbStruct = sizeof(WINTRUST_DATA);
  268. sWTD.dwUIChoice = WTD_UI_NONE;
  269. sWTD.dwUnionChoice = WTD_CHOICE_CATALOG;
  270. sWTD.pCatalog = &sWTCI;
  271. sWTD.dwStateAction = WTD_STATEACTION_VERIFY;
  272. sWTD.hWVTStateData = psState->hState;
  273. memset(&sWTCI, 0x00, sizeof(WINTRUST_CATALOG_INFO));
  274. sWTCI.cbStruct = sizeof(WINTRUST_CATALOG_INFO);
  275. sWTCI.pcwszCatalogFilePath = &wszCatFile[0];
  276. sWTCI.pcwszMemberTag = pwszMemberTag;
  277. sWTCI.pcwszMemberFilePath = pwszMemberFile;
  278. sWTCI.hMemberFile = hFile;
  279. sWTCI.pbCalculatedFileHash = pbHash;
  280. sWTCI.cbCalculatedFileHash = cbHash;
  281. hr = WinVerifyTrust(hWnd, &gAction, &sWTD);
  282. psState->hState = sWTD.hWVTStateData;
  283. CommonReturn:
  284. if (hr != (HRESULT)dwExpectedReturn)
  285. {
  286. wprintf(L"\nERROR: unexpected error from WVT for %s: return 0x%08X expected: 0x%08X lasterror: 0x%08X\n",
  287. pwszMemberTag, hr, dwExpectedReturn, GetLastError());
  288. dwTotalErrors++;
  289. }
  290. if (hFile != INVALID_HANDLE_VALUE)
  291. {
  292. CloseHandle(hFile);
  293. }
  294. if (hCatInfo)
  295. {
  296. CryptCATAdminReleaseCatalogContext(hCatAdmin, hCatInfo, 0);
  297. }
  298. return;
  299. ErrorReturn:
  300. if (GetLastError() != dwExpectedReturn)
  301. {
  302. wprintf(L"\nERROR: unable to find member: %s: return 0x%08X expected: 0x%08X\n",
  303. pwszMemberTag, GetLastError(), dwExpectedReturn);
  304. dwTotalErrors++;
  305. }
  306. hr = dwExpectedReturn;
  307. goto CommonReturn;
  308. TRACE_ERROR_EX(DBG_SS_APP, FailedHashCalc);
  309. TRACE_ERROR_EX(DBG_SS_APP, FailedOpenFile);
  310. TRACE_ERROR_EX(DBG_SS_APP, FailedEnumCatalog);
  311. SET_ERROR_VAR_EX(DBG_SS_APP, MemoryError, ERROR_NOT_ENOUGH_MEMORY);
  312. }
  313. void _CloseWVTHandles(void)
  314. {
  315. DWORD dwIdx;
  316. WINTRUST_DATA sWTD;
  317. WINTRUST_CATALOG_INFO sWTCI;
  318. STATEINFO *psState;
  319. dwIdx = 0;
  320. memset(&sWTD, 0x00, sizeof(WINTRUST_DATA));
  321. sWTD.cbStruct = sizeof(WINTRUST_DATA);
  322. sWTD.dwUIChoice = WTD_UI_NONE;
  323. sWTD.dwUnionChoice = WTD_CHOICE_CATALOG;
  324. sWTD.pCatalog = &sWTCI;
  325. sWTD.dwStateAction = WTD_STATEACTION_CLOSE;
  326. memset(&sWTCI, 0x00, sizeof(WINTRUST_CATALOG_INFO));
  327. sWTCI.cbStruct = sizeof(WINTRUST_CATALOG_INFO);
  328. while (psState = (STATEINFO *)pStateHandles->Get(dwIdx))
  329. {
  330. if (psState->hState)
  331. {
  332. sWTD.hWVTStateData = psState->hState;
  333. WinVerifyTrust(NULL, &gAction, &sWTD);
  334. }
  335. dwIdx++;
  336. }
  337. }
  338. STATEINFO * _FindStateHandle(WCHAR *pwszCatalogFile)
  339. {
  340. STATEINFO *psState;
  341. DWORD dwIdx;
  342. dwIdx = 0;
  343. while (psState = (STATEINFO *)pStateHandles->Get(dwIdx))
  344. {
  345. if (wcscmp(&psState->wszCatalogFile[0], pwszCatalogFile) == 0)
  346. {
  347. return(psState);
  348. }
  349. dwIdx++;
  350. }
  351. if (!(psState = (STATEINFO *)pStateHandles->Add(sizeof(STATEINFO))))
  352. {
  353. return(NULL);
  354. }
  355. memset(psState, 0x00, sizeof(STATEINFO));
  356. wcscpy(&psState->wszCatalogFile[0], pwszCatalogFile);
  357. dwTotalCatalogs++;
  358. return(psState);
  359. }
  360. void _StripQuotes(WCHAR *pwszIn)
  361. {
  362. DWORD dwSrc;
  363. DWORD dwDst;
  364. DWORD dwLen;
  365. dwSrc = 0;
  366. dwDst = 0;
  367. dwLen = wcslen(pwszIn);
  368. while (dwSrc < dwLen)
  369. {
  370. if (pwszIn[dwSrc] != L'\"')
  371. {
  372. pwszIn[dwDst] = pwszIn[dwSrc];
  373. dwDst++;
  374. }
  375. dwSrc++;
  376. }
  377. pwszIn[dwDst] = NULL;
  378. }
  379. void _ToLower(WCHAR *pwszInOut)
  380. {
  381. while (*pwszInOut)
  382. {
  383. *pwszInOut = towlower(*pwszInOut);
  384. pwszInOut++;
  385. }
  386. }