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.

384 lines
7.3 KiB

  1. #ifndef _IIS_DIGESTPROVIDER_HXX_
  2. #define _IIS_DIGESTPROVIDER_HXX_
  3. /*++
  4. Copyright (c) 2000 Microsoft Corporation
  5. Module Name :
  6. iisdigestprovider.hxx
  7. Abstract:
  8. IIS Digest authentication provider
  9. - version of Digest auth as implemented by IIS5 and IIS5.1
  10. Author:
  11. Jaroslad - based on code from md5filt 10-Nov-2000
  12. Environment:
  13. Win32 - User Mode
  14. Project:
  15. ULW3.DLL
  16. --*/
  17. //
  18. // Constants
  19. //
  20. //# of random bytes at beginning of nonce
  21. #define RANDOM_SIZE 8
  22. //size of timestamp in nonce
  23. #define TIMESTAMP_SIZE 12
  24. //MD5 hash size
  25. #define MD5_HASH_SIZE 16
  26. #define NONCE_SIZE ( 2*RANDOM_SIZE + TIMESTAMP_SIZE + 2*MD5_HASH_SIZE )
  27. #define NONCE_GRANULARITY 512
  28. #define DIGEST_AUTH "Digest"
  29. #define QOP_AUTH "auth"
  30. #define VALUE_NONE "none"
  31. // based on RFC2617 NC is defined as 8LHEX
  32. #define SIZE_OF_NC 8
  33. //
  34. // Class definitions
  35. //
  36. #define IIS_DIGEST_CONN_CONTEXT_SIGNATURE CREATE_SIGNATURE( 'DIGC' )
  37. #define IIS_DIGEST_CONN_CONTEXT_SIGNATURE_FREED CREATE_SIGNATURE( 'digX' )
  38. class IIS_DIGEST_CONN_CONTEXT: public CONNECTION_AUTH_CONTEXT
  39. {
  40. public:
  41. IIS_DIGEST_CONN_CONTEXT(
  42. VOID
  43. ):
  44. _fStale ( FALSE ),
  45. _tLastNonce ( 0 ),
  46. _straNonce ( __szNonce, sizeof(__szNonce))
  47. {
  48. SetSignature( IIS_DIGEST_CONN_CONTEXT_SIGNATURE );
  49. }
  50. virtual ~IIS_DIGEST_CONN_CONTEXT(
  51. VOID
  52. )
  53. {
  54. DBG_ASSERT( CheckSignature() );
  55. SetSignature( IIS_DIGEST_CONN_CONTEXT_SIGNATURE_FREED );
  56. }
  57. BOOL
  58. CheckSignature(
  59. VOID
  60. )
  61. { return( QuerySignature() == IIS_DIGEST_CONN_CONTEXT_SIGNATURE ); }
  62. VOID *
  63. operator new(
  64. size_t size
  65. )
  66. {
  67. DBG_ASSERT( size == sizeof( IIS_DIGEST_CONN_CONTEXT ) );
  68. DBG_ASSERT( sm_pachIISDIGESTConnContext != NULL );
  69. return sm_pachIISDIGESTConnContext->Alloc();
  70. }
  71. VOID
  72. operator delete(
  73. VOID * pDIGESTConnContext
  74. )
  75. {
  76. DBG_ASSERT( pDIGESTConnContext != NULL );
  77. DBG_ASSERT( sm_pachIISDIGESTConnContext != NULL );
  78. DBG_REQUIRE( sm_pachIISDIGESTConnContext->Free( pDIGESTConnContext ) );
  79. }
  80. BOOL
  81. Cleanup(
  82. VOID
  83. )
  84. {
  85. delete this;
  86. return TRUE;
  87. }
  88. static
  89. HRESULT
  90. Initialize(
  91. VOID
  92. );
  93. static
  94. VOID
  95. Terminate(
  96. VOID
  97. );
  98. VOID
  99. SetStale(
  100. IN BOOL fStale
  101. )
  102. {
  103. _fStale = fStale;
  104. }
  105. BOOL
  106. QueryStale(
  107. VOID
  108. )
  109. {
  110. return _fStale;
  111. }
  112. STRA&
  113. QueryNonce(
  114. VOID
  115. )
  116. {
  117. return _straNonce;
  118. }
  119. static
  120. HRESULT
  121. HashData(
  122. IN BUFFER& buffData,
  123. OUT BUFFER& buffHash
  124. );
  125. static
  126. BOOL
  127. IsExpiredNonce(
  128. IN STRA& strRequestNonce,
  129. IN STRA& strPresentNonce
  130. );
  131. static
  132. BOOL
  133. IsWellFormedNonce(
  134. IN STRA& strNonce
  135. );
  136. HRESULT
  137. GenerateNonce(
  138. VOID
  139. );
  140. static
  141. BOOL
  142. IIS_DIGEST_CONN_CONTEXT::ParseForName(
  143. IN PSTR pszStr,
  144. IN PSTR * pNameTable,
  145. IN UINT cNameTable,
  146. OUT PSTR * pValueTable
  147. );
  148. private:
  149. DWORD _dwSignature;
  150. // Is the nonce value stale
  151. BOOL _fStale;
  152. DWORD _tLastNonce;
  153. // buffer for _straNonce
  154. CHAR __szNonce[ NONCE_SIZE ];
  155. STRA _straNonce;
  156. static const PCHAR _pszSecret;
  157. static const DWORD _cchSecret;
  158. static HCRYPTPROV sm_hCryptProv;
  159. static ALLOC_CACHE_HANDLER * sm_pachIISDIGESTConnContext;
  160. };
  161. class IIS_DIGEST_AUTH_PROVIDER : public AUTH_PROVIDER
  162. {
  163. public:
  164. IIS_DIGEST_AUTH_PROVIDER(
  165. VOID
  166. )
  167. {
  168. }
  169. virtual ~IIS_DIGEST_AUTH_PROVIDER(
  170. VOID
  171. )
  172. {
  173. }
  174. HRESULT
  175. Initialize(
  176. DWORD dwInternalId
  177. );
  178. VOID
  179. Terminate(
  180. VOID
  181. );
  182. HRESULT
  183. DoesApply(
  184. IN W3_MAIN_CONTEXT * pMainContext,
  185. OUT BOOL * pfApplies
  186. );
  187. HRESULT
  188. DoAuthenticate(
  189. IN W3_MAIN_CONTEXT * pMainContext,
  190. OUT BOOL * pfFilterFinished
  191. );
  192. HRESULT
  193. OnAccessDenied(
  194. IN W3_MAIN_CONTEXT * pMainContext
  195. );
  196. HRESULT
  197. SetDigestHeader(
  198. IN W3_MAIN_CONTEXT * pMainContext,
  199. IN IIS_DIGEST_CONN_CONTEXT * pDigestConnContext
  200. );
  201. DWORD
  202. QueryAuthType(
  203. VOID
  204. )
  205. {
  206. return MD_AUTH_MD5;
  207. }
  208. static
  209. HRESULT
  210. GetDigestConnContext(
  211. IN W3_MAIN_CONTEXT * pMainContext,
  212. OUT IIS_DIGEST_CONN_CONTEXT ** ppDigestConnContext
  213. );
  214. static
  215. HRESULT
  216. GetLanGroupDomainName(
  217. OUT STRA& straDomain
  218. );
  219. static
  220. HRESULT
  221. BreakUserAndDomain(
  222. IN PCHAR pszFullName,
  223. OUT STRA& straDomainName,
  224. OUT STRA& straUserName
  225. );
  226. static
  227. STRA&
  228. QueryComputerDomain(
  229. VOID
  230. )
  231. {
  232. DBG_ASSERT( sm_pstraComputerDomain != NULL );
  233. return *sm_pstraComputerDomain;
  234. }
  235. private:
  236. static STRA * sm_pstraComputerDomain;
  237. };
  238. class IIS_DIGEST_USER_CONTEXT : public W3_USER_CONTEXT
  239. {
  240. public:
  241. IIS_DIGEST_USER_CONTEXT(
  242. AUTH_PROVIDER * pProvider
  243. ):
  244. W3_USER_CONTEXT( pProvider ),
  245. _hImpersonationToken( NULL ),
  246. _hPrimaryToken( NULL )
  247. {
  248. }
  249. virtual ~IIS_DIGEST_USER_CONTEXT(
  250. VOID
  251. )
  252. {
  253. if ( _hImpersonationToken != NULL )
  254. {
  255. CloseHandle( _hImpersonationToken );
  256. _hImpersonationToken = NULL;
  257. }
  258. if ( _hPrimaryToken != NULL )
  259. {
  260. CloseHandle( _hPrimaryToken );
  261. _hPrimaryToken = NULL;
  262. }
  263. }
  264. HRESULT
  265. Create(
  266. IN HANDLE hImpersonationToken,
  267. IN PSTR pszUserName
  268. );
  269. WCHAR *
  270. QueryUserName(
  271. VOID
  272. )
  273. {
  274. return _strUserName.QueryStr();
  275. }
  276. WCHAR *
  277. QueryRemoteUserName(
  278. VOID
  279. )
  280. {
  281. return _strUserName.QueryStr();
  282. }
  283. WCHAR *
  284. QueryPassword(
  285. VOID
  286. )
  287. {
  288. return L"";
  289. }
  290. DWORD
  291. QueryAuthType(
  292. VOID
  293. )
  294. {
  295. return MD_AUTH_MD5;
  296. }
  297. HANDLE
  298. QueryImpersonationToken(
  299. VOID
  300. )
  301. {
  302. DBG_ASSERT( _hImpersonationToken != NULL );
  303. return _hImpersonationToken;
  304. }
  305. HANDLE
  306. QueryPrimaryToken(
  307. VOID
  308. );
  309. private:
  310. HANDLE _hImpersonationToken;
  311. HANDLE _hPrimaryToken;
  312. STRU _strUserName;
  313. };
  314. #endif