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.

331 lines
9.4 KiB

  1. #ifndef _SSLCONTEXT_HXX_
  2. #define _SSLCONTEXT_HXX_
  3. /*++
  4. Copyright (c) 1998 Microsoft Corporation
  5. Module Name :
  6. sslcontext.hxx
  7. Abstract:
  8. SSL stream context
  9. Author:
  10. Bilal Alam (BAlam) 29-March-2000
  11. Environment:
  12. Win32 - User Mode
  13. Project:
  14. Stream Filter Worker Process
  15. --*/
  16. class ENDPOINT_CONFIG;
  17. #define SSL_ASC_FLAGS ( ASC_REQ_EXTENDED_ERROR | \
  18. ASC_REQ_SEQUENCE_DETECT | \
  19. ASC_REQ_REPLAY_DETECT | \
  20. ASC_REQ_CONFIDENTIALITY | \
  21. ASC_REQ_STREAM | \
  22. ASC_REQ_ALLOCATE_MEMORY )
  23. enum SSL_STATE
  24. {
  25. SSL_STATE_HANDSHAKE_START = 0,
  26. SSL_STATE_HANDSHAKE_IN_PROGRESS,
  27. SSL_STATE_HANDSHAKE_COMPLETE
  28. };
  29. #define SSL_CONTEXT_FLAG_SYNC 0x1
  30. #define SSL_CONTEXT_FLAG_ASYNC 0x2
  31. #define SZ_REG_CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL L"CertChainCacheOnlyUrlRetrieval"
  32. class SSL_STREAM_CONTEXT : public STREAM_CONTEXT
  33. {
  34. public:
  35. SSL_STREAM_CONTEXT(
  36. FILTER_CHANNEL_CONTEXT * pFiltChannelContext
  37. );
  38. virtual ~SSL_STREAM_CONTEXT();
  39. VOID *
  40. operator new(
  41. size_t size
  42. )
  43. {
  44. UNREFERENCED_PARAMETER( size );
  45. DBG_ASSERT( size == sizeof( SSL_STREAM_CONTEXT ) );
  46. DBG_ASSERT( sm_pachSslStreamContexts != NULL );
  47. return sm_pachSslStreamContexts->Alloc();
  48. }
  49. VOID
  50. operator delete(
  51. VOID * pSslStreamContext
  52. )
  53. {
  54. DBG_ASSERT( pSslStreamContext != NULL );
  55. DBG_ASSERT( sm_pachSslStreamContexts != NULL );
  56. DBG_REQUIRE( sm_pachSslStreamContexts->Free( pSslStreamContext ) );
  57. }
  58. static
  59. HRESULT
  60. Initialize(
  61. VOID
  62. );
  63. static
  64. VOID
  65. Terminate(
  66. VOID
  67. );
  68. HRESULT
  69. ProcessRawReadData(
  70. RAW_STREAM_INFO * pRawStreamInfo,
  71. BOOL * pfReadMore,
  72. BOOL * pfComplete
  73. );
  74. HRESULT
  75. ProcessRawWriteData(
  76. RAW_STREAM_INFO * pRawStreamInfo,
  77. BOOL * pfComplete
  78. );
  79. HRESULT
  80. ProcessNewConnection(
  81. CONNECTION_INFO * pConnectionInfo,
  82. ENDPOINT_CONFIG * pEndpointConfig
  83. );
  84. HRESULT
  85. SendDataBack(
  86. RAW_STREAM_INFO * pRawStreamInfo
  87. );
  88. private:
  89. VOID
  90. ConditionalAddWorkerThread(
  91. VOID
  92. )
  93. /*++
  94. Routine Description:
  95. AcceptSecurityContext is always synchronous call
  96. If hardware accelerator is used then during AcceptSecurityContext call
  97. our worker thread will get blocked waiting for accelerator to complete
  98. while doing nothing. That may lead to low CPU because of the blocking
  99. not enough worker threads is around to handle SSL.
  100. AcceptSecurityContext will not have async support any time soon so the
  101. only thing we can do to improve performance is to bump up the soft thread limit
  102. by one before calling AcceptSecurityContext and bumping it down on completion
  103. Also using DS mapper may cause delays on AcceptSecurityContext calls
  104. if remote DC is accessed
  105. Arguments:
  106. None
  107. Return Value:
  108. none
  109. --*/
  110. {
  111. DBG_ASSERT( _pEndpointConfig != NULL );
  112. DBG_ASSERT( _pEndpointConfig->QueryServerCert() );
  113. if ( _pEndpointConfig->QueryServerCert()->QueryUsesHardwareAccelerator() ||
  114. _pEndpointConfig->QueryUseDSMapper() )
  115. {
  116. QueryFiltChannelContext()->AddWorkerThread();
  117. }
  118. }
  119. VOID
  120. ConditionalRemoveWorkerThread(
  121. VOID
  122. )
  123. /*++
  124. Routine Description:
  125. AcceptSecurityContext is always synchronous call
  126. If hardware accelerator is used then during AcceptSecurityContext call
  127. our worker thread will get blocked waiting for accelerator to complete
  128. while doing nothing. That may lead to low CPU because of the blocking
  129. not enough worker threads is around to handle SSL.
  130. AcceptSecurityContext will not have async support any time soon so the
  131. only thing we can do to improve performance is to bump up the soft thread limit
  132. by one before calling AcceptSecurityContext and bumping it down on completion
  133. Also using DS mapper may cause delays on AcceptSecurityContext calls
  134. if remote DC is accessed
  135. Arguments:
  136. None
  137. Return Value:
  138. none
  139. --*/
  140. {
  141. DBG_ASSERT( _pEndpointConfig != NULL );
  142. DBG_ASSERT( _pEndpointConfig->QueryServerCert() );
  143. if ( _pEndpointConfig->QueryServerCert()->QueryUsesHardwareAccelerator() ||
  144. _pEndpointConfig->QueryUseDSMapper() )
  145. {
  146. QueryFiltChannelContext()->RemoveWorkerThread();
  147. }
  148. }
  149. CredHandle *
  150. QueryCredentials(
  151. VOID
  152. );
  153. HRESULT
  154. DoHandshakeCompleted(
  155. VOID
  156. );
  157. HRESULT
  158. DoHandshake(
  159. RAW_STREAM_INFO * pRawStreamInfo,
  160. BOOL * pfReadMore,
  161. BOOL * pfComplete,
  162. BOOL * pfExtraData
  163. );
  164. HRESULT
  165. RetrieveClientCertAndToken(
  166. VOID
  167. );
  168. HRESULT
  169. DoRenegotiate(
  170. VOID
  171. );
  172. HRESULT
  173. DoDecrypt(
  174. RAW_STREAM_INFO * pRawStreamInfo,
  175. BOOL * pfReadMore,
  176. BOOL * pfComplete,
  177. BOOL * pfExtraData
  178. );
  179. HRESULT
  180. DoEncrypt(
  181. RAW_STREAM_INFO * pRawStreamInfo,
  182. BOOL * pfComplete
  183. );
  184. HRESULT
  185. BuildSslInfo(
  186. VOID
  187. );
  188. VOID
  189. DumpCertDebugInfo(
  190. DWORD dwPolicyStatus
  191. );
  192. HRESULT
  193. BuildClientCertInfo(
  194. VOID
  195. );
  196. private:
  197. static
  198. HRESULT
  199. OnHandshakeRawWriteCompletion(
  200. PVOID pParam
  201. );
  202. enum INIT_STATE {
  203. INIT_NONE,
  204. INIT_CERT_STORE,
  205. INIT_SERVER_CERT,
  206. INIT_IIS_CTL,
  207. INIT_SITE_CREDENTIALS,
  208. INIT_ENDPOINT_CONFIG,
  209. INIT_ACACHE
  210. };
  211. // initialization state
  212. static enum INIT_STATE s_InitState;
  213. // Endpoint (IP:Port based) SSL configuration
  214. ENDPOINT_CONFIG * _pEndpointConfig;
  215. // The state of the handshake
  216. SSL_STATE _sslState;
  217. // Handshake state information
  218. // Stream sizes (as retrieved from QueryContextAttributes(SECPKG_ATTR_STREAM_SIZES)
  219. DWORD _cbHeader;
  220. DWORD _cbTrailer;
  221. DWORD _cbBlockSize;
  222. DWORD _cbMaximumMessage;
  223. // offset to the raw buffer of the incoming stream
  224. // where the data not processed yet starts
  225. // _cbToBeProcessedOffset is often equal to _cbDecrypted
  226. // but there are cases when they differ
  227. // See DoDecrypt() for example of difference in value
  228. DWORD _cbToBeProcessedOffset;
  229. // number of bytes in the raw data buffer on the incoming stream
  230. // that were already processed (decrypted)
  231. DWORD _cbDecrypted;
  232. // SSL Security Context Handle
  233. CtxtHandle _hContext;
  234. // Flag if hContext is valid
  235. BOOL _fValidContext;
  236. // Flag that application requested cert mapping
  237. // this flag is mostly legacy because IIS certificate
  238. // mapping are done in IIS by worker process and
  239. // DS mappings happen once they are enabled on endpoint
  240. BOOL _fDoCertMap;
  241. // Flag that client certificate renegotiation was started by server
  242. // Note: This flag doesn't uniquely indicate that client
  243. // certificates are negotiated. They are negotiated
  244. // also when client certificates are enabled on site level
  245. BOOL _fRenegotiate;
  246. // _fExpectRenegotiationFromClient flag will be used to eliminate
  247. // client triggered renegotiation by enabling client data to cause
  248. // renegotiation only if fExpectRenegotiationFromClient is already set
  249. BOOL _fExpectRenegotiationFromClient;
  250. // SSL information (not related to client certificates)
  251. HTTP_SSL_INFO _ulSslInfo;
  252. // SSL client certificate related info (including mapped tokens)
  253. HTTP_SSL_CLIENT_CERT_INFO _ulCertInfo;
  254. // Client Certificate context negotiated for connection
  255. PCCERT_CONTEXT _pClientCert;
  256. // If active directory mapping is enabled and handshake
  257. // Lookaside
  258. static ALLOC_CACHE_HANDLER * sm_pachSslStreamContexts;
  259. // flag for CertGetCertificateChain whether intermediate certificates
  260. // for the cert chain building can be retrieved of the network
  261. // (cache only is set by default because going the the network is not
  262. // a safe - although convenient - alternative)
  263. static BOOL sm_fCertChainCacheOnlyUrlRetrieval;
  264. };
  265. #endif