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.

768 lines
24 KiB

  1. #include <cdlpch.h>
  2. #include <softpub.h>
  3. #define WINTRUST TEXT("wintrust.dll")
  4. #ifdef DELAY_LOAD_WVT
  5. #ifndef _WVTP_NOCODE_
  6. Cwvt::Cwvt()
  7. {
  8. DEBUG_ENTER((DBG_DOWNLOAD,
  9. None,
  10. "Cwvt::Cwvt",
  11. "this=%#x",
  12. this
  13. ));
  14. m_fInited = FALSE;
  15. m_bHaveWTData = FALSE;
  16. DEBUG_LEAVE(0);
  17. }
  18. Cwvt::~Cwvt()
  19. {
  20. DEBUG_ENTER((DBG_DOWNLOAD,
  21. None,
  22. "Cwvt::~Cwvt",
  23. "this=%#x",
  24. this
  25. ));
  26. if (m_fInited) {
  27. FreeLibrary(m_hMod);
  28. }
  29. DEBUG_LEAVE(0);
  30. }
  31. HRESULT
  32. Cwvt::Init(void)
  33. {
  34. DEBUG_ENTER((DBG_DOWNLOAD,
  35. Hresult,
  36. "Cwvt::Init",
  37. "this=%#x",
  38. this
  39. ));
  40. GUID PublishedSoftware = WIN_SPUB_ACTION_PUBLISHED_SOFTWARE;
  41. GUID *ActionID = &PublishedSoftware;
  42. if (m_fInited) {
  43. DEBUG_LEAVE(S_OK);
  44. return S_OK;
  45. }
  46. m_hMod = LoadLibrary( WINTRUST );
  47. if (NULL == m_hMod) {
  48. DEBUG_LEAVE(HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND));
  49. return (HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND));
  50. }
  51. #define CHECKAPI(_fn) \
  52. *(FARPROC*)&(_pfn##_fn) = GetProcAddress(m_hMod, #_fn); \
  53. if (!(_pfn##_fn)) { \
  54. FreeLibrary(m_hMod); \
  55. \
  56. DEBUG_LEAVE(HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND)); \
  57. return (HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND)); \
  58. }
  59. CHECKAPI(WinVerifyTrust);
  60. if (g_bNT5OrGreater)
  61. {
  62. #define CHECKNT5API(_fn) \
  63. *(FARPROC*)&(_pfn##_fn) = GetProcAddress(m_hMod, #_fn);
  64. CHECKNT5API(IsCatalogFile);
  65. CHECKNT5API(CryptCATAdminAcquireContext);
  66. CHECKNT5API(CryptCATAdminReleaseContext);
  67. CHECKNT5API(CryptCATAdminReleaseCatalogContext);
  68. CHECKNT5API(CryptCATAdminEnumCatalogFromHash);
  69. CHECKNT5API(CryptCATAdminCalcHashFromFileHandle);
  70. CHECKNT5API(CryptCATAdminAddCatalog);
  71. CHECKNT5API(CryptCATAdminRemoveCatalog);
  72. CHECKNT5API(CryptCATCatalogInfoFromContext);
  73. CHECKNT5API(CryptCATAdminResolveCatalogPath);
  74. }
  75. m_fInited = TRUE;
  76. DEBUG_LEAVE(S_OK);
  77. return S_OK;
  78. }
  79. BOOL
  80. IsUIRestricted()
  81. {
  82. DEBUG_ENTER((DBG_DOWNLOAD,
  83. Bool,
  84. "IsUIRestricted",
  85. NULL
  86. ));
  87. HKEY hkeyRest = 0;
  88. BOOL bUIRest = FALSE;
  89. DWORD dwValue = 0;
  90. DWORD dwLen = sizeof(DWORD);
  91. // per-machine UI off policy
  92. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGSTR_PATH_INFODEL_REST, 0, KEY_READ, &hkeyRest) == ERROR_SUCCESS) {
  93. if (RegQueryValueEx( hkeyRest, REGVAL_UI_REST, NULL, NULL,
  94. (LPBYTE)&dwValue, &dwLen) == ERROR_SUCCESS && dwValue)
  95. bUIRest = TRUE;
  96. RegCloseKey(hkeyRest);
  97. }
  98. DEBUG_LEAVE(bUIRest);
  99. return bUIRest;
  100. }
  101. #endif // _WVTP_NOCODE_
  102. #endif // DELAY_LOAD_WVT
  103. // {D41E4F1D-A407-11d1-8BC9-00C04FA30A41}
  104. #define COR_POLICY_PROVIDER_DOWNLOAD \
  105. { 0xd41e4f1d, 0xa407, 0x11d1, {0x8b, 0xc9, 0x0, 0xc0, 0x4f, 0xa3, 0xa, 0x41 } }
  106. #define ZEROSTRUCT(arg) memset( &arg, 0, sizeof(arg))
  107. void PrintHash(BYTE* pbHash, DWORD cbHash)
  108. {
  109. LPSTR pszHash = NULL;
  110. if (cbHash)
  111. {
  112. char* pHexTable = "0123456789ABCDEF";
  113. pszHash = new CHAR[2*cbHash+1];
  114. if (!pszHash)
  115. goto End;
  116. for (DWORD i=0; i<cbHash; i++)
  117. {
  118. pszHash[2*i] = pHexTable[(pbHash[i]&0xf0)>>4];
  119. pszHash[2*i+1] = pHexTable[pbHash[i]&0x0f];
  120. }
  121. pszHash[2*i] = '\0';
  122. }
  123. DEBUG_ENTER((DBG_DOWNLOAD,
  124. None,
  125. "PrintHash",
  126. "%d, %.80q",
  127. cbHash, (pszHash?pszHash:"NULL")
  128. ));
  129. DEBUG_LEAVE(0);
  130. if(pszHash)
  131. delete [] pszHash;
  132. End:
  133. return;
  134. }
  135. HRESULT Cwvt::WinVerifyTrust_Wrap(HWND hwnd, GUID * ActionID, WINTRUST_DATA* ActionData)
  136. {
  137. DEBUG_ENTER((DBG_DOWNLOAD,
  138. Hresult,
  139. "EXTERNAL::WinVerifyTrust",
  140. "%#x, %#x, %#x",
  141. hwnd, ActionID, ActionData
  142. ));
  143. HRESULT hr;
  144. hr = WinVerifyTrust(hwnd, ActionID, ActionData);
  145. DEBUG_LEAVE(hr);
  146. return hr;
  147. }
  148. #ifdef DBG
  149. #define PRINT_HASH(pbHash, cbHash) PrintHash((pbHash),(cbHash))
  150. #else
  151. #define PRINT_HASH(pbHash, cbHash) {}
  152. #endif
  153. HRESULT Cwvt::VerifyTrust(HANDLE hFile, HWND hWnd, PJAVA_TRUST *ppJavaTrust,
  154. LPCWSTR szStatusText,
  155. IInternetHostSecurityManager *pHostSecurityManager,
  156. LPSTR szFilePath, LPSTR szCatalogFile,
  157. CDownload *pdl)
  158. {
  159. DEBUG_ENTER((DBG_DOWNLOAD,
  160. Hresult,
  161. "Cwvt::VerifyTrust",
  162. "this=%#x, %#x, %#x, %#x, %.80wq, %#x, %.80q, %#x=%.80q, %#x",
  163. this, hFile, hWnd, ppJavaTrust, szStatusText, pHostSecurityManager,
  164. szFilePath, szCatalogFile, szCatalogFile, pdl
  165. ));
  166. LPWSTR wzFileName = NULL;
  167. LPWSTR wzFilePath = NULL;
  168. LPWSTR wzCatalogFile = NULL;
  169. GUID guidJava = JAVA_POLICY_PROVIDER_DOWNLOAD;
  170. GUID guidCor = COR_POLICY_PROVIDER_DOWNLOAD;
  171. GUID guidAuthenticode = WINTRUST_ACTION_GENERIC_VERIFY_V2;
  172. GUID *pguidActionIDJava = &guidJava;
  173. GUID *pguidActionIDCor = &guidCor;
  174. WINTRUST_DATA wintrustData;
  175. WINTRUST_DATA wtdAuthenticode;
  176. WINTRUST_FILE_INFO fileData;
  177. JAVA_POLICY_PROVIDER javaPolicyData;
  178. WCHAR wpath [MAX_PATH];
  179. PJAVA_TRUST pbJavaTrust = NULL;
  180. IServiceProvider *pServProv = NULL;
  181. LPCATALOGFILEINFO pcfi = NULL;
  182. HRESULT hr = S_OK;
  183. ZEROSTRUCT(wintrustData);
  184. ZEROSTRUCT(fileData);
  185. ZEROSTRUCT(javaPolicyData);
  186. javaPolicyData.cbSize = sizeof(JAVA_POLICY_PROVIDER);
  187. javaPolicyData.VMBased = FALSE;
  188. javaPolicyData.fNoBadUI = FALSE;
  189. javaPolicyData.pwszZone = szStatusText;
  190. javaPolicyData.pZoneManager = (LPVOID)pHostSecurityManager;
  191. fileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
  192. fileData.pcwszFilePath = szStatusText;
  193. fileData.hFile = hFile;
  194. wintrustData.cbStruct = sizeof(WINTRUST_DATA);
  195. wintrustData.pPolicyCallbackData = &javaPolicyData;
  196. if ( (hWnd == INVALID_HANDLE_VALUE) || IsUIRestricted())
  197. wintrustData.dwUIChoice = WTD_UI_NONE;
  198. else
  199. wintrustData.dwUIChoice = WTD_UI_ALL;
  200. wintrustData.dwUnionChoice = WTD_CHOICE_FILE;
  201. wintrustData.pFile = &fileData;
  202. if (szCatalogFile) {
  203. ::Ansi2Unicode(szCatalogFile, &wzCatalogFile);
  204. ::Ansi2Unicode(szFilePath, &wzFilePath);
  205. wzFileName = PathFindFileNameW(szStatusText);
  206. if (!m_bHaveWTData) {
  207. memset(&m_wtCatalogInfo, 0x0, sizeof(m_wtCatalogInfo));
  208. m_wtCatalogInfo.cbStruct = sizeof(WINTRUST_CATALOG_INFO);
  209. m_bHaveWTData = TRUE;
  210. }
  211. m_wtCatalogInfo.pcwszCatalogFilePath = wzCatalogFile;
  212. m_wtCatalogInfo.pcwszMemberTag = wzFileName;
  213. m_wtCatalogInfo.pcwszMemberFilePath = wzFilePath;
  214. wtdAuthenticode = wintrustData;
  215. wtdAuthenticode.pCatalog = &m_wtCatalogInfo;
  216. wtdAuthenticode.dwUnionChoice = WTD_CHOICE_CATALOG;
  217. wtdAuthenticode.dwStateAction = WTD_STATEACTION_VERIFY;
  218. wtdAuthenticode.dwUIChoice = WTD_UI_NONE;
  219. hr = WinVerifyTrust_Wrap(hWnd, &guidAuthenticode, &wtdAuthenticode);
  220. if (FAILED(hr)) {
  221. hr = WinVerifyTrust_Wrap(hWnd, pguidActionIDCor, &wintrustData);
  222. if (hr == TRUST_E_PROVIDER_UNKNOWN)
  223. hr = WinVerifyTrust_Wrap(hWnd, pguidActionIDJava, &wintrustData);
  224. }
  225. else {
  226. // Clone Java permissions
  227. pbJavaTrust = pdl->GetCodeDownload()->GetJavaTrust();
  228. if (!pbJavaTrust) {
  229. hr = pdl->GetBSC()->QueryInterface(IID_IServiceProvider, (void **)&pServProv);
  230. if (SUCCEEDED(hr)) {
  231. hr = pServProv->QueryService(IID_ICatalogFileInfo, IID_ICatalogFileInfo, (void **)&pcfi);
  232. if (SUCCEEDED(hr)) {
  233. pcfi->GetJavaTrust((void **)&pbJavaTrust);
  234. }
  235. }
  236. SAFERELEASE(pServProv);
  237. SAFERELEASE(pcfi);
  238. pdl->SetMainCABJavaTrustPermissions(pbJavaTrust);
  239. }
  240. }
  241. }
  242. else {
  243. hr = WinVerifyTrust_Wrap(hWnd, pguidActionIDCor, &wintrustData);
  244. if (hr == TRUST_E_PROVIDER_UNKNOWN)
  245. hr = WinVerifyTrust_Wrap(hWnd, pguidActionIDJava, &wintrustData);
  246. if (SUCCEEDED(hr)) {
  247. pdl->SetMainCABJavaTrustPermissions(javaPolicyData.pbJavaTrust);
  248. }
  249. }
  250. SAFEDELETE(wzCatalogFile);
  251. SAFEDELETE(wzFilePath);
  252. // BUGBUG: this works around a wvt bug that returns 0x57 (success) when
  253. // you hit No to an usigned control
  254. if (SUCCEEDED(hr) && hr != S_OK) {
  255. hr = TRUST_E_FAIL;
  256. }
  257. if (FAILED(hr)) {
  258. // display original hr intact to help debugging
  259. // CodeDownloadDebugOut(DEB_CODEDL, TRUE, ID_CDLDBG_VERIFYTRUST_FAILED, hr);
  260. } else {
  261. *ppJavaTrust = javaPolicyData.pbJavaTrust;
  262. }
  263. if (hr == TRUST_E_SUBJECT_NOT_TRUSTED && wintrustData.dwUIChoice == WTD_UI_NONE) {
  264. // if we didn't ask for the UI to be out up there has been no UI
  265. // work around WVT bvug that it returns us this special error code
  266. // without putting up UI.
  267. hr = TRUST_E_FAIL; // this will put up mshtml ui after the fact
  268. // that security settings prevented us
  269. }
  270. if (FAILED(hr) && (hr != TRUST_E_SUBJECT_NOT_TRUSTED)) {
  271. // trust system has failed without UI
  272. // map error to this generic error that will falg our client to put
  273. // up additional info that this is a trust system error if reqd.
  274. hr = TRUST_E_FAIL;
  275. }
  276. if (hr == TRUST_E_SUBJECT_NOT_TRUSTED) {
  277. pdl->GetCodeDownload()->SetUserDeclined();
  278. }
  279. DEBUG_LEAVE(hr);
  280. return hr;
  281. }
  282. HRESULT
  283. Cwvt::VerifyTrustOnCatalogFile(IN LPCWSTR pwszCatalogFile)
  284. {
  285. DEBUG_ENTER((DBG_DOWNLOAD,
  286. Hresult,
  287. "Cwvt::VerifyTrustOnCatalogFile",
  288. "this=%#x, %.200wq",
  289. this, pwszCatalogFile
  290. ));
  291. HRESULT hr = NOERROR;
  292. WINTRUST_DATA WintrustData;
  293. WINTRUST_FILE_INFO WintrustFileInfo;
  294. GUID guidDriverActionVerify = DRIVER_ACTION_VERIFY;
  295. ZeroMemory(&WintrustData, sizeof(WINTRUST_DATA));
  296. WintrustData.cbStruct = sizeof(WINTRUST_DATA);
  297. WintrustData.dwUIChoice = WTD_UI_NONE;
  298. WintrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
  299. WintrustData.dwUnionChoice = WTD_CHOICE_FILE;
  300. WintrustData.pFile = &WintrustFileInfo;
  301. WintrustData.dwProvFlags = WTD_REVOCATION_CHECK_NONE;
  302. ZeroMemory(&WintrustFileInfo, sizeof(WINTRUST_FILE_INFO));
  303. WintrustFileInfo.cbStruct = sizeof(WINTRUST_FILE_INFO);
  304. WintrustFileInfo.pcwszFilePath = pwszCatalogFile;
  305. hr = WinVerifyTrust_Wrap(NULL, &guidDriverActionVerify, &WintrustData);
  306. DEBUG_LEAVE(hr);
  307. return hr;
  308. }
  309. HRESULT Cwvt::IsValidCatalogFile(LPCWSTR pwszCatalogFile)
  310. {
  311. DEBUG_ENTER((DBG_DOWNLOAD,
  312. Hresult,
  313. "Cwvt::IsValidCatalogFile",
  314. "this=%#x, %.200wq",
  315. this, pwszCatalogFile
  316. ));
  317. HRESULT hr = E_FAIL;
  318. DWORD cbLen = lstrlenW(pwszCatalogFile);
  319. WCHAR* pwszCatalogFileCopy = new WCHAR[cbLen+1];
  320. if (pwszCatalogFileCopy)
  321. {
  322. StrCpyW(pwszCatalogFileCopy, pwszCatalogFile);
  323. // IsCatalogFile doesn't take LPCWSTR, only a LPWSTR - may mangle url.
  324. if(IsCatalogFile(NULL, pwszCatalogFileCopy))
  325. {
  326. StrCpyW(pwszCatalogFileCopy, pwszCatalogFile);
  327. hr = VerifyTrustOnCatalogFile(pwszCatalogFile);
  328. }
  329. }
  330. delete [] pwszCatalogFileCopy;
  331. DEBUG_LEAVE(hr);
  332. return hr;
  333. }
  334. /*
  335. return value:
  336. S_OK - all ok.
  337. S_FALSE - AddCatalog succeeded, but getting Cataloginfo failed.
  338. E_FAIL - any other failure
  339. */
  340. HRESULT Cwvt::InstallCatalogFile(LPSTR pszCatalogFile)
  341. {
  342. DEBUG_ENTER((DBG_DOWNLOAD,
  343. Hresult,
  344. "Cwvt::InstallCatalogFile",
  345. "this=%#x, %.200q",
  346. this, pszCatalogFile
  347. ));
  348. WCHAR* pwszCatalogFile = NULL;
  349. HRESULT hr;
  350. HCATADMIN hCatAdmin;
  351. GUID guidDriverActionVerify = DRIVER_ACTION_VERIFY;
  352. hr = Ansi2Unicode(pszCatalogFile, &pwszCatalogFile);
  353. if(SUCCEEDED(hr)
  354. && SUCCEEDED(hr = IsValidCatalogFile(pwszCatalogFile))
  355. && CryptCATAdminAcquireContext(&hCatAdmin, &guidDriverActionVerify, 0))
  356. {
  357. HCATINFO hCatInfo;
  358. CATALOG_INFO CatalogInfo;
  359. hCatInfo = CryptCATAdminAddCatalog(hCatAdmin, pwszCatalogFile, NULL, 0);
  360. if (hCatInfo)
  361. {
  362. CatalogInfo.cbStruct = sizeof(CATALOG_INFO);
  363. if (CryptCATCatalogInfoFromContext(hCatInfo, &CatalogInfo, 0))
  364. {
  365. #ifdef DBG
  366. DEBUG_PRINT(DOWNLOAD,
  367. INFO,
  368. ("Cwvt::InstallCatalogFile: cat_filename: %.200wq \n",
  369. (CatalogInfo.wszCatalogFile ? CatalogInfo.wszCatalogFile : L"NULL")
  370. ));
  371. #endif
  372. /* can get name and full path of catalog file if need be
  373. StrCpyW(pwszFullPathCatalogFile, CatalogInfo.wszCatalogFile);
  374. */
  375. hr = S_OK;
  376. }
  377. else
  378. {
  379. hr = S_FALSE;
  380. }
  381. CryptCATAdminReleaseCatalogContext(hCatAdmin, hCatInfo, 0);
  382. }
  383. else
  384. {
  385. hr = E_FAIL;
  386. }
  387. CryptCATAdminReleaseContext(hCatAdmin, 0);
  388. }
  389. else
  390. {
  391. hr = E_FAIL;
  392. }
  393. if (pwszCatalogFile)
  394. delete [] pwszCatalogFile;
  395. DEBUG_LEAVE(hr);
  396. return hr;
  397. }
  398. // This function verifies the specified file against the database in the
  399. // specified subsystem.
  400. // NOTE: *pdwBuffer = count of *WIDECHARs* in pwszFullPathCatalogFile.
  401. HRESULT Cwvt::VerifyFileAgainstSystemCatalog(LPCSTR pcszFile, LPWSTR pwszFullPathCatalogFile, DWORD* pdwBuffer)
  402. {
  403. DEBUG_ENTER((DBG_DOWNLOAD,
  404. Hresult,
  405. "Cwvt::VerifyFileAgainstSystemCatalog",
  406. "this=%#x, %.90q, %.90wq, %#x[%d]",
  407. this, pcszFile, pwszFullPathCatalogFile, pdwBuffer, (pdwBuffer?*pdwBuffer:0)
  408. ));
  409. HCATADMIN hAdmin = NULL ;
  410. HRESULT hr = E_FAIL;
  411. BOOL fOK = FALSE;
  412. HANDLE hFile = NULL ;
  413. DWORD cbHash;
  414. BYTE* pbHash = NULL;
  415. HCATINFO hCatInfo = NULL ;
  416. GUID guidDriverActionVerify = DRIVER_ACTION_VERIFY;
  417. DEBUG_ENTER((DBG_DOWNLOAD,
  418. Dword,
  419. "EXTERNAL::CreateFile",
  420. "%.80q",
  421. pcszFile
  422. ));
  423. // Open the file for read-only access
  424. hFile = CreateFile(
  425. pcszFile,
  426. GENERIC_READ,
  427. FILE_SHARE_READ,
  428. NULL,
  429. OPEN_EXISTING,
  430. FILE_ATTRIBUTE_NORMAL,
  431. NULL
  432. ) ;
  433. DEBUG_LEAVE(hFile);
  434. if (INVALID_HANDLE_VALUE != hFile)
  435. {
  436. // Acquire a catalog context
  437. fOK = CryptCATAdminAcquireContext(
  438. &hAdmin,
  439. &guidDriverActionVerify,
  440. 0
  441. ) ;
  442. if (fOK)
  443. {
  444. // Determine the hash of the file
  445. fOK = CryptCATAdminCalcHashFromFileHandle(
  446. hFile,
  447. &cbHash,
  448. NULL,
  449. 0
  450. );
  451. DEBUG_ENTER((DBG_DOWNLOAD,
  452. Bool,
  453. "CryptCATAdminCalcHashFromFileHandle",
  454. "%d",
  455. cbHash)
  456. );
  457. DEBUG_LEAVE(fOK);
  458. if (fOK && (pbHash = new BYTE[cbHash]))
  459. {
  460. fOK = CryptCATAdminCalcHashFromFileHandle(
  461. hFile,
  462. &cbHash,
  463. pbHash,
  464. 0
  465. ) ;
  466. PRINT_HASH(pbHash, cbHash);
  467. if (fOK)
  468. {
  469. // Find the first catalog that contains this hash
  470. hCatInfo = CryptCATAdminEnumCatalogFromHash(
  471. hAdmin,
  472. pbHash,
  473. cbHash,
  474. 0,
  475. NULL
  476. ) ;
  477. if (hCatInfo) // A catalog was found
  478. {
  479. hr = S_OK;
  480. CATALOG_INFO CatalogInfo;
  481. if (pwszFullPathCatalogFile)
  482. {
  483. CatalogInfo.cbStruct = sizeof(CATALOG_INFO);
  484. DWORD dwLen = 0;
  485. if (CryptCATCatalogInfoFromContext(hCatInfo, &CatalogInfo, 0)
  486. && pdwBuffer)
  487. {
  488. if (CatalogInfo.wszCatalogFile
  489. && ((dwLen = lstrlenW(CatalogInfo.wszCatalogFile)) <= *pdwBuffer))
  490. {
  491. StrCpyW(pwszFullPathCatalogFile, CatalogInfo.wszCatalogFile);
  492. }
  493. else
  494. {
  495. hr = S_FALSE;
  496. *pdwBuffer = dwLen+1;
  497. }
  498. }
  499. }
  500. #ifdef DBG
  501. {
  502. CatalogInfo.cbStruct = sizeof(CATALOG_INFO);
  503. if (CryptCATCatalogInfoFromContext(hCatInfo, &CatalogInfo, 0))
  504. {
  505. DEBUG_PRINT(DOWNLOAD,
  506. INFO,
  507. ("Cwvt::VerifyFileAgainstSystemCatalog: cat_filename: %.200wq \n",
  508. (CatalogInfo.wszCatalogFile ? CatalogInfo.wszCatalogFile : L"NULL")
  509. ));
  510. }
  511. }
  512. #endif
  513. CryptCATAdminReleaseCatalogContext(
  514. hAdmin,
  515. hCatInfo,
  516. 0
  517. ) ;
  518. }//hCatInfo
  519. }//fOK
  520. }//(fOK && (pbHash = new BYTE[cbHash]))
  521. CryptCATAdminReleaseContext( hAdmin, 0 ) ;
  522. }//fOK
  523. CloseHandle( hFile ) ;
  524. }//(INVALID_HANDLE_VALUE != hFile)
  525. if (pbHash)
  526. delete [] pbHash;
  527. DEBUG_LEAVE(hr);
  528. return hr ;
  529. }
  530. /*
  531. return value:
  532. S_OK - all ok.
  533. S_FALSE - failed to remove catalog.
  534. */
  535. HRESULT Cwvt::UninstallCatalogFile(LPWSTR pwszCatalogFile)
  536. {
  537. DEBUG_ENTER((DBG_DOWNLOAD,
  538. Hresult,
  539. "Cwvt::UninstallCatalogFile",
  540. "this=%#x, %.90wq",
  541. this, pwszCatalogFile
  542. ));
  543. HRESULT hr = S_FALSE;
  544. HCATADMIN hCatAdmin;
  545. GUID guidDriverActionVerify = DRIVER_ACTION_VERIFY;
  546. if (CryptCATAdminAcquireContext(&hCatAdmin, &guidDriverActionVerify, 0))
  547. {
  548. if(CryptCATAdminRemoveCatalog(hCatAdmin, pwszCatalogFile, 0))
  549. {
  550. hr = S_OK;
  551. }
  552. CryptCATAdminReleaseContext(hCatAdmin, 0);
  553. }
  554. DEBUG_LEAVE(hr);
  555. return hr;
  556. }
  557. HRESULT GetActivePolicy(IInternetHostSecurityManager* pZoneManager,
  558. LPCWSTR pwszZone,
  559. DWORD dwUrlAction,
  560. DWORD& dwPolicy,
  561. BOOL fEnforceRestricted)
  562. {
  563. DEBUG_ENTER((DBG_DOWNLOAD,
  564. Hresult,
  565. "GetActivePolicy",
  566. "%#x, %.80wq, %#x, %#x, #B",
  567. pZoneManager, pwszZone, dwUrlAction, &dwPolicy, fEnforceRestricted
  568. ));
  569. HRESULT hr = TRUST_E_FAIL;
  570. HRESULT hr2 = TRUST_E_FAIL;
  571. DWORD cbPolicy = sizeof(DWORD);
  572. // Policy are ordered such that high numbers are the most conservative
  573. // and the lower numbers are less conservative
  574. DWORD dwDocumentPolicy = URLPOLICY_ALLOW;
  575. DWORD dwUrlPolicy = URLPOLICY_ALLOW;
  576. // We are going for the most conservative so lets set the
  577. // value to the least conservative
  578. dwPolicy = URLPOLICY_ALLOW;
  579. IInternetSecurityManager* iSM = NULL;
  580. DWORD dwPUAflags = fEnforceRestricted ? PUAF_ENFORCERESTRICTED : 0;
  581. // Ask the document base for its policy
  582. if(pZoneManager) { // Given a IInternetHostSecurityManager
  583. hr = pZoneManager->ProcessUrlAction(dwUrlAction,
  584. (PBYTE) &dwDocumentPolicy,
  585. cbPolicy,
  586. NULL,
  587. 0,
  588. dwPUAflags | PUAF_NOUI,
  589. 0);
  590. }
  591. // Get the policy for the URL
  592. if(pwszZone) { // Create an IInternetSecurityManager
  593. hr2 = CoInternetCreateSecurityManager(NULL,
  594. &iSM,
  595. 0);
  596. if(hr2 == S_OK) { // We got the manager so get the policy info
  597. hr2 = iSM->ProcessUrlAction(pwszZone,
  598. dwUrlAction,
  599. (PBYTE) &dwUrlPolicy,
  600. cbPolicy,
  601. NULL,
  602. 0,
  603. dwPUAflags | PUAF_NOUI,
  604. 0);
  605. iSM->Release();
  606. }
  607. else
  608. iSM = NULL;
  609. }
  610. // if they both failed and we have zones then set it to deny and return an error
  611. if(FAILED(hr) && FAILED(hr2)) {
  612. // If we failed because there are on zones then lets QUERY
  613. // BUGBUG: we should actually try to get the IE30 security policy here.
  614. if(iSM == NULL && pZoneManager == NULL) {
  615. dwPolicy = URLPOLICY_QUERY;
  616. hr = S_OK;
  617. }
  618. else {
  619. dwPolicy = URLPOLICY_DISALLOW;
  620. hr = TRUST_E_FAIL;
  621. }
  622. }
  623. else {
  624. if(SUCCEEDED(hr))
  625. dwPolicy = dwDocumentPolicy;
  626. if(SUCCEEDED(hr2))
  627. dwPolicy = dwPolicy > dwUrlPolicy ? dwPolicy : dwUrlPolicy;
  628. if (dwPolicy == URLPOLICY_DISALLOW)
  629. hr = TRUST_E_FAIL;
  630. else
  631. hr = S_OK;
  632. }
  633. DEBUG_LEAVE(hr);
  634. return hr;
  635. }