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.

551 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. globals.c
  5. Abstract:
  6. Global definitions and initialization routines for the IIS
  7. cryptographic package.
  8. The following routines are exported by this module:
  9. IISCryptoInitialize
  10. IISCryptoTerminate
  11. IcpGetLastError
  12. Author:
  13. Keith Moore (keithmo) 02-Dec-1996
  14. Revision History:
  15. --*/
  16. #include "precomp.h"
  17. #pragma hdrstop
  18. //
  19. // Public globals.
  20. //
  21. IC_GLOBALS IcpGlobals;
  22. #if IC_ENABLE_COUNTERS
  23. IC_COUNTERS IcpCounters;
  24. #endif // IC_ENABLE_COUNTERS
  25. //
  26. // Private constants.
  27. //
  28. //
  29. // Private types.
  30. //
  31. //
  32. // Private globals.
  33. //
  34. // these flags are used for programatic override of encryption presnece/absence
  35. // and are used for the case when on French machine without encryption locale becomes
  36. // changed and encryption becomes available
  37. // in NT5 RC3 French encryption was enabled, so French now has encryption!
  38. BOOL fCryptoSettingsDoOverrride = FALSE;
  39. BOOL fCryptoSettingsOverrideFlag = FALSE;
  40. //
  41. // Private prototypes.
  42. //
  43. //
  44. // Public functions.
  45. //
  46. HRESULT
  47. WINAPI
  48. IISCryptoInitialize(
  49. VOID
  50. )
  51. /*++
  52. Routine Description:
  53. This routine initializes the IIS crypto package.
  54. N.B. This routine may only be called via a single thread of
  55. execution; it is not necessarily multi-thread safe.
  56. Arguments:
  57. None.
  58. Return Value:
  59. HRESULT - Completion status, 0 if successful, !0 otherwise.
  60. --*/
  61. {
  62. BOOL isNt = FALSE;
  63. OSVERSIONINFO osInfo;
  64. if( !IcpGlobals.Initialized ) {
  65. //
  66. // Initialize our critical section.
  67. //
  68. INITIALIZE_CRITICAL_SECTION(
  69. &IcpGlobals.GlobalLock
  70. );
  71. #if IC_ENABLE_COUNTERS
  72. //
  73. // Initialize our object counters.
  74. //
  75. RtlZeroMemory(
  76. &IcpCounters,
  77. sizeof(IcpCounters)
  78. );
  79. #endif // IC_ENABLE_COUNTERS
  80. //
  81. // The hash length will get initialized the first time
  82. // it's needed.
  83. //
  84. IcpGlobals.HashLength = 0;
  85. //
  86. // Determine if cryptography should be enabled.
  87. //
  88. osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  89. if ( GetVersionEx( &osInfo ) ) {
  90. isNt = (osInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
  91. }
  92. if( isNt &&
  93. (IcpIsEncryptionPermitted())) {
  94. IcpGlobals.EnableCryptography = TRUE;
  95. } else {
  96. IcpGlobals.EnableCryptography = FALSE;
  97. }
  98. #ifdef _WIN64
  99. // 64 bit hack... 64 bit crypto should be working now, so no need to do this anymore.
  100. // IcpGlobals.EnableCryptography = FALSE;
  101. #endif
  102. #if DBG
  103. {
  104. //
  105. // On checked builds, you can override the default
  106. // EnableCryptography flag via a registry parameter.
  107. //
  108. HKEY key;
  109. LONG err;
  110. LONG flag;
  111. DWORD type;
  112. DWORD length;
  113. err = RegOpenKeyEx(
  114. HKEY_LOCAL_MACHINE,
  115. TEXT("Software\\Microsoft\\K2"),
  116. 0,
  117. KEY_ALL_ACCESS,
  118. &key
  119. );
  120. if( err == NO_ERROR ) {
  121. length = sizeof(flag);
  122. err = RegQueryValueEx(
  123. key,
  124. TEXT("EnableCryptography"),
  125. NULL,
  126. &type,
  127. (LPBYTE)&flag,
  128. &length
  129. );
  130. if( err == NO_ERROR && type == REG_DWORD ) {
  131. IcpGlobals.EnableCryptography = ( flag != 0 );
  132. }
  133. RegCloseKey( key );
  134. }
  135. }
  136. #endif // DBG
  137. //
  138. // Remember that we're successfully initialized.
  139. //
  140. IcpGlobals.Initialized = TRUE;
  141. }
  142. // that's a special case for handling for override of encryption presense
  143. // will be called only on French machines with english locale
  144. if (fCryptoSettingsDoOverrride)
  145. {
  146. IcpGlobals.EnableCryptography = fCryptoSettingsOverrideFlag;
  147. }
  148. //
  149. // Success!
  150. //
  151. return NO_ERROR;
  152. } // IISCryptoInitialize
  153. HRESULT
  154. WINAPI
  155. IISCryptoTerminate(
  156. VOID
  157. )
  158. /*++
  159. Routine Description:
  160. This routine terminates the IIS crypto package.
  161. N.B. This routine may only be called via a single thread of
  162. execution; it is not necessarily multi-thread safe.
  163. Arguments:
  164. None.
  165. Return Value:
  166. HRESULT - Completion status, 0 if successful, !0 otherwise.
  167. --*/
  168. {
  169. if( IcpGlobals.Initialized ) {
  170. //
  171. // Nuke our critical section.
  172. //
  173. DeleteCriticalSection( &IcpGlobals.GlobalLock );
  174. //
  175. // Remember that we're successfully terminated.
  176. //
  177. IcpGlobals.Initialized = FALSE;
  178. }
  179. //
  180. // Success!
  181. //
  182. return NO_ERROR;
  183. } // IISCryptoTerminate
  184. VOID
  185. WINAPI
  186. IISCryptoInitializeOverride(BOOL flag)
  187. /*++
  188. Routine Description:
  189. This routine overides global flag about presence of encryption
  190. functions. It should be used only in one case, when French machine
  191. without encryption, has locale changed to US and then gets encryption
  192. capability what breaks a lot of code where attemt to decrypt is made on
  193. non encrypted data
  194. Arguments:
  195. BOOL flag indicating how to override encryption presence. Only False suppose
  196. to be used.
  197. Return Value:
  198. None
  199. --*/
  200. {
  201. IcpGlobals.EnableCryptography = flag;
  202. fCryptoSettingsDoOverrride = TRUE;
  203. fCryptoSettingsOverrideFlag = flag;
  204. } //IISCryptoInitializeOverride
  205. BOOL
  206. IcpIsEncryptionPermitted(
  207. VOID
  208. )
  209. /*++
  210. Routine Description:
  211. This routine checks whether encryption is getting the system default
  212. LCID and checking whether the country code is CTRY_FRANCE.
  213. This code was received from Jeff Spelman, and is the same
  214. code the crypto API's use do determine if encryption is
  215. allowed.
  216. Arguments:
  217. none
  218. Return Value:
  219. TRUE - encryption is permitted
  220. FALSE - encryption is not permitted
  221. --*/
  222. {
  223. // in NT5 RC3 French encryption was enabled, so French now has encryption!
  224. // since French was the only special case for encryption, just return TRUE all the time.
  225. /*
  226. LCID DefaultLcid;
  227. CHAR CountryCode[10];
  228. ULONG CountryValue;
  229. DefaultLcid = GetSystemDefaultLCID();
  230. //
  231. // Check if the default language is Standard French
  232. //
  233. if (LANGIDFROMLCID(DefaultLcid) == 0x40c) {
  234. return(FALSE);
  235. }
  236. //
  237. // Check if the users's country is set to FRANCE
  238. //
  239. if (GetLocaleInfoA(DefaultLcid,LOCALE_ICOUNTRY,CountryCode,10) == 0) {
  240. return(FALSE);
  241. }
  242. CountryValue = (ULONG) atol(CountryCode);
  243. if (CountryValue == CTRY_FRANCE) {
  244. return(FALSE);
  245. }
  246. //
  247. // and it still we think that encryption is permited thetre it comes a special hack for that
  248. // setup case where english or whatever install is installed with France locality from the begining and setup
  249. // thread still thinks that it is not in France.
  250. // Setup in iis.dll sets SetThreadLocale to the correct one
  251. //
  252. DefaultLcid = GetThreadLocale();
  253. //
  254. // Check if the default language is Standard French
  255. //
  256. if (LANGIDFROMLCID(DefaultLcid) == 0x40c) {
  257. return(FALSE);
  258. }
  259. */
  260. return(TRUE);
  261. }
  262. VOID
  263. WINAPI
  264. IcpAcquireGlobalLock(
  265. VOID
  266. )
  267. /*++
  268. Routine Description:
  269. This routine acquires the global IIS crypto lock.
  270. N.B. This routine is "semi-private"; it is only used by IISCRYPT.LIB
  271. and ICRYPT.LIB, not by "normal" code.
  272. Arguments:
  273. None.
  274. Return Value:
  275. None.
  276. --*/
  277. {
  278. EnterCriticalSection( &IcpGlobals.GlobalLock );
  279. } // IcpAcquireGlobalLock
  280. VOID
  281. WINAPI
  282. IcpReleaseGlobalLock(
  283. VOID
  284. )
  285. /*++
  286. Routine Description:
  287. This routine releases the global IIS crypto lock.
  288. N.B. This routine is "semi-private"; it is only used by IISCRYPT.LIB
  289. and ICRYPT.LIB, not by "normal" code.
  290. Arguments:
  291. None.
  292. Return Value:
  293. None.
  294. --*/
  295. {
  296. LeaveCriticalSection( &IcpGlobals.GlobalLock );
  297. } // IcpReleaseGlobalLock
  298. HRESULT
  299. IcpGetLastError(
  300. VOID
  301. )
  302. /*++
  303. Routine Description:
  304. Returns the last error, mapped to an HRESULT.
  305. Arguments:
  306. None.
  307. Return Value:
  308. HRESULT - Last error.
  309. --*/
  310. {
  311. DWORD lastErr;
  312. lastErr = GetLastError();
  313. return RETURNCODETOHRESULT(lastErr);
  314. } // IcpGetLastError
  315. //
  316. // Private functions.
  317. //
  318. #if IC_ENABLE_COUNTERS
  319. PVOID
  320. WINAPI
  321. IcpAllocMemory(
  322. IN DWORD Size
  323. )
  324. {
  325. PVOID buffer;
  326. buffer = IISCryptoAllocMemory( Size );
  327. if( buffer != NULL ) {
  328. UpdateAllocs();
  329. }
  330. return buffer;
  331. } // IcpAllocMemory
  332. VOID
  333. WINAPI
  334. IcpFreeMemory(
  335. IN PVOID Buffer
  336. )
  337. {
  338. UpdateFrees();
  339. IISCryptoFreeMemory( Buffer );
  340. } // IcpFreeMemory
  341. #endif // IC_ENABLE_COUNTERS
  342. BOOL
  343. WINAPI
  344. IISCryptoIsClearTextSignature (
  345. IIS_CRYPTO_BLOB UNALIGNED *pBlob
  346. )
  347. /*++
  348. Routine Description:
  349. Returns TRUE if blob is clear text
  350. Arguments:
  351. Ptr to blob
  352. Return Value:
  353. BOOL
  354. --*/
  355. {
  356. return (pBlob->BlobSignature == CLEARTEXT_BLOB_SIGNATURE);
  357. }