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.

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