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.

260 lines
6.2 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 = -1;
  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. {
  87. m_pCert->m_KeyLength = 0;
  88. }
  89. }
  90. VERIFY(m_pCert->SetSecuritySettings());
  91. m_pCert->m_DefaultCSP = !m_choose_CSP;
  92. return m_choose_CSP ? IDD_NEXT_CSP : IDD_NEXT_PAGE;
  93. }
  94. BEGIN_MESSAGE_MAP(CSecuritySettingsPage, CIISWizardPage)
  95. //{{AFX_MSG_MAP(CSecuritySettingsPage)
  96. ON_EN_CHANGE(IDC_FRIENDLY_NAME, OnChangeFriendlyName)
  97. ON_BN_CLICKED(IDC_SGC_CERT, OnSgcCert)
  98. //}}AFX_MSG_MAP
  99. END_MESSAGE_MAP()
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CSecuritySettingsPage message handlers
  102. DWORD dwPredefinedKeyLength[] =
  103. {
  104. 0, // 0 means default
  105. 512,
  106. 1024,
  107. 2048,
  108. 4096,
  109. 8192,
  110. 16384
  111. };
  112. #define COUNT_KEYLENGTH sizeof(dwPredefinedKeyLength)/sizeof(dwPredefinedKeyLength[0])
  113. BOOL CSecuritySettingsPage::OnInitDialog()
  114. {
  115. ASSERT(m_pCert != NULL);
  116. m_FriendlyName = m_pCert->m_FriendlyName;
  117. CIISWizardPage::OnInitDialog();
  118. OnChangeFriendlyName();
  119. HRESULT hr;
  120. CString str;
  121. if (GetKeySizeLimits(m_pCert->GetEnrollObject(),
  122. &m_regkey_limits.min,
  123. &m_regkey_limits.max,
  124. &m_regkey_limits.def,
  125. FALSE,
  126. &hr))
  127. {
  128. for (int i = 0, n = 0; i < COUNT_KEYLENGTH; i++)
  129. {
  130. if ( dwPredefinedKeyLength[i] >= m_regkey_limits.min
  131. && dwPredefinedKeyLength[i] <= m_regkey_limits.max
  132. )
  133. {
  134. m_regkey_size_list.AddTail(dwPredefinedKeyLength[i]);
  135. if (m_pCert->GetStatusCode() == CCertificate::REQUEST_NEW_CERT)
  136. {
  137. if (m_regkey_limits.def == (int)dwPredefinedKeyLength[i])
  138. m_BitLengthIndex = n;
  139. }
  140. else
  141. {
  142. if (m_pCert->m_KeyLength == (int)dwPredefinedKeyLength[i])
  143. m_BitLengthIndex = n;
  144. }
  145. n++;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. ASSERT(FALSE);
  152. m_pCert->m_hResult = hr;
  153. }
  154. if (m_BitLengthIndex == -1)
  155. m_BitLengthIndex = 0;
  156. // for SGC temporarily set only one size
  157. m_sgckey_limits.min = 1024;
  158. m_sgckey_limits.max = 1024;
  159. m_sgckey_limits.def = 1024;
  160. m_sgckey_size_list.AddTail(1024);
  161. m_SGC_cert = m_pCert->m_SGCcertificat;
  162. m_choose_CSP = !m_pCert->m_DefaultCSP;
  163. UpdateData(FALSE);
  164. SetupKeySizesCombo();
  165. GetDlgItem(IDC_FRIENDLY_NAME)->SetFocus();
  166. return FALSE;
  167. }
  168. void CSecuritySettingsPage::OnChangeFriendlyName()
  169. {
  170. UpdateData(TRUE);
  171. SetWizardButtons(m_FriendlyName.IsEmpty() ?
  172. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  173. }
  174. void CSecuritySettingsPage::OnSgcCert()
  175. {
  176. UpdateData();
  177. SetupKeySizesCombo();
  178. }
  179. void CSecuritySettingsPage::SetupKeySizesCombo()
  180. {
  181. // Currently, only one key size works with SGC flag:
  182. // 1024, so we need to limit combobox to this length, if
  183. // button is checked
  184. CButton * pCheckBox = (CButton *)CWnd::FromHandle(GetDlgItem(IDC_SGC_CERT)->m_hWnd);
  185. CComboBox * pCombo = (CComboBox *)CWnd::FromHandle(GetDlgItem(IDC_BIT_LENGTH)->m_hWnd);
  186. int check_state = pCheckBox->GetCheck();
  187. int index, count;
  188. CList<int, int> * pList;
  189. if (m_SGC_cert)
  190. {
  191. // switch combo to previously selected SGC size
  192. m_lru_reg = pCombo->GetCurSel();
  193. index = m_lru_sgc;
  194. pList = &m_sgckey_size_list;
  195. }
  196. else
  197. {
  198. // switch combo to previously selected regular size
  199. m_lru_sgc = pCombo->GetCurSel();
  200. index = m_lru_reg;
  201. pList = &m_regkey_size_list;
  202. }
  203. // now refill the combo with key length and select the relevant last one
  204. pCombo->ResetContent();
  205. CString str;
  206. POSITION pos = pList->GetHeadPosition();
  207. while (pos != NULL)
  208. {
  209. str.Format(L"%d", pList->GetNext(pos));
  210. pCombo->AddString(str);
  211. }
  212. count = pCombo->GetCount();
  213. if (m_SGC_cert)
  214. {
  215. if (index == CB_ERR)
  216. index = 0;
  217. }
  218. else
  219. {
  220. if (index == CB_ERR)
  221. index = m_BitLengthIndex;
  222. }
  223. pCombo->SetCurSel(index);
  224. pCombo->EnableWindow(count > 1);
  225. }
  226. void CSecuritySettingsPage::OnSelectCsp()
  227. {
  228. m_pCert->m_DefaultCSP = m_check_csp.GetCheck() == 0;
  229. m_choose_CSP = !m_pCert->m_DefaultCSP;
  230. if (m_pCert->m_DefaultCSP)
  231. m_pCert->m_CspName.Empty();
  232. }