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.

367 lines
6.5 KiB

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