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.

542 lines
9.7 KiB

  1. #ifndef _SSPIPROVIDER_HXX_
  2. #define _SSPIPROVIDER_HXX_
  3. #define NTDIGEST_SP_NAME "wdigest"
  4. class SSPI_CONTEXT_STATE : public W3_MAIN_CONTEXT_STATE
  5. {
  6. public:
  7. SSPI_CONTEXT_STATE(
  8. CHAR * pszCredentials
  9. )
  10. {
  11. DBG_ASSERT( pszCredentials != NULL );
  12. _pszCredentials = pszCredentials;
  13. }
  14. HRESULT
  15. SetPackage(
  16. CHAR * pszPackage
  17. )
  18. {
  19. return _strPackage.Copy( pszPackage );
  20. }
  21. CHAR *
  22. QueryPackage(
  23. VOID
  24. )
  25. {
  26. return _strPackage.QueryStr();
  27. }
  28. CHAR *
  29. QueryCredentials(
  30. VOID
  31. ) const
  32. {
  33. return _pszCredentials;
  34. }
  35. STRA *
  36. QueryResponseHeader(
  37. VOID
  38. )
  39. {
  40. return &_strResponseHeader;
  41. }
  42. BOOL
  43. Cleanup(
  44. W3_MAIN_CONTEXT * pMainContext
  45. )
  46. {
  47. delete this;
  48. return TRUE;
  49. }
  50. private:
  51. STRA _strPackage;
  52. CHAR * _pszCredentials;
  53. STRA _strResponseHeader;
  54. };
  55. class SSPI_AUTH_PROVIDER : public AUTH_PROVIDER
  56. {
  57. public:
  58. SSPI_AUTH_PROVIDER( DWORD dwAuthType )
  59. {
  60. m_dwAuthType = dwAuthType;
  61. }
  62. virtual ~SSPI_AUTH_PROVIDER()
  63. {
  64. }
  65. virtual
  66. HRESULT
  67. Initialize(
  68. DWORD dwInternalId
  69. );
  70. virtual
  71. VOID
  72. Terminate(
  73. VOID
  74. );
  75. virtual
  76. HRESULT
  77. DoesApply(
  78. W3_MAIN_CONTEXT * pMainContext,
  79. BOOL * pfApplies
  80. );
  81. virtual
  82. HRESULT
  83. DoAuthenticate(
  84. W3_MAIN_CONTEXT * pMainContext
  85. );
  86. virtual
  87. HRESULT
  88. OnAccessDenied(
  89. W3_MAIN_CONTEXT * pMainContext
  90. );
  91. DWORD
  92. QueryAuthType(
  93. VOID
  94. )
  95. {
  96. return m_dwAuthType;
  97. }
  98. private:
  99. DWORD m_dwAuthType;
  100. };
  101. #define SSPI_CREDENTIAL_SIGNATURE CREATE_SIGNATURE( 'SCCS' )
  102. #define SSPI_CREDENTIAL_FREE_SIGNATURE CREATE_SIGNATURE( 'fCCS' )
  103. class SSPI_CREDENTIAL
  104. {
  105. public:
  106. SSPI_CREDENTIAL()
  107. : m_dwSignature ( SSPI_CREDENTIAL_SIGNATURE ),
  108. m_strPackageName( m_rgPackageName, sizeof( m_rgPackageName ) ),
  109. m_cbMaxTokenLen ( 0 ),
  110. m_fSupportsEncoding( FALSE )
  111. {
  112. m_ListEntry.Flink = NULL;
  113. m_hCredHandle.dwLower = 0;
  114. m_hCredHandle.dwUpper = 0;
  115. }
  116. ~SSPI_CREDENTIAL()
  117. {
  118. DBG_ASSERT( CheckSignature() );
  119. FreeCredentialsHandle( &m_hCredHandle );
  120. m_dwSignature = SSPI_CREDENTIAL_FREE_SIGNATURE;
  121. }
  122. BOOL
  123. CheckSignature(
  124. VOID
  125. ) const
  126. {
  127. return m_dwSignature == SSPI_CREDENTIAL_SIGNATURE;
  128. }
  129. BOOL
  130. QuerySupportsEncoding(
  131. VOID
  132. ) const
  133. {
  134. return m_fSupportsEncoding;
  135. }
  136. STRA *
  137. QueryPackageName(
  138. VOID
  139. )
  140. {
  141. return &m_strPackageName;
  142. }
  143. DWORD
  144. QueryMaxTokenSize(
  145. VOID
  146. )
  147. {
  148. return m_cbMaxTokenLen;
  149. }
  150. CredHandle *
  151. QueryCredHandle(
  152. VOID
  153. )
  154. {
  155. return &m_hCredHandle;
  156. }
  157. static
  158. HRESULT
  159. Initialize(
  160. VOID
  161. );
  162. static
  163. VOID
  164. Terminate(
  165. VOID
  166. );
  167. HRESULT
  168. static
  169. GetCredential(
  170. CHAR * pszPackage,
  171. SSPI_CREDENTIAL ** ppCredential
  172. );
  173. private:
  174. DWORD m_dwSignature;
  175. CHAR m_rgPackageName[ 64 ];
  176. STRA m_strPackageName;
  177. LIST_ENTRY m_ListEntry;
  178. //
  179. // Handle to the server's credentials
  180. //
  181. CredHandle m_hCredHandle;
  182. //
  183. // Used for SSPI, max message token size
  184. //
  185. ULONG m_cbMaxTokenLen;
  186. //
  187. // Do we need to uudecode/encode buffers when dealing with this package
  188. //
  189. BOOL m_fSupportsEncoding;
  190. //
  191. // Global lock
  192. //
  193. static CRITICAL_SECTION sm_csCredentials;
  194. //
  195. // Global credential list
  196. //
  197. static LIST_ENTRY sm_CredentialListHead;
  198. };
  199. #define SSPI_CONTEXT_SIGNATURE CREATE_SIGNATURE( 'SXCS' )
  200. #define SSPI_CONTEXT_FREE_SIGNATURE CREATE_SIGNATURE( 'fXCS' )
  201. class SSPI_SECURITY_CONTEXT : public CONNECTION_AUTH_CONTEXT
  202. {
  203. public:
  204. SSPI_SECURITY_CONTEXT(
  205. SSPI_CREDENTIAL * pCredential
  206. )
  207. {
  208. DBG_ASSERT( pCredential != NULL );
  209. _pCredential = pCredential;
  210. SetSignature( SSPI_CONTEXT_SIGNATURE );
  211. _fIsComplete = FALSE;
  212. _fHaveAContext = FALSE;
  213. }
  214. virtual ~SSPI_SECURITY_CONTEXT()
  215. {
  216. SetSignature( SSPI_CONTEXT_FREE_SIGNATURE );
  217. if ( _fHaveAContext )
  218. {
  219. DeleteSecurityContext( &_hCtxtHandle );
  220. }
  221. }
  222. SSPI_CREDENTIAL *
  223. QueryCredentials(
  224. VOID
  225. )
  226. {
  227. return _pCredential;
  228. }
  229. CtxtHandle *
  230. QueryContextHandle(
  231. VOID
  232. )
  233. {
  234. return &_hCtxtHandle;
  235. }
  236. VOID
  237. SetContextHandle(
  238. CtxtHandle ctxtHandle
  239. )
  240. {
  241. _fHaveAContext = TRUE;
  242. _hCtxtHandle = ctxtHandle;
  243. }
  244. BOOL
  245. CheckSignature(
  246. VOID
  247. )
  248. {
  249. return QuerySignature() == SSPI_CONTEXT_SIGNATURE;
  250. }
  251. VOID *
  252. operator new(
  253. size_t size
  254. )
  255. {
  256. DBG_ASSERT( size == sizeof( SSPI_SECURITY_CONTEXT ) );
  257. DBG_ASSERT( sm_pachSSPISecContext != NULL );
  258. return sm_pachSSPISecContext->Alloc();
  259. }
  260. VOID
  261. operator delete(
  262. VOID * pSSPISecContext
  263. )
  264. {
  265. DBG_ASSERT( pSSPISecContext != NULL );
  266. DBG_ASSERT( sm_pachSSPISecContext != NULL );
  267. DBG_REQUIRE( sm_pachSSPISecContext->Free( pSSPISecContext ) );
  268. }
  269. virtual
  270. BOOL
  271. Cleanup(
  272. VOID
  273. )
  274. {
  275. delete this;
  276. return TRUE;
  277. }
  278. BOOL
  279. QueryIsComplete(
  280. VOID
  281. ) const
  282. {
  283. return _fIsComplete;
  284. }
  285. VOID
  286. SetIsComplete(
  287. BOOL fIsComplete
  288. )
  289. {
  290. _fIsComplete = fIsComplete;
  291. }
  292. VOID
  293. SetContextAttributes(
  294. ULONG ulContextAttributes
  295. )
  296. {
  297. _ulContextAttributes = ulContextAttributes;
  298. }
  299. ULONG
  300. QueryContextAttributes(
  301. VOID
  302. )
  303. {
  304. return _ulContextAttributes;
  305. }
  306. static
  307. HRESULT
  308. Initialize(
  309. VOID
  310. );
  311. static
  312. VOID
  313. Terminate(
  314. VOID
  315. );
  316. private:
  317. SSPI_CREDENTIAL * _pCredential;
  318. //
  319. // Do we have a context set? If so we should delete it on cleanup
  320. //
  321. BOOL _fHaveAContext;
  322. //
  323. // Is the handshake complete?
  324. //
  325. BOOL _fIsComplete;
  326. //
  327. // Handle to the partially formed context
  328. //
  329. CtxtHandle _hCtxtHandle;
  330. //
  331. // Attributes of the established context
  332. //
  333. ULONG _ulContextAttributes;
  334. //
  335. // Allocation cache for SSPI_SECURITY_CONTEXT
  336. //
  337. static ALLOC_CACHE_HANDLER * sm_pachSSPISecContext;
  338. };
  339. class SSPI_USER_CONTEXT : public W3_USER_CONTEXT
  340. {
  341. public:
  342. SSPI_USER_CONTEXT( AUTH_PROVIDER * pProvider )
  343. : W3_USER_CONTEXT( pProvider )
  344. {
  345. _fDelegatable = FALSE;
  346. _hImpersonationToken = NULL;
  347. _hPrimaryToken = NULL;
  348. _fSetAccountPwdExpiry = FALSE;
  349. _pSecurityContext = NULL;
  350. }
  351. virtual ~SSPI_USER_CONTEXT()
  352. {
  353. if ( _hImpersonationToken != NULL )
  354. {
  355. CloseHandle( _hImpersonationToken );
  356. _hImpersonationToken = NULL;
  357. }
  358. if ( _hPrimaryToken != NULL )
  359. {
  360. CloseHandle( _hPrimaryToken );
  361. _hPrimaryToken = NULL;
  362. }
  363. }
  364. HRESULT
  365. Create(
  366. SSPI_SECURITY_CONTEXT * pSecurityContext,
  367. W3_MAIN_CONTEXT * pMainContext
  368. );
  369. BOOL
  370. QueryDelegatable(
  371. VOID
  372. )
  373. {
  374. return _fDelegatable;
  375. }
  376. WCHAR *
  377. QueryUserName(
  378. VOID
  379. )
  380. {
  381. return _strUserName.QueryStr();
  382. }
  383. WCHAR *
  384. QueryPassword(
  385. VOID
  386. )
  387. {
  388. return L"";
  389. }
  390. DWORD
  391. QueryAuthType(
  392. VOID
  393. )
  394. {
  395. return QueryProvider()->QueryAuthType();
  396. }
  397. virtual
  398. HRESULT
  399. GetSspiInfo(
  400. BYTE * pCredHandle,
  401. DWORD cbCredHandle,
  402. BYTE * pCtxtHandle,
  403. DWORD cbCtxtHandle
  404. )
  405. {
  406. if ( cbCredHandle < sizeof( CredHandle ) ||
  407. cbCtxtHandle < sizeof( CtxtHandle ) )
  408. {
  409. return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
  410. }
  411. memcpy( pCredHandle,
  412. _pSecurityContext->QueryCredentials()->QueryCredHandle(),
  413. sizeof( CredHandle ) );
  414. memcpy( pCtxtHandle,
  415. _pSecurityContext->QueryContextHandle(),
  416. sizeof( CtxtHandle ) );
  417. return NO_ERROR;
  418. }
  419. HANDLE
  420. QueryImpersonationToken(
  421. VOID
  422. )
  423. {
  424. DBG_ASSERT( _hImpersonationToken != NULL );
  425. return _hImpersonationToken;
  426. }
  427. HANDLE
  428. QueryPrimaryToken(
  429. VOID
  430. );
  431. LARGE_INTEGER *
  432. QueryExpiry(
  433. VOID
  434. ) ;
  435. private:
  436. BOOL _fDelegatable;
  437. HANDLE _hImpersonationToken;
  438. HANDLE _hPrimaryToken;
  439. STRU _strUserName;
  440. STRA _strPackageName;
  441. DWORD _dwAuthType;
  442. LARGE_INTEGER _AccountPwdExpiry;
  443. BOOL _fSetAccountPwdExpiry;
  444. SSPI_SECURITY_CONTEXT * _pSecurityContext;
  445. };
  446. #endif