Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
14 KiB

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