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.

448 lines
8.9 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. secinit.cxx
  5. Abstract:
  6. Contains load function for security.dll on NT and secur32.dll on win95
  7. Also handles WinTrust.dll function loading.
  8. Author:
  9. Sophia Chung (sophiac) 6-Feb-1996
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. #include <wininetp.h>
  15. //
  16. // InitializationLock - protects against multiple threads loading security.dll
  17. // (secur32.dll) and entry points
  18. //
  19. CCritSec InitializationSecLock;
  20. //
  21. // GlobalSecFuncTable - Pointer to Global Structure of Pointers that are used
  22. // for storing the entry points into the SCHANNEL.dll
  23. //
  24. PSecurityFunctionTable GlobalSecFuncTable = NULL;
  25. //
  26. // pWinVerifyTrust - Pointer to Entry Point in WINTRUST.DLL
  27. //
  28. WIN_VERIFY_TRUST_FN pWinVerifyTrust;
  29. WT_HELPER_PROV_DATA_FROM_STATE_DATA_FN pWTHelperProvDataFromStateData;
  30. //
  31. // g_hSecurity - NULL when security.dll/secur32.dll is not loaded
  32. //
  33. HINSTANCE g_hSecurity = NULL;
  34. //
  35. // g_hWinTrust - NULL when WinTrust DLL is not loaded.
  36. //
  37. HINSTANCE g_hWinTrust = NULL;
  38. HINSTANCE g_hCrypt32 = NULL;
  39. CERT_OPEN_STORE_FN g_pfnCertOpenStore = NULL;
  40. CERT_FIND_CERTIFICATE_IN_STORE_FN g_pfnCertFindCertificateInStore = NULL;
  41. CERT_DUPLICATE_CERTIFICATE_CONTEXT_FN g_pfnCertDuplicateCertificateContext = NULL;
  42. CERT_NAME_TO_STR_W_FN g_pfnCertNameToStr = NULL;
  43. CERT_CONTROL_STORE_FN g_pfnCertControlStore = NULL;
  44. CRYPT_UNPROTECT_DATA_FN g_pfnCryptUnprotectData = NULL;
  45. CERT_CLOSE_STORE_FN g_pfnCertCloseStore = NULL;
  46. CERT_GET_CERT_PROPERTY g_pfnCertGetCertProperty = NULL;
  47. DWORD
  48. LoadWinTrust(
  49. VOID
  50. )
  51. /*++
  52. Routine Description:
  53. This function loads the WinTrust.DLL and binds a pointer to a function
  54. that is needed in the WinTrust DLL.
  55. Arguments:
  56. NONE.
  57. Return Value:
  58. WINDOWS Error Code.
  59. --*/
  60. {
  61. DWORD error = ERROR_SUCCESS;
  62. if (!LOCK_SECURITY())
  63. {
  64. return ERROR_NOT_ENOUGH_MEMORY;
  65. }
  66. if( g_hWinTrust == NULL )
  67. {
  68. LPSTR lpszDllFileName = WINTRUST_DLLNAME;
  69. pWinVerifyTrust = NULL;
  70. //
  71. // Load the DLL
  72. //
  73. g_hWinTrust = LoadLibrary(lpszDllFileName);
  74. if ( g_hWinTrust )
  75. {
  76. pWinVerifyTrust = (WIN_VERIFY_TRUST_FN)
  77. GetProcAddress(g_hWinTrust, WIN_VERIFY_TRUST_NAME);
  78. pWTHelperProvDataFromStateData = (WT_HELPER_PROV_DATA_FROM_STATE_DATA_FN)
  79. GetProcAddress(g_hWinTrust, WT_HELPER_PROV_DATA_FROM_STATE_DATA_NAME);
  80. }
  81. if ( !g_hWinTrust || !pWinVerifyTrust )
  82. {
  83. error = GetLastError();
  84. if ( error == ERROR_SUCCESS )
  85. {
  86. error = ERROR_WINHTTP_INTERNAL_ERROR;
  87. }
  88. }
  89. }
  90. INET_ASSERT(pWinVerifyTrust);
  91. if ( error != ERROR_SUCCESS )
  92. {
  93. if (g_hWinTrust)
  94. {
  95. FreeLibrary(g_hWinTrust);
  96. g_hWinTrust = NULL;
  97. }
  98. }
  99. UNLOCK_SECURITY();
  100. return error;
  101. }
  102. BOOL
  103. SecurityInitialize(
  104. VOID
  105. )
  106. /*++
  107. Routine Description:
  108. This function initializes the global lock required for the security
  109. pkgs.
  110. Arguments:
  111. NONE.
  112. Return Value:
  113. WINDOWS Error Code.
  114. --*/
  115. {
  116. return InitializationSecLock.Init();
  117. }
  118. VOID
  119. SecurityTerminate(
  120. VOID
  121. )
  122. /*++
  123. Routine Description:
  124. This function Deletes the global lock required for the security
  125. pkgs.
  126. Arguments:
  127. NONE.
  128. Return Value:
  129. WINDOWS Error Code.
  130. --*/
  131. {
  132. InitializationSecLock.FreeLock();
  133. }
  134. VOID
  135. UnloadSecurity(
  136. VOID
  137. )
  138. /*++
  139. Routine Description:
  140. This function terminates the global data required for the security
  141. pkgs and dynamically unloads security APIs from security.dll (NT)
  142. or secur32.dll (WIN95).
  143. Arguments:
  144. NONE.
  145. Return Value:
  146. WINDOWS Error Code.
  147. --*/
  148. {
  149. if (!LOCK_SECURITY())
  150. {
  151. INET_ASSERT(FALSE);
  152. return;
  153. }
  154. //
  155. // unload dll
  156. //
  157. if (g_hSecurity != NULL)
  158. {
  159. FreeLibrary(g_hSecurity);
  160. g_hSecurity = NULL;
  161. }
  162. if (g_hCrypt32 != NULL)
  163. {
  164. FreeLibrary(g_hCrypt32);
  165. g_hCrypt32 = NULL;
  166. }
  167. if (g_hWinTrust)
  168. {
  169. FreeLibrary(g_hWinTrust);
  170. g_hWinTrust = NULL;
  171. }
  172. UNLOCK_SECURITY();
  173. }
  174. DWORD
  175. LoadSecurity(
  176. VOID
  177. )
  178. /*++
  179. Routine Description:
  180. This function dynamically loads security APIs from security.dll (NT)
  181. or secur32.dll (WIN95).
  182. Arguments:
  183. NONE.
  184. Return Value:
  185. WINDOWS Error Code.
  186. --*/
  187. {
  188. DWORD Error = ERROR_SUCCESS;
  189. INITSECURITYINTERFACE pfInitSecurityInterface = NULL;
  190. if (!LOCK_SECURITY())
  191. return ERROR_NOT_ENOUGH_MEMORY;
  192. Error = LoadWinTrust();
  193. if ( Error != ERROR_SUCCESS )
  194. {
  195. goto Cleanup;
  196. }
  197. if( g_hSecurity != NULL && g_hCrypt32 != NULL)
  198. {
  199. goto quit;
  200. }
  201. //
  202. // load dll.
  203. //
  204. //
  205. // This is better for performance. Rather than call through
  206. // SSPI, we go right to the DLL doing the work.
  207. //
  208. g_hSecurity = LoadLibrary( "schannel" );
  209. if ( g_hSecurity == NULL )
  210. {
  211. goto ErrorFail;
  212. }
  213. g_hCrypt32 = LoadLibrary( "crypt32" );
  214. if ( g_hCrypt32 == NULL )
  215. {
  216. goto ErrorFail;
  217. }
  218. else
  219. {
  220. g_pfnCertOpenStore = (CERT_OPEN_STORE_FN)
  221. GetProcAddress(g_hCrypt32, "CertOpenStore");
  222. if (!g_pfnCertOpenStore)
  223. {
  224. goto ErrorFail;
  225. }
  226. g_pfnCertCloseStore = (CERT_CLOSE_STORE_FN)
  227. GetProcAddress(g_hCrypt32, "CertCloseStore");
  228. if (!g_pfnCertCloseStore)
  229. {
  230. goto ErrorFail;
  231. }
  232. g_pfnCertGetCertProperty = (CERT_GET_CERT_PROPERTY)
  233. GetProcAddress(g_hCrypt32, "CertGetCertificateContextProperty");
  234. if (!g_pfnCertGetCertProperty)
  235. {
  236. goto ErrorFail;
  237. }
  238. g_pfnCertFindCertificateInStore = (CERT_FIND_CERTIFICATE_IN_STORE_FN)
  239. GetProcAddress(g_hCrypt32, "CertFindCertificateInStore");
  240. if (!g_pfnCertFindCertificateInStore)
  241. {
  242. goto ErrorFail;
  243. }
  244. g_pfnCertFreeCertificateContext = (CERT_FREE_CERTIFICATE_CONTEXT_FN)
  245. GetProcAddress(g_hCrypt32, "CertFreeCertificateContext");
  246. if (!g_pfnCertFreeCertificateContext)
  247. {
  248. goto ErrorFail;
  249. }
  250. g_pfnCertDuplicateCertificateContext = (CERT_DUPLICATE_CERTIFICATE_CONTEXT_FN)
  251. GetProcAddress(g_hCrypt32, "CertDuplicateCertificateContext");
  252. if (!g_pfnCertDuplicateCertificateContext)
  253. {
  254. goto ErrorFail;
  255. }
  256. g_pfnCertNameToStr = (CERT_NAME_TO_STR_W_FN)
  257. GetProcAddress(g_hCrypt32, "CertNameToStrW");
  258. if (!g_pfnCertNameToStr)
  259. {
  260. goto ErrorFail;
  261. }
  262. g_pfnCertControlStore = (CERT_CONTROL_STORE_FN)
  263. GetProcAddress(g_hCrypt32, "CertControlStore");
  264. if (!g_pfnCertControlStore)
  265. {
  266. goto ErrorFail;
  267. }
  268. g_pfnCryptUnprotectData = (CRYPT_UNPROTECT_DATA_FN)
  269. GetProcAddress(g_hCrypt32, "CryptUnprotectData");
  270. if (!g_pfnCryptUnprotectData)
  271. {
  272. goto ErrorFail;
  273. }
  274. }
  275. //
  276. // get function addresses.
  277. //
  278. #ifdef UNICODE
  279. pfInitSecurityInterface =
  280. (INITSECURITYINTERFACE) GetProcAddress( g_hSecurity,
  281. "InitSecurityInterfaceW" );
  282. #else
  283. pfInitSecurityInterface =
  284. (INITSECURITYINTERFACE) GetProcAddress( g_hSecurity,
  285. "InitSecurityInterfaceA" );
  286. #endif
  287. if ( pfInitSecurityInterface == NULL )
  288. {
  289. goto ErrorFail;
  290. }
  291. GlobalSecFuncTable = (SecurityFunctionTable*) ((*pfInitSecurityInterface) ());
  292. if ( GlobalSecFuncTable == NULL ) {
  293. goto ErrorFail;
  294. }
  295. Cleanup:
  296. if ( Error != ERROR_SUCCESS )
  297. {
  298. if (g_hSecurity)
  299. {
  300. FreeLibrary( g_hSecurity );
  301. g_hSecurity = NULL;
  302. }
  303. if (g_hCrypt32)
  304. {
  305. FreeLibrary( g_hCrypt32 );
  306. g_hCrypt32 = NULL;
  307. }
  308. }
  309. quit:
  310. UNLOCK_SECURITY();
  311. return( Error );
  312. ErrorFail:
  313. Error = GetLastError();
  314. goto Cleanup;
  315. }