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.

5971 lines
169 KiB

  1. //+---------------------------------------------------------------------------
  2. /////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Microsoft Windows
  5. // Copyright (C) Microsoft Corporation, 1997-2001.
  6. //
  7. // File: compdata.cpp
  8. //
  9. // Contents: Implementation of CCertMgrComponentData
  10. //
  11. //----------------------------------------------------------------------------
  12. #include "stdafx.h"
  13. USE_HANDLE_MACROS ("CERTMGR (compdata.cpp)")
  14. #include <gpedit.h>
  15. #include "compdata.h"
  16. #include "dataobj.h"
  17. #include "cookie.h"
  18. #include "snapmgr.h"
  19. #include "Certifct.h"
  20. #include "dlgs.h"
  21. #include "SelAcct.h"
  22. #include "FindDlg.h"
  23. #pragma warning(push, 3)
  24. #include <wintrust.h>
  25. #include <cryptui.h>
  26. #include <sceattch.h>
  27. #pragma warning(pop)
  28. #include "selservc.h"
  29. #include "acrgenpg.h"
  30. #include "acrspsht.h"
  31. #include "acrswlcm.h"
  32. #include "acrstype.h"
  33. #include "acrslast.h"
  34. #include "addsheet.h"
  35. #include "gpepage.h"
  36. #include "password.h"
  37. #include "storegpe.h"
  38. #include "uuids.h"
  39. #include "StoreRSOP.h"
  40. #include "PolicyPrecedencePropertyPage.h"
  41. #include "AutoenrollmentPropertyPage.h"
  42. #include "SaferEntry.h"
  43. #include "SaferUtil.h"
  44. #include "SaferDefinedFileTypesPropertyPage.h"
  45. #include "EFSGeneralPropertyPage.h"
  46. #ifdef _DEBUG
  47. #ifndef ALPHA
  48. #define new DEBUG_NEW
  49. #endif
  50. #undef THIS_FILE
  51. static char THIS_FILE[] = __FILE__;
  52. #endif
  53. #include "stdcdata.cpp" // CComponentData implementation
  54. extern HINSTANCE g_hInstance;
  55. extern GUID g_guidExtension;
  56. extern GUID g_guidRegExt;
  57. extern GUID g_guidSnapin;
  58. //
  59. // CCertMgrComponentData
  60. //
  61. extern CString g_szFileName; // If not empty, was called from command-line.
  62. CCertMgrComponentData::CCertMgrComponentData ()
  63. : m_pRootCookie (0),
  64. m_activeViewPersist (IDM_STORE_VIEW),
  65. m_hRootScopeItem (0),
  66. m_bShowPhysicalStoresPersist (0),
  67. m_bShowArchivedCertsPersist (0),
  68. m_fAllowOverrideMachineName (0),
  69. m_dwFlagsPersist (0),
  70. m_dwLocationPersist (0),
  71. m_pResultData (0),
  72. m_pGPEInformation (0),
  73. m_pRSOPInformationComputer (0),
  74. m_pRSOPInformationUser (0),
  75. m_bIsUserAdministrator (FALSE),
  76. m_dwSCEMode (SCE_MODE_UNKNOWN),
  77. m_pHeader (0),
  78. m_bMultipleObjectsSelected (false) ,
  79. m_pCryptUIMMCCallbackStruct (0),
  80. m_pGPERootStore (0),
  81. m_pGPETrustStore (0),
  82. m_pFileBasedStore (0),
  83. m_pGPEACRSUserStore (0),
  84. m_pGPEACRSComputerStore (0),
  85. m_fInvalidComputer (false),
  86. m_bMachineIsStandAlone (true),
  87. m_pComponentConsole (0),
  88. m_bIsRSOP (false),
  89. m_pIWbemServicesComputer (0),
  90. m_pIWbemServicesUser (0),
  91. m_pbstrLanguage (SysAllocString (STR_WQL)),
  92. m_pbstrQuery (SysAllocString (STR_SELECT_STATEMENT)),
  93. m_pbstrValueName (SysAllocString (STR_PROP_VALUENAME)),
  94. m_pbstrRegistryKey (SysAllocString (STR_PROP_REGISTRYKEY)),
  95. m_pbstrValue (SysAllocString (STR_PROP_VALUE)),
  96. m_pbstrPrecedence (SysAllocString (STR_PROP_PRECEDENCE)),
  97. m_pbstrGPOid (SysAllocString (STR_PROP_GPOID)),
  98. m_dwRSOPFlagsComputer (0),
  99. m_dwRSOPFlagsUser (0),
  100. m_dwDefaultSaferLevel (0),
  101. m_pdwSaferLevels (0),
  102. m_bSaferSupported (false)
  103. {
  104. _TRACE (1, L"Entering CCertMgrComponentData::CCertMgrComponentData\n");
  105. m_pRootCookie = new CCertMgrCookie (CERTMGR_SNAPIN);
  106. // Get name of logged-in user
  107. DWORD dwSize = 0;
  108. ::GetUserName (0, &dwSize);
  109. BOOL bRet = ::GetUserName (m_szLoggedInUser.GetBufferSetLength (dwSize), &dwSize);
  110. ASSERT (bRet);
  111. m_szLoggedInUser.ReleaseBuffer ();
  112. // Get name of this computer
  113. dwSize = MAX_COMPUTERNAME_LENGTH + 1 ;
  114. bRet = ::GetComputerName (m_szThisComputer.GetBufferSetLength (MAX_COMPUTERNAME_LENGTH + 1 ), &dwSize);
  115. ASSERT (bRet);
  116. m_szThisComputer.ReleaseBuffer ();
  117. // Find out if logged-in users is an Administrator
  118. IsUserAdministrator (m_bIsUserAdministrator);
  119. if ( !g_szFileName.IsEmpty () )
  120. {
  121. m_szFileName = g_szFileName;
  122. g_szFileName = _T ("");
  123. }
  124. // Find out if we're joined to a domain.
  125. PDSROLE_PRIMARY_DOMAIN_INFO_BASIC pInfo = 0;
  126. DWORD dwErr = ::DsRoleGetPrimaryDomainInformation (
  127. 0,
  128. DsRolePrimaryDomainInfoBasic,
  129. (PBYTE*) &pInfo);
  130. if ( ERROR_SUCCESS == dwErr )
  131. {
  132. switch (pInfo->MachineRole)
  133. {
  134. case DsRole_RoleStandaloneWorkstation:
  135. case DsRole_RoleStandaloneServer:
  136. m_bMachineIsStandAlone = true;
  137. break;
  138. case DsRole_RoleMemberWorkstation:
  139. case DsRole_RoleMemberServer:
  140. case DsRole_RoleBackupDomainController:
  141. case DsRole_RolePrimaryDomainController:
  142. m_bMachineIsStandAlone = false;
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. else
  149. {
  150. _TRACE (0, L"DsRoleGetPrimaryDomainInformation () failed: 0x%x\n", dwErr);
  151. }
  152. NetApiBufferFree (pInfo);
  153. _TRACE (-1, L"Leaving CCertMgrComponentData::CCertMgrComponentData\n");
  154. }
  155. CCertMgrComponentData::~CCertMgrComponentData ()
  156. {
  157. _TRACE (1, L"Entering CCertMgrComponentData::~CCertMgrComponentData\n");
  158. if ( m_pCryptUIMMCCallbackStruct )
  159. {
  160. ::MMCFreeNotifyHandle (m_pCryptUIMMCCallbackStruct->lNotifyHandle);
  161. ((LPDATAOBJECT)(m_pCryptUIMMCCallbackStruct->param))->Release ();
  162. ::GlobalFree (m_pCryptUIMMCCallbackStruct);
  163. m_pCryptUIMMCCallbackStruct = 0;
  164. }
  165. if ( m_pGPERootStore )
  166. {
  167. m_pGPERootStore->Release ();
  168. m_pGPERootStore = 0;
  169. }
  170. if ( m_pGPETrustStore )
  171. {
  172. m_pGPETrustStore->Release ();
  173. m_pGPETrustStore = 0;
  174. }
  175. if ( m_pFileBasedStore )
  176. {
  177. m_pFileBasedStore->Release ();
  178. m_pFileBasedStore = 0;
  179. }
  180. if ( m_pGPEACRSUserStore )
  181. {
  182. m_pGPEACRSUserStore->Release ();
  183. m_pGPEACRSUserStore = 0;
  184. }
  185. if ( m_pGPEACRSComputerStore )
  186. {
  187. m_pGPEACRSComputerStore->Release ();
  188. m_pGPEACRSComputerStore = 0;
  189. }
  190. CCookie& rootCookie = QueryBaseRootCookie ();
  191. while ( !rootCookie.m_listResultCookieBlocks.IsEmpty() )
  192. {
  193. (rootCookie.m_listResultCookieBlocks.RemoveHead())->Release();
  194. }
  195. if ( m_pGPEInformation )
  196. {
  197. m_pGPEInformation->Release ();
  198. m_pGPEInformation = 0;
  199. }
  200. if ( m_pRSOPInformationComputer )
  201. {
  202. m_pRSOPInformationComputer->Release ();
  203. m_pRSOPInformationComputer = 0;
  204. }
  205. if ( m_pRSOPInformationUser )
  206. {
  207. m_pRSOPInformationUser->Release ();
  208. m_pRSOPInformationUser = 0;
  209. }
  210. if ( m_pResultData )
  211. {
  212. m_pResultData->Release ();
  213. m_pResultData = 0;
  214. }
  215. if ( m_pComponentConsole )
  216. {
  217. SAFE_RELEASE (m_pComponentConsole);
  218. m_pComponentConsole = 0;
  219. }
  220. if (m_pbstrLanguage)
  221. SysFreeString (m_pbstrLanguage);
  222. if (m_pbstrQuery)
  223. SysFreeString (m_pbstrQuery);
  224. if (m_pbstrRegistryKey)
  225. SysFreeString (m_pbstrRegistryKey);
  226. if (m_pbstrValueName)
  227. SysFreeString (m_pbstrValueName);
  228. if (m_pbstrValue)
  229. SysFreeString (m_pbstrValue);
  230. if ( m_pbstrPrecedence )
  231. SysFreeString (m_pbstrPrecedence);
  232. if ( m_pbstrGPOid )
  233. SysFreeString (m_pbstrGPOid);
  234. int nIndex = 0;
  235. INT_PTR nUpperBound = m_rsopObjectArrayComputer.GetUpperBound ();
  236. while ( nUpperBound >= nIndex )
  237. {
  238. CRSOPObject* pCurrObject = m_rsopObjectArrayComputer.GetAt (nIndex);
  239. if ( pCurrObject )
  240. {
  241. delete pCurrObject;
  242. }
  243. nIndex++;
  244. }
  245. m_rsopObjectArrayComputer.RemoveAll ();
  246. nIndex = 0;
  247. nUpperBound = m_rsopObjectArrayUser.GetUpperBound ();
  248. while ( nUpperBound >= nIndex )
  249. {
  250. CRSOPObject* pCurrObject = m_rsopObjectArrayUser.GetAt (nIndex);
  251. if ( pCurrObject )
  252. {
  253. delete pCurrObject;
  254. }
  255. nIndex++;
  256. }
  257. m_rsopObjectArrayUser.RemoveAll ();
  258. if ( m_pIWbemServicesComputer )
  259. m_pIWbemServicesComputer->Release ();
  260. if ( m_pIWbemServicesUser )
  261. m_pIWbemServicesUser->Release ();
  262. if ( m_pRootCookie )
  263. m_pRootCookie->Release ();
  264. if ( m_pdwSaferLevels )
  265. delete m_pdwSaferLevels;
  266. _TRACE (-1, L"Leaving CCertMgrComponentData::~CCertMgrComponentData\n");
  267. }
  268. DEFINE_FORWARDS_MACHINE_NAME ( CCertMgrComponentData, (m_pRootCookie) )
  269. CCookie& CCertMgrComponentData::QueryBaseRootCookie ()
  270. {
  271. _TRACE (0, L"Entering and leaving CCertMgrComponentData::QueryBaseRootCookie\n");
  272. ASSERT (m_pRootCookie);
  273. return (CCookie&) *m_pRootCookie;
  274. }
  275. STDMETHODIMP CCertMgrComponentData::CreateComponent (LPCOMPONENT* ppComponent)
  276. {
  277. _TRACE (1, L"Entering CCertMgrComponentData::CreateComponent\n");
  278. ASSERT (ppComponent);
  279. CComObject<CCertMgrComponent>* pObject = 0;
  280. CComObject<CCertMgrComponent>::CreateInstance (&pObject);
  281. ASSERT (pObject);
  282. pObject->SetComponentDataPtr ( (CCertMgrComponentData*) this);
  283. HRESULT hr = pObject->QueryInterface (IID_PPV_ARG (IComponent, ppComponent));
  284. _TRACE (1, L"Entering CCertMgrComponentData::CreateComponent\n");
  285. return hr;
  286. }
  287. HRESULT CCertMgrComponentData::LoadIcons (LPIMAGELIST pImageList, BOOL /*fLoadLargeIcons*/)
  288. {
  289. _TRACE (1, L"Entering CCertMgrComponentData::LoadIcons\n");
  290. // Structure to map a Resource ID to an index of icon
  291. struct RESID2IICON
  292. {
  293. UINT uIconId; // Icon resource ID
  294. int iIcon; // Index of the icon in the image list
  295. };
  296. const static RESID2IICON rgzLoadIconList[] =
  297. {
  298. // Misc icons
  299. { IDI_CERTIFICATE, iIconCertificate },
  300. { IDI_CTL, iIconCTL },
  301. { IDI_CRL, iIconCRL },
  302. { IDI_AUTO_CERT_REQUEST, iIconAutoCertRequest },
  303. { IDI_AUTOENROLL, iIconAutoEnroll },
  304. { IDI_SAFER_LEVEL, iIconSaferLevel },
  305. { IDI_DEFAULT_SAFER_LEVEL, iIconDefaultSaferLevel },
  306. { IDI_SAFER_HASH_ENTRY, iIconSaferHashEntry },
  307. { IDI_SAFER_URL_ENTRY, iIconSaferURLEntry },
  308. { IDI_SAFER_NAME_ENTRY, iIconSaferNameEntry },
  309. { IDI_SETTINGS, iIconSettings },
  310. { IDI_SAFER_CERT_ENTRY, iIconSaferCertEntry },
  311. { 0, 0} // Must be last
  312. };
  313. for (int i = 0; rgzLoadIconList[i].uIconId != 0; i++)
  314. {
  315. HICON hIcon = ::LoadIcon (AfxGetInstanceHandle (),
  316. MAKEINTRESOURCE (rgzLoadIconList[i].uIconId));
  317. ASSERT (hIcon && "Icon ID not found in resources");
  318. /*HRESULT hr =*/ pImageList->ImageListSetIcon ( (PLONG_PTR) hIcon,
  319. rgzLoadIconList[i].iIcon);
  320. // ASSERT (SUCCEEDED (hr) && "Unable to add icon to ImageList");
  321. }
  322. _TRACE (-1, L"Leaving CCertMgrComponentData::LoadIcons\n");
  323. return S_OK;
  324. }
  325. HRESULT CCertMgrComponentData::OnNotifyExpand (LPDATAOBJECT pDataObject, BOOL bExpanding, HSCOPEITEM hParent)
  326. {
  327. _TRACE (1, L"Entering CCertMgrComponentData::OnNotifyExpand\n");
  328. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  329. CWaitCursor waitCursor;
  330. ASSERT (pDataObject && hParent && m_pConsoleNameSpace);
  331. if (!bExpanding)
  332. return S_OK;
  333. static bool bDomainVersionChecked = false;
  334. if ( !bDomainVersionChecked )
  335. {
  336. if ( !m_bMachineIsStandAlone ) // only check if joined to a domain
  337. CheckDomainVersion ();
  338. bDomainVersionChecked = true;
  339. }
  340. GUID guidObjectType;
  341. HRESULT hr = ExtractObjectTypeGUID (pDataObject, &guidObjectType);
  342. ASSERT (SUCCEEDED (hr));
  343. if ( IsSecurityConfigurationEditorNodetype (guidObjectType) )
  344. {
  345. hr = ExtractData (pDataObject, CCertMgrDataObject::m_CFSCEModeType,
  346. &m_dwSCEMode, sizeof (DWORD));
  347. ASSERT (SUCCEEDED (hr));
  348. if ( SUCCEEDED (hr) )
  349. {
  350. switch (m_dwSCEMode)
  351. {
  352. case SCE_MODE_DOMAIN_USER: // User Settings
  353. case SCE_MODE_OU_USER:
  354. case SCE_MODE_LOCAL_USER:
  355. case SCE_MODE_DOMAIN_COMPUTER: // Computer Settings
  356. case SCE_MODE_OU_COMPUTER:
  357. case SCE_MODE_LOCAL_COMPUTER:
  358. m_bIsRSOP = false;
  359. if ( !m_pGPEInformation )
  360. {
  361. IUnknown* pIUnknown = 0;
  362. hr = ExtractData (pDataObject,
  363. CCertMgrDataObject::m_CFSCE_GPTUnknown,
  364. &pIUnknown, sizeof (IUnknown*));
  365. ASSERT (SUCCEEDED (hr));
  366. if ( SUCCEEDED (hr) )
  367. {
  368. hr = pIUnknown->QueryInterface (
  369. IID_PPV_ARG (IGPEInformation, &m_pGPEInformation));
  370. ASSERT (SUCCEEDED (hr));
  371. #if DBG
  372. if ( SUCCEEDED (hr) )
  373. {
  374. const int cbLen = 512;
  375. WCHAR szName[cbLen];
  376. hr = m_pGPEInformation->GetName (szName, cbLen);
  377. if ( SUCCEEDED (hr) )
  378. {
  379. _TRACE (0, L"IGPEInformation::GetName () returned: %s",
  380. szName);
  381. }
  382. else
  383. {
  384. _TRACE (0, L"IGPEInformation::GetName () failed: 0x%x\n", hr);
  385. }
  386. }
  387. #endif
  388. pIUnknown->Release ();
  389. }
  390. }
  391. if ( SUCCEEDED (hr) )
  392. {
  393. switch (m_dwSCEMode)
  394. {
  395. case SCE_MODE_DOMAIN_USER:
  396. case SCE_MODE_OU_USER:
  397. case SCE_MODE_LOCAL_USER:
  398. hr = ExpandScopeNodes (NULL, hParent, _T (""),
  399. CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, NODEID_User);
  400. break;
  401. case SCE_MODE_DOMAIN_COMPUTER:
  402. case SCE_MODE_OU_COMPUTER:
  403. case SCE_MODE_LOCAL_COMPUTER:
  404. hr = ExpandScopeNodes (NULL, hParent, _T (""),
  405. CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, NODEID_Machine);
  406. break;
  407. default:
  408. ASSERT (0);
  409. hr = E_FAIL;
  410. break;
  411. }
  412. }
  413. break;
  414. case SCE_MODE_RSOP_USER:
  415. case SCE_MODE_RSOP_COMPUTER:
  416. m_bIsRSOP = true;
  417. hr = BuildWMIList (pDataObject, SCE_MODE_RSOP_COMPUTER == m_dwSCEMode);
  418. if ( SUCCEEDED (hr) )
  419. {
  420. switch (m_dwSCEMode)
  421. {
  422. case SCE_MODE_RSOP_USER:
  423. hr = ExpandScopeNodes (NULL, hParent, _T (""),
  424. CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY,
  425. NODEID_User);
  426. break;
  427. case SCE_MODE_RSOP_COMPUTER:
  428. hr = ExpandScopeNodes (NULL, hParent, _T (""),
  429. CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY,
  430. NODEID_Machine);
  431. break;
  432. default:
  433. ASSERT (0);
  434. hr = E_FAIL;
  435. break;
  436. }
  437. }
  438. break;
  439. default:
  440. // we are not extending other nodes
  441. break;
  442. }
  443. }
  444. return hr;
  445. }
  446. // Beyond this point we are not dealing with extension node types.
  447. {
  448. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  449. if ( pParentCookie )
  450. {
  451. hr = ExpandScopeNodes (pParentCookie, hParent, _T (""), 0, guidObjectType);
  452. }
  453. else
  454. hr = E_UNEXPECTED;
  455. }
  456. _TRACE (-1, L"Leaving CCertMgrComponentData::OnNotifyExpand: 0x%x\n", hr);
  457. return hr;
  458. }
  459. HRESULT CCertMgrComponentData::OnNotifyRelease (LPDATAOBJECT /*pDataObject*/, HSCOPEITEM hItem)
  460. {
  461. _TRACE (1, L"Entering CCertMgrComponentData::OnNotifyRelease\n");
  462. HRESULT hr = DeleteChildren (hItem);
  463. _TRACE (-1, L"Leaving CCertMgrComponentData::OnNotifyRelease: 0x%x\n", hr);
  464. return hr;
  465. }
  466. BSTR CCertMgrComponentData::QueryResultColumnText (CCookie& basecookie, int nCol)
  467. {
  468. // _TRACE (1, L"Entering CCertMgrComponentData::QueryResultColumnText\n");
  469. CCertMgrCookie& cookie = (CCertMgrCookie&) basecookie;
  470. BSTR strResult = L"";
  471. #ifndef UNICODE
  472. #error not ANSI-enabled
  473. #endif
  474. switch ( cookie.m_objecttype )
  475. {
  476. case CERTMGR_SNAPIN:
  477. case CERTMGR_USAGE:
  478. case CERTMGR_CRL_CONTAINER:
  479. case CERTMGR_CTL_CONTAINER:
  480. case CERTMGR_CERT_CONTAINER:
  481. case CERTMGR_CERT_POLICIES_USER:
  482. case CERTMGR_CERT_POLICIES_COMPUTER:
  483. case CERTMGR_SAFER_COMPUTER_ROOT:
  484. case CERTMGR_SAFER_USER_ROOT:
  485. case CERTMGR_SAFER_COMPUTER_LEVELS:
  486. case CERTMGR_SAFER_USER_LEVELS:
  487. case CERTMGR_SAFER_COMPUTER_ENTRIES:
  488. case CERTMGR_SAFER_USER_ENTRIES:
  489. if ( 0 == nCol )
  490. strResult = const_cast<BSTR> (cookie.GetObjectName ());
  491. break;
  492. case CERTMGR_SAFER_COMPUTER_LEVEL:
  493. case CERTMGR_SAFER_USER_LEVEL:
  494. if ( 0 == nCol )
  495. strResult = const_cast<BSTR> (cookie.GetObjectName ());
  496. break;
  497. case CERTMGR_SAFER_COMPUTER_ENTRY:
  498. case CERTMGR_SAFER_USER_ENTRY:
  499. ASSERT (0);
  500. break;
  501. case CERTMGR_PHYS_STORE:
  502. case CERTMGR_LOG_STORE:
  503. case CERTMGR_LOG_STORE_GPE:
  504. case CERTMGR_LOG_STORE_RSOP:
  505. if (COLNUM_CERT_SUBJECT == nCol)
  506. {
  507. CCertStore* pStore = reinterpret_cast <CCertStore*> (&cookie);
  508. ASSERT (pStore);
  509. if ( pStore )
  510. strResult = const_cast<BSTR> (pStore->GetLocalizedName ());
  511. }
  512. break;
  513. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  514. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  515. ASSERT (0);
  516. break;
  517. case CERTMGR_CERTIFICATE:
  518. case CERTMGR_CRL:
  519. case CERTMGR_CTL:
  520. case CERTMGR_AUTO_CERT_REQUEST:
  521. case CERTMGR_NULL_POLICY:
  522. _TRACE (0, L"CCertMgrComponentData::QueryResultColumnText bad parent type\n");
  523. ASSERT (0);
  524. break;
  525. default:
  526. ASSERT (0);
  527. break;
  528. }
  529. // _TRACE (-1, L"Leaving CCertMgrComponentData::QueryResultColumnText\n");
  530. return strResult;
  531. }
  532. int CCertMgrComponentData::QueryImage (CCookie& basecookie, BOOL /*fOpenImage*/)
  533. {
  534. // _TRACE (1, L"Entering CCertMgrComponentData::QueryImage\n");
  535. int nIcon = 0;
  536. CCertMgrCookie& cookie = (CCertMgrCookie&)basecookie;
  537. switch ( cookie.m_objecttype )
  538. {
  539. case CERTMGR_SNAPIN:
  540. nIcon = iIconCertificate;
  541. break;
  542. case CERTMGR_USAGE:
  543. case CERTMGR_PHYS_STORE:
  544. case CERTMGR_LOG_STORE:
  545. case CERTMGR_LOG_STORE_GPE:
  546. case CERTMGR_LOG_STORE_RSOP:
  547. case CERTMGR_CTL_CONTAINER:
  548. case CERTMGR_CERT_CONTAINER:
  549. case CERTMGR_CRL_CONTAINER:
  550. case CERTMGR_CERT_POLICIES_USER:
  551. case CERTMGR_CERT_POLICIES_COMPUTER:
  552. case CERTMGR_SAFER_COMPUTER_ROOT:
  553. case CERTMGR_SAFER_USER_ROOT:
  554. case CERTMGR_SAFER_COMPUTER_LEVELS:
  555. case CERTMGR_SAFER_USER_LEVELS:
  556. case CERTMGR_SAFER_COMPUTER_ENTRIES:
  557. case CERTMGR_SAFER_USER_ENTRIES:
  558. break;
  559. case CERTMGR_CERTIFICATE:
  560. case CERTMGR_CRL:
  561. case CERTMGR_CTL:
  562. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  563. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  564. ASSERT (0); // not expected in scope pane
  565. break;
  566. default:
  567. _TRACE (0, L"CCertMgrComponentData::QueryImage bad parent type\n");
  568. ASSERT (0);
  569. break;
  570. }
  571. // _TRACE (-1, L"Leaving CCertMgrComponentData::QueryImage\n");
  572. return nIcon;
  573. }
  574. ///////////////////////////////////////////////////////////////////////////////
  575. /// IExtendPropertySheet
  576. STDMETHODIMP CCertMgrComponentData::QueryPagesFor (LPDATAOBJECT pDataObject)
  577. {
  578. _TRACE (1, L"Entering CCertMgrComponentData::QueryPagesFor\n");
  579. HRESULT hr = S_OK;
  580. ASSERT (pDataObject);
  581. if ( pDataObject )
  582. {
  583. DATA_OBJECT_TYPES dataobjecttype = CCT_SCOPE;
  584. hr = ExtractData (pDataObject,
  585. CCertMgrDataObject::m_CFDataObjectType,
  586. &dataobjecttype, sizeof (dataobjecttype));
  587. if ( SUCCEEDED (hr) )
  588. {
  589. switch (dataobjecttype)
  590. {
  591. case CCT_SNAPIN_MANAGER:
  592. if ( !m_bIsUserAdministrator )
  593. {
  594. // Non-admins may manage only their own certs
  595. m_dwLocationPersist = CERT_SYSTEM_STORE_CURRENT_USER;
  596. hr = S_FALSE;
  597. }
  598. break;
  599. case CCT_RESULT:
  600. {
  601. hr = S_FALSE;
  602. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  603. if ( pParentCookie )
  604. {
  605. switch (pParentCookie->m_objecttype)
  606. {
  607. case CERTMGR_CERTIFICATE:
  608. case CERTMGR_AUTO_CERT_REQUEST:
  609. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  610. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  611. case CERTMGR_SAFER_COMPUTER_LEVEL:
  612. case CERTMGR_SAFER_USER_LEVEL:
  613. case CERTMGR_SAFER_COMPUTER_TRUSTED_PUBLISHERS:
  614. case CERTMGR_SAFER_USER_TRUSTED_PUBLISHERS:
  615. case CERTMGR_SAFER_COMPUTER_DEFINED_FILE_TYPES:
  616. case CERTMGR_SAFER_USER_DEFINED_FILE_TYPES:
  617. case CERTMGR_SAFER_COMPUTER_ENTRY:
  618. case CERTMGR_SAFER_USER_ENTRY:
  619. case CERTMGR_SAFER_COMPUTER_ENFORCEMENT:
  620. case CERTMGR_SAFER_USER_ENFORCEMENT:
  621. hr = S_OK;
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. }
  628. break;
  629. case CCT_SCOPE:
  630. {
  631. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  632. if ( pParentCookie )
  633. {
  634. switch ( pParentCookie->m_objecttype )
  635. {
  636. case CERTMGR_LOG_STORE_GPE:
  637. case CERTMGR_LOG_STORE_RSOP:
  638. {
  639. CCertStore* pStore = reinterpret_cast <CCertStore*> (pParentCookie);
  640. ASSERT (pStore);
  641. if ( pStore )
  642. {
  643. switch (pStore->GetStoreType ())
  644. {
  645. case ROOT_STORE:
  646. case EFS_STORE:
  647. hr = S_OK;
  648. break;
  649. default:
  650. break;
  651. }
  652. }
  653. else
  654. hr = S_FALSE;
  655. }
  656. break;
  657. default:
  658. hr = S_FALSE;
  659. break;
  660. }
  661. }
  662. else
  663. {
  664. hr = S_FALSE;
  665. }
  666. }
  667. break;
  668. default:
  669. hr = S_FALSE;
  670. break;
  671. }
  672. }
  673. }
  674. else
  675. hr = E_POINTER;
  676. _TRACE (-1, L"Leaving CCertMgrComponentData::QueryPagesFor: 0x%x\n", hr);
  677. return hr;
  678. }
  679. STDMETHODIMP CCertMgrComponentData::CreatePropertyPages (
  680. LPPROPERTYSHEETCALLBACK pCallback,
  681. LONG_PTR handle, // This handle must be saved in the property page object to notify the parent when modified
  682. LPDATAOBJECT pDataObject)
  683. {
  684. _TRACE (1, L"Entering CCertMgrComponentData::CreatePropertyPages\n");
  685. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  686. HRESULT hr = S_OK;
  687. ASSERT (pCallback && pDataObject);
  688. if ( pCallback && pDataObject )
  689. {
  690. DATA_OBJECT_TYPES dataobjecttype = CCT_SCOPE;
  691. hr = ExtractData (pDataObject,
  692. CCertMgrDataObject::m_CFDataObjectType,
  693. &dataobjecttype, sizeof (dataobjecttype));
  694. switch (dataobjecttype)
  695. {
  696. case CCT_SNAPIN_MANAGER:
  697. hr = AddSnapMgrPropPages (pCallback);
  698. break;
  699. case CCT_RESULT:
  700. {
  701. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  702. if ( pParentCookie )
  703. {
  704. switch (pParentCookie->m_objecttype)
  705. {
  706. case CERTMGR_CERTIFICATE:
  707. {
  708. CCertificate* pCert = reinterpret_cast <CCertificate*> (pParentCookie);
  709. ASSERT (pCert);
  710. if ( pCert )
  711. {
  712. // Anything, except ACRS
  713. hr = AddCertPropPages (pCert, pCallback, pDataObject, handle);
  714. }
  715. else
  716. hr = E_FAIL;
  717. }
  718. break;
  719. case CERTMGR_AUTO_CERT_REQUEST:
  720. {
  721. CAutoCertRequest* pACR = reinterpret_cast <CAutoCertRequest*> (pParentCookie);
  722. ASSERT (pACR);
  723. if ( pACR )
  724. {
  725. hr = AddACRSCTLPropPages (pACR, pCallback);
  726. }
  727. else
  728. hr = E_FAIL;
  729. }
  730. break;
  731. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  732. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  733. hr = AddAutoenrollmentSettingsPropPages (pCallback,
  734. CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS ==
  735. pParentCookie->m_objecttype);
  736. break;
  737. case CERTMGR_SAFER_COMPUTER_LEVEL:
  738. case CERTMGR_SAFER_USER_LEVEL:
  739. hr = AddSaferLevelPropPage (pCallback,
  740. dynamic_cast <CSaferLevel*>(pParentCookie),
  741. handle,
  742. pDataObject);
  743. break;
  744. case CERTMGR_SAFER_COMPUTER_TRUSTED_PUBLISHERS:
  745. case CERTMGR_SAFER_USER_TRUSTED_PUBLISHERS:
  746. hr = AddSaferTrustedPublisherPropPages (pCallback,
  747. CERTMGR_SAFER_COMPUTER_TRUSTED_PUBLISHERS ==
  748. pParentCookie->m_objecttype);
  749. break;
  750. case CERTMGR_SAFER_COMPUTER_DEFINED_FILE_TYPES:
  751. case CERTMGR_SAFER_USER_DEFINED_FILE_TYPES:
  752. hr = AddSaferDefinedFileTypesPropPages (pCallback,
  753. CERTMGR_SAFER_COMPUTER_DEFINED_FILE_TYPES ==
  754. pParentCookie->m_objecttype);
  755. break;
  756. case CERTMGR_SAFER_COMPUTER_ENFORCEMENT:
  757. case CERTMGR_SAFER_USER_ENFORCEMENT:
  758. hr = AddSaferEnforcementPropPages (pCallback,
  759. CERTMGR_SAFER_COMPUTER_ENFORCEMENT ==
  760. pParentCookie->m_objecttype);
  761. break;
  762. case CERTMGR_SAFER_COMPUTER_ENTRY:
  763. case CERTMGR_SAFER_USER_ENTRY:
  764. hr = AddSaferEntryPropertyPage (pCallback,
  765. pParentCookie, pDataObject, handle);
  766. break;
  767. default:
  768. break;
  769. }
  770. }
  771. else
  772. hr = E_UNEXPECTED;
  773. }
  774. break;
  775. case CCT_SCOPE:
  776. {
  777. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  778. if ( pParentCookie )
  779. {
  780. switch ( pParentCookie->m_objecttype )
  781. {
  782. case CERTMGR_LOG_STORE_GPE:
  783. case CERTMGR_LOG_STORE_RSOP:
  784. {
  785. CCertStore* pStore = reinterpret_cast <CCertStore*> (pParentCookie);
  786. ASSERT (pStore);
  787. if ( pStore )
  788. {
  789. if ( ROOT_STORE == pStore->GetStoreType () )
  790. {
  791. hr = AddGPEStorePropPages (pCallback, pStore);
  792. }
  793. else if ( EFS_STORE == pStore->GetStoreType () )
  794. {
  795. hr = AddEFSSettingsPropPages (pCallback,
  796. pStore->IsMachineStore ());
  797. }
  798. }
  799. else
  800. hr = E_FAIL;
  801. }
  802. break;
  803. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  804. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  805. ASSERT (0);
  806. break;
  807. default:
  808. break;
  809. }
  810. }
  811. else
  812. hr = E_UNEXPECTED;
  813. }
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. else
  820. hr = E_POINTER;
  821. _TRACE (-1, L"Leaving CCertMgrComponentData::CreatePropertyPages: 0x%x\n", hr);
  822. return hr;
  823. }
  824. HRESULT CCertMgrComponentData::AddSnapMgrPropPages (LPPROPERTYSHEETCALLBACK pCallback)
  825. {
  826. _TRACE (1, L"Entering CCertMgrComponentData::AddSnapMgrPropPages\n");
  827. HRESULT hr = S_OK;
  828. ASSERT (pCallback);
  829. if ( pCallback )
  830. {
  831. //
  832. // Note that once we have established that this is a CCT_SNAPIN_MANAGER cookie,
  833. // we don't care about its other properties. A CCT_SNAPIN_MANAGER cookie is
  834. // equivalent to a BOOL flag asking for the Node Properties page instead of a
  835. // managed object property page. JonN 10/9/96
  836. //
  837. if ( m_bIsUserAdministrator )
  838. {
  839. CSelectAccountPropPage * pSelAcctPage =
  840. new CSelectAccountPropPage (IsWindowsNT ());
  841. if ( pSelAcctPage )
  842. {
  843. pSelAcctPage->AssignLocationPtr (&m_dwLocationPersist);
  844. HPROPSHEETPAGE hSelAcctPage = MyCreatePropertySheetPage (&pSelAcctPage->m_psp);
  845. if ( hSelAcctPage )
  846. {
  847. hr = pCallback->AddPage (hSelAcctPage);
  848. ASSERT (SUCCEEDED (hr));
  849. if ( FAILED (hr) )
  850. VERIFY (::DestroyPropertySheetPage (hSelAcctPage));
  851. }
  852. else
  853. delete pSelAcctPage;
  854. }
  855. else
  856. {
  857. hr = E_OUTOFMEMORY;
  858. }
  859. // In Windows 95 or Windows 98,users will only be able to manage the
  860. // local machine.
  861. if ( IsWindowsNT () )
  862. {
  863. CCertMgrChooseMachinePropPage * pChooseMachinePage = new CCertMgrChooseMachinePropPage ();
  864. if ( pChooseMachinePage )
  865. {
  866. pChooseMachinePage->AssignLocationPtr (&m_dwLocationPersist);
  867. // Initialize state of object
  868. ASSERT (m_pRootCookie);
  869. if ( m_pRootCookie )
  870. {
  871. pChooseMachinePage->InitMachineName (m_pRootCookie->QueryTargetServer ());
  872. pChooseMachinePage->SetOutputBuffers (
  873. OUT &m_strMachineNamePersist,
  874. OUT &m_fAllowOverrideMachineName,
  875. OUT &m_pRootCookie->m_strMachineName); // Effective machine name
  876. HPROPSHEETPAGE hChooseMachinePage = MyCreatePropertySheetPage (&pChooseMachinePage->m_psp);
  877. if ( hChooseMachinePage )
  878. {
  879. hr = pCallback->AddPage (hChooseMachinePage);
  880. ASSERT (SUCCEEDED (hr));
  881. if ( FAILED (hr) )
  882. VERIFY (::DestroyPropertySheetPage (hChooseMachinePage));
  883. }
  884. else
  885. delete pChooseMachinePage;
  886. }
  887. }
  888. else
  889. {
  890. hr = E_OUTOFMEMORY;
  891. }
  892. CSelectServiceAccountPropPage* pServicePage = new
  893. CSelectServiceAccountPropPage (&m_szManagedServicePersist,
  894. &m_szManagedServiceDisplayName,
  895. m_strMachineNamePersist);
  896. if ( pServicePage )
  897. {
  898. // pServicePage->SetCaption (IDS_MS_CERT_MGR); // access violation when called
  899. HPROPSHEETPAGE hServicePage = MyCreatePropertySheetPage (&pServicePage->m_psp);
  900. if ( hServicePage )
  901. {
  902. hr = pCallback->AddPage (hServicePage);
  903. ASSERT (SUCCEEDED (hr));
  904. if ( FAILED (hr) )
  905. VERIFY (::DestroyPropertySheetPage (hServicePage));
  906. }
  907. else
  908. delete pServicePage;
  909. }
  910. else
  911. {
  912. hr = E_OUTOFMEMORY;
  913. }
  914. }
  915. }
  916. else
  917. {
  918. // Non-administrators may view their own certs only.
  919. m_dwLocationPersist = CERT_SYSTEM_STORE_CURRENT_USER;
  920. }
  921. }
  922. else
  923. hr = E_POINTER;
  924. _TRACE (-1, L"Leaving CCertMgrComponentData::AddSnapMgrPropPages: 0x%x\n", hr);
  925. return hr;
  926. }
  927. HRESULT CCertMgrComponentData::AddACRSCTLPropPages (CAutoCertRequest* pACR, LPPROPERTYSHEETCALLBACK pCallback)
  928. {
  929. _TRACE (1, L"Entering CCertMgrComponentData::AddACRSCTLPropPages\n");
  930. HRESULT hr = S_OK;
  931. ASSERT (pACR && pCallback);
  932. if ( pACR && pCallback )
  933. {
  934. CACRGeneralPage * pACRPage = new CACRGeneralPage (*pACR);
  935. if ( pACRPage )
  936. {
  937. HPROPSHEETPAGE hACRPage = MyCreatePropertySheetPage (&pACRPage->m_psp);
  938. if ( hACRPage )
  939. {
  940. hr = pCallback->AddPage (hACRPage);
  941. ASSERT (SUCCEEDED (hr));
  942. if ( FAILED (hr) )
  943. VERIFY (::DestroyPropertySheetPage (hACRPage));
  944. }
  945. else
  946. delete pACRPage;
  947. }
  948. else
  949. {
  950. hr = E_OUTOFMEMORY;
  951. }
  952. }
  953. else
  954. hr = E_POINTER;
  955. _TRACE (-1, L"Leaving CCertMgrComponentData::AddACRSCTLPropPages: 0x%x\n", hr);
  956. return hr;
  957. }
  958. HRESULT CCertMgrComponentData::AddEFSSettingsPropPages (
  959. LPPROPERTYSHEETCALLBACK pCallback,
  960. bool fIsComputerType)
  961. {
  962. _TRACE (1, L"Entering CCertMgrComponentData::AddEFSSettingsPropPages\n");
  963. HRESULT hr = S_OK;
  964. ASSERT (pCallback);
  965. if ( pCallback )
  966. {
  967. CEFSGeneralPropertyPage * pEFSPage = new CEFSGeneralPropertyPage (
  968. this, fIsComputerType);
  969. if ( pEFSPage )
  970. {
  971. HPROPSHEETPAGE hEFSPage = MyCreatePropertySheetPage (&pEFSPage->m_psp);
  972. if ( hEFSPage )
  973. {
  974. hr = pCallback->AddPage (hEFSPage);
  975. ASSERT (SUCCEEDED (hr));
  976. if ( SUCCEEDED (hr) )
  977. {
  978. if ( m_bIsRSOP )
  979. {
  980. CString storePath = EFS_SETTINGS_REGPATH;
  981. CPolicyPrecedencePropertyPage * pPrecedencePage =
  982. new CPolicyPrecedencePropertyPage (this, storePath,
  983. EFS_SETTINGS_REGVALUE,
  984. fIsComputerType);
  985. if ( pPrecedencePage )
  986. {
  987. HPROPSHEETPAGE hPrecedencePage = MyCreatePropertySheetPage (&pPrecedencePage->m_psp);
  988. if ( hPrecedencePage )
  989. {
  990. hr = pCallback->AddPage (hPrecedencePage);
  991. ASSERT (SUCCEEDED (hr));
  992. if ( FAILED (hr) )
  993. VERIFY (::DestroyPropertySheetPage (hPrecedencePage));
  994. }
  995. else
  996. delete pPrecedencePage;
  997. }
  998. else
  999. {
  1000. hr = E_OUTOFMEMORY;
  1001. }
  1002. }
  1003. }
  1004. else
  1005. VERIFY (::DestroyPropertySheetPage (hEFSPage));
  1006. }
  1007. else
  1008. delete pEFSPage;
  1009. }
  1010. else
  1011. {
  1012. hr = E_OUTOFMEMORY;
  1013. }
  1014. }
  1015. else
  1016. hr = E_POINTER;
  1017. _TRACE (-1, L"Leaving CCertMgrComponentData::AddEFSSettingsPropPages: 0x%x\n", hr);
  1018. return hr;
  1019. }
  1020. HRESULT CCertMgrComponentData::AddAutoenrollmentSettingsPropPages (
  1021. LPPROPERTYSHEETCALLBACK pCallback,
  1022. bool fIsComputerType)
  1023. {
  1024. _TRACE (1, L"Entering CCertMgrComponentData::AddAutoenrollmentSettingsPropPages\n");
  1025. HRESULT hr = S_OK;
  1026. ASSERT (pCallback);
  1027. if ( pCallback )
  1028. {
  1029. CAutoenrollmentPropertyPage * pAutoEnrollmentPage = new CAutoenrollmentPropertyPage (
  1030. this, fIsComputerType);
  1031. if ( pAutoEnrollmentPage )
  1032. {
  1033. HPROPSHEETPAGE hAutoEnrollmentPage = MyCreatePropertySheetPage (&pAutoEnrollmentPage->m_psp);
  1034. if ( hAutoEnrollmentPage )
  1035. {
  1036. hr = pCallback->AddPage (hAutoEnrollmentPage);
  1037. ASSERT (SUCCEEDED (hr));
  1038. if ( SUCCEEDED (hr) )
  1039. {
  1040. if ( m_bIsRSOP )
  1041. {
  1042. CString storePath = AUTO_ENROLLMENT_KEY;
  1043. CPolicyPrecedencePropertyPage * pPrecedencePage =
  1044. new CPolicyPrecedencePropertyPage (this, storePath,
  1045. AUTO_ENROLLMENT_POLICY,
  1046. fIsComputerType);
  1047. if ( pPrecedencePage )
  1048. {
  1049. HPROPSHEETPAGE hPrecedencePage = MyCreatePropertySheetPage (&pPrecedencePage->m_psp);
  1050. if ( hPrecedencePage )
  1051. {
  1052. hr = pCallback->AddPage (hPrecedencePage);
  1053. ASSERT (SUCCEEDED (hr));
  1054. if ( FAILED (hr) )
  1055. VERIFY (::DestroyPropertySheetPage (hPrecedencePage));
  1056. }
  1057. else
  1058. delete pPrecedencePage;
  1059. }
  1060. else
  1061. {
  1062. hr = E_OUTOFMEMORY;
  1063. }
  1064. }
  1065. }
  1066. else
  1067. VERIFY (::DestroyPropertySheetPage (hAutoEnrollmentPage));
  1068. }
  1069. else
  1070. delete pAutoEnrollmentPage;
  1071. }
  1072. else
  1073. {
  1074. hr = E_OUTOFMEMORY;
  1075. }
  1076. }
  1077. else
  1078. hr = E_POINTER;
  1079. _TRACE (-1, L"Leaving CCertMgrComponentData::AddAutoenrollmentSettingsPropPages: 0x%x\n", hr);
  1080. return hr;
  1081. }
  1082. HRESULT CCertMgrComponentData::AddGPEStorePropPages (
  1083. LPPROPERTYSHEETCALLBACK pCallback,
  1084. CCertStore* pStore)
  1085. {
  1086. _TRACE (1, L"Entering CCertMgrComponentData::AddGPEStorePropPages\n");
  1087. HRESULT hr = S_OK;
  1088. ASSERT (pCallback && pStore);
  1089. if ( !pCallback || !pStore)
  1090. return E_POINTER;
  1091. ASSERT (m_pGPEInformation || m_pRSOPInformationComputer || m_pRSOPInformationUser );
  1092. if ( !m_pGPEInformation && !m_pRSOPInformationComputer && !m_pRSOPInformationUser )
  1093. return E_UNEXPECTED;
  1094. bool bIsComputerType = pStore->IsMachineStore ();
  1095. CGPERootGeneralPage * pGPERootPage = new CGPERootGeneralPage (this, bIsComputerType);
  1096. if ( pGPERootPage )
  1097. {
  1098. HPROPSHEETPAGE hGPERootPage = MyCreatePropertySheetPage (&pGPERootPage->m_psp);
  1099. if ( hGPERootPage )
  1100. {
  1101. hr = pCallback->AddPage (hGPERootPage);
  1102. ASSERT (SUCCEEDED (hr));
  1103. if ( FAILED (hr) )
  1104. VERIFY (::DestroyPropertySheetPage (hGPERootPage));
  1105. }
  1106. else
  1107. delete pGPERootPage;
  1108. }
  1109. else
  1110. {
  1111. hr = E_OUTOFMEMORY;
  1112. }
  1113. if ( SUCCEEDED (hr) )
  1114. {
  1115. if ( m_bIsRSOP )
  1116. {
  1117. CString storePath = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
  1118. storePath += L"\\";
  1119. storePath += pStore->GetStoreName ();
  1120. CPolicyPrecedencePropertyPage * pPrecedencePage =
  1121. new CPolicyPrecedencePropertyPage (this, storePath, STR_BLOB, bIsComputerType);
  1122. if ( pPrecedencePage )
  1123. {
  1124. HPROPSHEETPAGE hPrecedencePage = MyCreatePropertySheetPage (&pPrecedencePage->m_psp);
  1125. if ( hPrecedencePage )
  1126. {
  1127. hr = pCallback->AddPage (hPrecedencePage);
  1128. ASSERT (SUCCEEDED (hr));
  1129. if ( FAILED (hr) )
  1130. VERIFY (::DestroyPropertySheetPage (hPrecedencePage));
  1131. }
  1132. else
  1133. delete pPrecedencePage;
  1134. }
  1135. else
  1136. {
  1137. hr = E_OUTOFMEMORY;
  1138. }
  1139. }
  1140. }
  1141. _TRACE (-1, L"Leaving CCertMgrComponentData::AddGPEStorePropPages: 0x%x\n", hr);
  1142. return hr;
  1143. }
  1144. HRESULT CCertMgrComponentData::AddCertPropPages (CCertificate * pCert, LPPROPERTYSHEETCALLBACK pCallback, LPDATAOBJECT pDataObject, LONG_PTR lNotifyHandle)
  1145. {
  1146. _TRACE (1, L"Entering CCertMgrComponentData::AddCertPropPages\n");
  1147. HRESULT hr = S_OK;
  1148. CWaitCursor waitCursor;
  1149. ASSERT (pCert);
  1150. ASSERT (pCallback);
  1151. if ( pCert && pCallback )
  1152. {
  1153. PROPSHEETPAGEW* ppsp = 0;
  1154. DWORD dwPageCnt = 0;
  1155. CRYPTUI_VIEWCERTIFICATEPROPERTIES_STRUCT sps;
  1156. HCERTSTORE* pPropPageStores = new HCERTSTORE[1];
  1157. if ( pPropPageStores )
  1158. {
  1159. m_pCryptUIMMCCallbackStruct = (PCRYPTUI_MMCCALLBACK_STRUCT)
  1160. ::GlobalAlloc (GMEM_FIXED, sizeof (CRYPTUI_MMCCALLBACK_STRUCT));
  1161. if ( m_pCryptUIMMCCallbackStruct )
  1162. {
  1163. m_pCryptUIMMCCallbackStruct->pfnCallback = &MMCPropertyChangeNotify;
  1164. m_pCryptUIMMCCallbackStruct->lNotifyHandle = lNotifyHandle;
  1165. pDataObject->AddRef ();
  1166. m_pCryptUIMMCCallbackStruct->param = (LPARAM) pDataObject;
  1167. CCertStore* pStore = pCert->GetCertStore ();
  1168. if ( pStore )
  1169. {
  1170. pPropPageStores[0] = pStore->GetStoreHandle ();
  1171. ::ZeroMemory (&sps, sizeof (sps));
  1172. sps.dwSize = sizeof (sps);
  1173. sps.pMMCCallback = m_pCryptUIMMCCallbackStruct;
  1174. sps.pCertContext = pCert->GetNewCertContext ();
  1175. sps.cStores = 1;
  1176. sps.rghStores = pPropPageStores;
  1177. // All dialogs should be read-only under RSOP
  1178. if ( m_bIsRSOP || pCert->IsReadOnly () )
  1179. sps.dwFlags |= CRYPTUI_DISABLE_EDITPROPERTIES;
  1180. _TRACE (0, L"Calling CryptUIGetCertificatePropertiesPages()\n");
  1181. BOOL bReturn = ::CryptUIGetCertificatePropertiesPages (
  1182. &sps,
  1183. NULL,
  1184. &ppsp,
  1185. &dwPageCnt);
  1186. ASSERT (bReturn);
  1187. if ( bReturn )
  1188. {
  1189. HPROPSHEETPAGE hPage = 0;
  1190. for (DWORD dwIndex = 0; dwIndex < dwPageCnt; dwIndex++)
  1191. {
  1192. _TRACE (0, L"Calling CreatePropertySheetPage()\n");
  1193. // Not necessary to call MyCreatePropertySheetPage here
  1194. // as these are not MFC-based property pages
  1195. hPage = ::CreatePropertySheetPage (&ppsp[dwIndex]);
  1196. if ( hPage )
  1197. {
  1198. hr = pCallback->AddPage (hPage);
  1199. ASSERT (SUCCEEDED (hr));
  1200. if ( FAILED (hr) )
  1201. {
  1202. VERIFY (::DestroyPropertySheetPage (hPage));
  1203. break;
  1204. }
  1205. }
  1206. else
  1207. {
  1208. hr = HRESULT_FROM_WIN32 (GetLastError ());
  1209. break;
  1210. }
  1211. }
  1212. }
  1213. else
  1214. {
  1215. hr = E_UNEXPECTED;
  1216. GlobalFree (m_pCryptUIMMCCallbackStruct);
  1217. ::CertFreeCertificateContext (sps.pCertContext);
  1218. }
  1219. }
  1220. }
  1221. else
  1222. {
  1223. hr = E_OUTOFMEMORY;
  1224. }
  1225. if ( E_OUTOFMEMORY == hr && ppsp )
  1226. free (ppsp); // source uses malloc
  1227. delete [] pPropPageStores;
  1228. }
  1229. else
  1230. hr = E_OUTOFMEMORY;
  1231. }
  1232. else
  1233. hr = E_POINTER;
  1234. _TRACE (-1, L"Leaving CCertMgrComponentData::AddCertPropPages: 0x%x\n", hr);
  1235. return hr;
  1236. }
  1237. HRESULT CCertMgrComponentData::AddContainersToScopePane (
  1238. HSCOPEITEM hParent,
  1239. CCertMgrCookie& parentCookie,
  1240. bool bDeleteAndExpand)
  1241. {
  1242. _TRACE (1, L"Entering CCertMgrComponentData::AddContainersToScopePane\n");
  1243. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  1244. LPCONSOLENAMESPACE2 pConsoleNameSpace2 = 0;
  1245. HRESULT hr = m_pConsoleNameSpace->QueryInterface (
  1246. IID_PPV_ARG (IConsoleNameSpace2, &pConsoleNameSpace2));
  1247. if ( SUCCEEDED (hr) && pConsoleNameSpace2 )
  1248. {
  1249. hr = pConsoleNameSpace2->Expand (hParent);
  1250. ASSERT (SUCCEEDED (hr));
  1251. pConsoleNameSpace2->Release ();
  1252. pConsoleNameSpace2 = 0;
  1253. }
  1254. if ( CERTMGR_PHYS_STORE == parentCookie.m_objecttype ||
  1255. (CERTMGR_LOG_STORE == parentCookie.m_objecttype && !m_bShowPhysicalStoresPersist) )
  1256. {
  1257. CCertStore* pStore =
  1258. reinterpret_cast <CCertStore*> (&parentCookie);
  1259. ASSERT (pStore);
  1260. if ( pStore )
  1261. {
  1262. CString objectName;
  1263. if ( pStore->ContainsCRLs () &&
  1264. !ContainerExists (hParent, CERTMGR_CRL_CONTAINER) )
  1265. {
  1266. VERIFY (objectName.LoadString (IDS_CERTIFICATE_REVOCATION_LIST));
  1267. hr = AddScopeNode (new CContainerCookie (
  1268. *pStore,
  1269. CERTMGR_CRL_CONTAINER,
  1270. pStore->QueryNonNULLMachineName (),
  1271. objectName), L"", hParent);
  1272. }
  1273. if ( SUCCEEDED (hr) && pStore->ContainsCTLs () &&
  1274. !ContainerExists (hParent, CERTMGR_CTL_CONTAINER) )
  1275. {
  1276. VERIFY (objectName.LoadString (IDS_CERTIFICATE_TRUST_LIST));
  1277. hr = AddScopeNode (new CContainerCookie (
  1278. *pStore,
  1279. CERTMGR_CTL_CONTAINER,
  1280. pStore->QueryNonNULLMachineName (),
  1281. objectName), L"", hParent);
  1282. }
  1283. if ( SUCCEEDED (hr) && pStore->ContainsCertificates () &&
  1284. !ContainerExists (hParent, CERTMGR_CERT_CONTAINER) )
  1285. {
  1286. VERIFY (objectName.LoadString (IDS_CERTIFICATES));
  1287. hr = AddScopeNode (new CContainerCookie (
  1288. *pStore,
  1289. CERTMGR_CERT_CONTAINER,
  1290. pStore->QueryNonNULLMachineName (),
  1291. objectName), L"", hParent);
  1292. }
  1293. }
  1294. else
  1295. hr = E_UNEXPECTED;
  1296. }
  1297. else if ( CERTMGR_LOG_STORE == parentCookie.m_objecttype && m_bShowPhysicalStoresPersist )
  1298. {
  1299. if ( bDeleteAndExpand )
  1300. {
  1301. hr = DeleteChildren (hParent);
  1302. if ( SUCCEEDED (hr) )
  1303. {
  1304. GUID guid;
  1305. hr = ExpandScopeNodes (&parentCookie, hParent, _T (""), 0, guid);
  1306. }
  1307. }
  1308. }
  1309. _TRACE (-1, L"Leaving CCertMgrComponentData::AddContainersToScopePane: 0x%x\n", hr);
  1310. return hr;
  1311. }
  1312. typedef struct _ENUM_ARG {
  1313. DWORD m_dwFlags;
  1314. LPCONSOLENAMESPACE m_pConsoleNameSpace;
  1315. HSCOPEITEM m_hParent;
  1316. CCertMgrComponentData* m_pCompData;
  1317. PCWSTR m_pcszMachineName;
  1318. CCertMgrCookie* m_pParentCookie;
  1319. SPECIAL_STORE_TYPE m_storeType;
  1320. LPCONSOLE m_pConsole;
  1321. } ENUM_ARG, *PENUM_ARG;
  1322. static BOOL WINAPI EnumPhyCallback (
  1323. IN const void *pvSystemStore,
  1324. IN DWORD dwFlags,
  1325. IN PCWSTR pwszStoreName,
  1326. IN PCERT_PHYSICAL_STORE_INFO pStoreInfo,
  1327. IN OPTIONAL void* /*pvReserved*/,
  1328. IN OPTIONAL void *pvArg
  1329. )
  1330. {
  1331. _TRACE (1, L"Entering EnumPhyCallback\n");
  1332. if ( ! (pStoreInfo->dwFlags & CERT_PHYSICAL_STORE_OPEN_DISABLE_FLAG) )
  1333. {
  1334. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  1335. SCOPEDATAITEM tSDItem;
  1336. ::ZeroMemory (&tSDItem,sizeof (tSDItem));
  1337. tSDItem.mask = SDI_STR | SDI_IMAGE | SDI_STATE | SDI_PARAM | SDI_PARENT;
  1338. tSDItem.displayname = MMC_CALLBACK;
  1339. tSDItem.relativeID = pEnumArg->m_hParent;
  1340. tSDItem.nState = 0;
  1341. if ( pEnumArg->m_pCompData->ShowArchivedCerts () )
  1342. dwFlags |= CERT_STORE_ENUM_ARCHIVED_FLAG;
  1343. // Create new cookies
  1344. CCertStore* pNewCookie = new CCertStore (
  1345. CERTMGR_PHYS_STORE,
  1346. CERT_STORE_PROV_PHYSICAL,
  1347. dwFlags,
  1348. pEnumArg->m_pcszMachineName,
  1349. pwszStoreName, (PCWSTR) pvSystemStore, pwszStoreName,
  1350. pEnumArg->m_storeType,
  1351. dwFlags,
  1352. pEnumArg->m_pConsole);
  1353. if ( pNewCookie )
  1354. {
  1355. // pEnumArg->m_pParentCookie->m_listScopeCookieBlocks.AddHead (
  1356. // (CBaseCookieBlock*) pNewCookie);
  1357. // WARNING cookie cast
  1358. tSDItem.lParam = reinterpret_cast<LPARAM> ( (CCookie*) pNewCookie);
  1359. tSDItem.nImage = pEnumArg->m_pCompData->QueryImage (*pNewCookie, FALSE);
  1360. HRESULT hr = pEnumArg->m_pConsoleNameSpace->InsertItem (&tSDItem);
  1361. ASSERT (SUCCEEDED (hr));
  1362. if ( SUCCEEDED (hr) )
  1363. pNewCookie->m_hScopeItem = tSDItem.ID;
  1364. }
  1365. }
  1366. _TRACE (-1, L"Leaving EnumPhyCallback\n");
  1367. return TRUE;
  1368. }
  1369. HRESULT CCertMgrComponentData::AddPhysicalStoresToScopePane (HSCOPEITEM hParent, CCertMgrCookie& parentCookie, const SPECIAL_STORE_TYPE storeType)
  1370. {
  1371. _TRACE (1, L"Entering CCertMgrComponentData::AddPhysicalStoresToScopePane\n");
  1372. CWaitCursor cursor;
  1373. HRESULT hr = S_OK;
  1374. DWORD dwFlags = 0;
  1375. ENUM_ARG enumArg;
  1376. dwFlags &= ~CERT_SYSTEM_STORE_LOCATION_MASK;
  1377. dwFlags |= CERT_STORE_READONLY_FLAG | m_dwLocationPersist;
  1378. ::ZeroMemory (&enumArg, sizeof (enumArg));
  1379. enumArg.m_dwFlags = dwFlags;
  1380. enumArg.m_pConsoleNameSpace = m_pConsoleNameSpace;
  1381. enumArg.m_hParent = hParent;
  1382. enumArg.m_pCompData = this;
  1383. enumArg.m_pcszMachineName = parentCookie.QueryNonNULLMachineName ();
  1384. enumArg.m_pParentCookie = &parentCookie;
  1385. enumArg.m_storeType = storeType;
  1386. enumArg.m_pConsole = m_pConsole;
  1387. if (!::CertEnumPhysicalStore (
  1388. (PWSTR) (PCWSTR) parentCookie.GetObjectName (),
  1389. dwFlags,
  1390. &enumArg,
  1391. EnumPhyCallback))
  1392. {
  1393. DWORD dwErr = GetLastError ();
  1394. DisplaySystemError (dwErr);
  1395. hr = HRESULT_FROM_WIN32 (dwErr);
  1396. }
  1397. _TRACE (-1, L"Leaving CCertMgrComponentData::AddPhysicalStoresToScopePane: 0x%x\n", hr);
  1398. return hr;
  1399. }
  1400. static BOOL WINAPI EnumIComponentDataSysCallback (
  1401. IN const void* pwszSystemStore,
  1402. IN DWORD dwFlags,
  1403. IN PCERT_SYSTEM_STORE_INFO /*pStoreInfo*/,
  1404. IN OPTIONAL void* /*pvReserved*/,
  1405. IN OPTIONAL void *pvArg
  1406. )
  1407. {
  1408. _TRACE (1, L"Entering EnumIComponentDataSysCallback\n");
  1409. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  1410. SCOPEDATAITEM tSDItem;
  1411. ::ZeroMemory (&tSDItem,sizeof (tSDItem));
  1412. tSDItem.mask = SDI_STR | SDI_IMAGE | SDI_STATE | SDI_PARAM | SDI_PARENT;
  1413. tSDItem.displayname = MMC_CALLBACK;
  1414. tSDItem.relativeID = pEnumArg->m_hParent;
  1415. tSDItem.nState = 0;
  1416. // Create new cookies
  1417. SPECIAL_STORE_TYPE storeType = GetSpecialStoreType ((PWSTR) pwszSystemStore);
  1418. //
  1419. // We will not expose the ACRS store for machines or users. It is not
  1420. // interesting or useful at this level. All Auto Cert Requests should
  1421. // be managed only at the policy level.
  1422. //
  1423. if ( ACRS_STORE != storeType )
  1424. {
  1425. if ( pEnumArg->m_pCompData->ShowArchivedCerts () )
  1426. dwFlags |= CERT_STORE_ENUM_ARCHIVED_FLAG;
  1427. CCertStore* pNewCookie = new CCertStore (
  1428. CERTMGR_LOG_STORE,
  1429. CERT_STORE_PROV_SYSTEM,
  1430. dwFlags,
  1431. pEnumArg->m_pcszMachineName,
  1432. (PCWSTR) pwszSystemStore,
  1433. (PCWSTR) pwszSystemStore,
  1434. _T (""),
  1435. storeType,
  1436. pEnumArg->m_dwFlags,
  1437. pEnumArg->m_pConsole);
  1438. if ( pNewCookie )
  1439. {
  1440. pEnumArg->m_pParentCookie->m_listScopeCookieBlocks.AddHead (
  1441. (CBaseCookieBlock*) pNewCookie);
  1442. // WARNING cookie cast
  1443. tSDItem.lParam = reinterpret_cast<LPARAM> ( (CCookie*) pNewCookie);
  1444. tSDItem.nImage = pEnumArg->m_pCompData->QueryImage (*pNewCookie, FALSE);
  1445. HRESULT hr = pEnumArg->m_pConsoleNameSpace->InsertItem (&tSDItem);
  1446. ASSERT (SUCCEEDED (hr));
  1447. if ( SUCCEEDED (hr) )
  1448. pNewCookie->m_hScopeItem = tSDItem.ID;
  1449. }
  1450. }
  1451. _TRACE (-1, L"Leaving EnumIComponentDataSysCallback\n");
  1452. return TRUE;
  1453. }
  1454. HRESULT CCertMgrComponentData::AddLogicalStoresToScopePane (HSCOPEITEM hParent, CCertMgrCookie& parentCookie)
  1455. {
  1456. _TRACE (1, L"Entering CCertMgrComponentData::AddLogicalStoresToScopePane\n");
  1457. CWaitCursor cursor;
  1458. HRESULT hr = S_OK;
  1459. // If m_dwLocationPersist is 0 but the file name is empty, this means the
  1460. // user launched certmgr.msc without providing a target file. Launch
  1461. // certificates snapin as the current user instead.
  1462. if ( !m_dwLocationPersist && m_szFileName.IsEmpty () )
  1463. {
  1464. m_dwLocationPersist = CERT_SYSTEM_STORE_CURRENT_USER;
  1465. ChangeRootNodeName (L"");
  1466. }
  1467. DWORD dwFlags = m_dwLocationPersist;
  1468. ENUM_ARG enumArg;
  1469. ::ZeroMemory (&enumArg, sizeof (enumArg));
  1470. enumArg.m_dwFlags = dwFlags;
  1471. enumArg.m_pConsoleNameSpace = m_pConsoleNameSpace;
  1472. enumArg.m_hParent = hParent;
  1473. enumArg.m_pCompData = this;
  1474. enumArg.m_pcszMachineName = parentCookie.QueryNonNULLMachineName ();
  1475. enumArg.m_pParentCookie = &parentCookie;
  1476. enumArg.m_pConsole = m_pConsole;
  1477. CString location;
  1478. void* pvPara = 0;
  1479. switch (m_dwLocationPersist)
  1480. {
  1481. case CERT_SYSTEM_STORE_CURRENT_USER:
  1482. case CERT_SYSTEM_STORE_LOCAL_MACHINE:
  1483. if ( !m_szManagedServicePersist.IsEmpty () )
  1484. m_szManagedServicePersist.Empty ();
  1485. break;
  1486. case CERT_SYSTEM_STORE_CURRENT_SERVICE:
  1487. case CERT_SYSTEM_STORE_SERVICES:
  1488. break;
  1489. default:
  1490. ASSERT (0);
  1491. break;
  1492. }
  1493. if ( !m_szManagedServicePersist.IsEmpty () )
  1494. {
  1495. if ( m_szManagedComputer.CompareNoCase (m_szThisComputer) ) //!=
  1496. {
  1497. location = m_szManagedComputer + _T ("\\") +
  1498. m_szManagedServicePersist;
  1499. pvPara = (void *) (PCWSTR) location;
  1500. }
  1501. else
  1502. pvPara = (void *) (PCWSTR) m_szManagedServicePersist;
  1503. }
  1504. else if ( m_szManagedComputer.CompareNoCase (m_szThisComputer) ) //!=
  1505. {
  1506. pvPara = (void *) (PCWSTR) m_szManagedComputer;
  1507. }
  1508. if ( m_szFileName.IsEmpty () )
  1509. {
  1510. // Ensure creation of MY store
  1511. HCERTSTORE hTempStore = ::CertOpenStore (CERT_STORE_PROV_SYSTEM,
  1512. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1513. NULL,
  1514. dwFlags | CERT_STORE_SET_LOCALIZED_NAME_FLAG,
  1515. MY_SYSTEM_STORE_NAME);
  1516. if ( hTempStore ) // otherwise, store is read only
  1517. {
  1518. VERIFY (::CertCloseStore (hTempStore, CERT_CLOSE_STORE_CHECK_FLAG));
  1519. }
  1520. else
  1521. {
  1522. _TRACE (0, L"CertOpenStore (%s) failed: 0x%x\n",
  1523. MY_SYSTEM_STORE_NAME, GetLastError ());
  1524. }
  1525. if ( !::CertEnumSystemStore (dwFlags, pvPara, &enumArg,
  1526. EnumIComponentDataSysCallback) )
  1527. {
  1528. DWORD dwErr = GetLastError ();
  1529. CString text;
  1530. CString caption;
  1531. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  1532. if ( ERROR_ACCESS_DENIED == dwErr )
  1533. {
  1534. VERIFY (text.LoadString (IDS_NO_PERMISSION));
  1535. }
  1536. else
  1537. {
  1538. text.FormatMessage (IDS_CANT_ENUMERATE_SYSTEM_STORES, GetSystemMessage (dwErr));
  1539. }
  1540. int iRetVal = 0;
  1541. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  1542. MB_OK, &iRetVal)));
  1543. hr = HRESULT_FROM_WIN32 (dwErr);
  1544. if ( ERROR_BAD_NETPATH == dwErr )
  1545. {
  1546. m_fInvalidComputer = true;
  1547. }
  1548. }
  1549. }
  1550. else
  1551. {
  1552. // CertOpenStore with provider type of:
  1553. // CERT_STORE_PROV_FILE or CERT_STORE_PROV_FILENAME_A
  1554. // or CERT_STORE_PROV_FILENAME_W.
  1555. // See online documentation or wincrypt.h for more info.
  1556. // Create new cookies
  1557. dwFlags = 0;
  1558. if ( ShowArchivedCerts () )
  1559. dwFlags |= CERT_STORE_ENUM_ARCHIVED_FLAG;
  1560. ASSERT (!m_pFileBasedStore);
  1561. m_pFileBasedStore = new CCertStore (
  1562. CERTMGR_LOG_STORE,
  1563. CERT_STORE_PROV_FILENAME_W,
  1564. dwFlags,
  1565. parentCookie.QueryNonNULLMachineName (),
  1566. m_szFileName, m_szFileName, L"", NO_SPECIAL_TYPE,
  1567. m_dwLocationPersist,
  1568. m_pConsole);
  1569. if ( m_pFileBasedStore )
  1570. {
  1571. m_pFileBasedStore->AddRef ();
  1572. hr = AddScopeNode (m_pFileBasedStore,
  1573. L"", hParent);
  1574. }
  1575. else
  1576. {
  1577. hr = E_OUTOFMEMORY;
  1578. }
  1579. }
  1580. _TRACE (-1, L"Leaving CCertMgrComponentData::AddLogicalStoresToScopePane: 0x%x\n", hr);
  1581. return hr;
  1582. }
  1583. // If the callback returns FALSE, stops the enumeration.
  1584. BOOL EnumOIDInfo (PCCRYPT_OID_INFO pInfo, void *pvArg)
  1585. {
  1586. _TRACE (1, L"Entering EnumOIDInfo\n");
  1587. ENUM_ARG* pEnumArg = (ENUM_ARG*) pvArg;
  1588. SCOPEDATAITEM tSDItem;
  1589. ::ZeroMemory (&tSDItem,sizeof (tSDItem));
  1590. tSDItem.mask = SDI_STR | SDI_IMAGE | SDI_STATE | SDI_PARAM | SDI_PARENT;
  1591. tSDItem.displayname = MMC_CALLBACK;
  1592. tSDItem.relativeID = pEnumArg->m_hParent;
  1593. tSDItem.nState = 0;
  1594. // See if this usage is already listed by name. If so, just add the
  1595. // additional OID, otherwise, create a new cookie.
  1596. CUsageCookie* pUsageCookie =
  1597. pEnumArg->m_pCompData->FindDuplicateUsage (pEnumArg->m_hParent,
  1598. pInfo->pwszName);
  1599. if ( !pUsageCookie )
  1600. {
  1601. pUsageCookie= new CUsageCookie (CERTMGR_USAGE,
  1602. pEnumArg->m_pcszMachineName,
  1603. pInfo->pwszName);
  1604. if ( pUsageCookie )
  1605. {
  1606. pEnumArg->m_pCompData->GetRootCookie ()->m_listScopeCookieBlocks.AddHead ( (CBaseCookieBlock*) pUsageCookie);
  1607. // WARNING cookie cast
  1608. tSDItem.mask |= SDI_CHILDREN;
  1609. tSDItem.cChildren = 0;
  1610. tSDItem.lParam = reinterpret_cast<LPARAM> ( (CCookie*) pUsageCookie);
  1611. tSDItem.nImage = pEnumArg->m_pCompData->QueryImage (*pUsageCookie, FALSE);
  1612. HRESULT hr = pEnumArg->m_pConsoleNameSpace->InsertItem (&tSDItem);
  1613. ASSERT (SUCCEEDED (hr));
  1614. }
  1615. }
  1616. pUsageCookie->AddOID (pInfo->pszOID);
  1617. _TRACE (-1, L"Leaving EnumOIDInfo\n");
  1618. return TRUE;
  1619. }
  1620. HRESULT CCertMgrComponentData::AddUsagesToScopePane (HSCOPEITEM hParent, CCertMgrCookie& parentCookie)
  1621. {
  1622. _TRACE (1, L"Entering CCertMgrComponentData::AddUsagesToScopePane\n");
  1623. HRESULT hr = S_OK;
  1624. ENUM_ARG enumArg;
  1625. ::ZeroMemory (&enumArg, sizeof (enumArg));
  1626. enumArg.m_dwFlags = 0;
  1627. enumArg.m_pConsoleNameSpace = m_pConsoleNameSpace;
  1628. enumArg.m_hParent = hParent;
  1629. enumArg.m_pCompData = this;
  1630. enumArg.m_pcszMachineName = parentCookie.QueryNonNULLMachineName ();
  1631. enumArg.m_pParentCookie = &parentCookie;
  1632. enumArg.m_pConsole = m_pConsole;
  1633. BOOL bResult = ::CryptEnumOIDInfo (CRYPT_ENHKEY_USAGE_OID_GROUP_ID, 0,
  1634. &enumArg, EnumOIDInfo);
  1635. ASSERT (bResult);
  1636. _TRACE (-1, L"Leaving CCertMgrComponentData::AddUsagesToScopePane: 0x%x\n", hr);
  1637. return hr;
  1638. }
  1639. BOOL IsMMCMultiSelectDataObject(IDataObject* pDataObject)
  1640. {
  1641. _TRACE (1, L"Entering IsMMCMultiSelectDataObject\n");
  1642. if (pDataObject == NULL)
  1643. return FALSE;
  1644. CLIPFORMAT s_cf = 0;
  1645. if (s_cf == 0)
  1646. {
  1647. USES_CONVERSION;
  1648. s_cf = (CLIPFORMAT)RegisterClipboardFormat(W2T(CCF_MMC_MULTISELECT_DATAOBJECT));
  1649. }
  1650. FORMATETC fmt = {s_cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  1651. BOOL bResult = ((pDataObject->QueryGetData(&fmt) == S_OK));
  1652. _TRACE (-1, L"Leaving IsMMCMultiSelectDataObject - return %d\n", bResult);
  1653. return bResult;
  1654. }
  1655. STDMETHODIMP CCertMgrComponentData::Command (long nCommandID, LPDATAOBJECT pDataObject)
  1656. {
  1657. _TRACE (1, L"Entering CCertMgrComponentData::Command\n");
  1658. HRESULT hr = S_OK;
  1659. switch (nCommandID)
  1660. {
  1661. case IDM_TASK_RENEW_SAME_KEY:
  1662. hr = OnRenew (pDataObject, false);
  1663. break;
  1664. case IDM_TASK_RENEW_NEW_KEY:
  1665. hr = OnRenew (pDataObject, true);
  1666. break;
  1667. case IDM_TASK_IMPORT:
  1668. hr = OnImport (pDataObject);
  1669. break;
  1670. case IDM_TASK_EXPORT:
  1671. hr = OnExport (pDataObject);
  1672. break;
  1673. case IDM_CTL_EDIT:
  1674. hr = OnCTLEdit (pDataObject);
  1675. break;
  1676. case IDM_EDIT_ACRS:
  1677. hr = OnACRSEdit (pDataObject);
  1678. break;
  1679. case IDM_NEW_CTL:
  1680. hr = OnNewCTL (pDataObject);
  1681. break;
  1682. case IDM_TASK_CTL_EXPORT:
  1683. case IDM_TASK_CRL_EXPORT:
  1684. hr = OnExport (pDataObject);
  1685. break;
  1686. case IDM_TASK_EXPORT_STORE:
  1687. hr = OnExport (pDataObject);
  1688. break;
  1689. case IDM_TOP_FIND:
  1690. case IDM_TASK_FIND:
  1691. hr = OnFind (pDataObject);
  1692. break;
  1693. case IDM_TASK_PULSEAUTOENROLL:
  1694. hr = OnPulseAutoEnroll();
  1695. break;
  1696. case IDM_TOP_CHANGE_COMPUTER:
  1697. case IDM_TASK_CHANGE_COMPUTER:
  1698. hr = OnChangeComputer (pDataObject);
  1699. break;
  1700. case IDM_ENROLL_NEW_CERT:
  1701. hr = OnEnroll (pDataObject, true);
  1702. break;
  1703. case IDM_ENROLL_NEW_CERT_SAME_KEY:
  1704. hr = OnEnroll (pDataObject, false);
  1705. break;
  1706. case IDM_ENROLL_NEW_CERT_NEW_KEY:
  1707. hr = OnEnroll (pDataObject, true);
  1708. break;
  1709. case IDM_OPTIONS:
  1710. hr = OnOptions (pDataObject);
  1711. break;
  1712. case IDM_INIT_POLICY:
  1713. hr = OnInitEFSPolicy (pDataObject);
  1714. break;
  1715. case IDM_DEL_POLICY:
  1716. case IDM_DEL_POLICY1:
  1717. {
  1718. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  1719. CString text;
  1720. CString caption;
  1721. int iRetVal = 0;
  1722. VERIFY (text.LoadString (IDS_CONFIRM_DELETE_EFS_POLICY));
  1723. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  1724. hr = m_pConsole->MessageBox (text, caption,
  1725. MB_YESNO, &iRetVal);
  1726. ASSERT (SUCCEEDED (hr));
  1727. if ( SUCCEEDED (hr) && IDYES == iRetVal )
  1728. hr = OnDeleteEFSPolicy (pDataObject, true);
  1729. }
  1730. break;
  1731. case IDM_ADD_DOMAIN_ENCRYPTED_RECOVERY_AGENT:
  1732. case IDM_ADD_DOMAIN_ENCRYPTED_RECOVERY_AGENT1:
  1733. case IDM_ADD_DOMAIN_ENCRYPTED_RECOVERY_AGENT2:
  1734. hr = OnAddDomainEncryptedDataRecoveryAgent (pDataObject);
  1735. break;
  1736. case IDM_CREATE_DOMAIN_ENCRYPTED_RECOVERY_AGENT:
  1737. hr = OnEnroll (pDataObject, true);
  1738. break;
  1739. case IDM_NEW_ACRS:
  1740. hr = OnNewACRS (pDataObject);
  1741. break;
  1742. case IDM_SAFER_LEVEL_SET_DEFAULT:
  1743. hr = OnSetSaferLevelDefault (pDataObject);
  1744. break;
  1745. case IDM_SAFER_NEW_ENTRY_PATH:
  1746. case IDM_SAFER_NEW_ENTRY_HASH:
  1747. case IDM_SAFER_NEW_ENTRY_CERTIFICATE:
  1748. case IDM_SAFER_NEW_ENTRY_INTERNET_ZONE:
  1749. hr = OnNewSaferEntry (nCommandID, pDataObject);
  1750. break;
  1751. case IDM_TOP_CREATE_NEW_SAFER_POLICY:
  1752. case IDM_TASK_CREATE_NEW_SAFER_POLICY:
  1753. hr = OnCreateNewSaferPolicy (pDataObject);
  1754. break;
  1755. case -1: // Received on forward/back buttons from toolbar
  1756. break;
  1757. default:
  1758. ASSERT (0);
  1759. break;
  1760. }
  1761. _TRACE (-1, L"Leaving CCertMgrComponentData::Command: 0x%x\n", hr);
  1762. return hr;
  1763. }
  1764. HRESULT CCertMgrComponentData::OnNewACRS (LPDATAOBJECT pDataObject)
  1765. {
  1766. _TRACE (1, L"Entering CCertMgrComponentData::OnNewACRS\n");
  1767. ASSERT (pDataObject);
  1768. if ( !pDataObject )
  1769. return E_POINTER;
  1770. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  1771. HRESULT hr = S_OK;
  1772. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  1773. ASSERT (pCookie);
  1774. if ( pCookie )
  1775. {
  1776. if ( CERTMGR_LOG_STORE_GPE == pCookie->m_objecttype )
  1777. {
  1778. CCertStoreGPE* pStore = reinterpret_cast <CCertStoreGPE*> (pCookie);
  1779. ASSERT (pStore);
  1780. if ( pStore )
  1781. {
  1782. HWND hwndConsole = 0;
  1783. hr = m_pConsole->GetMainWindow (&hwndConsole);
  1784. ASSERT (SUCCEEDED (hr));
  1785. if ( SUCCEEDED (hr) )
  1786. {
  1787. ACRSWizardPropertySheet sheet (pStore, NULL);
  1788. ACRSWizardWelcomePage welcomePage;
  1789. ACRSWizardTypePage typePage;
  1790. ACRSCompletionPage completionPage;
  1791. sheet.AddPage (&welcomePage);
  1792. sheet.AddPage (&typePage);
  1793. sheet.AddPage (&completionPage);
  1794. if ( sheet.DoWizard (hwndConsole) )
  1795. {
  1796. pStore->SetDirty ();
  1797. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  1798. ASSERT (SUCCEEDED (hr));
  1799. }
  1800. }
  1801. }
  1802. else
  1803. hr = E_UNEXPECTED;
  1804. }
  1805. else
  1806. hr = E_UNEXPECTED;
  1807. }
  1808. else
  1809. hr = E_UNEXPECTED;
  1810. _TRACE (-1, L"Leaving CCertMgrComponentData::OnNewACRS: 0x%x\n", hr);
  1811. return hr;
  1812. }
  1813. HRESULT CCertMgrComponentData::RefreshScopePane (LPDATAOBJECT pDataObject)
  1814. {
  1815. _TRACE (1, L"Entering CCertMgrComponentData::RefreshScopePane\n");
  1816. HRESULT hr = S_OK;
  1817. CCertMgrCookie* pCookie = 0;
  1818. if ( pDataObject )
  1819. pCookie = ConvertCookie (pDataObject);
  1820. if ( !pDataObject || ( pCookie && ((CCertMgrCookie*) CERTMGR_NULL_POLICY) != pCookie) )
  1821. {
  1822. // If m_hRootScopeItem is NULL, then this is an extension and we don't want to go in here.
  1823. if ( !pDataObject || (m_hRootScopeItem && pCookie->m_hScopeItem == m_hRootScopeItem) )
  1824. {
  1825. hr = DeleteScopeItems ();
  1826. ASSERT (SUCCEEDED (hr));
  1827. if ( 1 ) //SUCCEEDED (hr) )
  1828. {
  1829. GUID guid;
  1830. hr = ExpandScopeNodes (m_pRootCookie, m_hRootScopeItem,
  1831. _T (""), 0, guid);
  1832. }
  1833. else if ( E_UNEXPECTED == hr )
  1834. {
  1835. ASSERT (0);
  1836. }
  1837. else if ( E_INVALIDARG == hr )
  1838. {
  1839. ASSERT (0);
  1840. }
  1841. }
  1842. else
  1843. {
  1844. switch (pCookie->m_objecttype)
  1845. {
  1846. case CERTMGR_LOG_STORE_GPE:
  1847. case CERTMGR_LOG_STORE_RSOP:
  1848. case CERTMGR_USAGE:
  1849. case CERTMGR_LOG_STORE:
  1850. case CERTMGR_PHYS_STORE:
  1851. case CERTMGR_CRL_CONTAINER:
  1852. case CERTMGR_CTL_CONTAINER:
  1853. case CERTMGR_CERT_CONTAINER:
  1854. hr = DeleteChildren (pCookie->m_hScopeItem);
  1855. break;
  1856. default:
  1857. break;
  1858. }
  1859. }
  1860. }
  1861. _TRACE (-1, L"Leaving CCertMgrComponentData::RefreshScopePane: 0x%x\n", hr);
  1862. return hr;
  1863. }
  1864. HRESULT CCertMgrComponentData::ExpandScopeNodes (
  1865. CCertMgrCookie* pParentCookie,
  1866. HSCOPEITEM hParent,
  1867. const CString& strServerName,
  1868. DWORD dwLocation,
  1869. const GUID& guidObjectType)
  1870. {
  1871. _TRACE (1, L"Entering CCertMgrComponentData::ExpandScopeNodes\n");
  1872. ASSERT (hParent);
  1873. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  1874. CWaitCursor waitCursor;
  1875. HRESULT hr = S_OK;
  1876. if ( pParentCookie )
  1877. {
  1878. CString objectName;
  1879. switch ( pParentCookie->m_objecttype )
  1880. {
  1881. // These node types have no children yet
  1882. case CERTMGR_SNAPIN:
  1883. // We don't expect the handle of the root scope item to change, ever!
  1884. ASSERT ( m_hRootScopeItem ? (m_hRootScopeItem == hParent) : 1);
  1885. if ( !m_hRootScopeItem )
  1886. m_hRootScopeItem = hParent;
  1887. switch (m_activeViewPersist)
  1888. {
  1889. case IDM_USAGE_VIEW:
  1890. hr = AddUsagesToScopePane (hParent, *pParentCookie);
  1891. break;
  1892. case IDM_STORE_VIEW:
  1893. hr = AddLogicalStoresToScopePane (hParent, *pParentCookie);
  1894. break;
  1895. default:
  1896. ASSERT (0);
  1897. hr = E_UNEXPECTED;
  1898. break;
  1899. }
  1900. break;
  1901. // This node type has no children
  1902. case CERTMGR_USAGE:
  1903. case CERTMGR_CRL_CONTAINER:
  1904. case CERTMGR_CTL_CONTAINER:
  1905. case CERTMGR_CERT_CONTAINER:
  1906. break;
  1907. case CERTMGR_PHYS_STORE:
  1908. // Create one each of a CRL_CONTAINER node, CTL_CONTAINER
  1909. // node and CERT container node.
  1910. hr = AddContainersToScopePane (hParent, *pParentCookie,
  1911. false);
  1912. break;
  1913. case CERTMGR_LOG_STORE_RSOP:
  1914. case CERTMGR_LOG_STORE_GPE:
  1915. // This is the Group Policy Editor extension
  1916. // This node type has no children
  1917. break;
  1918. case CERTMGR_LOG_STORE:
  1919. if ( m_bShowPhysicalStoresPersist )
  1920. {
  1921. SPECIAL_STORE_TYPE storeType = NO_SPECIAL_TYPE;
  1922. CCertStore* pCertStoreCookie =
  1923. reinterpret_cast <CCertStore*> (pParentCookie);
  1924. ASSERT (pCertStoreCookie);
  1925. if ( pCertStoreCookie )
  1926. storeType = pCertStoreCookie->GetStoreType ();
  1927. hr = AddPhysicalStoresToScopePane (hParent, *pParentCookie,
  1928. storeType);
  1929. }
  1930. else
  1931. {
  1932. // Create one each of a CRL_CONTAINER node, CTL_CONTAINER
  1933. // node and CERT container node.
  1934. hr = AddContainersToScopePane (hParent, *pParentCookie,
  1935. false);
  1936. }
  1937. break;
  1938. case CERTMGR_CERT_POLICIES_USER:
  1939. // Don't add these nodes for local machine policy
  1940. if ( SCE_MODE_LOCAL_COMPUTER != m_dwSCEMode )
  1941. {
  1942. // Add "Trusted Certificate Authorities"
  1943. VERIFY (objectName.LoadString (IDS_CERTIFICATE_TRUST_LISTS));
  1944. if ( SUCCEEDED (hr) )
  1945. {
  1946. if ( m_pGPEInformation )
  1947. {
  1948. hr = AddScopeNode (new CCertStoreGPE (
  1949. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  1950. _T (""),
  1951. (PCWSTR) objectName,
  1952. TRUST_SYSTEM_STORE_NAME,
  1953. _T (""),
  1954. m_pGPEInformation,
  1955. NODEID_User,
  1956. m_pConsole),
  1957. strServerName,
  1958. hParent);
  1959. }
  1960. else if ( m_pRSOPInformationUser )
  1961. {
  1962. hr = AddScopeNode (new CCertStoreRSOP (
  1963. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  1964. _T (""),
  1965. (PCWSTR) objectName,
  1966. TRUST_SYSTEM_STORE_NAME,
  1967. _T (""),
  1968. m_rsopObjectArrayUser,
  1969. NODEID_User,
  1970. m_pConsole),
  1971. strServerName,
  1972. hParent);
  1973. }
  1974. }
  1975. }
  1976. break;
  1977. case CERTMGR_CERT_POLICIES_COMPUTER:
  1978. // Add only this node for local machine policy
  1979. // Add "Encrypting File System"
  1980. VERIFY (objectName.LoadString (IDS_ENCRYPTING_FILE_SYSTEM_NODE_NAME));
  1981. if ( m_pGPEInformation )
  1982. {
  1983. hr = AddScopeNode (new CCertStoreGPE (
  1984. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  1985. _T (""),
  1986. (PCWSTR) objectName,
  1987. EFS_SYSTEM_STORE_NAME,
  1988. _T (""),
  1989. m_pGPEInformation,
  1990. NODEID_Machine,
  1991. m_pConsole),
  1992. strServerName,
  1993. hParent);
  1994. }
  1995. else if ( m_pRSOPInformationComputer )
  1996. {
  1997. hr = AddScopeNode (new CCertStoreRSOP (
  1998. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  1999. _T (""),
  2000. (PCWSTR) objectName,
  2001. EFS_SYSTEM_STORE_NAME,
  2002. _T (""),
  2003. m_rsopObjectArrayComputer,
  2004. NODEID_Machine,
  2005. m_pConsole),
  2006. strServerName,
  2007. hParent);
  2008. }
  2009. if ( SCE_MODE_LOCAL_COMPUTER != m_dwSCEMode )
  2010. {
  2011. // Add these policies if this is the domain policy
  2012. if ( SUCCEEDED (hr) )
  2013. {
  2014. // Add "Automatic Certificate Request Settings"
  2015. VERIFY (objectName.LoadString (IDS_AUTOMATIC_CERT_REQUEST_SETTINGS_NODE_NAME));
  2016. if ( m_pGPEInformation )
  2017. {
  2018. m_pGPEACRSComputerStore = new CCertStoreGPE (
  2019. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  2020. _T (""),
  2021. (PCWSTR) objectName,
  2022. ACRS_SYSTEM_STORE_NAME,
  2023. _T (""),
  2024. m_pGPEInformation,
  2025. NODEID_Machine,
  2026. m_pConsole);
  2027. }
  2028. else if ( m_pRSOPInformationComputer )
  2029. {
  2030. m_pGPEACRSComputerStore = new CCertStoreRSOP (
  2031. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  2032. _T (""),
  2033. (PCWSTR) objectName,
  2034. ACRS_SYSTEM_STORE_NAME,
  2035. _T (""),
  2036. m_rsopObjectArrayComputer,
  2037. NODEID_Machine,
  2038. m_pConsole);
  2039. }
  2040. if ( m_pGPEACRSComputerStore )
  2041. {
  2042. m_pGPEACRSComputerStore->AddRef ();
  2043. hr = AddScopeNode (m_pGPEACRSComputerStore,
  2044. strServerName, hParent);
  2045. }
  2046. else
  2047. {
  2048. hr = E_OUTOFMEMORY;
  2049. }
  2050. }
  2051. if ( SUCCEEDED (hr) )
  2052. {
  2053. // Add "Domain Root Certificate Authorities"
  2054. VERIFY (objectName.LoadString (IDS_DOMAIN_ROOT_CERT_AUTHS_NODE_NAME));
  2055. ASSERT (!m_pGPERootStore);
  2056. if ( m_pGPEInformation )
  2057. {
  2058. m_pGPERootStore = new CCertStoreGPE (
  2059. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  2060. _T (""),
  2061. (PCWSTR) objectName,
  2062. ROOT_SYSTEM_STORE_NAME,
  2063. _T (""),
  2064. m_pGPEInformation,
  2065. NODEID_Machine,
  2066. m_pConsole);
  2067. }
  2068. else if ( m_pRSOPInformationComputer )
  2069. {
  2070. m_pGPERootStore = new CCertStoreRSOP (
  2071. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  2072. _T (""),
  2073. (PCWSTR) objectName,
  2074. ROOT_SYSTEM_STORE_NAME,
  2075. _T (""),
  2076. m_rsopObjectArrayComputer,
  2077. NODEID_Machine,
  2078. m_pConsole);
  2079. }
  2080. if ( m_pGPERootStore )
  2081. {
  2082. m_pGPERootStore->AddRef ();
  2083. hr = AddScopeNode (m_pGPERootStore,
  2084. strServerName, hParent);
  2085. }
  2086. else
  2087. {
  2088. hr = E_OUTOFMEMORY;
  2089. }
  2090. }
  2091. if ( SUCCEEDED (hr) )
  2092. {
  2093. // Add "Trusted Certificate Authorities"
  2094. VERIFY (objectName.LoadString (IDS_CERTIFICATE_TRUST_LISTS));
  2095. ASSERT (!m_pGPETrustStore);
  2096. if ( m_pGPEInformation )
  2097. {
  2098. m_pGPETrustStore = new CCertStoreGPE (
  2099. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  2100. _T (""),
  2101. (PCWSTR) objectName,
  2102. TRUST_SYSTEM_STORE_NAME,
  2103. _T (""),
  2104. m_pGPEInformation,
  2105. NODEID_Machine,
  2106. m_pConsole);
  2107. }
  2108. else if ( m_pRSOPInformationComputer )
  2109. {
  2110. m_pGPETrustStore = new CCertStoreRSOP (
  2111. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  2112. _T (""),
  2113. (PCWSTR) objectName,
  2114. TRUST_SYSTEM_STORE_NAME,
  2115. _T (""),
  2116. m_rsopObjectArrayComputer,
  2117. NODEID_Machine,
  2118. m_pConsole);
  2119. }
  2120. if ( m_pGPETrustStore )
  2121. {
  2122. m_pGPETrustStore->AddRef ();
  2123. hr = AddScopeNode (m_pGPETrustStore,
  2124. strServerName, hParent);
  2125. }
  2126. else
  2127. {
  2128. hr = E_OUTOFMEMORY;
  2129. }
  2130. }
  2131. }
  2132. break;
  2133. case CERTMGR_SAFER_COMPUTER_ROOT:
  2134. case CERTMGR_SAFER_USER_ROOT:
  2135. {
  2136. CSaferRootCookie* pSaferRootCookie =
  2137. dynamic_cast <CSaferRootCookie*> (pParentCookie);
  2138. if ( pSaferRootCookie )
  2139. {
  2140. bool bIsComputer =
  2141. (CERTMGR_SAFER_COMPUTER_ROOT == pParentCookie->m_objecttype);
  2142. if ( !m_bIsRSOP )
  2143. {
  2144. // Find out if Safer is supported by the OS
  2145. m_bSaferSupported = false;
  2146. SAFER_LEVEL_HANDLE hLevel = 0;
  2147. CPolicyKey policyKey (m_pGPEInformation,
  2148. SAFER_HKLM_REGBASE,
  2149. CERTMGR_SAFER_COMPUTER_ROOT == pParentCookie->m_objecttype);
  2150. SetRegistryScope (policyKey.GetKey (),
  2151. CERTMGR_SAFER_COMPUTER_ROOT == pParentCookie->m_objecttype);
  2152. BOOL bRVal = SaferCreateLevel (SAFER_SCOPEID_REGISTRY,
  2153. SAFER_LEVELID_FULLYTRUSTED,
  2154. SAFER_LEVEL_OPEN,
  2155. &hLevel,
  2156. policyKey.GetKey ());
  2157. if ( bRVal )
  2158. {
  2159. m_bSaferSupported = true;
  2160. VERIFY (SaferCloseLevel (hLevel));
  2161. }
  2162. else
  2163. {
  2164. DWORD dwErr = GetLastError ();
  2165. _TRACE (0, L"SaferCreateLevel () failed: 0x%x\n", dwErr);
  2166. }
  2167. // Install default file types
  2168. if ( m_bSaferSupported && m_pGPEInformation )
  2169. {
  2170. HKEY hGroupPolicyKey = 0;
  2171. hr = m_pGPEInformation->GetRegistryKey (
  2172. bIsComputer ?
  2173. GPO_SECTION_MACHINE : GPO_SECTION_USER,
  2174. &hGroupPolicyKey);
  2175. if ( SUCCEEDED (hr) )
  2176. {
  2177. // Check to see if safer defaults have already
  2178. // been defined. If not, prompt the user
  2179. // for confirmation. If the response is "no"
  2180. // then do not create the nodes
  2181. PCWSTR pszKeyName = bIsComputer ?
  2182. SAFER_COMPUTER_CODEIDS_REGKEY :
  2183. SAFER_USER_CODEIDS_REGKEY;
  2184. HKEY hCodeIDsKey = 0;
  2185. LONG lResult = RegOpenKeyEx (hGroupPolicyKey,
  2186. pszKeyName, 0, KEY_READ, &hCodeIDsKey);
  2187. if ( ERROR_FILE_NOT_FOUND == lResult )
  2188. {
  2189. pSaferRootCookie->m_bCreateSaferNodes = false;
  2190. break;
  2191. }
  2192. else if ( hCodeIDsKey )
  2193. {
  2194. ::RegCloseKey (hCodeIDsKey);
  2195. hCodeIDsKey = 0;
  2196. }
  2197. ::RegCloseKey (hGroupPolicyKey);
  2198. }
  2199. }
  2200. }
  2201. if ( m_bSaferSupported || m_bIsRSOP )
  2202. {
  2203. // Add "Levels" node
  2204. VERIFY (objectName.LoadString (IDS_SAFER_LEVELS_NODE_NAME));
  2205. hr = AddScopeNode (new CCertMgrCookie (
  2206. bIsComputer ?
  2207. CERTMGR_SAFER_COMPUTER_LEVELS : CERTMGR_SAFER_USER_LEVELS,
  2208. 0,
  2209. (PCWSTR) objectName), strServerName, hParent);
  2210. // Add "Entries" node
  2211. if ( SUCCEEDED (hr) )
  2212. {
  2213. VERIFY (objectName.LoadString (IDS_SAFER_ENTRIES_NODE_NAME));
  2214. hr = AddScopeNode (new CSaferEntries (
  2215. bIsComputer,
  2216. strServerName,
  2217. objectName,
  2218. m_pGPEInformation,
  2219. bIsComputer ? m_pRSOPInformationComputer : m_pRSOPInformationUser,
  2220. bIsComputer ? m_rsopObjectArrayComputer : m_rsopObjectArrayUser,
  2221. m_pConsole),
  2222. strServerName, hParent);
  2223. if ( SUCCEEDED (hr) )
  2224. {
  2225. hr = SaferEnumerateLevels (bIsComputer);
  2226. }
  2227. }
  2228. }
  2229. }
  2230. }
  2231. break;
  2232. case CERTMGR_SAFER_COMPUTER_LEVELS:
  2233. break;
  2234. case CERTMGR_SAFER_USER_LEVELS:
  2235. // TODO: Enumerate user levels
  2236. break;
  2237. case CERTMGR_SAFER_COMPUTER_ENTRIES:
  2238. // TODO: Enumerate computer entries
  2239. break;
  2240. case CERTMGR_SAFER_USER_ENTRIES:
  2241. // TODO: Enumerate user entries
  2242. break;
  2243. case CERTMGR_CERTIFICATE: // not expected in scope pane
  2244. case CERTMGR_CRL:
  2245. case CERTMGR_CTL:
  2246. case CERTMGR_AUTO_CERT_REQUEST:
  2247. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  2248. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  2249. ASSERT (0);
  2250. hr = E_UNEXPECTED;
  2251. break;
  2252. default:
  2253. _TRACE (0, L"CCertMgrComponentData::EnumerateScopeChildren bad parent type\n");
  2254. ASSERT (0);
  2255. hr = S_OK;
  2256. break;
  2257. }
  2258. }
  2259. else
  2260. {
  2261. // If parentCookie not passed in, then this is an extension snap-in
  2262. m_dwLocationPersist = dwLocation;
  2263. if ( m_pGPEInformation || m_pRSOPInformationComputer || m_pRSOPInformationUser )
  2264. {
  2265. CString objectName;
  2266. if ( ::IsEqualGUID (guidObjectType, NODEID_Machine) )
  2267. {
  2268. if ( SUCCEEDED (hr) )
  2269. {
  2270. CLSID classID;
  2271. GetClassID (&classID);
  2272. if ( ::IsEqualGUID (classID, CLSID_CertificateManagerPKPOLExt) )
  2273. {
  2274. // Add "Public Key Policies" node
  2275. VERIFY (objectName.LoadString (IDS_PUBLIC_KEY_POLICIES_NODE_NAME));
  2276. hr = AddScopeNode (new CCertMgrCookie (
  2277. CERTMGR_CERT_POLICIES_COMPUTER,
  2278. 0,
  2279. (PCWSTR) objectName),
  2280. strServerName, hParent);
  2281. }
  2282. else if ( ::IsEqualGUID (classID, CLSID_SaferWindowsExtension) )
  2283. {
  2284. // Add "Software Restriction Policies" node
  2285. VERIFY (objectName.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  2286. hr = AddScopeNode (new CSaferRootCookie (
  2287. CERTMGR_SAFER_COMPUTER_ROOT,
  2288. 0,
  2289. (PCWSTR) objectName),
  2290. strServerName, hParent);
  2291. }
  2292. }
  2293. }
  2294. else if ( ::IsEqualGUID (guidObjectType, NODEID_User) )
  2295. {
  2296. if ( SUCCEEDED (hr) )
  2297. {
  2298. CLSID classID;
  2299. GetClassID (&classID);
  2300. if ( ::IsEqualGUID (classID, CLSID_CertificateManagerPKPOLExt) )
  2301. {
  2302. // Add "Public Key Policies" node
  2303. VERIFY (objectName.LoadString (IDS_PUBLIC_KEY_POLICIES_NODE_NAME));
  2304. hr = AddScopeNode (new CCertMgrCookie (
  2305. CERTMGR_CERT_POLICIES_USER,
  2306. 0,
  2307. (PCWSTR) objectName), strServerName, hParent);
  2308. }
  2309. else if ( ::IsEqualGUID (classID, CLSID_SaferWindowsExtension) )
  2310. {
  2311. if ( SCE_MODE_LOCAL_USER != m_dwSCEMode )
  2312. {
  2313. // Add "Software Restriction Policies" node
  2314. VERIFY (objectName.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  2315. hr = AddScopeNode (new CSaferRootCookie (
  2316. CERTMGR_SAFER_USER_ROOT,
  2317. 0,
  2318. (PCWSTR) objectName), strServerName, hParent);
  2319. }
  2320. }
  2321. }
  2322. }
  2323. }
  2324. }
  2325. _TRACE (-1, L"Leaving CCertMgrComponentData::ExpandScopeNodes: 0x%x\n", hr);
  2326. return hr;
  2327. }
  2328. HRESULT CCertMgrComponentData::DeleteScopeItems (HSCOPEITEM hScopeItem /* = 0 */)
  2329. {
  2330. _TRACE (1, L"Entering CCertMgrComponentData::DeleteScopeItems\n");
  2331. HRESULT hr = S_OK;
  2332. if ( m_pGPERootStore )
  2333. {
  2334. m_pGPERootStore->Release ();
  2335. m_pGPERootStore = 0;
  2336. }
  2337. if ( m_pGPETrustStore )
  2338. {
  2339. m_pGPETrustStore->Release ();
  2340. m_pGPETrustStore = 0;
  2341. }
  2342. if ( m_pGPEACRSComputerStore )
  2343. {
  2344. m_pGPEACRSComputerStore->Release ();
  2345. m_pGPEACRSComputerStore = 0;
  2346. }
  2347. if ( m_pGPEACRSUserStore )
  2348. {
  2349. m_pGPEACRSUserStore->Release ();
  2350. m_pGPEACRSUserStore = 0;
  2351. }
  2352. if ( m_pFileBasedStore )
  2353. {
  2354. m_pFileBasedStore->Release ();
  2355. m_pFileBasedStore = 0;
  2356. }
  2357. hr = DeleteChildren (hScopeItem ? hScopeItem : m_hRootScopeItem);
  2358. _TRACE (-1, L"Leaving CCertMgrComponentData::DeleteScopeItems: 0x%x\n", hr);
  2359. return hr;
  2360. }
  2361. HRESULT CCertMgrComponentData::DeleteChildren (HSCOPEITEM hParent)
  2362. {
  2363. _TRACE (1, L"Entering CCertMgrComponentData::DeleteChildren\n");
  2364. if ( !hParent )
  2365. return S_OK;
  2366. HSCOPEITEM hChild = 0;
  2367. HSCOPEITEM hNextChild = 0;
  2368. MMC_COOKIE lCookie = 0;
  2369. CCertMgrCookie* pCookie = 0;
  2370. HRESULT hr = S_OK;
  2371. CCookie& rootCookie = QueryBaseRootCookie ();
  2372. // Optimization: If we're deleting everything below the root, free all
  2373. // the result items here so we don't have to go looking for them later by
  2374. // store
  2375. if ( hParent == m_hRootScopeItem )
  2376. {
  2377. LPRESULTDATA pResultData = 0;
  2378. hr = GetResultData (&pResultData);
  2379. if ( SUCCEEDED (hr) )
  2380. {
  2381. hr = pResultData->DeleteAllRsltItems ();
  2382. if ( SUCCEEDED (hr) || E_UNEXPECTED == hr ) // returns E_UNEXPECTED if console shutting down
  2383. {
  2384. RemoveResultCookies (pResultData);
  2385. }
  2386. else
  2387. {
  2388. _TRACE (0, L"IResultData::DeleteAllRsltItems () failed: 0x%x\n", hr);
  2389. }
  2390. pResultData->Release ();
  2391. }
  2392. }
  2393. hr = m_pConsoleNameSpace->GetChildItem (hParent, &hChild, &lCookie);
  2394. ASSERT (SUCCEEDED (hr) || E_FAIL == hr); // appears to return E_FAIL when there are no children
  2395. while ( SUCCEEDED (hr) && hChild )
  2396. {
  2397. pCookie = reinterpret_cast <CCertMgrCookie*> (lCookie);
  2398. hr = DeleteChildren (hChild);
  2399. if ( !SUCCEEDED (hr) )
  2400. break;
  2401. hNextChild = 0;
  2402. hr = m_pConsoleNameSpace->GetNextItem (hChild, &hNextChild, &lCookie);
  2403. ASSERT (SUCCEEDED (hr));
  2404. hr = m_pConsoleNameSpace->DeleteItem (hChild, TRUE);
  2405. ASSERT (SUCCEEDED (hr));
  2406. switch (pCookie->m_objecttype)
  2407. {
  2408. case CERTMGR_LOG_STORE:
  2409. case CERTMGR_LOG_STORE_GPE:
  2410. case CERTMGR_LOG_STORE_RSOP:
  2411. case CERTMGR_PHYS_STORE:
  2412. {
  2413. // If this is a store, delete all the result nodes that belong to this
  2414. // store. We can tell if objects were enumerated from this store simply
  2415. // by comparing the store handles.
  2416. CCertStore* pStore = reinterpret_cast <CCertStore*> (pCookie);
  2417. ASSERT (pStore);
  2418. if ( pStore )
  2419. {
  2420. // If the store is not 'open' (it's HCERTSTORE handle is still '0')
  2421. // then we can skip checking this list. We haven't enumerated anything
  2422. // in this store.
  2423. if ( pStore->IsOpen () )
  2424. {
  2425. POSITION pos1 = 0;
  2426. POSITION pos2 = 0;
  2427. CBaseCookieBlock* pResultCookie = 0;
  2428. HCERTSTORE hStoreHandle = pStore->GetStoreHandle ();
  2429. // As an optimization, if DeleteChildren was originally called with
  2430. // the root scope item, all the result cookies have already been
  2431. // deleted since we're going to delete them all anyway.
  2432. for (pos1 = rootCookie.m_listResultCookieBlocks.GetHeadPosition();
  2433. (pos2 = pos1) != NULL; )
  2434. {
  2435. pResultCookie = rootCookie.m_listResultCookieBlocks.GetNext (pos1);
  2436. ASSERT (pResultCookie);
  2437. if ( pResultCookie )
  2438. {
  2439. hr = ReleaseResultCookie (pResultCookie,
  2440. rootCookie, hStoreHandle, pos2);
  2441. }
  2442. }
  2443. pStore->Close ();
  2444. }
  2445. }
  2446. }
  2447. // fall through
  2448. case CERTMGR_CRL_CONTAINER:
  2449. case CERTMGR_CTL_CONTAINER:
  2450. case CERTMGR_CERT_CONTAINER:
  2451. case CERTMGR_USAGE:
  2452. {
  2453. POSITION pos1 = 0;
  2454. POSITION pos2 = 0;
  2455. CBaseCookieBlock* pResultCookie = 0;
  2456. // Find and remove this cookie from the scope cookie list
  2457. for (pos1 = rootCookie.m_listScopeCookieBlocks.GetHeadPosition();
  2458. pos1 != NULL; )
  2459. {
  2460. pos2 = pos1;
  2461. pResultCookie = rootCookie.m_listResultCookieBlocks.GetNext (pos1);
  2462. ASSERT (pResultCookie);
  2463. if ( pResultCookie )
  2464. {
  2465. if ( pResultCookie->QueryBaseCookie (0) == pCookie )
  2466. {
  2467. rootCookie.m_listScopeCookieBlocks.RemoveAt (pos2);
  2468. pResultCookie->Release ();
  2469. break;
  2470. }
  2471. }
  2472. }
  2473. }
  2474. break;
  2475. default:
  2476. break;
  2477. }
  2478. hChild = hNextChild;
  2479. }
  2480. _TRACE (-1, L"Leaving CCertMgrComponentData::DeleteChildren: 0x%x\n", hr);
  2481. return hr;
  2482. }
  2483. CertificateManagerObjectType CCertMgrComponentData::GetObjectType (LPDATAOBJECT pDataObject)
  2484. {
  2485. _TRACE (1, L"Entering CCertMgrComponentData::GetObjectType\n");
  2486. CertificateManagerObjectType objType = CERTMGR_INVALID;
  2487. ASSERT (pDataObject);
  2488. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  2489. if ( ((CCertMgrCookie*) MMC_MULTI_SELECT_COOKIE) == pCookie )
  2490. objType = CERTMGR_MULTISEL;
  2491. else if ( pCookie )
  2492. objType = pCookie->m_objecttype;
  2493. _TRACE (-1, L"Leaving CCertMgrComponentData::GetObjectType\n");
  2494. return objType;
  2495. }
  2496. HRESULT CCertMgrComponentData::OnPulseAutoEnroll()
  2497. {
  2498. _TRACE (1, L"Entering CCertMgrComponentData::OnPulseAutoEnroll\n");
  2499. HRESULT hr = S_OK;
  2500. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  2501. HANDLE hEvent = NULL;
  2502. LPWSTR wszEventName;
  2503. // pulse autoenroll event here, choose between machine or user
  2504. // user or machine pulse?
  2505. wszEventName = L"Global\\" MACHINE_AUTOENROLLMENT_TRIGGER_EVENT;
  2506. if (CERT_SYSTEM_STORE_CURRENT_USER == m_dwLocationPersist)
  2507. wszEventName = USER_AUTOENROLLMENT_TRIGGER_EVENT;
  2508. hEvent=OpenEvent(EVENT_MODIFY_STATE, false, wszEventName);
  2509. if (NULL==hEvent)
  2510. {
  2511. DWORD dwErr = GetLastError();
  2512. CString text;
  2513. CString caption;
  2514. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  2515. text.FormatMessage (IDS_CANT_OPEN_AUTOENROLL_EVENT, GetSystemMessage (dwErr));
  2516. int iRetVal = 0;
  2517. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption, MB_OK, &iRetVal)));
  2518. hr=HRESULT_FROM_WIN32(dwErr);
  2519. _TRACE (0, L"OpenEvent(%s) failed with 0x%08X.\n", wszEventName, hr);
  2520. goto error;
  2521. }
  2522. if (!SetEvent(hEvent))
  2523. {
  2524. DWORD dwErr = GetLastError();
  2525. DisplaySystemError (dwErr);
  2526. hr=HRESULT_FROM_WIN32(dwErr);
  2527. _TRACE (0, L"SetEvent failed with 0x%08X.\n", hr);
  2528. goto error;
  2529. }
  2530. error:
  2531. if (NULL!=hEvent)
  2532. CloseHandle(hEvent);
  2533. _TRACE (-1, L"Leaving CCertMgrComponentData::OnPulseAutoEnroll: 0x%x\n", hr);
  2534. return hr;
  2535. }
  2536. HRESULT CCertMgrComponentData::OnFind (LPDATAOBJECT pDataObject)
  2537. {
  2538. _TRACE (1, L"Entering CCertMgrComponentData::OnFind\n");
  2539. HRESULT hr = S_OK;
  2540. ASSERT (pDataObject);
  2541. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  2542. HWND hParent = 0;
  2543. ASSERT (pDataObject);
  2544. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  2545. if ( pParentCookie )
  2546. {
  2547. switch (pParentCookie->m_objecttype)
  2548. {
  2549. case CERTMGR_SNAPIN:
  2550. case CERTMGR_PHYS_STORE:
  2551. case CERTMGR_LOG_STORE:
  2552. case CERTMGR_LOG_STORE_GPE:
  2553. case CERTMGR_LOG_STORE_RSOP:
  2554. case CERTMGR_USAGE:
  2555. {
  2556. // Get parent window handle and attach to a CWnd object
  2557. hr = m_pConsole->GetMainWindow (&hParent);
  2558. ASSERT (SUCCEEDED (hr));
  2559. if ( SUCCEEDED (hr) )
  2560. {
  2561. CWnd parentWnd;
  2562. VERIFY (parentWnd.Attach (hParent));
  2563. CFindDialog findDlg (&parentWnd,
  2564. pParentCookie->QueryNonNULLMachineName (),
  2565. m_szFileName,
  2566. this);
  2567. CThemeContextActivator activator;
  2568. INT_PTR iReturn = findDlg.DoModal ();
  2569. ASSERT (-1 != iReturn && IDABORT != iReturn);
  2570. if ( -1 == iReturn || IDABORT == iReturn )
  2571. hr = E_UNEXPECTED;
  2572. else
  2573. {
  2574. if ( findDlg.ConsoleRefreshRequired () )
  2575. {
  2576. hr = m_pConsole->UpdateAllViews (pDataObject, 0,
  2577. HINT_REFRESH_STORES);
  2578. }
  2579. }
  2580. parentWnd.Detach ();
  2581. }
  2582. }
  2583. break;
  2584. case CERTMGR_CERTIFICATE:
  2585. case CERTMGR_CRL:
  2586. case CERTMGR_CTL:
  2587. case CERTMGR_AUTO_CERT_REQUEST:
  2588. ASSERT (0);
  2589. hr = E_UNEXPECTED;
  2590. break;
  2591. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  2592. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  2593. ASSERT (0);
  2594. break;
  2595. case CERTMGR_CRL_CONTAINER:
  2596. case CERTMGR_CTL_CONTAINER:
  2597. case CERTMGR_CERT_CONTAINER:
  2598. break;
  2599. default:
  2600. ASSERT (0);
  2601. hr = E_UNEXPECTED;
  2602. }
  2603. }
  2604. _TRACE (-1, L"Leaving CCertMgrComponentData::OnFind: 0x%x\n", hr);
  2605. return hr;
  2606. }
  2607. HRESULT CCertMgrComponentData::OnChangeComputer (LPDATAOBJECT pDataObject)
  2608. {
  2609. if ( !pDataObject )
  2610. return E_POINTER;
  2611. _TRACE (1, L"Entering CCertMgrComponentData::OnChangeComputer\n");
  2612. HRESULT hr = S_OK;
  2613. ASSERT (pDataObject);
  2614. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  2615. ASSERT (pDataObject);
  2616. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  2617. if ( pParentCookie && CERTMGR_SNAPIN == pParentCookie->m_objecttype )
  2618. {
  2619. HWND hWndParent = NULL;
  2620. hr = m_pConsole->GetMainWindow (&hWndParent);
  2621. CString machineName;
  2622. hr = ComputerNameFromObjectPicker (hWndParent, machineName);
  2623. if ( S_OK == hr ) // S_FALSE means user pressed "Cancel"
  2624. {
  2625. machineName.MakeUpper ();
  2626. // added IsLocalComputername 1/27/99 JonN
  2627. // If the user chooses the local computer, treat that as if they had chosen
  2628. // "Local Computer" in Snapin Manager. This means that there is no way to
  2629. // reset the snapin to target explicitly at this computer without either
  2630. // reloading the snapin from Snapin Manager, or going to a different computer.
  2631. // When the Choose Target Computer UI is revised, we can make this more
  2632. // consistent with Snapin Manager.
  2633. if ( IsLocalComputername( machineName ) )
  2634. machineName = L"";
  2635. QueryRootCookie().SetMachineName (machineName);
  2636. // Set the persistent name. If we are managing the local computer
  2637. // this name should be empty.
  2638. m_strMachineNamePersist = machineName;
  2639. hr = ChangeRootNodeName (machineName);
  2640. if ( SUCCEEDED(hr) )
  2641. {
  2642. hr = m_pConsole->UpdateAllViews (pDataObject, 0, HINT_CHANGE_COMPUTER);
  2643. }
  2644. }
  2645. }
  2646. else
  2647. hr = E_UNEXPECTED;
  2648. _TRACE (-1, L"Leaving CCertMgrComponentData::OnChangeComputer: 0x%x\n", hr);
  2649. return hr;
  2650. }
  2651. HRESULT CCertMgrComponentData::IsUserAdministrator (BOOL & bIsAdministrator)
  2652. {
  2653. _TRACE (1, L"Entering CCertMgrComponentData::IsUserAdministrator\n");
  2654. HRESULT hr = S_OK;
  2655. DWORD dwErr = 0;
  2656. bIsAdministrator = FALSE;
  2657. if ( IsWindowsNT () )
  2658. {
  2659. PSID psidAdministrators;
  2660. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  2661. BOOL bResult = AllocateAndInitializeSid (&siaNtAuthority, 2,
  2662. SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  2663. 0, 0, 0, 0, 0, 0, &psidAdministrators);
  2664. if ( bResult )
  2665. {
  2666. bResult = CheckTokenMembership (0, psidAdministrators,
  2667. &bIsAdministrator);
  2668. ASSERT (bResult);
  2669. if ( !bResult )
  2670. {
  2671. dwErr = GetLastError ();
  2672. DisplaySystemError (dwErr);
  2673. hr = HRESULT_FROM_WIN32 (dwErr);
  2674. }
  2675. FreeSid (psidAdministrators);
  2676. }
  2677. else
  2678. {
  2679. dwErr = GetLastError ();
  2680. DisplaySystemError (dwErr);
  2681. hr = HRESULT_FROM_WIN32 (dwErr);
  2682. }
  2683. }
  2684. _TRACE (-1, L"Leaving CCertMgrComponentData::IsUserAdministrator: 0x%x\n", hr);
  2685. return hr;
  2686. }
  2687. void CCertMgrComponentData::DisplaySystemError (DWORD dwErr)
  2688. {
  2689. _TRACE (1, L"Entering CCertMgrComponentData::DisplaySystemError\n");
  2690. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  2691. LPVOID lpMsgBuf;
  2692. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  2693. NULL,
  2694. dwErr,
  2695. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  2696. (PWSTR) &lpMsgBuf, 0, NULL );
  2697. // Display the string.
  2698. CString caption;
  2699. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  2700. int iRetVal = 0;
  2701. if ( m_pConsole )
  2702. {
  2703. HRESULT hr = m_pConsole->MessageBox ( (PWSTR) lpMsgBuf, caption,
  2704. MB_ICONWARNING | MB_OK, &iRetVal);
  2705. ASSERT (SUCCEEDED (hr));
  2706. }
  2707. else
  2708. {
  2709. CThemeContextActivator activator;
  2710. ::MessageBox (NULL, (PWSTR) lpMsgBuf, caption, MB_ICONWARNING | MB_OK);
  2711. }
  2712. // Free the buffer.
  2713. LocalFree (lpMsgBuf);
  2714. _TRACE (-1, L"Leaving CCertMgrComponentData::DisplaySystemError\n");
  2715. }
  2716. CString CCertMgrComponentData::GetCommandLineFileName () const
  2717. {
  2718. _TRACE (0, L"Entering and leaving CCertMgrComponentData::GetCommandLineFileName\n");
  2719. return m_szFileName;
  2720. }
  2721. //
  2722. // GetManagedComputer ()
  2723. //
  2724. // Returns the name of the managed computer. If we are managing the local machine
  2725. // returns an empty string. (As required by a number of Crypt32 APIs
  2726. //
  2727. CString CCertMgrComponentData::GetManagedComputer () const
  2728. {
  2729. _TRACE (0, L"Entering and leaving CCertMgrComponentData::GetManagedComputer\n");
  2730. if ( m_szManagedComputer.CompareNoCase (m_szThisComputer) ) // !=
  2731. {
  2732. return m_szManagedComputer;
  2733. }
  2734. else
  2735. return _T ("");
  2736. }
  2737. CString CCertMgrComponentData::GetManagedService () const
  2738. {
  2739. _TRACE (0, L"Entering and leaving CCertMgrComponentData::GetManagedService\n");
  2740. return m_szManagedServicePersist;
  2741. }
  2742. DWORD CCertMgrComponentData::GetLocation () const
  2743. {
  2744. _TRACE (0, L"Entering and leaving CCertMgrComponentData::GetLocation\n");
  2745. return m_dwLocationPersist;
  2746. }
  2747. LPCONSOLENAMESPACE CCertMgrComponentData::GetConsoleNameSpace () const
  2748. {
  2749. _TRACE (0, L"Entering and leaving CCertMgrComponentData::GetConsoleNameSpace\n");
  2750. return m_pConsoleNameSpace;
  2751. }
  2752. CUsageCookie* CCertMgrComponentData::FindDuplicateUsage (HSCOPEITEM hParent, PCWSTR pszName)
  2753. {
  2754. _TRACE (1, L"Entering CCertMgrComponentData::FindDuplicateUsage\n");
  2755. CUsageCookie* pUsageCookie = 0;
  2756. MMC_COOKIE lCookie = 0;
  2757. HSCOPEITEM hChildItem = 0;
  2758. bool bFound = false;
  2759. HRESULT hr = m_pConsoleNameSpace->GetChildItem (hParent, &hChildItem, &lCookie);
  2760. ASSERT (SUCCEEDED (hr));
  2761. while ( hChildItem && SUCCEEDED (hr) )
  2762. {
  2763. pUsageCookie = reinterpret_cast <CUsageCookie*> (lCookie);
  2764. if ( !wcscoll (pszName, pUsageCookie->GetObjectName ()) )
  2765. {
  2766. bFound = true;
  2767. break;
  2768. }
  2769. hr = m_pConsoleNameSpace->GetNextItem (hChildItem, &hChildItem, &lCookie);
  2770. ASSERT (SUCCEEDED (hr));
  2771. }
  2772. _TRACE (-1, L"Leaving CCertMgrComponentData::FindDuplicateUsage\n");
  2773. if ( bFound )
  2774. return pUsageCookie;
  2775. else
  2776. return NULL;
  2777. }
  2778. bool CCertMgrComponentData::IsSecurityConfigurationEditorNodetype (const GUID& refguid) const
  2779. {
  2780. _TRACE (0, L"Entering and leaving CCertMgrComponentData::IsSecurityConfigurationEditorNodetype\n");
  2781. return ::IsEqualGUID (refguid, cNodetypeSceTemplate) ? true : false;
  2782. }
  2783. HRESULT CCertMgrComponentData::OnEnroll (LPDATAOBJECT pDataObject, bool bNewKey)
  2784. {
  2785. _TRACE (1, L"Entering CCertMgrComponentData::OnEnroll\n");
  2786. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  2787. ASSERT (pDataObject);
  2788. if ( !pDataObject )
  2789. return E_POINTER;
  2790. HRESULT hr = S_OK;
  2791. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  2792. if ( pParentCookie )
  2793. {
  2794. CCertStore* pStore = 0;
  2795. CCertificate* pCert = 0;
  2796. CCertStoreGPE* pGPEStore = 0;
  2797. bool bParentIsStoreOrContainer = false;
  2798. HSCOPEITEM hScopeItem = 0;
  2799. bool bEFSPolicyTurnedOn = false;
  2800. switch (pParentCookie->m_objecttype)
  2801. {
  2802. case CERTMGR_CERTIFICATE:
  2803. pCert = reinterpret_cast <CCertificate*> (pParentCookie);
  2804. ASSERT (pCert);
  2805. break;
  2806. case CERTMGR_PHYS_STORE:
  2807. case CERTMGR_LOG_STORE:
  2808. if ( !m_pGPEInformation ) // If we are not extending the GPE/SCE
  2809. {
  2810. hScopeItem = pParentCookie->m_hScopeItem; // = 0;
  2811. pStore = reinterpret_cast <CCertStore*> (pParentCookie);
  2812. ASSERT (pStore);
  2813. }
  2814. bParentIsStoreOrContainer = true;
  2815. break;
  2816. case CERTMGR_CERT_CONTAINER:
  2817. if ( !m_pGPEInformation ) // If we are not extending the GPE/SCE
  2818. {
  2819. CContainerCookie* pContainer = reinterpret_cast <CContainerCookie*> (pParentCookie);
  2820. ASSERT (pContainer);
  2821. if ( pContainer )
  2822. {
  2823. MMC_COOKIE lCookie = 0;
  2824. hr = m_pConsoleNameSpace->GetParentItem (
  2825. pContainer->m_hScopeItem, &hScopeItem, &lCookie);
  2826. ASSERT (SUCCEEDED (hr));
  2827. pStore = &pContainer->GetCertStore ();
  2828. }
  2829. }
  2830. bParentIsStoreOrContainer = true;
  2831. break;
  2832. case CERTMGR_USAGE:
  2833. break;
  2834. case CERTMGR_LOG_STORE_RSOP:
  2835. ASSERT (0);
  2836. return E_FAIL;
  2837. break;
  2838. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  2839. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  2840. ASSERT (0);
  2841. break;
  2842. case CERTMGR_LOG_STORE_GPE:
  2843. pGPEStore = reinterpret_cast <CCertStoreGPE*> (pParentCookie);
  2844. ASSERT (pGPEStore);
  2845. if ( pGPEStore )
  2846. {
  2847. if ( pGPEStore->IsNullEFSPolicy () )
  2848. {
  2849. pGPEStore->AllowEmptyEFSPolicy ();
  2850. bEFSPolicyTurnedOn = true;
  2851. }
  2852. }
  2853. else
  2854. return E_FAIL;
  2855. break;
  2856. default:
  2857. ASSERT (0);
  2858. return E_UNEXPECTED;
  2859. }
  2860. HWND hwndParent = 0;
  2861. hr = m_pConsole->GetMainWindow (&hwndParent);
  2862. ASSERT (SUCCEEDED (hr));
  2863. CRYPTUI_WIZ_CERT_REQUEST_PVK_CERT pvkCert;
  2864. CRYPTUI_WIZ_CERT_REQUEST_PVK_NEW pvkNew;
  2865. CRYPTUI_WIZ_CERT_REQUEST_INFO certRequestInfo;
  2866. CRYPT_KEY_PROV_INFO provInfo;
  2867. // For EFS Recovery Agent
  2868. CRYPTUI_WIZ_CERT_TYPE certType;
  2869. PWSTR rgwszCertType = wszCERTTYPE_EFS_RECOVERY;
  2870. ::ZeroMemory (&certRequestInfo, sizeof (certRequestInfo));
  2871. certRequestInfo.dwSize = sizeof (certRequestInfo);
  2872. certRequestInfo.dwPurpose = CRYPTUI_WIZ_CERT_ENROLL;
  2873. // User wants to manage user account
  2874. // pass in NULL to machine name and to account name
  2875. // User wants to manage local machine account
  2876. // pass in NULL for account name and result of ::GetComputerName ()
  2877. // to machine name
  2878. // User want to manage remote machine
  2879. // pass in NULL for account name and machine name for machineName
  2880. // User wants to manage remote account on remote machine
  2881. // pass in account name for accountName and machine name for machineName
  2882. switch (m_dwLocationPersist)
  2883. {
  2884. case CERT_SYSTEM_STORE_CURRENT_SERVICE:
  2885. case CERT_SYSTEM_STORE_SERVICES:
  2886. certRequestInfo.pwszMachineName = (PCWSTR) m_szManagedComputer;
  2887. certRequestInfo.pwszAccountName = (PCWSTR) m_szManagedServicePersist;
  2888. break;
  2889. case CERT_SYSTEM_STORE_CURRENT_USER:
  2890. certRequestInfo.pwszMachineName = NULL;
  2891. certRequestInfo.pwszAccountName = NULL;
  2892. break;
  2893. case CERT_SYSTEM_STORE_LOCAL_MACHINE:
  2894. certRequestInfo.pwszMachineName = (PCWSTR) m_szManagedComputer;
  2895. certRequestInfo.pwszAccountName = NULL;
  2896. break;
  2897. case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
  2898. case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
  2899. certRequestInfo.pwszMachineName = NULL;
  2900. certRequestInfo.pwszAccountName = NULL;
  2901. certRequestInfo.pwszDesStore = 0;
  2902. certRequestInfo.dwCertOpenStoreFlag = 0;
  2903. break;
  2904. default:
  2905. ASSERT (0);
  2906. return E_UNEXPECTED;
  2907. break;
  2908. }
  2909. if ( !pCert || bNewKey )
  2910. {
  2911. // Request a certificate with a new key
  2912. certRequestInfo.dwPvkChoice = CRYPTUI_WIZ_CERT_REQUEST_PVK_CHOICE_NEW;
  2913. ::ZeroMemory (&pvkNew, sizeof (pvkNew));
  2914. pvkNew.dwSize = sizeof (pvkNew);
  2915. certRequestInfo.pPvkNew = &pvkNew;
  2916. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == m_dwLocationPersist )
  2917. {
  2918. ::ZeroMemory (&provInfo, sizeof (provInfo));
  2919. provInfo.dwFlags = CRYPT_MACHINE_KEYSET;
  2920. pvkNew.pKeyProvInfo = &provInfo;
  2921. }
  2922. if ( pGPEStore && EFS_STORE == pGPEStore->GetStoreType () )
  2923. {
  2924. // This creates an Encryption Recovery Agent.
  2925. ::ZeroMemory (&certType, sizeof (CRYPTUI_WIZ_CERT_TYPE));
  2926. certType.dwSize = sizeof (CRYPTUI_WIZ_CERT_TYPE);
  2927. certType.cCertType = 1;
  2928. certType.rgwszCertType = &rgwszCertType;
  2929. certRequestInfo.dwCertChoice = CRYPTUI_WIZ_CERT_REQUEST_CERT_TYPE;
  2930. certRequestInfo.pCertType = &certType;
  2931. ::ZeroMemory (&provInfo, sizeof (provInfo));
  2932. provInfo.pwszProvName = MS_DEF_PROV_W;
  2933. provInfo.dwProvType = PROV_RSA_FULL;
  2934. pvkNew.pKeyProvInfo = &provInfo;
  2935. pvkNew.dwGenKeyFlags = CRYPT_EXPORTABLE;
  2936. }
  2937. }
  2938. else
  2939. {
  2940. // Request a certificate with the same key as an existing certificate
  2941. if ( IsLocalComputername (m_szManagedComputer) )
  2942. {
  2943. // Find out if the cert has a private key before continuing.
  2944. DWORD dwFlags = 0;
  2945. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == m_dwLocationPersist )
  2946. dwFlags = CRYPT_FIND_MACHINE_KEYSET_FLAG;
  2947. if ( !::CryptFindCertificateKeyProvInfo (
  2948. pCert->GetCertContext (), dwFlags, 0) )
  2949. {
  2950. CString text;
  2951. CString caption;
  2952. CThemeContextActivator activator;
  2953. VERIFY (text.LoadString (IDS_NO_PRIVATE_KEY));
  2954. VERIFY (caption.LoadString (IDS_REQUEST_CERT_SAME_KEY));
  2955. ::MessageBox (hwndParent, text, caption, MB_OK);
  2956. return hr;
  2957. }
  2958. }
  2959. certRequestInfo.dwPvkChoice = CRYPTUI_WIZ_CERT_REQUEST_PVK_CHOICE_CERT;
  2960. ::ZeroMemory (&pvkCert, sizeof (pvkCert));
  2961. pvkCert.dwSize = sizeof (pvkCert);
  2962. pvkCert.pCertContext = pCert->GetCertContext ();
  2963. certRequestInfo.pPvkCert = &pvkCert;
  2964. }
  2965. certRequestInfo.pwszCertDNName = NULL;
  2966. // Now that all the preliminaries are out of they way and the data is
  2967. // all set up, call the enrollment wizard.
  2968. DWORD status = 0;
  2969. PCCERT_CONTEXT pNewCertContext = 0;
  2970. BOOL bResult = FALSE;
  2971. CThemeContextActivator activator;
  2972. while (1)
  2973. {
  2974. bResult = ::CryptUIWizCertRequest (
  2975. bNewKey ? CRYPTUI_WIZ_CERT_REQUEST_REQUIRE_NEW_KEY : 0,
  2976. hwndParent, NULL,
  2977. &certRequestInfo, &pNewCertContext, &status);
  2978. DWORD dwErr = GetLastError ();
  2979. if ( !bResult && HRESULT_FROM_WIN32 (NTE_TOKEN_KEYSET_STORAGE_FULL) == dwErr )
  2980. {
  2981. // NTRAID# 299089 Enrollment Wizard: Should return some
  2982. // meaningful message when users fail to enroll/renew on a
  2983. // smart card
  2984. if ( !bNewKey )
  2985. break;
  2986. CString text;
  2987. CString caption;
  2988. int iRetVal = 0;
  2989. VERIFY (text.LoadString (IDS_SMARTCARD_FULL_REUSE_PRIVATE_KEY));
  2990. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  2991. hr = m_pConsole->MessageBox (text, caption,
  2992. MB_YESNO, &iRetVal);
  2993. ASSERT (SUCCEEDED (hr));
  2994. if ( IDYES == iRetVal )
  2995. {
  2996. bNewKey = false;
  2997. }
  2998. else
  2999. break;
  3000. }
  3001. else
  3002. break;
  3003. }
  3004. if ( bResult && (CRYPTUI_WIZ_CERT_REQUEST_STATUS_SUCCEEDED == status) && pNewCertContext )
  3005. {
  3006. if ( bEFSPolicyTurnedOn )
  3007. hr = m_pConsole->UpdateAllViews (pDataObject, 0, HINT_EFS_ADD_DEL_POLICY);
  3008. ASSERT (!(pStore && pGPEStore)); // these can't both be true
  3009. if ( pStore )
  3010. {
  3011. pStore->IncrementCertCount ();
  3012. pStore->SetDirty ();
  3013. pStore->Resync ();
  3014. }
  3015. else if ( pGPEStore )
  3016. {
  3017. pGPEStore->InvalidateCertCount ();
  3018. pGPEStore->SetDirty ();
  3019. pGPEStore->Resync ();
  3020. if ( EFS_STORE == pGPEStore->GetStoreType () )
  3021. {
  3022. hr = CompleteEFSRecoveryAgent (pGPEStore, pNewCertContext);
  3023. }
  3024. }
  3025. else if ( pCert && pCert->GetCertStore ())
  3026. {
  3027. pCert->GetCertStore ()->Resync ();
  3028. }
  3029. if ( !m_pGPEInformation ) // If we are not extending the GPE/SCE
  3030. {
  3031. if ( bParentIsStoreOrContainer )
  3032. {
  3033. ASSERT (hScopeItem);
  3034. ASSERT (pStore);
  3035. hr = CreateContainers (hScopeItem, *pStore);
  3036. if ( CERTMGR_CERT_CONTAINER == pParentCookie->m_objecttype )
  3037. {
  3038. // Add certificate to result pane
  3039. RESULTDATAITEM rdItem;
  3040. CCookie& rootCookie = QueryBaseRootCookie ();
  3041. ::ZeroMemory (&rdItem, sizeof (rdItem));
  3042. rdItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM | RDI_STATE;
  3043. rdItem.nImage = iIconCertificate;
  3044. rdItem.nCol = 0;
  3045. rdItem.nState = LVIS_SELECTED | LVIS_FOCUSED;
  3046. rdItem.str = MMC_TEXTCALLBACK;
  3047. PCCERT_CONTEXT pFoundCertContext =
  3048. pStore->FindCertificate (0, CERT_FIND_EXISTING,
  3049. (void*) pNewCertContext, NULL);
  3050. if ( pFoundCertContext )
  3051. {
  3052. pCert = new CCertificate (pFoundCertContext, pStore);
  3053. if ( pCert )
  3054. {
  3055. rootCookie.m_listResultCookieBlocks.AddHead (pCert);
  3056. rdItem.lParam = (LPARAM) pCert;
  3057. pCert->m_resultDataID = m_pResultData;
  3058. hr = m_pResultData->InsertItem (&rdItem);
  3059. if ( FAILED (hr) )
  3060. {
  3061. _TRACE (0, L"IResultData::InsertItem () failed: 0x%x\n", hr);
  3062. }
  3063. else
  3064. {
  3065. hr = DisplayCertificateCountByStore (
  3066. m_pComponentConsole, pStore, false);
  3067. }
  3068. }
  3069. else
  3070. hr = E_OUTOFMEMORY;
  3071. ::CertFreeCertificateContext (pFoundCertContext);
  3072. }
  3073. else
  3074. {
  3075. hr = HRESULT_FROM_WIN32 (GetLastError ());
  3076. }
  3077. ASSERT (SUCCEEDED (hr));
  3078. }
  3079. hr = DisplayCertificateCountByStore (m_pComponentConsole, pStore);
  3080. }
  3081. else
  3082. {
  3083. hr = m_pConsole->UpdateAllViews (pDataObject, 0,
  3084. HINT_CERT_ENROLLED_USAGE_MODE);
  3085. ASSERT (SUCCEEDED (hr));
  3086. }
  3087. }
  3088. else
  3089. {
  3090. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  3091. ASSERT (SUCCEEDED (hr));
  3092. hr = DisplayCertificateCountByStore (m_pComponentConsole, pGPEStore, true);
  3093. }
  3094. ::CertFreeCertificateContext (pNewCertContext);
  3095. }
  3096. else if ( bEFSPolicyTurnedOn )
  3097. {
  3098. // If we allowed policy creation just for this enrollment, but
  3099. // nothing was enrolled, go ahead and delete the policy.
  3100. hr = OnDeleteEFSPolicy (pDataObject, false);
  3101. }
  3102. }
  3103. else
  3104. hr = E_POINTER;
  3105. _TRACE (-1, L"Leaving CCertMgrComponentData::OnEnroll: 0x%x\n", hr);
  3106. return hr;
  3107. }
  3108. HRESULT RenewCertificate (
  3109. CCertificate* pCert,
  3110. bool bNewKey,
  3111. const CString& machineName,
  3112. DWORD dwLocation,
  3113. const CString& managedComputer,
  3114. const CString& managedService,
  3115. HWND hwndParent,
  3116. LPCONSOLE pConsole,
  3117. LPDATAOBJECT pDataObject)
  3118. {
  3119. HRESULT hr = S_OK;
  3120. if ( pCert )
  3121. {
  3122. CRYPTUI_WIZ_CERT_REQUEST_PVK_CERT pvkCert;
  3123. CRYPTUI_WIZ_CERT_REQUEST_PVK_NEW pvkNew;
  3124. CRYPTUI_WIZ_CERT_REQUEST_INFO certRequestInfo;
  3125. CRYPT_KEY_PROV_INFO provInfo;
  3126. ::ZeroMemory (&certRequestInfo, sizeof (certRequestInfo));
  3127. certRequestInfo.dwSize = sizeof (certRequestInfo);
  3128. certRequestInfo.dwPurpose = CRYPTUI_WIZ_CERT_RENEW;
  3129. // User wants to manage user account
  3130. // pass in NULL to machine name and to account name
  3131. // User wants to manage local machine account
  3132. // pass in NULL for account name and result of ::GetComputerName ()
  3133. // to machine name
  3134. // User want to manage remote machine
  3135. // pass in NULL for account name and machine name for machineName
  3136. // User wants to manage remote account on remote machine
  3137. // pass in account name for accountName and machine name for machineName
  3138. // TODO: Ensure that this is NULL if the local machine
  3139. BOOL bIsLocalMachine = IsLocalComputername (machineName);
  3140. switch (dwLocation)
  3141. {
  3142. case CERT_SYSTEM_STORE_CURRENT_SERVICE:
  3143. case CERT_SYSTEM_STORE_SERVICES:
  3144. certRequestInfo.pwszMachineName = (PCWSTR) managedComputer;
  3145. certRequestInfo.pwszAccountName = (PCWSTR) managedService;
  3146. break;
  3147. case CERT_SYSTEM_STORE_CURRENT_USER:
  3148. certRequestInfo.pwszMachineName = NULL;
  3149. certRequestInfo.pwszAccountName = NULL;
  3150. break;
  3151. case CERT_SYSTEM_STORE_LOCAL_MACHINE:
  3152. certRequestInfo.pwszMachineName = (PCWSTR) managedComputer;
  3153. certRequestInfo.pwszAccountName = NULL;
  3154. break;
  3155. default:
  3156. ASSERT (0);
  3157. return E_UNEXPECTED;
  3158. break;
  3159. }
  3160. certRequestInfo.pRenewCertContext = pCert->GetCertContext ();
  3161. if ( bNewKey )
  3162. {
  3163. certRequestInfo.dwPvkChoice = CRYPTUI_WIZ_CERT_REQUEST_PVK_CHOICE_NEW;
  3164. ::ZeroMemory (&pvkNew, sizeof (pvkNew));
  3165. pvkNew.dwSize = sizeof (pvkNew);
  3166. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == dwLocation )
  3167. {
  3168. ::ZeroMemory (&provInfo, sizeof (provInfo));
  3169. provInfo.dwFlags = CRYPT_MACHINE_KEYSET;
  3170. pvkNew.pKeyProvInfo = &provInfo;
  3171. }
  3172. certRequestInfo.pPvkNew = &pvkNew;
  3173. }
  3174. else
  3175. {
  3176. if ( bIsLocalMachine )
  3177. {
  3178. DWORD dwFlags = 0;
  3179. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == dwLocation )
  3180. dwFlags = CRYPT_FIND_MACHINE_KEYSET_FLAG;
  3181. if ( !::CryptFindCertificateKeyProvInfo (
  3182. pCert->GetCertContext (), dwFlags, 0) )
  3183. {
  3184. CString text;
  3185. CString caption;
  3186. CThemeContextActivator activator;
  3187. VERIFY (text.LoadString (IDS_NO_PRIVATE_KEY));
  3188. VERIFY (caption.LoadString (IDS_RENEW_CERT_SAME_KEY));
  3189. ::MessageBox (hwndParent, text, caption, MB_OK);
  3190. return hr;
  3191. }
  3192. }
  3193. certRequestInfo.dwPvkChoice = CRYPTUI_WIZ_CERT_REQUEST_PVK_CHOICE_CERT;
  3194. ::ZeroMemory (&pvkCert, sizeof (pvkCert));
  3195. pvkCert.dwSize = sizeof (pvkCert);
  3196. pvkCert.pCertContext = pCert->GetCertContext ();
  3197. certRequestInfo.pPvkCert = &pvkCert;
  3198. }
  3199. DWORD status = 0;
  3200. PCCERT_CONTEXT pNewCertContext = 0;
  3201. BOOL bResult = FALSE;
  3202. CThemeContextActivator activator;
  3203. while (1)
  3204. {
  3205. bResult = ::CryptUIWizCertRequest (
  3206. bNewKey ? CRYPTUI_WIZ_CERT_REQUEST_REQUIRE_NEW_KEY : 0,
  3207. hwndParent, NULL,
  3208. &certRequestInfo, &pNewCertContext, &status);
  3209. if ( !bResult && HRESULT_FROM_WIN32 (NTE_TOKEN_KEYSET_STORAGE_FULL) == GetLastError () )
  3210. {
  3211. // NTRAID# 299089 Enrollment Wizard: Should return some
  3212. // meaningful message when users fail to enroll/renew on a
  3213. // smart card
  3214. if ( !bNewKey )
  3215. break;
  3216. CString text;
  3217. CString caption;
  3218. int iRetVal = 0;
  3219. VERIFY (text.LoadString (IDS_SMARTCARD_FULL_REUSE_PRIVATE_KEY));
  3220. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  3221. if ( pConsole )
  3222. {
  3223. hr = pConsole->MessageBox (text, caption, MB_YESNO, &iRetVal);
  3224. ASSERT (SUCCEEDED (hr));
  3225. }
  3226. else
  3227. {
  3228. CThemeContextActivator activator;
  3229. iRetVal = ::MessageBox (hwndParent, text, caption, MB_OK);
  3230. }
  3231. if ( IDYES == iRetVal )
  3232. {
  3233. bNewKey = false;
  3234. }
  3235. else
  3236. break;
  3237. }
  3238. else
  3239. break;
  3240. }
  3241. if ( bResult && (CRYPTUI_WIZ_CERT_REQUEST_STATUS_SUCCEEDED == status) && pNewCertContext )
  3242. {
  3243. CCertStore* pStore = pCert->GetCertStore ();
  3244. if ( pStore )
  3245. {
  3246. pStore->SetDirty ();
  3247. pStore->Resync ();
  3248. if ( pConsole )
  3249. hr = pConsole->UpdateAllViews (pDataObject, 0, 0);
  3250. }
  3251. CertFreeCertificateContext (pNewCertContext);
  3252. ASSERT (SUCCEEDED (hr));
  3253. }
  3254. }
  3255. else
  3256. hr = E_POINTER;
  3257. return hr;
  3258. }
  3259. HRESULT CCertMgrComponentData::OnRenew (LPDATAOBJECT pDataObject, bool bNewKey)
  3260. {
  3261. _TRACE (1, L"Entering CCertMgrComponentData::OnRenew\n");
  3262. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3263. HRESULT hr = S_OK;
  3264. HWND hwndParent = 0;
  3265. VERIFY (SUCCEEDED (m_pConsole->GetMainWindow (&hwndParent)));
  3266. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  3267. if ( pParentCookie && CERTMGR_CERTIFICATE == pParentCookie->m_objecttype )
  3268. {
  3269. CCertificate* pCert = reinterpret_cast <CCertificate*> (pParentCookie);
  3270. ASSERT (pCert);
  3271. if ( pCert )
  3272. {
  3273. hr = RenewCertificate (pCert, bNewKey, m_strMachineNamePersist,
  3274. m_dwLocationPersist, m_szManagedComputer,
  3275. m_szManagedServicePersist, hwndParent, m_pConsole, pDataObject);
  3276. }
  3277. }
  3278. else
  3279. hr = E_POINTER;
  3280. _TRACE (-1, L"Leaving CCertMgrComponentData::OnRenew: 0x%x\n", hr);
  3281. return hr;
  3282. }
  3283. CCertMgrCookie* CCertMgrComponentData::ConvertCookie (LPDATAOBJECT pDataObject)
  3284. {
  3285. CCertMgrCookie* pParentCookie = 0;
  3286. CCookie* pBaseParentCookie = 0;
  3287. HRESULT hr = ExtractData (pDataObject,
  3288. CCertMgrDataObject::m_CFRawCookie,
  3289. &pBaseParentCookie,
  3290. sizeof (pBaseParentCookie) );
  3291. if ( SUCCEEDED (hr) )
  3292. {
  3293. pParentCookie = ActiveCookie (pBaseParentCookie);
  3294. ASSERT (pParentCookie);
  3295. }
  3296. return pParentCookie;
  3297. }
  3298. HRESULT CCertMgrComponentData::OnImport (LPDATAOBJECT pDataObject)
  3299. {
  3300. _TRACE (1, L"Entering CCertMgrComponentData::OnImport\n");
  3301. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3302. HRESULT hr = S_OK;
  3303. ASSERT (m_szFileName.IsEmpty ());
  3304. if ( !m_szFileName.IsEmpty () )
  3305. return E_UNEXPECTED;
  3306. ASSERT (pDataObject);
  3307. if ( !pDataObject )
  3308. return E_POINTER;
  3309. DWORD dwFlags = 0;
  3310. if ( CERT_SYSTEM_STORE_CURRENT_USER == m_dwLocationPersist )
  3311. {
  3312. // We're managing the user's certificate store
  3313. dwFlags |= CRYPTUI_WIZ_IMPORT_TO_CURRENTUSER;
  3314. }
  3315. else
  3316. {
  3317. // We're managing certificates on a machine
  3318. dwFlags |= CRYPTUI_WIZ_IMPORT_TO_LOCALMACHINE;
  3319. if ( !IsLocalComputername (m_szManagedComputer) )
  3320. {
  3321. // We're managing certificates on a remote machine
  3322. dwFlags |= CRYPTUI_WIZ_IMPORT_REMOTE_DEST_STORE;
  3323. }
  3324. }
  3325. HCERTSTORE hDestStore = 0;
  3326. CCertStore* pDestStore = 0;
  3327. HSCOPEITEM hScopeItem = 0;
  3328. int nOriginalCertCount = 0;
  3329. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  3330. if ( pParentCookie )
  3331. {
  3332. hScopeItem = pParentCookie->m_hScopeItem;
  3333. switch (pParentCookie->m_objecttype)
  3334. {
  3335. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  3336. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  3337. ASSERT (0);
  3338. break;
  3339. case CERTMGR_LOG_STORE_RSOP:
  3340. ASSERT (0);
  3341. return E_FAIL;
  3342. break;
  3343. case CERTMGR_LOG_STORE_GPE:
  3344. {
  3345. dwFlags |= CRYPTUI_WIZ_IMPORT_NO_CHANGE_DEST_STORE;
  3346. pDestStore = reinterpret_cast <CCertStore*> (pParentCookie);
  3347. ASSERT (pDestStore);
  3348. if ( pDestStore )
  3349. {
  3350. nOriginalCertCount = pDestStore->GetCertCount ();
  3351. hDestStore = pDestStore->GetStoreHandle ();
  3352. switch (pDestStore->GetStoreType ())
  3353. {
  3354. case ACRS_STORE:
  3355. break;
  3356. case TRUST_STORE:
  3357. dwFlags |= CRYPTUI_WIZ_IMPORT_ALLOW_CTL;
  3358. break;
  3359. case ROOT_STORE:
  3360. dwFlags |= CRYPTUI_WIZ_IMPORT_ALLOW_CERT;
  3361. break;
  3362. case EFS_STORE:
  3363. break;
  3364. default:
  3365. ASSERT (0);
  3366. break;
  3367. }
  3368. }
  3369. else
  3370. hr = E_UNEXPECTED;
  3371. }
  3372. break;
  3373. case CERTMGR_LOG_STORE:
  3374. case CERTMGR_PHYS_STORE:
  3375. {
  3376. pDestStore = reinterpret_cast <CCertStore*> (pParentCookie);
  3377. ASSERT (pDestStore);
  3378. if ( pDestStore )
  3379. {
  3380. nOriginalCertCount = pDestStore->GetCertCount ();
  3381. hDestStore = pDestStore->GetStoreHandle ();
  3382. }
  3383. else
  3384. hr = E_UNEXPECTED;
  3385. }
  3386. break;
  3387. case CERTMGR_CERT_CONTAINER:
  3388. case CERTMGR_CTL_CONTAINER:
  3389. {
  3390. CContainerCookie* pContainer = reinterpret_cast <CContainerCookie*> (pParentCookie);
  3391. ASSERT (pContainer);
  3392. if ( pContainer )
  3393. {
  3394. MMC_COOKIE lCookie = 0;
  3395. hr = m_pConsoleNameSpace->GetParentItem (
  3396. pContainer->m_hScopeItem, &hScopeItem, &lCookie);
  3397. ASSERT (SUCCEEDED (hr));
  3398. pDestStore = &pContainer->GetCertStore ();
  3399. nOriginalCertCount = pDestStore->GetCertCount ();
  3400. hDestStore = pDestStore->GetStoreHandle ();
  3401. }
  3402. else
  3403. hr = E_UNEXPECTED;
  3404. }
  3405. break;
  3406. case CERTMGR_USAGE:
  3407. pDestStore = 0;
  3408. hDestStore = 0;
  3409. break;
  3410. default:
  3411. ASSERT (0);
  3412. hr = E_UNEXPECTED;
  3413. }
  3414. }
  3415. else
  3416. hr = E_UNEXPECTED;
  3417. if ( SUCCEEDED (hr) )
  3418. {
  3419. HWND hwndParent = 0;
  3420. hr = m_pConsole->GetMainWindow (&hwndParent);
  3421. ASSERT (SUCCEEDED (hr));
  3422. // Now that all the data is set up and everything is in readiness,
  3423. // call the Import Wizard
  3424. CThemeContextActivator activator;
  3425. BOOL bResult = ::CryptUIWizImport (dwFlags, hwndParent, 0, NULL, hDestStore);
  3426. if ( bResult )
  3427. {
  3428. bool bWizardCancelled = false;
  3429. CCertStore* pStore = 0;
  3430. switch (pParentCookie->m_objecttype)
  3431. {
  3432. case CERTMGR_LOG_STORE_RSOP:
  3433. ASSERT (0);
  3434. return E_FAIL;
  3435. break;
  3436. case CERTMGR_LOG_STORE_GPE:
  3437. case CERTMGR_LOG_STORE:
  3438. case CERTMGR_PHYS_STORE:
  3439. {
  3440. pStore = reinterpret_cast <CCertStore*> (pParentCookie);
  3441. ASSERT (pStore);
  3442. if ( pStore )
  3443. {
  3444. pStore->InvalidateCertCount ();
  3445. if ( pStore->GetCertCount () != nOriginalCertCount )
  3446. {
  3447. pStore->SetDirty ();
  3448. hr = pStore->Commit ();
  3449. }
  3450. else
  3451. bWizardCancelled = true;
  3452. }
  3453. else
  3454. hr = E_UNEXPECTED;
  3455. }
  3456. break;
  3457. case CERTMGR_CERT_CONTAINER:
  3458. case CERTMGR_CTL_CONTAINER:
  3459. {
  3460. CContainerCookie* pContainer = reinterpret_cast <CContainerCookie*> (pParentCookie);
  3461. ASSERT (pContainer);
  3462. if ( pContainer )
  3463. {
  3464. pStore = &pContainer->GetCertStore ();
  3465. pStore->InvalidateCertCount ();
  3466. if ( pStore->GetCertCount () != nOriginalCertCount )
  3467. {
  3468. pStore->SetDirty ();
  3469. hr = pStore->Commit ();
  3470. }
  3471. else
  3472. bWizardCancelled = true;
  3473. }
  3474. else
  3475. hr = E_UNEXPECTED;
  3476. }
  3477. break;
  3478. case CERTMGR_USAGE:
  3479. break;
  3480. default:
  3481. ASSERT (0);
  3482. hr = E_UNEXPECTED;
  3483. }
  3484. if ( !bWizardCancelled )
  3485. {
  3486. if ( pStore )
  3487. {
  3488. if ( SUCCEEDED (hr) )
  3489. pStore->Resync ();
  3490. }
  3491. if ( CERTMGR_LOG_STORE == pParentCookie->m_objecttype ||
  3492. (CERTMGR_LOG_STORE == pParentCookie->m_objecttype && !m_bShowPhysicalStoresPersist) )
  3493. {
  3494. if ( pStore )
  3495. hr = CreateContainers (hScopeItem, *pStore);
  3496. }
  3497. hr = m_pConsole->UpdateAllViews (pDataObject, 0, HINT_IMPORT);
  3498. ASSERT (SUCCEEDED (hr));
  3499. }
  3500. }
  3501. if ( pDestStore )
  3502. pDestStore->Close ();
  3503. }
  3504. _TRACE (-1, L"Leaving CCertMgrComponentData::OnImport: 0x%x\n", hr);
  3505. return hr;
  3506. }
  3507. HRESULT CCertMgrComponentData::OnExport (LPDATAOBJECT pDataObject)
  3508. {
  3509. _TRACE (1, L"Entering CCertMgrComponentData::OnExport\n");
  3510. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3511. HRESULT hr = S_OK;
  3512. LPDATAOBJECT pMSDO = ExtractMultiSelect (pDataObject);
  3513. m_bMultipleObjectsSelected = false;
  3514. if ( pMSDO )
  3515. {
  3516. // Iterate through list of selected objects -
  3517. // Add them to a memory store
  3518. // Export to PFX file through wizard with new
  3519. // "export from store - certs only" flag
  3520. m_bMultipleObjectsSelected = true;
  3521. HCERTSTORE hCertStore = ::CertOpenStore (CERT_STORE_PROV_MEMORY,
  3522. 0, NULL, 0, NULL);
  3523. ASSERT (hCertStore);
  3524. if ( hCertStore )
  3525. {
  3526. CCertMgrCookie* pCookie = 0;
  3527. CCertMgrDataObject* pDO = reinterpret_cast <CCertMgrDataObject*>(pMSDO);
  3528. ASSERT (pDO);
  3529. if ( pDO )
  3530. {
  3531. BOOL bResult = FALSE;
  3532. pDO->Reset();
  3533. while (pDO->Next(1, reinterpret_cast<MMC_COOKIE*>(&pCookie), NULL) != S_FALSE)
  3534. {
  3535. ASSERT (CERTMGR_CERTIFICATE == pCookie->m_objecttype);
  3536. if ( CERTMGR_CERTIFICATE == pCookie->m_objecttype )
  3537. {
  3538. CCertificate* pCert = reinterpret_cast <CCertificate*> (pCookie);
  3539. ASSERT (pCert);
  3540. if ( pCert )
  3541. {
  3542. bResult = ::CertAddCertificateContextToStore (
  3543. hCertStore,
  3544. ::CertDuplicateCertificateContext (pCert->GetCertContext ()),
  3545. CERT_STORE_ADD_NEW, 0);
  3546. ASSERT (bResult);
  3547. if ( !bResult )
  3548. break;
  3549. }
  3550. }
  3551. }
  3552. // Call Export Wizard
  3553. CRYPTUI_WIZ_EXPORT_INFO cwi;
  3554. ::ZeroMemory (&cwi, sizeof (cwi));
  3555. cwi.dwSize = sizeof (cwi);
  3556. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY;
  3557. cwi.hCertStore = hCertStore;
  3558. HWND hwndParent = 0;
  3559. hr = m_pConsole->GetMainWindow (&hwndParent);
  3560. ASSERT (SUCCEEDED (hr));
  3561. CThemeContextActivator activator;
  3562. bResult = ::CryptUIWizExport (
  3563. 0,
  3564. hwndParent,
  3565. 0,
  3566. &cwi,
  3567. NULL);
  3568. VERIFY (::CertCloseStore (hCertStore, CERT_CLOSE_STORE_CHECK_FLAG));
  3569. }
  3570. else
  3571. hr = E_FAIL;
  3572. return hr;
  3573. }
  3574. else
  3575. {
  3576. _TRACE (0, L"CertOpenStore (%s) failed: 0x%x\n",
  3577. CERT_STORE_PROV_MEMORY, GetLastError ());
  3578. }
  3579. }
  3580. CRYPTUI_WIZ_EXPORT_INFO cwi;
  3581. CCertificate* pCert = 0;
  3582. CCRL* pCRL = 0;
  3583. CCTL* pCTL = 0;
  3584. CCertStore* pCertStore = 0;
  3585. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  3586. ASSERT (pCookie);
  3587. if ( !pCookie )
  3588. return E_UNEXPECTED;
  3589. ::ZeroMemory (&cwi, sizeof (cwi));
  3590. cwi.dwSize = sizeof (cwi);
  3591. switch (pCookie->m_objecttype)
  3592. {
  3593. case CERTMGR_CERTIFICATE:
  3594. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
  3595. pCert = reinterpret_cast <CCertificate*> (pCookie);
  3596. ASSERT (pCert);
  3597. if ( pCert )
  3598. cwi.pCertContext = pCert->GetCertContext ();
  3599. else
  3600. return E_UNEXPECTED;
  3601. break;
  3602. case CERTMGR_CRL:
  3603. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CRL_CONTEXT;
  3604. pCRL = reinterpret_cast <CCRL*> (pCookie);
  3605. ASSERT (pCRL);
  3606. if ( pCRL )
  3607. cwi.pCRLContext = pCRL->GetCRLContext ();
  3608. else
  3609. return E_UNEXPECTED;
  3610. break;
  3611. case CERTMGR_CTL:
  3612. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CTL_CONTEXT;
  3613. pCTL = reinterpret_cast <CCTL*> (pCookie);
  3614. ASSERT (pCTL);
  3615. if ( pCTL )
  3616. cwi.pCTLContext = pCTL->GetCTLContext ();
  3617. else
  3618. return E_UNEXPECTED;
  3619. break;
  3620. case CERTMGR_LOG_STORE:
  3621. case CERTMGR_PHYS_STORE:
  3622. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_STORE;
  3623. pCertStore = reinterpret_cast <CCertStore*> (pCookie);
  3624. ASSERT (pCertStore);
  3625. if ( pCertStore )
  3626. cwi.hCertStore = pCertStore->GetStoreHandle ();
  3627. else
  3628. return E_UNEXPECTED;
  3629. break;
  3630. default:
  3631. ASSERT (0);
  3632. return E_UNEXPECTED;
  3633. }
  3634. HWND hwndParent = 0;
  3635. hr = m_pConsole->GetMainWindow (&hwndParent);
  3636. ASSERT (SUCCEEDED (hr));
  3637. CThemeContextActivator activator;
  3638. ::CryptUIWizExport (
  3639. 0,
  3640. hwndParent,
  3641. 0,
  3642. &cwi,
  3643. NULL);
  3644. if ( pCertStore )
  3645. pCertStore->Close ();
  3646. _TRACE (-1, L"Leaving CCertMgrComponentData::OnExport: 0x%x\n", hr);
  3647. return hr;
  3648. }
  3649. HRESULT CCertMgrComponentData::OnNewCTL (LPDATAOBJECT pDataObject)
  3650. {
  3651. _TRACE (1, L"Entering CCertMgrComponentData::OnNewCTL\n");
  3652. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3653. ASSERT (pDataObject);
  3654. if ( !pDataObject )
  3655. return E_POINTER;
  3656. HRESULT hr = S_OK;
  3657. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  3658. ASSERT (pCookie);
  3659. if ( !pCookie )
  3660. return E_UNEXPECTED;
  3661. CCertStore* pStore = 0;
  3662. CContainerCookie* pCont = 0;
  3663. switch ( pCookie->m_objecttype )
  3664. {
  3665. case CERTMGR_CTL_CONTAINER:
  3666. {
  3667. pCont = reinterpret_cast <CContainerCookie*> (pCookie);
  3668. ASSERT (pCont);
  3669. if ( pCont )
  3670. {
  3671. pStore = &(pCont->GetCertStore ());
  3672. }
  3673. else
  3674. return E_UNEXPECTED;
  3675. }
  3676. break;
  3677. case CERTMGR_LOG_STORE_RSOP:
  3678. ASSERT (0);
  3679. return E_UNEXPECTED;
  3680. break;
  3681. case CERTMGR_LOG_STORE:
  3682. case CERTMGR_LOG_STORE_GPE:
  3683. case CERTMGR_PHYS_STORE:
  3684. {
  3685. pStore = reinterpret_cast <CCertStore*> (pCookie);
  3686. ASSERT (pStore);
  3687. if ( !pStore )
  3688. return E_UNEXPECTED;
  3689. }
  3690. break;
  3691. default:
  3692. ASSERT (0);
  3693. return E_UNEXPECTED;
  3694. }
  3695. ASSERT (pStore);
  3696. if ( !pStore )
  3697. return E_UNEXPECTED;
  3698. pStore->Lock ();
  3699. CRYPTUI_WIZ_BUILDCTL_DEST_INFO destInfo;
  3700. ::ZeroMemory (&destInfo, sizeof (destInfo));
  3701. destInfo.dwSize = sizeof (destInfo);
  3702. destInfo.dwDestinationChoice = CRYPTUI_WIZ_BUILDCTL_DEST_CERT_STORE;
  3703. destInfo.hCertStore = pStore->GetStoreHandle ();
  3704. ASSERT (destInfo.hCertStore);
  3705. if ( !destInfo.hCertStore )
  3706. return E_UNEXPECTED;
  3707. HWND hwndParent = 0;
  3708. hr = m_pConsole->GetMainWindow (&hwndParent);
  3709. ASSERT (SUCCEEDED (hr));
  3710. PCCTL_CONTEXT pCTLContext = 0;
  3711. CThemeContextActivator activator;
  3712. BOOL bResult = ::CryptUIWizBuildCTL (
  3713. CRYPTUI_WIZ_BUILDCTL_SKIP_DESTINATION,
  3714. hwndParent,
  3715. 0,
  3716. NULL,
  3717. &destInfo,
  3718. &pCTLContext);
  3719. if ( bResult )
  3720. {
  3721. // If pCTLContext, then the wizard completed
  3722. if ( pCTLContext )
  3723. {
  3724. pStore->SetDirty ();
  3725. pStore->Commit ();
  3726. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  3727. ASSERT (SUCCEEDED (hr));
  3728. ::CertFreeCTLContext (pCTLContext);
  3729. }
  3730. }
  3731. pStore->Unlock ();
  3732. pStore->Close ();
  3733. _TRACE (-1, L"Leaving CCertMgrComponentData::OnNewCTL: 0x%x\n", hr);
  3734. return hr;
  3735. }
  3736. HRESULT CCertMgrComponentData::OnACRSEdit (LPDATAOBJECT pDataObject)
  3737. {
  3738. _TRACE (1, L"Entering CCertMgrComponentData::OnACRSEdit\n");
  3739. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3740. HRESULT hr = S_OK;
  3741. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  3742. ASSERT (pCookie);
  3743. if ( !pCookie )
  3744. return E_UNEXPECTED;
  3745. switch ( pCookie->m_objecttype )
  3746. {
  3747. case CERTMGR_AUTO_CERT_REQUEST:
  3748. {
  3749. CAutoCertRequest* pACR = reinterpret_cast <CAutoCertRequest*> (pCookie);
  3750. ASSERT (pACR);
  3751. if ( pACR )
  3752. {
  3753. CCertStore& rStore = pACR->GetCertStore ();
  3754. CCertStoreGPE* pStore = reinterpret_cast <CCertStoreGPE*> (&rStore);
  3755. ASSERT (pStore);
  3756. if ( pStore )
  3757. {
  3758. HWND hwndConsole = 0;
  3759. hr = m_pConsole->GetMainWindow (&hwndConsole);
  3760. ASSERT (SUCCEEDED (hr));
  3761. if ( SUCCEEDED (hr) )
  3762. {
  3763. ACRSWizardPropertySheet sheet (pStore, pACR);
  3764. ACRSWizardWelcomePage welcomePage;
  3765. ACRSWizardTypePage typePage;
  3766. ACRSCompletionPage completionPage;
  3767. sheet.AddPage (&welcomePage);
  3768. sheet.AddPage (&typePage);
  3769. sheet.AddPage (&completionPage);
  3770. if ( sheet.DoWizard (hwndConsole) )
  3771. {
  3772. pStore->SetDirty ();
  3773. hr = DeleteCTLFromResultPane (pACR, pDataObject);
  3774. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  3775. ASSERT (SUCCEEDED (hr));
  3776. }
  3777. }
  3778. }
  3779. else
  3780. hr = E_UNEXPECTED;
  3781. }
  3782. else
  3783. return E_UNEXPECTED;
  3784. }
  3785. break;
  3786. default:
  3787. ASSERT (0);
  3788. return E_UNEXPECTED;
  3789. }
  3790. _TRACE (-1, L"Leaving CCertMgrComponentData::OnACRSEdit: 0x%x\n", hr);
  3791. return hr;
  3792. }
  3793. HRESULT CCertMgrComponentData::OnCTLEdit (LPDATAOBJECT pDataObject)
  3794. {
  3795. _TRACE (1, L"Entering CCertMgrComponentData::OnCTLEdit\n");
  3796. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3797. ASSERT (pDataObject);
  3798. if ( !pDataObject )
  3799. return E_POINTER;
  3800. HRESULT hr = S_OK;
  3801. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  3802. ASSERT (pCookie);
  3803. if ( !pCookie )
  3804. return E_UNEXPECTED;
  3805. switch ( pCookie->m_objecttype )
  3806. {
  3807. case CERTMGR_CTL:
  3808. {
  3809. CCTL* pCTL = reinterpret_cast <CCTL*> (pCookie);
  3810. ASSERT (pCTL);
  3811. if ( pCTL )
  3812. {
  3813. CCertStore& rStore = pCTL->GetCertStore ();
  3814. CRYPTUI_WIZ_BUILDCTL_SRC_INFO srcInfo;
  3815. ::ZeroMemory (&srcInfo, sizeof (srcInfo));
  3816. srcInfo.dwSize = sizeof (srcInfo);
  3817. srcInfo.pCTLContext = pCTL->GetCTLContext ();
  3818. srcInfo.dwSourceChoice = CRYPTUI_WIZ_BUILDCTL_SRC_EXISTING_CTL;
  3819. HWND hwndParent = 0;
  3820. hr = m_pConsole->GetMainWindow (&hwndParent);
  3821. ASSERT (SUCCEEDED (hr));
  3822. PCCTL_CONTEXT pNewCTLContext = 0;
  3823. CThemeContextActivator activator;
  3824. BOOL bResult = ::CryptUIWizBuildCTL (
  3825. CRYPTUI_WIZ_BUILDCTL_SKIP_DESTINATION,
  3826. hwndParent,
  3827. 0,
  3828. &srcInfo,
  3829. NULL,
  3830. &pNewCTLContext);
  3831. ASSERT (bResult);
  3832. if ( bResult && pNewCTLContext )
  3833. {
  3834. rStore.SetDirty ();
  3835. // Delete old CTL and add the new one.
  3836. if ( pCTL->DeleteFromStore () )
  3837. {
  3838. if ( !rStore.AddCTLContext (pNewCTLContext) )
  3839. {
  3840. DWORD dwErr = GetLastError ();
  3841. if ( CRYPT_E_EXISTS == dwErr )
  3842. {
  3843. CString text;
  3844. CString caption;
  3845. int iRetVal = 0;
  3846. VERIFY (text.LoadString (IDS_DUPLICATE_CTL));
  3847. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  3848. hr = m_pConsole->MessageBox (text, caption,
  3849. MB_OK, &iRetVal);
  3850. ASSERT (SUCCEEDED (hr));
  3851. hr = E_FAIL;
  3852. }
  3853. else
  3854. {
  3855. DisplaySystemError (dwErr);
  3856. hr = HRESULT_FROM_WIN32 (dwErr);
  3857. }
  3858. }
  3859. rStore.Commit ();
  3860. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  3861. ASSERT (SUCCEEDED (hr));
  3862. }
  3863. else
  3864. {
  3865. DWORD dwErr = GetLastError ();
  3866. DisplaySystemError (dwErr);
  3867. hr = HRESULT_FROM_WIN32 (dwErr);
  3868. }
  3869. }
  3870. }
  3871. else
  3872. return E_UNEXPECTED;
  3873. }
  3874. break;
  3875. default:
  3876. ASSERT (0);
  3877. return E_UNEXPECTED;
  3878. }
  3879. _TRACE (-1, L"Leaving CCertMgrComponentData::OnCTLEdit: 0x%x\n", hr);
  3880. return hr;
  3881. }
  3882. HRESULT CCertMgrComponentData::OnAddDomainEncryptedDataRecoveryAgent (LPDATAOBJECT pDataObject)
  3883. {
  3884. _TRACE (1, L"Entering CCertMgrComponentData::OnAddDomainEncryptedDataRecoveryAgent\n");
  3885. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  3886. ASSERT (pDataObject);
  3887. if ( !pDataObject )
  3888. return E_POINTER;
  3889. HRESULT hr = S_OK;
  3890. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  3891. ASSERT (pCookie);
  3892. if ( pCookie )
  3893. {
  3894. CCertStoreGPE* pStore = reinterpret_cast <CCertStoreGPE*> (pCookie);
  3895. ASSERT (pStore && EFS_STORE == pStore->GetStoreType ());
  3896. if ( pStore && EFS_STORE == pStore->GetStoreType () )
  3897. {
  3898. HWND hwndConsole = 0;
  3899. hr = m_pConsole->GetMainWindow (&hwndConsole);
  3900. ASSERT (SUCCEEDED (hr));
  3901. if ( SUCCEEDED (hr) )
  3902. {
  3903. CUsers EFSUsers;
  3904. CAddEFSWizSheet efsAddSheet (IDS_ADDTITLE, EFSUsers, m_bMachineIsStandAlone);
  3905. if ( efsAddSheet.DoWizard (hwndConsole) )
  3906. {
  3907. pStore->SetDirty ();
  3908. CString szUserName;
  3909. CString szCertName;
  3910. PUSERSONFILE pUser = EFSUsers.StartEnum ();
  3911. if ( pStore->IsNullEFSPolicy () )
  3912. {
  3913. pStore->AllowEmptyEFSPolicy ();
  3914. hr = m_pConsole->UpdateAllViews (pDataObject, 0, HINT_EFS_ADD_DEL_POLICY);
  3915. ASSERT (SUCCEEDED (hr));
  3916. }
  3917. // If the store is an empty store, we need to delete it before adding
  3918. // the first cert. Otherwise CertAddCertificateContextToStore () fails
  3919. // with E_ACCESS_DENIED
  3920. if ( 0 == pStore->GetCertCount () )
  3921. pStore->DeleteEFSPolicy (false);
  3922. while ( pUser )
  3923. {
  3924. hr = pStore->AddCertificateContext (
  3925. pUser->m_pCertContext, m_pConsole, false);
  3926. if ( SUCCEEDED (hr) )
  3927. {
  3928. pStore->AddCertToList (pUser->m_pCertContext, pUser->m_UserSid);
  3929. hr = AddCertChainToPolicy (pUser->m_pCertContext);
  3930. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  3931. ASSERT (SUCCEEDED (hr));
  3932. }
  3933. else
  3934. break;
  3935. pUser = EFSUsers.GetNextUser (pUser, szUserName, szCertName);
  3936. }
  3937. pStore->Commit ();
  3938. hr = DisplayCertificateCountByStore (m_pComponentConsole, pStore, true);
  3939. }
  3940. }
  3941. }
  3942. else
  3943. hr = E_UNEXPECTED;
  3944. }
  3945. _TRACE (-1, L"Leaving CCertMgrComponentData::OnAddDomainEncryptedDataRecoveryAgent: 0x%x\n", hr);
  3946. return hr;
  3947. }
  3948. // This code from Robert Reichel
  3949. /*++
  3950. Routine Description:
  3951. This routine returns the TOKEN_USER structure for the
  3952. current user, and optionally, the AuthenticationId from his
  3953. token.
  3954. Arguments:
  3955. AuthenticationId - Supplies an optional pointer to return the
  3956. AuthenticationId.
  3957. Return Value:
  3958. On success, returns a pointer to a TOKEN_USER structure.
  3959. On failure, returns NULL. Call GetLastError() for more
  3960. detailed error information.
  3961. --*/
  3962. PTOKEN_USER EfspGetTokenUser ()
  3963. {
  3964. _TRACE (1, L"Entering EfspGetTokenUser\n");
  3965. HANDLE hToken = 0;
  3966. DWORD dwReturnLength = 0;
  3967. PTOKEN_USER pTokenUser = NULL;
  3968. BOOL bResult = ::OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hToken);
  3969. if ( bResult )
  3970. {
  3971. bResult = ::GetTokenInformation (
  3972. hToken,
  3973. TokenUser,
  3974. NULL,
  3975. 0,
  3976. &dwReturnLength
  3977. );
  3978. if ( !bResult && dwReturnLength > 0 )
  3979. {
  3980. pTokenUser = (PTOKEN_USER) malloc (dwReturnLength);
  3981. if (pTokenUser)
  3982. {
  3983. bResult = GetTokenInformation (
  3984. hToken,
  3985. TokenUser,
  3986. pTokenUser,
  3987. dwReturnLength,
  3988. &dwReturnLength
  3989. );
  3990. if ( !bResult)
  3991. {
  3992. DWORD dwErr = GetLastError ();
  3993. DisplaySystemError (NULL, dwErr);
  3994. free (pTokenUser);
  3995. pTokenUser = NULL;
  3996. }
  3997. }
  3998. }
  3999. else
  4000. {
  4001. DWORD dwErr = GetLastError ();
  4002. DisplaySystemError (NULL, dwErr);
  4003. }
  4004. ::CloseHandle (hToken);
  4005. }
  4006. else
  4007. {
  4008. DWORD dwErr = GetLastError ();
  4009. DisplaySystemError (NULL, dwErr);
  4010. }
  4011. _TRACE (-1, L"Leaving EfspGetTokenUser\n");
  4012. return pTokenUser;
  4013. }
  4014. HRESULT CCertMgrComponentData::CompleteEFSRecoveryAgent(CCertStoreGPE* pStore, PCCERT_CONTEXT pCertContext)
  4015. {
  4016. _TRACE (1, L"Entering CCertMgrComponentData::CompleteEFSRecoveryAgent\n");
  4017. HRESULT hr = S_OK;
  4018. ASSERT (pCertContext);
  4019. if ( !pCertContext || !pStore )
  4020. return E_POINTER;
  4021. // This is using to Enroll to create a new EFS Recovery Agent.
  4022. // Get PSID of logged-in user and save to store
  4023. // If the store is an empty store, we need to delete it before adding
  4024. // the first cert. Otherwise CertAddCertificateContextToStore () fails
  4025. // with E_ACCESS_DENIED
  4026. if ( 0 == pStore->GetCertCount () )
  4027. pStore->DeleteEFSPolicy (false);
  4028. hr = pStore->AddCertificateContext (pCertContext, m_pConsole, false);
  4029. if ( SUCCEEDED (hr) )
  4030. {
  4031. pStore->Commit ();
  4032. PTOKEN_USER pTokenUser = ::EfspGetTokenUser ();
  4033. if ( pTokenUser )
  4034. {
  4035. pStore->AddCertToList (pCertContext, pTokenUser->User.Sid);
  4036. free (pTokenUser);
  4037. }
  4038. if ( SUCCEEDED (hr) )
  4039. {
  4040. hr = AddCertChainToPolicy (pCertContext);
  4041. }
  4042. }
  4043. else
  4044. {
  4045. int iRetVal = 0;
  4046. CString text;
  4047. CString caption;
  4048. text.FormatMessage (IDS_CANT_ADD_CERT, pStore->GetLocalizedName (),
  4049. GetSystemMessage (hr));
  4050. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  4051. m_pConsole->MessageBox (text, caption,
  4052. MB_OK | MB_ICONWARNING, &iRetVal);
  4053. }
  4054. // Exportable keys are not currently supported. We can uncomment this code if this
  4055. // feature becomes available again in the future.
  4056. /*
  4057. int iRetVal = 0;
  4058. CString text;
  4059. CString caption;
  4060. VERIFY (text.LoadString (IDS_EXPORT_AND_DELETE_EFS_KEY));
  4061. VERIFY (caption.LoadString (IDS_CREATE_AUTO_CERT_REQUEST));
  4062. hr = m_pConsole->MessageBox (text, caption,
  4063. MB_YESNO | MB_ICONQUESTION, &iRetVal);
  4064. ASSERT (SUCCEEDED (hr));
  4065. if ( SUCCEEDED (hr) && IDYES == iRetVal )
  4066. {
  4067. // Remove the private key from the cert.
  4068. hr = CertSetCertificateContextProperty (pCertContext,
  4069. CERT_KEY_PROV_INFO_PROP_ID, 0, 0);
  4070. ASSERT (SUCCEEDED (hr));
  4071. // Bring up the common file open dialog to get a filename
  4072. // and the standard password dialog to get a password so
  4073. // that I can write out the PFX file.
  4074. HWND hwndConsole = 0;
  4075. hr = m_pConsole->GetMainWindow (&hwndConsole);
  4076. ASSERT (SUCCEEDED (hr));
  4077. if ( SUCCEEDED (hr) )
  4078. {
  4079. CString szFilter;
  4080. VERIFY (szFilter.LoadString (IDS_SAVE_PFX_FILTER));
  4081. CWnd mainWindow;
  4082. if ( mainWindow.Attach (hwndConsole) )
  4083. {
  4084. CFileDialog* pFileDlg = new CFileDialog (FALSE, // use as File Save As
  4085. L"pfx", // default extension
  4086. NULL, // preferred file name
  4087. OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_CREATEPROMPT | OFN_NOREADONLYRETURN,
  4088. (PCWSTR) szFilter,
  4089. &mainWindow);
  4090. if ( pFileDlg )
  4091. {
  4092. CThemeContextActivator activator;
  4093. if ( IDOK == pFileDlg->DoModal () )
  4094. {
  4095. CString pathName = pFileDlg->GetPathName ();
  4096. CRYPTUI_WIZ_EXPORT_INFO cwi;
  4097. CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO cci;
  4098. ::ZeroMemory (&cwi, sizeof (cwi));
  4099. cwi.dwSize = sizeof (cwi);
  4100. cwi.pwszExportFileName = (PCWSTR) pathName;
  4101. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
  4102. cwi.pCertContext = pCertContext;
  4103. ::ZeroMemory (&cci, sizeof (cci));
  4104. cci.dwSize = sizeof (cci);
  4105. cci.dwExportFormat = CRYPTUI_WIZ_EXPORT_FORMAT_PFX;
  4106. cci.fExportChain = TRUE;
  4107. cci.fExportPrivateKeys = TRUE;
  4108. CPassword passwordDlg;
  4109. // Launch password dialog
  4110. CThemeContextActivator activator;
  4111. if ( IDOK == passwordDlg.DoModal () )
  4112. {
  4113. if ( !wcslen (passwordDlg.GetPassword ()) )
  4114. {
  4115. // If password string is empty, pass NULL.
  4116. cci.pwszPassword = 0;
  4117. }
  4118. else
  4119. cci.pwszPassword = passwordDlg.GetPassword ();
  4120. CThemeContextActivator activator;
  4121. BOOL bResult = ::CryptUIWizExport (
  4122. CRYPTUI_WIZ_NO_UI,
  4123. 0, // hwndParent ignored
  4124. 0, // pwszWizardTitle ignored
  4125. &cwi,
  4126. (void*) &cci);
  4127. if ( bResult )
  4128. {
  4129. hr = DeleteKeyFromRSABASE (pCertContext);
  4130. }
  4131. else
  4132. {
  4133. DWORD dwErr = GetLastError ();
  4134. DisplaySystemError (dwErr);
  4135. }
  4136. }
  4137. }
  4138. delete pFileDlg;
  4139. }
  4140. else
  4141. {
  4142. hr = E_OUTOFMEMORY;
  4143. }
  4144. }
  4145. else
  4146. ASSERT (0);
  4147. VERIFY (mainWindow.Detach () == hwndConsole);
  4148. }
  4149. }
  4150. */
  4151. _TRACE (-1, L"Leaving CCertMgrComponentData::CompleteEFSRecoveryAgent: 0x%x\n", hr);
  4152. return hr;
  4153. }
  4154. HRESULT CCertMgrComponentData::AddScopeNode(CCertMgrCookie * pNewCookie, const CString & strServerName, HSCOPEITEM hParent)
  4155. {
  4156. _TRACE (1, L"Entering CCertMgrComponentData::AddScopeNode\n");
  4157. ASSERT (pNewCookie);
  4158. if ( !pNewCookie )
  4159. return E_POINTER;
  4160. HRESULT hr = S_OK;
  4161. SCOPEDATAITEM tSDItem;
  4162. ::ZeroMemory (&tSDItem,sizeof (tSDItem));
  4163. tSDItem.mask = SDI_STR | SDI_IMAGE | SDI_STATE | SDI_PARAM | SDI_PARENT;
  4164. tSDItem.displayname = MMC_CALLBACK;
  4165. tSDItem.relativeID = hParent;
  4166. tSDItem.nState = 0;
  4167. switch (pNewCookie->m_objecttype)
  4168. {
  4169. case CERTMGR_USAGE:
  4170. case CERTMGR_CRL_CONTAINER:
  4171. case CERTMGR_CTL_CONTAINER:
  4172. case CERTMGR_CERT_CONTAINER:
  4173. case CERTMGR_LOG_STORE_GPE:
  4174. case CERTMGR_LOG_STORE_RSOP:
  4175. case CERTMGR_SAFER_COMPUTER_LEVELS:
  4176. case CERTMGR_SAFER_USER_LEVELS:
  4177. case CERTMGR_SAFER_COMPUTER_ENTRIES:
  4178. case CERTMGR_SAFER_USER_ENTRIES:
  4179. tSDItem.mask |= SDI_CHILDREN;
  4180. tSDItem.cChildren = 0;
  4181. break;
  4182. case CERTMGR_PKP_AUTOENROLLMENT_COMPUTER_SETTINGS:
  4183. case CERTMGR_PKP_AUTOENROLLMENT_USER_SETTINGS:
  4184. ASSERT (0);
  4185. break;
  4186. default:
  4187. break;
  4188. }
  4189. if ( pNewCookie != m_pRootCookie && m_pRootCookie )
  4190. m_pRootCookie->m_listScopeCookieBlocks.AddHead ( (CBaseCookieBlock*) pNewCookie);
  4191. if ( !strServerName.IsEmpty () )
  4192. pNewCookie->m_strMachineName = strServerName;
  4193. tSDItem.lParam = reinterpret_cast<LPARAM> ( (CCookie*) pNewCookie);
  4194. tSDItem.nImage = QueryImage (*pNewCookie, FALSE);
  4195. hr = m_pConsoleNameSpace->InsertItem (&tSDItem);
  4196. if ( SUCCEEDED (hr) )
  4197. pNewCookie->m_hScopeItem = tSDItem.ID;
  4198. _TRACE (-1, L"Leaving CCertMgrComponentData::AddScopeNode: 0x%x\n", hr);
  4199. return hr;
  4200. }
  4201. HRESULT CCertMgrComponentData::DeleteKeyFromRSABASE(PCCERT_CONTEXT pCertContext)
  4202. {
  4203. _TRACE (1, L"Entering CCertMgrComponentData::DeleteKeyFromRSABASE\n");
  4204. ASSERT (pCertContext);
  4205. if ( !pCertContext )
  4206. return E_POINTER;
  4207. HRESULT hr = S_OK;
  4208. DWORD cbData = 0;
  4209. BOOL bResult = ::CertGetCertificateContextProperty (pCertContext,
  4210. CERT_KEY_PROV_INFO_PROP_ID, 0, &cbData);
  4211. ASSERT (bResult);
  4212. if ( bResult )
  4213. {
  4214. PCRYPT_KEY_PROV_INFO pKeyProvInfo = (PCRYPT_KEY_PROV_INFO) ::LocalAlloc (LPTR, cbData);
  4215. if ( pKeyProvInfo )
  4216. {
  4217. bResult = ::CertGetCertificateContextProperty (pCertContext,
  4218. CERT_KEY_PROV_INFO_PROP_ID, pKeyProvInfo, &cbData);
  4219. ASSERT (bResult);
  4220. if ( bResult )
  4221. {
  4222. HCRYPTPROV hProv = 0;
  4223. bResult = ::CryptAcquireContext (&hProv,
  4224. pKeyProvInfo->pwszContainerName,
  4225. pKeyProvInfo->pwszProvName,
  4226. pKeyProvInfo->dwProvType,
  4227. CRYPT_DELETEKEYSET);
  4228. ASSERT (bResult);
  4229. if ( !bResult )
  4230. {
  4231. DWORD dwErr = GetLastError ();
  4232. hr = HRESULT_FROM_WIN32 (dwErr);
  4233. DisplaySystemError (dwErr);
  4234. }
  4235. }
  4236. else
  4237. {
  4238. DWORD dwErr = GetLastError ();
  4239. hr = HRESULT_FROM_WIN32 (dwErr);
  4240. DisplaySystemError (dwErr);
  4241. }
  4242. ::LocalFree (pKeyProvInfo);
  4243. }
  4244. else
  4245. {
  4246. hr = E_OUTOFMEMORY;
  4247. }
  4248. }
  4249. else
  4250. {
  4251. DWORD dwErr = GetLastError ();
  4252. hr = HRESULT_FROM_WIN32 (dwErr);
  4253. DisplaySystemError (dwErr);
  4254. }
  4255. _TRACE (-1, L"Leaving CCertMgrComponentData::DeleteKeyFromRSABASE: 0x%x\n", hr);
  4256. return hr;
  4257. }
  4258. HRESULT CCertMgrComponentData::ReleaseResultCookie (
  4259. CBaseCookieBlock * pResultCookie,
  4260. CCookie& rootCookie,
  4261. HCERTSTORE hStoreHandle,
  4262. POSITION pos2)
  4263. {
  4264. // _TRACE (1, L"Entering CCertMgrComponentData::ReleaseResultCookie\n");
  4265. CCertMgrCookie* pCookie = reinterpret_cast <CCertMgrCookie*> (pResultCookie);
  4266. ASSERT (pCookie);
  4267. if ( pCookie )
  4268. {
  4269. switch (pCookie->m_objecttype)
  4270. {
  4271. case CERTMGR_CERTIFICATE:
  4272. {
  4273. CCertificate* pCert = reinterpret_cast <CCertificate*> (pCookie);
  4274. ASSERT (pCert);
  4275. if ( pCert && pCert->GetCertStore () )
  4276. {
  4277. if ( pCert->GetCertStore ()->GetStoreHandle () == hStoreHandle )
  4278. {
  4279. // pCookie and pCert point to the same object
  4280. pResultCookie = rootCookie.m_listResultCookieBlocks.GetAt (pos2);
  4281. ASSERT (pResultCookie);
  4282. rootCookie.m_listResultCookieBlocks.RemoveAt (pos2);
  4283. if ( pResultCookie )
  4284. {
  4285. pResultCookie->Release ();
  4286. }
  4287. }
  4288. pCert->GetCertStore ()->Close ();
  4289. }
  4290. }
  4291. break;
  4292. case CERTMGR_CTL:
  4293. case CERTMGR_AUTO_CERT_REQUEST:
  4294. {
  4295. CCTL* pCTL = reinterpret_cast <CCTL*> (pCookie);
  4296. ASSERT (pCTL);
  4297. if ( pCTL )
  4298. {
  4299. if ( pCTL->GetCertStore ().GetStoreHandle () == hStoreHandle )
  4300. {
  4301. // pCookie and pCert point to the same object
  4302. pResultCookie = rootCookie.m_listResultCookieBlocks.GetAt (pos2);
  4303. ASSERT (pResultCookie);
  4304. rootCookie.m_listResultCookieBlocks.RemoveAt (pos2);
  4305. if ( pResultCookie )
  4306. {
  4307. pResultCookie->Release ();
  4308. }
  4309. }
  4310. pCTL->GetCertStore ().Close ();
  4311. }
  4312. }
  4313. break;
  4314. case CERTMGR_CRL:
  4315. {
  4316. CCRL* pCRL = reinterpret_cast <CCRL*> (pCookie);
  4317. ASSERT (pCRL);
  4318. if ( pCRL )
  4319. {
  4320. if ( pCRL->GetCertStore ().GetStoreHandle () == hStoreHandle )
  4321. {
  4322. // pCookie and pCert point to the same object
  4323. pResultCookie = rootCookie.m_listResultCookieBlocks.GetAt (pos2);
  4324. ASSERT (pResultCookie);
  4325. rootCookie.m_listResultCookieBlocks.RemoveAt (pos2);
  4326. if ( pResultCookie )
  4327. {
  4328. pResultCookie->Release ();
  4329. }
  4330. }
  4331. pCRL->GetCertStore ().Close ();
  4332. }
  4333. }
  4334. break;
  4335. default:
  4336. // _TRACE (0, L"CCertMgrComponentData::ReleaseResultCookie () - bad cookie type: 0x%x\n",
  4337. // pCookie->m_objecttype);
  4338. break;
  4339. }
  4340. }
  4341. // _TRACE (-1, L"Leaving CCertMgrComponentData::ReleaseResultCookie: S_OK\n");
  4342. return S_OK;
  4343. }
  4344. void CCertMgrComponentData::SetResultData(LPRESULTDATA pResultData)
  4345. {
  4346. _TRACE (1, L"Entering CCertMgrComponentData::SetResultData\n");
  4347. ASSERT (pResultData);
  4348. if ( pResultData && !m_pResultData )
  4349. {
  4350. m_pResultData = pResultData;
  4351. m_pResultData->AddRef ();
  4352. }
  4353. _TRACE (-1, L"Leaving CCertMgrComponentData::SetResultData\n");
  4354. }
  4355. HRESULT CCertMgrComponentData::GetResultData(LPRESULTDATA* ppResultData)
  4356. {
  4357. HRESULT hr = S_OK;
  4358. if ( !ppResultData )
  4359. hr = E_POINTER;
  4360. else if ( !m_pResultData )
  4361. {
  4362. if ( m_pConsole )
  4363. {
  4364. hr = m_pConsole->QueryInterface(IID_PPV_ARG (IResultData, &m_pResultData));
  4365. _ASSERT (SUCCEEDED (hr));
  4366. }
  4367. else
  4368. hr = E_FAIL;
  4369. }
  4370. if ( SUCCEEDED (hr) && m_pResultData )
  4371. {
  4372. *ppResultData = m_pResultData;
  4373. m_pResultData->AddRef ();
  4374. }
  4375. return hr;
  4376. }
  4377. ///////////////////////////////////////////////////////////////////////////////
  4378. //
  4379. // Check to see if a child scope pane object exists of the desired type
  4380. // immediately below hParent
  4381. //
  4382. ///////////////////////////////////////////////////////////////////////////////
  4383. bool CCertMgrComponentData::ContainerExists(HSCOPEITEM hParent, CertificateManagerObjectType objectType)
  4384. {
  4385. _TRACE (1, L"Entering CCertMgrComponentData::ContainerExists\n");
  4386. bool bExists = false;
  4387. CCertMgrCookie* pCookie = 0;
  4388. HSCOPEITEM hChild = 0;
  4389. MMC_COOKIE lCookie = 0;
  4390. HRESULT hr = m_pConsoleNameSpace->GetChildItem (hParent,
  4391. &hChild, &lCookie);
  4392. ASSERT (SUCCEEDED (hr));
  4393. while ( SUCCEEDED (hr) && hChild )
  4394. {
  4395. pCookie = reinterpret_cast <CCertMgrCookie*> (lCookie);
  4396. ASSERT (pCookie);
  4397. if ( pCookie )
  4398. {
  4399. if ( pCookie->m_objecttype == objectType )
  4400. {
  4401. bExists = true;
  4402. break;
  4403. }
  4404. }
  4405. hr = m_pConsoleNameSpace->GetNextItem (hChild, &hChild, &lCookie);
  4406. ASSERT (SUCCEEDED (hr));
  4407. }
  4408. _TRACE (-1, L"Leaving CCertMgrComponentData::ContainerExists\n");
  4409. return bExists;
  4410. }
  4411. void CCertMgrComponentData::DisplayAccessDenied ()
  4412. {
  4413. _TRACE (1, L"Entering CCertMgrComponentData::DisplayAccessDenied\n");
  4414. DWORD dwErr = GetLastError ();
  4415. ASSERT (E_ACCESSDENIED == dwErr);
  4416. if ( E_ACCESSDENIED == dwErr )
  4417. {
  4418. LPVOID lpMsgBuf;
  4419. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  4420. NULL,
  4421. GetLastError (),
  4422. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  4423. (PWSTR) &lpMsgBuf, 0, NULL );
  4424. // Display the string.
  4425. CString caption;
  4426. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  4427. int iRetVal = 0;
  4428. VERIFY (SUCCEEDED (m_pConsole->MessageBox ( (PWSTR) lpMsgBuf, caption,
  4429. MB_ICONWARNING | MB_OK, &iRetVal)));
  4430. // Free the buffer.
  4431. LocalFree (lpMsgBuf);
  4432. }
  4433. _TRACE (-1, L"Leaving CCertMgrComponentData::DisplayAccessDenied\n");
  4434. }
  4435. HRESULT CCertMgrComponentData::DeleteCTLFromResultPane (CCTL * pCTL, LPDATAOBJECT pDataObject)
  4436. {
  4437. _TRACE (1, L"Entering CCertMgrComponentData::DeleteCTLFromResultPane\n");
  4438. ASSERT (pCTL);
  4439. ASSERT (pDataObject);
  4440. if ( !pCTL || !pDataObject )
  4441. return E_POINTER;
  4442. HRESULT hr = S_OK;
  4443. if ( pCTL->DeleteFromStore () )
  4444. {
  4445. hr = pCTL->GetCertStore ().Commit ();
  4446. ASSERT (SUCCEEDED (hr));
  4447. if ( SUCCEEDED (hr) )
  4448. {
  4449. HRESULTITEM itemID = 0;
  4450. hr = m_pResultData->FindItemByLParam ( (LPARAM) pCTL, &itemID);
  4451. if ( SUCCEEDED (hr) )
  4452. {
  4453. hr = m_pResultData->DeleteItem (itemID, 0);
  4454. ASSERT (SUCCEEDED (hr));
  4455. }
  4456. // If we can't succeed in removing this one item, then update the whole panel.
  4457. if ( !SUCCEEDED (hr) )
  4458. {
  4459. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  4460. }
  4461. }
  4462. }
  4463. else
  4464. {
  4465. DisplayAccessDenied ();
  4466. }
  4467. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::DeleteCTLFromResultPane: 0x%x\n", hr);
  4468. return hr;
  4469. }
  4470. /*
  4471. HRESULT CCertMgrComponentData::OnSetAsRecoveryCert(LPDATAOBJECT pDataObject)
  4472. {
  4473. HRESULT hr = S_OK;
  4474. CCertMgrCookie* pParentCookie = ConvertCookie (pDataObject);
  4475. if ( pParentCookie && CERTMGR_CERTIFICATE == pParentCookie->m_objecttype )
  4476. {
  4477. CCertificate* pCert = reinterpret_cast <CCertificate*> (pParentCookie);
  4478. ASSERT (pCert);
  4479. if ( pCert )
  4480. {
  4481. PTOKEN_USER pTokenUser = ::EfspGetTokenUser ();
  4482. if ( pTokenUser )
  4483. {
  4484. ASSERT (::CertHasEFSKeyUsage (pCert->GetCertContext ()));
  4485. PCCERT_CONTEXT pCertContext = pCert->GetCertContext ();
  4486. EFS_CERTIFICATE_BLOB efsCertBlob;
  4487. ::ZeroMemory (&efsCertBlob, sizeof (efsCertBlob));
  4488. efsCertBlob.dwCertEncodingType = pCertContext->dwCertEncodingType;
  4489. efsCertBlob.cbData = pCertContext->cbCertEncoded;
  4490. efsCertBlob.pbData = pCertContext->pbCertEncoded;
  4491. ENCRYPTION_CERTIFICATE encryptionCert;
  4492. ::ZeroMemory (&encryptionCert, sizeof (encryptionCert));
  4493. encryptionCert.cbTotalLength = sizeof (encryptionCert);
  4494. encryptionCert.pUserSid = (SID*) pTokenUser->User.Sid;
  4495. encryptionCert.pCertBlob = &efsCertBlob;
  4496. DWORD dwResult = ::SetUserFileEncryptionKey (&encryptionCert);
  4497. if ( dwResult )
  4498. {
  4499. // The most likely error here is that the user doesn't have the
  4500. // private key of this certificate. In that case, put up a special
  4501. // message box to that effect.
  4502. // TODO: Get the return code from RobertRe for this case. As of
  4503. // 3/4/98 he doesn't yet know what it is.
  4504. DisplaySystemError (dwResult);
  4505. }
  4506. }
  4507. else
  4508. hr = E_FAIL;
  4509. }
  4510. else
  4511. hr = E_UNEXPECTED;
  4512. }
  4513. else
  4514. hr = E_UNEXPECTED;
  4515. return hr;
  4516. }
  4517. */
  4518. CString CCertMgrComponentData::GetThisComputer() const
  4519. {
  4520. _TRACE (0, L"Entering and leaving CCertMgrComponentData::GetThisComputer\n");
  4521. return m_szThisComputer;
  4522. }
  4523. typedef struct _ENUM_LOG_ARG {
  4524. DWORD m_dwFlags;
  4525. CTypedPtrList<CPtrList, CCertStore*>* m_pStoreList;
  4526. PCWSTR m_pcszMachineName;
  4527. CCertMgrComponentData* m_pCompData;
  4528. LPCONSOLE m_pConsole;
  4529. } ENUM_LOG_ARG, *PENUM_LOG_ARG;
  4530. static BOOL WINAPI EnumLogCallback (
  4531. IN const void* pwszSystemStore,
  4532. IN DWORD dwFlags,
  4533. IN PCERT_SYSTEM_STORE_INFO /*pStoreInfo*/,
  4534. IN OPTIONAL void* /*pvReserved*/,
  4535. IN OPTIONAL void *pvArg
  4536. )
  4537. {
  4538. _TRACE (1, L"Entering EnumLogCallback\n");
  4539. BOOL bResult = TRUE;
  4540. PENUM_LOG_ARG pEnumArg = (PENUM_LOG_ARG) pvArg;
  4541. // Create new cookies
  4542. SPECIAL_STORE_TYPE storeType = GetSpecialStoreType ((PWSTR) pwszSystemStore);
  4543. //
  4544. // We will not expose the ACRS store for machines or users. It is not
  4545. // interesting or useful at this level. All Auto Cert Requests should
  4546. // be managed only at the policy level.
  4547. //
  4548. if ( ACRS_STORE != storeType )
  4549. {
  4550. if ( pEnumArg->m_pCompData->ShowArchivedCerts () )
  4551. dwFlags |= CERT_STORE_ENUM_ARCHIVED_FLAG;
  4552. CCertStore* pStore = new CCertStore (
  4553. CERTMGR_LOG_STORE,
  4554. CERT_STORE_PROV_SYSTEM,
  4555. dwFlags,
  4556. pEnumArg->m_pcszMachineName,
  4557. (PCWSTR) pwszSystemStore,
  4558. (PCWSTR) pwszSystemStore,
  4559. _T (""), storeType,
  4560. pEnumArg->m_dwFlags,
  4561. pEnumArg->m_pConsole);
  4562. if ( pStore )
  4563. {
  4564. pEnumArg->m_pStoreList->AddTail (pStore);
  4565. }
  4566. }
  4567. _TRACE (-1, L"Leaving EnumLogCallback\n");
  4568. return bResult;
  4569. }
  4570. HRESULT CCertMgrComponentData::EnumerateLogicalStores (CTypedPtrList<CPtrList, CCertStore*>* pStoreList)
  4571. {
  4572. _TRACE (1, L"Entering CCertMgrComponentData::EnumerateLogicalStores\n");
  4573. CWaitCursor cursor;
  4574. HRESULT hr = S_OK;
  4575. ENUM_LOG_ARG enumArg;
  4576. DWORD dwFlags = GetLocation ();
  4577. ::ZeroMemory (&enumArg, sizeof (enumArg));
  4578. enumArg.m_dwFlags = dwFlags;
  4579. enumArg.m_pStoreList = pStoreList;
  4580. enumArg.m_pcszMachineName =
  4581. QueryRootCookie ().QueryNonNULLMachineName ();
  4582. enumArg.m_pCompData = this;
  4583. enumArg.m_pConsole = m_pConsole;
  4584. CString location;
  4585. void* pvPara = 0;
  4586. if ( !GetManagedService ().IsEmpty () )
  4587. {
  4588. if ( !GetManagedComputer ().IsEmpty () )
  4589. {
  4590. location = GetManagedComputer () + _T("\\") +
  4591. GetManagedComputer ();
  4592. pvPara = (void *) (PCWSTR) location;
  4593. }
  4594. else
  4595. pvPara = (void *) (PCWSTR) GetManagedService ();
  4596. }
  4597. else if ( !GetManagedComputer ().IsEmpty () )
  4598. {
  4599. pvPara = (void *) (PCWSTR) GetManagedComputer ();
  4600. }
  4601. CString fileName = GetCommandLineFileName ();
  4602. if ( fileName.IsEmpty () )
  4603. {
  4604. // Ensure creation of MY store
  4605. HCERTSTORE hTempStore = ::CertOpenStore (CERT_STORE_PROV_SYSTEM,
  4606. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  4607. NULL,
  4608. dwFlags | CERT_STORE_SET_LOCALIZED_NAME_FLAG,
  4609. MY_SYSTEM_STORE_NAME);
  4610. if ( hTempStore ) // otherwise, store is read only
  4611. {
  4612. VERIFY (::CertCloseStore (hTempStore, CERT_CLOSE_STORE_CHECK_FLAG));
  4613. }
  4614. else
  4615. {
  4616. _TRACE (0, L"CertOpenStore (%s) failed: 0x%x\n",
  4617. MY_SYSTEM_STORE_NAME, GetLastError ());
  4618. }
  4619. if ( !::CertEnumSystemStore (dwFlags, pvPara, &enumArg, EnumLogCallback) )
  4620. {
  4621. DWORD dwErr = GetLastError ();
  4622. CString text;
  4623. CString caption;
  4624. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  4625. if ( ERROR_ACCESS_DENIED == dwErr )
  4626. {
  4627. VERIFY (text.LoadString (IDS_NO_PERMISSION));
  4628. }
  4629. else
  4630. {
  4631. text.FormatMessage (IDS_CANT_ENUMERATE_SYSTEM_STORES, GetSystemMessage (dwErr));
  4632. }
  4633. int iRetVal = 0;
  4634. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  4635. MB_OK, &iRetVal)));
  4636. hr = HRESULT_FROM_WIN32 (dwErr);
  4637. hr = HRESULT_FROM_WIN32 (dwErr);
  4638. }
  4639. }
  4640. else
  4641. {
  4642. // Create new cookies
  4643. dwFlags = 0;
  4644. // If there is a class file-based store, use that, otherwise allocate
  4645. // a new one.
  4646. CCertStore* pStore = m_pFileBasedStore;
  4647. if ( !pStore )
  4648. pStore = new CCertStore (
  4649. CERTMGR_LOG_STORE,
  4650. CERT_STORE_PROV_FILENAME_W,
  4651. dwFlags,
  4652. QueryRootCookie ().QueryNonNULLMachineName (),
  4653. fileName, fileName, _T (""), NO_SPECIAL_TYPE,
  4654. m_dwLocationPersist,
  4655. m_pConsole);
  4656. else
  4657. pStore->AddRef ();
  4658. if ( pStore )
  4659. {
  4660. pStoreList->AddTail (pStore);
  4661. }
  4662. }
  4663. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::EnumerateLogicalStores: 0x%x\n", hr);
  4664. return hr;
  4665. }
  4666. HRESULT CCertMgrComponentData::OnNotifyPreload(
  4667. LPDATAOBJECT /*lpDataObject*/,
  4668. HSCOPEITEM hRootScopeItem)
  4669. {
  4670. _TRACE (1, L"Entering CCertMgrComponentData::OnNotifyPreload\n");
  4671. ASSERT (m_fAllowOverrideMachineName);
  4672. HRESULT hr = S_OK;
  4673. QueryBaseRootCookie ().m_hScopeItem = hRootScopeItem;
  4674. // The machine name will be changed only if the stores are machine-based
  4675. // stores.
  4676. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == m_dwLocationPersist )
  4677. {
  4678. CString machineName = QueryRootCookie ().QueryNonNULLMachineName();
  4679. hr = ChangeRootNodeName (machineName);
  4680. }
  4681. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::OnNotifyPreload: 0x%x\n", hr);
  4682. return hr;
  4683. }
  4684. ///////////////////////////////////////////////////////////////////////////////
  4685. //
  4686. // ChangeRootNodeName ()
  4687. //
  4688. // Purpose: Change the text of the root node
  4689. //
  4690. // Input: newName - the new machine name that the snapin manages
  4691. // Output: Returns S_OK on success
  4692. //
  4693. ///////////////////////////////////////////////////////////////////////////////
  4694. HRESULT CCertMgrComponentData::ChangeRootNodeName(const CString & newName)
  4695. {
  4696. _TRACE (1, L"Entering CCertMgrComponentData::ChangeRootNodeName\n");
  4697. if ( !QueryBaseRootCookie ().m_hScopeItem )
  4698. {
  4699. if ( m_hRootScopeItem )
  4700. QueryBaseRootCookie ().m_hScopeItem = m_hRootScopeItem;
  4701. else
  4702. return E_UNEXPECTED;
  4703. }
  4704. CString formattedName;
  4705. switch (m_dwLocationPersist)
  4706. {
  4707. case CERT_SYSTEM_STORE_LOCAL_MACHINE:
  4708. {
  4709. CString machineName (newName);
  4710. // If machineName is empty, then this manages the local machine. Get
  4711. // the local machine name. Then format the computer name with the snapin
  4712. // name
  4713. if ( IsLocalComputername (machineName) )
  4714. {
  4715. formattedName.LoadString (IDS_SCOPE_SNAPIN_TITLE_LOCAL_MACHINE);
  4716. m_szManagedComputer = L"";
  4717. }
  4718. else
  4719. {
  4720. machineName.MakeUpper ();
  4721. formattedName.FormatMessage (IDS_SCOPE_SNAPIN_TITLE_MACHINE, machineName);
  4722. m_szManagedComputer = machineName;
  4723. }
  4724. }
  4725. break;
  4726. case CERT_SYSTEM_STORE_CURRENT_SERVICE:
  4727. case CERT_SYSTEM_STORE_SERVICES:
  4728. {
  4729. CString machineName (newName);
  4730. // If machineName is empty, then this manages the local machine. Get
  4731. // the local machine name. Then format the computer name with the snapin
  4732. // name
  4733. if ( IsLocalComputername (machineName) )
  4734. {
  4735. // Get this machine name and add it to the string.
  4736. formattedName.FormatMessage (IDS_SCOPE_SNAPIN_TITLE_SERVICE_LOCAL_MACHINE,
  4737. m_szManagedServiceDisplayName);
  4738. m_szManagedComputer = L"";
  4739. }
  4740. else
  4741. {
  4742. formattedName.FormatMessage (IDS_SCOPE_SNAPIN_TITLE_SERVICE,
  4743. m_szManagedServiceDisplayName, machineName);
  4744. m_szManagedComputer = machineName;
  4745. }
  4746. }
  4747. break;
  4748. case CERT_SYSTEM_STORE_CURRENT_USER:
  4749. VERIFY (formattedName.LoadString (IDS_SCOPE_SNAPIN_TITLE_USER));
  4750. break;
  4751. default:
  4752. return S_OK;
  4753. }
  4754. SCOPEDATAITEM item;
  4755. ::ZeroMemory (&item, sizeof (SCOPEDATAITEM));
  4756. item.mask = SDI_STR;
  4757. item.displayname = (PWSTR) (PCWSTR) formattedName;
  4758. item.ID = QueryBaseRootCookie ().m_hScopeItem;
  4759. HRESULT hr = m_pConsoleNameSpace->SetItem (&item);
  4760. if ( FAILED (hr) )
  4761. {
  4762. _TRACE (0, L"IConsoleNameSpace2::SetItem () failed: 0x%x\n", hr);
  4763. }
  4764. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::ChangeRootNodeName: 0x%x\n", hr);
  4765. return hr;
  4766. }
  4767. HRESULT CCertMgrComponentData::CreateContainers(
  4768. HSCOPEITEM hScopeItem,
  4769. CCertStore& rTargetStore)
  4770. {
  4771. _TRACE (1, L"Entering CCertMgrComponentData::CreateContainers\n");
  4772. HRESULT hr = S_OK;
  4773. // If the container was a cert store and it does not
  4774. // already have Certs/CRLs/CTLs, instantiate a new CRL container
  4775. // in the scope pane.
  4776. if ( -1 != hScopeItem )
  4777. {
  4778. SCOPEDATAITEM item;
  4779. ::ZeroMemory (&item, sizeof (item));
  4780. item.mask = SDI_STATE;
  4781. item.nState = 0;
  4782. item.ID = hScopeItem;
  4783. hr = m_pConsoleNameSpace->SetItem (&item);
  4784. if ( FAILED (hr) )
  4785. {
  4786. _TRACE (0, L"IConsoleNameSpace2::SetItem () failed: 0x%x\n", hr);
  4787. }
  4788. if ( CERTMGR_LOG_STORE_GPE != rTargetStore.m_objecttype &&
  4789. CERTMGR_LOG_STORE_RSOP != rTargetStore.m_objecttype)
  4790. {
  4791. AddContainersToScopePane (hScopeItem,
  4792. rTargetStore, true);
  4793. }
  4794. }
  4795. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::CreateContainers: 0x%x\n", hr);
  4796. return hr;
  4797. }
  4798. HRESULT CCertMgrComponentData::OnOptions(LPDATAOBJECT pDataObject)
  4799. {
  4800. _TRACE (1, L"Entering CCertMgrComponentData::OnOptions\n");
  4801. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  4802. HWND hParent = 0;
  4803. // Get parent window handle and attach to a CWnd object
  4804. HRESULT hr = m_pConsole->GetMainWindow (&hParent);
  4805. ASSERT (SUCCEEDED (hr));
  4806. if ( SUCCEEDED (hr) )
  4807. {
  4808. int activeView = m_activeViewPersist;
  4809. BOOL bShowPhysicalStores = m_bShowPhysicalStoresPersist;
  4810. BOOL bShowArchivedCerts = m_bShowArchivedCertsPersist;
  4811. CWnd parentWnd;
  4812. VERIFY (parentWnd.Attach (hParent));
  4813. CViewOptionsDlg optionsDlg (&parentWnd,
  4814. this);
  4815. CThemeContextActivator activator;
  4816. INT_PTR iReturn = optionsDlg.DoModal ();
  4817. ASSERT (-1 != iReturn);
  4818. if ( -1 == iReturn )
  4819. hr = (HRESULT)iReturn;
  4820. if ( IDOK == iReturn )
  4821. {
  4822. long hint = 0;
  4823. if ( activeView != m_activeViewPersist )
  4824. {
  4825. hint |= HINT_CHANGE_VIEW_TYPE;
  4826. if ( IDM_USAGE_VIEW == m_activeViewPersist )
  4827. {
  4828. // view by usage
  4829. ASSERT (m_pHeader);
  4830. if ( m_pHeader && GetObjectType (pDataObject) == CERTMGR_SNAPIN )
  4831. {
  4832. CString str;
  4833. VERIFY (str.LoadString (IDS_COLUMN_PURPOSE) );
  4834. hr = m_pHeader->SetColumnText (0,
  4835. const_cast<PWSTR> ( (PCWSTR) str));
  4836. }
  4837. }
  4838. else
  4839. {
  4840. // View by stores
  4841. ASSERT (m_pHeader);
  4842. if ( m_pHeader && GetObjectType (pDataObject) == CERTMGR_SNAPIN )
  4843. {
  4844. CString str;
  4845. VERIFY (str.LoadString (IDS_COLUMN_LOG_CERTIFICATE_STORE));
  4846. hr = m_pHeader->SetColumnText (0,
  4847. const_cast<PWSTR> ( (PCWSTR) str));
  4848. }
  4849. }
  4850. }
  4851. if ( bShowPhysicalStores != m_bShowPhysicalStoresPersist )
  4852. hint |= HINT_CHANGE_STORE_TYPE;
  4853. if ( bShowArchivedCerts != m_bShowArchivedCertsPersist )
  4854. hint |= HINT_SHOW_ARCHIVE_CERTS;
  4855. if ( hint )
  4856. {
  4857. hr = m_pConsole->UpdateAllViews (pDataObject, 0, hint);
  4858. ASSERT (SUCCEEDED (hr));
  4859. m_fDirty = TRUE;
  4860. }
  4861. }
  4862. parentWnd.Detach ();
  4863. }
  4864. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::OnOptions: 0x%x\n", hr);
  4865. return hr;
  4866. }
  4867. bool CCertMgrComponentData::ShowArchivedCerts() const
  4868. {
  4869. _TRACE (0, L"Entering and leaving CCertMgrComponentData::ShowArchivedCerts\n");
  4870. if ( m_bShowArchivedCertsPersist )
  4871. return true;
  4872. else
  4873. return false;
  4874. }
  4875. HRESULT CCertMgrComponentData::OnPropertyChange (LPARAM param)
  4876. {
  4877. _TRACE (1, L"Entering CCertMgrComponentData::OnPropertyChange\n");
  4878. ASSERT (param);
  4879. if ( !param )
  4880. return E_FAIL;
  4881. HRESULT hr = S_OK;
  4882. LPDATAOBJECT pDataObject = reinterpret_cast<LPDATAOBJECT>(param);
  4883. bool bHandled = false;
  4884. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  4885. if ( pCookie )
  4886. {
  4887. switch (pCookie->m_objecttype)
  4888. {
  4889. case CERTMGR_SAFER_COMPUTER_ENTRY:
  4890. case CERTMGR_SAFER_USER_ENTRY:
  4891. {
  4892. HRESULTITEM itemID = 0;
  4893. if ( m_pResultData )
  4894. {
  4895. pCookie->Refresh ();
  4896. hr = m_pResultData->FindItemByLParam ( (LPARAM) pCookie, &itemID);
  4897. if ( SUCCEEDED (hr) )
  4898. {
  4899. hr = m_pResultData->UpdateItem (itemID);
  4900. if ( FAILED (hr) )
  4901. {
  4902. _TRACE (0, L"IResultData::UpdateItem () failed: 0x%x\n", hr);
  4903. }
  4904. }
  4905. else
  4906. {
  4907. _TRACE (0, L"IResultData::FindItemByLParam () failed: 0x%x\n", hr);
  4908. }
  4909. }
  4910. else
  4911. {
  4912. _TRACE (0, L"Unexpected error: m_pResultData was NULL\n");
  4913. hr = E_FAIL;
  4914. }
  4915. bHandled = true;
  4916. }
  4917. break;
  4918. case CERTMGR_SAFER_COMPUTER_LEVEL:
  4919. case CERTMGR_SAFER_USER_LEVEL:
  4920. if ( m_pConsole )
  4921. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  4922. break;
  4923. default:
  4924. break;
  4925. }
  4926. }
  4927. if ( !bHandled && m_pCryptUIMMCCallbackStruct )
  4928. {
  4929. if ( pDataObject == reinterpret_cast<LPDATAOBJECT>(m_pCryptUIMMCCallbackStruct->param) )
  4930. {
  4931. ::MMCFreeNotifyHandle (m_pCryptUIMMCCallbackStruct->lNotifyHandle);
  4932. pCookie = ConvertCookie (pDataObject);
  4933. if ( pCookie )
  4934. {
  4935. ASSERT (CERTMGR_CERTIFICATE == pCookie->m_objecttype);
  4936. if ( CERTMGR_CERTIFICATE == pCookie->m_objecttype )
  4937. {
  4938. CCertificate* pCert = reinterpret_cast<CCertificate*>(pCookie);
  4939. ASSERT (pCert);
  4940. if ( pCert && pCert->GetCertStore () )
  4941. {
  4942. pCert->GetCertStore ()->SetDirty ();
  4943. pCert->GetCertStore ()->Commit ();
  4944. pCert->GetCertStore ()->Close ();
  4945. }
  4946. }
  4947. }
  4948. pDataObject->Release ();
  4949. ::GlobalFree (m_pCryptUIMMCCallbackStruct);
  4950. m_pCryptUIMMCCallbackStruct = 0;
  4951. m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  4952. }
  4953. }
  4954. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::OnPropertyChange: 0x%x\n", hr);
  4955. return hr;
  4956. }
  4957. HRESULT CCertMgrComponentData::OnDeleteEFSPolicy(LPDATAOBJECT pDataObject, bool bCommitChanges)
  4958. {
  4959. _TRACE (1, L"Entering CCertMgrComponentData::OnDeleteEFSPolicy\n");
  4960. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  4961. ASSERT (pDataObject);
  4962. if ( !pDataObject )
  4963. return E_POINTER;
  4964. HRESULT hr = S_OK;
  4965. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  4966. ASSERT (pCookie);
  4967. if ( pCookie )
  4968. {
  4969. CCertStoreGPE* pStore = reinterpret_cast <CCertStoreGPE*> (pCookie);
  4970. ASSERT (pStore && EFS_STORE == pStore->GetStoreType () && !pStore->IsNullEFSPolicy () );
  4971. if ( pStore && EFS_STORE == pStore->GetStoreType () && !pStore->IsNullEFSPolicy () )
  4972. {
  4973. pStore->DeleteEFSPolicy (bCommitChanges);
  4974. hr = m_pConsole->UpdateAllViews (pDataObject, 0, HINT_EFS_ADD_DEL_POLICY);
  4975. }
  4976. }
  4977. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::OnDeleteEFSPolicy: 0x%x\n", hr);
  4978. return hr;
  4979. }
  4980. HRESULT CCertMgrComponentData::OnInitEFSPolicy(LPDATAOBJECT pDataObject)
  4981. {
  4982. _TRACE (1, L"Entering CCertMgrComponentData::OnInitEFSPolicy\n");
  4983. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  4984. ASSERT (pDataObject);
  4985. if ( !pDataObject )
  4986. return E_POINTER;
  4987. HRESULT hr = S_OK;
  4988. CCertMgrCookie* pCookie = ConvertCookie (pDataObject);
  4989. ASSERT (pCookie);
  4990. if ( pCookie )
  4991. {
  4992. CCertStoreGPE* pStore = reinterpret_cast <CCertStoreGPE*> (pCookie);
  4993. ASSERT (pStore && EFS_STORE == pStore->GetStoreType () && pStore->IsNullEFSPolicy () );
  4994. if ( pStore && EFS_STORE == pStore->GetStoreType () && pStore->IsNullEFSPolicy () )
  4995. {
  4996. pStore->SetDirty ();
  4997. pStore->AllowEmptyEFSPolicy ();
  4998. pStore->PolicyChanged ();
  4999. pStore->Commit ();
  5000. hr = m_pConsole->UpdateAllViews (pDataObject, 0, HINT_EFS_ADD_DEL_POLICY);
  5001. }
  5002. }
  5003. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::OnInitEFSPolicy: 0x%x\n", hr);
  5004. return hr;
  5005. }
  5006. ///////////////////////////////////////////////////////////////////////////////
  5007. //
  5008. // CCertMgrComponentData::RemoveResultCookies
  5009. //
  5010. // Remove and delete all the result cookies corresponding to the LPRESULTDATA
  5011. // object passed in. Thus all cookies added to pResultData are released and
  5012. // removed from the master list.
  5013. //
  5014. ///////////////////////////////////////////////////////////////////////////////
  5015. void CCertMgrComponentData::RemoveResultCookies(LPRESULTDATA pResultData)
  5016. {
  5017. _TRACE (1, L"Entering CCertMgrComponentData::RemoveResultCookies\n");
  5018. CCertMgrCookie* pCookie = 0;
  5019. CCookie& rootCookie = QueryBaseRootCookie ();
  5020. POSITION curPos = 0;
  5021. for (POSITION nextPos = rootCookie.m_listResultCookieBlocks.GetHeadPosition (); nextPos; )
  5022. {
  5023. curPos = nextPos;
  5024. pCookie = reinterpret_cast <CCertMgrCookie*> (rootCookie.m_listResultCookieBlocks.GetNext (nextPos));
  5025. ASSERT (pCookie);
  5026. if ( pCookie )
  5027. {
  5028. if ( pCookie->m_resultDataID == pResultData )
  5029. {
  5030. pCookie->Release ();
  5031. rootCookie.m_listResultCookieBlocks.RemoveAt (curPos);
  5032. }
  5033. }
  5034. }
  5035. _TRACE (-1, L"Leaving CCertMgrComponentData::RemoveResultCookies\n");
  5036. }
  5037. HRESULT CCertMgrComponentData::AddCertChainToPolicy(PCCERT_CONTEXT pCertContext)
  5038. {
  5039. _TRACE (1, L"Entering CCertMgrComponentData::AddCertChainToPolicy\n");
  5040. HRESULT hr = S_OK;
  5041. ASSERT (pCertContext);
  5042. if ( !pCertContext )
  5043. return E_POINTER;
  5044. CERT_CHAIN_PARA certChainPara;
  5045. ::ZeroMemory (&certChainPara, sizeof (CERT_CHAIN_PARA));
  5046. certChainPara.cbSize = sizeof (CERT_CHAIN_PARA);
  5047. certChainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
  5048. certChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = new LPSTR[1];
  5049. if ( !certChainPara.RequestedUsage.Usage.rgpszUsageIdentifier )
  5050. return E_OUTOFMEMORY;
  5051. certChainPara.RequestedUsage.Usage.rgpszUsageIdentifier[0] = szOID_EFS_RECOVERY;
  5052. PCCERT_CHAIN_CONTEXT pChainContext = 0;
  5053. BOOL bValidated = ::CertGetCertificateChain (
  5054. HCCE_LOCAL_MACHINE, // HCERTCHAINENGINE hChainEngine,
  5055. pCertContext,
  5056. 0, // LPFILETIME pTime,
  5057. 0, // HCERTSTORE hAdditionalStore,
  5058. &certChainPara,
  5059. 0, // dwFlags,
  5060. 0, // pvReserved,
  5061. &pChainContext);
  5062. if ( bValidated )
  5063. {
  5064. // NTRAID# 310388 CRYPTUI: Chain validation failing for self-signed certificates
  5065. // Check to see if cert is self-signed
  5066. PCERT_SIMPLE_CHAIN pChain = pChainContext->rgpChain[pChainContext->cChain - 1];
  5067. if ( pChain )
  5068. {
  5069. PCERT_CHAIN_ELEMENT pElement = pChain->rgpElement[pChain->cElement - 1];
  5070. if ( pElement )
  5071. {
  5072. BOOL bSelfSigned = pElement->TrustStatus.dwInfoStatus & CERT_TRUST_IS_SELF_SIGNED;
  5073. DWORD dwErrorStatus = pChainContext->TrustStatus.dwErrorStatus;
  5074. bValidated = ((0 == dwErrorStatus) ||
  5075. (dwErrorStatus == CERT_TRUST_IS_UNTRUSTED_ROOT) && bSelfSigned);
  5076. if ( bValidated )
  5077. {
  5078. // NTRAID# 301213 EFS Recovery Agent Import:
  5079. // Should not import the whole chain by default
  5080. /*
  5081. CString objectName;
  5082. CCertStoreGPE CAStore (
  5083. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  5084. _T (""),
  5085. _T (""),
  5086. CA_SYSTEM_STORE_NAME,
  5087. _T (""),
  5088. m_pGPEInformation,
  5089. NODEID_Machine,
  5090. m_pConsole);
  5091. CCertStoreGPE* pRootStore = dynamic_cast <CCertStoreGPE*> (m_pGPERootStore);
  5092. if ( !pRootStore )
  5093. {
  5094. VERIFY (objectName.LoadString (IDS_DOMAIN_ROOT_CERT_AUTHS_NODE_NAME));
  5095. pRootStore = new CCertStoreGPE (CERT_SYSTEM_STORE_RELOCATE_FLAG,
  5096. _T (""),
  5097. (PCWSTR) objectName,
  5098. ROOT_SYSTEM_STORE_NAME,
  5099. _T (""),
  5100. m_pGPEInformation,
  5101. NODEID_Machine,
  5102. m_pConsole);
  5103. if ( !pRootStore )
  5104. {
  5105. hr = E_OUTOFMEMORY;
  5106. }
  5107. }
  5108. CCertStoreGPE* pTrustStore = dynamic_cast<CCertStoreGPE*> (m_pGPETrustStore);
  5109. if ( !pTrustStore )
  5110. {
  5111. VERIFY (objectName.LoadString (IDS_CERTIFICATE_TRUST_LISTS));
  5112. pTrustStore = new CCertStoreGPE (CERT_SYSTEM_STORE_RELOCATE_FLAG,
  5113. _T (""),
  5114. (PCWSTR) objectName,
  5115. TRUST_SYSTEM_STORE_NAME,
  5116. _T (""),
  5117. m_pGPEInformation,
  5118. NODEID_Machine,
  5119. m_pConsole);
  5120. if ( !pTrustStore )
  5121. {
  5122. hr = E_OUTOFMEMORY;
  5123. }
  5124. }
  5125. //
  5126. // Copy all certs from all simple chains to the CA store, except the
  5127. // last cert on the last chain: copy that to the root store.
  5128. // If any CTLs are found, copy those to the trust store.
  5129. //
  5130. for (DWORD dwIndex = 0; dwIndex < pChainContext->cChain; dwIndex++)
  5131. {
  5132. DWORD i = 0;
  5133. if ( pChainContext->rgpChain[dwIndex]->pTrustListInfo )
  5134. {
  5135. // Add to Trust Store
  5136. if ( pChainContext->rgpChain[dwIndex]->pTrustListInfo->pCtlContext )
  5137. {
  5138. pTrustStore->AddCTLContext(
  5139. pChainContext->rgpChain[dwIndex]->pTrustListInfo->pCtlContext);
  5140. }
  5141. }
  5142. while (i < pChainContext->rgpChain[dwIndex]->cElement)
  5143. {
  5144. //
  5145. // If we are on the root cert, then add to the root store,
  5146. // otherwise, add to the CA store
  5147. //
  5148. // The last element is the self-signed cert if
  5149. // CERT_TRUST_NO_ERROR was returned
  5150. if ( (dwIndex == pChainContext->cChain - 1) &&
  5151. (i == pChainContext->rgpChain[dwIndex]->cElement - 1) )
  5152. {
  5153. pRootStore->AddCertificateContext(
  5154. pChainContext->rgpChain[dwIndex]->rgpElement[i]->pCertContext,
  5155. 0, false);
  5156. }
  5157. else
  5158. {
  5159. CAStore.AddCertificateContext(
  5160. pChainContext->rgpChain[dwIndex]->rgpElement[i]->pCertContext,
  5161. 0, false);
  5162. }
  5163. i++;
  5164. }
  5165. }
  5166. pRootStore->Commit ();
  5167. pTrustStore->Commit ();
  5168. CAStore.Commit ();
  5169. pRootStore->Commit ();
  5170. pTrustStore->Commit ();
  5171. CAStore.Commit ();
  5172. ::CertFreeCertificateChain(pCertChainContext);
  5173. pCertChainContext = 0;
  5174. if ( pRootStore && !m_pGPERootStore )
  5175. pRootStore->Release ();
  5176. if ( pTrustStore && !m_pGPETrustStore )
  5177. pTrustStore->Release ();
  5178. */
  5179. ::CertFreeCertificateChain(pChainContext);
  5180. pChainContext = 0;
  5181. }
  5182. }
  5183. else
  5184. bValidated = FALSE;
  5185. }
  5186. else
  5187. bValidated = FALSE;
  5188. }
  5189. else
  5190. bValidated = FALSE;
  5191. if ( pChainContext )
  5192. ::CertFreeCertificateChain(pChainContext);
  5193. if ( !bValidated )
  5194. {
  5195. int iRetVal = 0;
  5196. CString text;
  5197. CString caption;
  5198. VERIFY (text.LoadString (IDS_CERT_COULD_NOT_BE_VALIDATED));
  5199. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  5200. hr = m_pConsole->MessageBox (text, caption,
  5201. MB_OK, &iRetVal);
  5202. }
  5203. delete [] certChainPara.RequestedUsage.Usage.rgpszUsageIdentifier;
  5204. _TRACE (-1, L"LeavingLeaving CCertMgrComponentData::AddCertChainToPolicy: 0x%x\n", hr);
  5205. return hr;
  5206. }
  5207. STDMETHODIMP CCertMgrComponentData::GetLinkedTopics(LPOLESTR* lpCompiledHelpFiles)
  5208. {
  5209. HRESULT hr = S_OK;
  5210. if ( lpCompiledHelpFiles )
  5211. {
  5212. CString strLinkedTopic;
  5213. UINT nLen = ::GetSystemWindowsDirectory (strLinkedTopic.GetBufferSetLength(2 * MAX_PATH), 2 * MAX_PATH);
  5214. strLinkedTopic.ReleaseBuffer();
  5215. if ( nLen )
  5216. {
  5217. strLinkedTopic += L"\\help\\";
  5218. strLinkedTopic += m_strLinkedHelpFile;
  5219. *lpCompiledHelpFiles = reinterpret_cast<LPOLESTR>
  5220. (CoTaskMemAlloc((strLinkedTopic.GetLength() + 1)* sizeof(wchar_t)));
  5221. if ( *lpCompiledHelpFiles )
  5222. {
  5223. wcscpy(*lpCompiledHelpFiles, (PWSTR)(PCWSTR)strLinkedTopic);
  5224. }
  5225. else
  5226. hr = E_OUTOFMEMORY;
  5227. }
  5228. else
  5229. hr = E_FAIL;
  5230. }
  5231. else
  5232. return E_POINTER;
  5233. return hr;
  5234. }
  5235. STDMETHODIMP CCertMgrComponentData::GetHelpTopic(LPOLESTR* lpCompiledHelpFile)
  5236. {
  5237. return CComponentData::GetHelpTopic (lpCompiledHelpFile);
  5238. }