Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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