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.

377 lines
8.2 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. DWORD m_dwSecureProtocols;
  38. SECURITY_CACHE_LIST *m_pCertCache;
  39. SECURITY_CACHE_LIST_ENTRY *m_pSecurityInfo;
  40. #if INET_DEBUG
  41. #define SECURE_SOCKET_SIGNATURE 0x534c5353 // "SSLS"
  42. #define SIGN_SECURE_SOCKET() \
  43. m_Signature = SECURE_SOCKET_SIGNATURE
  44. #define CHECK_SECURE_SOCKET() \
  45. INET_ASSERT(m_Signature == SECURE_SOCKET_SIGNATURE)
  46. #else
  47. #define SIGN_SECURE_SOCKET() \
  48. /* NOTHING */
  49. #define CHECK_SECURE_SOCKET() \
  50. /* NOTHING */
  51. #endif
  52. VOID SetSecure(VOID)
  53. {
  54. SetSecureFlags(SECURITY_FLAG_SECURE);
  55. }
  56. DWORD
  57. EncryptData(
  58. IN LPVOID lpBuffer,
  59. IN DWORD dwInBufferLen,
  60. OUT LPVOID * lplpBuffer,
  61. OUT LPDWORD lpdwOutBufferLen,
  62. OUT LPDWORD lpdwInBufferBytesEncrypted
  63. );
  64. DWORD
  65. DecryptData(
  66. OUT DWORD * lpdwBytesNeeded,
  67. OUT LPBYTE lpOutBuffer,
  68. IN OUT LPDWORD lpdwOutBufferLeft,
  69. IN OUT LPDWORD lpdwOutBufferReceived,
  70. IN OUT LPDWORD lpdwOutBufferBytesRead
  71. );
  72. VOID
  73. TerminateSecConnection(
  74. VOID
  75. );
  76. BOOL IsSameFingerPrint(PCCERT_CONTEXT pCachedCert);
  77. DWORD ReVerifyTrust(DWORD dwRecheckFlag);
  78. public:
  79. ICSecureSocket(void);
  80. virtual ~ICSecureSocket(VOID);
  81. DWORD
  82. Connect(
  83. IN LONG ConnectTimeout,
  84. IN INT Retries,
  85. IN LONG SendTimeout,
  86. IN LONG RecvTimeout,
  87. IN DWORD dwFlags
  88. );
  89. DWORD
  90. Connect_Fsm(
  91. IN CFsm_SecureConnect * Fsm
  92. );
  93. DWORD
  94. SecureHandshake_Fsm(
  95. IN CFsm_SecureHandshake * Fsm
  96. );
  97. DWORD
  98. SecureNegotiate_Fsm(
  99. IN CFsm_SecureNegotiate * Fsm
  100. );
  101. DWORD
  102. NegotiateLoop_Fsm(
  103. IN CFsm_NegotiateLoop * Fsm
  104. );
  105. DWORD
  106. NegotiateSecConnection(
  107. IN DWORD dwFlags,
  108. OUT LPBOOL lpbAttemptReconnect
  109. );
  110. DWORD
  111. SSPINegotiateLoop(
  112. OUT DBLBUFFER * pDoubleBuffer,
  113. IN DWORD dwFlags,
  114. IN CredHandle hCreds,
  115. IN BOOL fDoInitialRead,
  116. IN BOOL bDoingClientAuth
  117. );
  118. DWORD
  119. Disconnect(
  120. IN DWORD dwFlags
  121. );
  122. DWORD
  123. Send(
  124. IN LPVOID lpBuffer,
  125. IN DWORD dwBufferLength,
  126. IN DWORD dwFlags
  127. );
  128. DWORD
  129. Send_Fsm(
  130. IN CFsm_SecureSend * Fsm
  131. );
  132. DWORD
  133. Receive(
  134. IN OUT LPVOID* lplpBuffer,
  135. IN OUT LPDWORD lpdwBufferLength,
  136. IN OUT LPDWORD lpdwBufferRemaining,
  137. IN OUT LPDWORD lpdwBytesReceived,
  138. IN DWORD dwExtraSpace,
  139. IN DWORD dwFlags,
  140. OUT LPBOOL lpbEof
  141. );
  142. DWORD
  143. Receive_Fsm(
  144. IN CFsm_SecureReceive * Fsm
  145. );
  146. DWORD
  147. SecureHandshakeWithServer(
  148. IN DWORD dwFlags,
  149. OUT LPBOOL lpfAttemptReconnect
  150. );
  151. DWORD
  152. VerifyTrust(
  153. VOID
  154. );
  155. SECURITY_CACHE_LIST_ENTRY * GetSecurityEntry()
  156. {
  157. if (m_pSecurityInfo != NULL) {
  158. m_pSecurityInfo->AddRef();
  159. return m_pSecurityInfo;
  160. }
  161. return NULL;
  162. }
  163. VOID SetSecurityEntry(SECURITY_CACHE_LIST_ENTRY *entry)
  164. {
  165. if (entry != NULL) {
  166. entry->AddRef();
  167. }
  168. if (m_pSecurityInfo != NULL) {
  169. m_pSecurityInfo->Release();
  170. }
  171. m_pSecurityInfo = entry;
  172. }
  173. DWORD
  174. SetHostName(
  175. IN LPSTR lpszHostName,
  176. IN INTERNET_PORT HostPort,
  177. IN SECURITY_CACHE_LIST *pCertCache
  178. );
  179. LPSTR GetHostName(VOID) const
  180. {
  181. return m_lpszHostName;
  182. }
  183. //
  184. // GetCertChainList (and)
  185. // SetCertChainList -
  186. // Sets and Gets Client Authentication Cert Chains.
  187. //
  188. CERT_CONTEXT_ARRAY* GetCertContextArray(VOID)
  189. {
  190. if(m_pSecurityInfo)
  191. {
  192. return m_pSecurityInfo->GetCertContextArray();
  193. }
  194. return NULL;
  195. }
  196. VOID SetCertContextArray(CERT_CONTEXT_ARRAY* pNewCertContextArray)
  197. {
  198. if(m_pSecurityInfo)
  199. {
  200. m_pSecurityInfo->SetCertContextArray(pNewCertContextArray);
  201. }
  202. }
  203. //
  204. // GetSecureFlags AND SetSecureFlags AND GetCertInfo
  205. // Allows setting and getting of a bitmask which
  206. // stores various data bits on current socket connection.
  207. //
  208. DWORD GetSecurityInfo(LPINTERNET_SECURITY_INFO pInfo)
  209. {
  210. if(m_pSecurityInfo)
  211. {
  212. m_pSecurityInfo->CopyOut(*pInfo);
  213. return ERROR_SUCCESS;
  214. }
  215. else
  216. {
  217. return ERROR_WINHTTP_INTERNAL_ERROR;
  218. }
  219. }
  220. VOID SetSecureFlags(DWORD Flags)
  221. {
  222. if(m_pSecurityInfo)
  223. {
  224. m_pSecurityInfo->SetSecureFlags(Flags);
  225. }
  226. }
  227. DWORD GetSecureFlags(VOID)
  228. {
  229. if(m_pSecurityInfo)
  230. {
  231. return m_pSecurityInfo->GetSecureFlags();
  232. }
  233. return 0;
  234. }
  235. VOID SetStatusFlags(DWORD Flags)
  236. {
  237. if(m_pSecurityInfo)
  238. {
  239. m_pSecurityInfo->SetStatusFlags(Flags);
  240. }
  241. }
  242. DWORD GetStatusFlags(VOID)
  243. {
  244. if(m_pSecurityInfo)
  245. {
  246. return m_pSecurityInfo->GetStatusFlags();
  247. }
  248. return 0;
  249. }
  250. DWORD GetProviderIndex(VOID) const
  251. {
  252. INET_ASSERT(IsSecure());
  253. return m_dwProviderIndex;
  254. }
  255. VOID SetProviderIndex(DWORD dwIndex)
  256. {
  257. INET_ASSERT(IsSecure());
  258. m_dwProviderIndex = dwIndex;
  259. }
  260. BOOL MatchSecureProtocolSemantics(
  261. DWORD dwFlags,
  262. LPSTR pszHostName = NULL,
  263. DWORD dwSecureProtocols = 0,
  264. DWORD dwSecureFlags = 0) {
  265. DWORD dwSocketSecureFlags = GetSecureFlags() & ~(SECURITY_FLAG_SECURE |
  266. SECURITY_FLAG_STRENGTH_WEAK |
  267. SECURITY_FLAG_STRENGTH_MEDIUM |
  268. SECURITY_FLAG_STRENGTH_STRONG);
  269. if ((dwSecureFlags & SECURITY_FLAG_CHECK_REVOCATION) &&
  270. !(dwSocketSecureFlags & SECURITY_FLAG_CHECK_REVOCATION))
  271. {
  272. return FALSE;
  273. }
  274. dwSocketSecureFlags &= (~SECURITY_FLAG_CHECK_REVOCATION);
  275. dwSecureFlags &= (~SECURITY_FLAG_CHECK_REVOCATION);
  276. // Enabled protocols should match what's being requested,
  277. // and if a k-a, the protocols for the socket still should match
  278. // that of the session
  279. // AND
  280. // Tunnel flag must match, as well as the established tunnel server if
  281. // going through a proxy.
  282. return ( (0 == (dwSecureProtocols ^ m_pCertCache->GetSecureProtocols()) &&
  283. m_pCertCache->GetSecureProtocols() == m_dwSecureProtocols) &&
  284. ((dwSocketSecureFlags & dwSecureFlags) == dwSocketSecureFlags) &&
  285. (((m_dwFlags & SF_TUNNEL) == (dwFlags & SF_TUNNEL)) ? TRUE : FALSE) &&
  286. (!pszHostName || 0 == strcmp(m_lpszHostName, pszHostName)) );
  287. }
  288. // Helper for flushing flags when first used as a CONNECT
  289. // for SSL tunneling.
  290. VOID ResetFlags(BOOL fSecure)
  291. {
  292. m_dwFlags = (fSecure ? SF_SECURE : 0);
  293. }
  294. };