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.

272 lines
7.8 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 <windows.h>
  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. static HINSTANCE hPvkInst;
  36. // Forward reference to local functions
  37. static int GetPassword(
  38. IN HWND hwndDlg,
  39. IN PASSWORD_TYPE PasswordType,
  40. OUT LPSTR *ppszPassword
  41. );
  42. static INT_PTR CALLBACK KeyPasswordDlgProc(
  43. IN HWND hwndDlg,
  44. IN UINT uMsg,
  45. IN WPARAM wParam,
  46. IN LPARAM lParam
  47. );
  48. extern
  49. BOOL
  50. WINAPI
  51. UnicodeDllMain(
  52. HMODULE hInst,
  53. ULONG ulReason,
  54. LPVOID lpReserved);
  55. //+-------------------------------------------------------------------------
  56. // Dll initialization
  57. //--------------------------------------------------------------------------
  58. BOOL WINAPI DllMain(
  59. HMODULE hInstDLL,
  60. DWORD fdwReason,
  61. LPVOID lpvReserved
  62. )
  63. {
  64. switch (fdwReason) {
  65. case DLL_PROCESS_ATTACH:
  66. hPvkInst = hInstDLL;
  67. break;
  68. case DLL_PROCESS_DETACH:
  69. break;
  70. }
  71. return UnicodeDllMain( hInstDLL, fdwReason, lpvReserved);
  72. }
  73. //+-------------------------------------------------------------------------
  74. // Enter or Create Private Key Password Dialog Box
  75. //--------------------------------------------------------------------------
  76. int PvkDlgGetKeyPassword(
  77. IN PASSWORD_TYPE PasswordType,
  78. IN HWND hwndOwner,
  79. IN LPCWSTR pwszKeyName,
  80. OUT BYTE **ppbPassword,
  81. OUT DWORD *pcbPassword
  82. )
  83. {
  84. int nResult;
  85. LPSTR pszPassword = NULL;
  86. KEY_PASSWORD_PARAM KeyPasswordParam = {
  87. PasswordType,
  88. (LPWSTR) pwszKeyName,
  89. &pszPassword
  90. };
  91. LPCSTR pszTemplate = PasswordType == ENTER_PASSWORD ?
  92. MAKEINTRESOURCE(IDD_ENTERKEYPASSWORD) :
  93. MAKEINTRESOURCE(IDD_CREATEKEYPASSWORD);
  94. nResult = (BOOL)DialogBoxParam(
  95. hPvkInst,
  96. pszTemplate,
  97. hwndOwner,
  98. KeyPasswordDlgProc,
  99. (LPARAM) &KeyPasswordParam
  100. );
  101. *ppbPassword = (BYTE *) pszPassword;
  102. if (pszPassword)
  103. *pcbPassword = strlen(pszPassword);
  104. else
  105. *pcbPassword = 0;
  106. return nResult;
  107. }
  108. //+-------------------------------------------------------------------------
  109. // Allocate and get the password(s) from the dialog box
  110. //
  111. // For no password input, returns NULL
  112. // pointer for the password. Otherwise, the password is PvkAlloc'ed.
  113. //--------------------------------------------------------------------------
  114. static int GetPassword(
  115. IN HWND hwndDlg,
  116. IN PASSWORD_TYPE PasswordType,
  117. OUT LPSTR *ppszPassword
  118. )
  119. {
  120. LPSTR rgpszPassword[2] = {NULL, NULL};
  121. *ppszPassword = NULL;
  122. // Get the entered password(s)
  123. assert(PasswordType < 2);
  124. int i;
  125. for (i = 0; i <= PasswordType; i++) {
  126. LONG cchPassword;
  127. cchPassword = (LONG)SendDlgItemMessage(
  128. hwndDlg,
  129. IDC_PASSWORD0 + i,
  130. EM_LINELENGTH,
  131. (WPARAM) 0,
  132. (LPARAM) 0
  133. );
  134. if (cchPassword > 0) {
  135. rgpszPassword[i] = (LPSTR) PvkAlloc(cchPassword + 1);
  136. assert(rgpszPassword[i]);
  137. if (rgpszPassword[i])
  138. GetDlgItemText(
  139. hwndDlg,
  140. IDC_PASSWORD0 + i,
  141. rgpszPassword[i],
  142. cchPassword + 1
  143. );
  144. }
  145. }
  146. if (PasswordType == ENTER_PASSWORD) {
  147. *ppszPassword = rgpszPassword[0];
  148. return IDOK;
  149. }
  150. int nResult = IDOK;
  151. #define MSG_BOX_TITLE_LEN 128
  152. char szMsgBoxTitle[MSG_BOX_TITLE_LEN];
  153. GetWindowText(hwndDlg, szMsgBoxTitle, MSG_BOX_TITLE_LEN);
  154. if (rgpszPassword[0] == NULL && rgpszPassword[1] == NULL) {
  155. // Didn't enter a password
  156. nResult = MessageBox(
  157. hwndDlg,
  158. "Without password protection ?",
  159. szMsgBoxTitle,
  160. MB_YESNOCANCEL | MB_ICONQUESTION | MB_DEFBUTTON2
  161. );
  162. if (nResult == IDYES)
  163. nResult = IDOK;
  164. else if (nResult == IDNO)
  165. nResult = IDRETRY;
  166. } else if (rgpszPassword[0] == NULL || rgpszPassword[1] == NULL ||
  167. strcmp(rgpszPassword[0], rgpszPassword[1]) != 0) {
  168. // Confirmed password didn't match
  169. nResult = MessageBox(
  170. hwndDlg,
  171. "Confirm password doesn't match",
  172. szMsgBoxTitle,
  173. MB_RETRYCANCEL | MB_ICONEXCLAMATION
  174. );
  175. if (nResult == IDRETRY) {
  176. SetDlgItemText(hwndDlg, IDC_PASSWORD0 + 0, "");
  177. SetDlgItemText(hwndDlg, IDC_PASSWORD0 + 1, "");
  178. }
  179. }
  180. if (nResult == IDOK)
  181. *ppszPassword = rgpszPassword[0];
  182. else if (rgpszPassword[0])
  183. PvkFree(rgpszPassword[0]);
  184. if (rgpszPassword[1])
  185. PvkFree(rgpszPassword[1]);
  186. if (nResult == IDRETRY)
  187. SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD0));
  188. return nResult;
  189. }
  190. //+-------------------------------------------------------------------------
  191. // Enter or Create Private Key Password DialogProc
  192. //--------------------------------------------------------------------------
  193. static INT_PTR CALLBACK KeyPasswordDlgProc(
  194. IN HWND hwndDlg,
  195. IN UINT uMsg,
  196. IN WPARAM wParam,
  197. IN LPARAM lParam
  198. )
  199. {
  200. switch (uMsg) {
  201. case WM_INITDIALOG:
  202. {
  203. PKEY_PASSWORD_PARAM pKeyPasswordParam =
  204. (PKEY_PASSWORD_PARAM) lParam;
  205. char sz[128];
  206. WideCharToMultiByte(CP_ACP, 0, pKeyPasswordParam->pwszKey, -1,
  207. (LPSTR) sz, 128, NULL, NULL);
  208. SetDlgItemText(hwndDlg, IDC_KEY, sz);
  209. SetWindowLongPtr(hwndDlg, DWLP_USER, (ULONG_PTR)pKeyPasswordParam);
  210. return TRUE;
  211. }
  212. case WM_COMMAND:
  213. int nResult = LOWORD(wParam);
  214. switch (nResult) {
  215. case IDOK:
  216. {
  217. PKEY_PASSWORD_PARAM pKeyPasswordParam =
  218. (PKEY_PASSWORD_PARAM) GetWindowLongPtr(hwndDlg, DWLP_USER);
  219. nResult = GetPassword(
  220. hwndDlg,
  221. pKeyPasswordParam->PasswordType,
  222. pKeyPasswordParam->ppszPassword
  223. );
  224. if (nResult != IDRETRY)
  225. EndDialog(hwndDlg, nResult);
  226. return TRUE;
  227. }
  228. break;
  229. case IDC_NONE:
  230. nResult = IDOK; // *ppszPassword == NULL
  231. // Fall through
  232. case IDCANCEL:
  233. EndDialog(hwndDlg, nResult);
  234. return TRUE;
  235. }
  236. }
  237. return FALSE;
  238. }