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.

616 lines
19 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. sspi.cxx
  5. Abstract:
  6. sspi
  7. Author:
  8. Larry Zhu (LZhu) January 1, 2002 Created
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include "sspi.hxx"
  16. #include "sspicli.hxx"
  17. #include "sspisrv.hxx"
  18. HRESULT
  19. DoSspiServerWork(
  20. IN PCtxtHandle phSrvCtxt,
  21. IN SOCKET ServerSocket,
  22. IN SOCKET ClientSocket
  23. )
  24. {
  25. THResult hRetval = E_FAIL;
  26. SecBufferDesc MessageDesc = {0};
  27. SecBuffer SecBuffers[3] = {0};
  28. CHAR DataBuffer[20] = {0};
  29. CHAR TokenBuffer[100] = {0};
  30. CHAR PaddingBlock[512] = {0};
  31. SecPkgContext_Sizes ContextSizes = {0};
  32. ULONG fQOP = 0;
  33. ULONG MessageSeqNo = 0;
  34. hRetval DBGCHK = QueryContextAttributesA(
  35. phSrvCtxt,
  36. SECPKG_ATTR_SIZES,
  37. &ContextSizes
  38. );
  39. if (SUCCEEDED(hRetval))
  40. {
  41. hRetval DBGCHK = ( (sizeof(TokenBuffer) >= ContextSizes.cbSecurityTrailer)
  42. && (sizeof(TokenBuffer) >= ContextSizes.cbMaxSignature)
  43. && (sizeof(PaddingBlock) >= ContextSizes.cbBlockSize) )
  44. ? S_OK : HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
  45. }
  46. if (SUCCEEDED(hRetval))
  47. {
  48. SecBuffers[0].pvBuffer = TokenBuffer;
  49. SecBuffers[0].cbBuffer = ContextSizes.cbMaxSignature;
  50. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  51. SecBuffers[1].pvBuffer = DataBuffer;
  52. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  53. SecBuffers[1].BufferType = SECBUFFER_DATA;
  54. #if 0
  55. SecBuffers[2].pvBuffer = PaddingBlock;
  56. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  57. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  58. #endif
  59. MessageDesc.pBuffers = SecBuffers;
  60. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - 1;
  61. MessageDesc.ulVersion = 0;
  62. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 1 (token)\n");
  63. hRetval DBGCHK = ReadMessage(ServerSocket,
  64. SecBuffers[0].cbBuffer,
  65. SecBuffers[0].pvBuffer,
  66. &SecBuffers[0].cbBuffer);
  67. }
  68. if (SUCCEEDED(hRetval))
  69. {
  70. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 2 (data)\n");
  71. hRetval DBGCHK = ReadMessage(ServerSocket,
  72. SecBuffers[1].cbBuffer,
  73. SecBuffers[1].pvBuffer,
  74. &SecBuffers[1].cbBuffer);
  75. }
  76. #if 0
  77. if (SUCCEEDED(hRetval))
  78. {
  79. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 3 (padding)\n");
  80. hRetval DBGCHK = ReadMessage(ServerSocket,
  81. SecBuffers[2].cbBuffer,
  82. SecBuffers[2].pvBuffer,
  83. &SecBuffers[2].cbBuffer);
  84. }
  85. #endif
  86. if (SUCCEEDED(hRetval))
  87. {
  88. DebugPrintf(SSPI_LOG, "DoSspiServerWork VerifySignature %#x\n", MessageSeqNo);
  89. hRetval DBGCHK = VerifySignature(
  90. phSrvCtxt,
  91. &MessageDesc,
  92. MessageSeqNo,
  93. &fQOP
  94. );
  95. }
  96. if (SUCCEEDED(hRetval))
  97. {
  98. SecBuffers[0].pvBuffer = TokenBuffer;
  99. SecBuffers[0].cbBuffer = ContextSizes.cbSecurityTrailer;
  100. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  101. SecBuffers[1].pvBuffer = DataBuffer;
  102. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  103. SecBuffers[1].BufferType = SECBUFFER_DATA;
  104. SecBuffers[2].pvBuffer = PaddingBlock;
  105. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  106. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  107. MessageDesc.pBuffers = SecBuffers;
  108. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - (ContextSizes.cbBlockSize > 1 ? 0 : 1);
  109. MessageDesc.ulVersion = 0;
  110. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 3 (token)\n");
  111. hRetval DBGCHK = ReadMessage(ServerSocket,
  112. SecBuffers[0].cbBuffer,
  113. SecBuffers[0].pvBuffer,
  114. &SecBuffers[0].cbBuffer);
  115. }
  116. if (SUCCEEDED(hRetval))
  117. {
  118. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 4 (data)\n");
  119. hRetval DBGCHK = ReadMessage(ServerSocket,
  120. SecBuffers[1].cbBuffer,
  121. SecBuffers[1].pvBuffer,
  122. &SecBuffers[1].cbBuffer);
  123. }
  124. if (SUCCEEDED(hRetval))
  125. {
  126. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 5 (padding)\n");
  127. hRetval DBGCHK = ReadMessage(ServerSocket,
  128. SecBuffers[2].cbBuffer,
  129. SecBuffers[2].pvBuffer,
  130. &SecBuffers[2].cbBuffer);
  131. }
  132. if (SUCCEEDED(hRetval))
  133. {
  134. DebugPrintf(SSPI_LOG, "DoSspiServerWork DecryptMessage %#x\n", MessageSeqNo + 1);
  135. hRetval DBGCHK = DecryptMessage(
  136. phSrvCtxt,
  137. &MessageDesc,
  138. ++MessageSeqNo,
  139. &fQOP
  140. );
  141. }
  142. if (SUCCEEDED(hRetval))
  143. {
  144. SecBuffers[0].pvBuffer = TokenBuffer;
  145. SecBuffers[0].cbBuffer = ContextSizes.cbSecurityTrailer;
  146. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  147. SecBuffers[1].pvBuffer = DataBuffer;
  148. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  149. SecBuffers[1].BufferType = SECBUFFER_DATA;
  150. SecBuffers[2].pvBuffer = PaddingBlock;
  151. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  152. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  153. MessageDesc.pBuffers = SecBuffers;
  154. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers);
  155. MessageDesc.ulVersion = 0;
  156. memset(
  157. DataBuffer,
  158. 0xeb,
  159. sizeof(DataBuffer)
  160. );
  161. MessageDesc.pBuffers = SecBuffers;
  162. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - (ContextSizes.cbBlockSize > 1 ? 0 : 1);
  163. MessageDesc.ulVersion = 0;
  164. DebugPrintf(SSPI_LOG, "DoSspiServerWork EncryptMessage %#x\n", MessageSeqNo + 1);
  165. hRetval DBGCHK = EncryptMessage(
  166. phSrvCtxt,
  167. fQOP,
  168. &MessageDesc,
  169. ++MessageSeqNo
  170. );
  171. }
  172. if (SUCCEEDED(hRetval))
  173. {
  174. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts writing 1 (token)\n");
  175. hRetval DBGCHK = WriteMessage(ClientSocket,
  176. SecBuffers[0].cbBuffer,
  177. SecBuffers[0].pvBuffer);
  178. }
  179. if (SUCCEEDED(hRetval))
  180. {
  181. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts writing 2 (data)\n");
  182. hRetval DBGCHK = WriteMessage(ClientSocket,
  183. SecBuffers[1].cbBuffer,
  184. SecBuffers[1].pvBuffer);
  185. }
  186. if (SUCCEEDED(hRetval))
  187. {
  188. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts reading 3 (padding)\n");
  189. hRetval DBGCHK = WriteMessage(ClientSocket,
  190. SecBuffers[2].cbBuffer,
  191. SecBuffers[2].pvBuffer);
  192. }
  193. if (SUCCEEDED(hRetval))
  194. {
  195. SecBuffers[0].pvBuffer = TokenBuffer;
  196. SecBuffers[0].cbBuffer = ContextSizes.cbMaxSignature;
  197. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  198. SecBuffers[1].pvBuffer = DataBuffer;
  199. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  200. SecBuffers[1].BufferType = SECBUFFER_DATA;
  201. #if 0
  202. SecBuffers[2].pvBuffer = PaddingBlock;
  203. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  204. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  205. #endif
  206. MessageDesc.pBuffers = SecBuffers;
  207. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - 1;
  208. MessageDesc.ulVersion = 0;
  209. memset(
  210. DataBuffer,
  211. 0xeb,
  212. sizeof(DataBuffer)
  213. );
  214. DebugPrintf(SSPI_LOG, "DoSspiServerWork MakeSignature %#x\n", MessageSeqNo + 1);
  215. hRetval DBGCHK = MakeSignature(
  216. phSrvCtxt,
  217. fQOP,
  218. &MessageDesc,
  219. ++MessageSeqNo
  220. );
  221. }
  222. if (SUCCEEDED(hRetval))
  223. {
  224. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts writing 4 (token)\n");
  225. hRetval DBGCHK = WriteMessage(ClientSocket,
  226. SecBuffers[0].cbBuffer,
  227. SecBuffers[0].pvBuffer);
  228. }
  229. if (SUCCEEDED(hRetval))
  230. {
  231. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts writing 5 (data)\n");
  232. hRetval DBGCHK = WriteMessage(ClientSocket,
  233. SecBuffers[1].cbBuffer,
  234. SecBuffers[1].pvBuffer);
  235. }
  236. #if 0
  237. if (SUCCEEDED(hRetval))
  238. {
  239. DebugPrintf(SSPI_LOG, "DoSspiServerWork starts writing 6 (padding)\n");
  240. hRetval DBGCHK = WriteMessage(ClientSocket,
  241. SecBuffers[2].cbBuffer,
  242. SecBuffers[2].pvBuffer);
  243. }
  244. #endif
  245. return hRetval;
  246. }
  247. HRESULT
  248. DoSspiClientWork(
  249. IN PCtxtHandle phCliCtxt,
  250. IN SOCKET ServerSocket,
  251. IN SOCKET ClientSocket
  252. )
  253. {
  254. THResult hRetval = E_FAIL;
  255. SecBufferDesc MessageDesc = {0};
  256. SecBuffer SecBuffers[3] = {0};
  257. CHAR DataBuffer[20] = {0};
  258. CHAR TokenBuffer[100] = {0};
  259. CHAR PaddingBlock[512] = {0};
  260. SecPkgContext_Sizes ContextSizes = {0};
  261. ULONG fQOP = 0;
  262. ULONG MessageSeqNo = 0;
  263. hRetval DBGCHK = QueryContextAttributesA(
  264. phCliCtxt,
  265. SECPKG_ATTR_SIZES,
  266. &ContextSizes
  267. );
  268. if (SUCCEEDED(hRetval))
  269. {
  270. hRetval DBGCHK = ( (sizeof(TokenBuffer) >= ContextSizes.cbSecurityTrailer)
  271. && (sizeof(TokenBuffer) >= ContextSizes.cbMaxSignature)
  272. && (sizeof(PaddingBlock) >= ContextSizes.cbBlockSize) )
  273. ? S_OK : HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
  274. }
  275. if (SUCCEEDED(hRetval))
  276. {
  277. SecBuffers[0].pvBuffer = TokenBuffer;
  278. SecBuffers[0].cbBuffer = ContextSizes.cbMaxSignature;
  279. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  280. SecBuffers[1].pvBuffer = DataBuffer;
  281. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  282. SecBuffers[1].BufferType = SECBUFFER_DATA;
  283. #if 0
  284. SecBuffers[2].pvBuffer = PaddingBlock;
  285. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  286. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  287. #endif
  288. MessageDesc.pBuffers = SecBuffers;
  289. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - 1;
  290. MessageDesc.ulVersion = 0;
  291. memset(
  292. DataBuffer,
  293. 0xeb,
  294. sizeof(DataBuffer)
  295. );
  296. DebugPrintf(SSPI_LOG, "DoSspiClientWork MakeSignature %#x\n", MessageSeqNo);
  297. hRetval DBGCHK = MakeSignature(
  298. phCliCtxt,
  299. fQOP,
  300. &MessageDesc,
  301. MessageSeqNo
  302. );
  303. }
  304. if (SUCCEEDED(hRetval))
  305. {
  306. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts writing 1 (token)\n");
  307. hRetval DBGCHK = WriteMessage(ServerSocket,
  308. SecBuffers[0].cbBuffer,
  309. SecBuffers[0].pvBuffer);
  310. }
  311. if (SUCCEEDED(hRetval))
  312. {
  313. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts writing 2 (data)\n");
  314. hRetval DBGCHK = WriteMessage(ServerSocket,
  315. SecBuffers[1].cbBuffer,
  316. SecBuffers[1].pvBuffer);
  317. }
  318. #if 0
  319. if (SUCCEEDED(hRetval))
  320. {
  321. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts writing 3 (padding)\n");
  322. hRetval DBGCHK = WriteMessage(ServerSocket,
  323. SecBuffers[2].cbBuffer,
  324. SecBuffers[2].pvBuffer);
  325. }
  326. #endif
  327. if (SUCCEEDED(hRetval))
  328. {
  329. SecBuffers[0].pvBuffer = TokenBuffer;
  330. SecBuffers[0].cbBuffer = ContextSizes.cbSecurityTrailer;
  331. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  332. SecBuffers[1].pvBuffer = DataBuffer;
  333. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  334. SecBuffers[1].BufferType = SECBUFFER_DATA;
  335. SecBuffers[2].pvBuffer = PaddingBlock;
  336. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  337. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  338. MessageDesc.pBuffers = SecBuffers;
  339. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - (ContextSizes.cbBlockSize > 1 ? 0 : 1);
  340. MessageDesc.ulVersion = 0;
  341. memset(
  342. DataBuffer,
  343. 0xeb,
  344. sizeof(DataBuffer)
  345. );
  346. DebugPrintf(SSPI_LOG, "DoSspiClientWork EncryptMessage %#x\n", MessageSeqNo + 1);
  347. hRetval DBGCHK = EncryptMessage(
  348. phCliCtxt,
  349. fQOP,
  350. &MessageDesc,
  351. ++MessageSeqNo
  352. );
  353. }
  354. if (SUCCEEDED(hRetval))
  355. {
  356. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts writing 3 (token)\n");
  357. hRetval DBGCHK = WriteMessage(ServerSocket,
  358. SecBuffers[0].cbBuffer,
  359. SecBuffers[0].pvBuffer);
  360. }
  361. if (SUCCEEDED(hRetval))
  362. {
  363. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts writing 4 (data)\n");
  364. hRetval DBGCHK = WriteMessage(ServerSocket,
  365. SecBuffers[1].cbBuffer,
  366. SecBuffers[1].pvBuffer);
  367. }
  368. if (SUCCEEDED(hRetval))
  369. {
  370. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts writing 5 (padding)\n");
  371. hRetval DBGCHK = WriteMessage(ServerSocket,
  372. SecBuffers[2].cbBuffer,
  373. SecBuffers[2].pvBuffer);
  374. }
  375. if (SUCCEEDED(hRetval))
  376. {
  377. SecBuffers[0].pvBuffer = TokenBuffer;
  378. SecBuffers[0].cbBuffer = ContextSizes.cbSecurityTrailer;
  379. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  380. SecBuffers[1].pvBuffer = DataBuffer;
  381. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  382. SecBuffers[1].BufferType = SECBUFFER_DATA;
  383. SecBuffers[2].pvBuffer = PaddingBlock;
  384. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  385. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  386. MessageDesc.pBuffers = SecBuffers;
  387. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - (ContextSizes.cbBlockSize > 1 ? 0 : 1);
  388. MessageDesc.ulVersion = 0;
  389. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts reading 1 (token)\n");
  390. hRetval DBGCHK = ReadMessage(ClientSocket,
  391. SecBuffers[0].cbBuffer,
  392. SecBuffers[0].pvBuffer,
  393. &SecBuffers[0].cbBuffer);
  394. }
  395. if (SUCCEEDED(hRetval))
  396. {
  397. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts reading 2 (data)\n");
  398. hRetval DBGCHK = ReadMessage(ClientSocket,
  399. SecBuffers[1].cbBuffer,
  400. SecBuffers[1].pvBuffer,
  401. &SecBuffers[1].cbBuffer);
  402. }
  403. if (SUCCEEDED(hRetval))
  404. {
  405. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts reading 3 (padding)\n");
  406. hRetval DBGCHK = ReadMessage(ClientSocket,
  407. SecBuffers[2].cbBuffer,
  408. SecBuffers[2].pvBuffer,
  409. &SecBuffers[2].cbBuffer);
  410. }
  411. if (SUCCEEDED(hRetval))
  412. {
  413. DebugPrintf(SSPI_LOG, "DoSspiClientWork DecryptMessage %#x\n", MessageSeqNo + 1);
  414. hRetval DBGCHK = DecryptMessage(
  415. phCliCtxt,
  416. &MessageDesc,
  417. ++MessageSeqNo,
  418. &fQOP
  419. );
  420. }
  421. if (SUCCEEDED(hRetval))
  422. {
  423. SecBuffers[0].pvBuffer = TokenBuffer;
  424. SecBuffers[0].cbBuffer = ContextSizes.cbMaxSignature;
  425. SecBuffers[0].BufferType = SECBUFFER_TOKEN;
  426. SecBuffers[1].pvBuffer = DataBuffer;
  427. SecBuffers[1].cbBuffer = sizeof(DataBuffer);
  428. SecBuffers[1].BufferType = SECBUFFER_DATA;
  429. #if 0
  430. SecBuffers[2].pvBuffer = PaddingBlock;
  431. SecBuffers[2].cbBuffer = ContextSizes.cbBlockSize > 1 ? ContextSizes.cbBlockSize : 0;
  432. SecBuffers[2].BufferType = SECBUFFER_PADDING;
  433. #endif
  434. MessageDesc.pBuffers = SecBuffers;
  435. MessageDesc.cBuffers = RTL_NUMBER_OF(SecBuffers) - 1;
  436. MessageDesc.ulVersion = 0;
  437. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts reading 4 (token)\n");
  438. hRetval DBGCHK = ReadMessage(ClientSocket,
  439. SecBuffers[0].cbBuffer,
  440. SecBuffers[0].pvBuffer,
  441. &SecBuffers[0].cbBuffer);
  442. }
  443. if (SUCCEEDED(hRetval))
  444. {
  445. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts reading 5 (data)\n");
  446. hRetval DBGCHK = ReadMessage(ClientSocket,
  447. SecBuffers[1].cbBuffer,
  448. SecBuffers[1].pvBuffer,
  449. &SecBuffers[1].cbBuffer);
  450. }
  451. #if 0
  452. if (SUCCEEDED(hRetval))
  453. {
  454. DebugPrintf(SSPI_LOG, "DoSspiClientWork starts reading 6 (padding)\n");
  455. hRetval DBGCHK = ReadMessage(ClientSocket,
  456. SecBuffers[2].cbBuffer,
  457. SecBuffers[2].pvBuffer,
  458. &SecBuffers[2].cbBuffer);
  459. }
  460. #endif
  461. if (SUCCEEDED(hRetval))
  462. {
  463. DebugPrintf(SSPI_LOG, "DoSspiClientWork VerifySignature %#x\n", MessageSeqNo + 1);
  464. hRetval DBGCHK = VerifySignature(
  465. phCliCtxt,
  466. &MessageDesc,
  467. ++MessageSeqNo,
  468. &fQOP
  469. );
  470. }
  471. return hRetval;
  472. }