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.

265 lines
6.8 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_WM_DESTROY()
  83. //}}AFX_MSG_MAP
  84. END_MESSAGE_MAP()
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CChooseCertPage message handlers
  87. LRESULT CChooseCertPage::OnWizardBack()
  88. {
  89. LRESULT id = 1;
  90. switch (m_pCert->GetStatusCode())
  91. {
  92. case CCertificate::REQUEST_REPLACE_CERT:
  93. id = IDD_PAGE_PREV_REPLACE;
  94. break;
  95. case CCertificate::REQUEST_INSTALL_CERT:
  96. id = IDD_PAGE_PREV_INSTALL;
  97. break;
  98. default:
  99. ASSERT(FALSE);
  100. }
  101. return id;
  102. }
  103. LRESULT CChooseCertPage::OnWizardNext()
  104. {
  105. // get hash pointer for selected cert
  106. int index = m_CertList.GetSelectedIndex();
  107. ASSERT(index != -1);
  108. // find cert in store
  109. CRYPT_HASH_BLOB * pHash = (CRYPT_HASH_BLOB *)m_CertList.GetItemData(index);
  110. ASSERT(pHash != NULL);
  111. m_pCert->m_pSelectedCertHash = pHash;
  112. LRESULT id = 1;
  113. switch (m_pCert->GetStatusCode())
  114. {
  115. case CCertificate::REQUEST_REPLACE_CERT:
  116. id = IDD_PAGE_NEXT_REPLACE;
  117. break;
  118. case CCertificate::REQUEST_INSTALL_CERT:
  119. id = IDD_PAGE_NEXT_INSTALL;
  120. break;
  121. default:
  122. ASSERT(FALSE);
  123. }
  124. return id;
  125. }
  126. BOOL CChooseCertPage::OnSetActive()
  127. {
  128. // If nothing is selected -- stay here
  129. SetWizardButtons(-1 == m_CertList.GetSelectedIndex() ?
  130. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  131. return CIISWizardPage::OnSetActive();
  132. }
  133. BOOL CChooseCertPage::OnInitDialog()
  134. {
  135. ASSERT(m_pCert != NULL);
  136. CIISWizardPage::OnInitDialog();
  137. CString str;
  138. str.LoadString(IDS_ISSUED_TO);
  139. m_CertList.InsertColumn(COL_COMMON_NAME, str, LVCFMT_LEFT, COL_COMMON_NAME_WID);
  140. str.LoadString(IDS_ISSUED_BY);
  141. m_CertList.InsertColumn(COL_CA_NAME, str, LVCFMT_LEFT, COL_CA_NAME_WID);
  142. str.LoadString(IDS_EXPIRATION_DATE);
  143. m_CertList.InsertColumn(COL_EXPIRATION_DATE, str, LVCFMT_LEFT, COL_EXPIRATION_DATE_WID);
  144. str.LoadString(IDS_PURPOSE);
  145. m_CertList.InsertColumn(COL_PURPOSE, str, LVCFMT_LEFT, COL_PURPOSE_WID);
  146. str.LoadString(IDS_FRIENDLY_NAME);
  147. m_CertList.InsertColumn(COL_FRIENDLY_NAME, str, LVCFMT_LEFT, COL_FRIENDLY_NAME_WID);
  148. m_CertList.AdjustStyle();
  149. if (m_pCert->GetCertDescList(m_DescList))
  150. {
  151. int item = 0;
  152. POSITION pos = m_DescList.GetHeadPosition();
  153. LV_ITEMW lvi;
  154. //
  155. // set up the fields in the list view item struct that don't change from item to item
  156. //
  157. memset(&lvi, 0, sizeof(LV_ITEMW));
  158. lvi.mask = LVIF_TEXT;
  159. m_CertList.SetItemCount((int)m_DescList.GetCount());
  160. while (pos != NULL)
  161. {
  162. CERT_DESCRIPTION * pDesc = m_DescList.GetNext(pos);
  163. int i;
  164. lvi.iItem = item;
  165. lvi.iSubItem = 0;
  166. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_CommonName;
  167. lvi.cchTextMax = pDesc->m_CommonName.GetLength();
  168. i = m_CertList.InsertItem(&lvi);
  169. ASSERT(i != -1);
  170. lvi.iItem = i;
  171. lvi.iSubItem = COL_CA_NAME;
  172. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_CAName;
  173. lvi.cchTextMax = pDesc->m_CAName.GetLength();
  174. VERIFY(m_CertList.SetItem(&lvi));
  175. lvi.iSubItem = COL_EXPIRATION_DATE;
  176. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_ExpirationDate;
  177. lvi.cchTextMax = pDesc->m_ExpirationDate.GetLength();
  178. VERIFY(m_CertList.SetItem(&lvi));
  179. lvi.iSubItem = COL_PURPOSE;
  180. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_Usage;
  181. lvi.cchTextMax = pDesc->m_Usage.GetLength();
  182. VERIFY(m_CertList.SetItem(&lvi));
  183. lvi.iSubItem = COL_FRIENDLY_NAME;
  184. lvi.pszText = (LPTSTR)(LPCTSTR)pDesc->m_FriendlyName;
  185. lvi.cchTextMax = pDesc->m_FriendlyName.GetLength();
  186. VERIFY(m_CertList.SetItem(&lvi));
  187. // create CRYPT_HASH_BLOB from desc data and put it to list item
  188. CRYPT_HASH_BLOB * pHashBlob = new CRYPT_HASH_BLOB;
  189. ASSERT(pHashBlob != NULL);
  190. pHashBlob->cbData = pDesc->m_hash_length;
  191. pHashBlob->pbData = pDesc->m_hash;
  192. VERIFY(m_CertList.SetItemData(item, (LONG_PTR)pHashBlob));
  193. item++;
  194. }
  195. }
  196. return TRUE;
  197. }
  198. void CChooseCertPage::OnClickCertList(NMHDR* pNMHDR, LRESULT* pResult)
  199. {
  200. SetWizardButtons(-1 == m_CertList.GetSelectedIndex() ?
  201. PSWIZB_BACK : PSWIZB_BACK | PSWIZB_NEXT);
  202. *pResult = 0;
  203. }
  204. void CChooseCertPage::OnDblClickCertList(NMHDR* pNMHDR, LRESULT* pResult)
  205. {
  206. // Get the hash for the certificate that is clicked on...
  207. int index = m_CertList.GetSelectedIndex();
  208. if (index != -1)
  209. {
  210. // find cert in store
  211. CRYPT_HASH_BLOB * pHash = (CRYPT_HASH_BLOB *)m_CertList.GetItemData(index);
  212. m_pCert->m_pSelectedCertHash = pHash;
  213. ViewCertificateDialog(pHash,m_hWnd);
  214. // don't need to make modal, so the user can compare certs side-by-side
  215. //ViewCertificateDialog(pHash,NULL);
  216. }
  217. return;
  218. }
  219. void CChooseCertPage::OnDestroy()
  220. {
  221. // before dialog will be desroyed we need to delete all
  222. // the item data pointers
  223. int count = m_CertList.GetItemCount();
  224. for (int index = 0; index < count; index++)
  225. {
  226. CRYPT_HASH_BLOB * pData = (CRYPT_HASH_BLOB *)m_CertList.GetItemData(index);
  227. delete pData;
  228. }
  229. CIISWizardPage::OnDestroy();
  230. }