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.

752 lines
25 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2001.
  5. //
  6. // File: StoreGPE.cpp
  7. //
  8. // Contents: Implementation of CCertStoreGPE
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "cookie.h"
  14. #include "storegpe.h"
  15. #include "certifct.h"
  16. USE_HANDLE_MACROS("CERTMGR(storegpe.cpp)")
  17. #ifdef _DEBUG
  18. #ifndef ALPHA
  19. #define new DEBUG_NEW
  20. #endif
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. GUID g_guidExtension = { 0xb1be8d72, 0x6eac, 0x11d2, {0xa4, 0xea, 0x00, 0xc0, 0x4f, 0x79, 0xf8, 0x3a }};
  25. GUID g_guidRegExt = REGISTRY_EXTENSION_GUID;
  26. GUID g_guidSnapin = CLSID_CertificateManager;
  27. ///////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////////////////
  29. HRESULT CCertStoreGPE::Commit ()
  30. {
  31. _TRACE (1, L"Entering CCertStoreGPE::Commit - %s\n",
  32. (LPCWSTR) m_pcszStoreName);
  33. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  34. HRESULT hr = S_OK;
  35. if ( GetStoreType () == EFS_STORE && !m_fIsNullEFSPolicy )
  36. {
  37. if ( SUCCEEDED (hr) )
  38. hr = WriteEFSBlobToRegistry ();
  39. }
  40. if ( SUCCEEDED (hr) && m_bDirty )
  41. {
  42. hr = CCertStore::Commit ();
  43. ASSERT (SUCCEEDED (hr));
  44. ASSERT (m_pGPEInformation);
  45. if ( SUCCEEDED (hr) && m_pGPEInformation )
  46. {
  47. hr = m_pGPEInformation->PolicyChanged (
  48. m_fIsComputerType ? TRUE : FALSE,
  49. TRUE, &g_guidExtension, &g_guidSnapin );
  50. hr = m_pGPEInformation->PolicyChanged (
  51. m_fIsComputerType ? TRUE : FALSE,
  52. TRUE, &g_guidRegExt, &g_guidSnapin );
  53. ASSERT (SUCCEEDED (hr));
  54. }
  55. }
  56. _TRACE (-1, L"Leaving CCertStoreGPE::Commit - %s\n",
  57. (LPCWSTR) m_pcszStoreName);
  58. return hr;
  59. }
  60. CCertStoreGPE::CCertStoreGPE (
  61. DWORD dwFlags,
  62. LPCWSTR lpcszMachineName,
  63. LPCWSTR objectName,
  64. const CString & pcszLogStoreName,
  65. const CString & pcszPhysStoreName,
  66. IGPEInformation * pGPTInformation,
  67. const GUID& compDataGUID,
  68. IConsole* pConsole)
  69. : CCertStore (CERTMGR_LOG_STORE_GPE,
  70. CERT_STORE_PROV_SYSTEM, dwFlags, lpcszMachineName, objectName,
  71. pcszLogStoreName, pcszPhysStoreName,
  72. StoreNameToType (pcszLogStoreName),
  73. 0,
  74. pConsole),
  75. m_pGPEInformation (pGPTInformation),
  76. m_fIsComputerType (false),
  77. m_fIsNullEFSPolicy (true), // assume NULL policy until proven otherwise
  78. m_hGroupPolicyKey (0)
  79. {
  80. _TRACE (1, L"Entering CCertStoreGPE::CCertStoreGPE - %s\n",
  81. (LPCWSTR) pcszLogStoreName);
  82. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  83. ASSERT (m_pGPEInformation);
  84. if ( m_pGPEInformation )
  85. {
  86. m_pGPEInformation->AddRef ();
  87. if ( ::IsEqualGUID (compDataGUID, NODEID_User) )
  88. {
  89. m_fIsComputerType = false;
  90. m_dwFlags |= CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY;
  91. }
  92. else if ( ::IsEqualGUID (compDataGUID, NODEID_Machine) )
  93. {
  94. m_fIsComputerType = true;
  95. m_dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY;
  96. }
  97. else
  98. ASSERT (0);
  99. }
  100. _TRACE (-1, L"Leaving CCertStoreGPE::CCertStoreGPE - %s\n",
  101. (LPCWSTR) pcszLogStoreName);
  102. }
  103. CCertStoreGPE::~CCertStoreGPE ()
  104. {
  105. _TRACE (1, L"Entering CCertStoreGPE::~CCertStoreGPE - %s\n",
  106. (LPCWSTR) m_pcszStoreName);;
  107. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  108. if ( m_hGroupPolicyKey )
  109. RegCloseKey (m_hGroupPolicyKey);
  110. if ( m_pGPEInformation )
  111. {
  112. m_pGPEInformation->Release ();
  113. m_pGPEInformation = 0;
  114. }
  115. CERT_CONTEXT_PSID_STRUCT* pCert = 0;
  116. while (!m_EFSCertList.IsEmpty () )
  117. {
  118. pCert = m_EFSCertList.RemoveHead ();
  119. ASSERT (pCert);
  120. if ( pCert )
  121. delete pCert;
  122. }
  123. _TRACE (-1, L"Leaving CCertStoreGPE::~CCertStoreGPE - %s\n",
  124. (LPCWSTR) m_pcszStoreName);
  125. }
  126. HCERTSTORE CCertStoreGPE::GetStoreHandle (BOOL bSilent /*= FALSE*/, HRESULT* phr /* = 0*/)
  127. {
  128. _TRACE (1, L"Entering CCertStoreGPE::GetStoreHandle - %s\n",
  129. (LPCWSTR) m_pcszStoreName);
  130. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  131. CERT_SYSTEM_STORE_RELOCATE_PARA RelocatePara;
  132. void* pvPara = 0;
  133. if ( !m_hCertStore )
  134. {
  135. DWORD dwErr = 0;
  136. if ( EFS_STORE == GetStoreType () && m_fIsNullEFSPolicy )
  137. {
  138. // Test to see if EFS key exists, if not, flag this as
  139. // having no EFS policy and return.
  140. HKEY hEFSKey = 0;
  141. LONG lResult = ::RegOpenKeyEx (GetGroupPolicyKey (), CERT_EFSBLOB_REGPATH, 0,
  142. KEY_ALL_ACCESS, &hEFSKey);
  143. if ( ERROR_SUCCESS == lResult )
  144. {
  145. m_fIsNullEFSPolicy = false;
  146. VERIFY (ERROR_SUCCESS == ::RegCloseKey (hEFSKey));
  147. }
  148. else
  149. return 0;
  150. }
  151. RelocatePara.hKeyBase = GetGroupPolicyKey ();
  152. RelocatePara.pwszSystemStore = (LPCWSTR) m_pcszStoreName;
  153. pvPara = (void*) &RelocatePara;
  154. m_hCertStore = ::CertOpenStore (m_storeProvider,
  155. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, NULL,
  156. m_dwFlags | CERT_STORE_SET_LOCALIZED_NAME_FLAG | CERT_STORE_MAXIMUM_ALLOWED_FLAG,
  157. pvPara);
  158. if ( !m_hCertStore )
  159. {
  160. dwErr = GetLastError ();
  161. if ( phr )
  162. *phr = HRESULT_FROM_WIN32 (dwErr);
  163. m_hCertStore = ::CertOpenStore (m_storeProvider,
  164. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, NULL,
  165. m_dwFlags | CERT_STORE_READONLY_FLAG | CERT_STORE_SET_LOCALIZED_NAME_FLAG,
  166. pvPara);
  167. if ( m_hCertStore )
  168. m_bReadOnly = true;
  169. else
  170. {
  171. dwErr = GetLastError ();
  172. if ( phr )
  173. *phr = HRESULT_FROM_WIN32 (dwErr);
  174. _TRACE (0, L"CertOpenStore (%s) failed: 0x%x\n",
  175. (PCWSTR) m_pcszStoreName, dwErr);
  176. }
  177. }
  178. if ( !m_hCertStore && !m_bUnableToOpenMsgDisplayed && !bSilent &&
  179. (USERDS_STORE != GetStoreType ()) )
  180. {
  181. m_bUnableToOpenMsgDisplayed = true;
  182. CString caption;
  183. CString text;
  184. int iRetVal = 0;
  185. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  186. text.FormatMessage (IDS_UNABLE_TO_OPEN_STORE, GetStoreName (),
  187. GetSystemMessage (dwErr));
  188. if ( m_pConsole )
  189. m_pConsole->MessageBox (text, caption, MB_OK | MB_ICONINFORMATION, &iRetVal);
  190. }
  191. }
  192. _TRACE (-1, L"Leaving CCertStoreGPE::GetStoreHandle - %s\n",
  193. (LPCWSTR) m_pcszStoreName);
  194. return m_hCertStore;
  195. }
  196. bool CCertStoreGPE::CanContain(CertificateManagerObjectType nodeType)
  197. {
  198. _TRACE (1, L"Entering CCertStoreGPE::CanContain - %s\n",
  199. (LPCWSTR) m_pcszStoreName);
  200. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  201. bool bCanContain = false;
  202. switch (nodeType)
  203. {
  204. case CERTMGR_CERTIFICATE:
  205. if ( ROOT_STORE == GetStoreType () ||
  206. EFS_STORE == GetStoreType () )
  207. {
  208. bCanContain = true;
  209. }
  210. break;
  211. case CERTMGR_CTL:
  212. if ( TRUST_STORE == GetStoreType () )
  213. {
  214. bCanContain = true;
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. _TRACE (-1, L"Leaving CCertStoreGPE::CanContain - %s\n",
  221. (LPCWSTR) m_pcszStoreName);
  222. return bCanContain;
  223. }
  224. bool CCertStoreGPE::IsMachineStore()
  225. {
  226. _TRACE (0, L"Entering and leaving CCertStoreGPE::IsMachineStore - %s\n",
  227. (LPCWSTR) m_pcszStoreName);
  228. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  229. if (m_dwFlags & CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY)
  230. return true;
  231. else
  232. return false;
  233. }
  234. HKEY CCertStoreGPE::GetGroupPolicyKey()
  235. {
  236. _TRACE (1, L"Entering CCertStoreGPE::GetGroupPolicyKey - %s\n",
  237. (LPCWSTR) m_pcszStoreName);
  238. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  239. if ( !m_hGroupPolicyKey )
  240. {
  241. if ( m_fIsComputerType )
  242. {
  243. HRESULT hr = m_pGPEInformation->GetRegistryKey (GPO_SECTION_MACHINE,
  244. &m_hGroupPolicyKey);
  245. ASSERT (SUCCEEDED (hr));
  246. }
  247. else
  248. {
  249. HRESULT hr = m_pGPEInformation->GetRegistryKey (GPO_SECTION_USER,
  250. &m_hGroupPolicyKey);
  251. ASSERT (SUCCEEDED (hr));
  252. }
  253. }
  254. _TRACE (-1, L"Leaving CCertStoreGPE::GetGroupPolicyKey - %s\n",
  255. (LPCWSTR) m_pcszStoreName);
  256. return m_hGroupPolicyKey;
  257. }
  258. IGPEInformation * CCertStoreGPE::GetGPEInformation() const
  259. {
  260. _TRACE (0, L"Entering and leaving CCertStoreGPE::GetGPEInformation - %s\n",
  261. (LPCWSTR) m_pcszStoreName);
  262. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  263. return m_pGPEInformation;
  264. }
  265. HRESULT CCertStoreGPE::WriteEFSBlobToRegistry()
  266. {
  267. _TRACE (1, L"Entering CCertStoreGPE::WriteEFSBlobToRegistry - %s\n",
  268. (LPCWSTR) m_pcszStoreName);
  269. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  270. HRESULT hr = S_OK;
  271. if ( !m_fIsNullEFSPolicy )
  272. {
  273. HKEY hGroupPolicyKey = GetGroupPolicyKey ();
  274. if ( hGroupPolicyKey )
  275. {
  276. DWORD dwDisposition = 0;
  277. HKEY efsBlobKey = 0;
  278. int nCertCnt = GetCertCount ();
  279. LONG lResult = ::RegCreateKeyEx (hGroupPolicyKey, // handle of an open key
  280. CERT_EFSBLOB_REGPATH, // address of subkey name
  281. 0, // reserved
  282. L"", // address of class string
  283. REG_OPTION_NON_VOLATILE, // special options flag
  284. KEY_ALL_ACCESS, // desired security access
  285. NULL, // address of key security structure
  286. &efsBlobKey, // address of buffer for opened handle
  287. &dwDisposition); // address of disposition value buffer
  288. ASSERT (lResult == ERROR_SUCCESS);
  289. if ( lResult == ERROR_SUCCESS )
  290. {
  291. PEFS_PUBLIC_KEY_INFO* pEFSPKI = new PEFS_PUBLIC_KEY_INFO[nCertCnt];
  292. DWORD* cbPKI = new DWORD[nCertCnt];
  293. PRECOVERY_KEY_1_1* pRecoveryKey = new PRECOVERY_KEY_1_1[nCertCnt];
  294. DWORD* cbRecoveryKey = new DWORD[nCertCnt];
  295. PRECOVERY_POLICY_1_1 pRecoveryPolicy = 0;
  296. DWORD cbRecoveryPolicy = 0;
  297. BYTE* pData = 0;
  298. DWORD cbData = 0;
  299. int nActualCertCnt = 0;
  300. PCCERT_CONTEXT pCertContext = 0;
  301. if ( !pEFSPKI || ! cbPKI || ! pRecoveryKey || !cbRecoveryKey )
  302. {
  303. VERIFY (ERROR_SUCCESS == ::RegCloseKey (efsBlobKey));
  304. hr = E_OUTOFMEMORY;
  305. }
  306. ::ZeroMemory (pEFSPKI, nCertCnt*sizeof (PEFS_PUBLIC_KEY_INFO));
  307. ::ZeroMemory (cbPKI, nCertCnt*sizeof (DWORD));
  308. ::ZeroMemory (pRecoveryKey, nCertCnt*sizeof (PRECOVERY_KEY_1_1));
  309. ::ZeroMemory (cbRecoveryKey, nCertCnt*sizeof (DWORD));
  310. while ( 1 )
  311. {
  312. // Subsequent calls to CertEnumCertificatesInStore () free pCertContext. If
  313. // we must break prematurely out of this loop, we must CertFreeCertificateContext ()
  314. // explicitly on the last pCertContext
  315. pCertContext = EnumCertificates (pCertContext);
  316. if ( pCertContext )
  317. {
  318. hr = CreatePublicKeyInformationCertificate (
  319. GetPSIDFromCert (pCertContext),
  320. pCertContext->pbCertEncoded,
  321. pCertContext->cbCertEncoded,
  322. &pEFSPKI[nActualCertCnt],
  323. &cbPKI[nActualCertCnt]);
  324. if ( SUCCEEDED (hr) )
  325. {
  326. cbRecoveryKey[nActualCertCnt] = sizeof (ULONG) + cbPKI[nActualCertCnt];
  327. pRecoveryKey[nActualCertCnt] = (PRECOVERY_KEY_1_1) ::LocalAlloc (LPTR, cbRecoveryKey[nActualCertCnt]);
  328. if ( pRecoveryKey[nActualCertCnt] )
  329. {
  330. pRecoveryKey[nActualCertCnt]->TotalLength = cbRecoveryKey[nActualCertCnt];
  331. memcpy (&(pRecoveryKey[nActualCertCnt]->PublicKeyInfo),
  332. pEFSPKI[nActualCertCnt],
  333. cbPKI[nActualCertCnt]);
  334. }
  335. else
  336. {
  337. hr = E_OUTOFMEMORY;
  338. ::CertFreeCertificateContext (pCertContext);
  339. break;
  340. }
  341. }
  342. nActualCertCnt++;
  343. if ( nActualCertCnt > nCertCnt )
  344. {
  345. ASSERT (0);
  346. ::CertFreeCertificateContext (pCertContext);
  347. break;
  348. }
  349. }
  350. else
  351. break;
  352. }
  353. Close ();
  354. ASSERT (nActualCertCnt == nCertCnt);
  355. if ( SUCCEEDED (hr) )
  356. {
  357. cbRecoveryPolicy = sizeof (RECOVERY_POLICY_HEADER);
  358. for (int nIndex = 0; nIndex < nActualCertCnt; nIndex++)
  359. cbRecoveryPolicy += cbRecoveryKey[nIndex];
  360. pRecoveryPolicy = (PRECOVERY_POLICY_1_1) ::LocalAlloc (LPTR, cbRecoveryPolicy);
  361. if ( pRecoveryPolicy )
  362. {
  363. pRecoveryPolicy->RecoveryPolicyHeader.MajorRevision = EFS_RECOVERY_POLICY_MAJOR_REVISION_1;
  364. pRecoveryPolicy->RecoveryPolicyHeader.MinorRevision = EFS_RECOVERY_POLICY_MINOR_REVISION_1;
  365. pRecoveryPolicy->RecoveryPolicyHeader.RecoveryKeyCount = nActualCertCnt;
  366. // Build array of variable size recovery keys.
  367. BYTE* ptr = (BYTE*) pRecoveryPolicy->RecoveryKeyList;
  368. for (int nIndex = 0; nIndex < nActualCertCnt; nIndex++)
  369. {
  370. memcpy (ptr, pRecoveryKey[nIndex], cbRecoveryKey[nIndex]);
  371. ptr += cbRecoveryKey[nIndex];
  372. }
  373. }
  374. else
  375. {
  376. hr = E_OUTOFMEMORY;
  377. }
  378. if ( pRecoveryPolicy ) // otherwise, the value is set to 0
  379. {
  380. pData = (BYTE*) pRecoveryPolicy;
  381. cbData = cbRecoveryPolicy;
  382. }
  383. lResult = RegSetValueEx (efsBlobKey, // handle of key to set value for
  384. CERT_EFSBLOB_VALUE_NAME, // address of value to set
  385. 0, // reserved
  386. REG_BINARY, // flag for value type
  387. pData, // address of value data
  388. cbData); // size of value data
  389. if ( lResult == ERROR_SUCCESS )
  390. {
  391. m_bDirty = true;
  392. }
  393. else
  394. DisplaySystemError (NULL, lResult);
  395. }
  396. VERIFY (ERROR_SUCCESS == ::RegCloseKey (efsBlobKey));
  397. efsBlobKey = 0;
  398. // Free all the allocated pointers in the arrays.
  399. for (int nIndex = 0; nIndex < nActualCertCnt; nIndex++)
  400. {
  401. if ( pEFSPKI[nIndex] )
  402. ::LocalFree (pEFSPKI[nIndex]);
  403. if ( pRecoveryKey[nIndex] )
  404. ::LocalFree (pRecoveryKey[nIndex]);
  405. }
  406. // Free the allocated arrays
  407. if ( pEFSPKI )
  408. delete [] pEFSPKI;
  409. if ( cbPKI )
  410. delete [] cbPKI;
  411. if ( cbRecoveryKey )
  412. delete [] cbRecoveryKey;
  413. if ( pRecoveryKey )
  414. delete [] pRecoveryKey;
  415. if ( pRecoveryPolicy )
  416. ::LocalFree (pRecoveryPolicy);
  417. }
  418. else
  419. {
  420. hr = HRESULT_FROM_WIN32 (lResult);
  421. DisplaySystemError (NULL, lResult);
  422. }
  423. if ( SUCCEEDED (hr) )
  424. m_bDirty = true;
  425. }
  426. }
  427. _TRACE (-1, L"Leaving CCertStoreGPE::WriteEFSBlobToRegistry - %s\n",
  428. (LPCWSTR) m_pcszStoreName);
  429. return hr;
  430. }
  431. #define POINTER_TO_OFFSET( Pointer, Base ) (((PUCHAR)(Pointer)) - ((PUCHAR)(Base)))
  432. HRESULT CCertStoreGPE::CreatePublicKeyInformationCertificate(
  433. IN PSID pUserSid OPTIONAL,
  434. PBYTE pbCert,
  435. DWORD cbCert,
  436. OUT PEFS_PUBLIC_KEY_INFO * PublicKeyInformation,
  437. DWORD* pcbPublicKeyInfo)
  438. {
  439. _TRACE (1, L"Entering CCertStoreGPE::CreatePublicKeyInformationCertificate - %s\n",
  440. (LPCWSTR) m_pcszStoreName);
  441. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  442. ASSERT (PublicKeyInformation && pcbPublicKeyInfo);
  443. if ( !PublicKeyInformation || !pcbPublicKeyInfo )
  444. return E_POINTER;
  445. DWORD PublicKeyInformationLength = 0;
  446. DWORD UserSidLength = 0;
  447. PWCHAR Base = 0;
  448. if (pUserSid != NULL)
  449. {
  450. UserSidLength = GetLengthSid( pUserSid );
  451. }
  452. PublicKeyInformationLength = sizeof( EFS_PUBLIC_KEY_INFO ) + UserSidLength + cbCert;
  453. //
  454. // Allocate and fill in the PublicKeyInformation structure
  455. //
  456. *PublicKeyInformation = (PEFS_PUBLIC_KEY_INFO) ::LocalAlloc (LPTR, PublicKeyInformationLength);
  457. if ( !(*PublicKeyInformation) )
  458. {
  459. return ERROR_NOT_ENOUGH_MEMORY;
  460. }
  461. (*PublicKeyInformation)->Length = PublicKeyInformationLength;
  462. (*PublicKeyInformation)->KeySourceTag = (ULONG)EfsCertificate;
  463. //
  464. // Copy the string and SID data to the end of the structure.
  465. //
  466. Base = (PWCHAR)(*PublicKeyInformation);
  467. Base = (PWCHAR)((PBYTE)Base + sizeof( EFS_PUBLIC_KEY_INFO ));
  468. if (pUserSid != NULL)
  469. {
  470. (*PublicKeyInformation)->PossibleKeyOwner = (ULONG)POINTER_TO_OFFSET( Base, *PublicKeyInformation );
  471. CopySid( UserSidLength, (PSID)Base, pUserSid );
  472. }
  473. else
  474. {
  475. (*PublicKeyInformation)->PossibleKeyOwner = (ULONG)NULL;
  476. }
  477. Base = (PWCHAR)((PBYTE)Base + UserSidLength);
  478. (*PublicKeyInformation)->CertificateInfo.CertificateLength = cbCert;
  479. (*PublicKeyInformation)->CertificateInfo.Certificate = (ULONG)POINTER_TO_OFFSET( Base, *PublicKeyInformation );
  480. memcpy( (PBYTE)Base, pbCert, cbCert );
  481. *pcbPublicKeyInfo = PublicKeyInformationLength;
  482. _TRACE (-1, L"Leaving CCertStoreGPE::CreatePublicKeyInformationCertificate - %s\n",
  483. (LPCWSTR) m_pcszStoreName);
  484. return 0;
  485. }
  486. void CCertStoreGPE::AddCertToList(PCCERT_CONTEXT pCertContext, PSID userPSID)
  487. {
  488. _TRACE (1, L"Entering CCertStoreGPE::AddCertToList - %s\n",
  489. (LPCWSTR) m_pcszStoreName);
  490. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  491. if ( pCertContext && userPSID )
  492. {
  493. CERT_CONTEXT_PSID_STRUCT* pCert = new CERT_CONTEXT_PSID_STRUCT (
  494. pCertContext, userPSID);
  495. if ( pCert )
  496. {
  497. m_EFSCertList.AddTail (pCert);
  498. m_bDirty = true;
  499. }
  500. }
  501. _TRACE (-1, L"Leaving CCertStoreGPE::AddCertToList - %s\n",
  502. (LPCWSTR) m_pcszStoreName);
  503. }
  504. PSID CCertStoreGPE::GetPSIDFromCert (PCCERT_CONTEXT pCertContext)
  505. {
  506. _TRACE (1, L"Entering CCertStoreGPE::GetPSIDFromCert - %s\n",
  507. (LPCWSTR) m_pcszStoreName);
  508. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  509. PSID pSID = 0;
  510. CERT_CONTEXT_PSID_STRUCT* pCert = 0;
  511. POSITION curPos = 0;
  512. for (POSITION nextPos = m_EFSCertList.GetHeadPosition (); nextPos; )
  513. {
  514. curPos = nextPos;
  515. pCert = m_EFSCertList.GetNext (nextPos);
  516. if ( CertCompareCertificate (
  517. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  518. pCert->m_pCertContext->pCertInfo,
  519. pCertContext->pCertInfo) )
  520. {
  521. pSID = pCert->m_psid;
  522. break;
  523. }
  524. }
  525. _TRACE (-1, L"Leaving CCertStoreGPE::GetPSIDFromCert - %s\n",
  526. (LPCWSTR) m_pcszStoreName);
  527. return pSID;
  528. }
  529. void CCertStoreGPE::FinalCommit()
  530. {
  531. _TRACE (1, L"Entering CCertStoreGPE::FinalCommit - %s\n",
  532. (LPCWSTR) m_pcszStoreName);
  533. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype);
  534. // Called only from destructor
  535. // Cannot commit here for GPT: GPT has already freed all pertinent data
  536. _TRACE (-1, L"Leaving CCertStoreGPE::FinalCommit - %s\n",
  537. (LPCWSTR) m_pcszStoreName);
  538. }
  539. bool CCertStoreGPE::IsNullEFSPolicy()
  540. {
  541. _TRACE (1, L"Entering CCertStoreGPE::IsNullEFSPolicy - %s\n",
  542. (LPCWSTR) m_pcszStoreName);
  543. GetStoreHandle (); // to initialize
  544. Close ();
  545. _TRACE (-1, L"Leaving CCertStoreGPE::IsNullEFSPolicy - %s\n",
  546. (LPCWSTR) m_pcszStoreName);
  547. return m_fIsNullEFSPolicy;
  548. }
  549. void CCertStoreGPE::AllowEmptyEFSPolicy()
  550. {
  551. _TRACE (1, L"Entering CCertStoreGPE::AllowEmptyEFSPolicy - %s\n",
  552. (LPCWSTR) m_pcszStoreName);
  553. m_fIsNullEFSPolicy = false;
  554. _TRACE (-1, L"Leaving CCertStoreGPE::AllowEmptyEFSPolicy - %s\n",
  555. (LPCWSTR) m_pcszStoreName);
  556. }
  557. HRESULT CCertStoreGPE::AddCertificateContext(PCCERT_CONTEXT pContext, LPCONSOLE pConsole, bool bDeletePrivateKey)
  558. {
  559. _TRACE (1, L"Entering CCertStoreGPE::AddCertificateContext - %s\n",
  560. (LPCWSTR) m_pcszStoreName);
  561. HRESULT hr = S_OK;
  562. AllowEmptyEFSPolicy ();
  563. hr = CCertStore::AddCertificateContext (pContext, pConsole, bDeletePrivateKey);
  564. _TRACE (-1, L"Leaving CCertStoreGPE::AddCertificateContext - %s\n",
  565. (LPCWSTR) m_pcszStoreName);
  566. return hr;
  567. }
  568. HRESULT CCertStoreGPE::DeleteEFSPolicy(bool bCommitChanges)
  569. {
  570. _TRACE (1, L"Entering CCertStoreGPE::DeleteEFSPolicy - %s\n",
  571. (LPCWSTR) m_pcszStoreName);
  572. ASSERT (EFS_STORE == GetStoreType ());
  573. if ( EFS_STORE == GetStoreType () )
  574. {
  575. // If the store is open, close it first
  576. if ( m_hCertStore )
  577. {
  578. CERT_CONTEXT_PSID_STRUCT* pCert = 0;
  579. while (!m_EFSCertList.IsEmpty () )
  580. {
  581. pCert = m_EFSCertList.RemoveHead ();
  582. ASSERT (pCert);
  583. if ( pCert )
  584. delete pCert;
  585. }
  586. VERIFY (::CertCloseStore (m_hCertStore, CERT_CLOSE_STORE_FORCE_FLAG)); //CERT_CLOSE_STORE_CHECK_FLAG);
  587. m_hCertStore = 0;
  588. }
  589. LRESULT lResult = ::RegDelnode (GetGroupPolicyKey (), CERT_EFSBLOB_REGPATH);
  590. if ( ERROR_SUCCESS == lResult )
  591. {
  592. m_fIsNullEFSPolicy = true;
  593. m_bDirty = true;
  594. if ( bCommitChanges )
  595. Commit ();
  596. }
  597. else
  598. DisplaySystemError (NULL, (DWORD)lResult);
  599. }
  600. _TRACE (-1, L"Leaving CCertStoreGPE::DeleteEFSPolicy - %s\n",
  601. (LPCWSTR) m_pcszStoreName);
  602. return S_OK;
  603. }
  604. HRESULT CCertStoreGPE::PolicyChanged()
  605. {
  606. _TRACE (1, L"Entering CCertStoreGPE::PolicyChanged - %s\n",
  607. (LPCWSTR) m_pcszStoreName);
  608. HRESULT hr = E_FAIL;
  609. if ( m_pGPEInformation )
  610. {
  611. hr = m_pGPEInformation->PolicyChanged (
  612. m_fIsComputerType ? TRUE : FALSE,
  613. TRUE, &g_guidExtension, &g_guidSnapin);
  614. hr = m_pGPEInformation->PolicyChanged (
  615. m_fIsComputerType ? TRUE : FALSE,
  616. TRUE, &g_guidRegExt, &g_guidSnapin);
  617. }
  618. _TRACE (-1, L"Leaving CCertStoreGPE::PolicyChanged - %s\n",
  619. (LPCWSTR) m_pcszStoreName);
  620. return hr;
  621. }
  622. PCCERT_CONTEXT CCertStoreGPE::EnumCertificates (PCCERT_CONTEXT pPrevCertContext)
  623. {
  624. PCCERT_CONTEXT pCertContext = CCertStore::EnumCertificates (pPrevCertContext);
  625. if ( pCertContext )
  626. m_fIsNullEFSPolicy = false;
  627. return pCertContext;
  628. }
  629. CERT_CONTEXT_PSID_STRUCT::CERT_CONTEXT_PSID_STRUCT (PCCERT_CONTEXT pCertContext, PSID psid) :
  630. m_pCertContext (0),
  631. m_psid (0)
  632. {
  633. if ( pCertContext && psid )
  634. {
  635. m_pCertContext = CertDuplicateCertificateContext (pCertContext);
  636. DWORD dwSidSize = ::GetLengthSid (psid);
  637. if ( dwSidSize > 0 )
  638. {
  639. m_psid = new BYTE[dwSidSize];
  640. if ( m_psid )
  641. {
  642. ::ZeroMemory (m_psid, dwSidSize);
  643. if ( !::CopySid (dwSidSize, m_psid, psid) )
  644. {
  645. ASSERT (0);
  646. delete [] m_psid;
  647. m_psid = 0;
  648. }
  649. }
  650. }
  651. }
  652. }
  653. CERT_CONTEXT_PSID_STRUCT::~CERT_CONTEXT_PSID_STRUCT ()
  654. {
  655. if ( m_pCertContext )
  656. ::CertFreeCertificateContext (m_pCertContext);
  657. if ( m_psid )
  658. delete [] m_psid;
  659. }