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.

294 lines
8.0 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. memset(&m_ChainPara, 0, sizeof(m_ChainPara));
  42. if (NULL != pChainPara)
  43. memcpy(&m_ChainPara, pChainPara, min(pChainPara->cbSize,
  44. sizeof(m_ChainPara)));
  45. m_ChainPara.cbSize = sizeof(m_ChainPara);
  46. if (0 == m_ChainPara.dwUrlRetrievalTimeout) {
  47. m_ChainPara.dwUrlRetrievalTimeout =
  48. pChainEngine->UrlRetrievalTimeout();
  49. m_fDefaultUrlRetrievalTimeout =
  50. pChainEngine->HasDefaultUrlRetrievalTimeout();
  51. } else
  52. m_fDefaultUrlRetrievalTimeout = FALSE;
  53. memset( &Config, 0, sizeof( Config ) );
  54. Config.dwFlags = LRU_CACHE_NO_SERIALIZE | LRU_CACHE_NO_COPY_IDENTIFIER;
  55. Config.pfnHash = CertObjectCacheHashMd5Identifier;
  56. Config.pfnOnRemoval = CallContextOnCreationCacheObjectRemoval;
  57. Config.cBuckets = DEFAULT_CREATION_CACHE_BUCKETS;
  58. rfResult = I_CryptCreateLruCache( &Config, &m_hObjectCreationCache );
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Member: CChainCallContext::~CChainCallContext, public
  63. //
  64. // Synopsis: Destructor
  65. //
  66. //----------------------------------------------------------------------------
  67. CChainCallContext::~CChainCallContext ()
  68. {
  69. if ( m_hObjectCreationCache != NULL )
  70. {
  71. I_CryptFreeLruCache( m_hObjectCreationCache, 0, NULL );
  72. }
  73. }
  74. //+---------------------------------------------------------------------------
  75. //
  76. // Member: CChainCallContext::AddPathObjectToCreationCache, public
  77. //
  78. // Synopsis: add a path object to the creation cache
  79. //
  80. //----------------------------------------------------------------------------
  81. BOOL
  82. CChainCallContext::AddPathObjectToCreationCache (
  83. IN PCCHAINPATHOBJECT pPathObject
  84. )
  85. {
  86. BOOL fResult;
  87. CRYPT_DATA_BLOB DataBlob;
  88. HLRUENTRY hEntry;
  89. DataBlob.cbData = CHAINHASHLEN;
  90. DataBlob.pbData = pPathObject->CertObject()->CertHash();
  91. fResult = I_CryptCreateLruEntry(
  92. m_hObjectCreationCache,
  93. &DataBlob,
  94. pPathObject,
  95. &hEntry
  96. );
  97. if ( fResult == TRUE )
  98. {
  99. I_CryptInsertLruEntry( hEntry, pPathObject );
  100. I_CryptReleaseLruEntry( hEntry );
  101. }
  102. return( fResult );
  103. }
  104. //+---------------------------------------------------------------------------
  105. //
  106. // Member: CChainCallContext::FindPathObjectInCreationCache, public
  107. //
  108. // Synopsis: find a path object in the creation cache
  109. //
  110. //----------------------------------------------------------------------------
  111. PCCHAINPATHOBJECT
  112. CChainCallContext::FindPathObjectInCreationCache (
  113. IN BYTE rgbCertHash[ CHAINHASHLEN ]
  114. )
  115. {
  116. HLRUENTRY hFound;
  117. PCCHAINPATHOBJECT pFound = NULL;
  118. CRYPT_DATA_BLOB DataBlob;
  119. DataBlob.cbData = CHAINHASHLEN;
  120. DataBlob.pbData = rgbCertHash;
  121. hFound = I_CryptFindLruEntry( m_hObjectCreationCache, &DataBlob );
  122. if ( hFound != NULL )
  123. {
  124. pFound = (PCCHAINPATHOBJECT)I_CryptGetLruEntryData( hFound );
  125. I_CryptReleaseLruEntry( hFound );
  126. }
  127. return( pFound );
  128. }
  129. DWORD CChainCallContext::RevocationUrlRetrievalTimeout()
  130. {
  131. DWORD dwRevTimeout;
  132. if (m_dwCallFlags & CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT)
  133. {
  134. if (m_dwStatus & CHAINCALLCONTEXT_REV_END_TIME_FLAG)
  135. {
  136. dwRevTimeout = I_CryptRemainingMilliseconds(&m_RevEndTime);
  137. if (0 == dwRevTimeout)
  138. dwRevTimeout = 1;
  139. }
  140. else
  141. {
  142. FILETIME ftCurrent;
  143. if (m_fDefaultUrlRetrievalTimeout)
  144. dwRevTimeout = DEFAULT_REV_ACCUMULATIVE_URL_RETRIEVAL_TIMEOUT;
  145. else
  146. dwRevTimeout = m_ChainPara.dwUrlRetrievalTimeout;
  147. GetSystemTimeAsFileTime(&ftCurrent);
  148. I_CryptIncrementFileTimeByMilliseconds(&ftCurrent,
  149. dwRevTimeout, &m_RevEndTime);
  150. m_dwStatus |= CHAINCALLCONTEXT_REV_END_TIME_FLAG;
  151. }
  152. }
  153. else
  154. {
  155. dwRevTimeout = m_ChainPara.dwUrlRetrievalTimeout;
  156. }
  157. return dwRevTimeout;
  158. }
  159. BOOL
  160. CChainCallContext::IsOnline ()
  161. {
  162. if ( !(m_dwStatus & CHAINCALLCONTEXT_CHECKED_ONLINE_FLAG) )
  163. {
  164. if (!(m_pChainEngine->Flags() & CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL) &&
  165. !(m_dwCallFlags & CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL))
  166. {
  167. if ( ChainIsConnected() )
  168. {
  169. m_dwStatus |= CHAINCALLCONTEXT_ONLINE_FLAG;
  170. }
  171. }
  172. m_dwStatus |= CHAINCALLCONTEXT_CHECKED_ONLINE_FLAG;
  173. }
  174. if (m_dwStatus & CHAINCALLCONTEXT_ONLINE_FLAG)
  175. {
  176. return TRUE;
  177. }
  178. else
  179. {
  180. return FALSE;
  181. }
  182. }
  183. BOOL
  184. CChainCallContext::IsTouchedEngine ()
  185. {
  186. if (m_dwTouchEngineCount == m_pChainEngine->TouchEngineCount())
  187. return FALSE;
  188. else
  189. return TRUE;
  190. }
  191. //+---------------------------------------------------------------------------
  192. //
  193. // Function: CallContextCreateCallObject
  194. //
  195. // Synopsis: create a chain call context object
  196. //
  197. //----------------------------------------------------------------------------
  198. BOOL WINAPI
  199. CallContextCreateCallObject (
  200. IN PCCERTCHAINENGINE pChainEngine,
  201. IN OPTIONAL LPFILETIME pRequestedTime,
  202. IN OPTIONAL PCERT_CHAIN_PARA pChainPara,
  203. IN DWORD dwFlags,
  204. OUT PCCHAINCALLCONTEXT* ppCallContext
  205. )
  206. {
  207. BOOL fResult = FALSE;
  208. PCCHAINCALLCONTEXT pCallContext;
  209. pCallContext = new CChainCallContext(
  210. pChainEngine,
  211. pRequestedTime,
  212. pChainPara,
  213. dwFlags,
  214. fResult
  215. );
  216. if ( pCallContext == NULL )
  217. {
  218. SetLastError( (DWORD) E_OUTOFMEMORY );
  219. return( FALSE );
  220. }
  221. if ( fResult == TRUE )
  222. {
  223. *ppCallContext = pCallContext;
  224. }
  225. else
  226. {
  227. CallContextFreeCallObject( pCallContext );
  228. }
  229. return( fResult );
  230. }
  231. //+---------------------------------------------------------------------------
  232. //
  233. // Function: CallContextFreeCallObject
  234. //
  235. // Synopsis: free the chain call context object
  236. //
  237. //----------------------------------------------------------------------------
  238. VOID WINAPI
  239. CallContextFreeCallObject (
  240. IN PCCHAINCALLCONTEXT pCallContext
  241. )
  242. {
  243. delete pCallContext;
  244. }
  245. //+---------------------------------------------------------------------------
  246. //
  247. // Function: CallContextOnCreationCacheObjectRemoval
  248. //
  249. // Synopsis: removal notification callback
  250. //
  251. //----------------------------------------------------------------------------
  252. VOID WINAPI
  253. CallContextOnCreationCacheObjectRemoval (
  254. IN LPVOID pv,
  255. IN LPVOID pvRemovalContext
  256. )
  257. {
  258. delete (PCCHAINPATHOBJECT) pv;
  259. }