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.

236 lines
5.8 KiB

  1. // SecuritySettingsPage.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CertWiz.h"
  5. #include "SecuritySettingsPage.h"
  6. #include "Certificat.h"
  7. #include "CertUtil.h"
  8. #include "Shlwapi.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CSecuritySettingsPage property page
  16. IMPLEMENT_DYNCREATE(CSecuritySettingsPage, CIISWizardPage)
  17. CSecuritySettingsPage::CSecuritySettingsPage(CCertificate * pCert)
  18. : CIISWizardPage(CSecuritySettingsPage::IDD, IDS_CERTWIZ, TRUE),
  19. m_pCert(pCert)
  20. {
  21. //{{AFX_DATA_INIT(CSecuritySettingsPage)
  22. m_BitLengthIndex = -1;
  23. m_FriendlyName = _T("");
  24. m_SGC_cert = FALSE;
  25. m_choose_CSP = FALSE;
  26. //}}AFX_DATA_INIT
  27. m_lru_reg = m_lru_sgc = 0;
  28. }
  29. CSecuritySettingsPage::~CSecuritySettingsPage()
  30. {
  31. }
  32. void CSecuritySettingsPage::DoDataExchange(CDataExchange* pDX)
  33. {
  34. CIISWizardPage::DoDataExchange(pDX);
  35. //{{AFX_DATA_MAP(CSecuritySettingsPage)
  36. DDX_CBIndex(pDX, IDC_BIT_LENGTH, m_BitLengthIndex);
  37. DDX_Text(pDX, IDC_FRIENDLY_NAME, m_FriendlyName);
  38. DDV_MaxChars(pDX, m_FriendlyName, 256);
  39. DDX_Check(pDX, IDC_SGC_CERT, m_SGC_cert);
  40. DDX_Check(pDX, IDC_PROVIDER_SELECT, m_choose_CSP);
  41. DDX_Control(pDX, IDC_PROVIDER_SELECT, m_check_csp);
  42. //}}AFX_DATA_MAP
  43. }
  44. BOOL
  45. CSecuritySettingsPage::OnSetActive()
  46. {
  47. SetWizardButtons(m_FriendlyName.IsEmpty() ?
  48. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  49. return CIISWizardPage::OnSetActive();
  50. }
  51. LRESULT
  52. CSecuritySettingsPage::OnWizardPrev()
  53. /*++
  54. Routine Description:
  55. Prev button handler
  56. Arguments:
  57. None
  58. Return Value:
  59. 0 to automatically advance to the prev page;
  60. 1 to prevent the page from changing.
  61. To jump to a page other than the prev one,
  62. return the identifier of the dialog to be displayed.
  63. --*/
  64. {
  65. return CSecuritySettingsPage::IDD_PREV_PAGE;
  66. }
  67. LRESULT
  68. CSecuritySettingsPage::OnWizardNext()
  69. {
  70. TCHAR buf[6];
  71. UpdateData();
  72. m_pCert->m_FriendlyName = m_FriendlyName;
  73. GetDlgItem(IDC_BIT_LENGTH)->SendMessage(CB_GETLBTEXT, m_BitLengthIndex, (LPARAM)buf);
  74. m_pCert->m_KeyLength = StrToInt(buf);
  75. m_pCert->m_SGCcertificat = m_SGC_cert;
  76. if (m_SGC_cert)
  77. {
  78. // it was a smart move, but xenroll makes 512 bits default for SGC,
  79. // so we always creating 512 certs
  80. // if (m_pCert->m_KeyLength == (int)m_sgckey_limits.def)
  81. // m_pCert->m_KeyLength = 0;
  82. }
  83. else
  84. {
  85. if (m_pCert->m_KeyLength == (int)m_regkey_limits.def)
  86. m_pCert->m_KeyLength = 0;
  87. }
  88. VERIFY(m_pCert->SetSecuritySettings());
  89. m_pCert->m_DefaultCSP = !m_choose_CSP;
  90. return m_choose_CSP ? IDD_NEXT_CSP : IDD_NEXT_PAGE;
  91. }
  92. BEGIN_MESSAGE_MAP(CSecuritySettingsPage, CIISWizardPage)
  93. //{{AFX_MSG_MAP(CSecuritySettingsPage)
  94. ON_EN_CHANGE(IDC_FRIENDLY_NAME, OnChangeFriendlyName)
  95. ON_BN_CLICKED(IDC_SGC_CERT, OnSgcCert)
  96. //}}AFX_MSG_MAP
  97. END_MESSAGE_MAP()
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CSecuritySettingsPage message handlers
  100. DWORD dwPredefinedKeyLength[] =
  101. {
  102. 0, // 0 means default
  103. 512,
  104. 1024,
  105. 2048,
  106. 4096,
  107. 8192,
  108. 16384
  109. };
  110. #define COUNT_KEYLENGTH sizeof(dwPredefinedKeyLength)/sizeof(dwPredefinedKeyLength[0])
  111. BOOL CSecuritySettingsPage::OnInitDialog()
  112. {
  113. ASSERT(m_pCert != NULL);
  114. m_FriendlyName = m_pCert->m_FriendlyName;
  115. CIISWizardPage::OnInitDialog();
  116. OnChangeFriendlyName();
  117. HRESULT hr;
  118. CString str;
  119. if (GetKeySizeLimits(m_pCert->GetEnrollObject(),
  120. &m_regkey_limits.min,
  121. &m_regkey_limits.max,
  122. &m_regkey_limits.def,
  123. FALSE,
  124. &hr))
  125. {
  126. for (int i = 0; i < COUNT_KEYLENGTH; i++)
  127. {
  128. if ( dwPredefinedKeyLength[i] >= m_regkey_limits.min
  129. && dwPredefinedKeyLength[i] <= m_regkey_limits.max
  130. )
  131. {
  132. m_regkey_size_list.AddTail(dwPredefinedKeyLength[i]);
  133. if (m_pCert->m_KeyLength == (int)dwPredefinedKeyLength[i])
  134. m_BitLengthIndex = i + 1;
  135. }
  136. }
  137. }
  138. else
  139. {
  140. ASSERT(FALSE);
  141. m_pCert->m_hResult = hr;
  142. }
  143. if (m_BitLengthIndex == -1)
  144. m_BitLengthIndex = 0;
  145. // for SGC temporarily set only one size
  146. m_sgckey_limits.min = 1024;
  147. m_sgckey_limits.max = 1024;
  148. m_sgckey_limits.def = 1024;
  149. m_sgckey_size_list.AddTail(1024);
  150. m_SGC_cert = m_pCert->m_SGCcertificat;
  151. m_choose_CSP = !m_pCert->m_DefaultCSP;
  152. UpdateData(FALSE);
  153. if (m_SGC_cert)
  154. GetDlgItem(IDC_SGC_CERT)->SendMessage(
  155. BM_SETCHECK, m_SGC_cert ? BST_CHECKED : BST_UNCHECKED, 0);
  156. OnSgcCert();
  157. GetDlgItem(IDC_FRIENDLY_NAME)->SetFocus();
  158. return FALSE;
  159. }
  160. void CSecuritySettingsPage::OnChangeFriendlyName()
  161. {
  162. UpdateData(TRUE);
  163. SetWizardButtons(m_FriendlyName.IsEmpty() ?
  164. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  165. }
  166. void CSecuritySettingsPage::OnSgcCert()
  167. {
  168. // Currently, only one key size works with SGC flag:
  169. // 1024, so we need to limit combobox to this length, if
  170. // button is checked
  171. CButton * pCheckBox = (CButton *)CWnd::FromHandle(GetDlgItem(IDC_SGC_CERT)->m_hWnd);
  172. CComboBox * pCombo = (CComboBox *)CWnd::FromHandle(GetDlgItem(IDC_BIT_LENGTH)->m_hWnd);
  173. int check_state = pCheckBox->GetCheck();
  174. int lru_index, count;
  175. CList<int, int> * pList;
  176. if (check_state == 1)
  177. {
  178. // switch combo to previously selected SGC size
  179. m_lru_reg = pCombo->GetCurSel();
  180. lru_index = m_lru_sgc;
  181. pList = &m_sgckey_size_list;
  182. }
  183. else
  184. {
  185. // switch combo to previously selected regular size
  186. m_lru_sgc = pCombo->GetCurSel();
  187. lru_index = m_lru_reg;
  188. pList = &m_regkey_size_list;
  189. }
  190. // now refill the combo with key length and select the relevant last one
  191. pCombo->ResetContent();
  192. CString str;
  193. POSITION pos = pList->GetHeadPosition();
  194. while (pos != NULL)
  195. {
  196. str.Format(L"%d", pList->GetNext(pos));
  197. pCombo->AddString(str);
  198. }
  199. count = pCombo->GetCount();
  200. pCombo->SetCurSel(count > 1 ? lru_index : 0);
  201. pCombo->EnableWindow(count > 1);
  202. }
  203. void CSecuritySettingsPage::OnSelectCsp()
  204. {
  205. m_pCert->m_DefaultCSP = m_check_csp.GetCheck() == 0;
  206. m_choose_CSP = !m_pCert->m_DefaultCSP;
  207. if (m_pCert->m_DefaultCSP)
  208. m_pCert->m_CspName.Empty();
  209. }