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.

323 lines
9.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2002.
  5. //
  6. // File: ACRSLast.cpp
  7. //
  8. // Contents: Implementation of Auto Cert Request Wizard Completion Page
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "ACRSLast.h"
  14. #include "ACRSPSht.h"
  15. #include "storegpe.h"
  16. USE_HANDLE_MACROS("CERTMGR(ACRSLast.cpp)")
  17. #ifdef _DEBUG
  18. #ifndef ALPHA
  19. #define new DEBUG_NEW
  20. #endif
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. // Gross
  25. #define MAX_GPE_NAME_SIZE 40
  26. /////////////////////////////////////////////////////////////////////////////
  27. // ACRSCompletionPage property page
  28. IMPLEMENT_DYNCREATE (ACRSCompletionPage, CWizard97PropertyPage)
  29. ACRSCompletionPage::ACRSCompletionPage () : CWizard97PropertyPage (ACRSCompletionPage::IDD)
  30. {
  31. //{{AFX_DATA_INIT(ACRSCompletionPage)
  32. // NOTE: the ClassWizard will add member initialization here
  33. //}}AFX_DATA_INIT
  34. InitWizard97 (TRUE);
  35. }
  36. ACRSCompletionPage::~ACRSCompletionPage ()
  37. {
  38. }
  39. void ACRSCompletionPage::DoDataExchange (CDataExchange* pDX)
  40. {
  41. CWizard97PropertyPage::DoDataExchange (pDX);
  42. //{{AFX_DATA_MAP(ACRSCompletionPage)
  43. DDX_Control (pDX, IDC_CHOICES_LIST, m_choicesList);
  44. DDX_Control (pDX, IDC_BOLD_STATIC, m_staticBold);
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(ACRSCompletionPage, CWizard97PropertyPage)
  48. //{{AFX_MSG_MAP(ACRSCompletionPage)
  49. // NOTE: the ClassWizard will add DDX and DDV calls here
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // ACRSCompletionPage message handlers
  54. BOOL ACRSCompletionPage::OnInitDialog ()
  55. {
  56. CWizard97PropertyPage::OnInitDialog ();
  57. m_staticBold.SetFont (&GetBigBoldFont ());
  58. // Set up columns in list view
  59. int colWidths[NUM_COLS] = {150, 200};
  60. VERIFY (m_choicesList.InsertColumn (COL_OPTION, L"",
  61. LVCFMT_LEFT, colWidths[COL_OPTION], COL_OPTION) != -1);
  62. VERIFY (m_choicesList.InsertColumn (COL_VALUE, L"",
  63. LVCFMT_LEFT, colWidths[COL_VALUE], COL_VALUE) != -1);
  64. return TRUE; // return TRUE unless you set the focus to a control
  65. // EXCEPTION: OCX Property Pages should return FALSE
  66. }
  67. BOOL ACRSCompletionPage::OnSetActive ()
  68. {
  69. BOOL bResult = CWizard97PropertyPage::OnSetActive ();
  70. if ( bResult )
  71. {
  72. // Remove all items then repopulate.
  73. ACRSWizardPropertySheet* pSheet = reinterpret_cast <ACRSWizardPropertySheet*> (m_pWiz);
  74. ASSERT (pSheet);
  75. if ( pSheet )
  76. {
  77. // If edit mode and nothing changed, show disabled finish
  78. if ( pSheet->GetACR () && !pSheet->m_bEditModeDirty )
  79. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0, PSWIZB_DISABLEDFINISH | PSWIZB_BACK);
  80. else
  81. GetParent ()->PostMessage (PSM_SETWIZBUTTONS, 0, PSWIZB_FINISH | PSWIZB_BACK);
  82. if ( pSheet->IsDirty () )
  83. pSheet->MarkAsClean ();
  84. VERIFY (m_choicesList.DeleteAllItems ());
  85. CString text;
  86. LV_ITEM lvItem;
  87. int iItem = 0;
  88. // Display cert type selection
  89. VERIFY (text.LoadString (IDS_CERTIFICATE_TYPE_COLUMN_NAME));
  90. // security review 2/25/2002 BryanWal ok
  91. ::ZeroMemory (&lvItem, sizeof (lvItem));
  92. lvItem.mask = LVIF_TEXT;
  93. lvItem.iItem = iItem;
  94. lvItem.iSubItem = COL_OPTION;
  95. lvItem.pszText = (LPWSTR) (LPCWSTR) text;
  96. VERIFY (-1 != m_choicesList.InsertItem (&lvItem));
  97. WCHAR** pawszPropertyValue = 0;
  98. HRESULT hResult = ::CAGetCertTypeProperty (pSheet->m_selectedCertType,
  99. CERTTYPE_PROP_FRIENDLY_NAME,
  100. &pawszPropertyValue);
  101. ASSERT (SUCCEEDED (hResult));
  102. if ( SUCCEEDED (hResult) )
  103. {
  104. if ( pawszPropertyValue[0] )
  105. {
  106. VERIFY (m_choicesList.SetItemText (iItem, COL_VALUE,
  107. *pawszPropertyValue));
  108. }
  109. VERIFY (SUCCEEDED (::CAFreeCertTypeProperty (
  110. pSheet->m_selectedCertType, pawszPropertyValue)));
  111. }
  112. iItem++;
  113. }
  114. }
  115. return bResult;
  116. }
  117. BOOL ACRSCompletionPage::OnWizardFinish ()
  118. {
  119. BOOL bResult = TRUE;
  120. HRESULT hResult = S_OK;
  121. CWaitCursor waitCursor;
  122. ACRSWizardPropertySheet* pSheet = reinterpret_cast <ACRSWizardPropertySheet*> (m_pWiz);
  123. ASSERT (pSheet);
  124. if ( pSheet )
  125. {
  126. // If edit mode and nothing changed, just return
  127. if ( pSheet->GetACR () && !pSheet->m_bEditModeDirty )
  128. {
  129. ASSERT (0);
  130. return FALSE;
  131. }
  132. BYTE *pbEncodedCTL = NULL;
  133. DWORD cbEncodedCTL = 0;
  134. hResult = MakeCTL (&pbEncodedCTL, &cbEncodedCTL);
  135. if ( SUCCEEDED (hResult) )
  136. {
  137. bResult = pSheet->m_pCertStore->AddEncodedCTL (
  138. X509_ASN_ENCODING,
  139. pbEncodedCTL, cbEncodedCTL,
  140. CERT_STORE_ADD_REPLACE_EXISTING,
  141. NULL);
  142. if ( !bResult )
  143. {
  144. DWORD dwErr = GetLastError ();
  145. hResult = HRESULT_FROM_WIN32 (dwErr);
  146. DisplaySystemError (m_hWnd, dwErr);
  147. }
  148. }
  149. if (pbEncodedCTL)
  150. ::LocalFree (pbEncodedCTL);
  151. }
  152. if ( SUCCEEDED (hResult) )
  153. bResult = CWizard97PropertyPage::OnWizardFinish ();
  154. else
  155. bResult = FALSE;
  156. return bResult;
  157. }
  158. HRESULT ACRSCompletionPage::MakeCTL (
  159. OUT BYTE **ppbEncodedCTL,
  160. OUT DWORD *pcbEncodedCTL)
  161. {
  162. HRESULT hResult = S_OK;
  163. ACRSWizardPropertySheet* pSheet = reinterpret_cast <ACRSWizardPropertySheet*> (m_pWiz);
  164. ASSERT (pSheet);
  165. if ( pSheet )
  166. {
  167. PCERT_EXTENSIONS pCertExtensions = NULL;
  168. hResult = ::CAGetCertTypeExtensions (pSheet->m_selectedCertType, &pCertExtensions);
  169. ASSERT (SUCCEEDED (hResult));
  170. if ( SUCCEEDED (hResult) )
  171. {
  172. CMSG_SIGNED_ENCODE_INFO SignerInfo;
  173. // security review 2/25/2002 BryanWal ok
  174. ::ZeroMemory (&SignerInfo, sizeof (SignerInfo));
  175. CTL_INFO CTLInfo;
  176. // security review 2/25/2002 BryanWal ok
  177. ::ZeroMemory (&CTLInfo, sizeof (CTLInfo));
  178. WCHAR** pawszPropName = 0;
  179. // set up the CTL info
  180. CTLInfo.dwVersion = sizeof (CTLInfo);
  181. CTLInfo.SubjectUsage.cUsageIdentifier = 1;
  182. hResult = ::CAGetCertTypeProperty (pSheet->m_selectedCertType,
  183. CERTTYPE_PROP_DN, &pawszPropName);
  184. ASSERT (SUCCEEDED (hResult));
  185. if ( SUCCEEDED (hResult) && pawszPropName[0] )
  186. {
  187. LPSTR psz = szOID_AUTO_ENROLL_CTL_USAGE;
  188. WCHAR szGPEName[MAX_GPE_NAME_SIZE];
  189. IGPEInformation *pGPEInfo = pSheet->m_pCertStore->GetGPEInformation();
  190. // security review 2/25/2002 BryanWal ok
  191. ::ZeroMemory (szGPEName, sizeof (szGPEName));
  192. // Allocate the size of the property name plus the GPEName, if any
  193. // security review 2/25/2002 BryanWal ok
  194. CTLInfo.ListIdentifier.cbData = (DWORD) (sizeof (WCHAR) * (wcslen (pawszPropName[0]) + 1));
  195. if ( pGPEInfo )
  196. {
  197. pGPEInfo->GetName(szGPEName, sizeof(szGPEName)/sizeof(szGPEName[0]));
  198. // security review 2/25/2002 BryanWal ok
  199. CTLInfo.ListIdentifier.cbData += (DWORD) (sizeof(WCHAR)*(wcslen(szGPEName)+1));
  200. }
  201. CTLInfo.ListIdentifier.pbData = (PBYTE)LocalAlloc(LPTR, CTLInfo.ListIdentifier.cbData);
  202. if(CTLInfo.ListIdentifier.pbData == NULL)
  203. {
  204. hResult = E_OUTOFMEMORY;
  205. }
  206. else //Bug 427957, 427958, Yanggao, 7/16/2001
  207. {
  208. // ISSUE - convert to strsafe. Ensure sufficient buffer
  209. // size for the following operations
  210. // NTRAID Bug9 538774 Security: certmgr.dll : convert to strsafe string functions
  211. if(szGPEName[0])
  212. {
  213. wcscpy((LPWSTR)CTLInfo.ListIdentifier.pbData, szGPEName);
  214. wcscat((LPWSTR)CTLInfo.ListIdentifier.pbData, L"|");
  215. }
  216. wcscat((LPWSTR)CTLInfo.ListIdentifier.pbData, pawszPropName[0]);
  217. }
  218. CTLInfo.SubjectUsage.rgpszUsageIdentifier = &psz;
  219. ::GetSystemTimeAsFileTime (&CTLInfo.ThisUpdate);
  220. CTLInfo.SubjectAlgorithm.pszObjId = szOID_OIWSEC_sha1;
  221. CTLInfo.cCTLEntry = 0;
  222. CTLInfo.rgCTLEntry = 0;
  223. // UNDONE - add the cert type extension
  224. // add all the reg info as an extension
  225. CTLInfo.cExtension = pCertExtensions->cExtension;
  226. CTLInfo.rgExtension = pCertExtensions->rgExtension;
  227. // encode the CTL
  228. *pcbEncodedCTL = 0;
  229. SignerInfo.cbSize = sizeof (SignerInfo);
  230. if ( ::CryptMsgEncodeAndSignCTL (PKCS_7_ASN_ENCODING,
  231. &CTLInfo, &SignerInfo, 0,
  232. NULL, pcbEncodedCTL) )
  233. {
  234. *ppbEncodedCTL = (BYTE*) ::LocalAlloc (LPTR, *pcbEncodedCTL);
  235. if ( *ppbEncodedCTL )
  236. {
  237. if (!::CryptMsgEncodeAndSignCTL (PKCS_7_ASN_ENCODING,
  238. &CTLInfo, &SignerInfo, 0,
  239. *ppbEncodedCTL, pcbEncodedCTL))
  240. {
  241. DWORD dwErr = GetLastError ();
  242. hResult = HRESULT_FROM_WIN32 (dwErr);
  243. DisplaySystemError (m_hWnd, dwErr);
  244. }
  245. }
  246. else
  247. {
  248. hResult = E_OUTOFMEMORY;
  249. }
  250. }
  251. else
  252. {
  253. DWORD dwErr = GetLastError ();
  254. hResult = HRESULT_FROM_WIN32 (dwErr);
  255. DisplaySystemError (m_hWnd, dwErr);
  256. }
  257. VERIFY (SUCCEEDED (::CAFreeCertTypeProperty (
  258. pSheet->m_selectedCertType, pawszPropName)));
  259. }
  260. if(CTLInfo.ListIdentifier.pbData)
  261. {
  262. ::LocalFree(CTLInfo.ListIdentifier.pbData);
  263. }
  264. }
  265. if (pCertExtensions)
  266. ::LocalFree (pCertExtensions);
  267. }
  268. return hResult;
  269. }