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.

612 lines
17 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. tssec.hxx
  5. Abstract:
  6. This file declares security related classes and functions
  7. Author:
  8. Murali R. Krishnan ( MuraliK ) 11-Oct-1995
  9. Environment:
  10. Win32 User Mode
  11. Project:
  12. Internet Services Common DLL
  13. Revision History:
  14. --*/
  15. # ifndef _TSSEC_HXX_
  16. # define _TSSEC_HXX_
  17. /************************************************************
  18. * Include Headers
  19. ************************************************************/
  20. # define SECURITY_WIN32
  21. #include <sspi.h> // Security Support Provider APIs
  22. #include <schnlsp.h>
  23. #include <pudebug.h>
  24. // forward declaration
  25. class IIS_SERVER_INSTANCE;
  26. class IIS_SSL_INFO;
  27. /************************************************************
  28. * Type Definitions
  29. ************************************************************/
  30. typedef BOOL (WINAPI *SECURITY_CONTEXT_DELETE_FUNCTION)( CtxtHandle*, PVOID );
  31. //
  32. // Globals
  33. //
  34. extern HANDLE g_hProcessImpersonationToken;
  35. extern HANDLE g_hProcessPrimaryToken;
  36. extern BOOL g_fUseSingleToken;
  37. //
  38. // The TCP_AUTHENT_INFO structure is used as a shorthand to convey
  39. // a bunch of authentication related information to a few routines that
  40. // need it.
  41. //
  42. class TCP_AUTHENT_INFO
  43. {
  44. public:
  45. TCP_AUTHENT_INFO( VOID )
  46. : fDontUseAnonSubAuth( FALSE ),
  47. dwLogonMethod ( LOGON32_LOGON_INTERACTIVE ),
  48. cbAnonAcctDesc ( 0 )
  49. {
  50. }
  51. STR strAnonUserName;
  52. STR strAnonUserPassword;
  53. STR strDefaultLogonDomain;
  54. DWORD dwLogonMethod;
  55. BOOL fDontUseAnonSubAuth;
  56. //
  57. // Stores the anonymous account descriptor
  58. //
  59. BUFFER bAnonAcctDesc;
  60. DWORD cbAnonAcctDesc;
  61. };
  62. typedef TCP_AUTHENT_INFO * PTCP_AUTHENT_INFO;
  63. //
  64. // Security functions.
  65. //
  66. #define IIS_DNLEN 256
  67. #define MAX_ACCT_DESC_LEN (UNLEN+1+IIS_DNLEN+1+PWLEN+1)
  68. class CACHED_CREDENTIAL
  69. {
  70. public:
  71. CACHED_CREDENTIAL()
  72. {
  73. _ListEntry.Flink = NULL;
  74. _fHaveCredHandle = FALSE;
  75. }
  76. ~CACHED_CREDENTIAL();
  77. BOOL
  78. static CACHED_CREDENTIAL::GetCredential(
  79. LPSTR pszPackage,
  80. PIIS_SERVER_INSTANCE psi,
  81. PTCP_AUTHENT_INFO pTAI,
  82. CredHandle* prcred,
  83. ULONG* pcbMaxToken
  84. );
  85. LIST_ENTRY _ListEntry;
  86. private:
  87. STR _PackageName;
  88. STR _DefaultDomain;
  89. CredHandle _hcred;
  90. BOOL _fHaveCredHandle;
  91. ULONG _cbMaxToken; // Used for SSP, max message token size
  92. } ;
  93. class CACHED_TOKEN
  94. {
  95. public:
  96. CACHED_TOKEN( VOID )
  97. : _hToken( NULL ),
  98. _cRef ( 1 ),
  99. _TTL ( 2 ),
  100. m_hImpersonationToken( NULL ),
  101. m_fGuest ( FALSE ),
  102. m_dwLogonMethod ( 0 )
  103. {
  104. _ListEntry.Flink = NULL;
  105. _liExpiry.HighPart = 0x7fffffff;
  106. _liExpiry.LowPart = 0xffffffff;
  107. }
  108. ~CACHED_TOKEN( VOID )
  109. {
  110. if ( !g_fUseSingleToken )
  111. {
  112. DBG_ASSERT( _ListEntry.Flink == NULL );
  113. if ( m_hImpersonationToken) {
  114. DBG_REQUIRE( CloseHandle( m_hImpersonationToken ));
  115. m_hImpersonationToken = NULL;
  116. }
  117. if ( _hToken )
  118. {
  119. DBG_REQUIRE( CloseHandle( _hToken ) );
  120. _hToken = NULL;
  121. }
  122. }
  123. }
  124. static VOID Reference( CACHED_TOKEN * pct )
  125. {
  126. DBG_ASSERT( pct->_cRef > 0 );
  127. InterlockedIncrement( &pct->_cRef );
  128. }
  129. static VOID Dereference( CACHED_TOKEN * pct )
  130. {
  131. DBG_ASSERT( pct->_cRef > 0 );
  132. if ( !InterlockedDecrement( &pct->_cRef ) )
  133. {
  134. delete pct;
  135. }
  136. }
  137. HANDLE QueryImpersonationToken(VOID) const
  138. { return g_fUseSingleToken ? g_hProcessImpersonationToken : m_hImpersonationToken; }
  139. HANDLE QueryPrimaryToken(VOID) const
  140. { return g_fUseSingleToken ? g_hProcessImpersonationToken : m_hImpersonationToken; }
  141. VOID SetImpersonationToken(IN HANDLE hImpersonation)
  142. {
  143. DBG_ASSERT( m_hImpersonationToken == NULL);
  144. if ( g_fUseSingleToken )
  145. {
  146. DBG_ASSERT( FALSE );
  147. }
  148. else
  149. {
  150. m_hImpersonationToken = hImpersonation;
  151. }
  152. }
  153. VOID SetExpiry( LARGE_INTEGER* pE )
  154. {
  155. if ( g_fUseSingleToken )
  156. {
  157. DBG_ASSERT( FALSE );
  158. }
  159. if ( NULL != pE )
  160. {
  161. memcpy( &_liExpiry, pE, sizeof(LARGE_INTEGER) );
  162. }
  163. else
  164. {
  165. _liExpiry.HighPart = 0x7fffffff;
  166. _liExpiry.LowPart = 0xffffffff;
  167. }
  168. }
  169. LARGE_INTEGER* QueryExpiry() { return &_liExpiry; }
  170. BOOL IsGuest(VOID) const { return (m_fGuest); }
  171. VOID SetGuest(IN BOOL fGuest) { m_fGuest = fGuest; }
  172. HANDLE _hToken; // Must be first data member
  173. LIST_ENTRY _ListEntry;
  174. LONG _cRef;
  175. DWORD _TTL; // Gets decremented on each timeout, when zero,
  176. // remove this item from the cache
  177. BOOL m_fGuest; // Is this token a guest user?
  178. HANDLE m_hImpersonationToken;
  179. CHAR _achAcctDesc[MAX_ACCT_DESC_LEN];
  180. DWORD m_dwAcctDescLen;
  181. LARGE_INTEGER _liExpiry;
  182. DWORD m_dwLogonMethod;
  183. CHAR m_achUserName[ UNLEN ];
  184. CHAR m_achDomainName[ IIS_DNLEN ];
  185. };
  186. typedef CACHED_TOKEN* TS_TOKEN; // Choose an incompatible type so warnings
  187. // are produced
  188. ///////////////////////////////////////////////////////////////////////
  189. //
  190. // NT Authentication support
  191. //
  192. //////////////////////////////////////////////////////////////////////
  193. //
  194. // TCP Authenticator flags passed to init
  195. //
  196. #define TCPAUTH_SERVER 0x00000001 // This is the server side
  197. #define TCPAUTH_CLIENT 0x00000002 // This is the client side
  198. #define TCPAUTH_UUENCODE 0x00000004 // Input buffers are uudecoded,
  199. // output buffers are uuencoded
  200. #define TCPAUTH_BASE64 0x00000008 // uses base64 for uuenc/dec
  201. #define CRED_STATUS_INVALID_TIME 0x00001000
  202. #define CRED_STATUS_REVOKED 0x00002000
  203. class TCP_AUTHENT
  204. {
  205. public:
  206. dllexp TCP_AUTHENT( DWORD AuthFlags );
  207. dllexp ~TCP_AUTHENT();
  208. //
  209. // Server side only: For clients that pass clear text, the server should
  210. // authenticate with this method
  211. //
  212. dllexp BOOL ClearTextLogon( CHAR * pszUser,
  213. CHAR * pszPassword,
  214. BOOL * pfAsGuest,
  215. BOOL * pfAsAnonymous,
  216. IIS_SERVER_INSTANCE * pInstance,
  217. PTCP_AUTHENT_INFO pTAI,
  218. CHAR * pszWorkstation = NULL
  219. );
  220. #if 0
  221. //
  222. // Server side only : Digest logon
  223. //
  224. dllexp BOOL LogonDigestUser(
  225. PSTR pszUserName,
  226. PSTR pszRealm,
  227. PSTR pszUri,
  228. PSTR pszMethod,
  229. PSTR pszNonce,
  230. PSTR pszServerNonce,
  231. PSTR pszDigest,
  232. DWORD dwAlgo,
  233. LPTSVC_INFO psi
  234. );
  235. #endif
  236. //
  237. // Server side only: For filters that set access tokens
  238. //
  239. dllexp BOOL SetAccessToken( HANDLE hPrimaryToken,
  240. HANDLE hImpersonationToken
  241. );
  242. //
  243. // Client calls this first to get the negotiation message which
  244. // it then sends to the server. The server calls this with the
  245. // client result and sends back the result. The conversation
  246. // continues until *pcbBuffOut is zero and *pfNeedMoreData is FALSE.
  247. //
  248. // On the first call, pszPackage must point to the zero terminated
  249. // authentication package name to be used and pszUser and pszPassword
  250. // should point to the user name and password to authenticated with
  251. // on the client side (server side will always be NULL).
  252. //
  253. dllexp BOOL Converse( VOID * pBuffIn,
  254. DWORD cbBuffIn,
  255. BUFFER * pbuffOut,
  256. DWORD * pcbBuffOut,
  257. BOOL * pfNeedMoreData,
  258. PTCP_AUTHENT_INFO pTAI,
  259. CHAR * pszPackage = NULL,
  260. CHAR * pszUser = NULL,
  261. CHAR * pszPassword = NULL,
  262. PIIS_SERVER_INSTANCE psi = NULL );
  263. dllexp BOOL TCP_AUTHENT::ConverseEx(
  264. SecBufferDesc* pInSecBufDesc, // passed in by caller
  265. BUFFER * pDecodedBuffer, // passed in by caller
  266. BUFFER * pbuffOut,
  267. DWORD * pcbBuffOut,
  268. BOOL * pfNeedMoreData,
  269. PTCP_AUTHENT_INFO pTAI,
  270. CHAR * pszPackage,
  271. CHAR * pszUser,
  272. CHAR * pszPassword,
  273. PIIS_SERVER_INSTANCE psi
  274. );
  275. //
  276. // Server side only. Impersonates client after successful authentication
  277. //
  278. dllexp BOOL Impersonate( VOID );
  279. dllexp BOOL RevertToSelf( VOID );
  280. dllexp BOOL IsForwardable( VOID ) const;
  281. dllexp BOOL StartProcessAsUser( LPCSTR lpApplicationName,
  282. LPSTR lpCommandLine,
  283. BOOL bInheritHandles,
  284. DWORD dwCreationFlags,
  285. LPVOID lpEnvironment,
  286. LPCSTR lpCurrentDirectory,
  287. LPSTARTUPINFOA lpStartupInfo,
  288. LPPROCESS_INFORMATION lpProcessInformation
  289. );
  290. //
  291. // Gives the name of all authentication packages in a double null
  292. // terminated list. i.e.:
  293. //
  294. // NTLM\0
  295. // MSKerberos\0
  296. // \0
  297. //
  298. dllexp BOOL EnumAuthPackages( BUFFER * pBuff );
  299. //
  300. // Returns the user name associated with this context, not supported for
  301. // clear text
  302. //
  303. dllexp BOOL QueryUserName( STR * pBuff, BOOL fImpersonated = FALSE );
  304. dllexp BOOL QueryExpiry( PTimeStamp pExpiry );
  305. dllexp TS_TOKEN GetToken( VOID ) const
  306. { return _hToken; }
  307. //
  308. // Gets actual impersonation token handle
  309. //
  310. dllexp HANDLE QueryPrimaryToken( VOID );
  311. dllexp HANDLE QueryImpersonationToken( VOID );
  312. dllexp HANDLE GetUserHandle( VOID )
  313. { return QueryPrimaryToken(); }
  314. dllexp BOOL QueryFullyQualifiedUserName(
  315. LPSTR pszUser,
  316. STR * strU,
  317. IIS_SERVER_INSTANCE * psi,
  318. PTCP_AUTHENT_INFO pTAI
  319. );
  320. dllexp BOOL IsGuest( BOOL );
  321. dllexp BOOL Reset( BOOL fSessionReset = TRUE );
  322. dllexp CredHandle * QueryCredHandle( VOID )
  323. { return (_fHaveCredHandle ? &_hcred : NULL); }
  324. dllexp CtxtHandle * QueryCtxtHandle( VOID )
  325. { return (_fHaveCtxtHandle ? &_hctxt : NULL); }
  326. dllexp CtxtHandle * QuerySslCtxtHandle( VOID )
  327. { return _phSslCtxt; }
  328. dllexp BOOL SetSecurityContextToken( CtxtHandle* pCtxt,
  329. HANDLE hImpersonationToken,
  330. SECURITY_CONTEXT_DELETE_FUNCTION pFn,
  331. PVOID pArg,
  332. IIS_SSL_INFO *pSslInfo );
  333. dllexp BOOL IsSslCertPresent();
  334. dllexp BOOL DeleteCachedTokenOnReset( VOID );
  335. dllexp BOOL QueryCertificateIssuer( LPSTR ppIssuer, DWORD, LPBOOL );
  336. dllexp BOOL QueryCertificateSubject( LPSTR ppSubject, DWORD, LPBOOL );
  337. dllexp BOOL QueryCertificateFlags( LPDWORD pdwFlags, LPBOOL );
  338. dllexp BOOL QueryCertificateSerialNumber( LPBYTE* pSerialNumber, LPDWORD pdwLen, LPBOOL );
  339. dllexp BOOL QueryServerCertificateIssuer( LPSTR* ppIssuer, LPBOOL );
  340. dllexp BOOL QueryServerCertificateSubject( LPSTR* ppSubject, LPBOOL );
  341. dllexp BOOL QueryEncryptionKeySize( LPDWORD, LPBOOL );
  342. dllexp BOOL QueryEncryptionServerPrivateKeySize( LPDWORD, LPBOOL );
  343. dllexp BOOL
  344. GetClientCertBlob(
  345. IN DWORD cbAllocated,
  346. OUT DWORD * pdwCertEncodingType,
  347. OUT unsigned char * pbCertEncoded,
  348. OUT DWORD * pcbCertEncoded,
  349. OUT DWORD * pfCertificateVerified);
  350. dllexp BOOL UpdateClientCertFlags( DWORD dwFlags, LPBOOL pfCert, LPBYTE pbCa, DWORD dwCa );
  351. dllexp BOOL PackageSupportsEncoding( LPSTR pszPackage );
  352. dllexp BOOL SetTargetName( LPSTR pszTarget );
  353. private:
  354. BOOL QueryCertificateInfo( LPBOOL );
  355. BOOL QueryServerCertificateInfo( LPBOOL );
  356. protected:
  357. DWORD _fClient:1; // TRUE if client side, FALSE if SERVER side
  358. DWORD _fNewConversation:1; // Forces initialization params for client side
  359. DWORD _fUUEncodeData:1; // uuencode/decode input and output buffers
  360. DWORD _fClearText:1; // Use the Gina APIs rather then the SSP APIs
  361. DWORD _fHaveCredHandle:1; // _hcred contains a credential handle
  362. DWORD _fHaveCtxtHandle:1; // _hctxt contains a context handle
  363. DWORD _fBase64:1; // uses base64 for uuenc/dec
  364. DWORD _fKnownToBeGuest:1; // TRUE if SSPI flag access token as "Guest"
  365. DWORD _fHaveAccessTokens:1;// TRUE if access token set by caller
  366. DWORD _fHaveExpiry:1; // TRUE if clear text logon has pwd expiry time
  367. DWORD _fDelegate:1; // TRUE if security context forwardable
  368. DWORD _fCertCheckForRevocation:1; // TRUE if we should revocation check
  369. DWORD _fCertCheckCacheOnly:1; // TRUE if we should not go on wire
  370. TS_TOKEN _hToken; // Used for clear text
  371. CredHandle _hcred; // Used for SSP
  372. ULONG _cbMaxToken; // Used for SSP, max message token size
  373. HANDLE _hSSPToken; // Used for SSP, caches real token
  374. HANDLE _hSSPPrimaryToken;// Used for SSP, caches duplicated token
  375. CtxtHandle _hctxt; // Used for SSP
  376. SECURITY_CONTEXT_DELETE_FUNCTION _pDeleteFunction;
  377. PVOID _pDeleteArg;
  378. LARGE_INTEGER _liPwdExpiry;
  379. PCERT_CONTEXT _pClientCertContext;
  380. DWORD _dwX509Flags;
  381. IIS_SSL_INFO * _pSslInfo;
  382. PX509Certificate _pServerX509Certificate;
  383. DWORD _dwServerX509Flags;
  384. DWORD _dwServerBitsInKey;
  385. CtxtHandle * _phSslCtxt; // ptr to SSL sec context
  386. STR _strTarget;
  387. };
  388. DWORD
  389. InitializeSecurity(
  390. HINSTANCE hDll
  391. );
  392. VOID
  393. TerminateSecurity(
  394. VOID
  395. );
  396. dllexp
  397. BOOL
  398. TsImpersonateUser(
  399. TS_TOKEN hToken
  400. );
  401. dllexp
  402. HANDLE
  403. TsTokenToHandle(
  404. TS_TOKEN hToken
  405. );
  406. dllexp
  407. HANDLE
  408. TsTokenToImpHandle(
  409. TS_TOKEN hToken
  410. );
  411. dllexp
  412. DWORD
  413. TsApiAccessCheck(
  414. ACCESS_MASK maskDesiredAccess
  415. );
  416. dllexp
  417. BOOL
  418. TsDeleteUserToken(
  419. TS_TOKEN hToken
  420. );
  421. dllexp
  422. TS_TOKEN
  423. TsLogonUser(
  424. CHAR * pszUser,
  425. CHAR * pszPassword,
  426. BOOL * pfAsGuest,
  427. BOOL * pfAsAnonymous,
  428. IIS_SERVER_INSTANCE * pInstance,
  429. PTCP_AUTHENT_INFO pTAI,
  430. CHAR * pszWorkstation = NULL,
  431. LARGE_INTEGER * pExpiry = NULL,
  432. BOOL * pfExpiry = NULL
  433. );
  434. dllexp
  435. BOOL
  436. TsGetSecretW(
  437. WCHAR * pszSecretName,
  438. BUFFER * pbufSecret
  439. );
  440. dllexp
  441. DWORD
  442. TsSetSecretW(
  443. IN LPWSTR SecretName,
  444. IN LPWSTR pSecret,
  445. IN DWORD cbSecret
  446. );
  447. #ifdef CHICAGO
  448. dllexp
  449. BOOL
  450. TsIsUserLevelPresent(VOID);
  451. #endif
  452. dllexp
  453. BOOL
  454. uudecode(
  455. char * bufcoded,
  456. BUFFER * pbuffdecoded,
  457. DWORD * pcbDecoded = NULL,
  458. BOOL fBase64 = FALSE
  459. );
  460. dllexp
  461. BOOL
  462. uuencode(
  463. BYTE * pchData,
  464. DWORD cbData,
  465. BUFFER * pbuffEncoded,
  466. BOOL fBase64 = FALSE
  467. );
  468. dllexp
  469. BOOL
  470. BuildAnonymousAcctDesc(
  471. PTCP_AUTHENT_INFO pTAI
  472. );
  473. dllexp
  474. QuerySingleAccessToken(
  475. VOID
  476. );
  477. extern TS_TOKEN g_pctProcessToken;
  478. #include <tslogon.hxx>
  479. #include <wintrust.h>
  480. typedef
  481. LONG (WINAPI *PFN_WinVerifyTrust)(IN OPTIONAL HWND hwnd,
  482. IN GUID *pgActionID,
  483. IN LPVOID pWintrustData);
  484. extern HINSTANCE g_hWinTrust;
  485. extern PFN_WinVerifyTrust g_pfnWinVerifyTrust;
  486. #endif
  487. /************************ End of File ***********************/
  488.