Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1530 lines
49 KiB

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