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
6.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. ssocket.hxx
  5. Abstract:
  6. Contains types, manifests, prototypes for Internet Secure Socket Class
  7. (ICSecureSocket) functions and methods (in common\ssocket.cxx)
  8. Author:
  9. Richard L Firth (rfirth) 08-Apr-1997
  10. Revision History:
  11. 08-Apr-1997 rfirth
  12. Created (from ixport.hxx)
  13. --*/
  14. #define SECURITY_WIN32
  15. #include <sspi.h>
  16. #include <issperr.h>
  17. #include <buffer.hxx>
  18. #include <winerror.h>
  19. //
  20. // forward references
  21. //
  22. class CFsm_SecureConnect;
  23. class CFsm_SecureHandshake;
  24. class CFsm_SecureNegotiate;
  25. class CFsm_NegotiateLoop;
  26. class CFsm_SecureSend;
  27. class CFsm_SecureReceive;
  28. //
  29. // classes
  30. //
  31. class ICSecureSocket : public ICSocket {
  32. private:
  33. CtxtHandle m_hContext;
  34. DWORD m_dwProviderIndex;
  35. LPSTR m_lpszHostName;
  36. DBLBUFFER * m_pdblbufBuffer;
  37. SECURITY_CACHE_LIST *m_pCertCache;
  38. SECURITY_CACHE_LIST_ENTRY *m_pSecurityInfo;
  39. #if INET_DEBUG
  40. #define SECURE_SOCKET_SIGNATURE 0x534c5353 // "SSLS"
  41. #define SIGN_SECURE_SOCKET() \
  42. m_Signature = SECURE_SOCKET_SIGNATURE
  43. #define CHECK_SECURE_SOCKET() \
  44. INET_ASSERT(m_Signature == SECURE_SOCKET_SIGNATURE)
  45. #else
  46. #define SIGN_SECURE_SOCKET() \
  47. /* NOTHING */
  48. #define CHECK_SECURE_SOCKET() \
  49. /* NOTHING */
  50. #endif
  51. VOID SetSecure(VOID)
  52. {
  53. SetSecureFlags(SECURITY_FLAG_SECURE);
  54. }
  55. DWORD
  56. EncryptData(
  57. IN LPVOID lpBuffer,
  58. IN DWORD dwInBufferLen,
  59. OUT LPVOID * lplpBuffer,
  60. OUT LPDWORD lpdwOutBufferLen,
  61. OUT LPDWORD lpdwInBufferBytesEncrypted
  62. );
  63. DWORD
  64. DecryptData(
  65. OUT DWORD * lpdwBytesNeeded,
  66. OUT LPBYTE lpOutBuffer,
  67. IN OUT LPDWORD lpdwOutBufferLeft,
  68. IN OUT LPDWORD lpdwOutBufferReceived,
  69. IN OUT LPDWORD lpdwOutBufferBytesRead
  70. );
  71. VOID
  72. TerminateSecConnection(
  73. VOID
  74. );
  75. public:
  76. ICSecureSocket(void);
  77. virtual ~ICSecureSocket(VOID);
  78. DWORD
  79. Connect(
  80. IN LONG Timeout,
  81. IN INT Retries,
  82. IN DWORD dwFlags
  83. );
  84. DWORD
  85. Connect_Fsm(
  86. IN CFsm_SecureConnect * Fsm
  87. );
  88. DWORD
  89. SecureHandshake_Fsm(
  90. IN CFsm_SecureHandshake * Fsm
  91. );
  92. DWORD
  93. SecureNegotiate_Fsm(
  94. IN CFsm_SecureNegotiate * Fsm
  95. );
  96. DWORD
  97. NegotiateLoop_Fsm(
  98. IN CFsm_NegotiateLoop * Fsm
  99. );
  100. DWORD
  101. NegotiateSecConnection(
  102. IN DWORD dwFlags,
  103. OUT LPBOOL lpbAttemptReconnect
  104. );
  105. DWORD
  106. SSPINegotiateLoop(
  107. OUT DBLBUFFER * pDoubleBuffer,
  108. IN DWORD dwFlags,
  109. IN CredHandle hCreds,
  110. IN BOOL fDoInitialRead,
  111. IN BOOL bDoingClientAuth
  112. );
  113. DWORD
  114. Disconnect(
  115. IN DWORD dwFlags
  116. );
  117. DWORD
  118. Send(
  119. IN LPVOID lpBuffer,
  120. IN DWORD dwBufferLength,
  121. IN DWORD dwFlags
  122. );
  123. DWORD
  124. Send_Fsm(
  125. IN CFsm_SecureSend * Fsm
  126. );
  127. DWORD
  128. Receive(
  129. IN OUT LPVOID* lplpBuffer,
  130. IN OUT LPDWORD lpdwBufferLength,
  131. IN OUT LPDWORD lpdwBufferRemaining,
  132. IN OUT LPDWORD lpdwBytesReceived,
  133. IN DWORD dwExtraSpace,
  134. IN DWORD dwFlags,
  135. OUT LPBOOL lpbEof
  136. );
  137. DWORD
  138. Receive_Fsm(
  139. IN CFsm_SecureReceive * Fsm
  140. );
  141. DWORD
  142. SecureHandshakeWithServer(
  143. IN DWORD dwFlags,
  144. OUT LPBOOL lpfAttemptReconnect
  145. );
  146. DWORD
  147. VerifyTrust(
  148. VOID
  149. );
  150. SECURITY_CACHE_LIST_ENTRY * GetSecurityEntry()
  151. {
  152. if (m_pSecurityInfo != NULL) {
  153. m_pSecurityInfo->AddRef();
  154. return m_pSecurityInfo;
  155. }
  156. return NULL;
  157. }
  158. VOID SetSecurityEntry(SECURITY_CACHE_LIST_ENTRY *entry)
  159. {
  160. if (entry != NULL) {
  161. entry->AddRef();
  162. }
  163. if (m_pSecurityInfo != NULL) {
  164. m_pSecurityInfo->Release();
  165. }
  166. m_pSecurityInfo = entry;
  167. }
  168. DWORD
  169. SetHostName(
  170. IN LPSTR lpszHostName,
  171. IN SECURITY_CACHE_LIST *pCertCache
  172. );
  173. LPSTR GetHostName(VOID) const
  174. {
  175. return m_lpszHostName;
  176. }
  177. //
  178. // GetCertChainList (and)
  179. // SetCertChainList -
  180. // Sets and Gets Client Authentication Cert Chains.
  181. //
  182. CERT_CONTEXT_ARRAY* GetCertContextArray(VOID)
  183. {
  184. if(m_pSecurityInfo)
  185. {
  186. return m_pSecurityInfo->GetCertContextArray();
  187. }
  188. return NULL;
  189. }
  190. VOID SetCertContextArray(CERT_CONTEXT_ARRAY* pNewCertContextArray)
  191. {
  192. if(m_pSecurityInfo)
  193. {
  194. m_pSecurityInfo->SetCertContextArray(pNewCertContextArray);
  195. }
  196. }
  197. //
  198. // GetSecureFlags AND SetSecureFlags AND GetCertInfo
  199. // Allows setting and getting of a bitmask which
  200. // stores various data bits on current socket connection.
  201. //
  202. DWORD GetSecurityInfo(LPINTERNET_SECURITY_INFO pInfo)
  203. {
  204. if(m_pSecurityInfo)
  205. {
  206. m_pSecurityInfo->CopyOut(*pInfo);
  207. return ERROR_SUCCESS;
  208. }
  209. else
  210. {
  211. return ERROR_WINHTTP_INTERNAL_ERROR;
  212. }
  213. }
  214. VOID SetSecureFlags(DWORD Flags)
  215. {
  216. if(m_pSecurityInfo)
  217. {
  218. m_pSecurityInfo->SetSecureFlags(Flags);
  219. }
  220. }
  221. DWORD GetSecureFlags(VOID)
  222. {
  223. if(m_pSecurityInfo)
  224. {
  225. return m_pSecurityInfo->GetSecureFlags();
  226. }
  227. return 0;
  228. }
  229. VOID SetStatusFlags(DWORD Flags)
  230. {
  231. if(m_pSecurityInfo)
  232. {
  233. m_pSecurityInfo->SetStatusFlags(Flags);
  234. }
  235. }
  236. DWORD GetStatusFlags(VOID)
  237. {
  238. if(m_pSecurityInfo)
  239. {
  240. return m_pSecurityInfo->GetStatusFlags();
  241. }
  242. return 0;
  243. }
  244. DWORD GetProviderIndex(VOID) const
  245. {
  246. INET_ASSERT(IsSecure());
  247. return m_dwProviderIndex;
  248. }
  249. VOID SetProviderIndex(DWORD dwIndex)
  250. {
  251. INET_ASSERT(IsSecure());
  252. m_dwProviderIndex = dwIndex;
  253. }
  254. BOOL MatchTunnelSemantics(DWORD dwFlags, LPSTR pszHostName = NULL)
  255. {
  256. return (((m_dwFlags & SF_TUNNEL) == (dwFlags & SF_TUNNEL)) ? TRUE : FALSE) &&
  257. (!pszHostName || 0 == strcmp(m_lpszHostName, pszHostName));
  258. }
  259. // Helper for flushing flags when first used as a CONNECT
  260. // for SSL tunneling.
  261. VOID ResetFlags(BOOL fSecure)
  262. {
  263. m_dwFlags = (fSecure ? SF_SECURE : 0);
  264. }
  265. };