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

2749 lines
92 KiB

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