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.

344 lines
12 KiB

  1. #include "precomp.h"
  2. #include <wincrypt.h>
  3. extern TCHAR g_szCustIns[];
  4. extern TCHAR g_szTempSign[];
  5. extern TCHAR g_szWizRoot[];
  6. extern PROPSHEETPAGE g_psp[];
  7. extern int g_iCurPage;
  8. static void initCerts(HWND hwndCtl);
  9. static void addCompanyCertToReg(HWND hDlg);
  10. INT_PTR CALLBACK ISPAddRootCertDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  11. {
  12. TCHAR szCertFile[MAX_PATH];
  13. TCHAR szWorkDir[MAX_PATH];
  14. TCHAR szTemp[MAX_PATH];
  15. switch (uMsg)
  16. {
  17. case WM_INITDIALOG:
  18. EnableDBCSChars(hDlg, IDE_ISPROOTCERT);
  19. Edit_LimitText(GetDlgItem(hDlg, IDE_ISPROOTCERT), countof(szCertFile) - 1);
  20. break;
  21. case WM_NOTIFY:
  22. switch (((LPNMHDR) lParam)->code)
  23. {
  24. case PSN_SETACTIVE:
  25. // import INS clean-up -- delete cert file from the temp location
  26. if (InsGetString(IS_ISPSECURITY, IK_ROOTCERT, szCertFile, countof(szCertFile), g_szCustIns))
  27. DeleteFileInDir(szCertFile, g_szTempSign);
  28. SetBannerText(hDlg);
  29. SetDlgItemText(hDlg, IDE_ISPROOTCERT, szCertFile);
  30. CheckBatchAdvance(hDlg);
  31. break;
  32. case PSN_WIZBACK:
  33. case PSN_WIZNEXT:
  34. if (!CheckField(hDlg, IDE_ISPROOTCERT, FC_FILE | FC_EXISTS))
  35. {
  36. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  37. return TRUE;
  38. }
  39. g_cmCabMappings.GetFeatureDir(FEATURE_BRAND, szWorkDir);
  40. // delete the old cert file
  41. if (InsGetString(IS_ISPSECURITY, IK_ROOTCERT, szTemp, countof(szTemp), g_szCustIns))
  42. DeleteFileInDir(szTemp, szWorkDir);
  43. // copy the new cert file
  44. GetDlgItemText(hDlg, IDE_ISPROOTCERT, szCertFile, countof(szCertFile));
  45. if (*szCertFile)
  46. CopyFileToDir(szCertFile, szWorkDir);
  47. InsWriteString(IS_ISPSECURITY, IK_ROOTCERT, szCertFile, g_szCustIns);
  48. g_iCurPage = PPAGE_ADDROOT;
  49. EnablePages();
  50. (((NMHDR FAR *) lParam)->code == PSN_WIZNEXT) ? PageNext(hDlg) : PagePrev(hDlg);
  51. break;
  52. case PSN_HELP:
  53. IeakPageHelp(hDlg, g_psp[g_iCurPage].pszTemplate);
  54. break;
  55. case PSN_QUERYCANCEL:
  56. QueryCancel(hDlg);
  57. break;
  58. default:
  59. return FALSE;
  60. }
  61. break;
  62. case WM_COMMAND:
  63. if (GET_WM_COMMAND_CMD(wParam, lParam) != BN_CLICKED)
  64. return FALSE;
  65. switch (GET_WM_COMMAND_ID(wParam, lParam))
  66. {
  67. case IDC_BROWSEROOTCERT:
  68. GetDlgItemText(hDlg, IDE_ISPROOTCERT, szCertFile, countof(szCertFile));
  69. if (BrowseForFile(hDlg, szCertFile, countof(szCertFile), GFN_CERTIFICATE))
  70. SetDlgItemText(hDlg, IDE_ISPROOTCERT, szCertFile);
  71. break;
  72. }
  73. break;
  74. case WM_HELP:
  75. IeakPageHelp(hDlg, g_psp[g_iCurPage].pszTemplate);
  76. break;
  77. case IDM_BATCHADVANCE:
  78. DoBatchAdvance(hDlg);
  79. break;
  80. default:
  81. return FALSE;
  82. }
  83. return TRUE;
  84. }
  85. INT_PTR CALLBACK CabSignDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  86. {
  87. TCHAR szSPCPath[MAX_PATH];
  88. TCHAR szPVKPath[MAX_PATH];
  89. TCHAR szDesc[MAX_PATH];
  90. TCHAR szInfoUrl[INTERNET_MAX_URL_LENGTH];
  91. TCHAR szTimeUrl[INTERNET_MAX_URL_LENGTH];
  92. switch (uMsg)
  93. {
  94. case WM_INITDIALOG:
  95. EnableDBCSChars(hDlg, IDC_CSCOMP);
  96. initCerts(GetDlgItem(hDlg, IDC_CSCOMP));
  97. EnableDBCSChars(hDlg, IDE_CSSPC);
  98. EnableDBCSChars(hDlg, IDE_CSPVK);
  99. EnableDBCSChars(hDlg, IDE_CSDESC);
  100. EnableDBCSChars(hDlg, IDE_CSURL);
  101. EnableDBCSChars(hDlg, IDE_CSTIME);
  102. Edit_LimitText(GetDlgItem(hDlg, IDE_CSSPC), countof(szSPCPath) - 1);
  103. Edit_LimitText(GetDlgItem(hDlg, IDE_CSPVK), countof(szPVKPath) - 1);
  104. Edit_LimitText(GetDlgItem(hDlg, IDE_CSDESC), countof(szDesc) - 1);
  105. Edit_LimitText(GetDlgItem(hDlg, IDE_CSURL), countof(szInfoUrl) - 1);
  106. Edit_LimitText(GetDlgItem(hDlg, IDE_CSTIME), countof(szTimeUrl) - 1);
  107. break;
  108. case WM_NOTIFY:
  109. switch (((LPNMHDR) lParam)->code)
  110. {
  111. case PSN_SETACTIVE:
  112. SetBannerText(hDlg);
  113. InsGetString(IS_CABSIGN, IK_SPC, szSPCPath, countof(szSPCPath), g_szCustIns);
  114. InsGetString(IS_CABSIGN, IK_PVK, szPVKPath, countof(szPVKPath), g_szCustIns);
  115. InsGetString(IS_CABSIGN, IK_NAME, szDesc, countof(szDesc), g_szCustIns);
  116. InsGetString(IS_CABSIGN, IK_CSURL, szInfoUrl, countof(szInfoUrl), g_szCustIns);
  117. InsGetString(IS_CABSIGN, IK_CSTIME, szTimeUrl, countof(szTimeUrl), g_szCustIns);
  118. SetDlgItemText(hDlg, IDE_CSSPC, szSPCPath);
  119. SetDlgItemText(hDlg, IDE_CSPVK, szPVKPath);
  120. SetDlgItemText(hDlg, IDE_CSDESC, szDesc);
  121. SetDlgItemText(hDlg, IDE_CSURL, szInfoUrl);
  122. SetDlgItemText(hDlg, IDE_CSTIME, szTimeUrl);
  123. CheckBatchAdvance(hDlg);
  124. break;
  125. case PSN_WIZBACK:
  126. case PSN_WIZNEXT:
  127. GetDlgItemText(hDlg, IDE_CSSPC, szSPCPath, countof(szSPCPath));
  128. GetDlgItemText(hDlg, IDE_CSPVK, szPVKPath, countof(szPVKPath));
  129. GetDlgItemText(hDlg, IDE_CSDESC, szDesc, countof(szDesc));
  130. GetDlgItemText(hDlg, IDE_CSURL, szInfoUrl, countof(szInfoUrl));
  131. GetDlgItemText(hDlg, IDE_CSTIME, szTimeUrl, countof(szTimeUrl));
  132. if (*szSPCPath || *szPVKPath || *szDesc || *szInfoUrl || *szTimeUrl)
  133. {
  134. TCHAR szCompanyName[MAX_PATH];
  135. HWND hwndCtl;
  136. int iSel;
  137. if (!CheckField(hDlg, IDE_CSSPC, FC_NONNULL | FC_FILE | FC_EXISTS) ||
  138. !CheckField(hDlg, IDE_CSPVK, FC_NONNULL | FC_FILE | FC_EXISTS) ||
  139. !CheckField(hDlg, IDE_CSDESC, FC_NONNULL) ||
  140. !CheckField(hDlg, IDE_CSURL, FC_URL) ||
  141. !CheckField(hDlg, IDE_CSTIME, FC_URL))
  142. {
  143. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  144. return TRUE;
  145. }
  146. *szCompanyName = TEXT('\0');
  147. hwndCtl = GetDlgItem(hDlg, IDC_CSCOMP);
  148. iSel = ComboBox_GetCurSel(hwndCtl);
  149. if (iSel != CB_ERR)
  150. ComboBox_GetLBText(hwndCtl, iSel, szCompanyName);
  151. InsWriteString(IS_CABSIGN, IK_COMPANYNAME, szCompanyName, g_szCustIns);
  152. }
  153. InsWriteString(IS_CABSIGN, IK_SPC, szSPCPath, g_szCustIns);
  154. InsWriteString(IS_CABSIGN, IK_PVK, szPVKPath, g_szCustIns);
  155. InsWriteString(IS_CABSIGN, IK_NAME, szDesc, g_szCustIns);
  156. InsWriteString(IS_CABSIGN, IK_CSURL, szInfoUrl, g_szCustIns);
  157. InsWriteString(IS_CABSIGN, IK_CSTIME, szTimeUrl, g_szCustIns);
  158. g_iCurPage = PPAGE_CABSIGN;
  159. EnablePages();
  160. (((NMHDR FAR *) lParam)->code == PSN_WIZNEXT) ? PageNext(hDlg) : PagePrev(hDlg);
  161. break;
  162. case PSN_HELP:
  163. IeakPageHelp(hDlg, g_psp[g_iCurPage].pszTemplate);
  164. break;
  165. case PSN_QUERYCANCEL:
  166. QueryCancel(hDlg);
  167. break;
  168. default:
  169. return FALSE;
  170. }
  171. break;
  172. case WM_COMMAND:
  173. if (GET_WM_COMMAND_CMD(wParam, lParam) != BN_CLICKED)
  174. return FALSE;
  175. switch (GET_WM_COMMAND_ID(wParam, lParam))
  176. {
  177. case IDC_CSADD:
  178. addCompanyCertToReg(hDlg);
  179. break;
  180. case IDC_BROWSECSSPC:
  181. GetDlgItemText(hDlg, IDE_CSSPC, szSPCPath, countof(szSPCPath));
  182. if (BrowseForFile(hDlg, szSPCPath, countof(szSPCPath), GFN_SPC))
  183. SetDlgItemText(hDlg, IDE_CSSPC, szSPCPath);
  184. break;
  185. case IDC_BROWSECSPVK:
  186. GetDlgItemText(hDlg, IDE_CSPVK, szPVKPath, countof(szPVKPath));
  187. if (BrowseForFile(hDlg, szPVKPath, countof(szPVKPath), GFN_PVK))
  188. SetDlgItemText(hDlg, IDE_CSPVK, szPVKPath);
  189. break;
  190. }
  191. break;
  192. case WM_HELP:
  193. IeakPageHelp(hDlg, g_psp[g_iCurPage].pszTemplate);
  194. break;
  195. case IDM_BATCHADVANCE:
  196. DoBatchAdvance(hDlg);
  197. break;
  198. default:
  199. return FALSE;
  200. }
  201. return TRUE;
  202. }
  203. static void initCerts(HWND hwndCtl)
  204. {
  205. HKEY hKey;
  206. TCHAR szCompanyName[MAX_PATH];
  207. if (SHOpenKeyHKCU(RK_TRUSTKEY, KEY_READ, &hKey) == ERROR_SUCCESS)
  208. {
  209. TCHAR szKey[MAX_PATH];
  210. TCHAR szValue[MAX_PATH];
  211. DWORD dwEntry;
  212. DWORD cchKey;
  213. DWORD cbValue;
  214. dwEntry = 0;
  215. cchKey = countof(szKey);
  216. cbValue = sizeof(szValue);
  217. while (RegEnumValue(hKey, dwEntry, szKey, &cchKey, NULL, NULL, (LPBYTE) szValue, &cbValue) == ERROR_SUCCESS)
  218. {
  219. if (ComboBox_FindStringExact(hwndCtl, -1, szValue) == CB_ERR) // string not present
  220. ComboBox_AddString(hwndCtl, szValue); // so, add it
  221. dwEntry++;
  222. cchKey = countof(szKey);
  223. cbValue = sizeof(szValue);
  224. }
  225. RegCloseKey(hKey);
  226. }
  227. InsGetString(IS_CABSIGN, IK_COMPANYNAME, szCompanyName, countof(szCompanyName), g_szCustIns);
  228. if (*szCompanyName == TEXT('\0'))
  229. StrCpy(szCompanyName, TEXT("MICROSOFT"));
  230. ComboBox_SelectString(hwndCtl, -1, szCompanyName);
  231. }
  232. static void addCompanyCertToReg(HWND hDlg)
  233. {
  234. if (CheckField(hDlg, IDE_CSSPC, FC_NONNULL | FC_FILE | FC_EXISTS) &&
  235. CheckField(hDlg, IDE_CSPVK, FC_NONNULL | FC_FILE | FC_EXISTS))
  236. {
  237. TCHAR szTempDir[MAX_PATH];
  238. TCHAR szTempFile[MAX_PATH];
  239. TCHAR szCabFile[MAX_PATH];
  240. TCHAR szResult[MAX_PATH + 16] = TEXT("");
  241. BOOL fSuccess = FALSE;
  242. PathCombine(szTempDir, g_szTempSign, TEXT("SIGN"));
  243. PathCreatePath(szTempDir);
  244. // copy signing files to temp dir
  245. PathCombine(szTempFile, g_szWizRoot, TEXT("tools\\signcode.exe"));
  246. CopyFileToDir(szTempFile, szTempDir);
  247. PathRemoveFileSpec(szTempFile);
  248. PathAppend(szTempFile, TEXT("signer.dll"));
  249. CopyFileToDir(szTempFile, szTempDir);
  250. GetDlgItemText(hDlg, IDE_CSSPC, szTempFile, countof(szTempFile));
  251. InsWriteString(IS_CABSIGN, IK_SPC, szTempFile, g_szCustIns);
  252. GetDlgItemText(hDlg, IDE_CSPVK, szTempFile, countof(szTempFile));
  253. InsWriteString(IS_CABSIGN, IK_PVK, szTempFile, g_szCustIns);
  254. InsFlushChanges(g_szCustIns);
  255. PathCombine(szTempFile, szTempDir, TEXT("temp.exe"));
  256. // copy cabarc.exe from tools dir to sign
  257. PathCombine(szCabFile, g_szWizRoot, TEXT("tools\\cabarc.exe"));
  258. CopyFile(szCabFile, szTempFile, FALSE);
  259. SignFile(szTempFile, NULL, g_szCustIns, szResult, NULL, TRUE);
  260. if (ISNULL(szResult) &&
  261. (CheckTrustExWrap(NULL, szTempFile, hDlg, FALSE, NULL) == NOERROR))
  262. fSuccess = TRUE;
  263. if (fSuccess)
  264. {
  265. initCerts(GetDlgItem(hDlg, IDC_CSCOMP));
  266. ErrorMessageBox(hDlg, IDS_SUCCESS_CSADDCERT);
  267. }
  268. else
  269. ErrorMessageBox(hDlg, IDS_ERROR_CSADDCERT);
  270. PathRemovePath(szTempDir);
  271. }
  272. }