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.

211 lines
8.2 KiB

  1. // --------------------------------------------------------------------------------
  2. // Demand.cpp
  3. // Written By: jimsch, brimo, t-erikne (bastardized by sbailey)
  4. // --------------------------------------------------------------------------------
  5. // W4 stuff
  6. #ifdef SMIME_V3
  7. #pragma warning(disable: 4201) // nameless struct/union
  8. #pragma warning(disable: 4514) // unreferenced inline function removed
  9. // --------------------------------------------------------------------------------
  10. // Includes
  11. // --------------------------------------------------------------------------------
  12. #include <windows.h>
  13. //#include "myassert.h"
  14. #define AssertSz(a, b)
  15. #define Assert(a)
  16. #define IMPLEMENT_LOADER_FUNCTIONS
  17. #include "crypttls.h"
  18. #include "ess.h"
  19. #include "demand2.h"
  20. #include "badstrfunctions.h"
  21. // --------------------------------------------------------------------------------
  22. // CRIT_GET_PROC_ADDR
  23. // --------------------------------------------------------------------------------
  24. #define CRIT_GET_PROC_ADDR(h, fn, temp) \
  25. temp = (TYP_##fn) GetProcAddress(h, #fn); \
  26. if (temp) \
  27. VAR_##fn = temp; \
  28. else \
  29. { \
  30. AssertSz(0, VAR_##fn" failed to load"); \
  31. goto error; \
  32. }
  33. // --------------------------------------------------------------------------------
  34. // RESET
  35. // --------------------------------------------------------------------------------
  36. #define RESET(fn) VAR_##fn = LOADER_##fn;
  37. // --------------------------------------------------------------------------------
  38. // GET_PROC_ADDR
  39. // --------------------------------------------------------------------------------
  40. #define GET_PROC_ADDR(h, fn) \
  41. VAR_##fn = (TYP_##fn) GetProcAddress(h, #fn); \
  42. Assert(VAR_##fn != NULL); \
  43. if(NULL == VAR_##fn ) { \
  44. VAR_##fn = LOADER_##fn; \
  45. }
  46. // --------------------------------------------------------------------------------
  47. // GET_PROC_ADDR_ORDINAL
  48. // --------------------------------------------------------------------------------
  49. #define GET_PROC_ADDR_ORDINAL(h, fn, ord) \
  50. VAR_##fn = (TYP_##fn) GetProcAddress(h, MAKEINTRESOURCE(ord)); \
  51. Assert(VAR_##fn != NULL);
  52. // --------------------------------------------------------------------------------
  53. // GET_PROC_ADDR3
  54. // --------------------------------------------------------------------------------
  55. #define GET_PROC_ADDR3(h, fn, varname) \
  56. VAR_##varname = (TYP_##varname) GetProcAddress(h, #fn); \
  57. Assert(VAR_##varname != NULL);
  58. // --------------------------------------------------------------------------------
  59. // Static Globals
  60. // --------------------------------------------------------------------------------
  61. static HMODULE s_hCrypt = 0;
  62. static HMODULE s_hNmasn1 = 0;
  63. static CRITICAL_SECTION g_csDefLoad = {0};
  64. // --------------------------------------------------------------------------------
  65. // InitDemandLoadedLibs
  66. // --------------------------------------------------------------------------------
  67. void ESS_InitDemandLoadLibs(void)
  68. {
  69. InitializeCriticalSection(&g_csDefLoad);
  70. }
  71. // --------------------------------------------------------------------------------
  72. // FreeDemandLoadedLibs
  73. // --------------------------------------------------------------------------------
  74. void ESS_FreeDemandLoadLibs(void)
  75. {
  76. EnterCriticalSection(&g_csDefLoad);
  77. if (s_hCrypt) FreeLibrary(s_hCrypt);
  78. if (s_hNmasn1) FreeLibrary(s_hNmasn1);
  79. LeaveCriticalSection(&g_csDefLoad);
  80. DeleteCriticalSection(&g_csDefLoad);
  81. }
  82. // --------------------------------------------------------------------------------
  83. // DemandLoadCrypt32
  84. // --------------------------------------------------------------------------------
  85. BOOL ESS_DemandLoadCrypt32(void)
  86. {
  87. BOOL fRet = TRUE;
  88. EnterCriticalSection(&g_csDefLoad);
  89. if (0 == s_hCrypt)
  90. {
  91. s_hCrypt = LoadLibrary("CRYPT32.DLL");
  92. AssertSz((BOOL)s_hCrypt, TEXT("LoadLibrary failed on CRYPT32.DLL"));
  93. if (0 == s_hCrypt)
  94. fRet = FALSE;
  95. else
  96. {
  97. GET_PROC_ADDR(s_hCrypt, CryptRegisterOIDFunction);
  98. GET_PROC_ADDR(s_hCrypt, I_CryptUninstallAsn1Module);
  99. GET_PROC_ADDR(s_hCrypt, I_CryptInstallAsn1Module);
  100. GET_PROC_ADDR(s_hCrypt, I_CryptGetAsn1Encoder);
  101. GET_PROC_ADDR(s_hCrypt, I_CryptGetAsn1Decoder);
  102. }
  103. }
  104. LeaveCriticalSection(&g_csDefLoad);
  105. return fRet;
  106. }
  107. BOOL FIsMsasn1Loaded()
  108. {
  109. static int fTested = FALSE;
  110. if (!fTested) {
  111. ESS_DemandLoadNmasn1();
  112. fTested = TRUE;
  113. }
  114. return s_hNmasn1 != 0;
  115. }
  116. // --------------------------------------------------------------------------------
  117. // DemandLoadNmasn1
  118. // --------------------------------------------------------------------------------
  119. BOOL ESS_DemandLoadNmasn1(void)
  120. {
  121. BOOL fRet = TRUE;
  122. EnterCriticalSection(&g_csDefLoad);
  123. if (0 == s_hNmasn1) {
  124. s_hNmasn1 = LoadLibrary("MSAsn1.DLL");
  125. AssertSz((BOOL)s_hNmasn1, TEXT("LoadLibrary failed on MSAsn1.DLL"));
  126. if (0 == s_hNmasn1)
  127. fRet = FALSE;
  128. else {
  129. GET_PROC_ADDR(s_hNmasn1, ASN1_CreateModule);
  130. GET_PROC_ADDR(s_hNmasn1, ASN1_CloseModule);
  131. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncObjectIdentifier2);
  132. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecObjectIdentifier2);
  133. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncEndOfContents);
  134. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncS32);
  135. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncOpenType);
  136. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncExplicitTag);
  137. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecEndOfContents);
  138. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecS32Val);
  139. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecOpenType2);
  140. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecExplicitTag);
  141. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncOctetString);
  142. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecOctetString2);
  143. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecOctetString);
  144. GET_PROC_ADDR(s_hNmasn1, ASN1octetstring_free);
  145. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncUTF8String);
  146. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecUTF8String);
  147. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecPeekTag);
  148. GET_PROC_ADDR(s_hNmasn1, ASN1utf8string_free);
  149. GET_PROC_ADDR(s_hNmasn1, ASN1DecRealloc);
  150. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecNotEndOfContents);
  151. GET_PROC_ADDR(s_hNmasn1, ASN1Free);
  152. GET_PROC_ADDR(s_hNmasn1, ASN1DecSetError);
  153. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncU32);
  154. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncCharString);
  155. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncBeginBlk);
  156. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncNewBlkElement);
  157. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncFlushBlkElement);
  158. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncEndBlk);
  159. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecU16Val);
  160. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecZeroCharString);
  161. GET_PROC_ADDR(s_hNmasn1, ASN1ztcharstring_free);
  162. GET_PROC_ADDR(s_hNmasn1, ASN1CEREncGeneralizedTime);
  163. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncNull);
  164. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecGeneralizedTime);
  165. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecNull);
  166. GET_PROC_ADDR(s_hNmasn1, ASN1_Encode);
  167. GET_PROC_ADDR(s_hNmasn1, ASN1_FreeEncoded);
  168. GET_PROC_ADDR(s_hNmasn1, ASN1_Decode);
  169. GET_PROC_ADDR(s_hNmasn1, ASN1_SetEncoderOption);
  170. GET_PROC_ADDR(s_hNmasn1, ASN1_GetEncoderOption);
  171. GET_PROC_ADDR(s_hNmasn1, ASN1_FreeDecoded);
  172. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncOctetString);
  173. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncCharString);
  174. GET_PROC_ADDR(s_hNmasn1, ASN1BEREncSX);
  175. GET_PROC_ADDR(s_hNmasn1, ASN1BERDecSXVal);
  176. GET_PROC_ADDR(s_hNmasn1, ASN1intx_free);
  177. }
  178. }
  179. LeaveCriticalSection(&g_csDefLoad);
  180. return fRet;
  181. }
  182. #endif // SMIME_V3