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.

251 lines
7.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: pvkdlg.cpp
  8. //
  9. // Contents: Private Key Dialog Box APIs.
  10. //
  11. // Functions: PvkDlgGetKeyPassword
  12. //
  13. // History: 12-May-96 philh created
  14. //
  15. //--------------------------------------------------------------------------
  16. #include "global.hxx"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <memory.h>
  22. #include <time.h>
  23. #include "pvk.h"
  24. // ENTER_PASSWORD:
  25. // IDC_PASSWORD0 - Password
  26. // CREATE_PASSWORD:
  27. // IDC_PASSWORD0 - Password
  28. // IDC_PASSWORD1 - Confirm Password
  29. typedef struct _KEY_PASSWORD_PARAM {
  30. PASSWORD_TYPE PasswordType;
  31. LPWSTR pwszKey; // IDC_KEY
  32. LPSTR *ppszPassword;
  33. } KEY_PASSWORD_PARAM, *PKEY_PASSWORD_PARAM;
  34. // Where to get the dialog resources from
  35. // Forward reference to local functions
  36. static int GetPassword(
  37. IN HWND hwndDlg,
  38. IN PASSWORD_TYPE PasswordType,
  39. OUT LPSTR *ppszPassword
  40. );
  41. static INT_PTR CALLBACK KeyPasswordDlgProc(
  42. IN HWND hwndDlg,
  43. IN UINT uMsg,
  44. IN WPARAM wParam,
  45. IN LPARAM lParam
  46. );
  47. //+-------------------------------------------------------------------------
  48. // Enter or Create Private Key Password Dialog Box
  49. //--------------------------------------------------------------------------
  50. int PvkDlgGetKeyPassword(
  51. IN PASSWORD_TYPE PasswordType,
  52. IN HWND hwndOwner,
  53. IN LPCWSTR pwszKeyName,
  54. OUT BYTE **ppbPassword,
  55. OUT DWORD *pcbPassword
  56. )
  57. {
  58. int nResult;
  59. LPSTR pszPassword = NULL;
  60. KEY_PASSWORD_PARAM KeyPasswordParam = {
  61. PasswordType,
  62. (LPWSTR) pwszKeyName,
  63. &pszPassword
  64. };
  65. LPCSTR pszTemplate = PasswordType == ENTER_PASSWORD ?
  66. MAKEINTRESOURCE(IDD_ENTERKEYPASSWORD) :
  67. MAKEINTRESOURCE(IDD_CREATEKEYPASSWORD);
  68. nResult = (BOOL)DialogBoxParam(
  69. GetInstanceHandle(),
  70. pszTemplate,
  71. hwndOwner,
  72. KeyPasswordDlgProc,
  73. (LPARAM) &KeyPasswordParam
  74. );
  75. *ppbPassword = (BYTE *) pszPassword;
  76. if (pszPassword)
  77. *pcbPassword = strlen(pszPassword);
  78. else
  79. *pcbPassword = 0;
  80. return nResult;
  81. }
  82. //+-------------------------------------------------------------------------
  83. // Allocate and get the password(s) from the dialog box
  84. //
  85. // For no password input, returns NULL
  86. // pointer for the password. Otherwise, the password is PvkAlloc'ed.
  87. //--------------------------------------------------------------------------
  88. static int GetPassword(
  89. IN HWND hwndDlg,
  90. IN PASSWORD_TYPE PasswordType,
  91. OUT LPSTR *ppszPassword
  92. )
  93. {
  94. LPSTR rgpszPassword[2] = {NULL, NULL};
  95. WCHAR wszNoPassword[128];
  96. WCHAR wszPasswordNoMatch[128];
  97. *ppszPassword = NULL;
  98. // Get the entered password(s)
  99. assert(PasswordType < 2);
  100. int i;
  101. for (i = 0; i <= PasswordType; i++) {
  102. LONG cchPassword;
  103. cchPassword = (LONG)SendDlgItemMessage(
  104. hwndDlg,
  105. IDC_PASSWORD0 + i,
  106. EM_LINELENGTH,
  107. (WPARAM) 0,
  108. (LPARAM) 0
  109. );
  110. if (cchPassword > 0) {
  111. rgpszPassword[i] = (LPSTR) PvkAlloc(cchPassword + 1);
  112. assert(rgpszPassword[i]);
  113. if (rgpszPassword[i])
  114. GetDlgItemText(
  115. hwndDlg,
  116. IDC_PASSWORD0 + i,
  117. rgpszPassword[i],
  118. cchPassword + 1
  119. );
  120. }
  121. }
  122. if (PasswordType == ENTER_PASSWORD) {
  123. *ppszPassword = rgpszPassword[0];
  124. return IDOK;
  125. }
  126. int nResult = IDOK;
  127. #define MSG_BOX_TITLE_LEN 128
  128. WCHAR wszMsgBoxTitle[MSG_BOX_TITLE_LEN];
  129. GetWindowTextU(hwndDlg, wszMsgBoxTitle, MSG_BOX_TITLE_LEN);
  130. if (rgpszPassword[0] == NULL && rgpszPassword[1] == NULL) {
  131. if(0 == LoadStringU(hInstance, IDS_NO_PASSWORD, wszNoPassword, 128))
  132. wcscpy(wszNoPassword, L"Without password protection ?");
  133. // Didn't enter a password
  134. nResult = MessageBoxU(
  135. hwndDlg,
  136. wszNoPassword,
  137. wszMsgBoxTitle,
  138. MB_YESNOCANCEL | MB_ICONQUESTION | MB_DEFBUTTON2
  139. );
  140. if (nResult == IDYES)
  141. nResult = IDOK;
  142. else if (nResult == IDNO)
  143. nResult = IDRETRY;
  144. } else if (rgpszPassword[0] == NULL || rgpszPassword[1] == NULL ||
  145. strcmp(rgpszPassword[0], rgpszPassword[1]) != 0) {
  146. if(0 == LoadStringU(hInstance, IDS_PASSWORD_NO_MATCH, wszPasswordNoMatch, 128))
  147. wcscpy(wszPasswordNoMatch, L"Confirm password doesn't match");
  148. // Confirmed password didn't match
  149. nResult = MessageBoxU(
  150. hwndDlg,
  151. wszPasswordNoMatch,
  152. wszMsgBoxTitle,
  153. MB_RETRYCANCEL | MB_ICONEXCLAMATION
  154. );
  155. if (nResult == IDRETRY) {
  156. SetDlgItemText(hwndDlg, IDC_PASSWORD0 + 0, "");
  157. SetDlgItemText(hwndDlg, IDC_PASSWORD0 + 1, "");
  158. }
  159. }
  160. if (nResult == IDOK)
  161. *ppszPassword = rgpszPassword[0];
  162. else if (rgpszPassword[0])
  163. PvkFree(rgpszPassword[0]);
  164. if (rgpszPassword[1])
  165. PvkFree(rgpszPassword[1]);
  166. if (nResult == IDRETRY)
  167. SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD0));
  168. return nResult;
  169. }
  170. //+-------------------------------------------------------------------------
  171. // Enter or Create Private Key Password DialogProc
  172. //--------------------------------------------------------------------------
  173. static INT_PTR CALLBACK KeyPasswordDlgProc(
  174. IN HWND hwndDlg,
  175. IN UINT uMsg,
  176. IN WPARAM wParam,
  177. IN LPARAM lParam
  178. )
  179. {
  180. switch (uMsg) {
  181. case WM_INITDIALOG:
  182. {
  183. PKEY_PASSWORD_PARAM pKeyPasswordParam =
  184. (PKEY_PASSWORD_PARAM) lParam;
  185. char sz[128];
  186. WideCharToMultiByte(CP_ACP, 0, pKeyPasswordParam->pwszKey, -1,
  187. (LPSTR) sz, 128, NULL, NULL);
  188. SetDlgItemText(hwndDlg, IDC_KEY, sz);
  189. SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR) pKeyPasswordParam);
  190. return TRUE;
  191. }
  192. case WM_COMMAND:
  193. int nResult = LOWORD(wParam);
  194. switch (nResult) {
  195. case IDOK:
  196. {
  197. PKEY_PASSWORD_PARAM pKeyPasswordParam =
  198. (PKEY_PASSWORD_PARAM) GetWindowLongPtr(hwndDlg, DWLP_USER);
  199. nResult = GetPassword(
  200. hwndDlg,
  201. pKeyPasswordParam->PasswordType,
  202. pKeyPasswordParam->ppszPassword
  203. );
  204. if (nResult != IDRETRY)
  205. EndDialog(hwndDlg, nResult);
  206. return TRUE;
  207. }
  208. break;
  209. case IDC_NONE:
  210. nResult = IDOK; // *ppszPassword == NULL
  211. // Fall through
  212. case IDCANCEL:
  213. EndDialog(hwndDlg, nResult);
  214. return TRUE;
  215. }
  216. }
  217. return FALSE;
  218. }