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.

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