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.

342 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: provload.cpp
  8. //
  9. // Contents: Microsoft Internet Security Trust Provider
  10. //
  11. // Functions: WintrustLoadFunctionPointers
  12. // WintrustFindProvider
  13. // WintrustUnloadProviderList
  14. //
  15. // *** local functions ***
  16. // _CheckLoadedProviders
  17. // _CheckRegisteredProviders
  18. // _provLoadDLL
  19. // _provUnloadDLL
  20. // _provLoadFunction
  21. //
  22. // History: 29-May-1997 pberkman created
  23. //
  24. //--------------------------------------------------------------------------
  25. #include "global.hxx"
  26. #include "cryptreg.h"
  27. #include "eventlst.h"
  28. LOADED_PROVIDER *_CheckLoadedProviders(GUID *pgActionID);
  29. LOADED_PROVIDER *_CheckRegisteredProviders(GUID *pgActionID);
  30. BOOL _provLoadDLL(WCHAR *pwszDLL, HINSTANCE *phDLL);
  31. void _provUnloadDLL(HINSTANCE hDLL, WCHAR *pwszDLLName);
  32. BOOL _provLoadFunction(char *pszFunc, HINSTANCE hDLL, void **pfn);
  33. LOADED_PROVIDER *pProviderList = NULL;
  34. BOOL WINAPI WintrustLoadFunctionPointers(GUID *pgActionID, CRYPT_PROVIDER_FUNCTIONS *pPfns)
  35. {
  36. LOADED_PROVIDER *pProvFuncs;
  37. if (!(pPfns) ||
  38. !(WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(CRYPT_PROVIDER_FUNCTIONS, pPfns->cbStruct, psUIpfns)))
  39. {
  40. return(FALSE);
  41. }
  42. if (!(pProvFuncs = WintrustFindProvider(pgActionID)))
  43. {
  44. return(FALSE);
  45. }
  46. pPfns->pfnAlloc = WVTNew;
  47. pPfns->pfnFree = WVTDelete;
  48. pPfns->pfnAddStore2Chain = WVTAddStore;
  49. pPfns->pfnAddSgnr2Chain = WVTAddSigner;
  50. pPfns->pfnAddCert2Chain = WVTAddCertContext;
  51. pPfns->pfnAddPrivData2Chain = WVTAddPrivateData;
  52. pPfns->pfnInitialize = pProvFuncs->pfnInitialize;
  53. pPfns->pfnObjectTrust = pProvFuncs->pfnObjectTrust;
  54. pPfns->pfnSignatureTrust = pProvFuncs->pfnSignatureTrust;
  55. pPfns->pfnCertificateTrust = pProvFuncs->pfnCertificateTrust;
  56. pPfns->pfnFinalPolicy = pProvFuncs->pfnFinalPolicy;
  57. pPfns->pfnCertCheckPolicy = pProvFuncs->pfnCertCheckPolicy;
  58. pPfns->pfnTestFinalPolicy = pProvFuncs->pfnTestFinalPolicy;
  59. if (WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(CRYPT_PROVIDER_FUNCTIONS, pPfns->cbStruct, pfnCleanupPolicy))
  60. {
  61. pPfns->pfnCleanupPolicy = pProvFuncs->pfnCleanupPolicy;
  62. }
  63. return(TRUE);
  64. }
  65. LOADED_PROVIDER *WintrustFindProvider(GUID *pgActionID)
  66. {
  67. LOADED_PROVIDER *pProvider;
  68. if (!(pProvider = _CheckLoadedProviders(pgActionID)))
  69. {
  70. # if (DBG)
  71. DbgPrintf(DBG_SS, "Loading Provider: %08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  72. pgActionID->Data1, pgActionID->Data2, pgActionID->Data3, pgActionID->Data4[0],
  73. pgActionID->Data4[1], pgActionID->Data4[2], pgActionID->Data4[3], pgActionID->Data4[4],
  74. pgActionID->Data4[5], pgActionID->Data4[6], pgActionID->Data4[7]);
  75. # endif // DBG
  76. pProvider = _CheckRegisteredProviders(pgActionID);
  77. }
  78. # if (DBG)
  79. if (!(pProvider))
  80. {
  81. DbgPrintf(DBG_SS, "PROV NOT FOUND: %08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  82. pgActionID->Data1, pgActionID->Data2, pgActionID->Data3, pgActionID->Data4[0],
  83. pgActionID->Data4[1], pgActionID->Data4[2], pgActionID->Data4[3], pgActionID->Data4[4],
  84. pgActionID->Data4[5], pgActionID->Data4[6], pgActionID->Data4[7]);
  85. }
  86. # endif
  87. return(pProvider);
  88. }
  89. LOADED_PROVIDER *_CheckLoadedProviders(GUID *pgActionID)
  90. {
  91. LOADED_PROVIDER *pProvider;
  92. AcquireReadLock(sProvLock);
  93. pProvider = pProviderList;
  94. while (pProvider)
  95. {
  96. if (memcmp(pgActionID, &pProvider->gActionID, sizeof(GUID)) == 0)
  97. {
  98. ReleaseReadLock(sProvLock);
  99. return(pProvider);
  100. }
  101. pProvider = pProvider->pNext;
  102. }
  103. ReleaseReadLock(sProvLock);
  104. return(NULL);
  105. }
  106. BOOL WintrustUnloadProviderList(void)
  107. {
  108. LOADED_PROVIDER *pProvider;
  109. LOADED_PROVIDER *pProvHold;
  110. AcquireWriteLock(sProvLock);
  111. pProvider = pProviderList;
  112. while (pProvider)
  113. {
  114. if (pProvider->hInitDLL) FreeLibrary(pProvider->hInitDLL);
  115. if (pProvider->hObjectDLL) FreeLibrary(pProvider->hObjectDLL);
  116. if (pProvider->hSignatureDLL) FreeLibrary(pProvider->hSignatureDLL);
  117. if (pProvider->hCertTrustDLL) FreeLibrary(pProvider->hCertTrustDLL);
  118. if (pProvider->hFinalPolicyDLL) FreeLibrary(pProvider->hFinalPolicyDLL);
  119. if (pProvider->hCertPolicyDLL) FreeLibrary(pProvider->hCertPolicyDLL);
  120. if (pProvider->hTestFinalPolicyDLL) FreeLibrary(pProvider->hTestFinalPolicyDLL);
  121. if (pProvider->hCleanupPolicyDLL) FreeLibrary(pProvider->hCleanupPolicyDLL);
  122. # if (DBG)
  123. DbgPrintf(DBG_SS, "Unloading Provider: %08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  124. pProvider->gActionID.Data1, pProvider->gActionID.Data2, pProvider->gActionID.Data3,
  125. pProvider->gActionID.Data4[0], pProvider->gActionID.Data4[1],
  126. pProvider->gActionID.Data4[2], pProvider->gActionID.Data4[3],
  127. pProvider->gActionID.Data4[4], pProvider->gActionID.Data4[5],
  128. pProvider->gActionID.Data4[6], pProvider->gActionID.Data4[7]);
  129. # endif // DBG
  130. pProvHold = pProvider->pNext;
  131. delete pProvider;
  132. pProvider = pProvHold;
  133. }
  134. pProviderList = NULL;
  135. ReleaseWriteLock(sProvLock);
  136. return(TRUE);
  137. }
  138. LOADED_PROVIDER *_CheckRegisteredProviders(GUID *pgActionID)
  139. {
  140. LOADED_PROVIDER *pProvider;
  141. BOOL fRet;
  142. WCHAR wszInitDLL[REG_MAX_KEY_NAME];
  143. WCHAR wszObjTrustDLL[REG_MAX_KEY_NAME];
  144. WCHAR wszSigTrustDLL[REG_MAX_KEY_NAME];
  145. WCHAR wszCertTrustDLL[REG_MAX_KEY_NAME];
  146. WCHAR wszCertPolDLL[REG_MAX_KEY_NAME];
  147. WCHAR wszFinalPolDLL[REG_MAX_KEY_NAME];
  148. WCHAR wszTestFinalPolDLL[REG_MAX_KEY_NAME];
  149. WCHAR wszCleanupPolDLL[REG_MAX_KEY_NAME];
  150. char szInitFunc[REG_MAX_FUNC_NAME];
  151. char szObjTrustFunc[REG_MAX_FUNC_NAME];
  152. char szSigTrustFunc[REG_MAX_FUNC_NAME];
  153. char szCertTrustFunc[REG_MAX_FUNC_NAME];
  154. char szCertPolFunc[REG_MAX_FUNC_NAME];
  155. char szFinalPolFunc[REG_MAX_FUNC_NAME];
  156. char szTestFinalPolFunc[REG_MAX_FUNC_NAME];
  157. char szCleanupPolFunc[REG_MAX_FUNC_NAME];
  158. if (!(GetRegProvider(pgActionID, REG_CERTTRUST_PROVIDER_KEY, &wszCertTrustDLL[0], &szCertTrustFunc[0])))
  159. {
  160. return(NULL);
  161. }
  162. if (!(GetRegProvider(pgActionID, REG_FINALPOL_PROVIDER_KEY, &wszFinalPolDLL[0], &szFinalPolFunc[0])))
  163. {
  164. return(NULL);
  165. }
  166. // optional!
  167. GetRegProvider(pgActionID, REG_INIT_PROVIDER_KEY, &wszInitDLL[0], &szInitFunc[0]);
  168. GetRegProvider(pgActionID, REG_OBJTRUST_PROVIDER_KEY, &wszObjTrustDLL[0], &szObjTrustFunc[0]);
  169. GetRegProvider(pgActionID, REG_SIGTRUST_PROVIDER_KEY, &wszSigTrustDLL[0], &szSigTrustFunc[0]);
  170. GetRegProvider(pgActionID, REG_CERTPOL_PROVIDER_KEY, &wszCertPolDLL[0], &szCertPolFunc[0]);
  171. GetRegProvider(pgActionID, REG_TESTPOL_PROVIDER_KEY, &wszTestFinalPolDLL[0], &szTestFinalPolFunc[0]);
  172. GetRegProvider(pgActionID, REG_CLEANUP_PROVIDER_KEY, &wszCleanupPolDLL[0], &szCleanupPolFunc[0]);
  173. AcquireWriteLock(sProvLock);
  174. if (!(pProvider = (LOADED_PROVIDER *)WVTNew(sizeof(LOADED_PROVIDER))))
  175. {
  176. ReleaseWriteLock(sProvLock);
  177. return(NULL);
  178. }
  179. memset(pProvider, 0x00, sizeof(LOADED_PROVIDER));
  180. memcpy(&pProvider->gActionID, pgActionID, sizeof(GUID));
  181. fRet = TRUE;
  182. fRet &= _provLoadDLL(&wszCertTrustDLL[0], &pProvider->hCertTrustDLL);
  183. fRet &= _provLoadDLL(&wszFinalPolDLL[0], &pProvider->hFinalPolicyDLL);
  184. // optional!
  185. _provLoadDLL(&wszInitDLL[0], &pProvider->hInitDLL);
  186. _provLoadDLL(&wszObjTrustDLL[0], &pProvider->hObjectDLL);
  187. _provLoadDLL(&wszSigTrustDLL[0], &pProvider->hSignatureDLL);
  188. _provLoadDLL(&wszCertPolDLL[0], &pProvider->hCertPolicyDLL);
  189. _provLoadDLL(&wszTestFinalPolDLL[0], &pProvider->hTestFinalPolicyDLL);
  190. _provLoadDLL(&wszCleanupPolDLL[0], &pProvider->hCleanupPolicyDLL);
  191. fRet &= _provLoadFunction(&szCertTrustFunc[0], pProvider->hCertTrustDLL, (void **)&pProvider->pfnCertificateTrust);
  192. fRet &= _provLoadFunction(&szFinalPolFunc[0], pProvider->hFinalPolicyDLL, (void **)&pProvider->pfnFinalPolicy);
  193. // optional!
  194. _provLoadFunction(&szInitFunc[0], pProvider->hInitDLL, (void **)&pProvider->pfnInitialize);
  195. _provLoadFunction(&szObjTrustFunc[0], pProvider->hObjectDLL, (void **)&pProvider->pfnObjectTrust);
  196. _provLoadFunction(&szSigTrustFunc[0], pProvider->hSignatureDLL, (void **)&pProvider->pfnSignatureTrust);
  197. _provLoadFunction(&szCertPolFunc[0], pProvider->hCertPolicyDLL, (void **)&pProvider->pfnCertCheckPolicy);
  198. _provLoadFunction(&szTestFinalPolFunc[0], pProvider->hTestFinalPolicyDLL, (void **)&pProvider->pfnTestFinalPolicy);
  199. _provLoadFunction(&szCleanupPolFunc[0], pProvider->hCleanupPolicyDLL, (void **)&pProvider->pfnCleanupPolicy);
  200. if (!(fRet))
  201. {
  202. ReleaseWriteLock(sProvLock);
  203. _provUnloadDLL(pProvider->hInitDLL, &wszInitDLL[0]);
  204. _provUnloadDLL(pProvider->hObjectDLL, &wszObjTrustDLL[0]);
  205. _provUnloadDLL(pProvider->hSignatureDLL, &wszSigTrustDLL[0]);
  206. _provUnloadDLL(pProvider->hCertTrustDLL, &wszCertTrustDLL[0]);
  207. _provUnloadDLL(pProvider->hFinalPolicyDLL, &wszFinalPolDLL[0]);
  208. _provUnloadDLL(pProvider->hCertPolicyDLL, &wszCertPolDLL[0]);
  209. _provUnloadDLL(pProvider->hTestFinalPolicyDLL, &wszTestFinalPolDLL[0]);
  210. _provUnloadDLL(pProvider->hCleanupPolicyDLL, &wszCleanupPolDLL[0]);
  211. delete pProvider;
  212. return(NULL);
  213. }
  214. pProvider->pNext = pProviderList;
  215. pProvider->pPrev = NULL;
  216. if (pProvider->pNext)
  217. {
  218. pProvider->pNext->pPrev = pProvider;
  219. }
  220. pProviderList = pProvider;
  221. ReleaseWriteLock(sProvLock);
  222. return(pProvider);
  223. }
  224. BOOL _provLoadDLL(WCHAR *pwszDLL, HINSTANCE *phDLL)
  225. {
  226. *phDLL = NULL;
  227. if (!(pwszDLL[0]))
  228. {
  229. return(FALSE);
  230. }
  231. if (_wcsicmp(pwszDLL, W_MY_NAME) == 0)
  232. {
  233. *phDLL = (HINSTANCE)hMeDLL;
  234. }
  235. else
  236. {
  237. *phDLL = LoadLibraryU(pwszDLL);
  238. }
  239. if (*phDLL)
  240. {
  241. return(TRUE);
  242. }
  243. return(FALSE);
  244. }
  245. BOOL _provLoadFunction(char *pszFunc, HINSTANCE hDLL, void **pfn)
  246. {
  247. *pfn = NULL;
  248. if (!(pszFunc[0]) ||
  249. !(hDLL))
  250. {
  251. return(FALSE);
  252. }
  253. *pfn = (void *)GetProcAddress(hDLL, pszFunc);
  254. if (*pfn)
  255. {
  256. return(TRUE);
  257. }
  258. return(FALSE);
  259. }
  260. void _provUnloadDLL(HINSTANCE hDLL, WCHAR *pwszDLLName)
  261. {
  262. if ((hDLL) &&
  263. (_wcsicmp(pwszDLLName, W_MY_NAME) != 0))
  264. {
  265. FreeLibrary(hDLL);
  266. }
  267. }