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.

302 lines
10 KiB

  1. //
  2. // SECCERTS.CPP
  3. //
  4. #include "precomp.h"
  5. #include <wintrust.h>
  6. #include <wintrustp.h>
  7. #include <cryptui.h>
  8. static BOOL importSiteCertHelper(LPCTSTR pcszInsFile, LPCTSTR pcszSCWorkDir, LPCTSTR pcszSCInf, BOOL fImportSC);
  9. static BOOL importAuthCodeHelper(LPCTSTR pcszInsFile, LPCTSTR pcszAuthWorkDir, LPCTSTR pcszAuthInf, BOOL fImportAuth);
  10. BOOL WINAPI ImportSiteCertA(LPCSTR pcszInsFile, LPCSTR pcszSCWorkDir, LPCSTR pcszSCInf, BOOL fImportSC)
  11. {
  12. USES_CONVERSION;
  13. return importSiteCertHelper(A2CT(pcszInsFile), A2CT(pcszSCWorkDir), A2CT(pcszSCInf), fImportSC);
  14. }
  15. BOOL WINAPI ImportSiteCertW(LPCWSTR pcwszInsFile, LPCWSTR pcwszSCWorkDir, LPCWSTR pcwszSCInf, BOOL fImportSC)
  16. {
  17. USES_CONVERSION;
  18. return importSiteCertHelper(W2CT(pcwszInsFile), W2CT(pcwszSCWorkDir), W2CT(pcwszSCInf), fImportSC);
  19. }
  20. BOOL WINAPI ModifySiteCert(HWND hDlg)
  21. {
  22. typedef DWORD (WINAPI * CRYPTUIDLGCERTMGR)(PCCRYPTUI_CERT_MGR_STRUCT);
  23. BOOL fRet;
  24. HINSTANCE hCryptUI;
  25. CRYPTUIDLGCERTMGR lpfnCryptUIDlgCertMgr;
  26. CRYPTUI_CERT_MGR_STRUCT ccm;
  27. fRet = FALSE;
  28. hCryptUI = NULL;
  29. if ((hCryptUI = LoadLibrary(TEXT("cryptui.dll"))) == NULL)
  30. goto Exit;
  31. if ((lpfnCryptUIDlgCertMgr = (CRYPTUIDLGCERTMGR) GetProcAddress(hCryptUI, "CryptUIDlgCertMgr")) == NULL)
  32. goto Exit;
  33. fRet = TRUE;
  34. // call into cryptui.dll to modify the certs
  35. ZeroMemory(&ccm, sizeof(ccm));
  36. ccm.dwSize = sizeof(ccm);
  37. ccm.hwndParent = hDlg;
  38. lpfnCryptUIDlgCertMgr(&ccm);
  39. Exit:
  40. if (hCryptUI != NULL)
  41. FreeLibrary(hCryptUI);
  42. return fRet;
  43. }
  44. BOOL WINAPI ImportAuthCodeA(LPCSTR pcszInsFile, LPCSTR pcszAuthWorkDir, LPCSTR pcszAuthInf, BOOL fImportAuth)
  45. {
  46. USES_CONVERSION;
  47. return importAuthCodeHelper(A2CT(pcszInsFile), A2CT(pcszAuthWorkDir), A2CT(pcszAuthInf), fImportAuth);
  48. }
  49. BOOL WINAPI ImportAuthCodeW(LPCWSTR pcwszInsFile, LPCWSTR pcwszAuthWorkDir, LPCWSTR pcwszAuthInf, BOOL fImportAuth)
  50. {
  51. USES_CONVERSION;
  52. return importAuthCodeHelper(W2CT(pcwszInsFile), W2CT(pcwszAuthWorkDir), W2CT(pcwszAuthInf), fImportAuth);
  53. }
  54. BOOL WINAPI ModifyAuthCode(HWND hDlg)
  55. {
  56. HINSTANCE hWinTrust = NULL;
  57. HINSTANCE hSoftPub = NULL;
  58. BOOL fRet = FALSE;
  59. //Thanks to a change from the crypto team, this needs to behave differently on whistler
  60. if (IsOS(OS_WHISTLERORGREATER))
  61. {
  62. typedef BOOL (WINAPI * OPENPERSONALTRUSTDBDIALOGEX)(HWND,DWORD,PVOID);
  63. OPENPERSONALTRUSTDBDIALOGEX pfnOpenPersonalTrustDBDialogEx;
  64. hWinTrust = NULL;
  65. hSoftPub = NULL;
  66. if ((hWinTrust = LoadLibrary(TEXT("wintrust.dll"))) == NULL)
  67. goto Exit;
  68. if ((pfnOpenPersonalTrustDBDialogEx = (OPENPERSONALTRUSTDBDIALOGEX) GetProcAddress(hWinTrust, "OpenPersonalTrustDBDialogEx")) == NULL)
  69. goto Exit;
  70. fRet = TRUE;
  71. DWORD dwFlags = WT_TRUSTDBDIALOG_ONLY_PUB_TAB_FLAG|WT_TRUSTDBDIALOG_WRITE_LEGACY_REG_FLAG|WT_TRUSTDBDIALOG_WRITE_IEAK_STORE_FLAG;
  72. // call into wintrust.dll/softpub.dll to modify the certs
  73. pfnOpenPersonalTrustDBDialogEx(hDlg,dwFlags,NULL);
  74. }
  75. else
  76. {
  77. typedef BOOL (WINAPI * OPENPERSONALTRUSTDBDIALOG)(HWND);
  78. HINSTANCE hWinTrust, hSoftPub;
  79. OPENPERSONALTRUSTDBDIALOG pfnOpenPersonalTrustDBDialog;
  80. hWinTrust = NULL;
  81. hSoftPub = NULL;
  82. if ((hWinTrust = LoadLibrary(TEXT("wintrust.dll"))) == NULL)
  83. goto Exit;
  84. if ((pfnOpenPersonalTrustDBDialog = (OPENPERSONALTRUSTDBDIALOG) GetProcAddress(hWinTrust, "OpenPersonalTrustDBDialog")) == NULL)
  85. {
  86. FreeLibrary(hWinTrust);
  87. hWinTrust = NULL;
  88. // We can also find the same function on NT machines (and possibly future Win9x's)
  89. // in SOFTPUB.DLL, so make another check there too
  90. if ((hSoftPub = LoadLibrary(TEXT("softpub.dll"))) == NULL)
  91. goto Exit;
  92. if ((pfnOpenPersonalTrustDBDialog = (OPENPERSONALTRUSTDBDIALOG) GetProcAddress(hSoftPub, "OpenPersonalTrustDBDialog")) == NULL)
  93. goto Exit;
  94. }
  95. fRet = TRUE;
  96. // call into wintrust.dll/softpub.dll to modify the certs
  97. pfnOpenPersonalTrustDBDialog(hDlg);
  98. }
  99. Exit:
  100. if (hWinTrust != NULL)
  101. FreeLibrary(hWinTrust);
  102. if (hSoftPub != NULL)
  103. FreeLibrary(hSoftPub);
  104. return fRet;
  105. }
  106. static BOOL importSiteCertHelper(LPCTSTR pcszInsFile, LPCTSTR pcszSCWorkDir, LPCTSTR pcszSCInf, BOOL fImportSC)
  107. {
  108. BOOL bRet = FALSE;
  109. TCHAR szFullInfName[MAX_PATH];
  110. HANDLE hInf;
  111. if (pcszInsFile == NULL || pcszSCInf == NULL)
  112. return FALSE;
  113. // Before processing anything, first clear out the entries in the INS file and delete work dirs
  114. // clear out the entries in the INS file that correspond to importing security certificates
  115. InsDeleteKey(SECURITY_IMPORTS, TEXT("ImportSiteCert"), pcszInsFile);
  116. InsDeleteKey(IS_EXTREGINF, TEXT("SiteCert"), pcszInsFile);
  117. InsDeleteKey(IS_EXTREGINF_HKLM, TEXT("SiteCert"), pcszInsFile);
  118. InsDeleteKey(IS_EXTREGINF_HKCU, TEXT("SiteCert"), pcszInsFile);
  119. // blow away the pcszSCWorkDir and pcszSCInf
  120. if (pcszSCWorkDir != NULL)
  121. PathRemovePath(pcszSCWorkDir);
  122. PathRemovePath(pcszSCInf);
  123. if (!fImportSC)
  124. return TRUE;
  125. if (pcszSCWorkDir != NULL && PathIsFileSpec(pcszSCInf)) // create SITECERT.INF under pcszSCWorkDir
  126. PathCombine(szFullInfName, pcszSCWorkDir, pcszSCInf);
  127. else
  128. StrCpy(szFullInfName, pcszSCInf);
  129. // create SITECERT.INF file
  130. if ((hInf = CreateNewFile(szFullInfName)) != INVALID_HANDLE_VALUE)
  131. {
  132. TCHAR szBuf[MAX_PATH];
  133. HKEY hkSite1 = NULL, hkSite2 = NULL;
  134. // first, write the standard goo - [Version], [DefaultInstall], etc. - to SITECERT.INF
  135. WriteStringToFile(hInf, (LPCVOID) SC_INF_ADD, StrLen(SC_INF_ADD));
  136. SHOpenKeyHKLM(REG_KEY_SITECERT1, KEY_DEFAULT_ACCESS, &hkSite1);
  137. SHOpenKeyHKLM(REG_KEY_SITECERT2, KEY_DEFAULT_ACCESS, &hkSite2);
  138. if (hkSite1 != NULL && hkSite2 != NULL)
  139. {
  140. ExportRegTree2Inf(hkSite1, TEXT("HKLM"), REG_KEY_SITECERT1, hInf);
  141. ExportRegTree2Inf(hkSite2, TEXT("HKLM"), REG_KEY_SITECERT2, hInf);
  142. bRet = TRUE;
  143. }
  144. SHCloseKey(hkSite1);
  145. SHCloseKey(hkSite2);
  146. SHOpenKeyHKCU(REG_KEY_SITECERT1, KEY_DEFAULT_ACCESS, &hkSite1);
  147. SHOpenKeyHKCU(REG_KEY_SITECERT2, KEY_DEFAULT_ACCESS, &hkSite2);
  148. if (hkSite1 != NULL && hkSite2 != NULL)
  149. {
  150. // write [AddReg.HKCU]
  151. WriteStringToFile(hInf, (LPCVOID) SC_INF_ADDREG_HKCU, StrLen(SC_INF_ADDREG_HKCU));
  152. ExportRegTree2Inf(hkSite1, TEXT("HKCU"), REG_KEY_SITECERT1, hInf);
  153. ExportRegTree2Inf(hkSite2, TEXT("HKCU"), REG_KEY_SITECERT2, hInf);
  154. bRet = TRUE;
  155. }
  156. SHCloseKey(hkSite1);
  157. SHCloseKey(hkSite2);
  158. CloseHandle(hInf);
  159. // update the INS file
  160. InsWriteBool(SECURITY_IMPORTS, TEXT("ImportSiteCert"), TRUE, pcszInsFile);
  161. wnsprintf(szBuf, countof(szBuf), TEXT("*,%s,") IS_DEFAULTINSTALL, PathFindFileName(pcszSCInf));
  162. WritePrivateProfileString(IS_EXTREGINF, TEXT("SiteCert"), szBuf, pcszInsFile);
  163. // write to new ExtRegInf.HKLM and ExtRegInf.HKCU sections
  164. if (!InsIsSectionEmpty(IS_IEAKADDREG_HKLM, szFullInfName))
  165. {
  166. wnsprintf(szBuf, countof(szBuf), TEXT("%s,") IS_IEAKINSTALL_HKLM, PathFindFileName(pcszSCInf));
  167. WritePrivateProfileString(IS_EXTREGINF_HKLM, TEXT("SiteCert"), szBuf, pcszInsFile);
  168. }
  169. if (!InsIsSectionEmpty(IS_IEAKADDREG_HKCU, szFullInfName))
  170. {
  171. wnsprintf(szBuf, countof(szBuf), TEXT("%s,") IS_IEAKINSTALL_HKCU, PathFindFileName(pcszSCInf));
  172. WritePrivateProfileString(IS_EXTREGINF_HKCU, TEXT("SiteCert"), szBuf, pcszInsFile);
  173. }
  174. }
  175. return bRet;
  176. }
  177. static BOOL importAuthCodeHelper(LPCTSTR pcszInsFile, LPCTSTR pcszAuthWorkDir, LPCTSTR pcszAuthInf, BOOL fImportAuth)
  178. {
  179. BOOL bRet = FALSE;
  180. HKEY hkAuth;
  181. if (pcszInsFile == NULL || pcszAuthInf == NULL)
  182. return FALSE;
  183. // Before processing anything, first clear out the entries in the INS file and delete work dirs
  184. // clear out the entries in the INS file that correspond to importing authenticode settings
  185. InsDeleteKey(SECURITY_IMPORTS, TEXT("ImportAuthCode"), pcszInsFile);
  186. InsDeleteKey(IS_EXTREGINF, TEXT("AuthCode"), pcszInsFile);
  187. InsDeleteKey(IS_EXTREGINF_HKLM, TEXT("AuthCode"), pcszInsFile);
  188. // blow away the pcszAuthWorkDir and pcszAuthInf
  189. if (pcszAuthWorkDir != NULL)
  190. PathRemovePath(pcszAuthWorkDir);
  191. PathRemovePath(pcszAuthInf);
  192. if (!fImportAuth)
  193. return TRUE;
  194. if (SHOpenKeyHKCU(REG_KEY_AUTHENTICODE, KEY_DEFAULT_ACCESS, &hkAuth) == ERROR_SUCCESS)
  195. {
  196. TCHAR szFullInfName[MAX_PATH];
  197. HANDLE hInf;
  198. if (pcszAuthWorkDir != NULL && PathIsFileSpec(pcszAuthInf)) // create AUTHCODE.INF under pcszAuthWorkDir
  199. PathCombine(szFullInfName, pcszAuthWorkDir, pcszAuthInf);
  200. else
  201. StrCpy(szFullInfName, pcszAuthInf);
  202. // create AUTHCODE.INF file
  203. if ((hInf = CreateNewFile(szFullInfName)) != INVALID_HANDLE_VALUE)
  204. {
  205. TCHAR szBuf[MAX_PATH];
  206. // first, write the standard goo - [Version], [DefaultInstall], etc. - to AUTHCODE.INF
  207. WriteStringToFile(hInf, (LPCVOID) AUTH_INF_ADD, StrLen(AUTH_INF_ADD));
  208. ExportRegTree2Inf(hkAuth, TEXT("HKCU"), REG_KEY_AUTHENTICODE, hInf);
  209. CloseHandle(hInf);
  210. // update the INS file
  211. InsWriteBool(SECURITY_IMPORTS, TEXT("ImportAuthCode"), TRUE, pcszInsFile);
  212. wnsprintf(szBuf, countof(szBuf), TEXT("*,%s,") IS_DEFAULTINSTALL, PathFindFileName(pcszAuthInf));
  213. WritePrivateProfileString(IS_EXTREGINF, TEXT("AuthCode"), szBuf, pcszInsFile);
  214. // write to new ExtRegInf.HKCU section
  215. if (!InsIsSectionEmpty(IS_IEAKADDREG_HKCU, szFullInfName))
  216. {
  217. wnsprintf(szBuf, countof(szBuf), TEXT("%s,") IS_IEAKINSTALL_HKCU, PathFindFileName(pcszAuthInf));
  218. WritePrivateProfileString(IS_EXTREGINF_HKCU, TEXT("AuthCode"), szBuf, pcszInsFile);
  219. }
  220. bRet = TRUE;
  221. }
  222. SHCloseKey(hkAuth);
  223. }
  224. return bRet;
  225. }