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.

355 lines
10 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: callctx.cpp
  7. //
  8. // Contents: Certificate Chaining Infrastructure Call Context
  9. //
  10. // History: 02-Mar-98 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <global.hxx>
  14. //+---------------------------------------------------------------------------
  15. //
  16. // Member: CChainCallContext::CChainCallContext, public
  17. //
  18. // Synopsis: Constructor
  19. //
  20. //----------------------------------------------------------------------------
  21. CChainCallContext::CChainCallContext (
  22. IN PCCERTCHAINENGINE pChainEngine,
  23. IN OPTIONAL LPFILETIME pRequestedTime,
  24. IN OPTIONAL PCERT_CHAIN_PARA pChainPara,
  25. IN DWORD dwFlags,
  26. OUT BOOL& rfResult
  27. )
  28. {
  29. LRU_CACHE_CONFIG Config;
  30. m_hObjectCreationCache = NULL;
  31. m_pChainEngine = pChainEngine;
  32. GetSystemTimeAsFileTime(&m_CurrentTime);
  33. if (pRequestedTime)
  34. m_RequestedTime = *pRequestedTime;
  35. else
  36. m_RequestedTime = m_CurrentTime;
  37. m_dwCallFlags = dwFlags;
  38. m_dwStatus = 0;
  39. m_dwTouchEngineCount = 0;
  40. // m_RevEndTime = // Initialized by RevocationUrlRetrievalTimeout()
  41. m_dwAIAUrlRetrievalCount = 0;
  42. // m_AIAEndTime = // Initialized by AIAUrlRetrievalTimeout()
  43. memset(&m_ChainPara, 0, sizeof(m_ChainPara));
  44. if (NULL != pChainPara)
  45. memcpy(&m_ChainPara, pChainPara, min(pChainPara->cbSize,
  46. sizeof(m_ChainPara)));
  47. m_ChainPara.cbSize = sizeof(m_ChainPara);
  48. if (0 == m_ChainPara.dwUrlRetrievalTimeout) {
  49. m_ChainPara.dwUrlRetrievalTimeout =
  50. pChainEngine->UrlRetrievalTimeout();
  51. m_fDefaultUrlRetrievalTimeout =
  52. pChainEngine->HasDefaultUrlRetrievalTimeout();
  53. } else
  54. m_fDefaultUrlRetrievalTimeout = FALSE;
  55. memset( &Config, 0, sizeof( Config ) );
  56. Config.dwFlags = LRU_CACHE_NO_SERIALIZE | LRU_CACHE_NO_COPY_IDENTIFIER;
  57. Config.pfnHash = CertObjectCacheHashMd5Identifier;
  58. Config.pfnOnRemoval = CallContextOnCreationCacheObjectRemoval;
  59. Config.cBuckets = DEFAULT_CREATION_CACHE_BUCKETS;
  60. rfResult = I_CryptCreateLruCache( &Config, &m_hObjectCreationCache );
  61. }
  62. //+---------------------------------------------------------------------------
  63. //
  64. // Member: CChainCallContext::~CChainCallContext, public
  65. //
  66. // Synopsis: Destructor
  67. //
  68. //----------------------------------------------------------------------------
  69. CChainCallContext::~CChainCallContext ()
  70. {
  71. if ( m_hObjectCreationCache != NULL )
  72. {
  73. I_CryptFreeLruCache( m_hObjectCreationCache, 0, NULL );
  74. }
  75. }
  76. //+---------------------------------------------------------------------------
  77. //
  78. // Member: CChainCallContext::AddPathObjectToCreationCache, public
  79. //
  80. // Synopsis: add a path object to the creation cache
  81. //
  82. //----------------------------------------------------------------------------
  83. BOOL
  84. CChainCallContext::AddPathObjectToCreationCache (
  85. IN PCCHAINPATHOBJECT pPathObject
  86. )
  87. {
  88. BOOL fResult;
  89. CRYPT_DATA_BLOB DataBlob;
  90. HLRUENTRY hEntry;
  91. DataBlob.cbData = CHAINHASHLEN;
  92. DataBlob.pbData = pPathObject->CertObject()->CertHash();
  93. fResult = I_CryptCreateLruEntry(
  94. m_hObjectCreationCache,
  95. &DataBlob,
  96. pPathObject,
  97. &hEntry
  98. );
  99. if ( fResult == TRUE )
  100. {
  101. I_CryptInsertLruEntry( hEntry, pPathObject );
  102. I_CryptReleaseLruEntry( hEntry );
  103. }
  104. return( fResult );
  105. }
  106. //+---------------------------------------------------------------------------
  107. //
  108. // Member: CChainCallContext::FindPathObjectInCreationCache, public
  109. //
  110. // Synopsis: find a path object in the creation cache
  111. //
  112. //----------------------------------------------------------------------------
  113. PCCHAINPATHOBJECT
  114. CChainCallContext::FindPathObjectInCreationCache (
  115. IN BYTE rgbCertHash[ CHAINHASHLEN ]
  116. )
  117. {
  118. HLRUENTRY hFound;
  119. PCCHAINPATHOBJECT pFound = NULL;
  120. CRYPT_DATA_BLOB DataBlob;
  121. DataBlob.cbData = CHAINHASHLEN;
  122. DataBlob.pbData = rgbCertHash;
  123. hFound = I_CryptFindLruEntry( m_hObjectCreationCache, &DataBlob );
  124. if ( hFound != NULL )
  125. {
  126. pFound = (PCCHAINPATHOBJECT)I_CryptGetLruEntryData( hFound );
  127. I_CryptReleaseLruEntry( hFound );
  128. }
  129. return( pFound );
  130. }
  131. //+---------------------------------------------------------------------------
  132. //
  133. // Member: CChainCallContext::RemovePathObjectFromCreationCache, public
  134. //
  135. // Synopsis: remove the specified path object from the creation cache
  136. //
  137. //----------------------------------------------------------------------------
  138. VOID
  139. CChainCallContext::RemovePathObjectFromCreationCache (
  140. IN PCCHAINPATHOBJECT pPathObject
  141. )
  142. {
  143. HLRUENTRY hEntry;
  144. CRYPT_DATA_BLOB DataBlob;
  145. DataBlob.cbData = CHAINHASHLEN;
  146. DataBlob.pbData = pPathObject->CertObject()->CertHash();
  147. hEntry = I_CryptFindLruEntry( m_hObjectCreationCache, &DataBlob );
  148. while (NULL != hEntry) {
  149. PCCHAINPATHOBJECT pFound;
  150. pFound = (PCCHAINPATHOBJECT) I_CryptGetLruEntryData( hEntry );
  151. if (pFound == pPathObject) {
  152. I_CryptReleaseLruEntry( hEntry );
  153. I_CryptRemoveLruEntry( hEntry, 0, this );
  154. break;
  155. }
  156. hEntry = I_CryptEnumMatchingLruEntries(hEntry);
  157. }
  158. }
  159. DWORD CChainCallContext::RevocationUrlRetrievalTimeout()
  160. {
  161. DWORD dwRevTimeout;
  162. if (m_dwCallFlags & CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT)
  163. {
  164. if (m_dwStatus & CHAINCALLCONTEXT_REV_END_TIME_FLAG)
  165. {
  166. dwRevTimeout = I_CryptRemainingMilliseconds(&m_RevEndTime);
  167. if (0 == dwRevTimeout)
  168. dwRevTimeout = 1;
  169. }
  170. else
  171. {
  172. FILETIME ftCurrent;
  173. if (m_fDefaultUrlRetrievalTimeout)
  174. dwRevTimeout = DEFAULT_REV_ACCUMULATIVE_URL_RETRIEVAL_TIMEOUT;
  175. else
  176. dwRevTimeout = m_ChainPara.dwUrlRetrievalTimeout;
  177. GetSystemTimeAsFileTime(&ftCurrent);
  178. I_CryptIncrementFileTimeByMilliseconds(&ftCurrent,
  179. dwRevTimeout, &m_RevEndTime);
  180. m_dwStatus |= CHAINCALLCONTEXT_REV_END_TIME_FLAG;
  181. }
  182. }
  183. else
  184. {
  185. dwRevTimeout = m_ChainPara.dwUrlRetrievalTimeout;
  186. }
  187. return dwRevTimeout;
  188. }
  189. DWORD CChainCallContext::AIAUrlRetrievalTimeout()
  190. {
  191. DWORD dwAIATimeout;
  192. if (m_dwStatus & CHAINCALLCONTEXT_AIA_END_TIME_FLAG)
  193. {
  194. dwAIATimeout = I_CryptRemainingMilliseconds(&m_AIAEndTime) / 2;
  195. if (0 == dwAIATimeout)
  196. dwAIATimeout = 1;
  197. }
  198. else
  199. {
  200. FILETIME ftCurrent;
  201. dwAIATimeout = m_ChainPara.dwUrlRetrievalTimeout;
  202. GetSystemTimeAsFileTime(&ftCurrent);
  203. I_CryptIncrementFileTimeByMilliseconds(&ftCurrent,
  204. dwAIATimeout * 2, &m_AIAEndTime);
  205. m_dwStatus |= CHAINCALLCONTEXT_AIA_END_TIME_FLAG;
  206. }
  207. return dwAIATimeout;
  208. }
  209. BOOL
  210. CChainCallContext::IsOnline ()
  211. {
  212. if ( !(m_dwStatus & CHAINCALLCONTEXT_CHECKED_ONLINE_FLAG) )
  213. {
  214. if (!(m_pChainEngine->Flags() & CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL) &&
  215. !(m_dwCallFlags & CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL))
  216. {
  217. if ( ChainIsConnected() )
  218. {
  219. m_dwStatus |= CHAINCALLCONTEXT_ONLINE_FLAG;
  220. }
  221. }
  222. m_dwStatus |= CHAINCALLCONTEXT_CHECKED_ONLINE_FLAG;
  223. }
  224. if (m_dwStatus & CHAINCALLCONTEXT_ONLINE_FLAG)
  225. {
  226. return TRUE;
  227. }
  228. else
  229. {
  230. return FALSE;
  231. }
  232. }
  233. BOOL
  234. CChainCallContext::IsTouchedEngine ()
  235. {
  236. if (m_dwTouchEngineCount == m_pChainEngine->TouchEngineCount())
  237. return FALSE;
  238. else
  239. return TRUE;
  240. }
  241. //+---------------------------------------------------------------------------
  242. //
  243. // Function: CallContextCreateCallObject
  244. //
  245. // Synopsis: create a chain call context object
  246. //
  247. //----------------------------------------------------------------------------
  248. BOOL WINAPI
  249. CallContextCreateCallObject (
  250. IN PCCERTCHAINENGINE pChainEngine,
  251. IN OPTIONAL LPFILETIME pRequestedTime,
  252. IN OPTIONAL PCERT_CHAIN_PARA pChainPara,
  253. IN DWORD dwFlags,
  254. OUT PCCHAINCALLCONTEXT* ppCallContext
  255. )
  256. {
  257. BOOL fResult = FALSE;
  258. PCCHAINCALLCONTEXT pCallContext;
  259. pCallContext = new CChainCallContext(
  260. pChainEngine,
  261. pRequestedTime,
  262. pChainPara,
  263. dwFlags,
  264. fResult
  265. );
  266. if ( pCallContext == NULL )
  267. {
  268. SetLastError( (DWORD) E_OUTOFMEMORY );
  269. return( FALSE );
  270. }
  271. if ( fResult == TRUE )
  272. {
  273. *ppCallContext = pCallContext;
  274. }
  275. else
  276. {
  277. CallContextFreeCallObject( pCallContext );
  278. }
  279. return( fResult );
  280. }
  281. //+---------------------------------------------------------------------------
  282. //
  283. // Function: CallContextFreeCallObject
  284. //
  285. // Synopsis: free the chain call context object
  286. //
  287. //----------------------------------------------------------------------------
  288. VOID WINAPI
  289. CallContextFreeCallObject (
  290. IN PCCHAINCALLCONTEXT pCallContext
  291. )
  292. {
  293. delete pCallContext;
  294. }
  295. //+---------------------------------------------------------------------------
  296. //
  297. // Function: CallContextOnCreationCacheObjectRemoval
  298. //
  299. // Synopsis: removal notification callback
  300. //
  301. //----------------------------------------------------------------------------
  302. VOID WINAPI
  303. CallContextOnCreationCacheObjectRemoval (
  304. IN LPVOID pv,
  305. IN LPVOID pvRemovalContext
  306. )
  307. {
  308. delete (PCCHAINPATHOBJECT) pv;
  309. }