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.

423 lines
15 KiB

  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2002.
  5. //
  6. // File: SelectTemplateDlg.cpp
  7. //
  8. // Contents: Implementation of CSelectTemplateDlg
  9. //
  10. //----------------------------------------------------------------------------
  11. //
  12. #include "stdafx.h"
  13. #include "CompData.h"
  14. #include "SelectTemplateDlg.h"
  15. #include "CertTemplate.h"
  16. #include "TemplateGeneralPropertyPage.h"
  17. #include "TemplateV1RequestPropertyPage.h"
  18. #include "TemplateV2RequestPropertyPage.h"
  19. #include "TemplateV1SubjectNamePropertyPage.h"
  20. #include "TemplateV2SubjectNamePropertyPage.h"
  21. #include "TemplateV2AuthenticationPropertyPage.h"
  22. #include "TemplateV2SupercedesPropertyPage.h"
  23. #include "TemplateExtensionsPropertyPage.h"
  24. #include "PolicyOID.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CSelectTemplateDlg dialog
  32. CSelectTemplateDlg::CSelectTemplateDlg(CWnd* pParent,
  33. const CCertTmplComponentData* pCompData,
  34. const CStringList& supercededNameList)
  35. : CHelpDialog(CSelectTemplateDlg::IDD, pParent),
  36. m_supercededTemplateNameList (supercededNameList),
  37. m_pCompData (pCompData)
  38. {
  39. //{{AFX_DATA_INIT(CSelectTemplateDlg)
  40. //}}AFX_DATA_INIT
  41. }
  42. void CSelectTemplateDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CHelpDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CSelectTemplateDlg)
  46. DDX_Control(pDX, IDC_TEMPLATE_LIST, m_templateList);
  47. //}}AFX_DATA_MAP
  48. }
  49. BEGIN_MESSAGE_MAP(CSelectTemplateDlg, CHelpDialog)
  50. //{{AFX_MSG_MAP(CSelectTemplateDlg)
  51. ON_BN_CLICKED(IDC_TEMPLATE_PROPERTIES, OnTemplateProperties)
  52. ON_NOTIFY(LVN_ITEMCHANGED, IDC_TEMPLATE_LIST, OnItemchangedTemplateList)
  53. ON_NOTIFY(NM_DBLCLK, IDC_TEMPLATE_LIST, OnDblclkTemplateList)
  54. ON_NOTIFY(LVN_DELETEITEM, IDC_TEMPLATE_LIST, OnDeleteitemTemplateList)
  55. ON_WM_DESTROY()
  56. //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CSelectTemplateDlg message handlers
  60. BOOL CSelectTemplateDlg::OnInitDialog()
  61. {
  62. _TRACE (1, L"Entering CSelectTemplateDlg::OnInitDialog\n");
  63. CHelpDialog::OnInitDialog();
  64. CWaitCursor cursor;
  65. // Set up list controls
  66. COLORREF cr = RGB (255, 0, 255);
  67. CThemeContextActivator activator;
  68. VERIFY (m_imageListNormal.Create (IDB_TEMPLATES, 32, 0, cr));
  69. VERIFY (m_imageListSmall.Create (IDB_TEMPLATES, 16, 0, cr));
  70. m_templateList.SetImageList (CImageList::FromHandle (m_imageListSmall), LVSIL_SMALL);
  71. m_templateList.SetImageList (CImageList::FromHandle (m_imageListNormal), LVSIL_NORMAL);
  72. int colWidths[NUM_COLS] = {200, 200};
  73. // Add "Certificate Extension" column
  74. CString szText;
  75. VERIFY (szText.LoadString (IDS_CERTIFICATE_TEMPLATES));
  76. VERIFY (m_templateList.InsertColumn (COL_CERT_TEMPLATE, (LPCWSTR) szText,
  77. LVCFMT_LEFT, colWidths[COL_CERT_TEMPLATE], COL_CERT_TEMPLATE) != -1);
  78. VERIFY (szText.LoadString (IDS_COLUMN_SUPPORTED_CAS));
  79. VERIFY (m_templateList.InsertColumn (COL_CERT_VERSION, (LPCWSTR) szText,
  80. LVCFMT_LEFT, colWidths[COL_CERT_VERSION], COL_CERT_VERSION) != -1);
  81. ASSERT (m_pCompData);
  82. if ( m_pCompData )
  83. {
  84. POSITION pos = m_pCompData->m_globalTemplateNameList.GetHeadPosition ();
  85. CString szTemplateName;
  86. for (; pos; )
  87. {
  88. szTemplateName = m_pCompData->m_globalTemplateNameList.GetNext (pos);
  89. // #NTRAID 363879 Certtmpl: Certificate Template Snapin must not
  90. // allow the Subordinate CA template to be Superceded
  91. if ( wszCERTTYPE_SUBORDINATE_CA == szTemplateName )
  92. continue;
  93. // Only add those templates which are not already superceded
  94. if ( !m_supercededTemplateNameList.Find (szTemplateName) )
  95. {
  96. HCERTTYPE hCertType = 0;
  97. HRESULT hr = CAFindCertTypeByName (szTemplateName,
  98. NULL,
  99. CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES | CT_FLAG_NO_CACHE_LOOKUP,
  100. &hCertType);
  101. _ASSERT (SUCCEEDED (hr));
  102. if ( SUCCEEDED (hr) )
  103. {
  104. // NTRAID# 660535 Certtmpl: Supercedes list should filter
  105. // out any certificate templates which have the TYPE == CA
  106. DWORD dwGeneralFlags = 0;
  107. hr = CAGetCertTypeFlagsEx (hCertType, CERTTYPE_GENERAL_FLAG,
  108. &dwGeneralFlags);
  109. if ( FAILED (hr) )
  110. continue;
  111. if ( dwGeneralFlags & (CT_FLAG_IS_CA | CT_FLAG_IS_CROSS_CA) )
  112. continue;
  113. PWSTR* rgwszProp = 0;
  114. hr = CAGetCertTypePropertyEx (hCertType,
  115. CERTTYPE_PROP_FRIENDLY_NAME, &rgwszProp);
  116. if ( SUCCEEDED (hr) )
  117. {
  118. DWORD dwVersion = 0;
  119. hr = CAGetCertTypePropertyEx (hCertType,
  120. CERTTYPE_PROP_SCHEMA_VERSION,
  121. &dwVersion);
  122. if ( SUCCEEDED (hr) )
  123. {
  124. LV_ITEM lvItem;
  125. int iItem = m_templateList.GetItemCount ();
  126. int iResult = 0;
  127. // security review 2/21/2002 BryanWal ok
  128. ::ZeroMemory (&lvItem, sizeof (lvItem));
  129. lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  130. lvItem.iItem = iItem;
  131. lvItem.iSubItem = COL_CERT_TEMPLATE;
  132. lvItem.pszText = rgwszProp[0];
  133. if ( 1 == dwVersion )
  134. lvItem.iImage = 0; // version is 1
  135. else
  136. lvItem.iImage = 1; // version is 2
  137. lvItem.lParam = (LPARAM) new CString (szTemplateName);
  138. iItem = m_templateList.InsertItem (&lvItem);
  139. ASSERT (-1 != iItem);
  140. if ( -1 == iItem )
  141. break;
  142. else
  143. {
  144. // security review 2/21/2002 BryanWal ok
  145. ::ZeroMemory (&lvItem, sizeof (lvItem));
  146. lvItem.mask = LVIF_TEXT;
  147. lvItem.iItem = iItem;
  148. lvItem.iSubItem = COL_CERT_VERSION;
  149. CString text;
  150. if ( 1 == dwVersion )
  151. VERIFY (text.LoadString (IDS_WINDOWS_2000_AND_LATER));
  152. else
  153. VERIFY (text.LoadString (IDS_WINDOWS_2002_AND_LATER));
  154. lvItem.pszText = (PWSTR)(PCWSTR) text;
  155. iResult = m_templateList.SetItem (&lvItem);
  156. ASSERT (-1 != iResult);
  157. }
  158. }
  159. else
  160. {
  161. _TRACE (0, L"CAGetCertTypePropertyEx (CERTTYPE_PROP_SCHEMA_VERSION) failed: 0x%x\n", hr);
  162. }
  163. CAFreeCertTypeProperty (hCertType, rgwszProp);
  164. }
  165. else
  166. {
  167. _TRACE (0, L"CAGetCertTypePropertyEx (CERTTYPE_PROP_FRIENDLY_NAME) failed: 0x%x\n", hr);
  168. }
  169. hr = CACloseCertType (hCertType);
  170. _ASSERT (SUCCEEDED (hr));
  171. if ( !SUCCEEDED (hr) )
  172. {
  173. _TRACE (0, L"CACloseCertType (%s) failed: 0x%x\n", hr);
  174. }
  175. }
  176. else
  177. {
  178. _TRACE (0, L"CAFindCertTypeByName (%s) failed: 0x%x\n",
  179. (PCWSTR) szTemplateName, hr);
  180. }
  181. }
  182. }
  183. }
  184. EnableControls ();
  185. _TRACE (-1, L"Leaving CSelectTemplateDlg::OnInitDialog\n");
  186. return TRUE; // return TRUE unless you set the focus to a control
  187. // EXCEPTION: OCX Property Pages should return FALSE
  188. }
  189. void CSelectTemplateDlg::OnTemplateProperties()
  190. {
  191. int nSelCnt = m_templateList.GetSelectedCount ();
  192. int nSelItem = GetSelectedListItem ();
  193. if ( 1 == nSelCnt )
  194. {
  195. CString szFriendlyName = m_templateList.GetItemText (nSelItem,
  196. COL_CERT_TEMPLATE);
  197. CString* pszTemplateName = (CString*) m_templateList.GetItemData (nSelItem);
  198. HCERTTYPE hCertType = 0;
  199. HRESULT hr = CAFindCertTypeByName (*pszTemplateName,
  200. NULL,
  201. CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES | CT_FLAG_NO_CACHE_LOOKUP,
  202. &hCertType);
  203. _ASSERT (SUCCEEDED (hr));
  204. if ( SUCCEEDED (hr) )
  205. {
  206. CCertTemplate certTemplate (szFriendlyName, *pszTemplateName,
  207. L"", true, m_pCompData->m_fUseCache);
  208. CString title;
  209. // security review 2/21/2002 BryanWal ok
  210. title.FormatMessage (IDS_PROPERTIES_OF_TEMPLATE_X, szFriendlyName);
  211. CTemplatePropertySheet propSheet (title, certTemplate, this);
  212. if ( 1 == certTemplate.GetType () )
  213. {
  214. CTemplateGeneralPropertyPage* pGeneralPage =
  215. new CTemplateGeneralPropertyPage (certTemplate,
  216. m_pCompData);
  217. if ( pGeneralPage )
  218. {
  219. // Add General page
  220. propSheet.AddPage (pGeneralPage);
  221. // Add Request and Subject Name page only if subject is not a CA
  222. if ( !certTemplate.SubjectIsCA () )
  223. {
  224. propSheet.AddPage (new CTemplateV1RequestPropertyPage (
  225. certTemplate));
  226. propSheet.AddPage (new CTemplateV1SubjectNamePropertyPage (
  227. certTemplate));
  228. }
  229. // Add extensions page
  230. propSheet.AddPage (new CTemplateExtensionsPropertyPage (
  231. certTemplate, pGeneralPage->m_bIsDirty));
  232. }
  233. }
  234. else // version is 2
  235. {
  236. CTemplateGeneralPropertyPage* pGeneralPage =
  237. new CTemplateGeneralPropertyPage (certTemplate,
  238. m_pCompData);
  239. if ( pGeneralPage )
  240. {
  241. propSheet.AddPage (pGeneralPage);
  242. // Add Request and Subject pages if subject is not a CA
  243. if ( !certTemplate.SubjectIsCA () )
  244. {
  245. propSheet.AddPage (new CTemplateV2RequestPropertyPage (
  246. certTemplate, pGeneralPage->m_bIsDirty));
  247. propSheet.AddPage (new CTemplateV2SubjectNamePropertyPage (
  248. certTemplate, pGeneralPage->m_bIsDirty));
  249. }
  250. propSheet.AddPage (new CTemplateV2AuthenticationPropertyPage (
  251. certTemplate, pGeneralPage->m_bIsDirty));
  252. propSheet.AddPage (new CTemplateV2SupercedesPropertyPage (
  253. certTemplate,
  254. pGeneralPage->m_bIsDirty,
  255. m_pCompData));
  256. propSheet.AddPage (new CTemplateExtensionsPropertyPage (
  257. certTemplate, pGeneralPage->m_bIsDirty));
  258. }
  259. }
  260. CThemeContextActivator activator;
  261. propSheet.DoModal ();
  262. hr = CACloseCertType (hCertType);
  263. _ASSERT (SUCCEEDED (hr));
  264. if ( !SUCCEEDED (hr) )
  265. {
  266. _TRACE (0, L"CACloseCertType (%s) failed: 0x%x\n", hr);
  267. }
  268. }
  269. else
  270. {
  271. _TRACE (0, L"CAFindCertTypeByName (%s) failed: 0x%x\n",
  272. (PCWSTR) pszTemplateName, hr);
  273. }
  274. }
  275. }
  276. void CSelectTemplateDlg::OnOK()
  277. {
  278. int nCnt = m_templateList.GetItemCount ();
  279. ASSERT (nCnt >= 1);
  280. UINT flag = 0;
  281. while (--nCnt >= 0)
  282. {
  283. flag = ListView_GetItemState (m_templateList.m_hWnd, nCnt, LVIS_SELECTED);
  284. if ( flag & LVNI_SELECTED )
  285. {
  286. CString* pszTemplateName = (CString*) m_templateList.GetItemData (nCnt);
  287. if ( pszTemplateName )
  288. m_returnedTemplates.AddTail (*pszTemplateName);
  289. }
  290. }
  291. CHelpDialog::OnOK();
  292. }
  293. void CSelectTemplateDlg::EnableControls()
  294. {
  295. int nSelCnt = m_templateList.GetSelectedCount ();
  296. GetDlgItem (IDC_TEMPLATE_PROPERTIES)->EnableWindow (1 == nSelCnt);
  297. GetDlgItem (IDOK)->EnableWindow (nSelCnt > 0);
  298. }
  299. void CSelectTemplateDlg::OnItemchangedTemplateList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  300. {
  301. EnableControls ();
  302. *pResult = 0;
  303. }
  304. int CSelectTemplateDlg::GetSelectedListItem()
  305. {
  306. int nSelItem = -1;
  307. if ( m_templateList.m_hWnd && m_templateList.GetSelectedCount () > 0 )
  308. {
  309. int nCnt = m_templateList.GetItemCount ();
  310. ASSERT (nCnt >= 1);
  311. UINT flag = 0;
  312. while (--nCnt >= 0)
  313. {
  314. flag = ListView_GetItemState (m_templateList.m_hWnd, nCnt, LVIS_SELECTED);
  315. if ( flag & LVNI_SELECTED )
  316. {
  317. nSelItem = nCnt;
  318. break;
  319. }
  320. }
  321. }
  322. return nSelItem;
  323. }
  324. void CSelectTemplateDlg::OnDblclkTemplateList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  325. {
  326. OnTemplateProperties ();
  327. *pResult = 0;
  328. }
  329. void CSelectTemplateDlg::OnDeleteitemTemplateList(NMHDR* pNMHDR, LRESULT* pResult)
  330. {
  331. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  332. CString* pszTemplateName = (CString*) m_templateList.GetItemData (pNMListView->iItem);
  333. if ( pszTemplateName )
  334. delete pszTemplateName;
  335. *pResult = 0;
  336. }
  337. void CSelectTemplateDlg::DoContextHelp (HWND hWndControl)
  338. {
  339. _TRACE(1, L"Entering CSelectTemplateDlg::DoContextHelp\n");
  340. switch (::GetDlgCtrlID (hWndControl))
  341. {
  342. case IDC_STATIC:
  343. break;
  344. default:
  345. // Display context help for a control
  346. if ( !::WinHelp (
  347. hWndControl,
  348. GetContextHelpFile (),
  349. HELP_WM_HELP,
  350. (DWORD_PTR) g_aHelpIDs_IDD_SELECT_TEMPLATE) )
  351. {
  352. _TRACE(0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  353. }
  354. break;
  355. }
  356. _TRACE(-1, L"Leaving CSelectTemplateDlg::DoContextHelp\n");
  357. }
  358. void CSelectTemplateDlg::OnDestroy()
  359. {
  360. CHelpDialog::OnDestroy();
  361. m_imageListNormal.Destroy ();
  362. m_imageListSmall.Destroy ();
  363. }