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.

293 lines
8.0 KiB

  1. // ReplaceChooseCert.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CertWiz.h"
  5. #include "ChooseCertPage.h"
  6. #include "Certificat.h"
  7. #include "CertUtil.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define COL_COMMON_NAME 0
  14. #define COL_CA_NAME 1
  15. #define COL_EXPIRATION_DATE 2
  16. #define COL_PURPOSE 3
  17. #define COL_FRIENDLY_NAME 4
  18. #define COL_COMMON_NAME_WID 100
  19. #define COL_CA_NAME_WID 100
  20. #define COL_EXPIRATION_DATE_WID 100
  21. #define COL_PURPOSE_WID 100
  22. #define COL_FRIENDLY_NAME_WID 100
  23. int
  24. CCertListCtrl::GetSelectedIndex()
  25. {
  26. #if _AFX_VER >= 0x0600
  27. POSITION pos = GetFirstSelectedItemPosition();
  28. return pos != NULL ? GetNextSelectedItem(pos) : -1;
  29. #else
  30. // I guess we should do it in a hard way
  31. int count = GetItemCount();
  32. int index = -1;
  33. for (int i = 0; i < count; i++)
  34. {
  35. if (GetItemState(i, LVIS_SELECTED))
  36. {
  37. index = i;
  38. break;
  39. }
  40. }
  41. return index;
  42. #endif
  43. }
  44. void
  45. CCertListCtrl::AdjustStyle()
  46. {
  47. #if _AFX_VER >= 0x0600
  48. DWORD dwStyle = m_CertList.GetExtendedStyle();
  49. m_CertList.SetExtendedStyle(dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
  50. #else
  51. ASSERT(m_hWnd != NULL);
  52. DWORD dwStyle = ListView_GetExtendedListViewStyle(m_hWnd);
  53. ListView_SetExtendedListViewStyle(m_hWnd,
  54. dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
  55. #endif
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CChooseCertPage property page
  59. IMPLEMENT_DYNCREATE(CChooseCertPage, CIISWizardPage)
  60. CChooseCertPage::CChooseCertPage(CCertificate * pCert)
  61. : CIISWizardPage(CChooseCertPage::IDD, IDS_CERTWIZ, TRUE),
  62. m_pCert(pCert)
  63. {
  64. //{{AFX_DATA_INIT(CChooseCertPage)
  65. // NOTE: the ClassWizard will add member initialization here
  66. //}}AFX_DATA_INIT
  67. }
  68. CChooseCertPage::~CChooseCertPage()
  69. {
  70. }
  71. void CChooseCertPage::DoDataExchange(CDataExchange* pDX)
  72. {
  73. CIISWizardPage::DoDataExchange(pDX);
  74. //{{AFX_DATA_MAP(CChooseCertPage)
  75. DDX_Control(pDX, IDC_CERT_LIST, m_CertList);
  76. //}}AFX_DATA_MAP
  77. }
  78. BEGIN_MESSAGE_MAP(CChooseCertPage, CIISWizardPage)
  79. //{{AFX_MSG_MAP(CChooseCertPage)
  80. ON_NOTIFY(NM_CLICK, IDC_CERT_LIST, OnClickCertList)
  81. ON_NOTIFY(NM_DBLCLK, IDC_CERT_LIST, OnDblClickCertList)
  82. ON_NOTIFY(LVN_KEYDOWN, IDC_CERT_LIST, OnKeydown)
  83. ON_WM_DESTROY()
  84. //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CChooseCertPage message handlers
  88. LRESULT CChooseCertPage::OnWizardBack()
  89. {
  90. LRESULT id = 1;
  91. switch (m_pCert->GetStatusCode())
  92. {
  93. case CCertificate::REQUEST_REPLACE_CERT:
  94. id = IDD_PAGE_PREV_REPLACE;
  95. break;
  96. case CCertificate::REQUEST_INSTALL_CERT:
  97. id = IDD_PAGE_PREV_INSTALL;
  98. break;
  99. default:
  100. ASSERT(FALSE);
  101. }
  102. return id;
  103. }
  104. LRESULT CChooseCertPage::OnWizardNext()
  105. {
  106. // get hash pointer for selected cert
  107. int index = m_CertList.GetSelectedIndex();
  108. ASSERT(index != -1);
  109. // find cert in store
  110. CRYPT_HASH_BLOB * pHash = (CRYPT_HASH_BLOB *)m_CertList.GetItemData(index);
  111. ASSERT(pHash != NULL);
  112. m_pCert->m_pSelectedCertHash = pHash;
  113. LRESULT id = 1;
  114. switch (m_pCert->GetStatusCode())
  115. {
  116. case CCertificate::REQUEST_REPLACE_CERT:
  117. id = IDD_PAGE_NEXT_REPLACE;
  118. break;
  119. case CCertificate::REQUEST_INSTALL_CERT:
  120. // Check if we are on the w3svc node...
  121. // if we are then show the ssl page..
  122. id = IDD_PAGE_NEXT_INSTALL;
  123. #ifdef ENABLE_W3SVC_SSL_PAGE
  124. if (IsWebServerType(m_pCert->m_WebSiteInstanceName))
  125. {
  126. id = IDD_PAGE_NEXT_INSTALL_W3SVC_ONLY;
  127. }
  128. #endif
  129. break;
  130. default:
  131. ASSERT(FALSE);
  132. }
  133. return id;
  134. }
  135. BOOL CChooseCertPage::OnSetActive()
  136. {
  137. // If nothing is selected -- stay here
  138. SetWizardButtons(-1 == m_CertList.GetSelectedIndex() ?
  139. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  140. return CIISWizardPage::OnSetActive();
  141. }
  142. BOOL CChooseCertPage::OnInitDialog()
  143. {
  144. ASSERT(m_pCert != NULL);
  145. CIISWizardPage::OnInitDialog();
  146. CString str;
  147. str.LoadString(IDS_ISSUED_TO);
  148. m_CertList.InsertColumn(COL_COMMON_NAME, str, LVCFMT_LEFT, COL_COMMON_NAME_WID);
  149. str.LoadString(IDS_ISSUED_BY);
  150. m_CertList.InsertColumn(COL_CA_NAME, str, LVCFMT_LEFT, COL_CA_NAME_WID);
  151. str.LoadString(IDS_EXPIRATION_DATE);
  152. m_CertList.InsertColumn(COL_EXPIRATION_DATE, str, LVCFMT_LEFT, COL_EXPIRATION_DATE_WID);
  153. str.LoadString(IDS_PURPOSE);
  154. m_CertList.InsertColumn(COL_PURPOSE, str, LVCFMT_LEFT, COL_PURPOSE_WID);
  155. str.LoadString(IDS_FRIENDLY_NAME);
  156. m_CertList.InsertColumn(COL_FRIENDLY_NAME, str, LVCFMT_LEFT, COL_FRIENDLY_NAME_WID);
  157. m_CertList.AdjustStyle();
  158. if (m_pCert->GetCertDescList(m_DescList))
  159. {
  160. int item = 0;
  161. POSITION pos = m_DescList.GetHeadPosition();
  162. LV_ITEMW lvi;
  163. //
  164. // set up the fields in the list view item struct that don't change from item to item
  165. //
  166. memset(&lvi, 0, sizeof(LV_ITEMW));
  167. lvi.mask = LVIF_TEXT;
  168. m_CertList.SetItemCount((int)m_DescList.GetCount());
  169. while (pos != NULL)
  170. {
  171. CERT_DESCRIPTION * pDesc = m_DescList.GetNext(pos);
  172. int i;
  173. if (!pDesc->m_CommonName.IsEmpty())
  174. {
  175. lvi.iItem = item;
  176. lvi.iSubItem = 0;
  177. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_CommonName;
  178. lvi.cchTextMax = pDesc->m_CommonName.GetLength();
  179. i = m_CertList.InsertItem(&lvi);
  180. ASSERT(i != -1);
  181. }
  182. else
  183. {
  184. lvi.iItem = item;
  185. lvi.iSubItem = 0;
  186. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_AltSubject;
  187. lvi.cchTextMax = pDesc->m_AltSubject.GetLength();
  188. i = m_CertList.InsertItem(&lvi);
  189. ASSERT(i != -1);
  190. }
  191. lvi.iItem = i;
  192. lvi.iSubItem = COL_CA_NAME;
  193. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_CAName;
  194. lvi.cchTextMax = pDesc->m_CAName.GetLength();
  195. VERIFY(m_CertList.SetItem(&lvi));
  196. lvi.iSubItem = COL_EXPIRATION_DATE;
  197. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_ExpirationDate;
  198. lvi.cchTextMax = pDesc->m_ExpirationDate.GetLength();
  199. VERIFY(m_CertList.SetItem(&lvi));
  200. lvi.iSubItem = COL_PURPOSE;
  201. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_Usage;
  202. lvi.cchTextMax = pDesc->m_Usage.GetLength();
  203. VERIFY(m_CertList.SetItem(&lvi));
  204. lvi.iSubItem = COL_FRIENDLY_NAME;
  205. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_FriendlyName;
  206. lvi.cchTextMax = pDesc->m_FriendlyName.GetLength();
  207. VERIFY(m_CertList.SetItem(&lvi));
  208. // create CRYPT_HASH_BLOB from desc data and put it to list item
  209. CRYPT_HASH_BLOB * pHashBlob = new CRYPT_HASH_BLOB;
  210. ASSERT(pHashBlob != NULL);
  211. pHashBlob->cbData = pDesc->m_hash_length;
  212. pHashBlob->pbData = pDesc->m_phash;
  213. VERIFY(m_CertList.SetItemData(item, (LONG_PTR)pHashBlob));
  214. item++;
  215. }
  216. }
  217. return TRUE;
  218. }
  219. void CChooseCertPage::OnClickCertList(NMHDR* pNMHDR, LRESULT* pResult)
  220. {
  221. SetWizardButtons(-1 == m_CertList.GetSelectedIndex() ?
  222. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  223. *pResult = 0;
  224. }
  225. void CChooseCertPage::OnDblClickCertList(NMHDR* pNMHDR, LRESULT* pResult)
  226. {
  227. // Get the hash for the certificate that is clicked on...
  228. int index = m_CertList.GetSelectedIndex();
  229. if (index != -1)
  230. {
  231. // find cert in store
  232. CRYPT_HASH_BLOB * pHash = (CRYPT_HASH_BLOB *)m_CertList.GetItemData(index);
  233. m_pCert->m_pSelectedCertHash = pHash;
  234. ViewCertificateDialog(pHash,m_hWnd);
  235. // don't need to make modal, so the user can compare certs side-by-side
  236. //ViewCertificateDialog(pHash,NULL);
  237. }
  238. return;
  239. }
  240. void CChooseCertPage::OnKeydown(NMHDR* pNMHDR, LRESULT* pResult)
  241. {
  242. SetWizardButtons(-1 == m_CertList.GetSelectedIndex() ? PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  243. *pResult = 0;
  244. return;
  245. }
  246. void CChooseCertPage::OnDestroy()
  247. {
  248. // before dialog will be desroyed we need to delete all
  249. // the item data pointers
  250. int count = m_CertList.GetItemCount();
  251. for (int index = 0; index < count; index++)
  252. {
  253. CRYPT_HASH_BLOB * pData = (CRYPT_HASH_BLOB *)m_CertList.GetItemData(index);
  254. delete pData;
  255. }
  256. CIISWizardPage::OnDestroy();
  257. }