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.

330 lines
11 KiB

  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2001.
  5. //
  6. // File: KeyUsageDlg.cpp
  7. //
  8. // Contents: Implementation of CKeyUsageDlg
  9. //
  10. //----------------------------------------------------------------------------
  11. // KeyUsageDlg.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "KeyUsageDlg.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. #define WM_INITIALIZATION_COMPLETE WM_APP + 2002
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CKeyUsageDlg property page
  23. CKeyUsageDlg::CKeyUsageDlg(CWnd* pParent, CCertTemplate& rCertTemplate, PCERT_EXTENSION pCertExtension) :
  24. CHelpDialog(CKeyUsageDlg::IDD, pParent),
  25. m_rCertTemplate (rCertTemplate),
  26. m_pCertExtension (pCertExtension),
  27. m_bModified (false),
  28. m_pKeyUsage (0),
  29. m_cbKeyUsage (0),
  30. m_bInitializationComplete (false)
  31. {
  32. //{{AFX_DATA_INIT(CKeyUsageDlg)
  33. // NOTE: the ClassWizard will add member initialization here
  34. //}}AFX_DATA_INIT
  35. m_rCertTemplate.AddRef ();
  36. }
  37. CKeyUsageDlg::~CKeyUsageDlg()
  38. {
  39. if ( m_pKeyUsage )
  40. LocalFree (m_pKeyUsage);
  41. m_rCertTemplate.Release ();
  42. }
  43. void CKeyUsageDlg::DoDataExchange(CDataExchange* pDX)
  44. {
  45. CHelpDialog::DoDataExchange(pDX);
  46. //{{AFX_DATA_MAP(CKeyUsageDlg)
  47. // NOTE: the ClassWizard will add DDX and DDV calls here
  48. //}}AFX_DATA_MAP
  49. }
  50. BEGIN_MESSAGE_MAP(CKeyUsageDlg, CHelpDialog)
  51. //{{AFX_MSG_MAP(CKeyUsageDlg)
  52. ON_BN_CLICKED(IDC_CHECK_CERT_SIGNING, OnCheckCertSigning)
  53. ON_BN_CLICKED(IDC_CHECK_DATA_ENCIPHERMENT, OnCheckDataEncipherment)
  54. ON_BN_CLICKED(IDC_CHECK_DIGITAL_SIGNATURE, OnCheckDigitalSignature)
  55. ON_BN_CLICKED(IDC_CHECK_KEY_AGREEMENT, OnCheckKeyAgreement)
  56. ON_BN_CLICKED(IDC_CHECK_KEY_ENCIPHERMENT, OnCheckKeyEncipherment)
  57. ON_BN_CLICKED(IDC_CHECK_NON_REPUDIATION, OnCheckNonRepudiation)
  58. ON_BN_CLICKED(IDC_CRL_SIGNING, OnCrlSigning)
  59. ON_BN_CLICKED(IDC_KEY_USAGE_CRITICAL, OnKeyUsageCritical)
  60. //}}AFX_MSG_MAP
  61. ON_MESSAGE (WM_INITIALIZATION_COMPLETE, OnInitializationComplete)
  62. END_MESSAGE_MAP()
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CKeyUsageDlg message handlers
  65. BOOL CKeyUsageDlg::OnInitDialog()
  66. {
  67. _TRACE (1, L"Entering CKeyUsageDlg::OnInitDialog\n");
  68. CHelpDialog::OnInitDialog();
  69. ASSERT (m_pCertExtension);
  70. if ( m_pCertExtension )
  71. {
  72. if ( m_pCertExtension->fCritical )
  73. SendDlgItemMessage (IDC_KEY_USAGE_CRITICAL, BM_SETCHECK, BST_CHECKED);
  74. if ( ::CryptDecodeObject(CRYPT_ASN_ENCODING,
  75. szOID_KEY_USAGE,
  76. m_pCertExtension->Value.pbData,
  77. m_pCertExtension->Value.cbData,
  78. 0, NULL, &m_cbKeyUsage) )
  79. {
  80. m_pKeyUsage = (CRYPT_BIT_BLOB*)
  81. ::LocalAlloc (LPTR, m_cbKeyUsage);
  82. if ( m_pKeyUsage )
  83. {
  84. if ( ::CryptDecodeObject (CRYPT_ASN_ENCODING,
  85. szOID_KEY_USAGE,
  86. m_pCertExtension->Value.pbData,
  87. m_pCertExtension->Value.cbData,
  88. 0, m_pKeyUsage, &m_cbKeyUsage) )
  89. {
  90. if (m_pKeyUsage->cbData >= 1)
  91. {
  92. if ( m_pKeyUsage->pbData[0] & CERT_DIGITAL_SIGNATURE_KEY_USAGE )
  93. SendDlgItemMessage (IDC_CHECK_DIGITAL_SIGNATURE, BM_SETCHECK, BST_CHECKED);
  94. if ( m_pKeyUsage->pbData[0] & CERT_NON_REPUDIATION_KEY_USAGE )
  95. SendDlgItemMessage (IDC_CHECK_NON_REPUDIATION, BM_SETCHECK, BST_CHECKED);
  96. if ( m_pKeyUsage->pbData[0] & CERT_KEY_ENCIPHERMENT_KEY_USAGE )
  97. SendDlgItemMessage (IDC_CHECK_KEY_ENCIPHERMENT, BM_SETCHECK, BST_CHECKED);
  98. if ( m_pKeyUsage->pbData[0] & CERT_DATA_ENCIPHERMENT_KEY_USAGE )
  99. SendDlgItemMessage (IDC_CHECK_DATA_ENCIPHERMENT, BM_SETCHECK, BST_CHECKED);
  100. if ( m_pKeyUsage->pbData[0] & CERT_KEY_AGREEMENT_KEY_USAGE )
  101. SendDlgItemMessage (IDC_CHECK_KEY_AGREEMENT, BM_SETCHECK, BST_CHECKED);
  102. if ( m_pKeyUsage->pbData[0] & CERT_KEY_CERT_SIGN_KEY_USAGE )
  103. SendDlgItemMessage (IDC_CHECK_CERT_SIGNING, BM_SETCHECK, BST_CHECKED);
  104. if ( m_pKeyUsage->pbData[0] & CERT_OFFLINE_CRL_SIGN_KEY_USAGE )
  105. SendDlgItemMessage (IDC_CRL_SIGNING, BM_SETCHECK, BST_CHECKED);
  106. }
  107. if (m_pKeyUsage->cbData >= 2)
  108. {
  109. // if ( m_pKeyUsage->pbData[1] & CERT_DECIPHER_ONLY_KEY_USAGE )
  110. // SendDlgItemMessage (IDC_CHECK_DECIPHERMENT_ONLY, BM_SETCHECK, BST_CHECKED);
  111. }
  112. }
  113. else
  114. {
  115. DWORD dwErr = GetLastError ();
  116. _TRACE (0, L"CryptDecodeObject (szOID_KEY_USAGE) failed: 0x%x\n", dwErr);
  117. DisplaySystemError (NULL, dwErr);
  118. }
  119. }
  120. }
  121. else
  122. {
  123. DWORD dwErr = GetLastError ();
  124. _TRACE (0, L"CryptDecodeObject (szOID_KEY_USAGE) failed: 0x%x\n", dwErr);
  125. DisplaySystemError (NULL, dwErr);
  126. }
  127. }
  128. // Digital Signature is always disabled
  129. GetDlgItem (IDC_CHECK_DIGITAL_SIGNATURE)->EnableWindow (FALSE);
  130. if ( 1 == m_rCertTemplate.GetType () )
  131. {
  132. GetDlgItem (IDC_KEY_USAGE_CRITICAL)->EnableWindow (FALSE);
  133. GetDlgItem (IDC_CHECK_NON_REPUDIATION)->EnableWindow (FALSE);
  134. GetDlgItem (IDC_CHECK_KEY_ENCIPHERMENT)->EnableWindow (FALSE);
  135. GetDlgItem (IDC_CHECK_DATA_ENCIPHERMENT)->EnableWindow (FALSE);
  136. GetDlgItem (IDC_CHECK_KEY_AGREEMENT)->EnableWindow (FALSE);
  137. GetDlgItem (IDC_CHECK_CERT_SIGNING)->EnableWindow (FALSE);
  138. }
  139. EnableControls ();
  140. PostMessage (WM_INITIALIZATION_COMPLETE);
  141. _TRACE (-1, L"Leaving CKeyUsageDlg::OnInitDialog\n");
  142. return TRUE; // return TRUE unless you set the focus to a control
  143. // EXCEPTION: OCX Property Pages should return FALSE
  144. }
  145. void CKeyUsageDlg::OnCheckCertSigning()
  146. {
  147. m_bModified = true;
  148. EnableControls ();
  149. }
  150. void CKeyUsageDlg::OnCheckDataEncipherment()
  151. {
  152. m_bModified = true;
  153. EnableControls ();
  154. }
  155. void CKeyUsageDlg::OnCheckDigitalSignature()
  156. {
  157. m_bModified = true;
  158. EnableControls ();
  159. }
  160. void CKeyUsageDlg::OnCheckKeyAgreement()
  161. {
  162. if ( m_bInitializationComplete )
  163. {
  164. SendDlgItemMessage (IDC_CHECK_DATA_ENCIPHERMENT, BM_SETCHECK, BST_UNCHECKED);
  165. m_bModified = true;
  166. EnableControls ();
  167. }
  168. }
  169. void CKeyUsageDlg::OnCheckKeyEncipherment()
  170. {
  171. m_bModified = true;
  172. EnableControls ();
  173. }
  174. void CKeyUsageDlg::OnCheckNonRepudiation()
  175. {
  176. m_bModified = true;
  177. EnableControls ();
  178. }
  179. void CKeyUsageDlg::OnCrlSigning()
  180. {
  181. m_bModified = true;
  182. EnableControls ();
  183. }
  184. void CKeyUsageDlg::EnableControls()
  185. {
  186. if ( 1 == m_rCertTemplate.GetType () )
  187. {
  188. GetDlgItem (IDC_SIGNATURE_OPTIONS)->EnableWindow (FALSE);
  189. GetDlgItem (IDC_CHECK_NON_REPUDIATION)->EnableWindow (FALSE);
  190. GetDlgItem (IDC_CHECK_CERT_SIGNING)->EnableWindow (FALSE);
  191. GetDlgItem (IDC_CRL_SIGNING)->EnableWindow (FALSE);
  192. GetDlgItem (IDC_ENCRYPTION_OPTIONS)->EnableWindow (FALSE);
  193. GetDlgItem (IDC_CHECK_KEY_AGREEMENT)->EnableWindow (FALSE);
  194. GetDlgItem (IDC_CHECK_KEY_ENCIPHERMENT)->EnableWindow (FALSE);
  195. GetDlgItem (IDC_CHECK_DATA_ENCIPHERMENT)->EnableWindow (FALSE);
  196. GetDlgItem (IDOK)->EnableWindow (FALSE);
  197. GetDlgItem (IDC_KEY_USAGE_CRITICAL)->EnableWindow (FALSE);
  198. }
  199. else
  200. {
  201. BOOL bKeyEnc = (BST_CHECKED == SendDlgItemMessage (IDC_CHECK_KEY_ENCIPHERMENT, BM_GETCHECK));
  202. bool bEncryption = m_rCertTemplate.HasEncryptionSignature () && !m_rCertTemplate.ReadOnly ();
  203. bool bSignature = m_rCertTemplate.HasKeySpecSignature () && !m_rCertTemplate.ReadOnly ();
  204. // Enable Signature group
  205. GetDlgItem (IDC_SIGNATURE_OPTIONS)->EnableWindow (bSignature);
  206. GetDlgItem (IDC_CHECK_NON_REPUDIATION)->EnableWindow (bSignature);
  207. GetDlgItem (IDC_CHECK_CERT_SIGNING)->EnableWindow (bSignature);
  208. GetDlgItem (IDC_CRL_SIGNING)->EnableWindow (bSignature);
  209. // Enable Encryption group
  210. GetDlgItem (IDC_ENCRYPTION_OPTIONS)->EnableWindow (bEncryption);
  211. GetDlgItem (IDC_CHECK_KEY_AGREEMENT)->EnableWindow (bEncryption);
  212. GetDlgItem (IDC_CHECK_KEY_ENCIPHERMENT)->EnableWindow (bEncryption);
  213. GetDlgItem (IDC_CHECK_DATA_ENCIPHERMENT)->EnableWindow (bKeyEnc && bEncryption);
  214. GetDlgItem (IDOK)->EnableWindow (m_bModified);
  215. GetDlgItem (IDC_KEY_USAGE_CRITICAL)->EnableWindow (!m_rCertTemplate.ReadOnly ());
  216. }
  217. }
  218. void CKeyUsageDlg::OnKeyUsageCritical()
  219. {
  220. m_bModified = true;
  221. EnableControls ();
  222. }
  223. void CKeyUsageDlg::DoContextHelp (HWND hWndControl)
  224. {
  225. _TRACE(1, L"Entering CKeyUsageDlg::DoContextHelp\n");
  226. switch (::GetDlgCtrlID (hWndControl))
  227. {
  228. case IDC_SIGNATURE_OPTIONS:
  229. case IDC_ENCRYPTION_OPTIONS:
  230. break;
  231. default:
  232. // Display context help for a control
  233. if ( !::WinHelp (
  234. hWndControl,
  235. GetContextHelpFile (),
  236. HELP_WM_HELP,
  237. (DWORD_PTR) g_aHelpIDs_IDD_KEY_USAGE) )
  238. {
  239. _TRACE(0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  240. }
  241. break;
  242. }
  243. _TRACE(-1, L"Leaving CKeyUsageDlg::DoContextHelp\n");
  244. }
  245. void CKeyUsageDlg::OnOK()
  246. {
  247. if (m_pKeyUsage->cbData >= 1)
  248. {
  249. m_pKeyUsage->pbData[0] = 0;
  250. m_pKeyUsage->cUnusedBits = 0;
  251. if ( BST_CHECKED == SendDlgItemMessage (IDC_CHECK_DIGITAL_SIGNATURE, BM_GETCHECK) )
  252. m_pKeyUsage->pbData[0] |= CERT_DIGITAL_SIGNATURE_KEY_USAGE;
  253. if ( BST_CHECKED == SendDlgItemMessage (IDC_CHECK_NON_REPUDIATION, BM_GETCHECK) )
  254. m_pKeyUsage->pbData[0] |= CERT_NON_REPUDIATION_KEY_USAGE;
  255. if ( BST_CHECKED == SendDlgItemMessage (IDC_CHECK_KEY_ENCIPHERMENT, BM_GETCHECK) )
  256. m_pKeyUsage->pbData[0] |= CERT_KEY_ENCIPHERMENT_KEY_USAGE;
  257. if ( BST_CHECKED == SendDlgItemMessage (IDC_CHECK_DATA_ENCIPHERMENT, BM_GETCHECK) )
  258. m_pKeyUsage->pbData[0] |= CERT_DATA_ENCIPHERMENT_KEY_USAGE;
  259. if ( BST_CHECKED == SendDlgItemMessage (IDC_CHECK_KEY_AGREEMENT, BM_GETCHECK) )
  260. m_pKeyUsage->pbData[0] |= CERT_KEY_AGREEMENT_KEY_USAGE;
  261. if ( BST_CHECKED == SendDlgItemMessage (IDC_CHECK_CERT_SIGNING, BM_GETCHECK) )
  262. m_pKeyUsage->pbData[0] |= CERT_KEY_CERT_SIGN_KEY_USAGE;
  263. if ( BST_CHECKED == SendDlgItemMessage (IDC_CRL_SIGNING, BM_GETCHECK) )
  264. m_pKeyUsage->pbData[0] |= CERT_OFFLINE_CRL_SIGN_KEY_USAGE;
  265. }
  266. bool bCritical = BST_CHECKED == SendDlgItemMessage (
  267. IDC_KEY_USAGE_CRITICAL, BM_GETCHECK);
  268. HRESULT hr = m_rCertTemplate.SetKeyUsage (m_pKeyUsage, bCritical);
  269. if ( FAILED (hr) )
  270. return;
  271. CHelpDialog::OnOK();
  272. }
  273. LRESULT CKeyUsageDlg::OnInitializationComplete (WPARAM, LPARAM)
  274. {
  275. m_bInitializationComplete = true;
  276. return 0;
  277. }