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.

769 lines
25 KiB

  1. //+---------------------------------------------------------------------------
  2. /////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Microsoft Windows
  5. // Copyright (C) Microsoft Corporation, 2000-2001.
  6. //
  7. // File: SaferEntryCertificatePropertyPage.cpp
  8. //
  9. // Contents: Implementation of CSaferEntryCertificatePropertyPage
  10. //
  11. //----------------------------------------------------------------------------
  12. // SaferEntryCertificatePropertyPage.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include <gpedit.h>
  16. #include "compdata.h"
  17. #include "certmgr.h"
  18. #include "SaferEntryCertificatePropertyPage.h"
  19. #include "SaferUtil.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CSaferEntryCertificatePropertyPage property page
  27. CSaferEntryCertificatePropertyPage::CSaferEntryCertificatePropertyPage(
  28. CSaferEntry& rSaferEntry,
  29. CSaferEntries* pSaferEntries,
  30. LONG_PTR lNotifyHandle,
  31. LPDATAOBJECT pDataObject,
  32. bool bReadOnly,
  33. CCertMgrComponentData* pCompData,
  34. bool bNew,
  35. IGPEInformation* pGPEInformation,
  36. bool bIsMachine) :
  37. CHelpPropertyPage(CSaferEntryCertificatePropertyPage::IDD),
  38. m_rSaferEntry (rSaferEntry),
  39. m_bStoresEnumerated (false),
  40. m_bCertificateChanged (false),
  41. m_pCertContext (0),
  42. m_pSaferEntries (pSaferEntries),
  43. m_bDirty (bNew),
  44. m_pOriginalStore (0),
  45. m_lNotifyHandle (lNotifyHandle),
  46. m_pDataObject (pDataObject),
  47. m_bReadOnly (bReadOnly),
  48. m_pCompData (pCompData),
  49. m_pGPEInformation (pGPEInformation),
  50. m_bIsMachine (bIsMachine),
  51. m_bFirst (true)
  52. {
  53. //{{AFX_DATA_INIT(CSaferEntryCertificatePropertyPage)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. m_rSaferEntry.AddRef ();
  57. m_rSaferEntry.IncrementOpenPageCount ();
  58. ::ZeroMemory (&m_selCertStruct, sizeof (m_selCertStruct));
  59. if ( m_pSaferEntries )
  60. m_pSaferEntries->AddRef ();
  61. if ( m_pCompData )
  62. m_pCompData->AddRef ();
  63. }
  64. CSaferEntryCertificatePropertyPage::~CSaferEntryCertificatePropertyPage()
  65. {
  66. m_rSaferEntry.DecrementOpenPageCount ();
  67. m_rSaferEntry.Release ();
  68. // Clean up enumerated store list
  69. for (DWORD dwIndex = 0; dwIndex < m_selCertStruct.cDisplayStores; dwIndex++)
  70. {
  71. ASSERT (m_selCertStruct.rghDisplayStores);
  72. if ( m_selCertStruct.rghDisplayStores[dwIndex] )
  73. ::CertCloseStore (m_selCertStruct.rghDisplayStores[dwIndex], CERT_CLOSE_STORE_FORCE_FLAG);
  74. }
  75. if ( m_selCertStruct.rghDisplayStores )
  76. delete [] m_selCertStruct.rghDisplayStores;
  77. // if ( m_pCertContext )
  78. // CertFreeCertificateContext (m_pCertContext);
  79. if ( m_pCompData )
  80. m_pCompData->Release ();
  81. if ( m_pSaferEntries )
  82. m_pSaferEntries->Release ();
  83. if ( m_pOriginalStore )
  84. m_pOriginalStore->Release ();
  85. if ( m_lNotifyHandle )
  86. MMCFreeNotifyHandle (m_lNotifyHandle);
  87. }
  88. void CSaferEntryCertificatePropertyPage::DoDataExchange(CDataExchange* pDX)
  89. {
  90. CHelpPropertyPage::DoDataExchange(pDX);
  91. //{{AFX_DATA_MAP(CSaferEntryCertificatePropertyPage)
  92. DDX_Control(pDX, IDC_CERT_ENTRY_DESCRIPTION, m_descriptionEdit);
  93. DDX_Control(pDX, IDC_CERT_ENTRY_SECURITY_LEVEL, m_securityLevelCombo);
  94. //}}AFX_DATA_MAP
  95. }
  96. BEGIN_MESSAGE_MAP(CSaferEntryCertificatePropertyPage, CHelpPropertyPage)
  97. //{{AFX_MSG_MAP(CSaferEntryCertificatePropertyPage)
  98. ON_BN_CLICKED(IDC_CERT_ENTRY_BROWSE, OnCertEntryBrowse)
  99. ON_EN_CHANGE(IDC_CERT_ENTRY_DESCRIPTION, OnChangeCertEntryDescription)
  100. ON_CBN_SELCHANGE(IDC_CERT_ENTRY_SECURITY_LEVEL, OnSelchangeCertEntrySecurityLevel)
  101. ON_BN_CLICKED(IDC_SAFER_CERT_VIEW, OnSaferCertView)
  102. ON_EN_SETFOCUS(IDC_CERT_ENTRY_SUBJECT_NAME, OnSetfocusCertEntrySubjectName)
  103. //}}AFX_MSG_MAP
  104. END_MESSAGE_MAP()
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CSaferEntryCertificatePropertyPage message handlers
  107. void CSaferEntryCertificatePropertyPage::DoContextHelp (HWND hWndControl)
  108. {
  109. _TRACE (1, L"Entering CSaferEntryCertificatePropertyPage::DoContextHelp\n");
  110. static const DWORD help_map[] =
  111. {
  112. IDC_CERT_ENTRY_SUBJECT_NAME, IDH_CERT_ENTRY_SUBJECT_NAME,
  113. IDC_CERT_ENTRY_BROWSE, IDH_CERT_ENTRY_BROWSE,
  114. IDC_CERT_ENTRY_SECURITY_LEVEL, IDH_CERT_ENTRY_SECURITY_LEVEL,
  115. IDC_CERT_ENTRY_DESCRIPTION, IDH_CERT_ENTRY_DESCRIPTION,
  116. IDC_CERT_ENTRY_LAST_MODIFIED, IDH_CERT_ENTRY_LAST_MODIFIED,
  117. 0, 0
  118. };
  119. switch (::GetDlgCtrlID (hWndControl))
  120. {
  121. case IDC_CERT_ENTRY_SUBJECT_NAME:
  122. case IDC_CERT_ENTRY_BROWSE:
  123. case IDC_CERT_ENTRY_SECURITY_LEVEL:
  124. case IDC_CERT_ENTRY_DESCRIPTION:
  125. case IDC_CERT_ENTRY_LAST_MODIFIED:
  126. if ( !::WinHelp (
  127. hWndControl,
  128. GetF1HelpFilename(),
  129. HELP_WM_HELP,
  130. (DWORD_PTR) help_map) )
  131. {
  132. _TRACE (0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. _TRACE (-1, L"Leaving CSaferEntryCertificatePropertyPage::DoContextHelp\n");
  139. }
  140. BOOL CSaferEntryCertificatePropertyPage::OnInitDialog()
  141. {
  142. CHelpPropertyPage::OnInitDialog();
  143. HRESULT hr = S_OK;
  144. DWORD dwLevelID = m_rSaferEntry.GetLevel ();
  145. ASSERT (SAFER_LEVELID_FULLYTRUSTED == dwLevelID || SAFER_LEVELID_DISALLOWED == dwLevelID);
  146. switch (dwLevelID)
  147. {
  148. case SAFER_LEVELID_FULLYTRUSTED:
  149. hr = m_pSaferEntries->GetTrustedPublishersStore (&m_pOriginalStore);
  150. break;
  151. case SAFER_LEVELID_DISALLOWED:
  152. hr = m_pSaferEntries->GetDisallowedStore (&m_pOriginalStore);
  153. break;
  154. default:
  155. break;
  156. }
  157. CPolicyKey policyKey (m_pGPEInformation,
  158. SAFER_HKLM_REGBASE,
  159. m_bIsMachine);
  160. InitializeSecurityLevelComboBox (m_securityLevelCombo, true, dwLevelID,
  161. policyKey.GetKey (), m_pCompData->m_pdwSaferLevels,
  162. m_bIsMachine);
  163. m_descriptionEdit.LimitText (SAFER_MAX_DESCRIPTION_SIZE);
  164. m_descriptionEdit.SetWindowText (m_rSaferEntry.GetDescription ());
  165. SetDlgItemText (IDC_CERT_ENTRY_LAST_MODIFIED, m_rSaferEntry.GetLongLastModified ());
  166. CCertificate* pCert = 0;
  167. hr = m_rSaferEntry.GetCertificate (&pCert);
  168. if ( SUCCEEDED (hr) && pCert )
  169. {
  170. m_pCertContext = CertDuplicateCertificateContext (pCert->GetCertContext ());
  171. if ( m_pCertContext )
  172. SetDlgItemText (IDC_CERT_ENTRY_SUBJECT_NAME, ::GetNameString (m_pCertContext, 0));
  173. pCert->Release ();
  174. }
  175. if ( !m_pCertContext )
  176. GetDlgItem (IDC_SAFER_CERT_VIEW)->EnableWindow (FALSE);
  177. if ( m_bReadOnly )
  178. {
  179. m_descriptionEdit.EnableWindow (FALSE);
  180. m_securityLevelCombo.EnableWindow (FALSE);
  181. GetDlgItem (IDC_CERT_ENTRY_BROWSE)->EnableWindow (FALSE);
  182. }
  183. if ( !m_bDirty )
  184. {
  185. CString szText;
  186. VERIFY (szText.LoadString (IDS_CERTIFICATE_TITLE));
  187. SetDlgItemText (IDC_CERTIFICATE_TITLE, szText);
  188. }
  189. else
  190. {
  191. SetDlgItemText (IDC_DATE_LAST_MODIFIED_LABEL, L"");
  192. GetDlgItem (IDC_CERT_ENTRY_LAST_MODIFIED)->ShowWindow (SW_HIDE);
  193. }
  194. return TRUE; // return TRUE unless you set the focus to a control
  195. // EXCEPTION: OCX Property Pages should return FALSE
  196. }
  197. typedef struct _ENUM_ARG {
  198. DWORD dwFlags;
  199. DWORD* pcDisplayStores;
  200. HCERTSTORE ** prghDisplayStores;
  201. } ENUM_ARG, *PENUM_ARG;
  202. static BOOL WINAPI EnumSaferStoresSysCallback(
  203. IN const void* pwszSystemStore,
  204. IN DWORD /*dwFlags*/,
  205. IN PCERT_SYSTEM_STORE_INFO /*pStoreInfo*/,
  206. IN OPTIONAL void * /*pvReserved*/,
  207. IN OPTIONAL void *pvArg
  208. )
  209. {
  210. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  211. void* pvPara = (void*)pwszSystemStore;
  212. HCERTSTORE hNewStore = ::CertOpenStore (CERT_STORE_PROV_SYSTEM,
  213. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, NULL,
  214. CERT_SYSTEM_STORE_CURRENT_USER, pvPara);
  215. if ( !hNewStore )
  216. {
  217. hNewStore = ::CertOpenStore (CERT_STORE_PROV_SYSTEM,
  218. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, NULL,
  219. CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_READONLY_FLAG, pvPara);
  220. }
  221. if ( hNewStore )
  222. {
  223. DWORD dwCnt = *(pEnumArg->pcDisplayStores);
  224. HCERTSTORE* phStores = 0;
  225. phStores = new HCERTSTORE[dwCnt+1];
  226. if ( phStores )
  227. {
  228. DWORD dwIndex = 0;
  229. if ( *(pEnumArg->prghDisplayStores) )
  230. {
  231. for (; dwIndex < dwCnt; dwIndex++)
  232. {
  233. phStores[dwIndex] = (*(pEnumArg->prghDisplayStores))[dwIndex];
  234. }
  235. delete [] (*(pEnumArg->prghDisplayStores));
  236. }
  237. (*(pEnumArg->pcDisplayStores))++;
  238. (*(pEnumArg->prghDisplayStores)) = phStores;
  239. (*(pEnumArg->prghDisplayStores))[dwIndex] = hNewStore;
  240. }
  241. else
  242. {
  243. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  244. return FALSE;
  245. }
  246. }
  247. return TRUE;
  248. }
  249. void CSaferEntryCertificatePropertyPage::OnCertEntryBrowse()
  250. {
  251. CString szFileFilter;
  252. VERIFY (szFileFilter.LoadString (IDS_SAFER_CERTFILEFILTER));
  253. // replace "|" with 0;
  254. const size_t nFilterLen = wcslen (szFileFilter) + 1;
  255. PWSTR pszFileFilter = new WCHAR [nFilterLen];
  256. if ( pszFileFilter )
  257. {
  258. wcscpy (pszFileFilter, szFileFilter);
  259. for (int nIndex = 0; nIndex < nFilterLen; nIndex++)
  260. {
  261. if ( L'|' == pszFileFilter[nIndex] )
  262. pszFileFilter[nIndex] = 0;
  263. }
  264. WCHAR szFile[MAX_PATH];
  265. ::ZeroMemory (szFile, MAX_PATH * sizeof (WCHAR));
  266. OPENFILENAME ofn;
  267. ::ZeroMemory (&ofn, sizeof (OPENFILENAME));
  268. ofn.lStructSize = sizeof (OPENFILENAME);
  269. ofn.hwndOwner = m_hWnd;
  270. ofn.lpstrFilter = (PCWSTR) pszFileFilter;
  271. ofn.lpstrFile = szFile;
  272. ofn.nMaxFile = MAX_PATH;
  273. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  274. BOOL bResult = ::GetOpenFileName (&ofn);
  275. if ( bResult )
  276. {
  277. CString szFileName = ofn.lpstrFile;
  278. //
  279. // Open cert store from the file
  280. //
  281. HCERTSTORE hCertStore = NULL;
  282. PVOID FileNameVoidP = (PVOID) (LPCWSTR)szFileName;
  283. PCCERT_CONTEXT pCertContext = NULL;
  284. DWORD dwEncodingType = 0;
  285. DWORD dwContentType = 0;
  286. DWORD dwFormatType = 0;
  287. BOOL bReturn = ::CryptQueryObject (
  288. CERT_QUERY_OBJECT_FILE,
  289. FileNameVoidP,
  290. CERT_QUERY_CONTENT_FLAG_ALL,
  291. CERT_QUERY_FORMAT_FLAG_ALL,
  292. 0,
  293. &dwEncodingType,
  294. &dwContentType,
  295. &dwFormatType,
  296. &hCertStore,
  297. NULL,
  298. (const void **)&pCertContext);
  299. if ( bReturn )
  300. {
  301. //
  302. // Success. See what we get back. A store or a cert.
  303. //
  304. if ( (dwContentType == CERT_QUERY_CONTENT_SERIALIZED_STORE)
  305. && hCertStore)
  306. {
  307. CERT_ENHKEY_USAGE enhKeyUsage;
  308. ::ZeroMemory (&enhKeyUsage, sizeof (CERT_ENHKEY_USAGE));
  309. enhKeyUsage.cUsageIdentifier = 1;
  310. enhKeyUsage.rgpszUsageIdentifier[0] = szOID_EFS_RECOVERY;
  311. //
  312. // We get the certificate store
  313. //
  314. pCertContext = ::CertFindCertificateInStore (
  315. hCertStore,
  316. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  317. 0,
  318. CERT_FIND_ENHKEY_USAGE,
  319. &enhKeyUsage,
  320. NULL);
  321. if ( !pCertContext )
  322. {
  323. CString caption;
  324. CString text;
  325. CThemeContextActivator activator;
  326. VERIFY (text.LoadString (IDS_FILE_CONTAINS_NO_CERT));
  327. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  328. MessageBox (text, caption, MB_OK);
  329. return;
  330. }
  331. if ( hCertStore )
  332. ::CertCloseStore (hCertStore, 0);
  333. }
  334. else if ( (dwContentType != CERT_QUERY_CONTENT_CERT) || !pCertContext )
  335. {
  336. //
  337. // Neither a valid cert file nor a store file we like.
  338. //
  339. if ( hCertStore )
  340. ::CertCloseStore (hCertStore, 0);
  341. if ( pCertContext )
  342. ::CertFreeCertificateContext (pCertContext);
  343. CString ErrMsg;
  344. CThemeContextActivator activator;
  345. VERIFY (ErrMsg.LoadString (IDS_CERTFILEFORMATERR));
  346. MessageBox (ErrMsg);
  347. return;
  348. }
  349. if ( pCertContext )
  350. {
  351. if ( m_pCertContext )
  352. CertFreeCertificateContext (m_pCertContext);
  353. m_pCertContext = pCertContext;
  354. if ( m_pCertContext )
  355. {
  356. SetDlgItemText (IDC_CERT_ENTRY_SUBJECT_NAME, ::GetNameString (m_pCertContext, 0));
  357. GetDlgItem (IDC_SAFER_CERT_VIEW)->EnableWindow (TRUE);
  358. }
  359. m_bCertificateChanged = true;
  360. m_bDirty = true;
  361. SetModified ();
  362. }
  363. if ( hCertStore )
  364. {
  365. ::CertCloseStore (hCertStore, 0);
  366. hCertStore = NULL;
  367. }
  368. }
  369. else
  370. {
  371. //
  372. // Fail. Get the error code.
  373. //
  374. DWORD dwErr = GetLastError ();
  375. CString text;
  376. CString caption;
  377. CThemeContextActivator activator;
  378. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  379. text.FormatMessage (IDS_CERTFILEOPENERR, szFileName,
  380. GetSystemMessage (dwErr));
  381. MessageBox (text, caption);
  382. }
  383. }
  384. delete [] pszFileFilter;
  385. }
  386. }
  387. BOOL CSaferEntryCertificatePropertyPage::OnApply()
  388. {
  389. if ( !m_bReadOnly )
  390. {
  391. ASSERT (m_pSaferEntries);
  392. if ( !m_pSaferEntries )
  393. return FALSE;
  394. CThemeContextActivator activator;
  395. if ( !m_pCertContext )
  396. {
  397. CString text;
  398. CString caption;
  399. VERIFY (text.LoadString (IDS_SAFER_MUST_CHOOSE_CERT));
  400. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  401. MessageBox (text, caption, MB_OK);
  402. GetDlgItem (IDC_CERT_ENTRY_BROWSE)->SetFocus ();
  403. return FALSE;
  404. }
  405. if ( m_bDirty )
  406. {
  407. int nCurSel = m_securityLevelCombo.GetCurSel ();
  408. ASSERT (CB_ERR < nCurSel);
  409. if ( CB_ERR < nCurSel )
  410. {
  411. CCertStore* pTrustedPublishersStore = 0;
  412. HRESULT hr = m_pSaferEntries->GetTrustedPublishersStore (&pTrustedPublishersStore);
  413. ASSERT (SUCCEEDED (hr));
  414. if ( SUCCEEDED (hr) )
  415. {
  416. CCertStore* pDisallowedStore = 0;
  417. hr = m_pSaferEntries->GetDisallowedStore (&pDisallowedStore);
  418. ASSERT (SUCCEEDED (hr));
  419. if ( SUCCEEDED (hr) )
  420. {
  421. DWORD_PTR dwLevel = m_securityLevelCombo.GetItemData (nCurSel);
  422. m_rSaferEntry.SetLevel ((DWORD) dwLevel);
  423. CCertStore* pStore = (SAFER_LEVELID_FULLYTRUSTED == dwLevel) ?
  424. pTrustedPublishersStore : pDisallowedStore;
  425. CCertificate* pCert = 0;
  426. hr = m_rSaferEntry.GetCertificate (&pCert);
  427. if ( E_NOTIMPL == hr )
  428. {
  429. // This is a new entry
  430. if ( m_pOriginalStore )
  431. m_pOriginalStore->Release ();
  432. m_pOriginalStore = pStore;
  433. m_pOriginalStore->AddRef ();
  434. CCertificate* pNewCert = new CCertificate (
  435. m_pCertContext,
  436. pStore);
  437. if ( pNewCert )
  438. {
  439. hr = m_rSaferEntry.SetCertificate (pNewCert);
  440. }
  441. else
  442. hr = E_OUTOFMEMORY;
  443. if ( SUCCEEDED (hr) )
  444. {
  445. CString szDescription;
  446. m_descriptionEdit.GetWindowText (szDescription);
  447. m_rSaferEntry.SetDescription (szDescription);
  448. hr = m_rSaferEntry.Save ();
  449. if ( SUCCEEDED (hr) )
  450. {
  451. pStore->Commit ();
  452. if ( m_lNotifyHandle )
  453. MMCPropertyChangeNotify (
  454. m_lNotifyHandle, // handle to a notification
  455. (LPARAM) m_pDataObject); // unique identifier
  456. m_bDirty = false;
  457. }
  458. else
  459. {
  460. CString text;
  461. CString caption;
  462. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  463. text.FormatMessage (IDS_ERROR_SAVING_ENTRY, GetSystemMessage (hr));
  464. MessageBox (text, caption, MB_OK);
  465. }
  466. }
  467. }
  468. else
  469. {
  470. // We're modifying an existing entry
  471. ASSERT (m_pSaferEntries);
  472. if ( m_pSaferEntries )
  473. {
  474. // 1. If original cert has been changed, it must be removed from its
  475. // store and the new one added to the appropriate store
  476. // 2. If the security level was changed. The cert
  477. // removed from the original store, which must be Committed and
  478. // released. The cert must then be added to the new store.
  479. // 3. If both the cert and the level have been changed, same as step 2.
  480. if ( m_bCertificateChanged )
  481. {
  482. CCertificate* pNewCert = new CCertificate (
  483. ::CertDuplicateCertificateContext (m_pCertContext),
  484. pStore);
  485. if ( pNewCert )
  486. {
  487. hr = m_rSaferEntry.SetCertificate (pNewCert);
  488. }
  489. }
  490. }
  491. CString szDescription;
  492. m_descriptionEdit.GetWindowText (szDescription);
  493. m_rSaferEntry.SetDescription (szDescription);
  494. hr = m_rSaferEntry.SetLevel ((DWORD) dwLevel);
  495. if ( SUCCEEDED (hr) )
  496. {
  497. hr = m_rSaferEntry.Save ();
  498. if ( SUCCEEDED (hr) )
  499. {
  500. pDisallowedStore->Commit ();
  501. pTrustedPublishersStore->Commit ();
  502. if ( m_lNotifyHandle )
  503. MMCPropertyChangeNotify (
  504. m_lNotifyHandle, // handle to a notification
  505. (LPARAM) m_pDataObject); // unique identifier
  506. m_bDirty = false;
  507. }
  508. else
  509. {
  510. CString text;
  511. CString caption;
  512. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  513. text.FormatMessage (IDS_ERROR_SAVING_ENTRY, GetSystemMessage (hr));
  514. MessageBox (text, caption, MB_OK);
  515. }
  516. }
  517. }
  518. pDisallowedStore->Release ();
  519. }
  520. pTrustedPublishersStore->Release ();
  521. }
  522. }
  523. }
  524. }
  525. if ( !m_bDirty )
  526. return CHelpPropertyPage::OnApply();
  527. else
  528. return FALSE;
  529. }
  530. void CSaferEntryCertificatePropertyPage::OnChangeCertEntryDescription()
  531. {
  532. m_bDirty = true;
  533. SetModified ();
  534. }
  535. void CSaferEntryCertificatePropertyPage::OnSelchangeCertEntrySecurityLevel()
  536. {
  537. m_bDirty = true;
  538. SetModified ();
  539. }
  540. void CSaferEntryCertificatePropertyPage::OnSaferCertView()
  541. {
  542. LaunchCommonCertDialog ();
  543. }
  544. void CSaferEntryCertificatePropertyPage::LaunchCommonCertDialog ()
  545. {
  546. _TRACE (1, L"Entering CSaferEntryCertificatePropertyPage::LaunchCommonCertDialog\n");
  547. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  548. if ( !m_pCertContext )
  549. return;
  550. HRESULT hr = S_OK;
  551. CWaitCursor waitCursor;
  552. CTypedPtrList<CPtrList, CCertStore*> storeList;
  553. // Add the Root store first on a remote machine.
  554. if ( !IsLocalComputername (m_pCompData->GetManagedComputer ()) )
  555. {
  556. storeList.AddTail (new CCertStore (CERTMGR_LOG_STORE,
  557. CERT_STORE_PROV_SYSTEM,
  558. CERT_SYSTEM_STORE_LOCAL_MACHINE,
  559. (LPCWSTR) m_pCompData->GetManagedComputer (),
  560. ROOT_SYSTEM_STORE_NAME,
  561. ROOT_SYSTEM_STORE_NAME,
  562. _T (""), ROOT_STORE,
  563. CERT_SYSTEM_STORE_LOCAL_MACHINE,
  564. m_pCompData->m_pConsole));
  565. }
  566. hr = m_pCompData->EnumerateLogicalStores (&storeList);
  567. if ( SUCCEEDED (hr) )
  568. {
  569. POSITION pos = 0;
  570. POSITION prevPos = 0;
  571. // Validate store handles
  572. for (pos = storeList.GetHeadPosition ();
  573. pos;)
  574. {
  575. prevPos = pos;
  576. CCertStore* pStore = storeList.GetNext (pos);
  577. ASSERT (pStore);
  578. if ( pStore )
  579. {
  580. // Do not open the userDS store
  581. if ( USERDS_STORE == pStore->GetStoreType () )
  582. {
  583. storeList.RemoveAt (prevPos);
  584. pStore->Release ();
  585. pStore = 0;
  586. }
  587. else
  588. {
  589. if ( !pStore->GetStoreHandle () )
  590. {
  591. CString caption;
  592. CString text;
  593. CThemeContextActivator activator;
  594. text.FormatMessage (IDS_CANT_OPEN_STORE_AND_FAIL, pStore->GetLocalizedName ());
  595. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  596. MessageBox (text, caption, MB_ICONWARNING | MB_OK);
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. // Proceed only if all handles are valid
  603. if ( SUCCEEDED (hr) )
  604. {
  605. CRYPTUI_VIEWCERTIFICATE_STRUCT vcs;
  606. ::ZeroMemory (&vcs, sizeof (vcs));
  607. vcs.dwSize = sizeof (vcs);
  608. vcs.hwndParent = m_hWnd;
  609. // Set these flags only on a remote machine.
  610. if ( !IsLocalComputername (m_pCompData->GetManagedComputer ()) )
  611. vcs.dwFlags = CRYPTUI_DONT_OPEN_STORES | CRYPTUI_WARN_UNTRUSTED_ROOT;
  612. else
  613. vcs.dwFlags = 0;
  614. vcs.dwFlags |= CRYPTUI_DISABLE_EDITPROPERTIES;
  615. vcs.pCertContext = m_pCertContext;
  616. vcs.cStores = (DWORD)storeList.GetCount ();
  617. vcs.rghStores = new HCERTSTORE[vcs.cStores];
  618. if ( vcs.rghStores )
  619. {
  620. CCertStore* pStore = 0;
  621. DWORD index = 0;
  622. for (pos = storeList.GetHeadPosition ();
  623. pos && index < vcs.cStores;
  624. index++)
  625. {
  626. pStore = storeList.GetNext (pos);
  627. ASSERT (pStore);
  628. if ( pStore )
  629. {
  630. vcs.rghStores[index] = pStore->GetStoreHandle ();
  631. }
  632. }
  633. BOOL fPropertiesChanged = FALSE;
  634. _TRACE (0, L"Calling CryptUIDlgViewCertificate()\n");
  635. CThemeContextActivator activator;
  636. ::CryptUIDlgViewCertificate (&vcs, &fPropertiesChanged);
  637. delete vcs.rghStores;
  638. }
  639. else
  640. hr = E_OUTOFMEMORY;
  641. }
  642. while (!storeList.IsEmpty () )
  643. {
  644. CCertStore* pStore = storeList.RemoveHead ();
  645. if ( pStore )
  646. {
  647. pStore->Close ();
  648. pStore->Release ();
  649. }
  650. }
  651. }
  652. _TRACE (-1, L"Leaving CSaferEntryCertificatePropertyPage::LaunchCommonCertDialog: 0x%x\n", hr);
  653. }
  654. void CSaferEntryCertificatePropertyPage::OnSetfocusCertEntrySubjectName()
  655. {
  656. if ( m_bFirst )
  657. {
  658. SendDlgItemMessage (IDC_CERT_ENTRY_SUBJECT_NAME, EM_SETSEL, (WPARAM) 0, 0);
  659. m_bFirst = false;
  660. }
  661. }