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.

2177 lines
62 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. stub.c
  5. Abstract:
  6. NT LM Security Support Provider client stubs.
  7. Author:
  8. Cliff Van Dyke (CliffV) 29-Jun-1993
  9. Environment: User Mode
  10. Revision History:
  11. --*/
  12. #ifdef BLDR_KERNEL_RUNTIME
  13. #include <bootdefs.h>
  14. #endif
  15. #ifdef WIN
  16. #include <windows.h>
  17. #include <ctype.h>
  18. #endif
  19. #include <security.h>
  20. #include <ntlmsspi.h>
  21. #include <crypt.h>
  22. #include <ntlmssp.h>
  23. #include <cred.h>
  24. #include <debug.h>
  25. #include <string.h>
  26. #include <memory.h>
  27. #include <cache.h>
  28. #include <rpc.h>
  29. #include <md5.h>
  30. #include <context.h>
  31. #include <stdio.h>
  32. #include "crc32.h"
  33. BOOL
  34. __loadds
  35. GetPassword(
  36. PSSP_CREDENTIAL Credential,
  37. int NeverPrompt
  38. )
  39. {
  40. #ifdef BL_USE_LM_PASSWORD
  41. if ((Credential->LmPassword != NULL) && (Credential->NtPassword != NULL)) {
  42. return (TRUE);
  43. }
  44. #else
  45. if (Credential->NtPassword != NULL) {
  46. return (TRUE);
  47. }
  48. #endif
  49. if (CacheGetPassword(Credential) == TRUE) {
  50. return (TRUE);
  51. }
  52. return (FALSE);
  53. }
  54. SECURITY_STATUS SEC_ENTRY
  55. QuerySecurityPackageInfo(
  56. IN SEC_CHAR SEC_FAR * PackageName,
  57. OUT PSecPkgInfo SEC_FAR *PackageInfo
  58. )
  59. /*++
  60. Routine Description:
  61. This API is intended to provide basic information about Security
  62. Packages themselves. This information will include the bounds on sizes
  63. of authentication information, credentials and contexts.
  64. ?? This is a local routine rather than the real API call since the API
  65. call has a bad interface that neither allows me to allocate the
  66. buffer nor tells me how big the buffer is. Perhaps when the real API
  67. is fixed, I'll make this the real API.
  68. Arguments:
  69. PackageName - Name of the package being queried.
  70. PackageInfo - Returns a pointer to an allocated block describing the
  71. security package. The allocated block must be freed using
  72. FreeContextBuffer.
  73. Return Value:
  74. SEC_E_OK -- Call completed successfully
  75. SEC_E_PACKAGE_UNKNOWN -- Package being queried is not this package
  76. SEC_E_INSUFFICIENT_MEMORY -- Not enough memory
  77. --*/
  78. {
  79. SEC_CHAR *Where;
  80. //
  81. // Ensure the correct package name was passed in.
  82. //
  83. if ( _fstrcmp( PackageName, NTLMSP_NAME ) != 0 ) {
  84. return SEC_E_PACKAGE_UNKNOWN;
  85. }
  86. //
  87. // Allocate a buffer for the PackageInfo
  88. //
  89. *PackageInfo = (PSecPkgInfo) SspAlloc (sizeof(SecPkgInfo) +
  90. sizeof(NTLMSP_NAME) +
  91. sizeof(NTLMSP_COMMENT) );
  92. if ( *PackageInfo == NULL ) {
  93. return SEC_E_INSUFFICIENT_MEMORY;
  94. }
  95. //
  96. // Fill in the information.
  97. //
  98. (*PackageInfo)->fCapabilities = NTLMSP_CAPABILITIES;
  99. (*PackageInfo)->wVersion = NTLMSP_VERSION;
  100. (*PackageInfo)->wRPCID = RPC_C_AUTHN_WINNT;
  101. (*PackageInfo)->cbMaxToken = NTLMSP_MAX_TOKEN_SIZE;
  102. Where = (SEC_CHAR *)((*PackageInfo)+1);
  103. (*PackageInfo)->Name = Where;
  104. _fstrcpy( Where, NTLMSP_NAME);
  105. Where += _fstrlen(Where) + 1;
  106. (*PackageInfo)->Comment = Where;
  107. _fstrcpy( Where, NTLMSP_COMMENT);
  108. Where += _fstrlen(Where) + 1;
  109. return SEC_E_OK;
  110. }
  111. SECURITY_STATUS SEC_ENTRY
  112. EnumerateSecurityPackages(
  113. OUT PULONG PackageCount,
  114. OUT PSecPkgInfo * PackageInfo
  115. )
  116. /*++
  117. Routine Description:
  118. This API returns a list of Security Packages available to client (i.e.
  119. those that are either loaded or can be loaded on demand). The caller
  120. must free the returned buffer with FreeContextBuffer. This API returns
  121. a list of all the security packages available to a service. The names
  122. returned can then be used to acquire credential handles, as well as
  123. determine which package in the system best satisfies the requirements
  124. of the caller. It is assumed that all available packages can be
  125. included in the single call.
  126. This is really a dummy API that just returns information about this
  127. security package. It is provided to ensure this security package has the
  128. same interface as the multiplexer DLL does.
  129. Arguments:
  130. PackageCount - Returns the number of packages supported.
  131. PackageInfo - Returns an allocate array of structures
  132. describing the security packages. The array must be freed
  133. using FreeContextBuffer.
  134. Return Value:
  135. SEC_E_OK -- Call completed successfully
  136. SEC_E_PACKAGE_UNKNOWN -- Package being queried is not this package
  137. SEC_E_INSUFFICIENT_MEMORY -- Not enough memory
  138. --*/
  139. {
  140. SECURITY_STATUS SecStatus;
  141. //
  142. // Get the information for this package.
  143. //
  144. SecStatus = QuerySecurityPackageInfo( NTLMSP_NAME,
  145. PackageInfo );
  146. if ( SecStatus != SEC_E_OK ) {
  147. return SecStatus;
  148. }
  149. *PackageCount = 1;
  150. return (SEC_E_OK);
  151. }
  152. SECURITY_STATUS SEC_ENTRY
  153. AcquireCredentialsHandle(
  154. IN SEC_CHAR * PrincipalName,
  155. IN SEC_CHAR * PackageName,
  156. IN ULONG CredentialUseFlags,
  157. IN PLUID LogonId,
  158. IN PVOID AuthData,
  159. IN SEC_GET_KEY_FN GetKeyFunction,
  160. IN PVOID GetKeyArgument,
  161. OUT PCredHandle CredentialHandle,
  162. OUT PTimeStamp Lifetime
  163. )
  164. /*++
  165. Routine Description:
  166. This API allows applications to acquire a handle to pre-existing
  167. credentials associated with the user on whose behalf the call is made
  168. i.e. under the identity this application is running. These pre-existing
  169. credentials have been established through a system logon not described
  170. here. Note that this is different from "login to the network" and does
  171. not imply gathering of credentials.
  172. Note for DOS we will ignore the previous note. On DOS we will gather
  173. logon credentials through the AuthData parameter.
  174. This API returns a handle to the credentials of a principal (user, client)
  175. as used by a specific security package. This handle can then be used
  176. in subsequent calls to the Context APIs. This API will not let a
  177. process obtain a handle to credentials that are not related to the
  178. process; i.e. we won't allow a process to grab the credentials of
  179. another user logged into the same machine. There is no way for us
  180. to determine if a process is a trojan horse or not, if it is executed
  181. by the user.
  182. Arguments:
  183. PrincipalName - Name of the principal for whose credentials the handle
  184. will reference. Note, if the process requesting the handle does
  185. not have access to the credentials, an error will be returned.
  186. A null string indicates that the process wants a handle to the
  187. credentials of the user under whose security it is executing.
  188. PackageName - Name of the package with which these credentials will
  189. be used.
  190. CredentialUseFlags - Flags indicating the way with which these
  191. credentials will be used.
  192. #define CRED_INBOUND 0x00000001
  193. #define CRED_OUTBOUND 0x00000002
  194. #define CRED_BOTH 0x00000003
  195. #define CRED_OWF_PASSWORD 0x00000010
  196. The credentials created with CRED_INBOUND option can only be used
  197. for (validating incoming calls and can not be used for making accesses.
  198. CRED_OWF_PASSWORD means that the password in AuthData has already
  199. been through the OWF function.
  200. LogonId - Pointer to NT style Logon Id which is a LUID. (Provided for
  201. file system ; processes such as network redirectors.)
  202. CredentialHandle - Returned credential handle.
  203. Lifetime - Time that these credentials expire. The value returned in
  204. this field depends on the security package.
  205. Return Value:
  206. STATUS_SUCCESS -- Call completed successfully
  207. SEC_E_NO_SPM -- Security Support Provider is not running
  208. SEC_E_PACKAGE_UNKNOWN -- Package being queried is not this package
  209. SEC_E_PRINCIPAL_UNKNOWN -- No such principal
  210. SEC_E_NOT_OWNER -- caller does not own the specified credentials
  211. SEC_E_INSUFFICIENT_MEMORY -- Not enough memory
  212. --*/
  213. {
  214. SECURITY_STATUS SecStatus;
  215. PSSP_CREDENTIAL Credential = NULL;
  216. #ifdef DEBUGRPC_DETAIL
  217. SspPrint(( SSP_API, "SspAcquireCredentialHandle Entered\n" ));
  218. #endif
  219. //
  220. // Validate the arguments
  221. //
  222. if ( _fstrcmp( PackageName, NTLMSP_NAME ) != 0 ) {
  223. return (SEC_E_PACKAGE_UNKNOWN);
  224. }
  225. if ( (CredentialUseFlags & SECPKG_CRED_OUTBOUND) &&
  226. ARGUMENT_PRESENT(PrincipalName) && *PrincipalName != L'\0' ) {
  227. return (SEC_E_PRINCIPAL_UNKNOWN);
  228. }
  229. if ( ARGUMENT_PRESENT(LogonId) ) {
  230. return (SEC_E_PRINCIPAL_UNKNOWN);
  231. }
  232. if ( ARGUMENT_PRESENT(GetKeyFunction) ) {
  233. return (SEC_E_PRINCIPAL_UNKNOWN);
  234. }
  235. if ( ARGUMENT_PRESENT(GetKeyArgument) ) {
  236. return (SEC_E_PRINCIPAL_UNKNOWN);
  237. }
  238. //
  239. // Ensure at least one Credential use bit is set.
  240. //
  241. if ( (CredentialUseFlags & (SECPKG_CRED_INBOUND|SECPKG_CRED_OUTBOUND)) == 0 ) {
  242. SspPrint(( SSP_API,
  243. "SspAcquireCredentialHandle: invalid credential use.\n" ));
  244. SecStatus = SEC_E_INVALID_CREDENTIAL_USE;
  245. goto Cleanup;
  246. }
  247. //
  248. // Allocate a credential block and initialize it.
  249. //
  250. Credential = SspCredentialAllocateCredential(CredentialUseFlags);
  251. if ( Credential == NULL ) {
  252. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  253. goto Cleanup;
  254. }
  255. SecStatus = CacheSetCredentials( AuthData, Credential );
  256. if (SecStatus != SEC_E_OK)
  257. goto Cleanup;
  258. //
  259. // Return output parameters to the caller.
  260. //
  261. CredentialHandle->dwUpper = (ULONG_PTR)Credential;
  262. CredentialHandle->dwLower = 0;
  263. Lifetime->HighPart = 0;
  264. Lifetime->LowPart = 0xffffffffL;
  265. SecStatus = SEC_E_OK;
  266. //
  267. // Free and locally used resources.
  268. //
  269. Cleanup:
  270. if ( SecStatus != SEC_E_OK ) {
  271. if ( Credential != NULL ) {
  272. SspFree( Credential );
  273. }
  274. }
  275. #ifdef DEBUGRPC_DETAIL
  276. SspPrint(( SSP_API, "SspAcquireCredentialHandle returns 0x%x\n", SecStatus ));
  277. #endif
  278. return SecStatus;
  279. }
  280. SECURITY_STATUS SEC_ENTRY
  281. FreeCredentialsHandle(
  282. IN PCredHandle CredentialHandle
  283. )
  284. /*++
  285. Routine Description:
  286. This API is used to notify the security system that the credentials are
  287. no longer needed and allows the application to free the handle acquired
  288. in the call described above. When all references to this credential
  289. set has been removed then the credentials may themselves be removed.
  290. Arguments:
  291. CredentialHandle - Credential Handle obtained through
  292. AcquireCredentialHandle.
  293. Return Value:
  294. STATUS_SUCCESS -- Call completed successfully
  295. SEC_E_NO_SPM -- Security Support Provider is not running
  296. SEC_E_INVALID_HANDLE -- Credential Handle is invalid
  297. --*/
  298. {
  299. SECURITY_STATUS SecStatus;
  300. PSSP_CREDENTIAL Credential;
  301. //
  302. // Initialization
  303. //
  304. #ifdef DEBUGRPC_DETAIL
  305. SspPrint(( SSP_API, "SspFreeCredentialHandle Entered\n" ));
  306. #endif
  307. //
  308. // Find the referenced credential and delink it.
  309. //
  310. Credential = SspCredentialReferenceCredential(CredentialHandle, TRUE);
  311. if ( Credential == NULL ) {
  312. SecStatus = SEC_E_INVALID_HANDLE;
  313. goto Cleanup;
  314. }
  315. SspCredentialDereferenceCredential( Credential );
  316. SspCredentialDereferenceCredential( Credential );
  317. SecStatus = SEC_E_OK;
  318. Cleanup:
  319. #ifdef DEBUGRPC_DETAIL
  320. SspPrint(( SSP_API, "SspFreeCredentialHandle returns 0x%x\n", SecStatus ));
  321. #endif
  322. return SecStatus;
  323. }
  324. BOOLEAN
  325. SspGetTokenBuffer(
  326. IN PSecBufferDesc TokenDescriptor OPTIONAL,
  327. OUT PVOID * TokenBuffer,
  328. OUT PULONG * TokenSize,
  329. IN BOOLEAN ReadonlyOK
  330. )
  331. /*++
  332. Routine Description:
  333. This routine parses a Token Descriptor and pulls out the useful
  334. information.
  335. Arguments:
  336. TokenDescriptor - Descriptor of the buffer containing (or to contain) the
  337. token. If not specified, TokenBuffer and TokenSize will be returned
  338. as NULL.
  339. TokenBuffer - Returns a pointer to the buffer for the token.
  340. TokenSize - Returns a pointer to the location of the size of the buffer.
  341. ReadonlyOK - TRUE if the token buffer may be readonly.
  342. Return Value:
  343. TRUE - If token buffer was properly found.
  344. --*/
  345. {
  346. ULONG i;
  347. //
  348. // If there is no TokenDescriptor passed in,
  349. // just pass out NULL to our caller.
  350. //
  351. if ( !ARGUMENT_PRESENT( TokenDescriptor) ) {
  352. *TokenBuffer = NULL;
  353. *TokenSize = NULL;
  354. return TRUE;
  355. }
  356. //
  357. // Check the version of the descriptor.
  358. //
  359. if ( TokenDescriptor->ulVersion != 0 ) {
  360. return FALSE;
  361. }
  362. //
  363. // Loop through each described buffer.
  364. //
  365. for ( i=0; i<TokenDescriptor->cBuffers ; i++ ) {
  366. PSecBuffer Buffer = &TokenDescriptor->pBuffers[i];
  367. if ( (Buffer->BufferType & (~SECBUFFER_READONLY)) == SECBUFFER_TOKEN ) {
  368. //
  369. // If the buffer is readonly and readonly isn't OK,
  370. // reject the buffer.
  371. //
  372. if ( !ReadonlyOK && (Buffer->BufferType & SECBUFFER_READONLY) ) {
  373. return FALSE;
  374. }
  375. //
  376. // Return the requested information
  377. //
  378. *TokenBuffer = Buffer->pvBuffer;
  379. *TokenSize = &Buffer->cbBuffer;
  380. return TRUE;
  381. }
  382. }
  383. return FALSE;
  384. }
  385. SECURITY_STATUS
  386. SspHandleFirstCall(
  387. IN PCredHandle CredentialHandle,
  388. IN OUT PCtxtHandle ContextHandle,
  389. IN ULONG ContextReqFlags,
  390. IN ULONG QoPFlags,
  391. IN ULONG InputTokenSize,
  392. IN PVOID InputToken,
  393. IN OUT PULONG OutputTokenSize,
  394. OUT PVOID OutputToken,
  395. OUT PULONG ContextAttributes,
  396. OUT PTimeStamp ExpirationTime
  397. )
  398. /*++
  399. Routine Description:
  400. Handle the First Call part of InitializeSecurityContext.
  401. Arguments:
  402. QoPFlags - Indicates security configuration
  403. All other arguments same as for InitializeSecurityContext
  404. Return Value:
  405. STATUS_SUCCESS -- All OK
  406. SEC_I_CALLBACK_NEEDED -- Caller should call again later
  407. SEC_E_INVALID_HANDLE -- Credential/Context Handle is invalid
  408. SEC_E_BUFFER_TOO_SMALL -- Buffer for output token isn't big enough
  409. SEC_E_INSUFFICIENT_MEMORY -- Not enough memory
  410. --*/
  411. {
  412. SECURITY_STATUS SecStatus;
  413. PSSP_CONTEXT Context = NULL;
  414. PSSP_CREDENTIAL Credential = NULL;
  415. NEGOTIATE_MESSAGE NegotiateMessage;
  416. //
  417. // Initialization
  418. //
  419. *ContextAttributes = 0;
  420. //
  421. // Get a pointer to the credential
  422. //
  423. Credential = SspCredentialReferenceCredential(
  424. CredentialHandle,
  425. FALSE );
  426. if ( Credential == NULL ) {
  427. SspPrint(( SSP_API,
  428. "SspHandleFirstCall: invalid credential handle.\n" ));
  429. SecStatus = SEC_E_INVALID_HANDLE;
  430. goto Cleanup;
  431. }
  432. if ( (Credential->CredentialUseFlags & SECPKG_CRED_OUTBOUND) == 0 ) {
  433. SspPrint(( SSP_API, "SspHandleFirstCall: invalid credential use.\n" ));
  434. SecStatus = SEC_E_INVALID_CREDENTIAL_USE;
  435. goto Cleanup;
  436. }
  437. //
  438. // Allocate a new context
  439. //
  440. Context = SspContextAllocateContext();
  441. if ( Context == NULL ) {
  442. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  443. goto Cleanup;
  444. }
  445. //
  446. // Build a handle to the newly created context.
  447. //
  448. ContextHandle->dwUpper = (ULONG_PTR) Context;
  449. ContextHandle->dwLower = 0;
  450. //
  451. // We don't support any options.
  452. //
  453. // Complain about those that require we do something.
  454. //
  455. if ( (ContextReqFlags & (ISC_REQ_ALLOCATE_MEMORY |
  456. ISC_REQ_PROMPT_FOR_CREDS |
  457. ISC_REQ_USE_SUPPLIED_CREDS )) != 0 ) {
  458. SspPrint(( SSP_API,
  459. "SspHandleFirstCall: invalid ContextReqFlags 0x%lx.\n",
  460. ContextReqFlags ));
  461. SecStatus = SEC_E_INVALID_CONTEXT_REQ;
  462. goto Cleanup;
  463. }
  464. //
  465. // If this is the first call,
  466. // build a Negotiate message.
  467. //
  468. // Offer to talk Oem character set.
  469. //
  470. _fstrcpy(NegotiateMessage.Signature, NTLMSSP_SIGNATURE );
  471. NegotiateMessage.MessageType = (ULONG)NtLmNegotiate;
  472. if (QoPFlags & QOP_NTLMV2)
  473. {
  474. Context->NegotiateFlags =
  475. NTLMSSP_NEGOTIATE_UNICODE |
  476. NTLMSSP_NEGOTIATE_OEM |
  477. NTLMSSP_NEGOTIATE_NTLM |
  478. NTLMSSP_NEGOTIATE_NTLM2 |
  479. NTLMSSP_REQUEST_TARGET |
  480. NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
  481. NTLMSSP_NEGOTIATE_128;
  482. NegotiateMessage.NegotiateFlags = NTLMSSP_NEGOTIATE_UNICODE |
  483. NTLMSSP_NEGOTIATE_OEM |
  484. NTLMSSP_REQUEST_TARGET |
  485. NTLMSSP_NEGOTIATE_NTLM |
  486. NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
  487. NTLMSSP_NEGOTIATE_NTLM2 |
  488. NTLMSSP_NEGOTIATE_56 |
  489. NTLMSSP_NEGOTIATE_128;
  490. }
  491. else
  492. {
  493. NegotiateMessage.NegotiateFlags = NTLMSSP_NEGOTIATE_OEM |
  494. NTLMSSP_NEGOTIATE_NTLM |
  495. NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
  496. if (Credential->Domain == NULL)
  497. {
  498. NegotiateMessage.NegotiateFlags |= NTLMSSP_REQUEST_TARGET;
  499. }
  500. }
  501. if ( *OutputTokenSize < sizeof(NEGOTIATE_MESSAGE) ) {
  502. SecStatus = SEC_E_BUFFER_TOO_SMALL;
  503. goto Cleanup;
  504. }
  505. if (ContextReqFlags & (ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT)) {
  506. Context->NegotiateFlags |= NTLMSSP_NEGOTIATE_SIGN;
  507. NegotiateMessage.NegotiateFlags |= NTLMSSP_NEGOTIATE_SIGN |
  508. NTLMSSP_NEGOTIATE_NT_ONLY;
  509. }
  510. if (ContextReqFlags & ISC_REQ_CONFIDENTIALITY) {
  511. Context->NegotiateFlags |= NTLMSSP_NEGOTIATE_SEAL;
  512. NegotiateMessage.NegotiateFlags |= NTLMSSP_NEGOTIATE_SEAL |
  513. NTLMSSP_NEGOTIATE_NT_ONLY;
  514. }
  515. swaplong(NegotiateMessage.NegotiateFlags);
  516. swaplong(NegotiateMessage.MessageType);
  517. _fmemcpy(OutputToken, &NegotiateMessage, sizeof(NEGOTIATE_MESSAGE));
  518. *OutputTokenSize = sizeof(NEGOTIATE_MESSAGE);
  519. //
  520. // Return output parameters to the caller.
  521. //
  522. *ExpirationTime = SspContextGetTimeStamp( Context, TRUE );
  523. Context->Credential = SspCredentialReferenceCredential(
  524. CredentialHandle,
  525. FALSE);
  526. SecStatus = SEC_I_CALLBACK_NEEDED;
  527. Context->State = NegotiateSentState;
  528. //
  529. // Free locally used resources.
  530. //
  531. Cleanup:
  532. if ( Context != NULL ) {
  533. if (SecStatus != SEC_I_CALLBACK_NEEDED) {
  534. SspContextDereferenceContext( Context );
  535. }
  536. }
  537. if ( Credential != NULL ) {
  538. SspCredentialDereferenceCredential( Credential );
  539. }
  540. return SecStatus;
  541. UNREFERENCED_PARAMETER( InputToken );
  542. UNREFERENCED_PARAMETER( InputTokenSize );
  543. }
  544. SECURITY_STATUS
  545. SspHandleChallengeMessage(
  546. IN PLUID LogonId,
  547. IN PCredHandle CredentialHandle,
  548. IN OUT PCtxtHandle ContextHandle,
  549. IN ULONG ContextReqFlags,
  550. IN ULONG InputTokenSize,
  551. IN PVOID InputToken,
  552. IN OUT PULONG OutputTokenSize,
  553. OUT PVOID OutputToken,
  554. OUT PULONG ContextAttributes,
  555. OUT PTimeStamp ExpirationTime
  556. )
  557. /*++
  558. Routine Description:
  559. Handle the Challenge message part of InitializeSecurityContext.
  560. Arguments:
  561. LogonId -- LogonId of the calling process.
  562. All other arguments same as for InitializeSecurityContext
  563. Return Value:
  564. STATUS_SUCCESS - Message handled
  565. SEC_I_CALLBACK_NEEDED -- Caller should call again later
  566. SEC_E_INVALID_TOKEN -- Token improperly formatted
  567. SEC_E_INVALID_HANDLE -- Credential/Context Handle is invalid
  568. SEC_E_BUFFER_TOO_SMALL -- Buffer for output token isn't big enough
  569. SEC_E_NO_CREDENTIALS -- There are no credentials for this client
  570. SEC_E_INSUFFICIENT_MEMORY -- Not enough memory
  571. --*/
  572. {
  573. SECURITY_STATUS SecStatus;
  574. PSSP_CONTEXT Context = NULL;
  575. PSSP_CREDENTIAL Credential = NULL;
  576. PCHALLENGE_MESSAGE ChallengeMessage = NULL;
  577. PAUTHENTICATE_MESSAGE AuthenticateMessage = NULL;
  578. ULONG AuthenticateMessageSize;
  579. PCHAR Where;
  580. #ifdef BL_USE_LM_PASSWORD
  581. LM_RESPONSE LmResponse;
  582. #endif
  583. NT_RESPONSE NtResponse;
  584. STRING32* pString;
  585. //
  586. // Initialization
  587. //
  588. *ContextAttributes = 0;
  589. //
  590. // Find the currently existing context.
  591. //
  592. Context = SspContextReferenceContext( ContextHandle, FALSE );
  593. if ( Context == NULL ) {
  594. SecStatus = SEC_E_INVALID_HANDLE;
  595. goto Cleanup;
  596. }
  597. //
  598. // If we have already sent the authenticate message, then this must be
  599. // RPC calling Initialize a third time to re-authenticate a connection.
  600. // This happens when a new interface is called over an existing
  601. // connection. What we do here is build a NULL authenticate message
  602. // that the server will recognize and also ignore.
  603. //
  604. if ( Context->State == AuthenticateSentState ) {
  605. AUTHENTICATE_MESSAGE NullMessage;
  606. //
  607. // To make sure this is the intended meaning of the call, check
  608. // that the input token is NULL.
  609. //
  610. if ( (InputTokenSize != 0) || (InputToken != NULL) ) {
  611. SecStatus = SEC_E_INVALID_TOKEN;
  612. goto Cleanup;
  613. }
  614. if ( *OutputTokenSize < sizeof(NullMessage) ) {
  615. SecStatus = SEC_E_BUFFER_TOO_SMALL;
  616. } else {
  617. _fstrcpy( NullMessage.Signature, NTLMSSP_SIGNATURE );
  618. NullMessage.MessageType = NtLmAuthenticate;
  619. swaplong(NullMessage.MessageType) ;
  620. _fmemset(&NullMessage.LmChallengeResponse, 0, 5*sizeof(STRING));
  621. *OutputTokenSize = sizeof(NullMessage);
  622. _fmemcpy(OutputToken, &NullMessage, sizeof(NullMessage));
  623. SecStatus = SEC_E_OK;
  624. }
  625. goto Cleanup;
  626. }
  627. if ( Context->State != NegotiateSentState ) {
  628. SspPrint(( SSP_API,
  629. "SspHandleChallengeMessage: "
  630. "Context not in NegotiateSentState\n" ));
  631. SecStatus = SEC_E_OUT_OF_SEQUENCE;
  632. goto Cleanup;
  633. }
  634. //
  635. // We don't support any options.
  636. //
  637. // Complain about those that require we do something.
  638. //
  639. if ( (ContextReqFlags & (ISC_REQ_ALLOCATE_MEMORY |
  640. ISC_REQ_PROMPT_FOR_CREDS |
  641. ISC_REQ_USE_SUPPLIED_CREDS )) != 0 ) {
  642. SspPrint(( SSP_API,
  643. "SspHandleFirstCall: invalid ContextReqFlags 0x%lx.\n",
  644. ContextReqFlags ));
  645. SecStatus = SEC_E_INVALID_CONTEXT_REQ;
  646. goto Cleanup;
  647. }
  648. if (ContextReqFlags & (ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT)) {
  649. Context->NegotiateFlags |= NTLMSSP_NEGOTIATE_SIGN;
  650. }
  651. if (ContextReqFlags & ISC_REQ_CONFIDENTIALITY) {
  652. Context->NegotiateFlags |= NTLMSSP_NEGOTIATE_SEAL;
  653. }
  654. //
  655. // Ignore the Credential Handle.
  656. //
  657. // Since this is the second call,
  658. // the credential is implied by the Context.
  659. // We could double check that the Credential Handle is either NULL or
  660. // correct. However, our implementation doesn't maintain a close
  661. // association between the two (actually no association) so checking
  662. // would require a lot of overhead.
  663. //
  664. UNREFERENCED_PARAMETER( CredentialHandle );
  665. ASSERT(Context->Credential != NULL);
  666. Credential = Context->Credential;
  667. //
  668. // Get the ChallengeMessage.
  669. //
  670. if ( InputTokenSize < sizeof(CHALLENGE_MESSAGE) ) {
  671. SspPrint(( SSP_API,
  672. "SspHandleChallengeMessage: "
  673. "ChallengeMessage size wrong %ld\n",
  674. InputTokenSize ));
  675. SecStatus = SEC_E_INVALID_TOKEN;
  676. goto Cleanup;
  677. }
  678. if ( InputTokenSize > NTLMSSP_MAX_MESSAGE_SIZE ) {
  679. SspPrint(( SSP_API,
  680. "SspHandleChallengeMessage: "
  681. "InputTokenSize > NTLMSSP_MAX_MESSAGE_SIZE\n" ));
  682. SecStatus = SEC_E_INVALID_TOKEN;
  683. goto Cleanup;
  684. }
  685. ChallengeMessage = (PCHALLENGE_MESSAGE) InputToken;
  686. swaplong(ChallengeMessage->MessageType) ;
  687. swaplong(ChallengeMessage->NegotiateFlags) ;
  688. if ( _fstrncmp( ChallengeMessage->Signature,
  689. NTLMSSP_SIGNATURE,
  690. sizeof(NTLMSSP_SIGNATURE)) != 0 ||
  691. ChallengeMessage->MessageType != NtLmChallenge ) {
  692. SspPrint(( SSP_API,
  693. "SspHandleChallengeMessage: "
  694. "InputToken has invalid NTLMSSP signature\n" ));
  695. SecStatus = SEC_E_INVALID_TOKEN;
  696. goto Cleanup;
  697. }
  698. //
  699. // Only negotiate OEM
  700. //
  701. if ( !(ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2) && ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_UNICODE ) {
  702. SspPrint(( SSP_API,
  703. "SspHandleChallengeMessage: "
  704. "ChallengeMessage bad NegotiateFlags (UNICODE) 0x%lx\n",
  705. ChallengeMessage->NegotiateFlags ));
  706. SecStatus = SEC_E_INVALID_TOKEN;
  707. goto Cleanup;
  708. }
  709. //
  710. // Check whether the server negotiated ALWAYS_SIGN
  711. //
  712. if ( ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN ) {
  713. Context->NegotiateFlags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
  714. }
  715. //
  716. // Only negotiate NTLM
  717. //
  718. if ( ( ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_NETWARE ) &&
  719. !( ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM ) ) {
  720. SspPrint(( SSP_API,
  721. "SspHandleChallengeMessage: "
  722. "ChallengeMessage bad NegotiateFlags (NETWARE) 0x%lx\n",
  723. ChallengeMessage->NegotiateFlags ));
  724. SecStatus = SEC_E_INVALID_TOKEN;
  725. goto Cleanup;
  726. }
  727. #if 0
  728. //
  729. // Make sure that if we are signing or sealing we only have to use the
  730. // LM key
  731. //
  732. if ((Context->NegotiateFlags & (NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_SEAL)) &&
  733. !(ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_LM_KEY))
  734. {
  735. SspPrint(( SSP_API,
  736. "SspHandleChallengeMessage: "
  737. "ChallengeMessage bad NegotiateFlags (Sign or Seal but no LM key) 0x%lx\n",
  738. ChallengeMessage->NegotiateFlags ));
  739. SecStatus = SEC_E_INVALID_TOKEN;
  740. goto Cleanup;
  741. }
  742. #endif
  743. if (!Credential || !Credential->Username)
  744. {
  745. SspPrint((SSP_CRITICAL, "SspHandleChallengeMessage no username\n"));
  746. SecStatus = SEC_E_NO_CREDENTIALS;
  747. goto Cleanup;
  748. }
  749. SspPrint((SSP_CRED, "User name: (%s)\n", Credential->Username));
  750. SspPrint((SSP_CRED, "Domain name: (%s)\n", Credential->Domain));
  751. SspPrint((SSP_CRED, "Workstation: (%s)\n", Credential->Workstation));
  752. if (ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  753. {
  754. if (!Credential || !Credential->NtPassword)
  755. {
  756. SspPrint((SSP_CRITICAL, "No NtPassword\n"));
  757. SecStatus = SEC_E_NO_CREDENTIALS;
  758. goto Cleanup;
  759. }
  760. SecStatus = SsprHandleNtlmv2ChallengeMessage(
  761. Credential,
  762. InputTokenSize,
  763. InputToken,
  764. &Context->NegotiateFlags,
  765. OutputTokenSize,
  766. OutputToken,
  767. &Context->UserSessionKey
  768. );
  769. if (SEC_E_BUFFER_TOO_SMALL == SecStatus)
  770. {
  771. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  772. }
  773. if (SecStatus != SEC_E_OK)
  774. {
  775. goto Cleanup;
  776. }
  777. SspMakeNtlmv2SKeys(
  778. &Context->UserSessionKey,
  779. Context->NegotiateFlags,
  780. 0, // SendNonce
  781. 0, // RecvNonce
  782. &Context->Ntlmv2SKeys
  783. );
  784. goto ReturnSuccess;
  785. }
  786. if (Credential->Domain == NULL) {
  787. ASSERT(ChallengeMessage->TargetName.Length != 0);
  788. Credential->Domain = SspAlloc(ChallengeMessage->TargetName.Length + 1);
  789. if (Credential->Domain == NULL) {
  790. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  791. goto Cleanup;
  792. }
  793. pString = &ChallengeMessage->TargetName;
  794. #if defined(_WIN64)
  795. _fmemcpy(Credential->Domain, (PCHAR)ChallengeMessage + (ULONG)((__int64)pString->Buffer), pString->Length);
  796. #else
  797. _fmemcpy(Credential->Domain, (PCHAR)ChallengeMessage + (ULONG)pString->Buffer, pString->Length);
  798. #endif
  799. Credential->Domain[pString->Length] = '\0';
  800. }
  801. if (GetPassword(Credential, 0) == FALSE) {
  802. SecStatus = SEC_E_NO_CREDENTIALS;
  803. goto Cleanup;
  804. }
  805. #ifdef BL_USE_LM_PASSWORD
  806. if (CalculateLmResponse((PLM_CHALLENGE)ChallengeMessage->Challenge, Credential->LmPassword, &LmResponse) == FALSE) {
  807. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  808. goto Cleanup;
  809. }
  810. #endif
  811. if (CalculateNtResponse((PNT_CHALLENGE)ChallengeMessage->Challenge, Credential->NtPassword, &NtResponse) == FALSE) {
  812. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  813. goto Cleanup;
  814. }
  815. //
  816. // Allocate an authenticate message. Change this #if 0 and the next one
  817. // to send an LM challenge response also.
  818. //
  819. #ifdef BL_USE_LM_PASSWORD
  820. AuthenticateMessageSize = sizeof(*AuthenticateMessage)+LM_RESPONSE_LENGTH+NT_RESPONSE_LENGTH;
  821. #else
  822. AuthenticateMessageSize = sizeof(*AuthenticateMessage)+NT_RESPONSE_LENGTH;
  823. #endif
  824. if (Credential->Domain != NULL) {
  825. AuthenticateMessageSize += _fstrlen(Credential->Domain);
  826. }
  827. if (Credential->Username != NULL) {
  828. AuthenticateMessageSize += _fstrlen(Credential->Username);
  829. }
  830. if (Credential->Workstation != NULL) {
  831. AuthenticateMessageSize += _fstrlen(Credential->Workstation);
  832. }
  833. if ( AuthenticateMessageSize > *OutputTokenSize ) {
  834. SecStatus = SEC_E_BUFFER_TOO_SMALL;
  835. goto Cleanup;
  836. }
  837. AuthenticateMessage = (PAUTHENTICATE_MESSAGE) SspAlloc ((int)AuthenticateMessageSize );
  838. if ( AuthenticateMessage == NULL ) {
  839. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  840. goto Cleanup;
  841. }
  842. //
  843. // Build the authenticate message
  844. //
  845. _fstrcpy( AuthenticateMessage->Signature, NTLMSSP_SIGNATURE );
  846. AuthenticateMessage->MessageType = NtLmAuthenticate;
  847. swaplong(AuthenticateMessage->MessageType) ;
  848. Where = (PCHAR)(AuthenticateMessage+1);
  849. #ifdef BL_USE_LM_PASSWORD
  850. SspCopyStringFromRaw( AuthenticateMessage,
  851. &AuthenticateMessage->LmChallengeResponse,
  852. (PCHAR)&LmResponse,
  853. LM_RESPONSE_LENGTH,
  854. &Where);
  855. #else
  856. SspCopyStringFromRaw( AuthenticateMessage,
  857. &AuthenticateMessage->LmChallengeResponse,
  858. NULL,
  859. 0,
  860. &Where);
  861. #endif
  862. SspCopyStringFromRaw( AuthenticateMessage,
  863. &AuthenticateMessage->NtChallengeResponse,
  864. (PCHAR)&NtResponse,
  865. NT_RESPONSE_LENGTH,
  866. &Where);
  867. if (Credential->Domain != NULL) {
  868. SspCopyStringFromRaw( AuthenticateMessage,
  869. &AuthenticateMessage->DomainName,
  870. Credential->Domain,
  871. _fstrlen(Credential->Domain),
  872. &Where);
  873. } else {
  874. SspCopyStringFromRaw( AuthenticateMessage,
  875. &AuthenticateMessage->DomainName,
  876. NULL, 0, &Where);
  877. }
  878. if (Credential->Username != NULL) {
  879. SspCopyStringFromRaw( AuthenticateMessage,
  880. &AuthenticateMessage->UserName,
  881. Credential->Username,
  882. _fstrlen(Credential->Username),
  883. &Where);
  884. } else {
  885. SspCopyStringFromRaw( AuthenticateMessage,
  886. &AuthenticateMessage->UserName,
  887. NULL, 0, &Where);
  888. }
  889. if (Credential->Workstation != NULL) {
  890. SspCopyStringFromRaw( AuthenticateMessage,
  891. &AuthenticateMessage->Workstation,
  892. Credential->Workstation,
  893. _fstrlen(Credential->Workstation),
  894. &Where);
  895. } else {
  896. SspCopyStringFromRaw( AuthenticateMessage,
  897. &AuthenticateMessage->Workstation,
  898. NULL, 0, &Where);
  899. }
  900. _fmemcpy(OutputToken, AuthenticateMessage, (int)AuthenticateMessageSize);
  901. *OutputTokenSize = AuthenticateMessageSize;
  902. //
  903. // The session key is the password, so convert it to a rc4 key.
  904. //
  905. if (Context->NegotiateFlags & (NTLMSSP_NEGOTIATE_SIGN |
  906. NTLMSSP_NEGOTIATE_SEAL)) {
  907. #ifdef BL_USE_LM_PASSWORD
  908. if (ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_LM_KEY) {
  909. LM_RESPONSE SessionKey;
  910. LM_OWF_PASSWORD LmKey;
  911. UCHAR Key[LM_SESSION_KEY_LENGTH];
  912. //
  913. // The session key is the first 8 bytes of the challenge response,
  914. // re-encrypted with the password with the second 8 bytes set to 0xbd
  915. //
  916. _fmemcpy(&LmKey,Credential->LmPassword,LM_SESSION_KEY_LENGTH);
  917. _fmemset( (PUCHAR)(&LmKey) + LM_SESSION_KEY_LENGTH,
  918. 0xbd,
  919. LM_OWF_PASSWORD_LENGTH - LM_SESSION_KEY_LENGTH);
  920. if (CalculateLmResponse( (PLM_CHALLENGE) &LmResponse,
  921. &LmKey,
  922. &SessionKey) == FALSE) {
  923. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  924. goto Cleanup;
  925. }
  926. _fmemcpy(Key,&SessionKey,5);
  927. ASSERT(LM_SESSION_KEY_LENGTH == 8);
  928. //
  929. // Put a well-known salt at the end of the key to limit
  930. // the changing part to 40 bits.
  931. //
  932. Key[5] = 0xe5;
  933. Key[6] = 0x38;
  934. Key[7] = 0xb0;
  935. Context->Rc4Key = SspAlloc(sizeof(struct RC4_KEYSTRUCT));
  936. if (Context->Rc4Key == NULL)
  937. {
  938. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  939. goto Cleanup;
  940. }
  941. rc4_key(Context->Rc4Key, LM_SESSION_KEY_LENGTH, Key);
  942. Context->Nonce = 0;
  943. } else
  944. #endif
  945. if (ChallengeMessage->NegotiateFlags & NTLMSSP_NEGOTIATE_NT_ONLY) {
  946. MD5_CTX Md5Context;
  947. USER_SESSION_KEY UserSessionKey;
  948. if (AuthenticateMessage->NtChallengeResponse.Length != NT_RESPONSE_LENGTH) {
  949. SecStatus = SEC_E_UNSUPPORTED_FUNCTION;
  950. goto Cleanup;
  951. }
  952. CalculateUserSessionKeyNt(
  953. &NtResponse,
  954. Credential->NtPassword,
  955. &UserSessionKey);
  956. //
  957. // The NT session key is made by MD5'ing the challenge response,
  958. // user name, domain name, and nt user session key together.
  959. //
  960. _fmemset(&Md5Context, 0, sizeof(MD5_CTX));
  961. MD5Init(
  962. &Md5Context
  963. );
  964. MD5Update(
  965. &Md5Context,
  966. (PUCHAR)&NtResponse,
  967. NT_RESPONSE_LENGTH
  968. );
  969. MD5Update(
  970. &Md5Context,
  971. Credential->Username,
  972. _fstrlen(Credential->Username)
  973. );
  974. MD5Update(
  975. &Md5Context,
  976. Credential->Domain,
  977. _fstrlen(Credential->Domain)
  978. );
  979. MD5Update(
  980. &Md5Context,
  981. (PUCHAR)&UserSessionKey,
  982. NT_SESSION_KEY_LENGTH
  983. );
  984. MD5Final(
  985. &Md5Context
  986. );
  987. ASSERT(MD5DIGESTLEN == NT_SESSION_KEY_LENGTH);
  988. Context->Rc4Key = SspAlloc(sizeof(struct RC4_KEYSTRUCT));
  989. if (Context->Rc4Key == NULL)
  990. {
  991. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  992. goto Cleanup;
  993. }
  994. rc4_key(Context->Rc4Key, NT_SESSION_KEY_LENGTH, Md5Context.digest);
  995. Context->Nonce = 0;
  996. } else {
  997. USER_SESSION_KEY UserSessionKey;
  998. if (AuthenticateMessage->NtChallengeResponse.Length != NT_RESPONSE_LENGTH) {
  999. SecStatus = SEC_E_UNSUPPORTED_FUNCTION;
  1000. goto Cleanup;
  1001. }
  1002. CalculateUserSessionKeyNt(
  1003. &NtResponse,
  1004. Credential->NtPassword,
  1005. &UserSessionKey);
  1006. Context->Rc4Key = SspAlloc(sizeof(struct RC4_KEYSTRUCT));
  1007. if (Context->Rc4Key == NULL)
  1008. {
  1009. SecStatus = SEC_E_INSUFFICIENT_MEMORY;
  1010. goto Cleanup;
  1011. }
  1012. rc4_key(Context->Rc4Key, NT_SESSION_KEY_LENGTH, (PUCHAR) &UserSessionKey);
  1013. Context->Nonce = 0;
  1014. }
  1015. }
  1016. ReturnSuccess:
  1017. //
  1018. // Return output parameters to the caller.
  1019. //
  1020. *ExpirationTime = SspContextGetTimeStamp( Context, TRUE );
  1021. SecStatus = SEC_E_OK;
  1022. //
  1023. // Free and locally used resources.
  1024. //
  1025. Cleanup:
  1026. if ( Context != NULL ) {
  1027. //
  1028. // Don't allow this context to be used again.
  1029. //
  1030. if ( SecStatus == SEC_E_OK ) {
  1031. Context->State = AuthenticateSentState;
  1032. } else {
  1033. Context->State = IdleState;
  1034. }
  1035. SspContextDereferenceContext( Context );
  1036. }
  1037. if ( AuthenticateMessage != NULL ) {
  1038. _fmemset(AuthenticateMessage, 0, AuthenticateMessageSize);
  1039. SspFree( AuthenticateMessage );
  1040. }
  1041. return SecStatus;
  1042. }
  1043. SECURITY_STATUS SEC_ENTRY
  1044. InitializeSecurityContext(
  1045. IN PCredHandle CredentialHandle,
  1046. IN PCtxtHandle OldContextHandle,
  1047. IN SEC_CHAR * TargetName,
  1048. IN ULONG ContextReqFlags,
  1049. IN ULONG Reserved1,
  1050. IN ULONG TargetDataRep,
  1051. IN PSecBufferDesc InputToken,
  1052. IN ULONG Reserved2,
  1053. OUT PCtxtHandle NewContextHandle,
  1054. OUT PSecBufferDesc OutputToken,
  1055. OUT PULONG ContextAttributes,
  1056. OUT PTimeStamp ExpirationTime
  1057. )
  1058. /*++
  1059. Routine Description:
  1060. This routine initiates the outbound security context from a credential
  1061. handle. This results in the establishment of a security context
  1062. between the application and a remote peer. The routine returns a token
  1063. which must be passed to the remote peer which in turn submits it to the
  1064. local security implementation via the AcceptSecurityContext() call.
  1065. The token generated should be considered opaque by all callers.
  1066. This function is used by a client to initialize an outbound context.
  1067. For a two leg security package, the calling sequence is as follows: The
  1068. client calls the function with OldContextHandle set to NULL and
  1069. InputToken set either to NULL or to a pointer to a security package
  1070. specific data structure. The package returns a context handle in
  1071. NewContextHandle and a token in OutputToken. The handle can then be
  1072. used for message APIs if desired.
  1073. The OutputToken returned here is sent across to target server which
  1074. calls AcceptSecuirtyContext() with this token as an input argument and
  1075. may receive a token which is returned to the initiator so it can call
  1076. InitializeSecurityContext() again.
  1077. For a three leg (mutual authentication) security package, the calling
  1078. sequence is as follows: The client calls the function as above, but the
  1079. package will return SEC_I_CALLBACK_NEEDED. The client then sends the
  1080. output token to the server and waits for the server's reply. Upon
  1081. receipt of the server's response, the client calls this function again,
  1082. with OldContextHandle set to the handle that was returned from the
  1083. first call. The token received from the server is supplied in the
  1084. InputToken parameter. If the server has successfully responded, then
  1085. the package will respond with success, or it will invalidate the
  1086. context.
  1087. Initialization of security context may require more than one call to
  1088. this function depending upon the underlying authentication mechanism as
  1089. well as the "choices" indicated via ContextReqFlags. The
  1090. ContextReqFlags and ContextAttributes are bit masks representing
  1091. various context level functions viz. delegation, mutual
  1092. authentication, confidentiality, replay detection and sequence
  1093. detection.
  1094. When ISC_REQ_PROMPT_FOR_CREDS flag is set the security package always
  1095. prompts the user for credentials, irrespective of whether credentials
  1096. are present or not. If user indicated that the supplied credentials be
  1097. used then they will be stashed (overwriting existing ones if any) for
  1098. future use. The security packages will always prompt for credentials
  1099. if none existed, this optimizes for the most common case before a
  1100. credentials database is built. But the security packages can be
  1101. configured to not do that. Security packages will ensure that they
  1102. only prompt to the interactive user, for other logon sessions, this
  1103. flag is ignored.
  1104. When ISC_REQ_USE_SUPPLIED_CREDS flag is set the security package always
  1105. uses the credentials supplied in the InitializeSecurityContext() call
  1106. via InputToken parameter. If the package does not have any credentials
  1107. available it will prompt for them and record it as indicated above.
  1108. It is an error to set both these flags simultaneously.
  1109. If the ISC_REQ_ALLOCATE_MEMORY was specified then the caller must free
  1110. the memory pointed to by OutputToken by calling FreeContextBuffer().
  1111. For example, the InputToken may be the challenge from a LAN Manager or
  1112. NT file server. In this case, the OutputToken would be the NTLM
  1113. encrypted response to the challenge. The caller of this API can then
  1114. take the appropriate response (case-sensitive v. case-insensitive) and
  1115. return it to the server for an authenticated connection.
  1116. Arguments:
  1117. CredentialHandle - Handle to the credentials to be used to
  1118. create the context.
  1119. OldContextHandle - Handle to the partially formed context, if this is
  1120. a second call (see above) or NULL if this is the first call.
  1121. TargetName - String indicating the target of the context. The name will
  1122. be security package specific. For example it will be a fully
  1123. qualified Cairo name for Kerberos package and can be UNC name or
  1124. domain name for the NTLM package.
  1125. ContextReqFlags - Requirements of the context, package specific.
  1126. #define ISC_REQ_DELEGATE 0x00000001
  1127. #define ISC_REQ_MUTUAL_AUTH 0x00000002
  1128. #define ISC_REQ_REPLAY_DETECT 0x00000004
  1129. #define ISC_REQ_SEQUENCE_DETECT 0x00000008
  1130. #define ISC_REQ_CONFIDENTIALITY 0x00000010
  1131. #define ISC_REQ_USE_SESSION_KEY 0x00000020
  1132. #define ISC_REQ_PROMT_FOR__CREDS 0x00000040
  1133. #define ISC_REQ_USE_SUPPLIED_CREDS 0x00000080
  1134. #define ISC_REQ_ALLOCATE_MEMORY 0x00000100
  1135. #define ISC_REQ_USE_DCE_STYLE 0x00000200
  1136. Reserved1 - Reserved value, MBZ.
  1137. TargetDataRep - Long indicating the data representation (byte ordering, etc)
  1138. on the target. The constant SECURITY_NATIVE_DREP may be supplied
  1139. by the transport indicating that the native format is in use.
  1140. InputToken - Pointer to the input token. In the first call this
  1141. token can either be NULL or may contain security package specific
  1142. information.
  1143. Reserved2 - Reserved value, MBZ.
  1144. NewContextHandle - New context handle. If this is a second call, this
  1145. can be the same as OldContextHandle.
  1146. OutputToken - Buffer to receive the output token.
  1147. ContextAttributes -Attributes of the context established.
  1148. #define ISC_RET_DELEGATE 0x00000001
  1149. #define ISC_RET_MUTUAL_AUTH 0x00000002
  1150. #define ISC_RET_REPLAY_DETECT 0x00000004
  1151. #define ISC_RET_SEQUENCE_DETECT 0x00000008
  1152. #define ISC_REP_CONFIDENTIALITY 0x00000010
  1153. #define ISC_REP_USE_SESSION_KEY 0x00000020
  1154. #define ISC_REP_USED_COLLECTED_CREDS 0x00000040
  1155. #define ISC_REP_USED_SUPPLIED_CREDS 0x00000080
  1156. #define ISC_REP_ALLOCATED_MEMORY 0x00000100
  1157. #define ISC_REP_USED_DCE_STYLE 0x00000200
  1158. ExpirationTime - Expiration time of the context.
  1159. Return Value:
  1160. STATUS_SUCCESS - Message handled
  1161. SEC_I_CALLBACK_NEEDED -- Caller should call again later
  1162. SEC_E_NO_SPM -- Security Support Provider is not running
  1163. SEC_E_INVALID_TOKEN -- Token improperly formatted
  1164. SEC_E_INVALID_HANDLE -- Credential/Context Handle is invalid
  1165. SEC_E_BUFFER_TOO_SMALL -- Buffer for output token isn't big enough
  1166. SEC_E_NO_CREDENTIALS -- There are no credentials for this client
  1167. SEC_E_INSUFFICIENT_MEMORY -- Not enough memory
  1168. --*/
  1169. {
  1170. SECURITY_STATUS SecStatus;
  1171. PVOID InputTokenBuffer;
  1172. PULONG InputTokenSize;
  1173. ULONG LocalInputTokenSize;
  1174. PVOID OutputTokenBuffer;
  1175. PULONG OutputTokenSize;
  1176. ULONG QopFlags;
  1177. //
  1178. // ideally pull these in from a global header.
  1179. //
  1180. extern ULONG AuthenticationType;
  1181. #define OSCHOICE_AUTHENETICATE_TYPE_NTLM_V1 0x00000001
  1182. #define OSCHOICE_AUTHENETICATE_TYPE_NTLM_V2 0x00000002
  1183. SspPrint((SSP_API, "SspInitializeSecurityContext Entered\n"));
  1184. //
  1185. // Check argument validity
  1186. //
  1187. if (!ARGUMENT_PRESENT(OutputToken)) {
  1188. return (ERROR_BAD_ARGUMENTS);
  1189. }
  1190. #ifdef notdef // ? RPC passes 0x10 or 0 here depending on attitude
  1191. if ( TargetDataRep != SECURITY_NATIVE_DREP ) {
  1192. return (STATUS_INVALID_PARAMETER);
  1193. }
  1194. #else // notdef
  1195. UNREFERENCED_PARAMETER( TargetDataRep );
  1196. #endif // notdef
  1197. if ( !SspGetTokenBuffer( InputToken,
  1198. &InputTokenBuffer,
  1199. &InputTokenSize,
  1200. TRUE ) ) {
  1201. return (SEC_E_INVALID_TOKEN);
  1202. }
  1203. if ( InputTokenSize == 0 ) {
  1204. InputTokenSize = &LocalInputTokenSize;
  1205. LocalInputTokenSize = 0;
  1206. }
  1207. if ( !SspGetTokenBuffer( OutputToken,
  1208. &OutputTokenBuffer,
  1209. &OutputTokenSize,
  1210. FALSE ) ) {
  1211. return (SEC_E_INVALID_TOKEN);
  1212. }
  1213. //
  1214. // If no previous context was passed in this is the first call.
  1215. //
  1216. if ( !ARGUMENT_PRESENT( OldContextHandle ) ) {
  1217. if ( !ARGUMENT_PRESENT( CredentialHandle ) ) {
  1218. return (SEC_E_INVALID_HANDLE);
  1219. }
  1220. if (AuthenticationType == OSCHOICE_AUTHENETICATE_TYPE_NTLM_V1) {
  1221. QopFlags = 0;
  1222. } else if (AuthenticationType == OSCHOICE_AUTHENETICATE_TYPE_NTLM_V2) {
  1223. QopFlags = QOP_NTLMV2;
  1224. }
  1225. return SspHandleFirstCall(
  1226. CredentialHandle,
  1227. NewContextHandle,
  1228. ContextReqFlags,
  1229. QopFlags,
  1230. *InputTokenSize,
  1231. InputTokenBuffer,
  1232. OutputTokenSize,
  1233. OutputTokenBuffer,
  1234. ContextAttributes,
  1235. ExpirationTime
  1236. );
  1237. //
  1238. // If context was passed in, continue where we left off.
  1239. //
  1240. } else {
  1241. *NewContextHandle = *OldContextHandle;
  1242. return SspHandleChallengeMessage(
  1243. NULL,
  1244. CredentialHandle,
  1245. NewContextHandle,
  1246. ContextReqFlags,
  1247. *InputTokenSize,
  1248. InputTokenBuffer,
  1249. OutputTokenSize,
  1250. OutputTokenBuffer,
  1251. ContextAttributes,
  1252. ExpirationTime
  1253. );
  1254. }
  1255. return (SecStatus);
  1256. }
  1257. SECURITY_STATUS SEC_ENTRY
  1258. DeleteSecurityContext (
  1259. PCtxtHandle ContextHandle
  1260. )
  1261. /*++
  1262. Routine Description:
  1263. Deletes the local data structures associated with the specified
  1264. security context and generates a token which is passed to a remote peer
  1265. so it too can remove the corresponding security context.
  1266. This API terminates a context on the local machine, and optionally
  1267. provides a token to be sent to the other machine. The OutputToken
  1268. generated by this call is to be sent to the remote peer (initiator or
  1269. acceptor). If the context was created with the I _REQ_ALLOCATE_MEMORY
  1270. flag, then the package will allocate a buffer for the output token.
  1271. Otherwise, it is the responsibility of the caller.
  1272. Arguments:
  1273. ContextHandle - Handle to the context to delete
  1274. TokenLength - Size of the output token (if any) that should be sent to
  1275. the process at the other end of the session.
  1276. Token - Pointer to the token to send.
  1277. Return Value:
  1278. SEC_E_OK - Call completed successfully
  1279. SEC_E_NO_SPM -- Security Support Provider is not running
  1280. SEC_E_INVALID_HANDLE -- Credential/Context Handle is invalid
  1281. --*/
  1282. {
  1283. SECURITY_STATUS SecStatus;
  1284. PSSP_CONTEXT Context = NULL;
  1285. //
  1286. // Initialization
  1287. //
  1288. SspPrint(( SSP_API, "SspDeleteSecurityContext Entered\n" ));
  1289. //
  1290. // Find the currently existing context (and delink it).
  1291. //
  1292. Context = SspContextReferenceContext( ContextHandle,
  1293. TRUE );
  1294. if ( Context == NULL ) {
  1295. SecStatus = SEC_E_INVALID_HANDLE;
  1296. goto cleanup;
  1297. } else {
  1298. SspContextDereferenceContext( Context );
  1299. SecStatus = SEC_E_OK;
  1300. }
  1301. cleanup:
  1302. if (Context != NULL) {
  1303. SspContextDereferenceContext(Context);
  1304. Context = NULL;
  1305. }
  1306. SspPrint(( SSP_API, "SspDeleteSecurityContext returns 0x%x\n", SecStatus ));
  1307. return SecStatus;
  1308. }
  1309. SECURITY_STATUS SEC_ENTRY
  1310. FreeContextBuffer (
  1311. void * ContextBuffer
  1312. )
  1313. /*++
  1314. Routine Description:
  1315. This API is provided to allow callers of security API such as
  1316. InitializeSecurityContext() for free the memory buffer allocated for
  1317. returning the outbound context token.
  1318. Arguments:
  1319. ContextBuffer - Address of the buffer to be freed.
  1320. Return Value:
  1321. SEC_E_OK - Call completed successfully
  1322. --*/
  1323. {
  1324. //
  1325. // The only allocated buffer that NtLmSsp currently returns to the caller
  1326. // is from EnumeratePackages. It uses LocalAlloc to allocate memory. If
  1327. // we ever need memory to be allocated by the service, we have to rethink
  1328. // how this routine distinguishes between to two types of allocated memory.
  1329. //
  1330. SspFree( ContextBuffer );
  1331. return (SEC_E_OK);
  1332. }
  1333. SECURITY_STATUS SEC_ENTRY
  1334. ApplyControlToken (
  1335. PCtxtHandle ContextHandle,
  1336. PSecBufferDesc Input
  1337. )
  1338. {
  1339. #ifdef DEBUGRPC
  1340. SspPrint(( SSP_API, "ApplyContextToken Called\n" ));
  1341. #endif // DEBUGRPC
  1342. return SEC_E_UNSUPPORTED_FUNCTION;
  1343. UNREFERENCED_PARAMETER( ContextHandle );
  1344. UNREFERENCED_PARAMETER( Input );
  1345. }
  1346. void
  1347. SsprGenCheckSum(
  1348. IN PSecBuffer pMessage,
  1349. OUT PNTLMSSP_MESSAGE_SIGNATURE pSig
  1350. )
  1351. {
  1352. Crc32(pSig->CheckSum,pMessage->cbBuffer,pMessage->pvBuffer,&pSig->CheckSum);
  1353. }
  1354. SECURITY_STATUS SEC_ENTRY
  1355. MakeSignature(
  1356. IN OUT PCtxtHandle ContextHandle,
  1357. IN ULONG fQOP,
  1358. IN OUT PSecBufferDesc pMessage,
  1359. IN ULONG MessageSeqNo
  1360. )
  1361. {
  1362. PSSP_CONTEXT pContext;
  1363. PNTLMSSP_MESSAGE_SIGNATURE pSig;
  1364. int Signature;
  1365. ULONG i;
  1366. pContext = SspContextReferenceContext(ContextHandle,FALSE);
  1367. if (!pContext ||
  1368. (!(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1369. && (pContext->Rc4Key == NULL)
  1370. && !(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
  1371. )
  1372. )
  1373. {
  1374. return(SEC_E_INVALID_HANDLE);
  1375. }
  1376. if (pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1377. {
  1378. return SspNtlmv2MakeSignature(
  1379. &pContext->Ntlmv2SKeys,
  1380. pContext->NegotiateFlags,
  1381. fQOP,
  1382. MessageSeqNo,
  1383. pMessage
  1384. );
  1385. }
  1386. Signature = -1;
  1387. for (i = 0; i < pMessage->cBuffers; i++)
  1388. {
  1389. if ((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_TOKEN)
  1390. {
  1391. Signature = i;
  1392. break;
  1393. }
  1394. }
  1395. if (Signature == -1)
  1396. {
  1397. SspContextDereferenceContext(pContext);
  1398. return(SEC_E_INVALID_TOKEN);
  1399. }
  1400. pSig = pMessage->pBuffers[Signature].pvBuffer;
  1401. if (!(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_SIGN))
  1402. {
  1403. _fmemset(pSig,0,NTLMSSP_MESSAGE_SIGNATURE_SIZE);
  1404. pSig->Version = NTLMSSP_SIGN_VERSION;
  1405. swaplong(pSig->Version) ; // MACBUG
  1406. SspContextDereferenceContext(pContext);
  1407. return(SEC_E_OK);
  1408. }
  1409. //
  1410. // required by CRC-32 algorithm
  1411. //
  1412. pSig->CheckSum = 0xffffffff;
  1413. for (i = 0; i < pMessage->cBuffers ; i++ )
  1414. {
  1415. if (((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_DATA) &&
  1416. !(pMessage->pBuffers[i].BufferType & SECBUFFER_READONLY))
  1417. {
  1418. SsprGenCheckSum(&pMessage->pBuffers[i], pSig);
  1419. }
  1420. }
  1421. //
  1422. // Required by CRC-32 algorithm
  1423. //
  1424. pSig->CheckSum ^= 0xffffffff;
  1425. pSig->Nonce = pContext->Nonce++;
  1426. pSig->Version = NTLMSSP_SIGN_VERSION; // MACBUG
  1427. swaplong(pSig->CheckSum) ;
  1428. swaplong(pSig->Nonce) ;
  1429. swaplong(pSig->Version) ;
  1430. rc4(pContext->Rc4Key, sizeof(NTLMSSP_MESSAGE_SIGNATURE) - sizeof(ULONG),
  1431. (unsigned char SEC_FAR *) &pSig->RandomPad);
  1432. pMessage->pBuffers[Signature].cbBuffer = sizeof(NTLMSSP_MESSAGE_SIGNATURE);
  1433. SspContextDereferenceContext(pContext);
  1434. return(SEC_E_OK);
  1435. }
  1436. SECURITY_STATUS SEC_ENTRY
  1437. VerifySignature(
  1438. IN OUT PCtxtHandle ContextHandle,
  1439. IN OUT PSecBufferDesc pMessage,
  1440. IN ULONG MessageSeqNo,
  1441. OUT PULONG pfQOP
  1442. )
  1443. {
  1444. PSSP_CONTEXT pContext;
  1445. PNTLMSSP_MESSAGE_SIGNATURE pSig;
  1446. NTLMSSP_MESSAGE_SIGNATURE Sig;
  1447. int Signature;
  1448. ULONG i;
  1449. UNREFERENCED_PARAMETER(pfQOP);
  1450. UNREFERENCED_PARAMETER(MessageSeqNo);
  1451. pContext = SspContextReferenceContext(ContextHandle,FALSE);
  1452. if (!pContext ||
  1453. (!(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1454. && (pContext->Rc4Key == NULL)
  1455. && !(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
  1456. )
  1457. )
  1458. {
  1459. return(SEC_E_INVALID_HANDLE);
  1460. }
  1461. if (pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1462. {
  1463. return SspNtlmv2VerifySignature(
  1464. &pContext->Ntlmv2SKeys,
  1465. pContext->NegotiateFlags,
  1466. MessageSeqNo,
  1467. pMessage,
  1468. pfQOP
  1469. );
  1470. }
  1471. Signature = -1;
  1472. for (i = 0; i < pMessage->cBuffers; i++)
  1473. {
  1474. if ((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_TOKEN)
  1475. {
  1476. Signature = i;
  1477. break;
  1478. }
  1479. }
  1480. if (Signature == -1)
  1481. {
  1482. SspContextDereferenceContext(pContext);
  1483. return(SEC_E_INVALID_TOKEN);
  1484. }
  1485. pSig = pMessage->pBuffers[Signature].pvBuffer;
  1486. swaplong(pSig->Version) ;
  1487. //
  1488. // Check if this is just a trailer and not a real signature
  1489. //
  1490. if (!(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_SIGN))
  1491. {
  1492. SspContextDereferenceContext(pContext);
  1493. _fmemset(&Sig,0,NTLMSSP_MESSAGE_SIGNATURE_SIZE);
  1494. Sig.Version = NTLMSSP_SIGN_VERSION;
  1495. if (!_fmemcmp(&Sig,pSig,NTLMSSP_MESSAGE_SIGNATURE_SIZE))
  1496. {
  1497. return(SEC_E_OK);
  1498. }
  1499. return(SEC_E_MESSAGE_ALTERED);
  1500. }
  1501. Sig.CheckSum = 0xffffffff;
  1502. for (i = 0; i < pMessage->cBuffers ; i++ )
  1503. {
  1504. if (((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_DATA) &&
  1505. !(pMessage->pBuffers[i].BufferType & SECBUFFER_READONLY))
  1506. {
  1507. SsprGenCheckSum(&pMessage->pBuffers[i], &Sig);
  1508. }
  1509. }
  1510. Sig.CheckSum ^= 0xffffffff;
  1511. Sig.Nonce = pContext->Nonce++;
  1512. rc4(pContext->Rc4Key, sizeof(NTLMSSP_MESSAGE_SIGNATURE) - sizeof(ULONG),
  1513. (unsigned char SEC_FAR *) &pSig->RandomPad);
  1514. SspContextDereferenceContext(pContext);
  1515. swaplong(pSig->CheckSum) ;
  1516. swaplong(pSig->Nonce) ;
  1517. if (pSig->CheckSum != Sig.CheckSum)
  1518. {
  1519. return(SEC_E_MESSAGE_ALTERED);
  1520. }
  1521. if (pSig->Nonce != Sig.Nonce)
  1522. {
  1523. return(SEC_E_OUT_OF_SEQUENCE);
  1524. }
  1525. return(SEC_E_OK);
  1526. }
  1527. SECURITY_STATUS SEC_ENTRY
  1528. SealMessage(
  1529. IN OUT PCtxtHandle ContextHandle,
  1530. IN ULONG fQOP,
  1531. IN OUT PSecBufferDesc pMessage,
  1532. IN ULONG MessageSeqNo
  1533. )
  1534. {
  1535. PSSP_CONTEXT pContext;
  1536. PNTLMSSP_MESSAGE_SIGNATURE pSig;
  1537. int Signature;
  1538. ULONG i;
  1539. UNREFERENCED_PARAMETER(fQOP);
  1540. UNREFERENCED_PARAMETER(MessageSeqNo);
  1541. pContext = SspContextReferenceContext(ContextHandle, FALSE);
  1542. if (!pContext ||
  1543. (!(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1544. && (pContext->Rc4Key == NULL)
  1545. )
  1546. )
  1547. {
  1548. return(SEC_E_INVALID_HANDLE);
  1549. }
  1550. if (pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1551. {
  1552. return SspNtlmv2SealMessage(
  1553. &pContext->Ntlmv2SKeys,
  1554. pContext->NegotiateFlags,
  1555. fQOP,
  1556. MessageSeqNo,
  1557. pMessage
  1558. );
  1559. }
  1560. Signature = -1;
  1561. for (i = 0; i < pMessage->cBuffers; i++)
  1562. {
  1563. if ((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_TOKEN)
  1564. {
  1565. Signature = i;
  1566. break;
  1567. }
  1568. }
  1569. if (Signature == -1)
  1570. {
  1571. SspContextDereferenceContext(pContext);
  1572. return(SEC_E_INVALID_TOKEN);
  1573. }
  1574. pSig = pMessage->pBuffers[Signature].pvBuffer;
  1575. //
  1576. // required by CRC-32 algorithm
  1577. //
  1578. pSig->CheckSum = 0xffffffff;
  1579. for (i = 0; i < pMessage->cBuffers ; i++ )
  1580. {
  1581. if (((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_DATA) &&
  1582. !(pMessage->pBuffers[i].BufferType & SECBUFFER_READONLY))
  1583. {
  1584. SsprGenCheckSum(&pMessage->pBuffers[i], pSig);
  1585. if (pMessage->pBuffers[i].cbBuffer) // rc4 fails with zero byte buffers
  1586. {
  1587. rc4(pContext->Rc4Key,
  1588. (int) pMessage->pBuffers[i].cbBuffer,
  1589. (PUCHAR) pMessage->pBuffers[i].pvBuffer );
  1590. }
  1591. }
  1592. }
  1593. //
  1594. // Required by CRC-32 algorithm
  1595. //
  1596. pSig->CheckSum ^= 0xffffffff;
  1597. pSig->Nonce = pContext->Nonce++;
  1598. pSig->Version = NTLMSSP_SIGN_VERSION; // MACBUG
  1599. swaplong(pSig->CheckSum) ;
  1600. swaplong(pSig->Nonce) ;
  1601. swaplong(pSig->Version) ;
  1602. rc4(pContext->Rc4Key, sizeof(NTLMSSP_MESSAGE_SIGNATURE) - sizeof(ULONG),
  1603. (PUCHAR) &pSig->RandomPad);
  1604. pMessage->pBuffers[Signature].cbBuffer = sizeof(NTLMSSP_MESSAGE_SIGNATURE);
  1605. SspContextDereferenceContext(pContext);
  1606. return(SEC_E_OK);
  1607. }
  1608. SECURITY_STATUS SEC_ENTRY
  1609. UnsealMessage(
  1610. IN OUT PCtxtHandle ContextHandle,
  1611. IN OUT PSecBufferDesc pMessage,
  1612. IN ULONG MessageSeqNo,
  1613. OUT PULONG pfQOP
  1614. )
  1615. {
  1616. PSSP_CONTEXT pContext;
  1617. PNTLMSSP_MESSAGE_SIGNATURE pSig;
  1618. NTLMSSP_MESSAGE_SIGNATURE Sig;
  1619. int Signature;
  1620. ULONG i;
  1621. UNREFERENCED_PARAMETER(pfQOP);
  1622. UNREFERENCED_PARAMETER(MessageSeqNo);
  1623. pContext = SspContextReferenceContext(ContextHandle, FALSE);
  1624. if (!pContext ||
  1625. (!(pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1626. && (!pContext->Rc4Key)
  1627. )
  1628. )
  1629. {
  1630. return(SEC_E_INVALID_HANDLE);
  1631. }
  1632. if (pContext->NegotiateFlags & NTLMSSP_NEGOTIATE_NTLM2)
  1633. {
  1634. return SspNtlmv2UnsealMessage(
  1635. &pContext->Ntlmv2SKeys,
  1636. pContext->NegotiateFlags,
  1637. MessageSeqNo,
  1638. pMessage,
  1639. pfQOP
  1640. );
  1641. }
  1642. Signature = -1;
  1643. for (i = 0; i < pMessage->cBuffers; i++)
  1644. {
  1645. if ((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_TOKEN)
  1646. {
  1647. Signature = i;
  1648. break;
  1649. }
  1650. }
  1651. if (Signature == -1)
  1652. {
  1653. SspContextDereferenceContext(pContext);
  1654. return(SEC_E_INVALID_TOKEN);
  1655. }
  1656. pSig = pMessage->pBuffers[Signature].pvBuffer;
  1657. Sig.CheckSum = 0xffffffff;
  1658. for (i = 0; i < pMessage->cBuffers ; i++ )
  1659. {
  1660. if (((pMessage->pBuffers[i].BufferType & 0xFF) == SECBUFFER_DATA) &&
  1661. !(pMessage->pBuffers[i].BufferType & SECBUFFER_READONLY))
  1662. {
  1663. if (pMessage->pBuffers[i].cbBuffer)
  1664. {
  1665. rc4(pContext->Rc4Key,
  1666. (int) pMessage->pBuffers[i].cbBuffer,
  1667. (unsigned char *) pMessage->pBuffers[i].pvBuffer );
  1668. }
  1669. SsprGenCheckSum(&pMessage->pBuffers[i], &Sig);
  1670. }
  1671. }
  1672. Sig.CheckSum ^= 0xffffffff;
  1673. Sig.Nonce = pContext->Nonce++;
  1674. rc4(pContext->Rc4Key, sizeof(NTLMSSP_MESSAGE_SIGNATURE) - sizeof(ULONG),
  1675. (unsigned char *) &pSig->RandomPad);
  1676. SspContextDereferenceContext(pContext);
  1677. swaplong(pSig->Nonce) ;
  1678. swaplong(pSig->CheckSum) ;
  1679. if (pSig->Nonce != Sig.Nonce)
  1680. {
  1681. return(SEC_E_OUT_OF_SEQUENCE);
  1682. }
  1683. if (pSig->CheckSum != Sig.CheckSum)
  1684. {
  1685. return(SEC_E_MESSAGE_ALTERED);
  1686. }
  1687. return(SEC_E_OK);
  1688. }