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.

2572 lines
73 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2001
  5. //
  6. // File: FindDlg.cpp
  7. //
  8. // Contents: implementation Find certificate dialog
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #pragma warning(push, 3)
  14. #include <process.h>
  15. #pragma warning(pop)
  16. #include "mbstring.h"
  17. #include "FindDlg.h"
  18. #include "cookie.h"
  19. #include <wintrust.h>
  20. #include <cryptui.h>
  21. #include "compdata.h"
  22. USE_HANDLE_MACROS("CERTMGR(finddlg.cpp)")
  23. #ifdef _DEBUG
  24. #ifndef ALPHA
  25. #define new DEBUG_NEW
  26. #endif
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CFindDialog dialog
  32. const WCHAR chLEFT_TO_RIGHT = 0x200e;
  33. CFindDialog::CFindDialog (CWnd* pParent,
  34. const CString &pcszMachineName,
  35. const CString &szFileName,
  36. CCertMgrComponentData* pCompData)
  37. : CHelpDialog(CFindDialog::IDD, pParent),
  38. m_cyOriginal (0),
  39. m_fWindowExpandedOnce (false),
  40. m_cyMin (0),
  41. m_cxBtnMargin (0),
  42. m_cxMin (0),
  43. m_cxAnimMargin (0),
  44. m_cxStoreListMargin (0),
  45. m_cxContainMargin (0),
  46. m_cxTabMargin (0),
  47. m_cxFieldListMargin (0),
  48. m_cxResultListMargin (0),
  49. m_szMachineName (pcszMachineName),
  50. m_bAnimationRunning (false),
  51. m_szFileName (szFileName),
  52. m_hSearchThread (0),
  53. m_singleLock (&m_critSec, FALSE),
  54. m_pCompData (pCompData),
  55. m_bConsoleRefreshRequired (false),
  56. m_hCancelSearchEvent (0),
  57. m_bInitComplete (false)
  58. {
  59. _TRACE (1, L"Entering CFindDialog::CFindDialog\n");
  60. //{{AFX_DATA_INIT(CFindDialog)
  61. m_szContains = _T("");
  62. m_szSearchField = _T("");
  63. m_szSelectedStore = _T("");
  64. //}}AFX_DATA_INIT
  65. // Get name of logged-in user
  66. DWORD dwSize = 0;
  67. ::GetUserName (0, &dwSize);
  68. ASSERT (dwSize > 0);
  69. if ( dwSize > 0 )
  70. {
  71. BOOL bRet = ::GetUserName (m_szLoggedInUser.GetBufferSetLength (dwSize), &dwSize);
  72. ASSERT (bRet);
  73. m_szLoggedInUser.ReleaseBuffer ();
  74. }
  75. m_hCancelSearchEvent = CreateEvent(
  76. NULL, // pointer to security attributes
  77. TRUE, // flag for manual-reset event
  78. FALSE, // flag for initial state
  79. L"CancelSearchEvent"); // pointer to event-object name
  80. if ( !m_hCancelSearchEvent )
  81. {
  82. _TRACE (0, L"CreateEvent (CancelSearchEvent) failed: 0x%x\n", GetLastError ());
  83. }
  84. _TRACE (-1, L"Leaving CFindDialog::CFindDialog\n");
  85. }
  86. CFindDialog::~CFindDialog ()
  87. {
  88. _TRACE (1, L"Entering CFindDialog::~CFindDialog\n");
  89. if ( m_hSearchThread )
  90. StopSearch ();
  91. if ( m_hCancelSearchEvent )
  92. CloseHandle (m_hCancelSearchEvent);
  93. _TRACE (-1, L"Leaving CFindDialog::~CFindDialog\n");
  94. }
  95. void CFindDialog::DoDataExchange(CDataExchange* pDX)
  96. {
  97. CHelpDialog::DoDataExchange(pDX);
  98. //{{AFX_DATA_MAP(CFindDialog)
  99. DDX_Control(pDX, IDC_STORE_LIST, m_storeList);
  100. DDX_Control(pDX, IDC_STOP, m_stopBtn);
  101. DDX_Control(pDX, IDC_RESULT_LIST, m_resultsList);
  102. DDX_Control(pDX, IDC_NEW_SEARCH, m_newSearchBtn);
  103. DDX_Control(pDX, IDC_FIND_NOW, m_findNowBtn);
  104. DDX_Control(pDX, IDC_FIELD_LIST, m_fieldList);
  105. DDX_Control(pDX, IDC_ANIMATE, m_animate);
  106. DDX_Text(pDX, IDC_CONTAINS_TEXT, m_szContains);
  107. DDX_CBString(pDX, IDC_FIELD_LIST, m_szSearchField);
  108. DDX_CBString(pDX, IDC_STORE_LIST, m_szSelectedStore);
  109. //}}AFX_DATA_MAP
  110. }
  111. BEGIN_MESSAGE_MAP(CFindDialog, CHelpDialog)
  112. //{{AFX_MSG_MAP(CFindDialog)
  113. ON_BN_CLICKED(IDC_FIND_NOW, OnFindNow)
  114. ON_WM_SIZING()
  115. ON_WM_DESTROY()
  116. ON_BN_CLICKED(IDC_NEW_SEARCH, OnNewSearch)
  117. ON_BN_CLICKED(IDC_STOP, OnStop)
  118. ON_NOTIFY(NM_RCLICK, IDC_RESULT_LIST, OnRclickResultList)
  119. ON_NOTIFY(NM_DBLCLK, IDC_RESULT_LIST, OnDblclkResultList)
  120. ON_NOTIFY(LVN_COLUMNCLICK, IDC_RESULT_LIST, OnColumnclickResultList)
  121. ON_EN_CHANGE(IDC_CONTAINS_TEXT, OnChangeContainsText)
  122. ON_NOTIFY(LVN_ITEMCHANGED, IDC_RESULT_LIST, OnItemchangedResultList)
  123. ON_WM_CONTEXTMENU()
  124. ON_WM_SIZE()
  125. //}}AFX_MSG_MAP
  126. ON_COMMAND(IDM_PROPERTIES, OnProperties)
  127. ON_COMMAND(ID_VIEW, OnView)
  128. ON_COMMAND(ID_ENROLL_SAME_KEY, OnEnrollSameKey)
  129. ON_COMMAND(ID_ENROLL_NEW_KEY, OnEnrollNewKey)
  130. ON_COMMAND(ID_FILE_DELETE, OnFileDelete)
  131. ON_COMMAND(ID_FILE_EXPORT, OnFileExport)
  132. ON_COMMAND(ID_FILE_RENEW_NEW_KEY, OnFileRenewNewKey)
  133. ON_COMMAND(ID_FILE_RENEW_SAME_KEY, OnFileRenewSameKey)
  134. ON_COMMAND(ID_EDIT_INVERTSELECTION, OnEditInvertselection)
  135. ON_COMMAND(ID_EDIT_SELECTALL, OnEditSelectall)
  136. ON_COMMAND(ID_FILE_PROPERTIES, OnFileProperties)
  137. ON_COMMAND(ID_HELP_HELPTOPICS, OnHelpHelptopics)
  138. ON_COMMAND(IDM_VIEW_DETAILS, OnViewDetails)
  139. ON_COMMAND(ID_VIEW_LARGEICONS, OnViewLargeicons)
  140. ON_COMMAND(IDM_VIEW_LIST, OnViewList)
  141. ON_COMMAND(ID_VIEW_SMALLICONS, OnViewSmallicons)
  142. ON_COMMAND(IDM_WHATS_THIS, OnWhatsThis)
  143. ON_MESSAGE(WM_HELP, OnHelp)
  144. END_MESSAGE_MAP()
  145. BOOL CFindDialog::OnInitDialog()
  146. {
  147. _TRACE (1, L"Entering CFindDialog::OnInitDialog\n");
  148. CHelpDialog::OnInitDialog();
  149. // Set up the menu
  150. HMENU hMenu = ::LoadMenu (AfxGetInstanceHandle (),
  151. MAKEINTRESOURCE (IDR_FIND_DLG_MENU));
  152. ASSERT (hMenu);
  153. if ( hMenu )
  154. {
  155. if (::SetMenu (m_hWnd, hMenu) )
  156. {
  157. // Disable these menu items until the window is expanded when searching
  158. VERIFY (::EnableMenuItem (hMenu, ID_VIEW_LARGEICONS, MF_GRAYED) != -1);
  159. VERIFY (::EnableMenuItem (hMenu, ID_VIEW_SMALLICONS, MF_GRAYED) != -1);
  160. VERIFY (::EnableMenuItem (hMenu, IDM_VIEW_LIST, MF_GRAYED) != -1);
  161. VERIFY (::EnableMenuItem (hMenu, IDM_VIEW_DETAILS, MF_GRAYED) != -1);
  162. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_SELECTALL, MF_GRAYED) != -1);
  163. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_INVERTSELECTION, MF_GRAYED) != -1);
  164. VERIFY (::EnableMenuItem (hMenu, ID_FILE_DELETE, MF_GRAYED) != -1);
  165. VERIFY (::EnableMenuItem (hMenu, ID_FILE_EXPORT, MF_GRAYED) != -1);
  166. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_SAME_KEY, MF_GRAYED) != -1);
  167. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_NEW_KEY, MF_GRAYED) != -1);
  168. VERIFY (::EnableMenuItem (hMenu, ID_FILE_PROPERTIES, MF_GRAYED) != -1);
  169. VERIFY (::EnableMenuItem (hMenu, ID_VIEW, MF_GRAYED) != -1);
  170. }
  171. else
  172. ASSERT (0);
  173. }
  174. SetUpResultList ();
  175. // Size window to hide list view until search is performed
  176. HideResultList ();
  177. // Initialize animation
  178. VERIFY (m_animate.Open (IDR_FINDCERT_AVI));
  179. // Set up cert store list
  180. AddLogicalStoresToList ();
  181. // Set cert field list
  182. AddFieldsToList ();
  183. m_findNowBtn.EnableWindow (FALSE);
  184. m_stopBtn.EnableWindow (FALSE);
  185. m_newSearchBtn.EnableWindow (FALSE);
  186. m_bInitComplete = true;
  187. _TRACE (-1, L"Leaving CFindDialog::OnInitDialog\n");
  188. return TRUE; // return TRUE unless you set the focus to a control
  189. // EXCEPTION: OCX Property Pages should return FALSE
  190. }
  191. void CFindDialog::OnFindNow()
  192. {
  193. _TRACE (1, L"Entering CFindDialog::OnFindNow\n");
  194. // Disable the controls during the search
  195. GetDlgItem (IDC_CONTAINS_TEXT)->EnableWindow (FALSE);
  196. m_resultsList.EnableWindow (FALSE);
  197. m_fieldList.EnableWindow (FALSE);
  198. m_storeList.EnableWindow (FALSE);
  199. m_findNowBtn.EnableWindow (FALSE);
  200. m_newSearchBtn.EnableWindow (FALSE);
  201. m_stopBtn.EnableWindow (TRUE);
  202. m_stopBtn.SetFocus ();
  203. VERIFY (m_animate.Play (0, (UINT) -1, (UINT) -1));
  204. m_bAnimationRunning = true;
  205. UpdateData (TRUE);
  206. DeleteAllResultItems ();
  207. if ( !m_fWindowExpandedOnce )
  208. {
  209. // ChangeToSizableFrame ();
  210. ExpandWindow ();
  211. }
  212. DoSearch ();
  213. EnableMenuItems ();
  214. }
  215. void CFindDialog::EnableMenuItems ()
  216. {
  217. _TRACE (1, L"Entering CFindDialog::EnableMenuItems\n");
  218. HMENU hMenu = ::GetMenu (m_hWnd);
  219. ASSERT (hMenu);
  220. if ( hMenu )
  221. {
  222. int nCnt = m_resultsList.GetItemCount ();
  223. if ( nCnt > 0 )
  224. {
  225. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_SELECTALL, MF_ENABLED) != -1);
  226. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_INVERTSELECTION, MF_ENABLED) != -1);
  227. }
  228. else
  229. {
  230. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_SELECTALL, MF_GRAYED) != -1);
  231. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_INVERTSELECTION, MF_GRAYED) != -1);
  232. }
  233. UINT nSelCnt = m_resultsList.GetSelectedCount ();
  234. VERIFY (::EnableMenuItem (hMenu, ID_FILE_DELETE,
  235. (nSelCnt >= 1) ? MF_ENABLED : MF_GRAYED) != -1);
  236. VERIFY (::EnableMenuItem (hMenu, ID_FILE_EXPORT,
  237. (nSelCnt >= 1) ? MF_ENABLED : MF_GRAYED) != -1);
  238. VERIFY (::EnableMenuItem (hMenu, ID_FILE_PROPERTIES,
  239. (nSelCnt == 1) ? MF_ENABLED : MF_GRAYED) != -1);
  240. VERIFY (::EnableMenuItem (hMenu, ID_VIEW,
  241. (nSelCnt == 1) ? MF_ENABLED : MF_GRAYED) != -1);
  242. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_SAME_KEY, MF_GRAYED) != -1);
  243. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_NEW_KEY, MF_GRAYED) != -1);
  244. if ( nSelCnt == 1 )
  245. {
  246. int nSelItem = 0;
  247. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  248. ASSERT (pCert);
  249. if ( pCert )
  250. {
  251. bool bIsMyStore = (pCert->GetStoreType () == MY_STORE);
  252. if ( bIsMyStore && CERT_SYSTEM_STORE_SERVICES != m_pCompData->GetLocation () )
  253. {
  254. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_SAME_KEY,
  255. MF_ENABLED) != -1);
  256. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_NEW_KEY,
  257. MF_ENABLED) != -1);
  258. }
  259. }
  260. }
  261. }
  262. _TRACE (-1, L"Leaving CFindDialog::EnableMenuItems\n");
  263. }
  264. void CFindDialog::OnSize (UINT nType, int cx, int cy)
  265. {
  266. CHelpDialog::OnSize (nType, cx, cy);
  267. MoveControls ();
  268. }
  269. void CFindDialog::OnSizing (UINT nSide, LPRECT lpRect)
  270. {
  271. _TRACE (1, L"Entering CFindDialog::OnSizing\n");
  272. int cyHeight = lpRect->bottom - lpRect->top;
  273. int cxWidth = lpRect->right - lpRect->left;
  274. // If the user has never pressed "Find Now", don't let the user expand the window down
  275. // Don't let the user shrink the window below it's initial state - I don't want to deal with
  276. // control compression!
  277. switch (nSide)
  278. {
  279. case WMSZ_BOTTOM:
  280. if ( !m_fWindowExpandedOnce )
  281. lpRect->top = lpRect->bottom - m_cyMin;
  282. else
  283. {
  284. if ( cyHeight < m_cyMin )
  285. lpRect->bottom = lpRect->top + m_cyMin;
  286. }
  287. break;
  288. case WMSZ_BOTTOMLEFT:
  289. if ( cxWidth < m_cxMin )
  290. lpRect->left = lpRect->right - m_cxMin;
  291. if ( !m_fWindowExpandedOnce )
  292. lpRect->top = lpRect->bottom - m_cyMin;
  293. else
  294. {
  295. if ( cyHeight < m_cyMin )
  296. lpRect->bottom = lpRect->top + m_cyMin;
  297. }
  298. break;
  299. case WMSZ_BOTTOMRIGHT:
  300. if ( cxWidth < m_cxMin )
  301. lpRect->right = lpRect->left + m_cxMin;
  302. if ( !m_fWindowExpandedOnce )
  303. lpRect->bottom = lpRect->top + m_cyMin;
  304. else
  305. {
  306. if ( cyHeight < m_cyMin )
  307. lpRect->bottom = lpRect->top + m_cyMin;
  308. }
  309. break;
  310. case WMSZ_TOP:
  311. if ( !m_fWindowExpandedOnce )
  312. lpRect->top = lpRect->bottom - m_cyMin;
  313. else
  314. {
  315. if ( cyHeight < m_cyMin )
  316. lpRect->top = lpRect->bottom - m_cyMin;
  317. }
  318. break;
  319. case WMSZ_TOPLEFT:
  320. if ( cxWidth < m_cxMin )
  321. lpRect->left = lpRect->right - m_cxMin;
  322. if ( !m_fWindowExpandedOnce )
  323. lpRect->top = lpRect->bottom - m_cyMin;
  324. else
  325. {
  326. if ( cyHeight < m_cyMin )
  327. lpRect->top = lpRect->bottom - m_cyMin;
  328. }
  329. break;
  330. case WMSZ_TOPRIGHT:
  331. if ( cxWidth < m_cxMin )
  332. lpRect->right = lpRect->left + m_cxMin;
  333. if ( !m_fWindowExpandedOnce )
  334. lpRect->top = lpRect->bottom - m_cyMin;
  335. else
  336. {
  337. if ( cyHeight < m_cyMin )
  338. lpRect->top = lpRect->bottom - m_cyMin;
  339. }
  340. break;
  341. case WMSZ_RIGHT:
  342. if ( cxWidth < m_cxMin )
  343. lpRect->right = lpRect->left + m_cxMin;
  344. break;
  345. case WMSZ_LEFT:
  346. if ( cxWidth < m_cxMin )
  347. lpRect->left = lpRect->right - m_cxMin;
  348. break;
  349. default:
  350. break;
  351. }
  352. CHelpDialog::OnSizing (nSide, lpRect);
  353. _TRACE (-1, L"Leaving CFindDialog::OnSizing\n");
  354. }
  355. void CFindDialog::MoveControls ()
  356. {
  357. _TRACE (1, L"Entering CFindDialog::MoveControls\n");
  358. if ( !m_hWnd || !m_bInitComplete )
  359. return;
  360. // Only come down here if the window has already been created.
  361. // Keep buttons glued to the right
  362. CRect rcDlg;
  363. GetWindowRect (&rcDlg); // returned in screen coords
  364. ScreenToClient (&rcDlg);
  365. // Move "Stop" button
  366. CRect rcCtrl;
  367. m_stopBtn.GetWindowRect (&rcCtrl); // returned in screen coords
  368. ScreenToClient (&rcCtrl);
  369. int cxCtrl = rcCtrl.right - rcCtrl.left;
  370. rcCtrl.right = rcDlg.right - m_cxBtnMargin;
  371. rcCtrl.left = rcCtrl.right - cxCtrl;
  372. m_stopBtn.MoveWindow (rcCtrl); // child window coords relative to parent client
  373. // Move "Find Now" button
  374. m_findNowBtn.GetWindowRect (&rcCtrl); // returned in screen coords
  375. ScreenToClient (&rcCtrl);
  376. cxCtrl = rcCtrl.right - rcCtrl.left;
  377. rcCtrl.right = rcDlg.right - m_cxBtnMargin;
  378. rcCtrl.left = rcCtrl.right - cxCtrl;
  379. m_findNowBtn.MoveWindow (rcCtrl); // child window coords relative to parent client
  380. // Move "New Search" button
  381. m_newSearchBtn.GetWindowRect (&rcCtrl); // returned in screen coords
  382. ScreenToClient (&rcCtrl);
  383. cxCtrl = rcCtrl.right - rcCtrl.left;
  384. rcCtrl.right = rcDlg.right - m_cxBtnMargin;
  385. rcCtrl.left = rcCtrl.right - cxCtrl;
  386. m_newSearchBtn.MoveWindow (rcCtrl); // child window coords relative to parent client
  387. // Move animation control
  388. m_animate.GetWindowRect (&rcCtrl); // returned in screen coords
  389. ScreenToClient (&rcCtrl);
  390. cxCtrl = rcCtrl.right - rcCtrl.left;
  391. rcCtrl.right = rcDlg.right - m_cxAnimMargin;
  392. rcCtrl.left = rcCtrl.right - cxCtrl;
  393. m_animate.MoveWindow (rcCtrl); // child window coords relative to parent client
  394. // Stretch store list control
  395. m_storeList.GetWindowRect (&rcCtrl); // returned in screen coords
  396. ScreenToClient (&rcCtrl);
  397. rcCtrl.right = rcDlg.right - m_cxStoreListMargin;
  398. m_storeList.MoveWindow (rcCtrl); // child window coords relative to parent client
  399. // Stretch "contains" edit control
  400. GetDlgItem (IDC_CONTAINS_TEXT)->GetWindowRect (&rcCtrl);
  401. ScreenToClient (&rcCtrl);
  402. rcCtrl.right = rcDlg.right - m_cxContainMargin;
  403. GetDlgItem (IDC_CONTAINS_TEXT)->MoveWindow (rcCtrl);
  404. // Stretch field list control
  405. m_singleLock.Lock ();
  406. m_fieldList.GetWindowRect (&rcCtrl); // returned in screen coords
  407. m_singleLock.Unlock ();
  408. ScreenToClient (&rcCtrl);
  409. rcCtrl.right = rcDlg.right - m_cxFieldListMargin;
  410. m_singleLock.Lock ();
  411. m_fieldList.MoveWindow (rcCtrl); // child window coords relative to parent client
  412. m_singleLock.Unlock ();
  413. m_fieldList.InvalidateRect (NULL, TRUE);
  414. // Stretch list view to right edge and to bottom
  415. int cyResultListMargin = 0;
  416. if ( m_statusBar.m_hWnd )
  417. {
  418. // If the status bar has already been created, we need to take that
  419. // account when we resize the list view
  420. CRect rcStatusBar;
  421. m_statusBar.GetWindowRect (&rcStatusBar);
  422. cyResultListMargin = (rcStatusBar.bottom - rcStatusBar.top)
  423. + ::GetSystemMetrics (SM_CYDLGFRAME);
  424. }
  425. m_resultsList.GetWindowRect (&rcCtrl); // returned in screen coords
  426. ScreenToClient (&rcCtrl);
  427. rcCtrl.right = rcDlg.right - m_cxResultListMargin;
  428. if ( m_fWindowExpandedOnce )
  429. rcCtrl.bottom = rcDlg.bottom - cyResultListMargin;
  430. m_resultsList.MoveWindow (rcCtrl); // child window coords relative to parent client
  431. // Resize last column to fill results list window
  432. int nNewWidth = rcCtrl.right - (m_resultsList.GetColumnWidth (COL_ISSUED_TO) +
  433. m_resultsList.GetColumnWidth (COL_ISSUED_BY) +
  434. m_resultsList.GetColumnWidth (COL_EXPIRATION_DATE) +
  435. m_resultsList.GetColumnWidth (COL_PURPOSES) +
  436. m_resultsList.GetColumnWidth (COL_FRIENDLY_NAME));
  437. if ( nNewWidth > m_resultsList.GetColumnWidth (COL_SOURCE_STORE) )
  438. VERIFY (m_resultsList.SetColumnWidth (COL_SOURCE_STORE, nNewWidth));
  439. // Stretch status bar to right edge
  440. if ( m_statusBar.m_hWnd )
  441. {
  442. m_statusBar.GetWindowRect (&rcCtrl); // returned in screen coords
  443. ScreenToClient (&rcCtrl);
  444. int cyCtrl = rcCtrl.bottom - rcCtrl.top;
  445. rcCtrl.right = rcDlg.right;
  446. rcCtrl.bottom = rcDlg.bottom;
  447. rcCtrl.top = rcCtrl.bottom - cyCtrl;
  448. m_statusBar.MoveWindow (rcCtrl); // child window coords relative to parent client
  449. }
  450. _TRACE (-1, L"Leaving CFindDialog::MoveControls\n");
  451. }
  452. typedef struct _ENUM_ARG {
  453. DWORD dwFlags;
  454. CComboBox* m_pComboBox;
  455. LPCWSTR m_szMachineName;
  456. IConsole* m_pConsole;
  457. } ENUM_ARG, *PENUM_ARG;
  458. static BOOL WINAPI EnumFindDlgSysCallback(
  459. IN const void* pwszSystemStore,
  460. IN DWORD dwFlags,
  461. IN PCERT_SYSTEM_STORE_INFO /*pStoreInfo*/,
  462. IN OPTIONAL void* /*pvReserved*/,
  463. IN OPTIONAL void *pvArg
  464. )
  465. {
  466. _TRACE (1, L"Entering EnumFindDlgSysCallback\n");
  467. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  468. // Create new cookies
  469. SPECIAL_STORE_TYPE storeType = GetSpecialStoreType ((LPWSTR) pwszSystemStore);
  470. //
  471. // We will not expose the ACRS store for machines or users. It is not
  472. // interesting or useful at this level. All Auto Cert Requests should
  473. // be managed only at the policy level.
  474. //
  475. if ( ACRS_STORE != storeType )
  476. {
  477. CCertStore* pNewCookie = new CCertStore (CERTMGR_LOG_STORE,
  478. CERT_STORE_PROV_SYSTEM,
  479. dwFlags, pEnumArg->m_szMachineName,
  480. (LPCWSTR) pwszSystemStore,
  481. (LPCWSTR) pwszSystemStore,
  482. _T(""), storeType,
  483. dwFlags,
  484. pEnumArg->m_pConsole);
  485. if ( pNewCookie )
  486. {
  487. CString storeName = _T(" ");
  488. int iResult = 0;
  489. LPCWSTR localizedName = pNewCookie->GetLocalizedName ();
  490. if ( localizedName )
  491. storeName += localizedName;
  492. else
  493. storeName += (LPWSTR) pwszSystemStore;
  494. iResult = pEnumArg->m_pComboBox->AddString ((LPCWSTR) storeName);
  495. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  496. if ( CB_ERR != iResult && CB_ERRSPACE != iResult)
  497. {
  498. iResult = pEnumArg->m_pComboBox->SetItemDataPtr (iResult, (void*) pNewCookie);
  499. ASSERT (CB_ERR != iResult);
  500. }
  501. else
  502. pNewCookie->Release ();
  503. }
  504. }
  505. _TRACE (-1, L"Leaving EnumFindDlgSysCallback\n");
  506. return TRUE;
  507. }
  508. void CFindDialog::AddLogicalStoresToList ()
  509. {
  510. _TRACE (1, L"Entering CFindDialog::AddLogicalStoresToList\n");
  511. CString text;
  512. CWaitCursor cursor;
  513. VERIFY (text.LoadString (IDS_ALL_STORES));
  514. VERIFY (m_storeList.AddString (text) >= 0);
  515. VERIFY (m_storeList.SetCurSel (0) != CB_ERR);
  516. DWORD dwFlags = m_pCompData->GetLocation ();
  517. ENUM_ARG EnumArg;
  518. ::ZeroMemory (&EnumArg, sizeof(EnumArg));
  519. EnumArg.dwFlags = dwFlags;
  520. EnumArg.m_szMachineName = m_szMachineName;
  521. EnumArg.m_pComboBox = &m_storeList;
  522. EnumArg.m_pConsole = m_pCompData->m_pConsole;
  523. CString location;
  524. void* pvPara = 0;
  525. if ( !m_pCompData->GetManagedService ().IsEmpty () )
  526. {
  527. if ( !m_szMachineName.IsEmpty () )
  528. {
  529. location = m_szMachineName + _T("\\") +
  530. m_pCompData->GetManagedService ();
  531. pvPara = (void *) (LPCWSTR) location;
  532. }
  533. else
  534. pvPara = (void *) (LPCWSTR) m_pCompData->GetManagedService ();
  535. }
  536. else if ( !m_szMachineName.IsEmpty () )
  537. {
  538. pvPara = (void *) (LPCWSTR) m_szMachineName;
  539. }
  540. if ( m_szFileName.IsEmpty () )
  541. {
  542. // This is not a file store but some kind of system store.
  543. if ( !::CertEnumSystemStore (dwFlags, pvPara, &EnumArg, EnumFindDlgSysCallback) )
  544. {
  545. DWORD dwErr = GetLastError ();
  546. CString caption;
  547. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  548. if ( ERROR_ACCESS_DENIED == dwErr )
  549. {
  550. VERIFY (text.LoadString (IDS_NO_PERMISSION));
  551. }
  552. else
  553. {
  554. text.FormatMessage (IDS_CANT_ENUMERATE_SYSTEM_STORES, GetSystemMessage (dwErr));
  555. }
  556. CThemeContextActivator activator;
  557. MessageBox (text, caption, MB_OK);
  558. if ( ERROR_BAD_NETPATH == dwErr )
  559. {
  560. // Close dialog
  561. PostMessage (WM_CLOSE, 0, 0L);
  562. }
  563. }
  564. }
  565. else
  566. {
  567. // CertOpenStore with provider type of:
  568. // CERT_STORE_PROV_FILE or CERT_STORE_PROV_FILENAME_A
  569. // or CERT_STORE_PROV_FILENAME_W.
  570. // See online documentation or wincrypt.h for more info.
  571. // Create new cookies
  572. CCertStore* pNewCookie = new CCertStore (
  573. CERTMGR_LOG_STORE,
  574. CERT_STORE_PROV_FILENAME_W,
  575. 0,
  576. m_szMachineName,
  577. m_szFileName, m_szFileName, _T(""), NO_SPECIAL_TYPE,
  578. m_pCompData->GetLocation (),
  579. m_pCompData->m_pConsole);
  580. if ( pNewCookie )
  581. {
  582. CString storeName = _T(" ");
  583. storeName += m_szFileName;
  584. int iResult = m_storeList.AddString ((LPCWSTR) storeName);
  585. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  586. if ( CB_ERR != iResult && CB_ERRSPACE != iResult )
  587. {
  588. iResult = m_storeList.SetItemDataPtr (iResult, (void*) pNewCookie);
  589. ASSERT (CB_ERR != iResult);
  590. }
  591. }
  592. }
  593. _TRACE (-1, L"Leaving CFindDialog::AddLogicalStoresToList\n");
  594. }
  595. void CFindDialog::OnDestroy()
  596. {
  597. _TRACE (1, L"Entering CFindDialog::OnDestroy\n");
  598. // In case a search is running when the user destroys the window, stop it!
  599. StopSearch ();
  600. CHelpDialog::OnDestroy();
  601. CCertStore* pCookie = 0;
  602. const int nCnt = m_storeList.GetCount ();
  603. for (int nIndex = 0; nIndex < nCnt; nIndex++)
  604. {
  605. pCookie = (CCertStore*) m_storeList.GetItemDataPtr (nIndex);
  606. if ( pCookie ) // one of the items has no cookie
  607. pCookie->Release ();
  608. }
  609. DeleteAllResultItems ();
  610. m_imageListNormal.Destroy ();
  611. m_imageListSmall.Destroy ();
  612. _TRACE (-1, L"Leaving CFindDialog::OnDestroy\n");
  613. }
  614. void CFindDialog::AddFieldsToList()
  615. {
  616. _TRACE (1, L"Entering CFindDialog::AddFieldsToList\n");
  617. CString text;
  618. int iResult = 0;
  619. VERIFY (text.LoadString (IDS_FIND_MD5_HASH));
  620. iResult = m_fieldList.AddString (text);
  621. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  622. if ( CB_ERR != iResult && CB_ERRSPACE != iResult )
  623. VERIFY (m_fieldList.SetItemData (iResult, CERT_FIND_MD5_HASH) != CB_ERR);
  624. VERIFY (text.LoadString (IDS_FIND_SHA1_HASH));
  625. iResult = m_fieldList.AddString (text);
  626. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  627. if ( CB_ERR != iResult && CB_ERRSPACE != iResult )
  628. VERIFY (m_fieldList.SetItemData (iResult, CERT_FIND_SHA1_HASH) != CB_ERR);
  629. VERIFY (text.LoadString (IDS_FIND_SUBJECT_NAME));
  630. iResult = m_fieldList.AddString (text);
  631. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  632. if ( CB_ERR != iResult && CB_ERRSPACE != iResult )
  633. VERIFY (m_fieldList.SetItemData (iResult, CERT_FIND_SUBJECT_STR_W) != CB_ERR);
  634. VERIFY (text.LoadString (IDS_FIND_ISSUER_NAME));
  635. iResult = m_fieldList.AddString (text);
  636. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  637. if ( CB_ERR != iResult && CB_ERRSPACE != iResult )
  638. {
  639. VERIFY (m_fieldList.SetItemData (iResult, CERT_FIND_ISSUER_STR_W) != CB_ERR);
  640. VERIFY (m_fieldList.SetCurSel (iResult) != CB_ERR);
  641. }
  642. VERIFY (text.LoadString (IDS_COLUMN_SERIAL_NUMBER));
  643. iResult = m_fieldList.AddString (text);
  644. ASSERT (CB_ERR != iResult && CB_ERRSPACE != iResult);
  645. if ( CB_ERR != iResult && CB_ERRSPACE != iResult )
  646. VERIFY (m_fieldList.SetItemData (iResult, CERT_FIND_SERIAL_NUMBER) != CB_ERR);
  647. _TRACE (-1, L"Leaving CFindDialog::AddFieldsToList\n");
  648. }
  649. void CFindDialog::OnNewSearch()
  650. {
  651. _TRACE (1, L"Entering CFindDialog::OnNewSearch\n");
  652. CString caption;
  653. CString text;
  654. VERIFY (text.LoadString (IDS_CLEAR_SEARCH));
  655. VERIFY (caption.LoadString (IDS_FIND_CERT));
  656. CThemeContextActivator activator;
  657. if ( MessageBox (text, caption, MB_ICONQUESTION | MB_OKCANCEL) == IDOK )
  658. {
  659. DeleteAllResultItems ();
  660. m_singleLock.Lock ();
  661. VERIFY (m_fieldList.SetCurSel (0) != CB_ERR);
  662. m_singleLock.Unlock ();
  663. VERIFY (text.LoadString (IDS_ALL_STORES));
  664. int nIndex = m_storeList.FindStringExact (-1, text);
  665. ASSERT (CB_ERR != nIndex);
  666. if ( CB_ERR != nIndex )
  667. VERIFY (m_storeList.SetCurSel (nIndex) != CB_ERR);
  668. GetDlgItem (IDC_CONTAINS_TEXT)->SetWindowText (L"");
  669. CString statusText;
  670. VERIFY (statusText.LoadString (IDS_NO_CERTS_FOUND));
  671. if ( m_statusBar.m_hWnd )
  672. m_statusBar.SetWindowText (statusText);
  673. m_newSearchBtn.EnableWindow (FALSE);
  674. HMENU hMenu = ::GetMenu (m_hWnd);
  675. ASSERT (hMenu);
  676. if ( hMenu )
  677. {
  678. VERIFY (::EnableMenuItem (hMenu, ID_FILE_DELETE, MF_GRAYED) != -1);
  679. VERIFY (::EnableMenuItem (hMenu, ID_FILE_EXPORT, MF_GRAYED) != -1);
  680. VERIFY (::EnableMenuItem (hMenu, ID_FILE_PROPERTIES, MF_GRAYED) != -1);
  681. VERIFY (::EnableMenuItem (hMenu, ID_VIEW, MF_GRAYED) != -1);
  682. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_SAME_KEY, MF_GRAYED) != -1);
  683. VERIFY (::EnableMenuItem (hMenu, ID_FILE_RENEW_NEW_KEY, MF_GRAYED) != -1);
  684. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_SELECTALL, MF_GRAYED) != -1);
  685. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_INVERTSELECTION, MF_GRAYED) != -1);
  686. }
  687. GetDlgItem (IDC_CONTAINS_TEXT)->SetFocus ();
  688. }
  689. _TRACE (-1, L"Leaving CFindDialog::OnNewSearch\n");
  690. }
  691. void CFindDialog::OnStop()
  692. {
  693. _TRACE (1, L"Entering CFindDialog::OnStop\n");
  694. StopSearch ();
  695. _TRACE (-1, L"Leaving CFindDialog::OnStop\n");
  696. }
  697. void CFindDialog::StopSearch()
  698. {
  699. _TRACE (1, L"Entering CFindDialog::StopSearch\n");
  700. if ( m_bAnimationRunning )
  701. {
  702. VERIFY (m_animate.Stop ());
  703. m_bAnimationRunning = false;
  704. }
  705. VERIFY (m_animate.Seek (0));
  706. if ( m_hSearchThread && WaitForSingleObject (m_hSearchThread, 0) != WAIT_OBJECT_0 )
  707. {
  708. VERIFY (SetEvent (m_hCancelSearchEvent));
  709. if ( WaitForSingleObject (m_hSearchThread, 5000) != WAIT_OBJECT_0 )
  710. {
  711. RestoreAfterSearchSettings ();
  712. }
  713. }
  714. _TRACE (-1, L"Leaving CFindDialog::StopSearch\n");
  715. }
  716. void CFindDialog::ExpandWindow()
  717. {
  718. _TRACE (1, L"Entering CFindDialog::ExpandWindow\n");
  719. ASSERT (!m_fWindowExpandedOnce);
  720. if ( m_fWindowExpandedOnce )
  721. return;
  722. m_fWindowExpandedOnce = true;
  723. CRect rcDlg;
  724. GetWindowRect (&rcDlg);
  725. VERIFY (SetWindowPos (&wndTop, rcDlg.left, rcDlg.top,
  726. rcDlg.right - rcDlg.left,
  727. m_cyOriginal,
  728. SWP_NOMOVE | SWP_NOOWNERZORDER));
  729. // Create the status bar
  730. CRect rcStatusBar;
  731. CThemeContextActivator activator;
  732. VERIFY (m_statusBar.Create (WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP,
  733. rcStatusBar, this, 1));
  734. if ( m_statusBar.m_hWnd )
  735. m_statusBar.GetWindowRect (&rcStatusBar);
  736. int cyResultListMargin = (rcStatusBar.bottom - rcStatusBar.top)
  737. + ::GetSystemMetrics (SM_CYDLGFRAME);
  738. // Stretch list view to right edge and to bottom
  739. GetWindowRect (&rcDlg);
  740. ScreenToClient (&rcDlg);
  741. CRect rcCtrl;
  742. m_resultsList.GetWindowRect (&rcCtrl); // returned in screen coords
  743. ScreenToClient (&rcCtrl);
  744. m_cxResultListMargin = (rcDlg.right - rcCtrl.right); // + ::GetSystemMetrics (SM_CXDLGFRAME);
  745. rcCtrl.right = rcDlg.right - m_cxResultListMargin;
  746. rcCtrl.bottom = rcDlg.bottom - cyResultListMargin;
  747. m_resultsList.MoveWindow (rcCtrl); // child window coords relative to parent client
  748. // Permanently enable some menu items
  749. HMENU hMenu = ::GetMenu (m_hWnd);
  750. ASSERT (hMenu);
  751. if ( hMenu)
  752. {
  753. VERIFY (::EnableMenuItem (hMenu, ID_VIEW_LARGEICONS, MF_ENABLED) != -1);
  754. VERIFY (::EnableMenuItem (hMenu, ID_VIEW_SMALLICONS, MF_ENABLED) != -1);
  755. VERIFY (::EnableMenuItem (hMenu, IDM_VIEW_LIST, MF_ENABLED) != -1);
  756. VERIFY (::EnableMenuItem (hMenu, IDM_VIEW_DETAILS, MF_ENABLED) != -1);
  757. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_SELECTALL, MF_ENABLED) != -1);
  758. VERIFY (::EnableMenuItem (hMenu, ID_EDIT_INVERTSELECTION, MF_ENABLED) != -1);
  759. }
  760. _TRACE (-1, L"Leaving CFindDialog::ExpandWindow\n");
  761. }
  762. //DWORD WINAPI FindThreadFunc (LPVOID lpvThreadParm)
  763. void __cdecl FindThreadFunc (LPVOID lpvThreadParm)
  764. {
  765. _TRACE (1, L"Entering FindThreadFunc\n");
  766. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  767. CFindDialog* pFindDlg = (CFindDialog*) lpvThreadParm;
  768. ASSERT (pFindDlg);
  769. if ( !pFindDlg )
  770. return; // dwResult;
  771. pFindDlg->m_singleLock.Lock ();
  772. int nCurSel = pFindDlg->m_fieldList.GetCurSel ();
  773. pFindDlg->m_singleLock.Unlock ();
  774. ASSERT (CB_ERR != nCurSel);
  775. if ( CB_ERR != nCurSel )
  776. {
  777. CString statusText;
  778. pFindDlg->m_singleLock.Lock ();
  779. DWORD dwFindType = (DWORD)pFindDlg->m_fieldList.GetItemData (nCurSel);
  780. pFindDlg->m_singleLock.Unlock ();
  781. CString szFindText;
  782. statusText.LoadString (IDS_SEARCHING);
  783. if ( pFindDlg->m_statusBar.m_hWnd )
  784. {
  785. pFindDlg->m_statusBar.SetWindowText (statusText);
  786. pFindDlg->GetDlgItem (IDC_CONTAINS_TEXT)->GetWindowText (szFindText);
  787. }
  788. // Bug 218084 - in BiDi, string may be prepended with Left-to-Right
  789. // mark (0x200e). See if this character is present. If so, omit it
  790. // from the search. Check for multiple occurances.
  791. while ( chLEFT_TO_RIGHT == szFindText.GetAt (0) )
  792. {
  793. szFindText = ((PCWSTR) szFindText) + 1;
  794. }
  795. switch (dwFindType)
  796. {
  797. case CERT_FIND_SUBJECT_STR_W:
  798. case CERT_FIND_ISSUER_STR_W:
  799. pFindDlg->SearchForNames (szFindText, dwFindType);
  800. break;
  801. case CERT_FIND_MD5_HASH:
  802. pFindDlg->SearchForText (szFindText, CERT_MD5_HASH_PROP_ID);
  803. break;
  804. case CERT_FIND_SHA1_HASH:
  805. pFindDlg->SearchForText (szFindText, CERT_SHA1_HASH_PROP_ID);
  806. break;
  807. case CERT_FIND_SERIAL_NUMBER:
  808. pFindDlg->SearchForText (szFindText, CERT_FIND_SERIAL_NUMBER);
  809. break;
  810. default:
  811. {
  812. CThemeContextActivator activator;
  813. AfxMessageBox (_T("Search type not implemented"), MB_OK);
  814. }
  815. break;
  816. }
  817. int nCnt = pFindDlg->m_resultsList.GetItemCount ();
  818. ASSERT (-1 != nCnt);
  819. switch (nCnt)
  820. {
  821. case -1:
  822. case 0:
  823. VERIFY (statusText.LoadString (IDS_NO_CERTS_FOUND));
  824. break;
  825. case 1:
  826. VERIFY (statusText.LoadString (IDS_1_CERT_FOUND));
  827. break;
  828. break;
  829. default:
  830. statusText.FormatMessage (IDS_X_CERTS_FOUND, nCnt);
  831. break;
  832. }
  833. pFindDlg->m_statusBar.SetWindowText (statusText);
  834. }
  835. pFindDlg->RestoreAfterSearchSettings ();
  836. _TRACE (-1, L"Leaving FindThreadFunc\n");
  837. pFindDlg->m_hSearchThread = 0;
  838. _endthread ();
  839. }
  840. void CFindDialog::DoSearch()
  841. {
  842. _TRACE (1, L"Entering CFindDialog::DoSearch\n");
  843. CWaitCursor waitCursor;
  844. // Because FindThreadFunc calls c-runtime functions, use _beginthread instead of CreateThread.
  845. // BoundsChecker warns that using CreateThread here results in small memory leaks
  846. VERIFY (ResetEvent (m_hCancelSearchEvent));
  847. m_hSearchThread = (HANDLE) _beginthread (FindThreadFunc, 0, reinterpret_cast <void*> (this));
  848. ASSERT (m_hSearchThread);
  849. _TRACE (-1, L"Leaving CFindDialog::DoSearch\n");
  850. }
  851. void CFindDialog::SearchForTextOnStore (DWORD dwPropId, CString &szFindText,
  852. CCertStore& rCertStore)
  853. {
  854. _TRACE (0, L"Entering CFindDialog::SearchForTextOnStore - %s\n",
  855. (LPCWSTR) rCertStore.GetStoreName ());
  856. // NOTE: szFindText is already in upper case
  857. CWaitCursor cursor;
  858. PCCERT_CONTEXT pCertContext = 0;
  859. CCertificate* pCert = 0;
  860. CString szCertText;
  861. int nPos = 0;
  862. // Remove all spaces from szFindText
  863. RemoveSpaces (szFindText);
  864. // Iterate through the list of certificates in the system store,
  865. // allocate new certificates with the CERT_CONTEXT returned,
  866. // and store them in the certificate list if they meet search criteria
  867. while ( WAIT_TIMEOUT == WaitForSingleObject (m_hCancelSearchEvent, 0) )
  868. {
  869. pCertContext = rCertStore.EnumCertificates (pCertContext);
  870. if ( !pCertContext )
  871. break;
  872. pCert =
  873. new CCertificate (pCertContext, &rCertStore);
  874. if ( pCert )
  875. {
  876. switch (dwPropId)
  877. {
  878. case CERT_MD5_HASH_PROP_ID:
  879. szCertText = pCert->GetMD5Hash ();
  880. break;
  881. case CERT_SHA1_HASH_PROP_ID:
  882. szCertText = pCert->GetSHAHash ();
  883. break;
  884. case CERT_FIND_SERIAL_NUMBER:
  885. szCertText = pCert->GetSerialNumber ();
  886. RemoveSpaces (szCertText);
  887. break;
  888. default:
  889. ASSERT (0);
  890. return;
  891. }
  892. szCertText.MakeUpper ();
  893. nPos = szCertText.Find (szFindText);
  894. if ( -1 != nPos )
  895. {
  896. pCert->AddRef ();
  897. // Add cert to list control
  898. InsertItemInList (pCert);
  899. }
  900. pCert->Release ();
  901. }
  902. }
  903. rCertStore.Close ();
  904. _TRACE (-1, L"Leaving CFindDialog::SearchForTextOnStore - %s\n",
  905. (LPCWSTR) rCertStore.GetStoreName ());
  906. }
  907. void CFindDialog::SearchForNameOnStore (DWORD dwFindFlags, DWORD dwFindType,
  908. void * pvPara, CCertStore& rCertStore)
  909. {
  910. _TRACE (0, L"Entering CFindDialog::SearchForNameOnStore - %s\n",
  911. (LPCWSTR) rCertStore.GetStoreName ());
  912. PCCERT_CONTEXT pPrevCertContext = 0;
  913. PCCERT_CONTEXT pCertContext = 0;
  914. CCertificate* pCert = 0;
  915. DWORD dwErr = 0;
  916. while ( WAIT_TIMEOUT == WaitForSingleObject (m_hCancelSearchEvent, 0) )
  917. {
  918. pCertContext = rCertStore.FindCertificate (
  919. dwFindFlags,
  920. dwFindType,
  921. pvPara,
  922. pPrevCertContext);
  923. if ( !pCertContext )
  924. {
  925. dwErr = GetLastError ();
  926. switch (dwErr)
  927. {
  928. case ERROR_SUCCESS:
  929. break;
  930. case CRYPT_E_NOT_FOUND: // We're done. No more certificates.
  931. case ERROR_FILE_NOT_FOUND:
  932. break;
  933. default:
  934. if ( !rCertStore.GetStoreHandle () )
  935. {
  936. CString text;
  937. CString caption;
  938. text.FormatMessage
  939. (IDS_CANNOT_OPEN_CERT_STORE_TO_FIND_CERT_BY_PURPOSE,
  940. rCertStore.GetLocalizedName ());
  941. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  942. MessageBox (text, caption, MB_ICONWARNING | MB_OK);
  943. break;
  944. }
  945. else
  946. DisplaySystemError ();
  947. break;
  948. }
  949. break;
  950. }
  951. pCert = new CCertificate (pCertContext, &rCertStore);
  952. if ( pCert )
  953. InsertItemInList (pCert); // Add cert to list control
  954. else
  955. break;
  956. pPrevCertContext = pCertContext;
  957. }
  958. rCertStore.Close ();
  959. _TRACE (-1, L"Leaving CFindDialog::SearchForNameOnStore - %s\n",
  960. (LPCWSTR) rCertStore.GetStoreName ());
  961. }
  962. void CFindDialog::InsertItemInList(CCertificate * pCert)
  963. {
  964. _TRACE (1, L"Entering CFindDialog::InsertItemInList\n");
  965. LV_ITEM lvItem;
  966. int iItem = m_resultsList.GetItemCount ();
  967. int iResult = 0;
  968. ::ZeroMemory (&lvItem, sizeof (lvItem));
  969. lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  970. lvItem.iItem = iItem;
  971. lvItem.iSubItem = COL_ISSUED_TO;
  972. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetSubjectName ();
  973. lvItem.iImage = 0;
  974. lvItem.lParam = (LPARAM) pCert;
  975. iItem = m_resultsList.InsertItem (&lvItem);
  976. ASSERT (-1 != iItem);
  977. if ( -1 == iItem )
  978. return;
  979. ::ZeroMemory (&lvItem, sizeof (lvItem));
  980. lvItem.mask = LVIF_TEXT;
  981. lvItem.iItem = iItem;
  982. lvItem.iSubItem = COL_ISSUED_BY;
  983. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetIssuerName ();
  984. iResult = m_resultsList.SetItem (&lvItem);
  985. ASSERT (-1 != iResult);
  986. ::ZeroMemory (&lvItem, sizeof (lvItem));
  987. lvItem.mask = LVIF_TEXT;
  988. lvItem.iItem = iItem;
  989. lvItem.iSubItem = COL_EXPIRATION_DATE;
  990. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetValidNotAfter ();
  991. iResult = m_resultsList.SetItem (&lvItem);
  992. ASSERT (-1 != iResult);
  993. ::ZeroMemory (&lvItem, sizeof (lvItem));
  994. lvItem.mask = LVIF_TEXT;
  995. lvItem.iItem = iItem;
  996. lvItem.iSubItem = COL_PURPOSES;
  997. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetEnhancedKeyUsage ();
  998. iResult = m_resultsList.SetItem (&lvItem);
  999. ASSERT (-1 != iResult);
  1000. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1001. lvItem.mask = LVIF_TEXT;
  1002. lvItem.iItem = iItem;
  1003. lvItem.iSubItem = COL_FRIENDLY_NAME;
  1004. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetFriendlyName ();
  1005. iResult = m_resultsList.SetItem (&lvItem);
  1006. ASSERT (-1 != iResult);
  1007. _TRACE (-1, L"Leaving CFindDialog::InsertItemInList\n");
  1008. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1009. lvItem.mask = LVIF_TEXT;
  1010. lvItem.iItem = iItem;
  1011. lvItem.iSubItem = COL_SOURCE_STORE;
  1012. if ( pCert->GetCertStore () )
  1013. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetCertStore ()->GetLocalizedName ();
  1014. iResult = m_resultsList.SetItem (&lvItem);
  1015. ASSERT (-1 != iResult);
  1016. _TRACE (-1, L"Leaving CFindDialog::InsertItemInList\n");
  1017. }
  1018. void CFindDialog::RefreshItemInList (CCertificate * pCert, int nItem)
  1019. {
  1020. _TRACE (1, L"Entering CFindDialog::RefreshItemInList\n");
  1021. LV_ITEM lvItem;
  1022. int iResult = 0;
  1023. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1024. lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  1025. lvItem.iItem = nItem;
  1026. lvItem.iSubItem = COL_ISSUED_TO;
  1027. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetSubjectName ();
  1028. lvItem.iImage = 0;
  1029. lvItem.lParam = (LPARAM) pCert;
  1030. iResult = m_resultsList.SetItem (&lvItem);
  1031. ASSERT (-1 != iResult);
  1032. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1033. lvItem.mask = LVIF_TEXT;
  1034. lvItem.iItem = nItem;
  1035. lvItem.iSubItem = COL_ISSUED_BY;
  1036. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetIssuerName ();
  1037. iResult = m_resultsList.SetItem (&lvItem);
  1038. ASSERT (-1 != iResult);
  1039. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1040. lvItem.mask = LVIF_TEXT;
  1041. lvItem.iItem = nItem;
  1042. lvItem.iSubItem = COL_EXPIRATION_DATE;
  1043. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetValidNotAfter ();
  1044. iResult = m_resultsList.SetItem (&lvItem);
  1045. ASSERT (-1 != iResult);
  1046. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1047. lvItem.mask = LVIF_TEXT;
  1048. lvItem.iItem = nItem;
  1049. lvItem.iSubItem = COL_PURPOSES;
  1050. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetEnhancedKeyUsage ();
  1051. iResult = m_resultsList.SetItem (&lvItem);
  1052. ASSERT (-1 != iResult);
  1053. ::ZeroMemory (&lvItem, sizeof (lvItem));
  1054. lvItem.mask = LVIF_TEXT;
  1055. lvItem.iItem = nItem;
  1056. lvItem.iSubItem = COL_FRIENDLY_NAME;
  1057. lvItem.pszText = (LPWSTR)(LPCWSTR) pCert->GetFriendlyName ();
  1058. iResult = m_resultsList.SetItem (&lvItem);
  1059. ASSERT (-1 != iResult);
  1060. VERIFY (m_resultsList.Update (nItem));
  1061. _TRACE (-1, L"Leaving CFindDialog::RefreshItemInList\n");
  1062. }
  1063. void CFindDialog::DeleteAllResultItems()
  1064. {
  1065. _TRACE (1, L"Entering CFindDialog::DeleteAllResultItems\n");
  1066. int nCnt = m_resultsList.GetItemCount ();
  1067. ASSERT (-1 != nCnt);
  1068. CCertificate* pCert = 0;
  1069. for (int nIndex = 0; nIndex < nCnt; nIndex++)
  1070. {
  1071. pCert = (CCertificate*) m_resultsList.GetItemData (nIndex);
  1072. ASSERT (pCert);
  1073. if ( pCert )
  1074. pCert->Release ();
  1075. }
  1076. VERIFY (m_resultsList.DeleteAllItems ());
  1077. _TRACE (-1, L"Leaving CFindDialog::DeleteAllResultItems\n");
  1078. }
  1079. void CFindDialog::OnRclickResultList(NMHDR* pNMHDR, LRESULT* pResult)
  1080. {
  1081. _TRACE (1, L"Entering CFindDialog::OnRclickResultList\n");
  1082. m_hWndWhatsThis = 0;
  1083. CMenu bar;
  1084. if ( bar.LoadMenu(IDR_FIND_DLG_CONTEXT_MENU) )
  1085. {
  1086. CMenu& popup = *bar.GetSubMenu (0);
  1087. ASSERT(popup.m_hMenu);
  1088. CPoint point;
  1089. NM_LISTVIEW* pnmv = (NM_LISTVIEW FAR *) pNMHDR;
  1090. bool bIsMyStore = false;
  1091. if ( -1 == pnmv->iItem )
  1092. return; // mouse not over valid item
  1093. DWORD_PTR dwItemData = m_resultsList.GetItemData (pnmv->iItem);
  1094. ASSERT (dwItemData);
  1095. if ( dwItemData )
  1096. {
  1097. CCertificate* pCert = (CCertificate*) dwItemData;
  1098. bIsMyStore = (pCert->GetStoreType () == MY_STORE);
  1099. }
  1100. int nSelCnt = m_resultsList.GetSelectedCount ();
  1101. switch (nSelCnt)
  1102. {
  1103. case 0:
  1104. popup.EnableMenuItem (ID_VIEW, MF_GRAYED);
  1105. popup.EnableMenuItem (IDM_PROPERTIES, MF_GRAYED);
  1106. popup.EnableMenuItem (ID_FILE_RENEW_NEW_KEY, MF_GRAYED);
  1107. popup.EnableMenuItem (ID_FILE_RENEW_SAME_KEY, MF_GRAYED);
  1108. popup.EnableMenuItem (ID_FILE_EXPORT, MF_GRAYED);
  1109. popup.EnableMenuItem (ID_FILE_DELETE, MF_GRAYED);
  1110. break;
  1111. case 1:
  1112. popup.EnableMenuItem (ID_VIEW, MF_ENABLED);
  1113. popup.EnableMenuItem (IDM_PROPERTIES, MF_ENABLED);
  1114. if ( bIsMyStore && CERT_SYSTEM_STORE_SERVICES != m_pCompData->GetLocation () )
  1115. {
  1116. popup.EnableMenuItem (ID_ENROLL_NEW_KEY, MF_ENABLED);
  1117. popup.EnableMenuItem (ID_ENROLL_SAME_KEY, MF_ENABLED);
  1118. popup.EnableMenuItem (ID_FILE_RENEW_NEW_KEY, MF_ENABLED);
  1119. popup.EnableMenuItem (ID_FILE_RENEW_SAME_KEY, MF_ENABLED);
  1120. }
  1121. else
  1122. {
  1123. popup.EnableMenuItem (ID_ENROLL_NEW_KEY, MF_GRAYED);
  1124. popup.EnableMenuItem (ID_ENROLL_SAME_KEY, MF_GRAYED);
  1125. popup.EnableMenuItem (ID_FILE_RENEW_NEW_KEY, MF_GRAYED);
  1126. popup.EnableMenuItem (ID_FILE_RENEW_SAME_KEY, MF_GRAYED);
  1127. }
  1128. popup.EnableMenuItem (ID_FILE_EXPORT, MF_ENABLED);
  1129. popup.EnableMenuItem (ID_FILE_DELETE, MF_ENABLED);
  1130. break;
  1131. default:
  1132. popup.EnableMenuItem (ID_VIEW, MF_GRAYED);
  1133. popup.EnableMenuItem (IDM_PROPERTIES, MF_GRAYED);
  1134. popup.EnableMenuItem (ID_ENROLL_NEW_KEY, MF_GRAYED);
  1135. popup.EnableMenuItem (ID_ENROLL_SAME_KEY, MF_GRAYED);
  1136. popup.EnableMenuItem (ID_FILE_RENEW_NEW_KEY, MF_GRAYED);
  1137. popup.EnableMenuItem (ID_FILE_RENEW_SAME_KEY, MF_GRAYED);
  1138. popup.EnableMenuItem (ID_FILE_EXPORT, MF_ENABLED);
  1139. popup.EnableMenuItem (ID_FILE_DELETE, MF_ENABLED);
  1140. break;
  1141. }
  1142. GetCursorPos (&point);
  1143. m_hWndWhatsThis = GetDlgItem (IDC_RESULT_LIST)->m_hWnd;
  1144. popup.TrackPopupMenu (TPM_RIGHTBUTTON | TPM_LEFTBUTTON,
  1145. point.x, point.y,
  1146. this); // route commands through main window
  1147. }
  1148. *pResult = 0;
  1149. _TRACE (-1, L"Leaving CFindDialog::OnRclickResultList\n");
  1150. }
  1151. void CFindDialog::ChangeViewStyle (DWORD dwNewStyle)
  1152. {
  1153. _TRACE (1, L"Entering CFindDialog::ChangeViewStyle\n");
  1154. if ( m_resultsList.m_hWnd )
  1155. {
  1156. DWORD dwStyle = ::GetWindowLong (m_resultsList.m_hWnd, GWL_STYLE);
  1157. ::SetWindowLong (m_resultsList.m_hWnd, GWL_STYLE,
  1158. (dwStyle & ~LVS_TYPEMASK) | dwNewStyle);
  1159. }
  1160. _TRACE (-1, L"Leaving CFindDialog::ChangeViewStyle\n");
  1161. }
  1162. void CFindDialog::OnDelete()
  1163. {
  1164. _TRACE (1, L"Entering CFindDialog::OnDelete\n");
  1165. if ( m_resultsList.m_hWnd )
  1166. {
  1167. int nCnt = m_resultsList.GetItemCount ();
  1168. ASSERT (nCnt >= 1);
  1169. CCertificate* pCert = 0;
  1170. CString text;
  1171. CString caption;
  1172. int nSelCnt = m_resultsList.GetSelectedCount ();
  1173. ASSERT (nSelCnt >= 1);
  1174. VERIFY (text.LoadString (1 == nSelCnt ? IDS_CONFIRM_DELETE : IDS_CONFIRM_DELETE_MULTIPLE));
  1175. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  1176. CThemeContextActivator activator;
  1177. if ( MessageBox (text, caption, MB_ICONWARNING | MB_YESNO) == IDYES )
  1178. {
  1179. UINT flag = 0;
  1180. while (--nCnt >= 0)
  1181. {
  1182. flag = ListView_GetItemState (m_resultsList.m_hWnd, nCnt, LVIS_SELECTED);
  1183. if ( flag & LVNI_SELECTED )
  1184. {
  1185. pCert = (CCertificate*) m_resultsList.GetItemData (nCnt);
  1186. ASSERT (pCert);
  1187. if ( pCert )
  1188. {
  1189. if ( pCert->DeleteFromStore (true) )
  1190. {
  1191. m_bConsoleRefreshRequired = true;
  1192. if ( m_resultsList.DeleteItem (nCnt) )
  1193. pCert->Release ();
  1194. }
  1195. else
  1196. {
  1197. DWORD dwErr = GetLastError ();
  1198. ASSERT (E_ACCESSDENIED == dwErr);
  1199. if ( E_ACCESSDENIED == dwErr )
  1200. {
  1201. DisplaySystemError ();
  1202. }
  1203. }
  1204. }
  1205. }
  1206. }
  1207. }
  1208. EnableMenuItems ();
  1209. }
  1210. _TRACE (-1, L"Leaving CFindDialog::OnDelete\n");
  1211. }
  1212. DWORD CFindDialog::DisplaySystemError()
  1213. {
  1214. LPVOID lpMsgBuf;
  1215. DWORD dwErr = GetLastError ();
  1216. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  1217. NULL,
  1218. dwErr,
  1219. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  1220. (LPWSTR) &lpMsgBuf, 0, NULL );
  1221. // Display the string.
  1222. CString caption;
  1223. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  1224. CThemeContextActivator activator;
  1225. MessageBox ((LPWSTR) lpMsgBuf, (LPCWSTR) caption, MB_OK);
  1226. // Free the buffer.
  1227. LocalFree (lpMsgBuf);
  1228. return dwErr;
  1229. }
  1230. CCertificate* CFindDialog::GetSelectedCertificate (int * pnSelItem)
  1231. {
  1232. _TRACE (1, L"Entering CFindDialog::GetSelectedCertificate\n");
  1233. CCertificate* pCert = 0;
  1234. if ( m_resultsList.m_hWnd )
  1235. {
  1236. int nCnt = m_resultsList.GetItemCount ();
  1237. int nSelCnt = m_resultsList.GetSelectedCount ();
  1238. ASSERT (1 == nSelCnt);
  1239. if ( 1 == nSelCnt )
  1240. {
  1241. UINT flag = 0;
  1242. while (--nCnt >= 0)
  1243. {
  1244. flag = ListView_GetItemState (m_resultsList.m_hWnd, nCnt, LVIS_SELECTED);
  1245. if ( flag & LVNI_SELECTED )
  1246. {
  1247. pCert = (CCertificate*) m_resultsList.GetItemData (nCnt);
  1248. ASSERT (pCert);
  1249. if ( pCert && pnSelItem )
  1250. *pnSelItem = nCnt;
  1251. break;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. _TRACE (-1, L"Leaving CFindDialog::GetSelectedCertificate\n");
  1257. return pCert;
  1258. }
  1259. void CFindDialog::OnProperties()
  1260. {
  1261. _TRACE (1, L"Entering CFindDialog::OnProperties\n");
  1262. int nSelItem = 0;
  1263. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  1264. ASSERT (pCert);
  1265. if ( pCert )
  1266. {
  1267. HCERTSTORE* pPropPageStores = new HCERTSTORE[1];
  1268. if ( pPropPageStores && pCert->GetCertStore () )
  1269. {
  1270. pPropPageStores[0] = pCert->GetCertStore ()->GetStoreHandle ();
  1271. CRYPTUI_VIEWCERTIFICATEPROPERTIES_STRUCT sps;
  1272. ::ZeroMemory (&sps, sizeof (sps));
  1273. sps.dwSize = sizeof (sps);
  1274. sps.hwndParent = m_hWnd;
  1275. sps.dwFlags = 0;
  1276. sps.pCertContext = pCert->GetNewCertContext ();
  1277. sps.cStores = 1;
  1278. sps.rghStores = pPropPageStores;
  1279. BOOL fPropertiesChanged = FALSE;
  1280. CThemeContextActivator activator;
  1281. BOOL bResult = ::CryptUIDlgViewCertificateProperties (&sps, &fPropertiesChanged);
  1282. if ( bResult )
  1283. {
  1284. if ( fPropertiesChanged )
  1285. {
  1286. m_bConsoleRefreshRequired = true;
  1287. if ( pCert->GetCertStore () )
  1288. pCert->GetCertStore ()->SetDirty ();
  1289. pCert->Refresh ();
  1290. RefreshItemInList (pCert, nSelItem);
  1291. }
  1292. }
  1293. if ( pCert->GetCertStore () )
  1294. pCert->GetCertStore ()->Close ();
  1295. delete [] pPropPageStores;
  1296. }
  1297. }
  1298. _TRACE (-1, L"Leaving CFindDialog::OnProperties\n");
  1299. }
  1300. void CFindDialog::OnView()
  1301. {
  1302. _TRACE (1, L"Entering CFindDialog::OnView\n");
  1303. int nSelItem = 0;
  1304. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  1305. ASSERT (pCert);
  1306. if ( pCert )
  1307. {
  1308. LaunchCommonCertDialog (pCert, nSelItem);
  1309. }
  1310. _TRACE (-1, L"Leaving CFindDialog::OnView\n");
  1311. }
  1312. void CFindDialog::OnFileDelete()
  1313. {
  1314. _TRACE (1, L"Entering CFindDialog::OnFileDelete\n");
  1315. OnDelete ();
  1316. _TRACE (-1, L"Leaving CFindDialog::OnFileDelete\n");
  1317. }
  1318. void CFindDialog::OnEnrollNewKey()
  1319. {
  1320. _TRACE (1, L"Entering CFindDialog::OnEnrollNewKey\n");
  1321. OnEnroll (true);
  1322. _TRACE (-1, L"Leaving CFindDialog::OnEnrollNewKey\n");
  1323. }
  1324. void CFindDialog::OnEnrollSameKey()
  1325. {
  1326. _TRACE (1, L"Entering CFindDialog::OnEnrollSameKey\n");
  1327. OnEnroll (false);
  1328. _TRACE (-1, L"Leaving CFindDialog::OnEnrollSameKey\n");
  1329. }
  1330. void CFindDialog::OnEnroll(bool bNewKey)
  1331. {
  1332. _TRACE (1, L"Entering CFindDialog::OnEnroll\n");
  1333. int nSelItem = 0;
  1334. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  1335. ASSERT (pCert);
  1336. if ( pCert )
  1337. {
  1338. CRYPTUI_WIZ_CERT_REQUEST_PVK_CERT pvkCert;
  1339. CRYPTUI_WIZ_CERT_REQUEST_PVK_NEW pvkNew;
  1340. CRYPTUI_WIZ_CERT_REQUEST_INFO cri;
  1341. CRYPT_KEY_PROV_INFO ckpi;
  1342. ::ZeroMemory (&cri, sizeof (cri));
  1343. cri.dwSize = sizeof (cri);
  1344. cri.dwPurpose = CRYPTUI_WIZ_CERT_ENROLL;
  1345. // User wants to manage user account
  1346. // pass in NULL to machine name and to account name
  1347. // User wants to manage local machine account
  1348. // pass in NULL for account name and result of ::GetComputerName ()
  1349. // to machine name
  1350. // User want to manage remote machine
  1351. // pass in NULL for account name and machine name for machineName
  1352. // User wants to manage remote account on remote machine
  1353. // pass in account name for accountName and machine name for machineName
  1354. // TODO: Ensure that this is NULL if the local machine
  1355. switch (m_pCompData->GetLocation ())
  1356. {
  1357. case CERT_SYSTEM_STORE_CURRENT_SERVICE:
  1358. case CERT_SYSTEM_STORE_SERVICES:
  1359. cri.pwszMachineName = (LPCWSTR) m_szMachineName;
  1360. cri.pwszAccountName = (LPCWSTR) m_pCompData->GetManagedService ();
  1361. break;
  1362. case CERT_SYSTEM_STORE_CURRENT_USER:
  1363. cri.pwszMachineName = NULL;
  1364. cri.pwszAccountName = NULL;
  1365. break;
  1366. case CERT_SYSTEM_STORE_LOCAL_MACHINE:
  1367. cri.pwszMachineName = (LPCWSTR) m_szMachineName;
  1368. cri.pwszAccountName = NULL;
  1369. break;
  1370. default:
  1371. ASSERT (0);
  1372. return;
  1373. break;
  1374. }
  1375. if ( bNewKey )
  1376. {
  1377. cri.dwPvkChoice = CRYPTUI_WIZ_CERT_REQUEST_PVK_CHOICE_NEW;
  1378. ::ZeroMemory (&pvkNew, sizeof (pvkNew));
  1379. pvkNew.dwSize = sizeof (pvkNew);
  1380. cri.pPvkNew = &pvkNew;
  1381. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == m_pCompData->GetLocation () )
  1382. {
  1383. ::ZeroMemory (&ckpi, sizeof (ckpi));
  1384. ckpi.dwFlags = CRYPT_MACHINE_KEYSET;
  1385. pvkNew.pKeyProvInfo = &ckpi;
  1386. }
  1387. }
  1388. else
  1389. {
  1390. if ( IsLocalComputername (m_pCompData->m_szManagedComputer) )
  1391. {
  1392. DWORD dwFlags = 0;
  1393. if ( CERT_SYSTEM_STORE_LOCAL_MACHINE == m_pCompData->m_dwLocationPersist )
  1394. dwFlags = CRYPT_FIND_MACHINE_KEYSET_FLAG;
  1395. if ( !::CryptFindCertificateKeyProvInfo (
  1396. pCert->GetCertContext (), dwFlags, 0) )
  1397. {
  1398. CString text;
  1399. CString caption;
  1400. CThemeContextActivator activator;
  1401. VERIFY (text.LoadString (IDS_NO_PRIVATE_KEY));
  1402. VERIFY (caption.LoadString (IDS_REQUEST_CERT_SAME_KEY));
  1403. MessageBox (text, caption, MB_OK);
  1404. return;
  1405. }
  1406. }
  1407. cri.dwPvkChoice = CRYPTUI_WIZ_CERT_REQUEST_PVK_CHOICE_CERT;
  1408. ::ZeroMemory (&pvkCert, sizeof (pvkCert));
  1409. pvkCert.dwSize = sizeof (pvkCert);
  1410. pvkCert.pCertContext = pCert->GetCertContext ();
  1411. cri.pPvkCert = &pvkCert;
  1412. }
  1413. CString dnName;
  1414. dnName.FormatMessage (L"CN=%1", m_szLoggedInUser);
  1415. cri.pwszCertDNName = (LPWSTR) (LPCWSTR) dnName;
  1416. DWORD status = 0;
  1417. PCCERT_CONTEXT pNewCertContext = 0;
  1418. BOOL bResult = FALSE;
  1419. CThemeContextActivator activator;
  1420. while (1)
  1421. {
  1422. bResult = ::CryptUIWizCertRequest (
  1423. bNewKey ? CRYPTUI_WIZ_CERT_REQUEST_REQUIRE_NEW_KEY : 0,
  1424. m_hWnd,
  1425. NULL,
  1426. &cri, &pNewCertContext, &status);
  1427. if ( !bResult && HRESULT_FROM_WIN32 (NTE_TOKEN_KEYSET_STORAGE_FULL) == GetLastError () )
  1428. {
  1429. // NTRAID# 299089 Enrollment Wizard: Should return some
  1430. // meaningful message when users fail to enroll/renew on a
  1431. // smart card
  1432. if ( !bNewKey )
  1433. break;
  1434. CString text;
  1435. CString caption;
  1436. VERIFY (text.LoadString (IDS_SMARTCARD_FULL_REUSE_PRIVATE_KEY));
  1437. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  1438. if ( IDYES == MessageBox (text, caption, MB_YESNO) )
  1439. {
  1440. bNewKey = false;
  1441. }
  1442. else
  1443. break;
  1444. }
  1445. else
  1446. break;
  1447. }
  1448. if ( pNewCertContext )
  1449. CertFreeCertificateContext (pNewCertContext);
  1450. }
  1451. ResetMenu ();
  1452. _TRACE (-1, L"Leaving CFindDialog::OnEnroll\n");
  1453. }
  1454. void CFindDialog::OnFileExport()
  1455. {
  1456. _TRACE (1, L"Entering CFindDialog::OnFileExport\n");
  1457. UINT nSelCnt = m_resultsList.GetSelectedCount ();
  1458. if ( 1 == nSelCnt )
  1459. {
  1460. int nSelItem = 0;
  1461. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  1462. ASSERT (pCert);
  1463. if ( pCert )
  1464. {
  1465. CRYPTUI_WIZ_EXPORT_INFO cwi;
  1466. ::ZeroMemory (&cwi, sizeof (cwi));
  1467. cwi.dwSize = sizeof (cwi);
  1468. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
  1469. cwi.pCertContext = pCert->GetCertContext ();
  1470. CThemeContextActivator activator;
  1471. ::CryptUIWizExport (
  1472. 0,
  1473. m_hWnd,
  1474. 0,
  1475. &cwi,
  1476. NULL);
  1477. }
  1478. }
  1479. else if ( nSelCnt > 1 )
  1480. {
  1481. HCERTSTORE hCertStore = ::CertOpenStore (CERT_STORE_PROV_MEMORY,
  1482. 0, NULL, 0, NULL);
  1483. ASSERT (hCertStore);
  1484. if ( hCertStore )
  1485. {
  1486. CCertificate* pCert = 0;
  1487. int nCnt = m_resultsList.GetItemCount ();
  1488. UINT flag = 0;
  1489. BOOL bResult = FALSE;
  1490. while (--nCnt >= 0)
  1491. {
  1492. flag = ListView_GetItemState (m_resultsList.m_hWnd, nCnt, LVIS_SELECTED);
  1493. if ( flag & LVNI_SELECTED )
  1494. {
  1495. pCert = (CCertificate*) m_resultsList.GetItemData (nCnt);
  1496. ASSERT (pCert);
  1497. if ( pCert )
  1498. {
  1499. bResult = ::CertAddCertificateContextToStore (
  1500. hCertStore,
  1501. ::CertDuplicateCertificateContext (pCert->GetCertContext ()),
  1502. CERT_STORE_ADD_NEW, 0);
  1503. ASSERT (bResult);
  1504. if ( !bResult )
  1505. break;
  1506. }
  1507. }
  1508. }
  1509. // Call Export Wizard
  1510. CRYPTUI_WIZ_EXPORT_INFO cwi;
  1511. ::ZeroMemory (&cwi, sizeof (cwi));
  1512. cwi.dwSize = sizeof (cwi);
  1513. cwi.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY;
  1514. cwi.hCertStore = hCertStore;
  1515. CThemeContextActivator activator;
  1516. bResult = ::CryptUIWizExport (
  1517. 0,
  1518. m_hWnd,
  1519. 0,
  1520. &cwi,
  1521. NULL);
  1522. VERIFY (::CertCloseStore (hCertStore, CERT_CLOSE_STORE_CHECK_FLAG));
  1523. }
  1524. else
  1525. {
  1526. _TRACE (0, L"CertOpenStore (CERT_STORE_PROVIDER_MEMORY) failed: 0x%x\n",
  1527. GetLastError ());
  1528. }
  1529. }
  1530. _TRACE (-1, L"Leaving CFindDialog::OnFileExport\n");
  1531. }
  1532. void CFindDialog::OnFileRenewNewKey()
  1533. {
  1534. _TRACE (1, L"Entering CFindDialog::OnFileRenewNewKey\n");
  1535. OnFileRenew (true);
  1536. _TRACE (-1, L"Leaving CFindDialog::OnFileRenewNewKey\n");
  1537. }
  1538. void CFindDialog::OnFileRenewSameKey()
  1539. {
  1540. _TRACE (1, L"Entering CFindDialog::OnFileRenewSameKey\n");
  1541. OnFileRenew (false);
  1542. _TRACE (-1, L"Leaving CFindDialog::OnFileRenewSameKey\n");
  1543. }
  1544. void CFindDialog::OnFileRenew(bool bNewKey)
  1545. {
  1546. _TRACE (1, L"Entering CFindDialog::OnFileRenew\n");
  1547. int nSelItem = 0;
  1548. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  1549. ASSERT (pCert);
  1550. if ( pCert )
  1551. {
  1552. RenewCertificate (
  1553. pCert,
  1554. bNewKey,
  1555. m_szMachineName,
  1556. m_pCompData->GetLocation (),
  1557. m_pCompData->GetManagedComputer (),
  1558. m_pCompData->GetManagedService (),
  1559. m_hWnd,
  1560. 0,
  1561. 0);
  1562. }
  1563. ResetMenu ();
  1564. _TRACE (-1, L"Leaving CFindDialog::OnFileRenew\n");
  1565. }
  1566. void CFindDialog::OnEditInvertselection()
  1567. {
  1568. _TRACE (1, L"Entering CFindDialog::OnEditInvertselection\n");
  1569. if ( m_resultsList.m_hWnd )
  1570. {
  1571. int iItem = -1;
  1572. while ((iItem = ListView_GetNextItem (m_resultsList.m_hWnd, iItem, 0)) != -1)
  1573. {
  1574. UINT flag;
  1575. // flip the selection bit on each item
  1576. flag = ListView_GetItemState (m_resultsList.m_hWnd, iItem, LVIS_SELECTED);
  1577. flag ^= LVNI_SELECTED;
  1578. ListView_SetItemState (m_resultsList.m_hWnd, iItem, flag, LVIS_SELECTED);
  1579. }
  1580. }
  1581. _TRACE (-1, L"Leaving CFindDialog::OnEditInvertselection\n");
  1582. }
  1583. void CFindDialog::OnEditSelectall()
  1584. {
  1585. _TRACE (1, L"Entering CFindDialog::OnEditSelectall\n");
  1586. if ( m_resultsList.m_hWnd )
  1587. ListView_SetItemState (m_resultsList.m_hWnd, -1, LVIS_SELECTED, LVIS_SELECTED);
  1588. _TRACE (-1, L"Leaving CFindDialog::OnEditSelectall\n");
  1589. }
  1590. void CFindDialog::OnFileProperties()
  1591. {
  1592. _TRACE (1, L"Entering CFindDialog::OnFileProperties\n");
  1593. OnProperties ();
  1594. _TRACE (-1, L"Leaving CFindDialog::OnFileProperties\n");
  1595. }
  1596. void CFindDialog::OnHelpHelptopics()
  1597. {
  1598. _TRACE (1, L"Entering CFindDialog::OnHelpHelptopics\n");
  1599. CComPtr<IDisplayHelp> spDisplayHelp;
  1600. HRESULT hr = m_pCompData->m_pConsole->QueryInterface (
  1601. IID_PPV_ARG (IDisplayHelp, &spDisplayHelp));
  1602. ASSERT (SUCCEEDED (hr));
  1603. if ( SUCCEEDED (hr) )
  1604. {
  1605. CString helpTopic;
  1606. UINT nLen = ::GetSystemWindowsDirectory (helpTopic.GetBufferSetLength(2 * MAX_PATH), 2 * MAX_PATH);
  1607. helpTopic.ReleaseBuffer();
  1608. if (0 == nLen)
  1609. {
  1610. ASSERT(FALSE);
  1611. return;
  1612. }
  1613. helpTopic += L"\\help\\";
  1614. helpTopic += CM_LINKED_HELP_FILE; //CM_HELP_FILE; //CM_LINKED_HELP_FILE;
  1615. helpTopic += L"::/";
  1616. helpTopic += CM_HELP_TOPIC;
  1617. hr = spDisplayHelp->ShowTopic (T2OLE ((LPWSTR)(LPCWSTR) helpTopic));
  1618. ASSERT (SUCCEEDED (hr));
  1619. }
  1620. if ( !SUCCEEDED (hr) )
  1621. {
  1622. CString caption;
  1623. CString text;
  1624. CThemeContextActivator activator;
  1625. VERIFY (caption.LoadString (IDS_FIND_CERT));
  1626. text.FormatMessage (IDS_CERTMGR_CHM_NOT_FOUND, CM_HELP_FILE);
  1627. MessageBox (text, caption, MB_OK);
  1628. }
  1629. _TRACE (-1, L"Leaving CFindDialog::OnHelpHelptopics\n");
  1630. }
  1631. void CFindDialog::OnViewDetails()
  1632. {
  1633. _TRACE (1, L"Entering CFindDialog::OnViewDetails\n");
  1634. ChangeViewStyle (LVS_REPORT);
  1635. _TRACE (-1, L"Leaving CFindDialog::OnViewDetails\n");
  1636. }
  1637. void CFindDialog::OnViewLargeicons()
  1638. {
  1639. _TRACE (1, L"Entering CFindDialog::OnViewLargeicons\n");
  1640. ChangeViewStyle (LVS_ICON);
  1641. _TRACE (-1, L"Leaving CFindDialog::OnViewLargeicons\n");
  1642. }
  1643. void CFindDialog::OnViewList()
  1644. {
  1645. _TRACE (1, L"Entering CFindDialog::OnViewList\n");
  1646. ChangeViewStyle (LVS_LIST);
  1647. _TRACE (-1, L"Leaving CFindDialog::OnViewList\n");
  1648. }
  1649. void CFindDialog::OnViewSmallicons()
  1650. {
  1651. _TRACE (1, L"Entering CFindDialog::OnViewSmallicons\n");
  1652. ChangeViewStyle (LVS_SMALLICON);
  1653. _TRACE (-1, L"Leaving CFindDialog::OnViewSmallicons\n");
  1654. }
  1655. HRESULT CFindDialog::SearchForNames(const CString & szFindText, DWORD dwFindType)
  1656. {
  1657. _TRACE (0, L"Entering CFindDialog::SearchForNames - %s\n", (LPCWSTR) szFindText);
  1658. HRESULT hr = S_OK;
  1659. void* pvFindPara = (void*) (LPCWSTR) szFindText;
  1660. int nCurSel = m_storeList.GetCurSel ();
  1661. CCertStore* pStore = (CCertStore*) m_storeList.GetItemData (nCurSel);
  1662. DWORD dwFindFlags = 0;
  1663. if ( pStore )
  1664. {
  1665. SearchForNameOnStore (dwFindFlags, dwFindType, pvFindPara,
  1666. *pStore);
  1667. }
  1668. else
  1669. {
  1670. // if pStore is 0, then search on all stores in store list
  1671. int nCnt = m_storeList.GetCount ();
  1672. ASSERT (CB_ERR != nCnt);
  1673. for (int nIndex = 0;
  1674. nIndex < nCnt && WAIT_TIMEOUT == WaitForSingleObject (m_hCancelSearchEvent, 0);
  1675. nIndex++)
  1676. {
  1677. pStore = (CCertStore*) m_storeList.GetItemData (nIndex);
  1678. if ( pStore )
  1679. {
  1680. SearchForNameOnStore (dwFindFlags, dwFindType,
  1681. pvFindPara, *pStore);
  1682. }
  1683. }
  1684. }
  1685. _TRACE (-1, L"Leaving CFindDialog::SearchForNames - %s\n", (LPCWSTR) szFindText);
  1686. return hr;
  1687. }
  1688. HRESULT CFindDialog::SearchForText (CString & szFindText, DWORD dwPropId)
  1689. {
  1690. _TRACE (0, L"Entering CFindDialog::SearchForText - %s\n", (LPCWSTR) szFindText);
  1691. HRESULT hr = S_OK;
  1692. int nCurSel = m_storeList.GetCurSel ();
  1693. CCertStore* pStore = (CCertStore*) m_storeList.GetItemData (nCurSel);
  1694. szFindText.MakeUpper ();
  1695. if ( pStore && pStore->GetStoreHandle () )
  1696. {
  1697. SearchForTextOnStore (dwPropId, szFindText, *pStore);
  1698. pStore->Close ();
  1699. }
  1700. else
  1701. {
  1702. // if hCertStore is 0, then search on all stores in store list
  1703. int nCnt = m_storeList.GetCount ();
  1704. ASSERT (CB_ERR != nCnt);
  1705. for (int nIndex = 0;
  1706. nIndex < nCnt && WAIT_TIMEOUT == WaitForSingleObject (m_hCancelSearchEvent, 0);
  1707. nIndex++)
  1708. {
  1709. pStore = (CCertStore*) m_storeList.GetItemData (nIndex);
  1710. if ( pStore )
  1711. {
  1712. SearchForTextOnStore (dwPropId,
  1713. szFindText, *pStore);
  1714. pStore->Close ();
  1715. }
  1716. }
  1717. }
  1718. _TRACE (-1, L"Leaving CFindDialog::SearchForText - %s\n", (LPCWSTR) szFindText);
  1719. return hr;
  1720. }
  1721. void CFindDialog::RemoveSpaces(CString & text)
  1722. {
  1723. _TRACE (0, L"Entering CFindDialog::RemoveSpaces - %s\n", (LPCWSTR) text);
  1724. text.TrimLeft ();
  1725. text.TrimRight ();
  1726. int nLen = text.GetLength ();
  1727. LPCWSTR pszText = text.GetBuffer (nLen);
  1728. int nResultLen = nLen+2;
  1729. LPWSTR pszResult = new WCHAR[nResultLen];
  1730. if ( pszResult )
  1731. {
  1732. while (--nResultLen >= 0)
  1733. pszResult[nResultLen] = 0;
  1734. nResultLen = 0;
  1735. for (int nText = 0; nText < nLen; nText++)
  1736. {
  1737. if ( _T(" ")[0] != pszText[nText] )
  1738. pszResult[nResultLen++] = pszText[nText];
  1739. }
  1740. text.ReleaseBuffer ();
  1741. text = pszResult;
  1742. delete [] pszResult;
  1743. }
  1744. _TRACE (-1, L"Leaving CFindDialog::RemoveSpaces - %s\n", (LPCWSTR) text);
  1745. }
  1746. void CFindDialog::OnDblclkResultList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  1747. {
  1748. _TRACE (1, L"Entering CFindDialog::OnDblclkResultList\n");
  1749. OnOpen ();
  1750. *pResult = 0;
  1751. _TRACE (-1, L"Leaving CFindDialog::OnDblclkResultList\n");
  1752. }
  1753. int CALLBACK CFindDialog::CompareFunc (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  1754. {
  1755. CCertificate* pCert1 = (CCertificate*) lParam1;
  1756. CCertificate* pCert2 = (CCertificate*) lParam2;
  1757. int compVal = 0;
  1758. ASSERT (pCert1 && pCert2);
  1759. if ( pCert1 && pCert2 )
  1760. {
  1761. switch (lParamSort)
  1762. {
  1763. case COL_ISSUED_TO:
  1764. compVal = LocaleStrCmp (pCert1->GetSubjectName (), pCert2->GetSubjectName ());
  1765. break;
  1766. case COL_ISSUED_BY:
  1767. compVal = LocaleStrCmp (pCert1->GetIssuerName (), pCert2->GetIssuerName ());
  1768. break;
  1769. case COL_EXPIRATION_DATE:
  1770. compVal = pCert1->CompareExpireDate (*pCert2);
  1771. break;
  1772. case COL_PURPOSES:
  1773. compVal = LocaleStrCmp (pCert1->GetEnhancedKeyUsage (), pCert2->GetEnhancedKeyUsage ());
  1774. break;
  1775. case COL_FRIENDLY_NAME:
  1776. compVal = LocaleStrCmp (pCert1->GetFriendlyName (), pCert2->GetFriendlyName ());
  1777. break;
  1778. case COL_SOURCE_STORE:
  1779. if ( pCert1->GetCertStore () && pCert2->GetCertStore () )
  1780. compVal = LocaleStrCmp (pCert1->GetCertStore ()->GetLocalizedName (),
  1781. pCert2->GetCertStore ()->GetLocalizedName ());
  1782. break;
  1783. default:
  1784. ASSERT (0);
  1785. break;
  1786. }
  1787. }
  1788. return compVal;
  1789. }
  1790. void CFindDialog::OnColumnclickResultList(NMHDR* pNMHDR, LRESULT* pResult)
  1791. {
  1792. _TRACE (1, L"Entering CFindDialog::OnColumnclickResultList\n");
  1793. CWaitCursor cursor;
  1794. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  1795. BOOL bResult = m_resultsList.SortItems (CompareFunc, pNMListView->iSubItem);
  1796. ASSERT (bResult);
  1797. *pResult = 0;
  1798. _TRACE (-1, L"Leaving CFindDialog::OnColumnclickResultList\n");
  1799. }
  1800. void CFindDialog::DoContextHelp (HWND hWndControl)
  1801. {
  1802. _TRACE (1, L"Entering CFindDialog::DoContextHelp\n");
  1803. static const DWORD help_map[] =
  1804. {
  1805. IDC_STORE_LIST, IDH_FIND_STORE_LIST,
  1806. IDC_CONTAINS_TEXT, IDH_FIND_CONTAINS_TEXT,
  1807. IDC_FIELD_LIST, IDH_FIND_FIELD_LIST,
  1808. IDC_FIND_NOW, IDH_FIND_FIND_NOW,
  1809. IDC_STOP, IDH_FIND_STOP,
  1810. IDC_NEW_SEARCH, IDH_FIND_NEW_SEARCH,
  1811. IDC_RESULT_LIST, IDH_FIND_RESULT_LIST,
  1812. 0, 0
  1813. };
  1814. switch (::GetDlgCtrlID (hWndControl))
  1815. {
  1816. case IDC_STORE_LIST:
  1817. case IDC_CONTAINS_TEXT:
  1818. case IDC_FIELD_LIST:
  1819. case IDC_FIND_NOW:
  1820. case IDC_STOP:
  1821. case IDC_NEW_SEARCH:
  1822. case IDC_RESULT_LIST:
  1823. // Display context help for a control
  1824. if ( !::WinHelp (
  1825. hWndControl,
  1826. GetF1HelpFilename(),
  1827. HELP_WM_HELP,
  1828. (DWORD_PTR) help_map) )
  1829. {
  1830. _TRACE (0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  1831. }
  1832. break;
  1833. default:
  1834. break;
  1835. }
  1836. _TRACE (-1, L"Leaving CFindDialog::DoContextHelp\n");
  1837. }
  1838. void CFindDialog::OnChangeContainsText()
  1839. {
  1840. _TRACE (1, L"Entering CFindDialog::OnChangeContainsText\n");
  1841. int nLen = GetDlgItem (IDC_CONTAINS_TEXT)->GetWindowTextLength ();
  1842. // If the text starts with the invisible Left-to-Right marker, don't count
  1843. // it in the length
  1844. UpdateData (TRUE);
  1845. int nIndex = 0;
  1846. while ( chLEFT_TO_RIGHT == m_szContains.GetAt (nIndex) )
  1847. {
  1848. nLen--;
  1849. nIndex++;
  1850. }
  1851. m_findNowBtn.EnableWindow (nLen ? TRUE : FALSE);
  1852. _TRACE (-1, L"Leaving CFindDialog::OnChangeContainsText\n");
  1853. }
  1854. void CFindDialog::RestoreAfterSearchSettings()
  1855. {
  1856. _TRACE (1, L"Entering CFindDialog::RestoreAfterSearchSettings\n");
  1857. if ( m_bAnimationRunning )
  1858. {
  1859. VERIFY (m_animate.Stop ());
  1860. m_bAnimationRunning = false;
  1861. }
  1862. VERIFY (m_animate.Seek (0));
  1863. // Reenable the controls
  1864. m_stopBtn.EnableWindow (FALSE);
  1865. m_findNowBtn.EnableWindow (TRUE);
  1866. m_newSearchBtn.EnableWindow (TRUE);
  1867. GetDlgItem (IDC_CONTAINS_TEXT)->EnableWindow (TRUE);
  1868. m_resultsList.EnableWindow (TRUE);
  1869. m_fieldList.EnableWindow (TRUE);
  1870. m_storeList.EnableWindow (TRUE);
  1871. m_resultsList.SetFocus ();
  1872. EnableMenuItems ();
  1873. // NTRAID# 281799 Cert UI: Cert Snapin: Accessibility: Focus indicator
  1874. // cannot be seen in certificate list in Find Certificates
  1875. int nIndex = m_resultsList.GetTopIndex();
  1876. m_resultsList.SetFocus ();
  1877. m_resultsList.SetItemState (nIndex, LVIS_FOCUSED, LVIS_FOCUSED);
  1878. _TRACE (-1, L"Leaving CFindDialog::RestoreAfterSearchSettings\n");
  1879. }
  1880. //
  1881. // Initialize the result list view
  1882. //
  1883. void CFindDialog::SetUpResultList()
  1884. {
  1885. _TRACE (1, L"Entering CFindDialog::SetUpResultList\n");
  1886. // Set up result list view
  1887. COLORREF cr = RGB (255, 0, 255);
  1888. CThemeContextActivator activator;
  1889. VERIFY (m_imageListNormal.Create (IDB_CERTIFICATE_LARGE, 32, 0, cr));
  1890. VERIFY (m_imageListSmall.Create (IDB_CERTIFICATE_SMALL, 16, 0, cr));
  1891. m_resultsList.SetImageList (CImageList::FromHandle (m_imageListSmall), LVSIL_SMALL);
  1892. m_resultsList.SetImageList (CImageList::FromHandle (m_imageListNormal), LVSIL_NORMAL);
  1893. int colWidths[NUM_COLS] = {100, 100, 100, 100, 100, 400};
  1894. // Add "Issued To" column
  1895. CString szText;
  1896. VERIFY (szText.LoadString (IDS_ISSUED_TO));
  1897. VERIFY (m_resultsList.InsertColumn (COL_ISSUED_TO, (LPCWSTR) szText,
  1898. LVCFMT_LEFT, colWidths[COL_ISSUED_TO], COL_ISSUED_TO) != -1);
  1899. // Add "Issued By" column
  1900. VERIFY (szText.LoadString (IDS_ISSUED_BY));
  1901. VERIFY (m_resultsList.InsertColumn (COL_ISSUED_BY, (LPCWSTR) szText,
  1902. LVCFMT_LEFT, colWidths[COL_ISSUED_BY], COL_ISSUED_BY) != -1);
  1903. // Add "Expiration Date" column
  1904. VERIFY (szText.LoadString (IDS_COLUMN_EXPIRATION_DATE));
  1905. VERIFY (m_resultsList.InsertColumn (COL_EXPIRATION_DATE, (LPCWSTR) szText,
  1906. LVCFMT_LEFT, colWidths[COL_EXPIRATION_DATE], COL_EXPIRATION_DATE) != -1);
  1907. // Add "Purposes" column
  1908. VERIFY (szText.LoadString (IDS_COLUMN_PURPOSE));
  1909. VERIFY (m_resultsList.InsertColumn (COL_PURPOSES, (LPCWSTR) szText,
  1910. LVCFMT_LEFT, colWidths[COL_PURPOSES], COL_PURPOSES) != -1);
  1911. // Add "Friendly Name" column
  1912. VERIFY (szText.LoadString (IDS_COLUMN_FRIENDLY_NAME));
  1913. VERIFY (m_resultsList.InsertColumn (COL_FRIENDLY_NAME, (LPCWSTR) szText,
  1914. LVCFMT_LEFT, colWidths[COL_FRIENDLY_NAME], COL_FRIENDLY_NAME) != -1);
  1915. // Add "Source Store" column
  1916. VERIFY (szText.LoadString (IDS_COLUMN_SOURCE_STORE));
  1917. VERIFY (m_resultsList.InsertColumn (COL_SOURCE_STORE, (LPCWSTR) szText,
  1918. LVCFMT_LEFT, colWidths[COL_SOURCE_STORE], COL_SOURCE_STORE) != -1);
  1919. // Set to full-row select
  1920. DWORD dwExstyle = m_resultsList.GetExtendedStyle ();
  1921. m_resultsList.SetExtendedStyle (dwExstyle | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
  1922. _TRACE (-1, L"Leaving CFindDialog::SetUpResultList\n");
  1923. }
  1924. //
  1925. // Hide the portion of the dialog containing the result list. This part
  1926. // becomes visible when a search is performed.
  1927. //
  1928. void CFindDialog::HideResultList()
  1929. {
  1930. _TRACE (1, L"Entering CFindDialog::HideResultList\n");
  1931. // Size window to hide list view until search is performed
  1932. CRect rcList;
  1933. m_resultsList.GetWindowRect (&rcList);
  1934. ScreenToClient (&rcList);
  1935. CRect rcDlg;
  1936. GetWindowRect (&rcDlg);
  1937. ScreenToClient (&rcDlg);
  1938. int cyCaption = ::GetSystemMetrics (SM_CYCAPTION);
  1939. m_cyOriginal = (rcDlg.bottom - rcDlg.top) + cyCaption;
  1940. m_cyMin = (rcList.top - rcDlg.top) + cyCaption - 16;
  1941. m_cxMin = rcDlg.right - rcDlg.left;
  1942. VERIFY (SetWindowPos (&wndTop, rcDlg.left, rcDlg.top,
  1943. rcDlg.right - rcDlg.left,
  1944. m_cyMin,
  1945. SWP_NOMOVE | SWP_NOOWNERZORDER));
  1946. //
  1947. // Get information about the spatial relationship of the controls to the window.
  1948. // We will need this later for expanding and resizing the dialog.
  1949. // Get right margin to maintain distance of buttons to right side
  1950. //
  1951. CRect rcCtrl;
  1952. m_stopBtn.GetWindowRect (&rcCtrl);
  1953. ScreenToClient (&rcCtrl);
  1954. m_cxBtnMargin = rcDlg.right - rcCtrl.right;
  1955. m_animate.GetWindowRect (&rcCtrl);
  1956. ScreenToClient (&rcCtrl);
  1957. m_cxAnimMargin = rcDlg.right - rcCtrl.right;
  1958. m_storeList.GetWindowRect (&rcCtrl);
  1959. ScreenToClient (&rcCtrl);
  1960. m_cxStoreListMargin = rcDlg.right - rcCtrl.right;
  1961. GetDlgItem (IDC_CONTAINS_TEXT)->GetWindowRect (&rcCtrl);
  1962. ScreenToClient (&rcCtrl);
  1963. m_cxContainMargin = rcDlg.right - rcCtrl.right;
  1964. m_fieldList.GetWindowRect (&rcCtrl);
  1965. ScreenToClient (&rcCtrl);
  1966. m_cxFieldListMargin = rcDlg.right - rcCtrl.right;
  1967. _TRACE (-1, L"Leaving CFindDialog::HideResultList\n");
  1968. }
  1969. void CFindDialog::OnItemchangedResultList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  1970. {
  1971. _TRACE (1, L"Entering CFindDialog::OnItemchangedResultList\n");
  1972. EnableMenuItems ();
  1973. *pResult = 0;
  1974. _TRACE (-1, L"Leaving CFindDialog::OnItemchangedResultList\n");
  1975. }
  1976. void CFindDialog::OnOpen()
  1977. {
  1978. _TRACE (1, L"Entering CFindDialog::OnOpen\n");
  1979. int nSelItem = 0;
  1980. CCertificate* pCert = GetSelectedCertificate (&nSelItem);
  1981. ASSERT (pCert);
  1982. if ( pCert )
  1983. {
  1984. VERIFY (SUCCEEDED (LaunchCommonCertDialog (pCert, nSelItem)));
  1985. }
  1986. _TRACE (-1, L"Leaving CFindDialog::OnOpen\n");
  1987. }
  1988. HRESULT CFindDialog::LaunchCommonCertDialog (CCertificate* pCert, const int nItem)
  1989. {
  1990. _TRACE (1, L"Entering CFindDialog::LaunchCommonCertDialog\n");
  1991. ASSERT (pCert);
  1992. if ( !pCert )
  1993. return E_POINTER;
  1994. HRESULT hr = S_OK;
  1995. CTypedPtrList<CPtrList, CCertStore*> storeList;
  1996. // Add the Root store first on a remote machine.
  1997. if ( !IsLocalComputername (m_pCompData->GetManagedComputer ()) )
  1998. {
  1999. storeList.AddTail (new CCertStore (CERTMGR_LOG_STORE,
  2000. CERT_STORE_PROV_SYSTEM,
  2001. CERT_SYSTEM_STORE_LOCAL_MACHINE,
  2002. (LPCWSTR) m_pCompData->GetManagedComputer (),
  2003. ROOT_SYSTEM_STORE_NAME,
  2004. ROOT_SYSTEM_STORE_NAME,
  2005. _T (""), ROOT_STORE,
  2006. CERT_SYSTEM_STORE_LOCAL_MACHINE,
  2007. m_pCompData->m_pConsole));
  2008. }
  2009. CCertStore* pStore = pCert->GetCertStore ();
  2010. if ( pStore )
  2011. {
  2012. pStore->AddRef ();
  2013. storeList.AddTail (pStore);
  2014. hr = m_pCompData->EnumerateLogicalStores (&storeList);
  2015. if ( SUCCEEDED (hr) )
  2016. {
  2017. POSITION pos = 0;
  2018. POSITION prevPos = 0;
  2019. // Validate store handles
  2020. for (pos = storeList.GetHeadPosition ();
  2021. pos;)
  2022. {
  2023. prevPos = pos;
  2024. pStore = storeList.GetNext (pos);
  2025. ASSERT (pStore);
  2026. if ( pStore )
  2027. {
  2028. // Do not open the userDS store
  2029. if ( USERDS_STORE == pStore->GetStoreType () )
  2030. {
  2031. storeList.RemoveAt (prevPos);
  2032. pStore->Release ();
  2033. pStore = 0;
  2034. }
  2035. else
  2036. {
  2037. if ( !pStore->GetStoreHandle () )
  2038. {
  2039. CString caption;
  2040. CString text;
  2041. CThemeContextActivator activator;
  2042. text.FormatMessage (IDS_CANT_OPEN_STORE_AND_FAIL, pStore->GetLocalizedName ());
  2043. VERIFY (caption.LoadString (IDS_CERTIFICATE_MANAGER));
  2044. MessageBox (text, caption, MB_ICONWARNING | MB_OK);
  2045. hr = E_FAIL;
  2046. break;
  2047. }
  2048. }
  2049. }
  2050. }
  2051. // Proceed only if all handles are valid
  2052. if ( SUCCEEDED (hr) )
  2053. {
  2054. CRYPTUI_VIEWCERTIFICATE_STRUCT vcs;
  2055. ::ZeroMemory (&vcs, sizeof (vcs));
  2056. vcs.dwSize = sizeof (vcs);
  2057. vcs.hwndParent = m_hWnd;
  2058. // Set these flags only on a remote machine.
  2059. if ( !IsLocalComputername (m_pCompData->GetManagedComputer ()) )
  2060. vcs.dwFlags = CRYPTUI_DONT_OPEN_STORES | CRYPTUI_WARN_UNTRUSTED_ROOT;
  2061. else
  2062. vcs.dwFlags = 0;
  2063. vcs.pCertContext = pCert->GetNewCertContext ();
  2064. vcs.cStores = (DWORD)storeList.GetCount ();
  2065. vcs.rghStores = new HCERTSTORE[vcs.cStores];
  2066. if ( vcs.rghStores )
  2067. {
  2068. DWORD index = 0;
  2069. for (pos = storeList.GetHeadPosition ();
  2070. pos && index < vcs.cStores;
  2071. index++)
  2072. {
  2073. pStore = storeList.GetNext (pos);
  2074. ASSERT (pStore);
  2075. if ( pStore )
  2076. {
  2077. vcs.rghStores[index] = pStore->GetStoreHandle ();
  2078. }
  2079. }
  2080. BOOL fPropertiesChanged = FALSE;
  2081. CThemeContextActivator activator;
  2082. BOOL bResult = ::CryptUIDlgViewCertificate (&vcs, &fPropertiesChanged);
  2083. if ( bResult )
  2084. {
  2085. if ( fPropertiesChanged )
  2086. {
  2087. m_bConsoleRefreshRequired = true;
  2088. if ( pCert->GetCertStore () )
  2089. pCert->GetCertStore ()->SetDirty ();
  2090. pCert->Refresh ();
  2091. RefreshItemInList (pCert, nItem);
  2092. }
  2093. }
  2094. delete vcs.rghStores;
  2095. }
  2096. else
  2097. hr = E_OUTOFMEMORY;
  2098. }
  2099. }
  2100. // Release all the stores in the list.
  2101. while (!storeList.IsEmpty () )
  2102. {
  2103. pStore = storeList.RemoveHead ();
  2104. ASSERT (pStore);
  2105. if ( pStore )
  2106. {
  2107. pStore->Close ();
  2108. pStore->Release ();
  2109. }
  2110. }
  2111. }
  2112. _TRACE (-1, L"Leaving CFindDialog::LaunchCommonCertDialog\n");
  2113. return hr;
  2114. }
  2115. void CFindDialog::ChangeToSizableFrame()
  2116. {
  2117. _TRACE (1, L"Entering CFindDialog::ChangeToSizableFrame\n");
  2118. LONG lStyle = ::GetWindowLong (m_hWnd, GWL_STYLE);
  2119. if ( lStyle )
  2120. {
  2121. lStyle &= ~DS_MODALFRAME;
  2122. lStyle |= WS_THICKFRAME;
  2123. if ( !::SetWindowLong (m_hWnd, GWL_STYLE, lStyle) )
  2124. {
  2125. _TRACE (0, L"SetWindowLong () failed: 0x%x\n", GetLastError ());
  2126. }
  2127. }
  2128. else
  2129. {
  2130. _TRACE (0, L"GetWindowLong () failed: 0x%x\n", GetLastError ());
  2131. }
  2132. _TRACE (-1, L"Leaving CFindDialog::ChangeToSizableFrame\n");
  2133. }
  2134. void CFindDialog::OnContextMenu(CWnd* /*pWnd*/, CPoint scrPoint)
  2135. {
  2136. // point is in screen coordinates
  2137. _TRACE (1, L"Entering CFindDialog::OnContextMenu\n");
  2138. CMenu bar;
  2139. CPoint clPoint;
  2140. clPoint.x = scrPoint.x;
  2141. clPoint.y = scrPoint.y;
  2142. ScreenToClient (&clPoint);
  2143. // Get the handle of the window under the point.
  2144. CWnd* pChild = ChildWindowFromPoint (
  2145. clPoint, // in client coordinates
  2146. CWP_SKIPINVISIBLE | CWP_SKIPTRANSPARENT);
  2147. if ( pChild && pChild->m_hWnd != GetDlgItem (IDC_RESULT_LIST)->m_hWnd )
  2148. {
  2149. m_hWndWhatsThis = 0;
  2150. if ( GetDlgItem (IDC_STORE_LIST)->m_hWnd == pChild->m_hWnd ||
  2151. GetDlgItem (IDC_FIND_NOW)->m_hWnd == pChild->m_hWnd ||
  2152. GetDlgItem (IDC_STOP)->m_hWnd == pChild->m_hWnd ||
  2153. GetDlgItem (IDC_NEW_SEARCH)->m_hWnd == pChild->m_hWnd ||
  2154. GetDlgItem (IDC_RESULT_LIST)->m_hWnd == pChild->m_hWnd ||
  2155. GetDlgItem (IDC_CONTAINS_TEXT)->m_hWnd == pChild->m_hWnd ||
  2156. GetDlgItem (IDC_FIELD_LIST)->m_hWnd == pChild->m_hWnd )
  2157. {
  2158. m_hWndWhatsThis = pChild->m_hWnd;
  2159. }
  2160. if ( m_hWndWhatsThis )
  2161. {
  2162. if ( bar.LoadMenu(IDR_WHATS_THIS_CONTEXT_MENU1) )
  2163. {
  2164. CMenu& popup = *bar.GetSubMenu (0);
  2165. ASSERT(popup.m_hMenu);
  2166. if ( !popup.TrackPopupMenu (TPM_RIGHTBUTTON | TPM_LEFTBUTTON,
  2167. scrPoint.x, // in screen coordinates
  2168. scrPoint.y, // in screen coordinates
  2169. this) ) // route commands through main window
  2170. {
  2171. m_hWndWhatsThis = 0;
  2172. }
  2173. }
  2174. else
  2175. m_hWndWhatsThis = 0;
  2176. }
  2177. }
  2178. _TRACE (-1, L"Leaving CFindDialog::OnContextMenu\n");
  2179. }
  2180. bool CFindDialog::ConsoleRefreshRequired() const
  2181. {
  2182. return m_bConsoleRefreshRequired;
  2183. }
  2184. void CFindDialog::ResetMenu()
  2185. {
  2186. // Set up the menu
  2187. if ( !::GetMenu (m_hWnd) )
  2188. {
  2189. HMENU hMenu = ::LoadMenu (AfxGetInstanceHandle (),
  2190. MAKEINTRESOURCE (IDR_FIND_DLG_MENU));
  2191. ASSERT (hMenu);
  2192. if ( hMenu )
  2193. {
  2194. if (::SetMenu (m_hWnd, hMenu) )
  2195. EnableMenuItems ();
  2196. }
  2197. }
  2198. }
  2199. void CFindDialog::OnCancel()
  2200. {
  2201. if ( m_hSearchThread && WaitForSingleObject (m_hSearchThread, 0) != WAIT_OBJECT_0 )
  2202. {
  2203. CString caption;
  2204. CString text;
  2205. VERIFY (text.LoadString (IDS_FIND_CLICK_STOP_BEFORE_CLOSING));
  2206. VERIFY (caption.LoadString (IDS_FIND_CERT));
  2207. CThemeContextActivator activator;
  2208. MessageBox (text, caption, MB_OK);
  2209. return;
  2210. }
  2211. CHelpDialog::OnCancel();
  2212. }