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.

308 lines
10 KiB

  1. // CreateKeyDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "KeyRing.h"
  5. extern "C"
  6. {
  7. #include <wincrypt.h>
  8. #include <sslsp.h>
  9. }
  10. #include "NwKeyDlg.h"
  11. #include "PassDlg.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CCreateKeyDlg dialog
  19. CCreateKeyDlg::CCreateKeyDlg(CWnd* pParent /*=NULL*/)
  20. : CDialog(CCreateKeyDlg::IDD, pParent)
  21. {
  22. //{{AFX_DATA_INIT(CCreateKeyDlg)
  23. m_szNetAddress = _T("");
  24. m_szCountry = _T("");
  25. m_szLocality = _T("");
  26. m_szOrganization = _T("");
  27. m_szUnit = _T("");
  28. m_szState = _T("");
  29. m_szKeyName = _T("");
  30. m_szCertificateFile = _T("");
  31. m_szPassword = _T("");
  32. //}}AFX_DATA_INIT
  33. }
  34. void CCreateKeyDlg::DoDataExchange(CDataExchange* pDX)
  35. {
  36. CDialog::DoDataExchange(pDX);
  37. //{{AFX_DATA_MAP(CCreateKeyDlg)
  38. DDX_Control(pDX, IDC_NEW_KEY_PASSWORD, m_ceditPassword);
  39. DDX_Control(pDX, IDOK, m_btnOK);
  40. DDX_Control(pDX, IDC_NEW_KEY_BITS, m_comboBits);
  41. DDX_Text(pDX, IDC_NEWKEY_COMMONNAME, m_szNetAddress);
  42. DDX_Text(pDX, IDC_NEWKEY_COUNTRY, m_szCountry);
  43. DDV_MaxChars(pDX, m_szCountry, 2);
  44. DDX_Text(pDX, IDC_NEWKEY_LOCALITY, m_szLocality);
  45. DDX_Text(pDX, IDC_NEWKEY_ORG, m_szOrganization);
  46. DDX_Text(pDX, IDC_NEWKEY_ORGUNIT, m_szUnit);
  47. DDX_Text(pDX, IDC_NEWKEY_STATE, m_szState);
  48. DDX_Text(pDX, IDC_NEW_KEY_NAME, m_szKeyName);
  49. DDX_Text(pDX, IDC_NEW_KEY_REQUEST_FILE, m_szCertificateFile);
  50. DDX_Text(pDX, IDC_NEW_KEY_PASSWORD, m_szPassword);
  51. //}}AFX_DATA_MAP
  52. }
  53. BEGIN_MESSAGE_MAP(CCreateKeyDlg, CDialog)
  54. //{{AFX_MSG_MAP(CCreateKeyDlg)
  55. ON_EN_CHANGE(IDC_NEW_KEY_NAME, OnChangeNewKeyName)
  56. ON_BN_CLICKED(IDC_NEW_KEY_BROWSE, OnNewKeyBrowse)
  57. ON_EN_CHANGE(IDC_NEW_KEY_REQUEST_FILE, OnChangeNewKeyRequestFile)
  58. ON_EN_CHANGE(IDC_NEW_KEY_PASSWORD, OnChangeNewKeyPassword)
  59. //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CCreateKeyDlg message handlers
  63. /////////////////////////////////////////////////////////////////////////////
  64. // members
  65. //----------------------------------------------------------------
  66. // override virtual oninitdialog
  67. BOOL CCreateKeyDlg::OnInitDialog( )
  68. {
  69. // set the initial strings
  70. m_szKeyName.LoadString( IDS_CREATE_KEY_NEW_NAME );
  71. m_szOrganization.LoadString( IDS_CREATE_KEY_YOUR_COMPANY );
  72. m_szUnit.LoadString( IDS_CREATE_KEY_YOUR_UNIT );
  73. m_szNetAddress.LoadString( IDS_CREATE_KEY_YOUR_ADDRESS );
  74. m_szCountry.LoadString( IDS_LOCALIZED_DEFAULT_COUNTRY_CODE );
  75. m_szState.LoadString( IDS_CREATE_KEY_YOUR_STATE );
  76. m_szLocality.LoadString( IDS_CREATE_KEY_YOUR_LOCALITY );
  77. // call the base oninit
  78. CDialog::OnInitDialog();
  79. // to comply with the munitions export laws, we need to limit the max bits available
  80. m_nMaxBits = 1024;
  81. // LOOK HERE KIM
  82. m_nMaxBits = SslGetMaximumKeySize(NULL);
  83. // set the default bit size
  84. m_nBits = m_nMaxBits;
  85. m_comboBits.SetCurSel( 2 );
  86. // if necessary, remove items from the bits combo box
  87. if ( m_nMaxBits < 1024 )
  88. {
  89. m_comboBits.DeleteString(2);
  90. m_comboBits.SetCurSel( 1 );
  91. }
  92. if ( m_nMaxBits < 768 )
  93. {
  94. m_comboBits.DeleteString(1);
  95. m_comboBits.SetCurSel( 0 );
  96. }
  97. // any other defaults
  98. m_fKeyNameChangedFile = FALSE;
  99. m_fSpecifiedFile = FALSE;
  100. OnChangeNewKeyName();
  101. // we start with no password, so diable the ok window
  102. m_btnOK.EnableWindow( FALSE );
  103. // return 0 to say we set the default item
  104. // return 1 to just select the default default item
  105. return 1;
  106. }
  107. //----------------------------------------------------------------
  108. void CCreateKeyDlg::OnChangeNewKeyName()
  109. {
  110. // if the user has not specifically chosen a file, update the
  111. // path of the request file to reflect the new file name
  112. if ( !m_fSpecifiedFile )
  113. {
  114. UpdateData( TRUE );
  115. m_szCertificateFile = _T("C:\\");
  116. m_szCertificateFile += m_szKeyName;
  117. m_szCertificateFile += _T(".req");
  118. UpdateData( FALSE );
  119. }
  120. }
  121. //----------------------------------------------------------------
  122. void CCreateKeyDlg::OnChangeNewKeyRequestFile()
  123. {
  124. if ( m_fKeyNameChangedFile )
  125. {
  126. // the change is because of a key name change. No big deal.
  127. // reset the flag and return
  128. m_fKeyNameChangedFile = FALSE;
  129. return;
  130. }
  131. // the user has been typing in the file box, or chose something in
  132. // the dialog. Either way, set the m_fSpecifiedFile flag so that
  133. // we stop changing the path with the key name is changed
  134. m_fSpecifiedFile = TRUE;
  135. }
  136. //----------------------------------------------------------------
  137. void CCreateKeyDlg::OnNewKeyBrowse()
  138. {
  139. CFileDialog cfdlg(FALSE, _T("*.req"), m_szCertificateFile);
  140. CString szFilter;
  141. WORD i = 0;
  142. LPSTR lpszBuffer;
  143. // prepare the filter string
  144. szFilter.LoadString( IDS_CERTIFICATE_FILTER );
  145. // replace the "!" characters with nulls
  146. lpszBuffer = szFilter.GetBuffer(MAX_PATH+1);
  147. while( lpszBuffer[i] )
  148. {
  149. if ( lpszBuffer[i] == _T('!') )
  150. lpszBuffer[i] = _T('\0'); // yes, set \0 on purpose
  151. i++;
  152. }
  153. // prep the dialog
  154. cfdlg.m_ofn.lpstrFilter = lpszBuffer;
  155. // run the dialog
  156. if ( cfdlg.DoModal() == IDOK )
  157. {
  158. // get the current data out of the dialog
  159. UpdateData( TRUE );
  160. // get the path for the file from the dialog
  161. m_szCertificateFile = cfdlg.GetPathName();
  162. UpdateData( FALSE );
  163. // the user has been typing in the file box, or chose something in
  164. // the dialog. Either way, set the m_fSpecifiedFile flag so that
  165. // we stop changing the path with the key name is changed
  166. m_fSpecifiedFile = TRUE;
  167. }
  168. // release the buffer in the filter string
  169. szFilter.ReleaseBuffer(60);
  170. }
  171. //----------------------------------------------------------------
  172. void CCreateKeyDlg::OnOK()
  173. {
  174. HANDLE hFile;
  175. // get the data out of the dialog
  176. UpdateData( TRUE );
  177. // if the file already exists, ask the user if they want to overwrite it
  178. // also, make sure that it is a valid pathname
  179. hFile = CreateFile( m_szCertificateFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  180. // if we opened the file, we have an error
  181. if ( hFile != INVALID_HANDLE_VALUE )
  182. {
  183. // well, first we close the handle, we were only checking if the file was there after all
  184. CloseHandle( hFile );
  185. // ask the user if they really want to overwrite the file
  186. try
  187. {
  188. // get the second part of the message from the resources
  189. CString szMessageNote;
  190. szMessageNote.LoadString( IDS_CERT_FILE_EXISTS );
  191. // next, build the string we will use in the message box
  192. CString szMessage;
  193. szMessage = m_szCertificateFile;
  194. szMessage += szMessageNote;
  195. // put up the message box, if the user choose no, they do not want to overwrite the
  196. // file. Then we should exit now
  197. if ( AfxMessageBox(szMessage, MB_ICONQUESTION|MB_YESNO) == IDNO )
  198. return;
  199. }
  200. catch( CException e )
  201. {
  202. return;
  203. }
  204. }
  205. // first, we need to confirm that the password is alright
  206. // the user must re-enter the password to confirm it
  207. CConfirmPassDlg dlgconfirm;
  208. if ( dlgconfirm.DoModal() != IDOK )
  209. return;
  210. // confirm the password
  211. if ( dlgconfirm.m_szPassword != m_szPassword )
  212. {
  213. AfxMessageBox( IDS_INCORRECT_PASSWORD );
  214. return;
  215. }
  216. // set the default bit size
  217. switch( m_comboBits.GetCurSel() )
  218. {
  219. case 0: // bits == 512
  220. m_nBits = 512;
  221. break;
  222. case 1: // bits == 768
  223. m_nBits = 768;
  224. break;
  225. case 2: // bits == 1024
  226. m_nBits = 1024;
  227. break;
  228. };
  229. // call the inherited OnOK
  230. CDialog::OnOK();
  231. }
  232. //----------------------------------------------------------------
  233. void CCreateKeyDlg::OnChangeNewKeyPassword()
  234. {
  235. // if there is no password, disable the ok button.
  236. // otherwise, enable it
  237. UpdateData( TRUE );
  238. m_btnOK.EnableWindow( !m_szPassword.IsEmpty() );
  239. }
  240. //----------------------------------------------------------------
  241. BOOL CCreateKeyDlg::PreTranslateMessage(MSG* pMsg)
  242. {
  243. // filter commas out of all edit fields except the password field.
  244. // commas would interfere with the distinguishing name information
  245. if ( (pMsg->message == WM_CHAR) &&
  246. (pMsg->hwnd != m_ceditPassword) &&
  247. ((TCHAR)pMsg->wParam == _T(',')) )
  248. {
  249. MessageBeep(0);
  250. return TRUE;
  251. }
  252. // translate normally
  253. return CDialog::PreTranslateMessage(pMsg);
  254. }