Source code of Windows XP (NT5)
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.

270 lines
5.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: init.c
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 8-01-95 RichardW Created
  15. // 8-13-95 TerenceS Mutated to PCT
  16. //
  17. //----------------------------------------------------------------------------
  18. #include "sslp.h"
  19. #include <basetyps.h>
  20. #include <oidenc.h>
  21. #include <userenv.h>
  22. #include <alloca.h>
  23. RTL_CRITICAL_SECTION g_InitCritSec;
  24. BOOL g_fSchannelInitialized = FALSE;
  25. void LoadSecurityDll(void);
  26. void UnloadSecurityDll(void);
  27. // MyStrToL
  28. // Can't use CRT routines, so steal from the C runtime sources
  29. DWORD MyStrToL(CHAR *InStr)
  30. {
  31. DWORD dwVal = 0;
  32. while(*InStr)
  33. {
  34. dwVal = (10 * dwVal) + (*InStr - '0');
  35. InStr++;
  36. }
  37. return dwVal;
  38. }
  39. /*++
  40. Routine Description:
  41. This routine checks whether encryption is getting the system default
  42. LCID and checking whether the country code is CTRY_FRANCE.
  43. --*/
  44. void
  45. IsSchEncryptionPermitted(VOID)
  46. {
  47. LCID DefaultLcid;
  48. CHAR CountryCode[10];
  49. ULONG CountryValue;
  50. BOOL fAllowed = TRUE;
  51. DefaultLcid = GetSystemDefaultLCID();
  52. //
  53. // Check if the default language is Standard French
  54. //
  55. if (LANGIDFROMLCID(DefaultLcid) == 0x40c)
  56. {
  57. fAllowed = FALSE;
  58. goto Ret;
  59. }
  60. //
  61. // Check if the users's country is set to FRANCE
  62. //
  63. if (GetLocaleInfoA(DefaultLcid,LOCALE_ICOUNTRY,CountryCode,10) == 0)
  64. {
  65. fAllowed = FALSE;
  66. goto Ret;
  67. }
  68. CountryValue = (ULONG) MyStrToL(CountryCode);
  69. if (CountryValue == CTRY_FRANCE)
  70. {
  71. fAllowed = FALSE;
  72. }
  73. Ret:
  74. if(FALSE == fAllowed)
  75. {
  76. // Disable PCT in France.
  77. g_ProtEnabled &= ~(SP_PROT_PCT1);
  78. g_fFranceLocale = TRUE;
  79. }
  80. }
  81. /*****************************************************************************/
  82. BOOL
  83. SchannelInit(BOOL fAppProcess)
  84. {
  85. DWORD Status;
  86. if(g_fSchannelInitialized) return TRUE;
  87. RtlEnterCriticalSection(&g_InitCritSec);
  88. if(g_fSchannelInitialized)
  89. {
  90. RtlLeaveCriticalSection(&g_InitCritSec);
  91. return TRUE;
  92. }
  93. DisableThreadLibraryCalls( g_hInstance );
  94. SafeAllocaInitialize(0, 0, NULL, NULL);
  95. // Read configuration parameters from registry.
  96. if(!fAppProcess)
  97. {
  98. IsSchEncryptionPermitted();
  99. SPLoadRegOptions();
  100. }
  101. #if DBG
  102. else
  103. {
  104. InitDebugSupport(NULL);
  105. }
  106. #endif
  107. if(!fAppProcess)
  108. {
  109. SchInitializeEvents();
  110. }
  111. if(!CryptAcquireContextA(&g_hRsaSchannel,
  112. NULL,
  113. NULL,
  114. PROV_RSA_SCHANNEL,
  115. CRYPT_VERIFYCONTEXT))
  116. {
  117. g_hRsaSchannel = 0;
  118. Status = GetLastError();
  119. DebugLog((DEB_ERROR, "Could not open static PROV_RSA_SCHANNEL: %x\n", Status));
  120. if(!fAppProcess)
  121. {
  122. LogGlobalAcquireContextFailedEvent(L"RSA", Status);
  123. }
  124. RtlLeaveCriticalSection(&g_InitCritSec);
  125. return FALSE;
  126. }
  127. if(!fAppProcess && g_hRsaSchannel)
  128. {
  129. GetSupportedCapiAlgs(g_hRsaSchannel,
  130. SCH_CAPI_USE_CSP,
  131. &g_pRsaSchannelAlgs,
  132. &g_cRsaSchannelAlgs);
  133. }
  134. if(!CryptAcquireContext(&g_hDhSchannelProv,
  135. NULL,
  136. NULL,
  137. PROV_DH_SCHANNEL,
  138. CRYPT_VERIFYCONTEXT))
  139. {
  140. g_hDhSchannelProv = 0;
  141. Status = GetLastError();
  142. DebugLog((DEB_WARN, "Could not open PROV_DH_SCHANNEL: %x\n", Status));
  143. if(!fAppProcess)
  144. {
  145. LogGlobalAcquireContextFailedEvent(L"DSS", Status);
  146. }
  147. CryptReleaseContext(g_hRsaSchannel, 0);
  148. RtlLeaveCriticalSection(&g_InitCritSec);
  149. return FALSE;
  150. }
  151. if(!fAppProcess && g_hDhSchannelProv)
  152. {
  153. GetSupportedCapiAlgs(g_hDhSchannelProv,
  154. SCH_CAPI_USE_CSP,
  155. &g_pDhSchannelAlgs,
  156. &g_cDhSchannelAlgs);
  157. }
  158. InitSchannelAsn1(g_hInstance);
  159. LoadSecurityDll();
  160. if(!fAppProcess)
  161. {
  162. SPInitSessionCache();
  163. SslInitCredentialManager();
  164. }
  165. g_fSchannelInitialized = TRUE;
  166. if(!fAppProcess)
  167. {
  168. LogSchannelStartedEvent();
  169. }
  170. RtlLeaveCriticalSection(&g_InitCritSec);
  171. return TRUE;
  172. }
  173. BOOL SchannelShutdown(VOID)
  174. {
  175. RtlEnterCriticalSection(&g_InitCritSec);
  176. if(!g_fSchannelInitialized)
  177. {
  178. RtlLeaveCriticalSection(&g_InitCritSec);
  179. return TRUE;
  180. }
  181. SPShutdownSessionCache();
  182. UnloadSecurityDll();
  183. SslFreeCredentialManager();
  184. ShutdownSchannelAsn1();
  185. SchShutdownEvents();
  186. SPUnloadRegOptions();
  187. g_fSchannelInitialized = FALSE;
  188. RtlLeaveCriticalSection(&g_InitCritSec);
  189. return TRUE;
  190. }
  191. HINSTANCE g_hSecur32;
  192. FREE_CONTEXT_BUFFER_FN g_pFreeContextBuffer;
  193. void LoadSecurityDll(void)
  194. {
  195. g_hSecur32 = LoadLibrary(TEXT("secur32.dll"));
  196. if(g_hSecur32)
  197. {
  198. g_pFreeContextBuffer = (FREE_CONTEXT_BUFFER_FN)GetProcAddress(
  199. g_hSecur32,
  200. "FreeContextBuffer");
  201. }
  202. else
  203. {
  204. g_pFreeContextBuffer = NULL;
  205. }
  206. }
  207. void UnloadSecurityDll(void)
  208. {
  209. if(g_hSecur32)
  210. {
  211. FreeLibrary(g_hSecur32);
  212. }
  213. }