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.

1046 lines
25 KiB

  1. /*--
  2. Copyright (c) 1987-1993 Microsoft Corporation
  3. Module Name:
  4. ssptest.c
  5. Abstract:
  6. Test program for the NtLmSsp service.
  7. Author:
  8. 28-Jun-1993 (cliffv)
  9. Environment:
  10. User mode only.
  11. Contains NT-specific code.
  12. Requires ANSI C extensions: slash-slash comments, long external names.
  13. Revision History:
  14. --*/
  15. #ifndef UNICODE
  16. #define UNICODE
  17. #endif // UNICODE
  18. #include <ntos.h>
  19. #include <ntlsa.h>
  20. #include <ntsam.h>
  21. #ifndef SECURITY_WIN32
  22. #define SECURITY_WIN32
  23. #endif // SECURITY_WIN32
  24. #define SECURITY_KERNEL
  25. #define SECURITY_PACKAGE
  26. #define SECURITY_KERBEROS
  27. #include <security.h>
  28. #include <zwapi.h>
  29. BOOLEAN QuietMode = FALSE;
  30. ULONG DoTests = FALSE;
  31. VOID
  32. DumpBuffer(
  33. PVOID Buffer,
  34. DWORD BufferSize
  35. )
  36. /*++
  37. Routine Description:
  38. Dumps the buffer content on to the debugger output.
  39. Arguments:
  40. Buffer: buffer pointer.
  41. BufferSize: size of the buffer.
  42. Return Value:
  43. none
  44. --*/
  45. {
  46. #define NUM_CHARS 16
  47. DWORD i, limit;
  48. CHAR TextBuffer[NUM_CHARS + 1];
  49. PUCHAR BufferPtr = Buffer;
  50. DbgPrint("------------------------------------\n");
  51. //
  52. // Hex dump of the bytes
  53. //
  54. limit = ((BufferSize - 1) / NUM_CHARS + 1) * NUM_CHARS;
  55. for (i = 0; i < limit; i++) {
  56. if (i < BufferSize) {
  57. DbgPrint("%02x ", BufferPtr[i]);
  58. if (BufferPtr[i] < 31 ) {
  59. TextBuffer[i % NUM_CHARS] = '.';
  60. } else if (BufferPtr[i] == '\0') {
  61. TextBuffer[i % NUM_CHARS] = ' ';
  62. } else {
  63. TextBuffer[i % NUM_CHARS] = (CHAR) BufferPtr[i];
  64. }
  65. } else {
  66. DbgPrint(" ");
  67. TextBuffer[i % NUM_CHARS] = ' ';
  68. }
  69. if ((i + 1) % NUM_CHARS == 0) {
  70. TextBuffer[NUM_CHARS] = 0;
  71. DbgPrint(" %s\n", TextBuffer);
  72. }
  73. }
  74. DbgPrint("------------------------------------\n");
  75. }
  76. VOID
  77. PrintTime(
  78. LPSTR Comment,
  79. TimeStamp ConvertTime
  80. )
  81. /*++
  82. Routine Description:
  83. Print the specified time
  84. Arguments:
  85. Comment - Comment to print in front of the time
  86. Time - Local time to print
  87. Return Value:
  88. None
  89. --*/
  90. {
  91. LARGE_INTEGER LocalTime;
  92. LocalTime.HighPart = ConvertTime.HighPart;
  93. LocalTime.LowPart = ConvertTime.LowPart;
  94. DbgPrint( "%s", Comment );
  95. //
  96. // If the time is infinite,
  97. // just say so.
  98. //
  99. if ( LocalTime.HighPart == 0x7FFFFFFF && LocalTime.LowPart == 0xFFFFFFFF ) {
  100. DbgPrint( "Infinite\n" );
  101. //
  102. // Otherwise print it more clearly
  103. //
  104. } else {
  105. TIME_FIELDS TimeFields;
  106. RtlTimeToTimeFields( &LocalTime, &TimeFields );
  107. DbgPrint( "%ld/%ld/%ld %ld:%2.2ld:%2.2ld\n",
  108. TimeFields.Month,
  109. TimeFields.Day,
  110. TimeFields.Year,
  111. TimeFields.Hour,
  112. TimeFields.Minute,
  113. TimeFields.Second );
  114. }
  115. }
  116. VOID
  117. PrintStatus(
  118. ULONG NetStatus
  119. )
  120. /*++
  121. Routine Description:
  122. Print a net status code.
  123. Arguments:
  124. NetStatus - The net status code to print.
  125. Return Value:
  126. None
  127. --*/
  128. {
  129. DbgPrint( "Status = %lu 0x%lx", NetStatus, NetStatus );
  130. switch (NetStatus) {
  131. case SEC_E_NO_SPM:
  132. DbgPrint( " SEC_E_NO_SPM" );
  133. break;
  134. case SEC_E_BAD_PKGID:
  135. DbgPrint( " SEC_E_BAD_PKGID" ); break;
  136. case SEC_E_NOT_OWNER:
  137. DbgPrint( " SEC_E_NOT_OWNER" ); break;
  138. case SEC_E_CANNOT_INSTALL:
  139. DbgPrint( " SEC_E_CANNOT_INSTALL" ); break;
  140. case SEC_E_INVALID_TOKEN:
  141. DbgPrint( " SEC_E_INVALID_TOKEN" ); break;
  142. case SEC_E_CANNOT_PACK:
  143. DbgPrint( " SEC_E_CANNOT_PACK" ); break;
  144. case SEC_E_QOP_NOT_SUPPORTED:
  145. DbgPrint( " SEC_E_QOP_NOT_SUPPORTED" ); break;
  146. case SEC_E_NO_IMPERSONATION:
  147. DbgPrint( " SEC_E_NO_IMPERSONATION" ); break;
  148. case SEC_E_LOGON_DENIED:
  149. DbgPrint( " SEC_E_LOGON_DENIED" ); break;
  150. case SEC_E_UNKNOWN_CREDENTIALS:
  151. DbgPrint( " SEC_E_UNKNOWN_CREDENTIALS" ); break;
  152. case SEC_E_NO_CREDENTIALS:
  153. DbgPrint( " SEC_E_NO_CREDENTIALS" ); break;
  154. case SEC_E_MESSAGE_ALTERED:
  155. DbgPrint( " SEC_E_MESSAGE_ALTERED" ); break;
  156. case SEC_E_OUT_OF_SEQUENCE:
  157. DbgPrint( " SEC_E_OUT_OF_SEQUENCE" ); break;
  158. case SEC_E_INSUFFICIENT_MEMORY:
  159. DbgPrint( " SEC_E_INSUFFICIENT_MEMORY" ); break;
  160. case SEC_E_INVALID_HANDLE:
  161. DbgPrint( " SEC_E_INVALID_HANDLE" ); break;
  162. case SEC_E_NOT_SUPPORTED:
  163. DbgPrint( " SEC_E_NOT_SUPPORTED" ); break;
  164. }
  165. DbgPrint( "\n" );
  166. }
  167. //+-------------------------------------------------------------------------
  168. //
  169. // Function: SecAllocate
  170. //
  171. // Synopsis:
  172. //
  173. // Effects:
  174. //
  175. // Arguments:
  176. //
  177. // Requires:
  178. //
  179. // Returns:
  180. //
  181. // Notes:
  182. //
  183. //
  184. //--------------------------------------------------------------------------
  185. VOID * SEC_ENTRY
  186. SspAlloc(ULONG Flags, ULONG cbMemory)
  187. {
  188. NTSTATUS scRet;
  189. PVOID Buffer = NULL;
  190. scRet = ZwAllocateVirtualMemory(
  191. NtCurrentProcess(),
  192. &Buffer,
  193. 0L,
  194. &cbMemory,
  195. MEM_COMMIT,
  196. PAGE_READWRITE
  197. );
  198. if (!NT_SUCCESS(scRet))
  199. {
  200. return(NULL);
  201. }
  202. return(Buffer);
  203. UNREFERENCED_PARAMETER(Flags);
  204. }
  205. //+-------------------------------------------------------------------------
  206. //
  207. // Function: SecFree
  208. //
  209. // Synopsis:
  210. //
  211. // Effects:
  212. //
  213. // Arguments:
  214. //
  215. // Requires:
  216. //
  217. // Returns:
  218. //
  219. // Notes:
  220. //
  221. //
  222. //--------------------------------------------------------------------------
  223. void SEC_ENTRY
  224. SspFree(PVOID pvMemory)
  225. {
  226. ULONG Length = 0;
  227. (VOID) ZwFreeVirtualMemory(
  228. NtCurrentProcess(),
  229. &pvMemory,
  230. &Length,
  231. MEM_RELEASE
  232. );
  233. }
  234. VOID
  235. TestSspRoutine(
  236. )
  237. /*++
  238. Routine Description:
  239. Test base SSP functionality
  240. Arguments:
  241. None
  242. Return Value:
  243. None
  244. --*/
  245. {
  246. SECURITY_STATUS SecStatus;
  247. SECURITY_STATUS AcceptStatus;
  248. SECURITY_STATUS InitStatus;
  249. CredHandle ServerCredHandle;
  250. CredHandle ClientCredentialHandle;
  251. CtxtHandle ClientContextHandle;
  252. CtxtHandle ServerContextHandle;
  253. TimeStamp Lifetime;
  254. ULONG ContextAttributes;
  255. ULONG PackageCount, Index;
  256. PSecPkgInfo PackageInfo = NULL;
  257. HANDLE Token = NULL;
  258. static int Calls;
  259. ULONG ClientFlags;
  260. ULONG ServerFlags;
  261. LPWSTR DomainName = NULL;
  262. LPWSTR UserName = NULL;
  263. LPWSTR TargetName = NULL;
  264. UNICODE_STRING TargetString;
  265. UNICODE_STRING PackageName;
  266. SecBufferDesc NegotiateDesc;
  267. SecBuffer NegotiateBuffer;
  268. SecBufferDesc ChallengeDesc;
  269. SecBuffer ChallengeBuffer;
  270. SecBufferDesc AuthenticateDesc;
  271. SecBuffer AuthenticateBuffer;
  272. SecPkgContext_Sizes ContextSizes;
  273. SecPkgContext_Names ContextNames;
  274. SecPkgContext_Lifespan ContextLifespan;
  275. PSecPkgCredentials_Names CredNames;
  276. SecBufferDesc SignMessage;
  277. SecBuffer SigBuffers[2];
  278. UCHAR bDataBuffer[20];
  279. UCHAR bSigBuffer[100];
  280. //
  281. // Allow tests to be disabled
  282. //
  283. if (!DoTests)
  284. {
  285. return;
  286. }
  287. NegotiateBuffer.pvBuffer = NULL;
  288. ChallengeBuffer.pvBuffer = NULL;
  289. AuthenticateBuffer.pvBuffer = NULL;
  290. SigBuffers[1].pvBuffer = bSigBuffer;
  291. SigBuffers[1].cbBuffer = sizeof(bSigBuffer);
  292. SigBuffers[1].BufferType = SECBUFFER_TOKEN;
  293. SigBuffers[0].pvBuffer = bDataBuffer;
  294. SigBuffers[0].cbBuffer = sizeof(bDataBuffer);
  295. SigBuffers[0].BufferType = SECBUFFER_DATA;
  296. memset(bDataBuffer,0xeb,sizeof(bDataBuffer));
  297. SignMessage.pBuffers = SigBuffers;
  298. SignMessage.cBuffers = 2;
  299. SignMessage.ulVersion = 0;
  300. DomainName = L"makalu";
  301. UserName = L"mikesw";
  302. PackageName.Buffer = (LPWSTR) SspAlloc(0,100);
  303. if (PackageName.Buffer == NULL)
  304. {
  305. return;
  306. }
  307. wcscpy(
  308. PackageName.Buffer,
  309. L"Kerberos"
  310. );
  311. RtlInitUnicodeString(
  312. &PackageName,
  313. PackageName.Buffer
  314. );
  315. //
  316. // Get info about the security packages.
  317. //
  318. SecStatus = EnumerateSecurityPackages( &PackageCount, &PackageInfo );
  319. if ( SecStatus != STATUS_SUCCESS ) {
  320. DbgPrint( "EnumerateSecurityPackages failed:" );
  321. PrintStatus( SecStatus );
  322. return;
  323. }
  324. DbgPrint( "PackageCount: %ld\n", PackageCount );
  325. for (Index = 0; Index < PackageCount ; Index++ )
  326. {
  327. DbgPrint( "Package %d:\n",Index);
  328. DbgPrint( "Name: %ws Comment: %ws\n", PackageInfo[Index].Name, PackageInfo[Index].Comment );
  329. DbgPrint( "Cap: %ld Version: %ld RPCid: %ld MaxToken: %ld\n\n",
  330. PackageInfo[Index].fCapabilities,
  331. PackageInfo[Index].wVersion,
  332. PackageInfo[Index].wRPCID,
  333. PackageInfo[Index].cbMaxToken );
  334. }
  335. #ifdef notdef
  336. //
  337. // Get info about the security packages.
  338. //
  339. SecStatus = QuerySecurityPackageInfo( &PackageName, &PackageInfo );
  340. if ( SecStatus != STATUS_SUCCESS ) {
  341. DbgPrint( "QuerySecurityPackageInfo failed:" );
  342. PrintStatus( SecStatus );
  343. return;
  344. }
  345. if ( !QuietMode ) {
  346. DbgPrint( "Name: %ws Comment: %ws\n", PackageInfo->Name, PackageInfo->Comment );
  347. DbgPrint( "Cap: %ld Version: %ld RPCid: %ld MaxToken: %ld\n\n",
  348. PackageInfo->fCapabilities,
  349. PackageInfo->wVersion,
  350. PackageInfo->wRPCID,
  351. PackageInfo->cbMaxToken );
  352. }
  353. FreeContextBuffer(PackageInfo);
  354. #endif
  355. //
  356. // Acquire a credential handle for the server side
  357. //
  358. SecStatus = AcquireCredentialsHandle(
  359. NULL, // New principal
  360. &PackageName, // Package Name
  361. SECPKG_CRED_INBOUND,
  362. NULL,
  363. NULL,
  364. NULL,
  365. NULL,
  366. &ServerCredHandle,
  367. &Lifetime );
  368. if ( SecStatus != STATUS_SUCCESS ) {
  369. DbgPrint( "AcquireCredentialsHandle failed: ");
  370. PrintStatus( SecStatus );
  371. return;
  372. }
  373. if ( !QuietMode ) {
  374. DbgPrint( "ServerCredHandle: 0x%lx 0x%lx ",
  375. ServerCredHandle.dwLower, ServerCredHandle.dwUpper );
  376. PrintTime( "Lifetime: ", Lifetime );
  377. }
  378. //
  379. // Acquire a credential handle for the client side
  380. //
  381. SecStatus = AcquireCredentialsHandle(
  382. NULL, // New principal
  383. &PackageName, // Package Name
  384. SECPKG_CRED_OUTBOUND,
  385. NULL,
  386. NULL,
  387. NULL,
  388. NULL,
  389. &ClientCredentialHandle,
  390. &Lifetime );
  391. if ( SecStatus != STATUS_SUCCESS ) {
  392. DbgPrint( "AcquireCredentialsHandle failed: " );
  393. PrintStatus( SecStatus );
  394. return;
  395. }
  396. if ( !QuietMode ) {
  397. DbgPrint( "ClientCredentialHandle: 0x%lx 0x%lx ",
  398. ClientCredentialHandle.dwLower, ClientCredentialHandle.dwUpper );
  399. PrintTime( "Lifetime: ", Lifetime );
  400. }
  401. //
  402. // Query some cred attributes
  403. //
  404. CredNames = SspAlloc(0, sizeof(*CredNames));
  405. if (CredNames == NULL)
  406. {
  407. DbgPrint("Failed to allocate CredNames\n");
  408. return;
  409. }
  410. SecStatus = QueryCredentialsAttributes(
  411. &ClientCredentialHandle,
  412. SECPKG_CRED_ATTR_NAMES,
  413. CredNames );
  414. if ( SecStatus != STATUS_SUCCESS ) {
  415. DbgPrint( "QueryCredentialsAttributes (Client) (names): " );
  416. PrintStatus( SecStatus );
  417. if ( !NT_SUCCESS(SecStatus) ) {
  418. return;
  419. }
  420. }
  421. DbgPrint("Client credential names: %ws\n",CredNames->sUserName);
  422. FreeContextBuffer(CredNames->sUserName);
  423. //
  424. // Do the same for the client
  425. //
  426. SecStatus = QueryCredentialsAttributes(
  427. &ServerCredHandle,
  428. SECPKG_CRED_ATTR_NAMES,
  429. CredNames );
  430. if ( SecStatus != STATUS_SUCCESS ) {
  431. DbgPrint( "QueryCredentialsAttributes (Server) (names): " );
  432. PrintStatus( SecStatus );
  433. if ( !NT_SUCCESS(SecStatus) ) {
  434. return;
  435. }
  436. }
  437. DbgPrint("Server credential names: %ws\n",CredNames->sUserName);
  438. FreeContextBuffer(CredNames->sUserName);
  439. SspFree(CredNames);
  440. //
  441. // Get the NegotiateMessage (ClientSide)
  442. //
  443. NegotiateDesc.ulVersion = 0;
  444. NegotiateDesc.cBuffers = 1;
  445. NegotiateDesc.pBuffers = &NegotiateBuffer;
  446. NegotiateBuffer.cbBuffer = PackageInfo->cbMaxToken;
  447. NegotiateBuffer.BufferType = SECBUFFER_TOKEN;
  448. NegotiateBuffer.pvBuffer = SspAlloc( 0, NegotiateBuffer.cbBuffer );
  449. if ( NegotiateBuffer.pvBuffer == NULL ) {
  450. DbgPrint( "Allocate NegotiateMessage failed\n" );
  451. return;
  452. }
  453. ClientFlags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_MUTUAL_AUTH; // | ISC_REQ_USE_DCE_STYLE | ISC_REQ_DATAGRAM; // | ISC_REQ_DELEGATE;
  454. TargetName = (LPWSTR) SspAlloc(0,100);
  455. if (TargetName == NULL)
  456. {
  457. return;
  458. }
  459. wcscpy(
  460. TargetName,
  461. DomainName
  462. );
  463. wcscat(
  464. TargetName,
  465. L"\\"
  466. );
  467. wcscat(
  468. TargetName,
  469. UserName
  470. );
  471. RtlInitUnicodeString(
  472. &TargetString,
  473. TargetName
  474. );
  475. InitStatus = InitializeSecurityContext(
  476. &ClientCredentialHandle,
  477. NULL, // No Client context yet
  478. &TargetString, // Faked target name
  479. ClientFlags,
  480. 0, // Reserved 1
  481. SECURITY_NATIVE_DREP,
  482. NULL, // No initial input token
  483. 0, // Reserved 2
  484. &ClientContextHandle,
  485. &NegotiateDesc,
  486. &ContextAttributes,
  487. &Lifetime );
  488. if ( InitStatus != STATUS_SUCCESS ) {
  489. if ( !QuietMode || !NT_SUCCESS(InitStatus) ) {
  490. DbgPrint( "InitializeSecurityContext (negotiate): " );
  491. PrintStatus( InitStatus );
  492. }
  493. if ( !NT_SUCCESS(InitStatus) ) {
  494. return;
  495. }
  496. }
  497. if ( !QuietMode ) {
  498. DbgPrint( "\n\nNegotiate Message:\n" );
  499. DbgPrint( "ClientContextHandle: 0x%lx 0x%lx Attributes: 0x%lx ",
  500. ClientContextHandle.dwLower, ClientContextHandle.dwUpper,
  501. ContextAttributes );
  502. PrintTime( "Lifetime: ", Lifetime );
  503. // DumpBuffer( NegotiateBuffer.pvBuffer, NegotiateBuffer.cbBuffer );
  504. }
  505. //
  506. // Get the ChallengeMessage (ServerSide)
  507. //
  508. NegotiateBuffer.BufferType |= SECBUFFER_READONLY;
  509. ChallengeDesc.ulVersion = 0;
  510. ChallengeDesc.cBuffers = 1;
  511. ChallengeDesc.pBuffers = &ChallengeBuffer;
  512. ChallengeBuffer.cbBuffer = PackageInfo->cbMaxToken;
  513. ChallengeBuffer.BufferType = SECBUFFER_TOKEN;
  514. ChallengeBuffer.pvBuffer = SspAlloc( 0, ChallengeBuffer.cbBuffer );
  515. if ( ChallengeBuffer.pvBuffer == NULL ) {
  516. DbgPrint( "Allocate ChallengeMessage failed\n");
  517. return;
  518. }
  519. ServerFlags = 0;
  520. AcceptStatus = AcceptSecurityContext(
  521. &ServerCredHandle,
  522. NULL, // No Server context yet
  523. &NegotiateDesc,
  524. ServerFlags,
  525. SECURITY_NATIVE_DREP,
  526. &ServerContextHandle,
  527. &ChallengeDesc,
  528. &ContextAttributes,
  529. &Lifetime );
  530. if ( AcceptStatus != STATUS_SUCCESS ) {
  531. if ( !QuietMode || !NT_SUCCESS(AcceptStatus) ) {
  532. DbgPrint( "AcceptSecurityContext (Challenge): " );
  533. PrintStatus( AcceptStatus );
  534. }
  535. if ( !NT_SUCCESS(AcceptStatus) ) {
  536. return;
  537. }
  538. }
  539. if ( !QuietMode ) {
  540. DbgPrint( "\n\nChallenge Message:\n" );
  541. DbgPrint( "ServerContextHandle: 0x%lx 0x%lx Attributes: 0x%lx ",
  542. ServerContextHandle.dwLower, ServerContextHandle.dwUpper,
  543. ContextAttributes );
  544. PrintTime( "Lifetime: ", Lifetime );
  545. // DumpBuffer( ChallengeBuffer.pvBuffer, ChallengeBuffer.cbBuffer );
  546. }
  547. if (InitStatus != STATUS_SUCCESS)
  548. {
  549. //
  550. // Get the AuthenticateMessage (ClientSide)
  551. //
  552. ChallengeBuffer.BufferType |= SECBUFFER_READONLY;
  553. AuthenticateDesc.ulVersion = 0;
  554. AuthenticateDesc.cBuffers = 1;
  555. AuthenticateDesc.pBuffers = &AuthenticateBuffer;
  556. AuthenticateBuffer.cbBuffer = PackageInfo->cbMaxToken;
  557. AuthenticateBuffer.BufferType = SECBUFFER_TOKEN;
  558. AuthenticateBuffer.pvBuffer = SspAlloc( 0, AuthenticateBuffer.cbBuffer );
  559. if ( AuthenticateBuffer.pvBuffer == NULL ) {
  560. DbgPrint( "Allocate AuthenticateMessage failed: \n" );
  561. return;
  562. }
  563. SecStatus = InitializeSecurityContext(
  564. NULL,
  565. &ClientContextHandle,
  566. NULL,
  567. 0,
  568. 0, // Reserved 1
  569. SECURITY_NATIVE_DREP,
  570. &ChallengeDesc,
  571. 0, // Reserved 2
  572. &ClientContextHandle,
  573. &AuthenticateDesc,
  574. &ContextAttributes,
  575. &Lifetime );
  576. if ( SecStatus != STATUS_SUCCESS ) {
  577. DbgPrint( "InitializeSecurityContext (Authenticate): " );
  578. PrintStatus( SecStatus );
  579. if ( !NT_SUCCESS(SecStatus) ) {
  580. return;
  581. }
  582. }
  583. if ( !QuietMode ) {
  584. DbgPrint( "\n\nAuthenticate Message:\n" );
  585. DbgPrint( "ClientContextHandle: 0x%lx 0x%lx Attributes: 0x%lx ",
  586. ClientContextHandle.dwLower, ClientContextHandle.dwUpper,
  587. ContextAttributes );
  588. PrintTime( "Lifetime: ", Lifetime );
  589. // DumpBuffer( AuthenticateBuffer.pvBuffer, AuthenticateBuffer.cbBuffer );
  590. }
  591. if (AcceptStatus != STATUS_SUCCESS)
  592. {
  593. //
  594. // Finally authenticate the user (ServerSide)
  595. //
  596. AuthenticateBuffer.BufferType |= SECBUFFER_READONLY;
  597. SecStatus = AcceptSecurityContext(
  598. NULL,
  599. &ServerContextHandle,
  600. &AuthenticateDesc,
  601. 0,
  602. SECURITY_NATIVE_DREP,
  603. &ServerContextHandle,
  604. NULL,
  605. &ContextAttributes,
  606. &Lifetime );
  607. if ( SecStatus != STATUS_SUCCESS ) {
  608. DbgPrint( "AcceptSecurityContext (Challenge): " );
  609. PrintStatus( SecStatus );
  610. if ( !NT_SUCCESS(SecStatus) ) {
  611. return;
  612. }
  613. }
  614. if ( !QuietMode ) {
  615. DbgPrint( "\n\nFinal Authentication:\n" );
  616. DbgPrint( "ServerContextHandle: 0x%lx 0x%lx Attributes: 0x%lx ",
  617. ServerContextHandle.dwLower, ServerContextHandle.dwUpper,
  618. ContextAttributes );
  619. PrintTime( "Lifetime: ", Lifetime );
  620. DbgPrint(" \n" );
  621. }
  622. }
  623. }
  624. SecStatus = QueryContextAttributes(
  625. &ServerContextHandle,
  626. SECPKG_ATTR_NAMES,
  627. &ContextNames );
  628. if ( SecStatus != STATUS_SUCCESS ) {
  629. DbgPrint( "QueryContextAttributes (Server) (names): " );
  630. PrintStatus( SecStatus );
  631. if ( !NT_SUCCESS(SecStatus) ) {
  632. return;
  633. }
  634. }
  635. DbgPrint("Server Context names: %ws\n",ContextNames.sUserName);
  636. FreeContextBuffer(ContextNames.sUserName);
  637. //
  638. // Impersonate the client (ServerSide)
  639. //
  640. SecStatus = ImpersonateSecurityContext( &ServerContextHandle );
  641. if ( SecStatus != STATUS_SUCCESS ) {
  642. DbgPrint( "ImpersonateSecurityContext: " );
  643. PrintStatus( SecStatus );
  644. if ( !NT_SUCCESS(SecStatus) ) {
  645. return;
  646. }
  647. }
  648. SecStatus = RevertSecurityContext( &ServerContextHandle );
  649. if ( SecStatus != STATUS_SUCCESS ) {
  650. DbgPrint( "RevertSecurityContext: " );
  651. PrintStatus( SecStatus );
  652. if ( !NT_SUCCESS(SecStatus) ) {
  653. return;
  654. }
  655. }
  656. #ifdef notdef
  657. //
  658. // Impersonate the client manually
  659. //
  660. SecStatus = QuerySecurityContextToken( &ServerContextHandle,&Token );
  661. if ( SecStatus != STATUS_SUCCESS ) {
  662. DbgPrint( "ImpersonateSecurityContext: " );
  663. PrintStatus( SecStatus );
  664. if ( !NT_SUCCESS(SecStatus) ) {
  665. return;
  666. }
  667. }
  668. NtClose(Token);
  669. #endif
  670. #ifdef notdef
  671. //
  672. // Sign a message
  673. //
  674. SecStatus = MakeSignature(
  675. &ClientContextHandle,
  676. 0,
  677. &SignMessage,
  678. 0 );
  679. if ( SecStatus != STATUS_SUCCESS ) {
  680. DbgPrint( "MakeSignature: " );
  681. PrintStatus( SecStatus );
  682. if ( !NT_SUCCESS(SecStatus) ) {
  683. return;
  684. }
  685. }
  686. if ( !QuietMode ) {
  687. DbgPrint("\n Signature: \n");
  688. // DumpBuffer(SigBuffers[1].pvBuffer,SigBuffers[1].cbBuffer);
  689. }
  690. //
  691. // Verify the signature
  692. //
  693. SecStatus = VerifySignature(
  694. &ServerContextHandle,
  695. &SignMessage,
  696. 0,
  697. 0 );
  698. if ( SecStatus != STATUS_SUCCESS ) {
  699. DbgPrint( "VerifySignature: " );
  700. PrintStatus( SecStatus );
  701. if ( !NT_SUCCESS(SecStatus) ) {
  702. return;
  703. }
  704. }
  705. //
  706. // Sign a message, this time to check if it can detect a change in the
  707. // message
  708. //
  709. SecStatus = MakeSignature(
  710. &ClientContextHandle,
  711. 0,
  712. &SignMessage,
  713. 0 );
  714. if ( SecStatus != STATUS_SUCCESS ) {
  715. DbgPrint( "MakeSignature: " );
  716. PrintStatus( SecStatus );
  717. if ( !NT_SUCCESS(SecStatus) ) {
  718. return;
  719. }
  720. }
  721. if ( !QuietMode ) {
  722. DbgPrint("\n Signature: \n");
  723. // DumpBuffer(SigBuffers[1].pvBuffer,SigBuffers[1].cbBuffer);
  724. }
  725. //
  726. // Mess up the message to see if VerifySignature works
  727. //
  728. bDataBuffer[10] = 0xec;
  729. //
  730. // Verify the signature
  731. //
  732. SecStatus = VerifySignature(
  733. &ServerContextHandle,
  734. &SignMessage,
  735. 0,
  736. 0 );
  737. if ( SecStatus != SEC_E_MESSAGE_ALTERED ) {
  738. DbgPrint( "VerifySignature: " );
  739. PrintStatus( SecStatus );
  740. if ( !NT_SUCCESS(SecStatus) ) {
  741. return;
  742. }
  743. }
  744. #endif
  745. //
  746. // Delete both contexts.
  747. //
  748. SecStatus = DeleteSecurityContext( &ClientContextHandle );
  749. if ( SecStatus != STATUS_SUCCESS ) {
  750. DbgPrint( "DeleteSecurityContext failed: " );
  751. PrintStatus( SecStatus );
  752. return;
  753. }
  754. SecStatus = DeleteSecurityContext( &ServerContextHandle );
  755. if ( SecStatus != STATUS_SUCCESS ) {
  756. DbgPrint( "DeleteSecurityContext failed: " );
  757. PrintStatus( SecStatus );
  758. return;
  759. }
  760. //
  761. // Free both credential handles
  762. //
  763. SecStatus = FreeCredentialsHandle( &ServerCredHandle );
  764. if ( SecStatus != STATUS_SUCCESS ) {
  765. DbgPrint( "FreeCredentialsHandle failed: " );
  766. PrintStatus( SecStatus );
  767. return;
  768. }
  769. SecStatus = FreeCredentialsHandle( &ClientCredentialHandle );
  770. if ( SecStatus != STATUS_SUCCESS ) {
  771. DbgPrint( "FreeCredentialsHandle failed: " );
  772. PrintStatus( SecStatus );
  773. return;
  774. }
  775. //
  776. // Final Cleanup
  777. //
  778. if (PackageInfo != NULL)
  779. {
  780. FreeContextBuffer(PackageInfo);
  781. }
  782. if (PackageName.Buffer != NULL)
  783. {
  784. SspFree(PackageName.Buffer);
  785. }
  786. if ( NegotiateBuffer.pvBuffer != NULL ) {
  787. (VOID) SspFree( NegotiateBuffer.pvBuffer );
  788. }
  789. if ( ChallengeBuffer.pvBuffer != NULL ) {
  790. (VOID) SspFree( ChallengeBuffer.pvBuffer );
  791. }
  792. if ( AuthenticateBuffer.pvBuffer != NULL ) {
  793. (VOID) SspFree( AuthenticateBuffer.pvBuffer );
  794. }
  795. if (TargetName != NULL)
  796. {
  797. SspFree(TargetName);
  798. }
  799. }