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.

1474 lines
38 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2001.
  5. //
  6. // File: Cookie.cpp
  7. //
  8. // Contents: Implementation of CCertMgrCookie and related classes
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "cookie.h"
  14. #include "storegpe.h"
  15. #pragma warning(push, 3)
  16. #include <atlimpl.cpp>
  17. #pragma warning(pop)
  18. DECLARE_INFOLEVEL(CertificateManagerSnapin)
  19. USE_HANDLE_MACROS("CERTMGR(cookie.cpp)")
  20. #ifdef _DEBUG
  21. #ifndef ALPHA
  22. #define new DEBUG_NEW
  23. #endif
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. #include "stdcooki.cpp"
  28. #include "stdutils.cpp"
  29. #include "certifct.h"
  30. //
  31. // CCertMgrCookie
  32. //
  33. // returns <0, 0 or >0
  34. CCertMgrCookie::CCertMgrCookie (CertificateManagerObjectType objecttype,
  35. LPCWSTR lpcszMachineName,
  36. LPCWSTR objectName)
  37. : CStoresMachineName (lpcszMachineName),
  38. m_objecttype (objecttype),
  39. m_objectName (objectName),
  40. m_resultDataID (0),
  41. m_nOpenPageCount (0)
  42. {
  43. ASSERT (IsValidObjectType (m_objecttype));
  44. if ( m_objectName.IsEmpty () )
  45. {
  46. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  47. VERIFY (m_objectName.LoadString (IDS_CERTIFICATE_MANAGER));
  48. }
  49. }
  50. CCertMgrCookie::~CCertMgrCookie ()
  51. {
  52. }
  53. HRESULT CCertMgrCookie::CompareSimilarCookies( CCookie* pOtherCookie, int* pnResult )
  54. {
  55. ASSERT (pOtherCookie);
  56. CCertMgrCookie* pcookie = reinterpret_cast <CCertMgrCookie*>(pOtherCookie);
  57. ASSERT (pcookie);
  58. if ( pcookie && m_objecttype != pcookie->m_objecttype )
  59. {
  60. *pnResult = ((int)m_objecttype) - ((int)pcookie->m_objecttype); // arbitrary ordering
  61. return S_OK;
  62. }
  63. return E_UNEXPECTED;
  64. }
  65. CCookie* CCertMgrCookie::QueryBaseCookie(int i)
  66. {
  67. ASSERT(!i);
  68. return (CCookie*)this;
  69. }
  70. int CCertMgrCookie::QueryNumCookies()
  71. {
  72. return 1;
  73. }
  74. LPCWSTR CCertMgrCookie::GetObjectName()
  75. {
  76. return m_objectName;
  77. }
  78. HRESULT CCertMgrCookie::Commit()
  79. {
  80. return S_OK;
  81. }
  82. UINT CCertMgrCookie::IncrementOpenPageCount ()
  83. {
  84. m_nOpenPageCount++;
  85. return m_nOpenPageCount;
  86. }
  87. UINT CCertMgrCookie::DecrementOpenPageCount ()
  88. {
  89. ASSERT (0 != m_nOpenPageCount);
  90. if ( 0 != m_nOpenPageCount )
  91. m_nOpenPageCount--;
  92. return m_nOpenPageCount;
  93. }
  94. bool CCertMgrCookie::HasOpenPropertyPages () const
  95. {
  96. return (0 != m_nOpenPageCount) ? true : false;
  97. }
  98. ///////////////////////////////////////////////////////////////////////////////
  99. ///////////////////////////////////////////////////////////////////////////////
  100. CCertStore::CCertStore (
  101. CertificateManagerObjectType objecttype,
  102. LPCSTR pszStoreProv,
  103. DWORD dwFlags,
  104. LPCWSTR lpcszMachineName,
  105. LPCWSTR objectName,
  106. const CString & pcszLogStoreName,
  107. const CString & pcszPhysStoreName,
  108. const SPECIAL_STORE_TYPE storeType,
  109. const DWORD dwLocation,
  110. IConsole* pConsole)
  111. : CCertMgrCookie (objecttype,
  112. lpcszMachineName,
  113. objectName),
  114. m_hCertStore (0),
  115. m_dwFlags (dwFlags),
  116. m_storeProvider (pszStoreProv),
  117. m_pcszStoreName (pcszLogStoreName),
  118. m_storeType (storeType),
  119. m_bReadOnly (false),
  120. m_bUnableToOpenMsgDisplayed (false),
  121. m_dwLocation (dwLocation),
  122. m_pConsole (pConsole),
  123. m_bDirty (false),
  124. m_fReadOnlyFlagChecked (false),
  125. m_nCertCount (0),
  126. m_fCertCountValid (false),
  127. m_nLockCnt (0) // test
  128. {
  129. _TRACE (1, L"Entering CCertStore::CCertStore LS: %s PS: %s\n",
  130. (LPCWSTR) pcszLogStoreName,
  131. (LPCWSTR) pcszPhysStoreName);
  132. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  133. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  134. CERTMGR_LOG_STORE == m_objecttype ||
  135. CERTMGR_PHYS_STORE == m_objecttype);
  136. if ( CERT_STORE_PROV_COLLECTION != pszStoreProv )
  137. {
  138. ASSERT (!pcszLogStoreName.IsEmpty ());
  139. if ( !pcszPhysStoreName.IsEmpty () )
  140. m_pcszStoreName += _T("\\") + pcszPhysStoreName;
  141. }
  142. ASSERT (m_pConsole);
  143. if ( m_pConsole )
  144. m_pConsole->AddRef ();
  145. _TRACE (-1, L"Leaving CCertStore::CCertStore - %s\n",
  146. (LPCWSTR) m_pcszStoreName);
  147. }
  148. CCertStore::~CCertStore ()
  149. {
  150. _TRACE (1, L"Entering CCertStore::~CCertStore - %s\n",
  151. (LPCWSTR) m_pcszStoreName);
  152. Close (true); // force close
  153. if ( m_pConsole )
  154. m_pConsole->Release ();
  155. _TRACE (-1, L"Leaving CCertStore::~CCertStore - %s\n",
  156. (LPCWSTR) m_pcszStoreName);
  157. }
  158. HCERTSTORE CCertStore::GetStoreHandle (BOOL bSilent /* = FALSE*/, HRESULT* phr /* = 0*/)
  159. {
  160. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  161. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  162. CERTMGR_LOG_STORE == m_objecttype ||
  163. CERTMGR_PHYS_STORE == m_objecttype);
  164. DWORD dwErr = 0;
  165. if ( !m_hCertStore )
  166. {
  167. void* pvPara = 0;
  168. if ( CERT_STORE_PROV_COLLECTION != m_storeProvider )
  169. pvPara = (void*)(LPCWSTR) m_pcszStoreName;
  170. m_dwFlags |= CERT_STORE_SET_LOCALIZED_NAME_FLAG;
  171. _TRACE (0, L"Opening %s store\n", (LPCWSTR) m_pcszStoreName);
  172. m_hCertStore = ::CertOpenStore (m_storeProvider,
  173. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  174. NULL,
  175. m_dwFlags | CERT_STORE_MAXIMUM_ALLOWED_FLAG,
  176. pvPara);
  177. if ( !m_hCertStore )
  178. {
  179. dwErr = GetLastError ();
  180. if ( phr )
  181. *phr = HRESULT_FROM_WIN32 (dwErr);
  182. _TRACE (0, L"Open of %s store failed. 0x%x\n", (LPCWSTR) m_pcszStoreName,
  183. dwErr);
  184. _TRACE (0, L"Opening %s store (read-only)\n", (LPCWSTR) m_pcszStoreName);
  185. m_dwFlags |= CERT_STORE_READONLY_FLAG;
  186. m_hCertStore = ::CertOpenStore (m_storeProvider,
  187. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  188. NULL,
  189. m_dwFlags,
  190. pvPara);
  191. if ( m_hCertStore )
  192. m_bReadOnly = true;
  193. else
  194. {
  195. dwErr = GetLastError ();
  196. if ( phr )
  197. *phr = HRESULT_FROM_WIN32 (dwErr);
  198. _TRACE (0, L"CertOpenStore (%s) failed: 0x%x\n",
  199. (PCWSTR) m_pcszStoreName, dwErr);
  200. }
  201. }
  202. if ( !m_hCertStore )
  203. {
  204. dwErr = GetLastError ();
  205. if ( phr )
  206. *phr = HRESULT_FROM_WIN32 (dwErr);
  207. _TRACE (0, L"Open of %s store (read-only) failed. 0x%x\n",
  208. (LPCWSTR) m_pcszStoreName, dwErr);
  209. if ( CERT_STORE_PROV_FILENAME_W == m_storeProvider)
  210. {
  211. _TRACE (0, L"Open %s store (file-based).\n", (LPCWSTR) m_pcszStoreName);
  212. DWORD cbData = 0;
  213. BYTE* pbData = NULL;
  214. //get the BLOB from the file
  215. HRESULT hr = RetrieveBLOBFromFile ((LPCWSTR) m_pcszStoreName,
  216. &cbData, &pbData);
  217. if ( SUCCEEDED (hr) )
  218. {
  219. //open a generic memory store
  220. m_hCertStore = ::CertOpenStore (CERT_STORE_PROV_MEMORY,
  221. 0, NULL,
  222. CERT_STORE_SET_LOCALIZED_NAME_FLAG | CERT_STORE_MAXIMUM_ALLOWED_FLAG,
  223. NULL);
  224. if ( m_hCertStore )
  225. {
  226. if ( !CertAddSerializedElementToStore (
  227. m_hCertStore,
  228. pbData,
  229. cbData,
  230. CERT_STORE_ADD_ALWAYS,
  231. 0,
  232. CERT_STORE_ALL_CONTEXT_FLAG,
  233. NULL,
  234. NULL) )
  235. {
  236. // CertAddSerializedElementToStore failed. This means this is probably a
  237. // base64 certificate or a store containing a base64 certificate. Open it
  238. // up with CryptQueryObject.
  239. _TRACE (0, L"CCertStore::GetStoreHandle () - File input is not a serialized element.\n");
  240. DWORD dwMsgAndCertEncodingType = 0;
  241. DWORD dwContentType = 0;
  242. DWORD dwFormatType = 0;
  243. PCERT_CONTEXT pCertContext = 0;
  244. CERT_BLOB certBlob;
  245. HCERTSTORE hCertStore = 0;
  246. ::ZeroMemory (&certBlob, sizeof (CERT_BLOB));
  247. certBlob.cbData = cbData;
  248. certBlob.pbData = pbData;
  249. BOOL bResult = ::CryptQueryObject (
  250. CERT_QUERY_OBJECT_BLOB, // CERT_QUERY_OBJECT_FILE,
  251. (void *) &certBlob, //(LPCWSTR) m_pcszStoreName,
  252. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT |
  253. CERT_QUERY_CONTENT_FLAG_CERT |
  254. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
  255. CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED |
  256. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
  257. CERT_QUERY_FORMAT_FLAG_ALL,
  258. 0,
  259. &dwMsgAndCertEncodingType,
  260. &dwContentType,
  261. &dwFormatType,
  262. &hCertStore,
  263. NULL,
  264. (const void **) &pCertContext);
  265. if ( bResult && pCertContext )
  266. {
  267. // The file contains a certificate context
  268. hr = AddCertificateContext (pCertContext, 0, false);
  269. }
  270. else if ( bResult && hCertStore )
  271. {
  272. // The file contains a certificate store, so close the temporary memory store we created
  273. // and assign the returned store handle to the global handle.
  274. ::CertCloseStore (m_hCertStore, CERT_CLOSE_STORE_FORCE_FLAG);
  275. m_hCertStore = hCertStore;
  276. }
  277. else
  278. {
  279. CString caption;
  280. CString message;
  281. int iRetVal = 0;
  282. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  283. VERIFY (message.LoadString (IDS_UNKNOWN_CERT_FILE_TYPE));
  284. if ( m_pConsole )
  285. m_pConsole->MessageBox (message, caption, MB_ICONWARNING | MB_OK, &iRetVal);
  286. }
  287. }
  288. }
  289. else
  290. {
  291. dwErr = GetLastError ();
  292. _TRACE (0, L"CertOpenStore (%s) failed: 0x%x\n",
  293. (PCWSTR) m_pcszStoreName, dwErr);
  294. }
  295. }
  296. else
  297. {
  298. _TRACE (0, L"CCertStore::GetStoreHandle () - Unable to retrieve BLOB from file: %x.\n", hr);
  299. }
  300. if ( phr )
  301. *phr = hr;
  302. }
  303. }
  304. else
  305. {
  306. _TRACE (0, L"Open of %s store succeeded.\n", (LPCWSTR) m_pcszStoreName);
  307. }
  308. }
  309. if ( !m_hCertStore && !m_bUnableToOpenMsgDisplayed && !bSilent &&
  310. (USERDS_STORE != GetStoreType ()) )
  311. {
  312. m_bUnableToOpenMsgDisplayed = true;
  313. CString caption;
  314. CString text;
  315. int iRetVal = 0;
  316. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  317. text.FormatMessage (IDS_UNABLE_TO_OPEN_STORE, GetStoreName (),
  318. GetSystemMessage (dwErr));
  319. if ( m_pConsole )
  320. m_pConsole->MessageBox ( text, caption, MB_OK, &iRetVal);
  321. }
  322. return m_hCertStore;
  323. }
  324. //--------------------------------------------------------------------------------
  325. //
  326. //get the bytes from the file name
  327. //
  328. // Note; Memory is allocated via malloc. S_OK is returned
  329. // via succees
  330. //
  331. //
  332. //
  333. //---------------------------------------------------------------------------------
  334. HRESULT CCertStore::RetrieveBLOBFromFile (LPCWSTR pwszFileName, DWORD *pcb, BYTE **ppb)
  335. {
  336. _TRACE (1, L"Entering CCertStore::RetrieveBLOBFromFile - %s\n",
  337. (LPCWSTR) m_pcszStoreName);
  338. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  339. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  340. CERTMGR_LOG_STORE == m_objecttype ||
  341. CERTMGR_PHYS_STORE == m_objecttype);
  342. HRESULT hr = S_OK;
  343. HANDLE hFile = NULL;
  344. DWORD dwBytesRead = 0;
  345. if(!pcb || !ppb || !pwszFileName)
  346. return E_INVALIDARG;
  347. *ppb=NULL;
  348. *pcb=0;
  349. hFile = ::CreateFile (pwszFileName,
  350. GENERIC_READ,
  351. FILE_SHARE_READ,
  352. NULL, // lpsa
  353. OPEN_EXISTING,
  354. FILE_ATTRIBUTE_NORMAL,
  355. NULL);
  356. if (INVALID_HANDLE_VALUE != hFile )
  357. {
  358. *pcb = GetFileSize(hFile, NULL);
  359. if ( 0xffffffff != *pcb )
  360. {
  361. *ppb = (BYTE *)malloc(*pcb);
  362. if ( *ppb )
  363. {
  364. // Read the pkcs7 message
  365. if ( !ReadFile(hFile,
  366. *ppb,
  367. *pcb,
  368. &dwBytesRead,
  369. NULL) ||
  370. dwBytesRead != *pcb )
  371. {
  372. free(*ppb);
  373. *ppb=NULL;
  374. hr = HRESULT_FROM_WIN32(GetLastError());;
  375. }
  376. }
  377. else
  378. hr = E_OUTOFMEMORY;
  379. }
  380. else
  381. hr = HRESULT_FROM_WIN32(GetLastError());
  382. //close the file handle
  383. CloseHandle(hFile);
  384. }
  385. else
  386. hr = HRESULT_FROM_WIN32(GetLastError());
  387. _TRACE (-1, L"Leaving CCertStore::RetrieveBLOBFromFile - %s\n",
  388. (LPCWSTR) m_pcszStoreName);
  389. return S_OK;
  390. }
  391. bool CCertStore::IsReadOnly ()
  392. {
  393. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  394. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  395. CERTMGR_LOG_STORE == m_objecttype ||
  396. CERTMGR_PHYS_STORE == m_objecttype);
  397. // GetCertStore () sets the read-only checked flag
  398. // Optimization: If the flag is true, it's already been set.
  399. if ( !m_fReadOnlyFlagChecked )
  400. {
  401. GetStoreHandle ();
  402. Close ();
  403. m_fReadOnlyFlagChecked = true;
  404. }
  405. return m_bReadOnly;
  406. }
  407. HRESULT CCertStore::Commit()
  408. {
  409. _TRACE (1, L"Entering CCertStore::Commit - %s\n",
  410. (LPCWSTR) m_pcszStoreName);
  411. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  412. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  413. CERTMGR_LOG_STORE == m_objecttype ||
  414. CERTMGR_PHYS_STORE == m_objecttype);
  415. HRESULT hr = CCertMgrCookie::Commit ();
  416. if ( m_hCertStore && !m_bReadOnly && m_bDirty )
  417. {
  418. BOOL bResult = ::CertControlStore (m_hCertStore, 0,
  419. CERT_STORE_CTRL_COMMIT, NULL);
  420. if ( bResult )
  421. m_bDirty = false;
  422. else
  423. {
  424. DWORD dwErr = GetLastError ();
  425. hr = HRESULT_FROM_WIN32 (dwErr);
  426. if ( E_ACCESSDENIED == hr )
  427. {
  428. m_bReadOnly = true;
  429. m_dwFlags |= CERT_STORE_READONLY_FLAG;
  430. }
  431. else
  432. {
  433. LPVOID lpMsgBuf = 0;
  434. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  435. NULL,
  436. dwErr,
  437. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  438. (LPWSTR) &lpMsgBuf, 0, NULL);
  439. // Display the string.
  440. CString caption;
  441. CString message;
  442. int iRetVal = 0;
  443. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  444. message.FormatMessage (IDS_CANT_SAVE_STORE, GetStoreName (), (LPWSTR) lpMsgBuf);
  445. if ( m_pConsole )
  446. m_pConsole->MessageBox ((LPCWSTR) message, (LPCWSTR) caption,
  447. MB_OK, &iRetVal);
  448. // Free the buffer.
  449. LocalFree (lpMsgBuf);
  450. }
  451. }
  452. }
  453. _TRACE (-1, L"Leaving CCertStore::Commit - %s\n",
  454. (LPCWSTR) m_pcszStoreName);
  455. return hr;
  456. }
  457. bool CCertStore::ContainsCertificates()
  458. {
  459. _TRACE (1, L"Entering CCertStore::ContainsCertificates - %s\n",
  460. (LPCWSTR) m_pcszStoreName);
  461. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  462. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  463. CERTMGR_LOG_STORE == m_objecttype ||
  464. CERTMGR_PHYS_STORE == m_objecttype);
  465. bool bResult = false;
  466. bResult = (GetCertCount () > 0);
  467. _TRACE (-1, L"Leaving CCertStore::ContainsCertificates - %s\n",
  468. (LPCWSTR) m_pcszStoreName);
  469. return bResult;
  470. }
  471. bool CCertStore::ContainsCRLs()
  472. {
  473. _TRACE (1, L"Entering CCertStore::ContainsCRLs - %s\n",
  474. (LPCWSTR) m_pcszStoreName);
  475. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  476. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  477. CERTMGR_LOG_STORE == m_objecttype ||
  478. CERTMGR_PHYS_STORE == m_objecttype);
  479. bool bResult = false;
  480. PCCRL_CONTEXT pCRLContext = EnumCRLs (0);
  481. if ( !pCRLContext )
  482. bResult = false;
  483. else
  484. {
  485. ::CertFreeCRLContext (pCRLContext);
  486. bResult = true;
  487. }
  488. Close ();
  489. _TRACE (-1, L"Leaving CCertStore::ContainsCRLs - %s\n",
  490. (LPCWSTR) m_pcszStoreName);
  491. return bResult;
  492. }
  493. bool CCertStore::ContainsCTLs()
  494. {
  495. _TRACE (1, L"Entering CCertStore::ContainsCTLs - %s\n",
  496. (LPCWSTR) m_pcszStoreName);
  497. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  498. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  499. CERTMGR_LOG_STORE == m_objecttype ||
  500. CERTMGR_PHYS_STORE == m_objecttype);
  501. bool bResult = false;
  502. PCCTL_CONTEXT pCTLContext = EnumCTLs (0);
  503. if ( !pCTLContext )
  504. bResult = false;
  505. else
  506. {
  507. ::CertFreeCTLContext (pCTLContext);
  508. bResult = true;
  509. }
  510. Close ();
  511. _TRACE (-1, L"Leaving CCertStore::ContainsCTLs - %s\n",
  512. (LPCWSTR) m_pcszStoreName);
  513. return bResult;
  514. }
  515. LPCWSTR CCertStore::GetLocalizedName()
  516. {
  517. // _TRACE (1, L"Entering CCertStore::GetLocalizedName - %s\n",
  518. // (LPCWSTR) m_pcszStoreName);
  519. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  520. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  521. CERTMGR_LOG_STORE == m_objecttype ||
  522. CERTMGR_PHYS_STORE == m_objecttype);
  523. if ( m_localizedName.IsEmpty () )
  524. {
  525. m_localizedName = ::CryptFindLocalizedName (GetObjectName ());
  526. if ( m_localizedName.IsEmpty () )
  527. {
  528. DWORD cbLocalizedName;
  529. HCERTSTORE hStore = 0;
  530. bool bStoreOpenedByThisMethod = false;
  531. // NTRAID# 366562 Safer: Inconsistent drag-and-drop behavior for certificate rules
  532. if ( m_hCertStore )
  533. hStore = m_hCertStore;
  534. else
  535. {
  536. hStore = GetStoreHandle ();
  537. bStoreOpenedByThisMethod = true;
  538. }
  539. if ( hStore )
  540. {
  541. if ( CertGetStoreProperty(
  542. hStore,
  543. CERT_STORE_LOCALIZED_NAME_PROP_ID,
  544. NULL,
  545. &cbLocalizedName))
  546. {
  547. LPWSTR pwszLocalizedName = new WCHAR[cbLocalizedName/sizeof (WCHAR)];
  548. if ( pwszLocalizedName )
  549. {
  550. ::ZeroMemory (pwszLocalizedName, cbLocalizedName);
  551. if ( CertGetStoreProperty(
  552. hStore,
  553. CERT_STORE_LOCALIZED_NAME_PROP_ID,
  554. pwszLocalizedName,
  555. &cbLocalizedName
  556. ))
  557. {
  558. m_localizedName = pwszLocalizedName;
  559. if ( m_localizedName == m_pcszStoreName )
  560. m_localizedName = GetObjectName ();
  561. }
  562. delete [] pwszLocalizedName;
  563. }
  564. }
  565. if ( bStoreOpenedByThisMethod )
  566. Close ();
  567. }
  568. }
  569. // If it's still empty, get the object name
  570. if ( m_localizedName.IsEmpty () )
  571. m_localizedName = GetObjectName ();
  572. }
  573. // _TRACE (-1, L"Leaving CCertStore::GetLocalizedName - %s\n",
  574. // (LPCWSTR) m_pcszStoreName);
  575. return (LPCWSTR) m_localizedName;
  576. }
  577. CString CCertStore::GetStoreName() const
  578. {
  579. // _TRACE (0, L"Entering and leaving CCertStore::GetStoreName - %s\n",
  580. // (LPCWSTR) m_pcszStoreName);
  581. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  582. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  583. CERTMGR_LOG_STORE == m_objecttype ||
  584. CERTMGR_PHYS_STORE == m_objecttype);
  585. return m_pcszStoreName;
  586. }
  587. HRESULT CCertStore::AddCertificateContext(
  588. PCCERT_CONTEXT pContext,
  589. LPCONSOLE pConsole,
  590. bool bDeletePrivateKey,
  591. PCCERT_CONTEXT* ppNewCertContext)
  592. {
  593. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  594. _TRACE (1, L"Entering CCertStore::AddCertificateContext - %s\n",
  595. (LPCWSTR) m_pcszStoreName);
  596. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  597. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  598. CERTMGR_LOG_STORE == m_objecttype ||
  599. CERTMGR_PHYS_STORE == m_objecttype);
  600. ASSERT (pContext);
  601. if ( !pContext )
  602. return E_POINTER;
  603. HRESULT hr = S_OK;
  604. HCERTSTORE hCertStore = GetStoreHandle (FALSE, &hr);
  605. if ( hCertStore )
  606. {
  607. PCCERT_CONTEXT pNewContext = 0;
  608. BOOL bResult = ::CertAddCertificateContextToStore (hCertStore,
  609. ::CertDuplicateCertificateContext (pContext), CERT_STORE_ADD_NEW,
  610. &pNewContext);
  611. if ( !bResult )
  612. {
  613. DWORD dwErr = GetLastError ();
  614. _TRACE (0, L"CertAddCertificateContextToStore () failed: 0x%x\n", dwErr);
  615. if ( CRYPT_E_EXISTS == dwErr )
  616. {
  617. if ( pConsole ) // if !pConsole then no popup is desired
  618. {
  619. CCertificate cert (pContext, this);
  620. CString text;
  621. CString caption;
  622. int iRetVal = 0;
  623. text.FormatMessage (IDS_DUPLICATE_CERT,
  624. (PCWSTR) GetLocalizedName (),
  625. (PCWSTR) cert.GetFriendlyName (),
  626. (PCWSTR) cert.GetSubjectName ());
  627. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  628. hr = pConsole->MessageBox (text, caption,
  629. MB_YESNO | MB_DEFBUTTON2, &iRetVal);
  630. if ( IDYES == iRetVal )
  631. {
  632. bResult = ::CertAddCertificateContextToStore (hCertStore,
  633. ::CertDuplicateCertificateContext (pContext),
  634. CERT_STORE_ADD_REPLACE_EXISTING,
  635. &pNewContext);
  636. if ( !bResult )
  637. {
  638. hr = HRESULT_FROM_WIN32 (GetLastError ());
  639. }
  640. }
  641. else
  642. hr = HRESULT_FROM_WIN32 (CRYPT_E_EXISTS);
  643. }
  644. }
  645. else
  646. {
  647. hr = HRESULT_FROM_WIN32 (dwErr);
  648. }
  649. }
  650. else
  651. {
  652. m_fCertCountValid = false;
  653. SetDirty ();
  654. ASSERT (pNewContext);
  655. if ( bDeletePrivateKey )
  656. {
  657. ::CertSetCertificateContextProperty (pNewContext,
  658. CERT_KEY_PROV_INFO_PROP_ID, 0, NULL);
  659. }
  660. }
  661. if ( pNewContext && ppNewCertContext)
  662. *ppNewCertContext = pNewContext;
  663. Close ();
  664. }
  665. else
  666. hr = E_FAIL;
  667. _TRACE (-1, L"Leaving CCertStore::AddCertificateContext - %s\n",
  668. (LPCWSTR) m_pcszStoreName);
  669. return hr;
  670. }
  671. void CCertStore::IncrementCertCount ()
  672. {
  673. m_nCertCount++;
  674. }
  675. int CCertStore::GetCertCount()
  676. {
  677. _TRACE (1, L"Entering CCertStore::GetCertCount - %s\n",
  678. (LPCWSTR) m_pcszStoreName);
  679. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  680. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  681. CERTMGR_LOG_STORE == m_objecttype ||
  682. CERTMGR_PHYS_STORE == m_objecttype);
  683. if ( !m_fCertCountValid )
  684. {
  685. PCCERT_CONTEXT pCertContext = 0;
  686. m_nCertCount = 0;
  687. while ( 1 )
  688. {
  689. pCertContext = EnumCertificates (pCertContext);
  690. if ( pCertContext )
  691. m_nCertCount++;
  692. else
  693. break;
  694. }
  695. Close ();
  696. m_fCertCountValid = true;
  697. }
  698. _TRACE (-1, L"Leaving CCertStore::GetCertCount - %s\n",
  699. (LPCWSTR) m_pcszStoreName);
  700. return m_nCertCount;
  701. }
  702. BOOL CCertStore::operator ==(CCertStore &rStore)
  703. {
  704. _TRACE (1, L"Entering CCertStore::operator == - %s\n",
  705. (LPCWSTR) m_pcszStoreName);
  706. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  707. CERTMGR_LOG_STORE == m_objecttype ||
  708. CERTMGR_PHYS_STORE == m_objecttype);
  709. BOOL bResult = FALSE;
  710. if ( GetStoreHandle () == rStore.GetStoreHandle () )
  711. bResult = TRUE;
  712. Close ();
  713. rStore.Close ();
  714. _TRACE (-1, L"Leaving CCertStore::operator == - %s\n",
  715. (LPCWSTR) m_pcszStoreName);
  716. return bResult;
  717. }
  718. CCertificate* CCertStore::GetSubjectCertificate(PCERT_INFO pCertId)
  719. {
  720. _TRACE (1, L"Entering CCertStore::GetSubjectCertificate - %s\n",
  721. (LPCWSTR) m_pcszStoreName);
  722. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  723. CERTMGR_LOG_STORE == m_objecttype ||
  724. CERTMGR_PHYS_STORE == m_objecttype);
  725. CCertificate* pCert = 0;
  726. HCERTSTORE hCertStore = GetStoreHandle ();
  727. if ( hCertStore )
  728. {
  729. PCCERT_CONTEXT pSignerCert =
  730. ::CertGetSubjectCertificateFromStore (
  731. hCertStore,
  732. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  733. pCertId);
  734. if ( pSignerCert )
  735. {
  736. pCert = new CCertificate (pSignerCert, this);
  737. }
  738. Close ();
  739. }
  740. _TRACE (-1, L"Leaving CCertStore::GetSubjectCertificate - %s\n",
  741. (LPCWSTR) m_pcszStoreName);
  742. return pCert;
  743. }
  744. PCCERT_CONTEXT CCertStore::EnumCertificates(PCCERT_CONTEXT pPrevCertContext)
  745. {
  746. // _TRACE (1, L"Entering CCertStore::EnumCertificates - %s\n",
  747. // (LPCWSTR) m_pcszStoreName);
  748. PCCERT_CONTEXT pCertContext = 0;
  749. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  750. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  751. CERTMGR_LOG_STORE == m_objecttype ||
  752. CERTMGR_PHYS_STORE == m_objecttype);
  753. HCERTSTORE hCertStore = GetStoreHandle ();
  754. if ( hCertStore )
  755. pCertContext = ::CertEnumCertificatesInStore (hCertStore, pPrevCertContext);
  756. m_fCertCountValid = false;
  757. // _TRACE (-1, L"Leaving CCertStore::EnumCertificates - %s\n",
  758. // (LPCWSTR) m_pcszStoreName);
  759. return pCertContext;
  760. }
  761. PCCTL_CONTEXT CCertStore::EnumCTLs(PCCTL_CONTEXT pPrevCtlContext)
  762. {
  763. _TRACE (1, L"Entering CCertStore::EnumCTLs - %s\n",
  764. (LPCWSTR) m_pcszStoreName);
  765. PCCTL_CONTEXT pCTLContext = 0;
  766. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  767. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  768. CERTMGR_LOG_STORE == m_objecttype ||
  769. CERTMGR_PHYS_STORE == m_objecttype);
  770. HCERTSTORE hCertStore = GetStoreHandle ();
  771. if ( hCertStore )
  772. pCTLContext = ::CertEnumCTLsInStore (hCertStore, pPrevCtlContext);
  773. _TRACE (-1, L"Leaving CCertStore::EnumCTLs - %s\n",
  774. (LPCWSTR) m_pcszStoreName);
  775. return pCTLContext;
  776. }
  777. PCCRL_CONTEXT CCertStore::EnumCRLs(PCCRL_CONTEXT pPrevCrlContext)
  778. {
  779. _TRACE (1, L"Entering CCertStore::EnumCRLs - %s\n",
  780. (LPCWSTR) m_pcszStoreName);
  781. PCCRL_CONTEXT pCRLContext = 0;
  782. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  783. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  784. CERTMGR_LOG_STORE == m_objecttype ||
  785. CERTMGR_PHYS_STORE == m_objecttype);
  786. HCERTSTORE hCertStore = GetStoreHandle ();
  787. if ( hCertStore )
  788. pCRLContext = ::CertEnumCRLsInStore (hCertStore, pPrevCrlContext);
  789. _TRACE (1, L"Entering CCertStore::EnumCRLs - %s\n",
  790. (LPCWSTR) m_pcszStoreName);
  791. return pCRLContext;
  792. }
  793. PCCERT_CONTEXT CCertStore::FindCertificate(
  794. DWORD dwFindFlags,
  795. DWORD dwFindType,
  796. const void * pvFindPara,
  797. PCCERT_CONTEXT pPrevCertContext)
  798. {
  799. _TRACE (1, L"Entering CCertStore::FindCertificate - %s\n",
  800. (LPCWSTR) m_pcszStoreName);
  801. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  802. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  803. CERTMGR_LOG_STORE == m_objecttype ||
  804. CERTMGR_PHYS_STORE == m_objecttype);
  805. PCCERT_CONTEXT pCertContext = 0;
  806. HCERTSTORE hCertStore = GetStoreHandle ();
  807. if ( hCertStore )
  808. pCertContext = ::CertFindCertificateInStore (hCertStore,
  809. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  810. dwFindFlags, dwFindType, pvFindPara, pPrevCertContext);
  811. _TRACE (-1, L"Leaving CCertStore::FindCertificate - %s\n",
  812. (LPCWSTR) m_pcszStoreName);
  813. return pCertContext;
  814. }
  815. void CCertStore::FinalCommit()
  816. {
  817. _TRACE (1, L"Entering CCertStore::FinalCommit - %s\n",
  818. (LPCWSTR) m_pcszStoreName);
  819. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  820. CERTMGR_LOG_STORE == m_objecttype ||
  821. CERTMGR_PHYS_STORE == m_objecttype);
  822. // Called only from destructor
  823. if ( m_hCertStore && m_bDirty )
  824. ::CertControlStore (m_hCertStore, 0, CERT_STORE_CTRL_COMMIT, NULL);
  825. _TRACE (-1, L"Leaving CCertStore::FinalCommit - %s\n",
  826. (LPCWSTR) m_pcszStoreName);
  827. }
  828. bool CCertStore::AddCTLContext(PCCTL_CONTEXT pCtlContext)
  829. {
  830. _TRACE (1, L"Entering CCertStore::AddCTLContext - %s\n",
  831. (LPCWSTR) m_pcszStoreName);
  832. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  833. CERTMGR_LOG_STORE == m_objecttype ||
  834. CERTMGR_PHYS_STORE == m_objecttype);
  835. BOOL bResult = FALSE;
  836. HCERTSTORE hCertStore = GetStoreHandle ();
  837. if ( hCertStore )
  838. {
  839. bResult = ::CertAddCTLContextToStore (hCertStore,
  840. pCtlContext,
  841. CERT_STORE_ADD_NEW,
  842. 0);
  843. Close ();
  844. }
  845. if ( bResult )
  846. SetDirty ();
  847. _TRACE (-1, L"Leaving CCertStore::AddCTLContext - %s\n",
  848. (LPCWSTR) m_pcszStoreName);
  849. return bResult ? true : false;
  850. }
  851. bool CCertStore::AddCRLContext(PCCRL_CONTEXT pCrlContext)
  852. {
  853. _TRACE (1, L"Entering CCertStore::AddCRLContext - %s\n",
  854. (LPCWSTR) m_pcszStoreName);
  855. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  856. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  857. CERTMGR_LOG_STORE == m_objecttype ||
  858. CERTMGR_PHYS_STORE == m_objecttype);
  859. BOOL bResult = FALSE;
  860. HCERTSTORE hCertStore = GetStoreHandle ();
  861. if ( hCertStore )
  862. {
  863. bResult = ::CertAddCRLContextToStore (hCertStore,
  864. pCrlContext,
  865. CERT_STORE_ADD_NEW,
  866. 0);
  867. Close ();
  868. }
  869. if ( bResult )
  870. SetDirty ();
  871. _TRACE (-1, L"Leaving CCertStore::AddCRLContext - %s\n",
  872. (LPCWSTR) m_pcszStoreName);
  873. return bResult ? true : false;
  874. }
  875. PCCRL_CONTEXT CCertStore::GetCRL(PCCERT_CONTEXT pIssuerContext, PCCRL_CONTEXT pPrevCrlContext, DWORD * pdwFlags)
  876. {
  877. _TRACE (1, L"Entering CCertStore::GetCRL - %s\n",
  878. (LPCWSTR) m_pcszStoreName);
  879. PCCRL_CONTEXT pCRLContext = 0;
  880. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  881. CERTMGR_LOG_STORE == m_objecttype ||
  882. CERTMGR_PHYS_STORE == m_objecttype);
  883. HCERTSTORE hCertStore = GetStoreHandle ();
  884. if ( hCertStore )
  885. {
  886. pCRLContext = ::CertGetCRLFromStore (hCertStore, pIssuerContext,
  887. pPrevCrlContext, pdwFlags);
  888. Close ();
  889. }
  890. _TRACE (-1, L"Leaving CCertStore::GetCRL - %s\n",
  891. (LPCWSTR) m_pcszStoreName);
  892. return pCRLContext;
  893. }
  894. HRESULT CCertStore::Resync()
  895. {
  896. _TRACE (1, L"Entering CCertStore::Resync - %s\n",
  897. (LPCWSTR) m_pcszStoreName);
  898. HRESULT hr = S_OK;
  899. // 256803 CertMgr: F5 refresh of Certificates Current User - Active
  900. // Directory User Object, overwrites UserCerificate DS object resulting
  901. // in data loss
  902. // Close (true);
  903. if ( (CERT_STORE_PROV_COLLECTION != m_storeProvider) &&
  904. !m_bReadOnly &&
  905. CERTMGR_LOG_STORE_RSOP != m_objecttype )
  906. {
  907. FinalCommit ();
  908. }
  909. HCERTSTORE hCertStore = GetStoreHandle (FALSE);
  910. if ( hCertStore )
  911. {
  912. BOOL bResult = ::CertControlStore (hCertStore, 0,
  913. CERT_STORE_CTRL_RESYNC, NULL);
  914. if ( !bResult )
  915. {
  916. DWORD dwErr = GetLastError ();
  917. ASSERT (ERROR_NOT_SUPPORTED == dwErr);
  918. if ( ERROR_NOT_SUPPORTED != dwErr )
  919. hr = HRESULT_FROM_WIN32 (dwErr);
  920. }
  921. }
  922. m_fCertCountValid = false;
  923. _TRACE (-1, L"Leaving CCertStore::Resync - %s\n",
  924. (LPCWSTR) m_pcszStoreName);
  925. return hr;
  926. }
  927. int CCertStore::GetCTLCount()
  928. {
  929. _TRACE (1, L"Entering CCertStore::GetCTLCount - %s\n",
  930. (LPCWSTR) m_pcszStoreName);
  931. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  932. CERTMGR_LOG_STORE == m_objecttype ||
  933. CERTMGR_PHYS_STORE == m_objecttype ||
  934. CERTMGR_LOG_STORE_RSOP == m_objecttype);
  935. int nCTLCount = 0;
  936. PCCTL_CONTEXT pCTLContext = 0;
  937. while ( 1 )
  938. {
  939. pCTLContext = EnumCTLs (pCTLContext);
  940. if ( pCTLContext )
  941. nCTLCount++;
  942. else
  943. break;
  944. }
  945. _TRACE (-1, L"Leaving CCertStore::GetCTLCount - %s\n",
  946. (LPCWSTR) m_pcszStoreName);
  947. return nCTLCount;
  948. }
  949. HRESULT CCertStore::AddStoreToCollection(HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
  950. {
  951. _TRACE (1, L"Entering CCertStore::AddStoreToCollection - %s\n",
  952. (LPCWSTR) m_pcszStoreName);
  953. HRESULT hr = S_OK;
  954. HCERTSTORE hCertStore = GetStoreHandle ();
  955. if ( hCertStore && hSiblingStore)
  956. {
  957. BOOL bResult = ::CertAddStoreToCollection (hCertStore,
  958. hSiblingStore,
  959. dwUpdateFlags,
  960. dwPriority);
  961. if ( bResult )
  962. SetDirty ();
  963. else
  964. {
  965. DWORD dwErr = GetLastError ();
  966. ASSERT (ERROR_NOT_SUPPORTED == dwErr);
  967. if ( ERROR_NOT_SUPPORTED != dwErr )
  968. hr = HRESULT_FROM_WIN32 (dwErr);
  969. }
  970. }
  971. else
  972. hr = E_FAIL;
  973. Close ();
  974. _TRACE (-1, L"Leaving CCertStore::AddStoreToCollection - %s\n",
  975. (LPCWSTR) m_pcszStoreName);
  976. return hr;
  977. }
  978. HRESULT CCertStore::AddStoreToCollection(CCertStore& siblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
  979. {
  980. _TRACE (1, L"Entering CCertStore::AddStoreToCollection - %s\n",
  981. (LPCWSTR) m_pcszStoreName);
  982. HRESULT hr = S_OK;
  983. HCERTSTORE hSiblingStore = siblingStore.GetStoreHandle ();
  984. if ( hSiblingStore)
  985. {
  986. hr = AddStoreToCollection (hSiblingStore, dwUpdateFlags, dwPriority);
  987. }
  988. else
  989. hr = E_FAIL;
  990. _TRACE (-1, L"Leaving CCertStore::AddStoreToCollection - %s\n",
  991. (LPCWSTR) m_pcszStoreName);
  992. return hr;
  993. }
  994. void CCertStore::SetDirty()
  995. {
  996. _TRACE (1, L"Entering CCertStore::SetDirty - %s\n",
  997. (LPCWSTR) m_pcszStoreName);
  998. m_bDirty = true;
  999. _TRACE (-1, L"Leaving CCertStore::SetDirty - %s\n",
  1000. (LPCWSTR) m_pcszStoreName);
  1001. }
  1002. void CCertStore::Close(bool bForceClose)
  1003. {
  1004. // _TRACE (1, L"Entering CCertStore::Close - %s\n",
  1005. // (LPCWSTR) m_pcszStoreName);
  1006. ASSERT (CERTMGR_LOG_STORE_GPE == m_objecttype ||
  1007. CERTMGR_LOG_STORE_RSOP == m_objecttype ||
  1008. CERTMGR_LOG_STORE == m_objecttype ||
  1009. CERTMGR_PHYS_STORE == m_objecttype);
  1010. if ( CERTMGR_LOG_STORE_GPE == m_objecttype || bForceClose )
  1011. {
  1012. if ( m_hCertStore && m_nLockCnt <= 0 )
  1013. {
  1014. if ( (CERT_STORE_PROV_COLLECTION != m_storeProvider) &&
  1015. !m_bReadOnly &&
  1016. CERTMGR_LOG_STORE_RSOP != m_objecttype )
  1017. {
  1018. FinalCommit ();
  1019. }
  1020. ::CertCloseStore (m_hCertStore, 0);
  1021. _TRACE (-1, L"Leaving%s store closed\n", (LPCWSTR) m_pcszStoreName);
  1022. m_hCertStore = 0;
  1023. m_bDirty = false;
  1024. }
  1025. }
  1026. // _TRACE (-1, L"Leaving CCertStore::Close - %s\n",
  1027. // (LPCWSTR) m_pcszStoreName);
  1028. }
  1029. BOOL CCertStore::AddEncodedCTL(DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded, DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
  1030. {
  1031. BOOL bResult = FALSE;
  1032. HCERTSTORE hCertStore = GetStoreHandle ();
  1033. if ( hCertStore )
  1034. {
  1035. bResult = ::CertAddEncodedCTLToStore (hCertStore,
  1036. dwMsgAndCertEncodingType,
  1037. pbCtlEncoded,
  1038. cbCtlEncoded,
  1039. dwAddDisposition, ppCtlContext);
  1040. if ( bResult )
  1041. {
  1042. m_bDirty = true;
  1043. Commit ();
  1044. }
  1045. Close ();
  1046. }
  1047. return bResult;
  1048. }
  1049. void CCertStore::InvalidateCertCount()
  1050. {
  1051. m_fCertCountValid = false;
  1052. }
  1053. void CCertStore::Lock()
  1054. {
  1055. m_nLockCnt++;
  1056. }
  1057. void CCertStore::Unlock()
  1058. {
  1059. ASSERT (m_nLockCnt > 0);
  1060. m_nLockCnt--;
  1061. }
  1062. ///////////////////////////////////////////////////////////////////////////////
  1063. ///////////////////////////////////////////////////////////////////////////////
  1064. CContainerCookie::CContainerCookie (CCertStore& rStore,
  1065. CertificateManagerObjectType objectType,
  1066. LPCWSTR lpcszMachineName,
  1067. LPCWSTR objectName)
  1068. : CCertMgrCookie (objectType, lpcszMachineName, objectName),
  1069. m_rCertStore (rStore)
  1070. {
  1071. m_rCertStore.AddRef ();
  1072. ASSERT (CERTMGR_CRL_CONTAINER == m_objecttype ||
  1073. CERTMGR_CTL_CONTAINER == m_objecttype ||
  1074. CERTMGR_CERT_CONTAINER == m_objecttype);
  1075. }
  1076. CContainerCookie::~CContainerCookie ()
  1077. {
  1078. ASSERT (CERTMGR_CRL_CONTAINER == m_objecttype ||
  1079. CERTMGR_CTL_CONTAINER == m_objecttype ||
  1080. CERTMGR_CERT_CONTAINER == m_objecttype);
  1081. m_rCertStore.Release ();
  1082. }
  1083. CCertStore& CContainerCookie::GetCertStore () const
  1084. {
  1085. ASSERT (CERTMGR_CRL_CONTAINER == m_objecttype ||
  1086. CERTMGR_CTL_CONTAINER == m_objecttype ||
  1087. CERTMGR_CERT_CONTAINER == m_objecttype);
  1088. return m_rCertStore;
  1089. }
  1090. HRESULT CContainerCookie::Commit()
  1091. {
  1092. ASSERT (CERTMGR_CRL_CONTAINER == m_objecttype ||
  1093. CERTMGR_CTL_CONTAINER == m_objecttype ||
  1094. CERTMGR_CERT_CONTAINER == m_objecttype);
  1095. CCertMgrCookie::Commit ();
  1096. return m_rCertStore.Commit ();
  1097. }
  1098. ///////////////////////////////////////////////////////////////////////////////
  1099. ///////////////////////////////////////////////////////////////////////////////
  1100. CUsageCookie::CUsageCookie (
  1101. CertificateManagerObjectType objecttype,
  1102. LPCWSTR lpcszMachineName,
  1103. LPCWSTR objectName)
  1104. : CCertMgrCookie (objecttype, lpcszMachineName, objectName),
  1105. m_OIDListPos (0),
  1106. m_nCertCount (0)
  1107. {
  1108. ASSERT (CERTMGR_USAGE == m_objecttype);
  1109. }
  1110. CUsageCookie::~CUsageCookie ()
  1111. {
  1112. ASSERT (CERTMGR_USAGE == m_objecttype);
  1113. LPSTR pszOID = 0;
  1114. while ( !m_OIDList.IsEmpty () )
  1115. {
  1116. pszOID = m_OIDList.RemoveHead ();
  1117. ASSERT (pszOID);
  1118. if ( pszOID )
  1119. delete [] pszOID;
  1120. }
  1121. }
  1122. void CUsageCookie::AddOID (LPCSTR pszNewOID)
  1123. {
  1124. ASSERT (CERTMGR_USAGE == m_objecttype);
  1125. LPSTR pszOID = new char[strlen (pszNewOID)+1];
  1126. if ( pszOID )
  1127. {
  1128. ::ZeroMemory (pszOID, strlen (pszNewOID) + 1);
  1129. strcpy (pszOID, pszNewOID);
  1130. m_OIDList.AddTail (pszOID);
  1131. }
  1132. }
  1133. LPSTR CUsageCookie::GetFirstOID ()
  1134. {
  1135. ASSERT (CERTMGR_USAGE == m_objecttype);
  1136. LPSTR pszOID = 0;
  1137. m_OIDListPos = m_OIDList.GetHeadPosition ();
  1138. if ( m_OIDListPos )
  1139. pszOID = m_OIDList.GetNext (m_OIDListPos);
  1140. return pszOID;
  1141. }
  1142. LPSTR CUsageCookie::GetNextOID ()
  1143. {
  1144. ASSERT (CERTMGR_USAGE == m_objecttype);
  1145. LPSTR pszOID = 0;
  1146. if ( m_OIDListPos )
  1147. pszOID = m_OIDList.GetNext (m_OIDListPos);
  1148. return pszOID;
  1149. }
  1150. int CUsageCookie::GetOIDCount () const
  1151. {
  1152. ASSERT (CERTMGR_USAGE == m_objecttype);
  1153. return (int)m_OIDList.GetCount ();
  1154. }
  1155. void CUsageCookie::SetCertCount(int nCertCount)
  1156. {
  1157. m_nCertCount = nCertCount;
  1158. }
  1159. int CUsageCookie::GetCertCount() const
  1160. {
  1161. return m_nCertCount;
  1162. }
  1163. ///////////////////////////////////////////////////////////////////////////////
  1164. ///////////////////////////////////////////////////////////////////////////////
  1165. SPECIAL_STORE_TYPE GetSpecialStoreType(LPWSTR pwszStoreName)
  1166. {
  1167. SPECIAL_STORE_TYPE storeType = NO_SPECIAL_TYPE;
  1168. if ( !_wcsicmp (pwszStoreName, MY_SYSTEM_STORE_NAME) )
  1169. storeType = MY_STORE;
  1170. else if ( !_wcsicmp (pwszStoreName, CA_SYSTEM_STORE_NAME) )
  1171. storeType = CA_STORE;
  1172. else if ( !_wcsicmp (pwszStoreName, ROOT_SYSTEM_STORE_NAME) )
  1173. storeType = ROOT_STORE;
  1174. else if ( !_wcsicmp (pwszStoreName, TRUST_SYSTEM_STORE_NAME) )
  1175. storeType = TRUST_STORE;
  1176. else if ( !_wcsicmp (pwszStoreName, USERDS_SYSTEM_STORE_NAME) )
  1177. storeType = USERDS_STORE;
  1178. else if ( !_wcsicmp (pwszStoreName, ACRS_SYSTEM_STORE_NAME) )
  1179. storeType = ACRS_STORE;
  1180. else if ( !_wcsicmp (pwszStoreName, REQUEST_SYSTEM_STORE_NAME) )
  1181. storeType = REQUEST_STORE;
  1182. else
  1183. {
  1184. // The stores might be concatenated with machine or services names.
  1185. // Check for the token preceded by a slash.
  1186. CString revStoreName (pwszStoreName);
  1187. revStoreName.MakeReverse ();
  1188. revStoreName.MakeUpper ();
  1189. CString revToken (MY_SYSTEM_STORE_NAME);
  1190. revToken.MakeReverse ();
  1191. revToken += L"\\";
  1192. if ( 0 == revStoreName.Find (revToken) )
  1193. {
  1194. storeType = MY_STORE;
  1195. goto Found;
  1196. }
  1197. revToken = CA_SYSTEM_STORE_NAME;
  1198. revToken.MakeReverse ();
  1199. revToken += L"\\";
  1200. if ( 0 == revStoreName.Find (revToken) )
  1201. {
  1202. storeType = CA_STORE;
  1203. goto Found;
  1204. }
  1205. revToken = ROOT_SYSTEM_STORE_NAME;
  1206. revToken.MakeReverse ();
  1207. revToken += L"\\";
  1208. if ( 0 == revStoreName.Find (revToken) )
  1209. {
  1210. storeType = ROOT_STORE;
  1211. goto Found;
  1212. }
  1213. revToken = TRUST_SYSTEM_STORE_NAME;
  1214. revToken.MakeReverse ();
  1215. revToken += L"\\";
  1216. if ( 0 == revStoreName.Find (revToken) )
  1217. {
  1218. storeType = TRUST_STORE;
  1219. goto Found;
  1220. }
  1221. revToken = USERDS_SYSTEM_STORE_NAME;
  1222. revToken.MakeReverse ();
  1223. revToken += L"\\";
  1224. if ( 0 == revStoreName.Find (revToken) )
  1225. {
  1226. storeType = USERDS_STORE;
  1227. goto Found;
  1228. }
  1229. revToken = ACRS_SYSTEM_STORE_NAME;
  1230. revToken.MakeReverse ();
  1231. revToken += L"\\";
  1232. if ( 0 == revStoreName.Find (revToken) )
  1233. {
  1234. storeType = ACRS_STORE;
  1235. goto Found;
  1236. }
  1237. revToken = REQUEST_SYSTEM_STORE_NAME;
  1238. revToken.MakeReverse ();
  1239. revToken += L"\\";
  1240. if ( 0 == revStoreName.Find (revToken) )
  1241. {
  1242. storeType = REQUEST_STORE;
  1243. goto Found;
  1244. }
  1245. }
  1246. Found:
  1247. return storeType;
  1248. }
  1249. ///////////////////////////////////////////////////////////////////////////////
  1250. ///////////////////////////////////////////////////////////////////////////////
  1251. CEnrollmentNodeCookie::CEnrollmentNodeCookie (
  1252. CertificateManagerObjectType objecttype,
  1253. LPCWSTR objectName,
  1254. IGPEInformation* pGPEInformation)
  1255. : CCertMgrCookie (objecttype, 0, objectName),
  1256. m_pGPEInformation (pGPEInformation)
  1257. {
  1258. if ( m_pGPEInformation )
  1259. m_pGPEInformation->AddRef ();
  1260. }
  1261. CEnrollmentNodeCookie::~CEnrollmentNodeCookie ()
  1262. {
  1263. if ( m_pGPEInformation )
  1264. m_pGPEInformation->Release ();
  1265. }
  1266. IGPEInformation* CEnrollmentNodeCookie::GetGPEInformation ()
  1267. {
  1268. return m_pGPEInformation;
  1269. }
  1270. void CCertMgrCookie::Refresh()
  1271. {
  1272. }
  1273. SPECIAL_STORE_TYPE StoreNameToType (const CString& szStoreName)
  1274. {
  1275. SPECIAL_STORE_TYPE type = NO_SPECIAL_TYPE;
  1276. if ( szStoreName == ACRS_SYSTEM_STORE_NAME )
  1277. type = ACRS_STORE;
  1278. else if ( szStoreName == ROOT_SYSTEM_STORE_NAME )
  1279. type = ROOT_STORE;
  1280. else if ( szStoreName == TRUST_SYSTEM_STORE_NAME )
  1281. type = TRUST_STORE;
  1282. else if ( szStoreName == EFS_SYSTEM_STORE_NAME )
  1283. type = EFS_STORE;
  1284. else if ( szStoreName == SAFER_TRUSTED_PUBLISHER_STORE_NAME )
  1285. type = SAFER_TRUSTED_PUBLISHER_STORE;
  1286. else if ( szStoreName == SAFER_DISALLOWED_STORE_NAME )
  1287. type = SAFER_DISALLOWED_STORE;
  1288. return type;
  1289. }