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.

298 lines
9.1 KiB

  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2001.
  5. //
  6. // File: TemplateV1RequestPropertyPage.cpp
  7. //
  8. // Contents: Implementation of CTemplateV1RequestPropertyPage
  9. //
  10. //----------------------------------------------------------------------------
  11. // TemplateV1RequestPropertyPage.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "TemplateV1RequestPropertyPage.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CTemplateV1RequestPropertyPage property page
  22. enum {
  23. REQUEST_PURPOSE_SIGNATURE = 0,
  24. REQUEST_PURPOSE_ENCRYPTION,
  25. REQUEST_PURPOSE_SIGNATURE_AND_ENCRYPTION
  26. };
  27. CTemplateV1RequestPropertyPage::CTemplateV1RequestPropertyPage(CCertTemplate& rCertTemplate) :
  28. CHelpPropertyPage(CTemplateV1RequestPropertyPage::IDD),
  29. m_rCertTemplate (rCertTemplate)
  30. {
  31. _TRACE (1, L"Entering CTemplateV1RequestPropertyPage::CTemplateV1RequestPropertyPage ()\n");
  32. //{{AFX_DATA_INIT(CTemplateV1RequestPropertyPage)
  33. //}}AFX_DATA_INIT
  34. m_rCertTemplate.AddRef ();
  35. _TRACE (-1, L"Leaving CTemplateV1RequestPropertyPage::CTemplateV1RequestPropertyPage ()\n");
  36. }
  37. CTemplateV1RequestPropertyPage::~CTemplateV1RequestPropertyPage()
  38. {
  39. _TRACE (1, L"Entering CTemplateV1RequestPropertyPage::~CTemplateV1RequestPropertyPage ()\n");
  40. m_rCertTemplate.Release ();
  41. _TRACE (-1, L"Leaving CTemplateV1RequestPropertyPage::~CTemplateV1RequestPropertyPage ()\n");
  42. }
  43. void CTemplateV1RequestPropertyPage::DoDataExchange(CDataExchange* pDX)
  44. {
  45. CHelpPropertyPage::DoDataExchange(pDX);
  46. //{{AFX_DATA_MAP(CTemplateV1RequestPropertyPage)
  47. DDX_Control(pDX, IDC_PURPOSE_COMBO, m_purposeCombo);
  48. DDX_Control(pDX, IDC_CSP_LIST, m_CSPList);
  49. //}}AFX_DATA_MAP
  50. }
  51. BEGIN_MESSAGE_MAP(CTemplateV1RequestPropertyPage, CHelpPropertyPage)
  52. //{{AFX_MSG_MAP(CTemplateV1RequestPropertyPage)
  53. ON_CBN_SELCHANGE(IDC_PURPOSE_COMBO, OnSelchangePurposeCombo)
  54. ON_BN_CLICKED(IDC_EXPORT_PRIVATE_KEY, OnExportPrivateKey)
  55. //}}AFX_MSG_MAP
  56. ON_CONTROL(CLBN_CHKCHANGE, IDC_CSP_LIST, OnCheckChange)
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CTemplateV1RequestPropertyPage message handlers
  60. BOOL CTemplateV1RequestPropertyPage::OnInitDialog()
  61. {
  62. _TRACE (1, L"Entering CTemplateV1RequestPropertyPage::OnInitDialog ()\n");
  63. CHelpPropertyPage::OnInitDialog();
  64. CString text;
  65. VERIFY (text.LoadString (IDS_SIGNATURE));
  66. int nIndex = m_purposeCombo.AddString (text);
  67. if ( nIndex >= 0 )
  68. {
  69. m_purposeCombo.SetItemData (nIndex, (DWORD_PTR) REQUEST_PURPOSE_SIGNATURE);
  70. if ( m_rCertTemplate.HasKeySpecSignature () )
  71. m_purposeCombo.SetCurSel (nIndex);
  72. }
  73. VERIFY (text.LoadString (IDS_ENCRYPTION));
  74. nIndex = m_purposeCombo.AddString (text);
  75. if ( nIndex >= 0 )
  76. {
  77. m_purposeCombo.SetItemData (nIndex, (DWORD_PTR) REQUEST_PURPOSE_ENCRYPTION);
  78. if ( m_rCertTemplate.HasEncryptionSignature () )
  79. m_purposeCombo.SetCurSel (nIndex);
  80. }
  81. VERIFY (text.LoadString (IDS_SIGNATURE_AND_ENCRYPTION));
  82. nIndex = m_purposeCombo.AddString (text);
  83. if ( nIndex >= 0 )
  84. {
  85. // NTRAID# 269907 Certificate Template Snap-in: Should set the
  86. // purpose of the template to both signature and encryption if
  87. // CERT_DIGITAL_SIGNATURE_KEY_USAGE is set
  88. bool bHasDigitalSignature = false;
  89. m_rCertTemplate.GetDigitalSignature (bHasDigitalSignature);
  90. m_purposeCombo.SetItemData (nIndex, (DWORD_PTR) REQUEST_PURPOSE_SIGNATURE_AND_ENCRYPTION);
  91. if ( m_rCertTemplate.HasEncryptionSignature () &&
  92. (bHasDigitalSignature || m_rCertTemplate.HasKeySpecSignature ()) )
  93. m_purposeCombo.SetCurSel (nIndex);
  94. }
  95. if ( SUCCEEDED (EnumerateCSPs ()) )
  96. {
  97. }
  98. if ( m_rCertTemplate.PrivateKeyIsExportable () )
  99. SendDlgItemMessage (IDC_EXPORT_PRIVATE_KEY, BM_SETCHECK, BST_CHECKED);
  100. EnableControls ();
  101. _TRACE (-1, L"Leaving CTemplateV1RequestPropertyPage::OnInitDialog ()\n");
  102. return TRUE; // return TRUE unless you set the focus to a control
  103. // EXCEPTION: OCX Property Pages should return FALSE
  104. }
  105. void CTemplateV1RequestPropertyPage::EnableControls ()
  106. {
  107. GetDlgItem (IDC_PURPOSE_COMBO)->EnableWindow (FALSE);
  108. int nCnt = m_CSPList.GetCount ();
  109. for (int nIndex = 0; nIndex < nCnt; nIndex++)
  110. m_CSPList.Enable (nIndex, FALSE);
  111. GetDlgItem (IDC_EXPORT_PRIVATE_KEY)->EnableWindow (FALSE);
  112. }
  113. HRESULT CTemplateV1RequestPropertyPage::EnumerateCSPs()
  114. {
  115. _TRACE (1, L"Entering CTemplateV1RequestPropertyPage::EnumerateCSPs\n");
  116. HRESULT hr = S_OK;
  117. for (DWORD dwIndex = 0; ;dwIndex++)
  118. {
  119. DWORD cbName = 0;
  120. DWORD dwProvType = 0;
  121. if ( CryptEnumProviders (dwIndex, NULL, 0, &dwProvType, NULL, &cbName) )
  122. {
  123. PWSTR pszTypeName = new WCHAR[cbName];
  124. if ( pszTypeName )
  125. {
  126. if ( CryptEnumProviders (dwIndex, NULL, 0, &dwProvType,
  127. pszTypeName, &cbName) )
  128. {
  129. int nIndex = m_CSPList.AddString (pszTypeName);
  130. if ( nIndex < 0 )
  131. {
  132. _TRACE (0, L"AddString (%s) failed: %d\n", nIndex);
  133. break;
  134. }
  135. }
  136. else
  137. {
  138. hr = HRESULT_FROM_WIN32 (GetLastError ());
  139. _TRACE (0, L"CryptEnumProviderTypes () failed: 0x%x\n", hr);
  140. break;
  141. }
  142. delete [] pszTypeName;
  143. }
  144. else
  145. hr = E_OUTOFMEMORY;
  146. }
  147. else
  148. {
  149. hr = HRESULT_FROM_WIN32 (GetLastError ());
  150. _TRACE (0, L"CryptEnumProviderTypes () failed: 0x%x\n", hr);
  151. break;
  152. }
  153. }
  154. int nCSPIndex = 0;
  155. CString szCSP;
  156. while ( SUCCEEDED (m_rCertTemplate.GetCSP (nCSPIndex, szCSP)) )
  157. {
  158. int nIndex = m_CSPList.FindString (-1, szCSP);
  159. if ( CB_ERR != nIndex )
  160. m_CSPList.SetCheck (nIndex, BST_CHECKED);
  161. else
  162. {
  163. // was not found - add it
  164. nIndex = m_CSPList.AddString (szCSP);
  165. if ( nIndex >= 0 )
  166. m_CSPList.SetCheck (nIndex, BST_CHECKED);
  167. }
  168. nCSPIndex++;
  169. }
  170. _TRACE (-1, L"Entering CTemplateV1RequestPropertyPage::EnumerateCSPs: 0x%x\n", hr);
  171. return hr;
  172. }
  173. void CTemplateV1RequestPropertyPage::OnSelchangePurposeCombo()
  174. {
  175. int nIndex = m_purposeCombo.GetCurSel ();
  176. if ( nIndex >= 0 )
  177. {
  178. switch (m_purposeCombo.GetItemData (nIndex))
  179. {
  180. case REQUEST_PURPOSE_SIGNATURE:
  181. m_rCertTemplate.SetEncryptionSignature (false);
  182. m_rCertTemplate.SetKeySpecSignature (true);
  183. break;
  184. case REQUEST_PURPOSE_ENCRYPTION:
  185. m_rCertTemplate.SetEncryptionSignature (true);
  186. m_rCertTemplate.SetKeySpecSignature (false);
  187. break;
  188. case REQUEST_PURPOSE_SIGNATURE_AND_ENCRYPTION:
  189. m_rCertTemplate.SetEncryptionSignature (true);
  190. m_rCertTemplate.SetKeySpecSignature (true);
  191. break;
  192. default:
  193. _ASSERT (0);
  194. break;
  195. }
  196. }
  197. SetModified ();
  198. EnableControls ();
  199. }
  200. void CTemplateV1RequestPropertyPage::OnExportPrivateKey()
  201. {
  202. bool bMakeExportable = (BST_CHECKED == SendDlgItemMessage (IDC_EXPORT_PRIVATE_KEY, BM_GETCHECK));
  203. m_rCertTemplate.MakePrivateKeyExportable (bMakeExportable);
  204. SetModified ();
  205. }
  206. void CTemplateV1RequestPropertyPage::OnCheckChange()
  207. {
  208. int nSel = m_CSPList.GetCurSel ();
  209. if ( nSel >= 0 )
  210. {
  211. CString szCSPName;
  212. m_CSPList.GetText (nSel, szCSPName);
  213. if ( !szCSPName.IsEmpty () )
  214. {
  215. HRESULT hr = S_OK;
  216. if ( BST_CHECKED == m_CSPList.GetCheck (nSel) )
  217. {
  218. hr = m_rCertTemplate.ModifyCSPList (szCSPName, true); // add
  219. }
  220. else
  221. {
  222. hr = m_rCertTemplate.ModifyCSPList (szCSPName, false); // remove
  223. }
  224. if ( SUCCEEDED (hr) )
  225. SetModified ();
  226. }
  227. }
  228. }
  229. void CTemplateV1RequestPropertyPage::DoContextHelp (HWND hWndControl)
  230. {
  231. _TRACE(1, L"Entering CTemplateV1RequestPropertyPage::DoContextHelp\n");
  232. switch (::GetDlgCtrlID (hWndControl))
  233. {
  234. case IDC_STATIC:
  235. break;
  236. default:
  237. // Display context help for a control
  238. if ( !::WinHelp (
  239. hWndControl,
  240. GetContextHelpFile (),
  241. HELP_WM_HELP,
  242. (DWORD_PTR) g_aHelpIDs_IDD_TEMPLATE_V1_REQUEST) )
  243. {
  244. _TRACE(0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  245. }
  246. break;
  247. }
  248. _TRACE(-1, L"Leaving CTemplateV1RequestPropertyPage::DoContextHelp\n");
  249. }