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.

4548 lines
133 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1996
  6. //
  7. // File: userapi.cxx
  8. //
  9. // Contents: User-mode APIs to Kerberos package
  10. //
  11. //
  12. // History: 17-April-1996 Created MikeSw
  13. // 26-Sep-1998 ChandanS
  14. // Added more debugging support etc.
  15. //
  16. //------------------------------------------------------------------------
  17. /*
  18. * Copyright 1993 by OpenVision Technologies, Inc.
  19. *
  20. * Permission to use, copy, modify, distribute, and sell this software
  21. * and its documentation for any purpose is hereby granted without fee,
  22. * provided that the above copyright notice appears in all copies and
  23. * that both that copyright notice and this permission notice appear in
  24. * supporting documentation, and that the name of OpenVision not be used
  25. * in advertising or publicity pertaining to distribution of the software
  26. * without specific, written prior permission. OpenVision makes no
  27. * representations about the suitability of this software for any
  28. * purpose. It is provided "as is" without express or implied warranty.
  29. *
  30. * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  31. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  32. * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  33. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  34. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  35. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  36. * PERFORMANCE OF THIS SOFTWARE.
  37. */
  38. #include <kerb.hxx>
  39. #define USERAPI_ALLOCATE
  40. #include <kerbp.h>
  41. #ifdef RETAIL_LOG_SUPPORT
  42. static TCHAR THIS_FILE[]=TEXT(__FILE__);
  43. #endif
  44. #ifndef WIN32_CHICAGO
  45. extern "C"
  46. {
  47. #include <cryptdll.h>
  48. }
  49. #endif // WIN32_CHICAGO
  50. #include "userapi.h"
  51. #define DONT_SUPPORT_OLD_TYPES_USER 1
  52. #define ALIGN_SIZE sizeof(PVOID)
  53. #define ALIGN_SHIFT (ALIGN_SIZE - 0x00000001) // 0x00000007
  54. #define IS_POINTER_ALIGNED(ptr) (((UINT_PTR)(ptr) & ALIGN_SHIFT) == 0x00000000)
  55. // can't sign or seal messages greater than this
  56. #define KERB_MAX_MESSAGE_SIZE 0x40000000
  57. //
  58. // Common GSS object IDs, taken from MIT kerberos distribution.
  59. //
  60. gss_OID_desc oids[] = {
  61. {5, "\053\005\001\005\002"}, // original mech id
  62. {9, "\052\206\110\206\367\022\001\002\002"}, // standard mech id
  63. {10, "\052\206\110\206\367\022\001\002\002\001"}, // krb5_name type
  64. {10, "\052\206\110\206\367\022\001\002\002\002"}, // krb5_principal type
  65. {10, "\052\206\110\206\367\022\001\002\002\003"}, // user2user mech id
  66. {9, "\052\206\110\202\367\022\001\002\002"}, // bogus mangled OID from spnego
  67. };
  68. gss_OID_desc * gss_mech_krb5 = oids;
  69. gss_OID_desc * gss_mech_krb5_new = oids+1;
  70. gss_OID_desc * gss_mech_krb5_u2u = oids+4;
  71. gss_OID_desc * gss_mech_krb5_spnego = oids+5;
  72. #ifndef WIN32_CHICAGO
  73. //+-------------------------------------------------------------------------
  74. //
  75. // Function: SpUserModeInitialize
  76. //
  77. // Synopsis: Returns table of usermode functions to caller
  78. //
  79. // Effects:
  80. //
  81. // Arguments:
  82. //
  83. // Requires:
  84. //
  85. // Returns: SUCCESS if version is correct
  86. //
  87. // Notes:
  88. //
  89. //
  90. //--------------------------------------------------------------------------
  91. NTSTATUS
  92. SEC_ENTRY
  93. SpUserModeInitialize(
  94. IN ULONG LsaVersion,
  95. OUT PULONG PackageVersion,
  96. OUT PSECPKG_USER_FUNCTION_TABLE * UserFunctionTable,
  97. OUT PULONG pcTables)
  98. {
  99. if (LsaVersion != SECPKG_INTERFACE_VERSION)
  100. {
  101. DebugLog((DEB_ERROR,"Invalid LSA version: %d. %ws, line %d\n", LsaVersion, THIS_FILE, __LINE__));
  102. return(STATUS_INVALID_PARAMETER);
  103. }
  104. *PackageVersion = SECPKG_INTERFACE_VERSION ;
  105. KerberosUserFunctionTable.InstanceInit = SpInstanceInit;
  106. KerberosUserFunctionTable.MakeSignature = SpMakeSignature;
  107. KerberosUserFunctionTable.VerifySignature = SpVerifySignature;
  108. KerberosUserFunctionTable.SealMessage = SpSealMessage;
  109. KerberosUserFunctionTable.UnsealMessage = SpUnsealMessage;
  110. KerberosUserFunctionTable.GetContextToken = SpGetContextToken;
  111. KerberosUserFunctionTable.QueryContextAttributes = SpQueryContextAttributes;
  112. KerberosUserFunctionTable.CompleteAuthToken = SpCompleteAuthToken;
  113. KerberosUserFunctionTable.InitUserModeContext = SpInitUserModeContext;
  114. KerberosUserFunctionTable.DeleteUserModeContext = SpDeleteUserModeContext;
  115. KerberosUserFunctionTable.FormatCredentials = SpFormatCredentials;
  116. KerberosUserFunctionTable.MarshallSupplementalCreds = SpMarshallSupplementalCreds;
  117. KerberosUserFunctionTable.ExportContext = SpExportSecurityContext;
  118. KerberosUserFunctionTable.ImportContext = SpImportSecurityContext;
  119. *pcTables = 1;
  120. *UserFunctionTable = &KerberosUserFunctionTable;
  121. if (KerberosState != KerberosLsaMode)
  122. {
  123. //
  124. // SafeAllocaInitialize was already called in SpLsaModeInitialize
  125. //
  126. SafeAllocaInitialize(SAFEALLOCA_USE_DEFAULT,
  127. SAFEALLOCA_USE_DEFAULT,
  128. KerbAllocate,
  129. KerbFree);
  130. }
  131. return( STATUS_SUCCESS );
  132. }
  133. #endif // WIN32_CHICAGO
  134. //+-------------------------------------------------------------------------
  135. //
  136. // Function: SpInstanceInit
  137. //
  138. // Synopsis: Initialize an instance of the Kerberos package in a client's
  139. // address space
  140. //
  141. // Effects:
  142. //
  143. // Arguments: Version - Version of the security dll loading the package
  144. // FunctionTable - Contains helper routines for use by Kerberos
  145. // UserFunctions - Receives a copy of Kerberos's user mode
  146. // function table
  147. //
  148. // Requires:
  149. //
  150. // Returns: STATUS_SUCCESS
  151. //
  152. // Notes:
  153. //
  154. //
  155. //--------------------------------------------------------------------------
  156. NTSTATUS NTAPI
  157. SpInstanceInit(
  158. IN ULONG Version,
  159. IN PSECPKG_DLL_FUNCTIONS DllFunctionTable,
  160. OUT PVOID * UserFunctionTable
  161. )
  162. {
  163. NTSTATUS Status = STATUS_SUCCESS;
  164. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  165. if (!KerbGlobalInitialized)
  166. {
  167. KerberosState = KerberosUserMode;
  168. Status = KerbInitGlobalVariables();
  169. if (!NT_SUCCESS(Status))
  170. {
  171. DebugLog((DEB_ERROR,"Failed to initialize global variables: 0x%x. %ws, line %d\n",
  172. Status, THIS_FILE, __LINE__ ));
  173. goto Cleanup;
  174. }
  175. Status = KerbInitContextList();
  176. if (!NT_SUCCESS(Status))
  177. {
  178. DebugLog((DEB_ERROR,"Failed to initialize context list: 0x%x. %ws, line %d\n",
  179. Status, THIS_FILE, __LINE__ ));
  180. goto Cleanup;
  181. }
  182. }
  183. else
  184. {
  185. D_DebugLog((DEB_TRACE,"Re-initializing kerberos from LSA mode to User Mode\n"));
  186. }
  187. UserFunctions = DllFunctionTable;
  188. #ifndef WIN32_CHICAGO
  189. //
  190. // Build the two well known sids we need.
  191. //
  192. if( KerbGlobalLocalSystemSid == NULL )
  193. {
  194. Status = RtlAllocateAndInitializeSid(
  195. &NtAuthority,
  196. 1,
  197. SECURITY_LOCAL_SYSTEM_RID,
  198. 0,0,0,0,0,0,0,
  199. &KerbGlobalLocalSystemSid
  200. );
  201. if (!NT_SUCCESS(Status))
  202. {
  203. goto Cleanup;
  204. }
  205. }
  206. if( KerbGlobalAliasAdminsSid == NULL )
  207. {
  208. Status = RtlAllocateAndInitializeSid(
  209. &NtAuthority,
  210. 2,
  211. SECURITY_BUILTIN_DOMAIN_RID,
  212. DOMAIN_ALIAS_RID_ADMINS,
  213. 0,0,0,0,0,0,
  214. &KerbGlobalAliasAdminsSid
  215. );
  216. if (!NT_SUCCESS(Status))
  217. {
  218. goto Cleanup;
  219. }
  220. }
  221. #endif // WIN32_CHICAGO
  222. KerbGlobalInitialized = TRUE;
  223. Cleanup:
  224. if( !KerbGlobalInitialized && !NT_SUCCESS(Status) )
  225. {
  226. if( KerbGlobalLocalSystemSid != NULL )
  227. {
  228. RtlFreeSid( KerbGlobalLocalSystemSid );
  229. KerbGlobalLocalSystemSid = NULL;
  230. }
  231. if( KerbGlobalAliasAdminsSid != NULL )
  232. {
  233. RtlFreeSid( KerbGlobalAliasAdminsSid );
  234. KerbGlobalAliasAdminsSid = NULL;
  235. }
  236. }
  237. return(Status);
  238. }
  239. //+-------------------------------------------------------------------------
  240. //
  241. // Function: SpDeleteUserModeContext
  242. //
  243. // Synopsis: Deletes a user mode context by unlinking it and then
  244. // dereferencing it.
  245. //
  246. // Effects:
  247. //
  248. // Arguments: ContextHandle - Lsa context handle of the context to delete
  249. //
  250. // Requires:
  251. //
  252. // Returns: STATUS_SUCCESS on success, STATUS_INVALID_HANDLE if the
  253. // context can't be located, SEC_I_NO_LSA_CONTEXT if this was
  254. // created from an exported context
  255. //
  256. // Notes:
  257. //
  258. //
  259. //--------------------------------------------------------------------------
  260. NTSTATUS NTAPI
  261. SpDeleteUserModeContext(
  262. IN LSA_SEC_HANDLE ContextHandle
  263. )
  264. {
  265. NTSTATUS Status = STATUS_SUCCESS;
  266. PKERB_CONTEXT Context = NULL;
  267. D_DebugLog((DEB_TRACE_API,"SpDeleteUserModeContext called\n"));
  268. Status = KerbReferenceContextByLsaHandle(
  269. ContextHandle,
  270. TRUE,
  271. &Context // unlink it
  272. );
  273. if (!NT_SUCCESS(Status))
  274. {
  275. D_DebugLog((DEB_TRACE,"Failed to reference context 0x%x by lsa handle\n",
  276. ContextHandle));
  277. return(STATUS_SUCCESS); // no error code should be returned in this case
  278. }
  279. //
  280. // Make sure we don't try to call the LSA to delete imported contexts
  281. //
  282. KerbReadLockContexts();
  283. if ((Context->ContextAttributes & KERB_CONTEXT_IMPORTED) != 0)
  284. {
  285. Status = SEC_I_NO_LSA_CONTEXT;
  286. }
  287. KerbUnlockContexts();
  288. KerbDereferenceContext(
  289. Context
  290. );
  291. D_DebugLog((DEB_TRACE_API, "SpDeleteUserModeContext returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  292. return(KerbMapKerbNtStatusToNtStatus(Status));
  293. }
  294. //+-------------------------------------------------------------------------
  295. //
  296. // Function: SpInitUserModeContext
  297. //
  298. // Synopsis: Creates a user-mode context from a packed LSA mode context
  299. //
  300. // Effects:
  301. //
  302. // Arguments: ContextHandle - Lsa mode context handle for the context
  303. // PackedContext - A marshalled buffer containing the LSA
  304. // mode context.
  305. //
  306. // Requires:
  307. //
  308. // Returns: STATUS_SUCCESS or STATUS_INSUFFICIENT_RESOURCES
  309. //
  310. // Notes:
  311. //
  312. //
  313. //--------------------------------------------------------------------------
  314. NTSTATUS NTAPI
  315. SpInitUserModeContext(
  316. IN LSA_SEC_HANDLE ContextHandle,
  317. IN PSecBuffer PackedContext
  318. )
  319. {
  320. NTSTATUS Status = STATUS_SUCCESS;
  321. PKERB_CONTEXT Context = NULL;
  322. D_DebugLog((DEB_TRACE_API,"SpInitUserModeContext called\n"));
  323. Status = KerbCreateUserModeContext(
  324. ContextHandle,
  325. PackedContext,
  326. &Context
  327. );
  328. if (!NT_SUCCESS(Status))
  329. {
  330. DebugLog((DEB_ERROR,"Failed to create user mode context: 0x%x. %ws, line %d\n",
  331. Status, THIS_FILE, __LINE__));
  332. goto Cleanup;
  333. }
  334. Cleanup:
  335. if (Context != NULL)
  336. {
  337. KerbDereferenceContext(Context);
  338. }
  339. if (PackedContext->pvBuffer != NULL)
  340. {
  341. FreeContextBuffer(PackedContext->pvBuffer);
  342. PackedContext->pvBuffer = NULL;
  343. }
  344. D_DebugLog((DEB_TRACE_API, "SpInitUserModeContext returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  345. return(KerbMapKerbNtStatusToNtStatus(Status));
  346. }
  347. //+-------------------------------------------------------------------------
  348. //
  349. // Function: SpExportSecurityContext
  350. //
  351. // Synopsis: Exports a security context to another process
  352. //
  353. // Effects: Allocates memory for output
  354. //
  355. // Arguments: ContextHandle - handle to context to export
  356. // Flags - Flags concerning duplication. Allowable flags:
  357. // SECPKG_CONTEXT_EXPORT_DELETE_OLD - causes old context
  358. // to be deleted.
  359. // PackedContext - Receives serialized context to be freed with
  360. // FreeContextBuffer
  361. // TokenHandle - Optionally receives handle to context's token.
  362. //
  363. // Requires:
  364. //
  365. // Returns:
  366. //
  367. // Notes:
  368. //
  369. //
  370. //--------------------------------------------------------------------------
  371. NTSTATUS
  372. SpExportSecurityContext(
  373. IN LSA_SEC_HANDLE ContextHandle,
  374. IN ULONG Flags,
  375. OUT PSecBuffer PackedContext,
  376. OUT PHANDLE TokenHandle
  377. )
  378. {
  379. PKERB_CONTEXT Context = NULL;
  380. NTSTATUS Status = STATUS_SUCCESS;
  381. BOOLEAN MappedContext = FALSE;
  382. D_DebugLog((DEB_TRACE_API,"SpExportContext Called\n"));
  383. D_DebugLog((DEB_TRACE_USER,"Exporting context 0x%p, flags 0x%x\n",ContextHandle, Flags));
  384. //
  385. // We don't support reseting the context
  386. //
  387. if ((Flags & SECPKG_CONTEXT_EXPORT_RESET_NEW) != 0)
  388. {
  389. return(SEC_E_UNSUPPORTED_FUNCTION);
  390. }
  391. if (ARGUMENT_PRESENT(TokenHandle))
  392. {
  393. *TokenHandle = NULL;
  394. }
  395. PackedContext->pvBuffer = NULL;
  396. PackedContext->cbBuffer = 0;
  397. PackedContext->BufferType = 0;
  398. Status = KerbReferenceContextByLsaHandle(
  399. ContextHandle,
  400. FALSE, // don't unlink
  401. &Context
  402. );
  403. if (!NT_SUCCESS(Status))
  404. {
  405. DebugLog((DEB_ERROR, "Invalid handle supplied for ExportSecurityContext(%p) Status = 0x%x. %ws, line %d\n",
  406. ContextHandle, Status, THIS_FILE, __LINE__));
  407. goto Cleanup;
  408. }
  409. Status = KerbMapContext(
  410. Context,
  411. &MappedContext,
  412. PackedContext
  413. );
  414. if (!NT_SUCCESS(Status))
  415. {
  416. goto Cleanup;
  417. }
  418. DsysAssert(MappedContext);
  419. //
  420. // We need to figure out if this was exported
  421. //
  422. ((PKERB_CONTEXT)PackedContext->pvBuffer)->ContextAttributes |= KERB_CONTEXT_EXPORTED;
  423. //
  424. // Now either duplicate the token or copy it.
  425. //
  426. if (ARGUMENT_PRESENT(TokenHandle))
  427. {
  428. KerbWriteLockContexts();
  429. if ((Flags & SECPKG_CONTEXT_EXPORT_DELETE_OLD) != 0)
  430. {
  431. *TokenHandle = Context->TokenHandle;
  432. Context->TokenHandle = NULL;
  433. }
  434. else
  435. {
  436. Status = NtDuplicateObject(
  437. NtCurrentProcess(),
  438. Context->TokenHandle,
  439. NULL,
  440. TokenHandle,
  441. 0, // no new access
  442. 0, // no handle attributes
  443. DUPLICATE_SAME_ACCESS
  444. );
  445. }
  446. KerbUnlockContexts();
  447. if (!NT_SUCCESS(Status))
  448. {
  449. goto Cleanup;
  450. }
  451. }
  452. Cleanup:
  453. if (Context != NULL)
  454. {
  455. KerbDereferenceContext(Context);
  456. }
  457. D_DebugLog((DEB_TRACE_API, "SpExportSecurityContext returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  458. return(KerbMapKerbNtStatusToNtStatus(Status));
  459. }
  460. //+-------------------------------------------------------------------------
  461. //
  462. // Function: SpImportSecurityContext
  463. //
  464. // Synopsis:
  465. //
  466. // Effects:
  467. //
  468. // Arguments:
  469. //
  470. // Requires:
  471. //
  472. // Returns:
  473. //
  474. // Notes:
  475. //
  476. //
  477. //--------------------------------------------------------------------------
  478. NTSTATUS
  479. SpImportSecurityContext(
  480. IN PSecBuffer PackedContext,
  481. IN HANDLE Token,
  482. OUT PLSA_SEC_HANDLE ContextHandle
  483. )
  484. {
  485. NTSTATUS Status = STATUS_SUCCESS;
  486. PKERB_CONTEXT Context = NULL;
  487. D_DebugLog((DEB_TRACE_API,"SpImportSecurityContext called\n"));
  488. SecBuffer TmpSecBuffer = (*PackedContext);
  489. #if _WIN64
  490. PBYTE TmpBuffer = NULL;
  491. //
  492. // See if this is an aligned buffer - SAP has some IPC mechanisms
  493. // which don't gaurantee that the pSecBuffer is actually 8 bit aligned,
  494. // so we should. Blech!
  495. //
  496. if (!IS_POINTER_ALIGNED(PackedContext->pvBuffer))
  497. {
  498. TmpBuffer = (PBYTE) UserFunctions->AllocateHeap(((UNALIGNED SecBuffer *)PackedContext)->cbBuffer);
  499. if ( NULL == TmpBuffer )
  500. {
  501. Status = STATUS_NO_MEMORY;
  502. goto Cleanup;
  503. }
  504. //
  505. // Align it. Note that the members inside of this buffer were already
  506. // aligned on export, so this should be all we need to do
  507. //
  508. RtlCopyMemory(
  509. TmpBuffer,
  510. PackedContext->pvBuffer,
  511. PackedContext->cbBuffer
  512. );
  513. TmpSecBuffer.pvBuffer = TmpBuffer;
  514. }
  515. #endif // _WIN64
  516. Status = KerbCreateUserModeContext(
  517. 0, // no lsa context
  518. &TmpSecBuffer,
  519. &Context
  520. );
  521. if (!NT_SUCCESS(Status))
  522. {
  523. DebugLog((DEB_ERROR,"Failed to create user mode context: 0x%x. %ws, line %d\n",
  524. Status, THIS_FILE, __LINE__));
  525. goto Cleanup;
  526. }
  527. KerbWriteLockContexts();
  528. Context->TokenHandle = Token;
  529. Context->ContextAttributes |= KERB_CONTEXT_IMPORTED;
  530. *ContextHandle = KerbGetContextHandle(Context);
  531. //Context->LsaContextHandle = *ContextHandle;
  532. KerbUnlockContexts();
  533. Cleanup:
  534. if (Context != NULL)
  535. {
  536. KerbDereferenceContext(Context);
  537. }
  538. #if _WIN64
  539. if ( TmpBuffer )
  540. {
  541. UserFunctions->FreeHeap(TmpBuffer);
  542. }
  543. #endif // _WIN64
  544. D_DebugLog((DEB_TRACE_API, "SpImportSecurityContext returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  545. D_DebugLog((DEB_TRACE_USER," Imported Context handle = 0x%x\n",*ContextHandle));
  546. return(KerbMapKerbNtStatusToNtStatus(Status));
  547. }
  548. //+-------------------------------------------------------------------------
  549. //
  550. // Function: KerbGetChecksumAndEncryptionType
  551. //
  552. // Synopsis: Gets the ChecksumType and the EncryptionType
  553. //
  554. // Effects:
  555. //
  556. // Arguments: Context - Context to use for signing
  557. // QualityOfProtection - flags indicating what kind of checksum
  558. // to use
  559. // ChecksumType - Receives the type of checksum to use
  560. // EncryptionType - Receives the type of encryption to use
  561. //
  562. // Requires: The context must be write locked
  563. //
  564. // Returns:
  565. //
  566. // Notes:
  567. //
  568. //
  569. //--------------------------------------------------------------------------
  570. NTSTATUS
  571. KerbGetChecksumAndEncryptionType(
  572. IN PKERB_CONTEXT Context,
  573. IN ULONG QualityOfProtection,
  574. OUT PLONG ChecksumType,
  575. OUT PLONG EncryptionType
  576. )
  577. {
  578. NTSTATUS Status = STATUS_SUCCESS;
  579. //
  580. // If the keytype is an MS keytype, we need to use an MS encryption
  581. // scheme.
  582. //
  583. if (!KERB_IS_DES_ENCRYPTION(Context->SessionKey.keytype))
  584. {
  585. #ifndef DONT_SUPPORT_OLD_TYPES_USER
  586. if (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_OLD)
  587. {
  588. *ChecksumType = KERB_CHECKSUM_HMAC_MD5;
  589. *EncryptionType = KERB_ETYPE_RC4_PLAIN_OLD;
  590. }
  591. else if (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_OLD_EXP)
  592. {
  593. *ChecksumType = KERB_CHECKSUM_HMAC_MD5;
  594. *EncryptionType = KERB_ETYPE_RC4_PLAIN_OLD_EXP;
  595. }
  596. else
  597. #endif
  598. if (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_NT)
  599. {
  600. *ChecksumType = KERB_CHECKSUM_HMAC_MD5;
  601. *EncryptionType = KERB_ETYPE_RC4_PLAIN;
  602. }
  603. else
  604. {
  605. DsysAssert (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_NT_EXP);
  606. *ChecksumType = KERB_CHECKSUM_HMAC_MD5;
  607. *EncryptionType = KERB_ETYPE_RC4_PLAIN_EXP;
  608. }
  609. }
  610. else
  611. {
  612. //
  613. // Use the exportable version if necessasry
  614. //
  615. *EncryptionType = KERB_ETYPE_DES_PLAIN;
  616. switch(QualityOfProtection)
  617. {
  618. case GSS_KRB5_INTEG_C_QOP_MD5:
  619. *ChecksumType = KERB_CHECKSUM_MD25;
  620. break;
  621. case KERB_WRAP_NO_ENCRYPT:
  622. case GSS_KRB5_INTEG_C_QOP_DEFAULT:
  623. case GSS_KRB5_INTEG_C_QOP_DES_MD5:
  624. *ChecksumType = KERB_CHECKSUM_DES_MAC_MD5;
  625. break;
  626. case GSS_KRB5_INTEG_C_QOP_DES_MAC:
  627. *ChecksumType = KERB_CHECKSUM_DES_MAC;
  628. break;
  629. default:
  630. DebugLog((DEB_ERROR,"Invalid quality of protection sent to MakeSignature: %d. %ws, line %d\n",
  631. QualityOfProtection, THIS_FILE, __LINE__ ));
  632. Status = STATUS_INVALID_PARAMETER;
  633. goto Cleanup;
  634. }
  635. }
  636. Cleanup:
  637. return(Status);
  638. }
  639. //+-------------------------------------------------------------------------
  640. //
  641. // Function: KerbMakeSignatureToken
  642. //
  643. // Synopsis: Makes the signature token for a signed or sealed message
  644. //
  645. // Effects:
  646. //
  647. // Arguments: Context - Context to use for signing
  648. // QualityOfProtection - flags indicating what kind of checksum
  649. // to use
  650. // SignatureBuffer - Buffer in which to place signature
  651. // TotalBufferSize - Total size of all buffers to be signed
  652. // Encrypt - if TRUE, then prepare a header for an encrypted buffer
  653. // SuppliedNonce - Nonce supplied by caller, used for datagram
  654. //
  655. // Requires: The context must be write locked
  656. //
  657. // Returns:
  658. //
  659. // Notes:
  660. //
  661. //
  662. //--------------------------------------------------------------------------
  663. NTSTATUS
  664. KerbMakeSignatureToken(
  665. IN PKERB_CONTEXT Context,
  666. IN ULONG QualityOfProtection,
  667. IN PSecBuffer SignatureBuffer,
  668. IN ULONG TotalBufferSize,
  669. IN BOOLEAN Encrypt,
  670. IN ULONG SuppliedNonce,
  671. OUT PKERB_GSS_SIGNATURE * OutputSignature,
  672. OUT PULONG SequenceNumber
  673. )
  674. {
  675. NTSTATUS Status = STATUS_SUCCESS;
  676. PKERB_GSS_SIGNATURE Signature;
  677. PKERB_GSS_SEAL_SIGNATURE SealSignature;
  678. ULONG MessageSize;
  679. ULONG SignatureSize;
  680. PULONG Nonce;
  681. gss_OID MechUsed;
  682. BOOLEAN GssCompatible = TRUE;
  683. //
  684. // Compute the size of the header. For encryption headers, we need
  685. // to round up the size of the data & add 8 bytes for a confounder.
  686. //
  687. if ((Context->ContextFlags & ISC_RET_USED_DCE_STYLE) != 0 ||
  688. (Context->ContextFlags & ISC_RET_DATAGRAM) != 0)
  689. {
  690. GssCompatible = FALSE;
  691. }
  692. //
  693. // Since RPC doesn't carry around the size of the size of the
  694. // signature bufer, we use it in the header. This break rfc1964 compat.
  695. //
  696. if (!GssCompatible || !Encrypt)
  697. {
  698. TotalBufferSize = 0;
  699. }
  700. // D_DebugLog((DEB_TRACE, "KerbMakeSignatureToken ContextAttributes %#x\n", Context->ContextAttributes));
  701. if ((Context->ContextAttributes & KERB_CONTEXT_USER_TO_USER) != 0)
  702. {
  703. D_DebugLog((DEB_TRACE_U2U, "KerbMakeSignatureToken u2u oid used\n"));
  704. MechUsed = gss_mech_krb5_u2u;
  705. }
  706. else
  707. {
  708. MechUsed = gss_mech_krb5_new;
  709. }
  710. if (Encrypt)
  711. {
  712. //
  713. // NOTE: according to rfc1964, buffers that are an even multiple of
  714. // 8 bytes have 8 bytes of zeros appended. Because we cannot modify
  715. // the input buffers, the caller will have to do this for us.
  716. //
  717. MessageSize = TotalBufferSize + sizeof(KERB_GSS_SEAL_SIGNATURE);
  718. }
  719. else
  720. {
  721. MessageSize = TotalBufferSize + sizeof(KERB_GSS_SIGNATURE);
  722. }
  723. SignatureSize = g_token_size(MechUsed, MessageSize) - TotalBufferSize;
  724. //
  725. // Make Dave happy (verify that the supplied signature buffer is large
  726. // enough for a signature):
  727. //
  728. if (SignatureBuffer->cbBuffer < SignatureSize)
  729. {
  730. Status = STATUS_BUFFER_TOO_SMALL;
  731. goto Cleanup;
  732. }
  733. //
  734. // create the header with the GSS oid
  735. //
  736. Signature = (PKERB_GSS_SIGNATURE) SignatureBuffer->pvBuffer;
  737. g_make_token_header(
  738. MechUsed,
  739. MessageSize,
  740. (PUCHAR *) &Signature,
  741. (Encrypt ? KG_TOK_WRAP_MSG : KG_TOK_MIC_MSG)
  742. );
  743. //
  744. // Fill in the header information according to RFC1964
  745. //
  746. Signature->SignatureAlgorithm[1] = KERB_GSS_SIG_SECOND;
  747. //
  748. // If the keytype is an MS keytype, we need to use an MS encryption
  749. // scheme.
  750. //
  751. if (!KERB_IS_DES_ENCRYPTION(Context->SessionKey.keytype))
  752. {
  753. #ifndef DONT_SUPPORT_OLD_TYPES_USER
  754. if (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_OLD)
  755. {
  756. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_HMAC;
  757. if (Encrypt)
  758. {
  759. Signature->SealAlgorithm[1] = KERB_GSS_SIG_SECOND;
  760. Signature->SealAlgorithm[0] = KERB_GSS_SEAL_RC4_OLD;
  761. }
  762. }
  763. else if (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_OLD_EXP)
  764. {
  765. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_HMAC;
  766. if (Encrypt)
  767. {
  768. Signature->SealAlgorithm[1] = KERB_GSS_SIG_SECOND;
  769. Signature->SealAlgorithm[0] = KERB_GSS_SEAL_RC4_OLD;
  770. }
  771. }
  772. else
  773. #endif
  774. if (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_NT)
  775. {
  776. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_HMAC;
  777. if (Encrypt)
  778. {
  779. Signature->SealAlgorithm[1] = KERB_GSS_SIG_SECOND;
  780. Signature->SealAlgorithm[0] = KERB_GSS_SEAL_RC4;
  781. }
  782. }
  783. else
  784. {
  785. DsysAssert (Context->SessionKey.keytype == KERB_ETYPE_RC4_HMAC_NT_EXP);
  786. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_HMAC;
  787. if (Encrypt)
  788. {
  789. Signature->SealAlgorithm[1] = KERB_GSS_SIG_SECOND;
  790. Signature->SealAlgorithm[0] = KERB_GSS_SEAL_RC4;
  791. }
  792. }
  793. //
  794. // if we aren't actually encrypting, reset the encryption alg
  795. //
  796. if (QualityOfProtection == KERB_WRAP_NO_ENCRYPT)
  797. {
  798. if (!Encrypt)
  799. {
  800. DebugLog((DEB_ERROR,"KERB_WRAP_NO_ENCRYPT flag passed to MakeSignature!\n"));
  801. Status = STATUS_INVALID_PARAMETER;
  802. goto Cleanup;
  803. }
  804. //
  805. // In this case use the default, but we will not encrypt
  806. //
  807. Signature->SealAlgorithm[1] = KERB_GSS_NO_SEAL_SECOND;
  808. Signature->SealAlgorithm[0] = KERB_GSS_NO_SEAL;
  809. }
  810. }
  811. else
  812. {
  813. if (Encrypt)
  814. {
  815. Signature->SealAlgorithm[1] = KERB_GSS_SIG_SECOND;
  816. Signature->SealAlgorithm[0] = KERB_GSS_SEAL_DES_CBC;
  817. }
  818. //
  819. // Use the exportable version if necessasry
  820. //
  821. switch(QualityOfProtection)
  822. {
  823. case GSS_KRB5_INTEG_C_QOP_MD5:
  824. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_MD25;
  825. break;
  826. case KERB_WRAP_NO_ENCRYPT:
  827. if (!Encrypt)
  828. {
  829. DebugLog((DEB_ERROR,"KERB_WRAP_NO_ENCRYPT flag passed to MakeSignature!\n"));
  830. Status = STATUS_INVALID_PARAMETER;
  831. goto Cleanup;
  832. }
  833. //
  834. // In this case use the default, but we will not encrypt
  835. //
  836. Signature->SealAlgorithm[1] = KERB_GSS_NO_SEAL_SECOND;
  837. Signature->SealAlgorithm[0] = KERB_GSS_NO_SEAL;
  838. case GSS_KRB5_INTEG_C_QOP_DEFAULT:
  839. case GSS_KRB5_INTEG_C_QOP_DES_MD5:
  840. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_DES_MAC_MD5;
  841. break;
  842. case GSS_KRB5_INTEG_C_QOP_DES_MAC:
  843. Signature->SignatureAlgorithm[0] = KERB_GSS_SIG_DES_MAC;
  844. break;
  845. default:
  846. DebugLog((DEB_ERROR,"Invalid quality of protection sent to MakeSignature: %d. %ws, line %d\n",
  847. QualityOfProtection, THIS_FILE, __LINE__ ));
  848. Status = STATUS_INVALID_PARAMETER;
  849. goto Cleanup;
  850. }
  851. }
  852. //
  853. // Put in the filler - it is different for signing & sealing
  854. //
  855. if (Encrypt)
  856. {
  857. memset(Signature->SealFiller,0xff,2);
  858. }
  859. else
  860. {
  861. memset(Signature->SignFiller,0xff,4);
  862. }
  863. //
  864. // Inbound contexts get a high dword of 0xffffffff, outbound gets
  865. // 0x00000000.
  866. //
  867. Nonce = &Context->Nonce;
  868. if (Context->ContextAttributes & KERB_CONTEXT_INBOUND)
  869. {
  870. *(ULONG UNALIGNED *)(&Signature->SequenceNumber[4]) = 0xffffffff;
  871. }
  872. else
  873. {
  874. DsysAssert((Context->ContextAttributes & KERB_CONTEXT_OUTBOUND) != 0);
  875. *(ULONG UNALIGNED *)(&Signature->SequenceNumber[4]) = 0x00000000;
  876. }
  877. //
  878. // If this is datagram, or integrity without replay & sequence detection,
  879. // use the nonce from the caller
  880. //
  881. if (((Context->ContextFlags & ISC_RET_DATAGRAM) != 0) ||
  882. ((Context->ContextFlags & (ISC_RET_INTEGRITY | ISC_RET_SEQUENCE_DETECT | ISC_RET_REPLAY_DETECT)) == ISC_RET_INTEGRITY))
  883. {
  884. Nonce = &SuppliedNonce;
  885. }
  886. if (!KERB_IS_DES_ENCRYPTION(Context->SessionKey.keytype))
  887. {
  888. Signature->SequenceNumber[0] = (UCHAR) ((*Nonce & 0xff000000) >> 24);
  889. Signature->SequenceNumber[1] = (UCHAR) ((*Nonce & 0x00ff0000) >> 16);
  890. Signature->SequenceNumber[2] = (UCHAR) ((*Nonce & 0x0000ff00) >> 8);
  891. Signature->SequenceNumber[3] = (UCHAR) (*Nonce & 0x000000ff);
  892. }
  893. else
  894. {
  895. Signature->SequenceNumber[3] = (UCHAR) ((*Nonce & 0xff000000) >> 24);
  896. Signature->SequenceNumber[2] = (UCHAR) ((*Nonce & 0x00ff0000) >> 16);
  897. Signature->SequenceNumber[1] = (UCHAR) ((*Nonce & 0x0000ff00) >> 8);
  898. Signature->SequenceNumber[0] = (UCHAR) (*Nonce & 0x000000ff);
  899. }
  900. (*Nonce)++;
  901. *SequenceNumber = *(ULONG UNALIGNED *)Signature->SequenceNumber;
  902. D_DebugLog((DEB_TRACE_USER,"Makign signature buffer (encrypt = %d) with nonce 0x%x\n",
  903. Encrypt,
  904. *SequenceNumber
  905. ));
  906. //
  907. // If we are encrypting, add the confounder to the end of the signature
  908. //
  909. if (Encrypt)
  910. {
  911. SealSignature = (PKERB_GSS_SEAL_SIGNATURE) Signature;
  912. KerbRandomFill(
  913. SealSignature->Confounder,
  914. KERB_GSS_SIG_CONFOUNDER_SIZE
  915. );
  916. }
  917. //
  918. // Set the size of the signature
  919. //
  920. SignatureBuffer->cbBuffer = SignatureSize;
  921. *OutputSignature = Signature;
  922. Cleanup:
  923. return (Status);
  924. }
  925. //+-------------------------------------------------------------------------
  926. //
  927. // Function: KerbVerifySignatureToken
  928. //
  929. // Synopsis: Verifies the header on a signed or sealed message
  930. //
  931. // Effects:
  932. //
  933. // Arguments: Context - context to use for verification
  934. // SignatureBuffer - Buffer containing signature
  935. // TotalBufferSize - Size of all buffers signed/encrypted
  936. // Decrypt - TRUE if we are unsealing
  937. // SuppliedNonce - Nonce supplied by caller, used for datagram
  938. // QualityOfProtection - returns GSS quality of protection flags
  939. // ChecksumType - Type of checksum used in this signature
  940. // EncryptionType - Type of encryption used in this signature
  941. //
  942. // Requires:
  943. //
  944. // Returns:
  945. //
  946. // Notes:
  947. //
  948. //
  949. //--------------------------------------------------------------------------
  950. NTSTATUS
  951. KerbVerifySignatureToken(
  952. IN PKERB_CONTEXT Context,
  953. IN PSecBuffer SignatureBuffer,
  954. IN ULONG TotalBufferSize,
  955. IN BOOLEAN Decrypt,
  956. IN ULONG SuppliedNonce,
  957. OUT PKERB_GSS_SIGNATURE * OutputSignature,
  958. OUT PULONG QualityOfProtection,
  959. OUT PLONG ChecksumType,
  960. OUT PCRYPTO_SYSTEM * CryptSystem,
  961. OUT PULONG SequenceNumber
  962. )
  963. {
  964. NTSTATUS Status = STATUS_SUCCESS;
  965. ULONG SignatureSize = 0;
  966. UCHAR Nonce[8];
  967. PCRYPT_STATE_BUFFER CryptBuffer = NULL;
  968. ULONG OutputSize;
  969. LONG EncryptionType = 0;
  970. PCRYPTO_SYSTEM LocalCryptSystem = NULL ;
  971. PKERB_GSS_SIGNATURE Signature;
  972. PULONG ContextNonce;
  973. gss_OID MechUsed;
  974. //
  975. // Since RPC doesn't carry around the size of the size of the
  976. // signature bufer, we use it in the header. This break rfc1964 compat.
  977. //
  978. if (!Decrypt ||
  979. ((Context->ContextFlags & ISC_RET_USED_DCE_STYLE) != 0) ||
  980. ((Context->ContextFlags & ISC_RET_DATAGRAM) != 0))
  981. {
  982. TotalBufferSize = 0;
  983. }
  984. //
  985. // Verify the signature header
  986. //
  987. D_DebugLog((DEB_TRACE, "KerbVerifySignatureToken ContextAttributes %#x\n", Context->ContextAttributes));
  988. if ((Context->ContextAttributes & KERB_CONTEXT_USER_TO_USER) != 0)
  989. {
  990. D_DebugLog((DEB_TRACE_U2U, "KerbVerifySignatureToken MechUsed = gss_mech_krb5_u2u\n"));
  991. MechUsed = gss_mech_krb5_u2u;
  992. }
  993. else
  994. {
  995. MechUsed = gss_mech_krb5_new;
  996. }
  997. Signature = (PKERB_GSS_SIGNATURE) SignatureBuffer->pvBuffer;
  998. if (!g_verify_token_header(
  999. MechUsed,
  1000. (INT *) &SignatureSize,
  1001. (PUCHAR *) &Signature,
  1002. (Decrypt ? KG_TOK_WRAP_MSG : KG_TOK_MIC_MSG),
  1003. SignatureBuffer->cbBuffer + TotalBufferSize))
  1004. {
  1005. //Status = SEC_E_MESSAGE_ALTERED; bug 28448
  1006. Status = SEC_E_INVALID_TOKEN;
  1007. }
  1008. //
  1009. // If that didn't work, try with the old mech. Need this is for DCE clients
  1010. // for whom we can't tell what mech they use.
  1011. //
  1012. if (!NT_SUCCESS(Status) && ((Context->ContextFlags & ISC_RET_USED_DCE_STYLE) != 0))
  1013. {
  1014. Signature = (PKERB_GSS_SIGNATURE) SignatureBuffer->pvBuffer;
  1015. if (!g_verify_token_header(
  1016. gss_mech_krb5,
  1017. (INT *) &SignatureSize,
  1018. (PUCHAR *) &Signature,
  1019. (Decrypt ? KG_TOK_WRAP_MSG : KG_TOK_MIC_MSG),
  1020. SignatureBuffer->cbBuffer + TotalBufferSize))
  1021. {
  1022. //Status = SEC_E_MESSAGE_ALTERED; bug 28448
  1023. Status = SEC_E_INVALID_TOKEN;
  1024. }
  1025. else
  1026. {
  1027. D_DebugLog((DEB_TRACE, "KerbVerifySignatureToken Signature gss_mech_krb5\n"));
  1028. Status = STATUS_SUCCESS;
  1029. }
  1030. }
  1031. //
  1032. // MS RPC clients don't send the size properly, so set the total size
  1033. // to zero and try again.
  1034. //
  1035. if (Decrypt && !NT_SUCCESS(Status))
  1036. {
  1037. TotalBufferSize = 0;
  1038. Signature = (PKERB_GSS_SIGNATURE) SignatureBuffer->pvBuffer;
  1039. if (!g_verify_token_header(
  1040. MechUsed,
  1041. (INT *) &SignatureSize,
  1042. (PUCHAR *) &Signature,
  1043. (Decrypt ? KG_TOK_WRAP_MSG : KG_TOK_MIC_MSG),
  1044. SignatureBuffer->cbBuffer + TotalBufferSize))
  1045. {
  1046. //Status = SEC_E_MESSAGE_ALTERED; bug 28448
  1047. Status = SEC_E_INVALID_TOKEN;
  1048. }
  1049. else
  1050. {
  1051. D_DebugLog((DEB_TRACE, "KerbVerifySignatureToken Signature MechUsed\n"));
  1052. Status = STATUS_SUCCESS;
  1053. }
  1054. //
  1055. // If that didn't work, try with the old mech. Need this is for DCE clients
  1056. // for whom we can't tell what mech they use.
  1057. //
  1058. if (!NT_SUCCESS(Status) && ((Context->ContextFlags & ISC_RET_USED_DCE_STYLE) != 0))
  1059. {
  1060. Signature = (PKERB_GSS_SIGNATURE) SignatureBuffer->pvBuffer;
  1061. if (!g_verify_token_header(
  1062. gss_mech_krb5,
  1063. (INT *) &SignatureSize,
  1064. (PUCHAR *) &Signature,
  1065. (Decrypt ? KG_TOK_WRAP_MSG : KG_TOK_MIC_MSG),
  1066. SignatureBuffer->cbBuffer + TotalBufferSize))
  1067. {
  1068. //Status = SEC_E_MESSAGE_ALTERED; bug 28448
  1069. Status = SEC_E_INVALID_TOKEN;
  1070. }
  1071. else
  1072. {
  1073. D_DebugLog((DEB_TRACE, "KerbVerifySignatureToken Signature gss_mech_krb5\n"));
  1074. Status = STATUS_SUCCESS;
  1075. }
  1076. }
  1077. }
  1078. //
  1079. // Protection from bad Signature Size
  1080. //
  1081. if (SignatureSize == 0)
  1082. {
  1083. D_DebugLog((DEB_ERROR, "Bad Signature %ws, %d\n", THIS_FILE, __LINE__));
  1084. Status = SEC_E_MESSAGE_ALTERED;
  1085. goto Cleanup;
  1086. }
  1087. //
  1088. // Subtract the total buffer size from Signature size to get the real
  1089. // size of the signature.
  1090. //
  1091. SignatureSize -= TotalBufferSize;
  1092. //
  1093. // Make sure the signature is big enough. We can't enforce a strict
  1094. // size because RPC will transmit the maximum number of bytes instead
  1095. // of the actual number.
  1096. //
  1097. if ((Decrypt && (SignatureSize < sizeof(KERB_GSS_SEAL_SIGNATURE))) ||
  1098. (!Decrypt && (SignatureSize < sizeof(KERB_GSS_SIGNATURE))))
  1099. {
  1100. //Status = SEC_E_MESSAGE_ALTERED; bug 28448
  1101. Status = SEC_E_INVALID_TOKEN;
  1102. goto Cleanup;
  1103. }
  1104. //
  1105. // Verify the sequence number
  1106. //
  1107. if (Signature->SignatureAlgorithm[1] != KERB_GSS_SIG_SECOND)
  1108. {
  1109. D_DebugLog((DEB_ERROR, "Not KERB_GSS_SIG_SECOND %ws, %d\n", THIS_FILE, __LINE__));
  1110. Status = SEC_E_MESSAGE_ALTERED;
  1111. goto Cleanup;
  1112. }
  1113. //
  1114. // Figure out the algorithm
  1115. //
  1116. switch(Context->SessionKey.keytype) {
  1117. case KERB_ETYPE_DES_CBC_MD5:
  1118. case KERB_ETYPE_DES_CBC_CRC:
  1119. EncryptionType = KERB_ETYPE_DES_PLAIN;
  1120. break;
  1121. case KERB_ETYPE_RC4_HMAC_OLD_EXP:
  1122. EncryptionType = KERB_ETYPE_RC4_PLAIN_OLD_EXP;
  1123. break;
  1124. case KERB_ETYPE_RC4_HMAC_OLD:
  1125. EncryptionType = KERB_ETYPE_RC4_PLAIN_OLD;
  1126. break;
  1127. case KERB_ETYPE_RC4_HMAC_NT_EXP:
  1128. EncryptionType = KERB_ETYPE_RC4_PLAIN_EXP;
  1129. break;
  1130. case KERB_ETYPE_RC4_HMAC_NT:
  1131. EncryptionType = KERB_ETYPE_RC4_PLAIN;
  1132. break;
  1133. default:
  1134. DebugLog((DEB_ERROR,"Unknown key type: %d. %ws, %d\n",
  1135. Context->SessionKey.keytype,
  1136. THIS_FILE, __LINE__ ));
  1137. Status = STATUS_INTERNAL_ERROR;
  1138. goto Cleanup;
  1139. }
  1140. //
  1141. // if the key is exportable, make sure to use the exportable plain
  1142. // version.
  1143. //
  1144. switch(Signature->SignatureAlgorithm[0]) {
  1145. case KERB_GSS_SIG_MD25:
  1146. *QualityOfProtection = GSS_KRB5_INTEG_C_QOP_MD5;
  1147. *ChecksumType = KERB_CHECKSUM_MD25;
  1148. break;
  1149. case KERB_GSS_SIG_DES_MAC_MD5:
  1150. *QualityOfProtection = GSS_KRB5_INTEG_C_QOP_DES_MD5;
  1151. *ChecksumType = KERB_CHECKSUM_DES_MAC_MD5;
  1152. break;
  1153. case KERB_GSS_SIG_DES_MAC:
  1154. *QualityOfProtection = GSS_KRB5_INTEG_C_QOP_DES_MAC;
  1155. *ChecksumType = KERB_CHECKSUM_DES_MAC;
  1156. break;
  1157. case KERB_GSS_SIG_HMAC:
  1158. *QualityOfProtection = GSS_KRB5_INTEG_C_QOP_DEFAULT;
  1159. *ChecksumType = KERB_CHECKSUM_HMAC_MD5;
  1160. break;
  1161. default:
  1162. DebugLog((DEB_ERROR,"Invalid signature type to VerifySignature: %d. %ws, line %d\n",
  1163. Signature->SignatureAlgorithm[0], THIS_FILE, __LINE__ ));
  1164. Status = SEC_E_MESSAGE_ALTERED;
  1165. goto Cleanup;
  1166. }
  1167. if (Decrypt)
  1168. {
  1169. if ((Signature->SealAlgorithm[1] == KERB_GSS_NO_SEAL_SECOND) &&
  1170. (Signature->SealAlgorithm[0] == KERB_GSS_NO_SEAL))
  1171. {
  1172. *QualityOfProtection = KERB_WRAP_NO_ENCRYPT;
  1173. }
  1174. else
  1175. {
  1176. if (Signature->SealAlgorithm[1] != KERB_GSS_SIG_SECOND)
  1177. {
  1178. D_DebugLog((DEB_ERROR, "Not KERB_GSS_SIG_SECOND %ws, %d\n", THIS_FILE, __LINE__));
  1179. Status = SEC_E_MESSAGE_ALTERED;
  1180. goto Cleanup;
  1181. }
  1182. //
  1183. // Verify the seal algorithm
  1184. //
  1185. switch(EncryptionType) {
  1186. case KERB_ETYPE_DES_PLAIN:
  1187. if (Signature->SealAlgorithm[0] != KERB_GSS_SEAL_DES_CBC)
  1188. {
  1189. DebugLog((DEB_ERROR,"Trying to mix encryption types. %ws, line %d\n", THIS_FILE, __LINE__));
  1190. Status = SEC_E_MESSAGE_ALTERED;
  1191. goto Cleanup;
  1192. }
  1193. break;
  1194. case KERB_ETYPE_RC4_PLAIN_OLD_EXP:
  1195. case KERB_ETYPE_RC4_PLAIN_OLD:
  1196. if (Signature->SealAlgorithm[0] != KERB_GSS_SEAL_RC4_OLD)
  1197. {
  1198. DebugLog((DEB_ERROR,"Trying to mix encryption types. %ws, line %d\n", THIS_FILE, __LINE__));
  1199. Status = SEC_E_MESSAGE_ALTERED;
  1200. goto Cleanup;
  1201. }
  1202. break;
  1203. case KERB_ETYPE_RC4_PLAIN_EXP:
  1204. case KERB_ETYPE_RC4_PLAIN:
  1205. if (Signature->SealAlgorithm[0] != KERB_GSS_SEAL_RC4)
  1206. {
  1207. DebugLog((DEB_ERROR, "Trying to mix encryption types. %ws, line %d\n", THIS_FILE, __LINE__));
  1208. Status = SEC_E_MESSAGE_ALTERED;
  1209. goto Cleanup;
  1210. }
  1211. break;
  1212. default:
  1213. DebugLog((DEB_ERROR,"Invalid seal type to VerifySignature: %d, %d. %ws, line %d\n",
  1214. Signature->SealAlgorithm[0], EncryptionType, THIS_FILE, __LINE__ ));
  1215. Status = SEC_E_MESSAGE_ALTERED;
  1216. goto Cleanup;
  1217. }
  1218. }
  1219. }
  1220. //
  1221. // Check the filler
  1222. //
  1223. if ((Decrypt && (*(USHORT UNALIGNED *) Signature->SealFiller != 0xffff)) ||
  1224. (!Decrypt && (*(ULONG UNALIGNED *) Signature->SignFiller != 0xffffffff)))
  1225. {
  1226. D_DebugLog((DEB_ERROR, "Bad filler %ws, %d\n", THIS_FILE, __LINE__));
  1227. Status = SEC_E_MESSAGE_ALTERED;
  1228. goto Cleanup;
  1229. }
  1230. //
  1231. // Verify the sequence number. To do this we need to decrypt it with
  1232. // the session key with the checksum as the IV.
  1233. //
  1234. Status = CDLocateCSystem(EncryptionType, &LocalCryptSystem);
  1235. if (!NT_SUCCESS(Status))
  1236. {
  1237. DebugLog((DEB_ERROR,"Failed to load %d crypt system: 0x%x. %ws, line %d\n",EncryptionType,Status, THIS_FILE, __LINE__));
  1238. goto Cleanup;
  1239. }
  1240. //
  1241. // Now we need to Decrypt the sequence number, using the checksum as the
  1242. // IV
  1243. //
  1244. Status = LocalCryptSystem->Initialize(
  1245. Context->SessionKey.keyvalue.value,
  1246. Context->SessionKey.keyvalue.length,
  1247. 0, // no flags
  1248. &CryptBuffer
  1249. );
  1250. if (!NT_SUCCESS(Status))
  1251. {
  1252. goto Cleanup;
  1253. }
  1254. //
  1255. // Set the initial vector
  1256. //
  1257. Status = LocalCryptSystem->Control(
  1258. CRYPT_CONTROL_SET_INIT_VECT,
  1259. CryptBuffer,
  1260. Signature->Checksum,
  1261. 8
  1262. );
  1263. if (!NT_SUCCESS(Status))
  1264. {
  1265. goto Cleanup;
  1266. }
  1267. //
  1268. // Now encrypt the sequence number
  1269. //
  1270. OutputSize = 8;
  1271. Status = LocalCryptSystem->Decrypt(
  1272. CryptBuffer,
  1273. Signature->SequenceNumber,
  1274. 8,
  1275. Signature->SequenceNumber,
  1276. &OutputSize
  1277. );
  1278. if (!NT_SUCCESS(Status))
  1279. {
  1280. goto Cleanup;
  1281. }
  1282. //
  1283. // For datagram or integrity only, we use just the supplied nonce.
  1284. //
  1285. if (((Context->ContextFlags & ISC_RET_DATAGRAM) != 0) ||
  1286. ((Context->ContextFlags & (ISC_RET_INTEGRITY | ISC_RET_SEQUENCE_DETECT | ISC_RET_REPLAY_DETECT)) == ISC_RET_INTEGRITY))
  1287. {
  1288. ContextNonce = &SuppliedNonce;
  1289. }
  1290. else
  1291. {
  1292. ContextNonce = &Context->ReceiveNonce;
  1293. }
  1294. if (!KERB_IS_DES_ENCRYPTION(Context->SessionKey.keytype))
  1295. {
  1296. Nonce[0] = (UCHAR) ((*ContextNonce & 0xff000000) >> 24);
  1297. Nonce[1] = (UCHAR) ((*ContextNonce & 0x00ff0000) >> 16);
  1298. Nonce[2] = (UCHAR) ((*ContextNonce & 0x0000ff00) >> 8);
  1299. Nonce[3] = (UCHAR) (*ContextNonce & 0x000000ff);
  1300. }
  1301. else
  1302. {
  1303. Nonce[3] = (UCHAR) ((*ContextNonce & 0xff000000) >> 24);
  1304. Nonce[2] = (UCHAR) ((*ContextNonce & 0x00ff0000) >> 16);
  1305. Nonce[1] = (UCHAR) ((*ContextNonce & 0x0000ff00) >> 8);
  1306. Nonce[0] = (UCHAR) (*ContextNonce & 0x000000ff);
  1307. }
  1308. *SequenceNumber = *(ULONG UNALIGNED *) Nonce;
  1309. D_DebugLog((DEB_TRACE_USER,"Verifying signature buffer (decrypt = %d) with nonce 0x%x, message seq 0x%x\n",
  1310. Decrypt,
  1311. *(ULONG UNALIGNED *) Nonce,
  1312. *(ULONG UNALIGNED *) Signature->SequenceNumber
  1313. ));
  1314. if (!RtlEqualMemory(
  1315. Nonce,
  1316. Signature->SequenceNumber,
  1317. 4))
  1318. {
  1319. Status = SEC_E_OUT_OF_SEQUENCE;
  1320. goto Cleanup;
  1321. }
  1322. (*ContextNonce)++;
  1323. //
  1324. // Inbound contexts send a high dword of 0xffffffff, outbound gets
  1325. // 0x00000000.
  1326. //
  1327. if (Context->ContextAttributes & KERB_CONTEXT_OUTBOUND)
  1328. {
  1329. if (*(ULONG UNALIGNED *)(&Signature->SequenceNumber[4]) != 0xffffffff)
  1330. {
  1331. D_DebugLog((DEB_ERROR, "Bad sequence number %ws, %d\n", THIS_FILE, __LINE__));
  1332. Status = SEC_E_MESSAGE_ALTERED;
  1333. goto Cleanup;
  1334. }
  1335. }
  1336. else
  1337. {
  1338. DsysAssert((Context->ContextAttributes & KERB_CONTEXT_INBOUND) != 0);
  1339. if (*(ULONG UNALIGNED *)(&Signature->SequenceNumber[4]) != 0)
  1340. {
  1341. Status = SEC_E_MESSAGE_ALTERED;
  1342. goto Cleanup;
  1343. }
  1344. }
  1345. if (ARGUMENT_PRESENT(CryptSystem))
  1346. {
  1347. *CryptSystem = LocalCryptSystem;
  1348. }
  1349. *OutputSignature = Signature;
  1350. Cleanup:
  1351. if ( ( CryptBuffer != NULL ) &&
  1352. ( LocalCryptSystem != NULL ) )
  1353. {
  1354. LocalCryptSystem->Discard(&CryptBuffer);
  1355. }
  1356. return(Status);
  1357. }
  1358. #define KERB_MAX_CHECKSUM_LENGTH 24
  1359. #define KERB_MAX_KEY_LENGTH 24
  1360. #define KERB_MAX_BLOCK_LENGTH 24
  1361. //+-------------------------------------------------------------------------
  1362. //
  1363. // Function: SpMakeSignature
  1364. //
  1365. // Synopsis: Signs a message buffer by calculatinga checksum over all
  1366. // the non-read only data buffers and encrypting the checksum
  1367. // along with a nonce.
  1368. //
  1369. // Effects:
  1370. //
  1371. // Arguments: ContextHandle - Handle of the context to use to sign the
  1372. // message.
  1373. // QualityOfProtection - Unused flags.
  1374. // MessageBuffers - Contains an array of buffers to sign and
  1375. // to store the signature.
  1376. // MessageSequenceNumber - Sequence number for this message,
  1377. // only used in datagram cases.
  1378. //
  1379. // Requires: STATUS_INVALID_HANDLE - the context could not be found or
  1380. // was not configured for message integrity.
  1381. // STATUS_INVALID_PARAMETER - the signature buffer could not
  1382. // be found.
  1383. // STATUS_BUFFER_TOO_SMALL - the signature buffer is too small
  1384. // to hold the signature
  1385. //
  1386. // Returns:
  1387. //
  1388. // Notes:
  1389. //
  1390. //
  1391. //--------------------------------------------------------------------------
  1392. NTSTATUS NTAPI
  1393. SpMakeSignature(
  1394. IN LSA_SEC_HANDLE ContextHandle,
  1395. IN ULONG QualityOfProtection,
  1396. IN PSecBufferDesc MessageBuffers,
  1397. IN ULONG MessageSequenceNumber
  1398. )
  1399. {
  1400. NTSTATUS Status = STATUS_SUCCESS;
  1401. PKERB_CONTEXT Context = NULL;
  1402. PCHECKSUM_FUNCTION Check;
  1403. PCRYPTO_SYSTEM CryptSystem = NULL ;
  1404. PSecBuffer SignatureBuffer = NULL;
  1405. ULONG Index;
  1406. PCHECKSUM_BUFFER CheckBuffer = NULL;
  1407. PCRYPT_STATE_BUFFER CryptBuffer = NULL;
  1408. PKERB_GSS_SIGNATURE Signature;
  1409. UCHAR LocalChecksum[KERB_MAX_CHECKSUM_LENGTH];
  1410. LONG ChecksumType = 0;
  1411. LONG EncryptType;
  1412. ULONG TotalBufferSize = 0;
  1413. ULONG OutputSize;
  1414. ULONG SequenceNumber;
  1415. D_DebugLog((DEB_TRACE_API,"SpMakeSignature Called\n"));
  1416. D_DebugLog((DEB_TRACE_USER, "Make Signature handle = 0x%x\n",ContextHandle));
  1417. Status = KerbReferenceContextByLsaHandle(
  1418. ContextHandle,
  1419. FALSE, // don't unlink
  1420. &Context
  1421. );
  1422. if (!NT_SUCCESS(Status))
  1423. {
  1424. DebugLog((DEB_ERROR, "Invalid handle supplied for MakeSignature(0x%x) Status = 0x%x. %ws, line %d\n",
  1425. ContextHandle, Status, THIS_FILE, __LINE__));
  1426. goto Cleanup;
  1427. }
  1428. //
  1429. // Find the body and signature SecBuffers from pMessage
  1430. //
  1431. for (Index = 0; Index < MessageBuffers->cBuffers ; Index++ )
  1432. {
  1433. //
  1434. // We can't allow a combination of the two readonly buffer types, or you'll just
  1435. // get READONLY behavior.
  1436. //
  1437. if (( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY ) &&
  1438. ( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM ))
  1439. {
  1440. Status = SEC_E_INVALID_TOKEN;
  1441. DebugLog((DEB_ERROR, "Can't have readonly & readonly_w_checksum\n"));
  1442. goto Cleanup;
  1443. }
  1444. if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_TOKEN)
  1445. {
  1446. SignatureBuffer = &MessageBuffers->pBuffers[Index];
  1447. }
  1448. else if ((BUFFERTYPE(MessageBuffers->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  1449. (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY)))
  1450. {
  1451. TotalBufferSize += MessageBuffers->pBuffers[Index].cbBuffer;
  1452. }
  1453. }
  1454. if (SignatureBuffer == NULL)
  1455. {
  1456. DebugLog((DEB_ERROR, "No signature buffer found. %ws, line %d\n", THIS_FILE, __LINE__));
  1457. Status = STATUS_INVALID_PARAMETER;
  1458. goto Cleanup;
  1459. }
  1460. //
  1461. // Verify that the context was created with the integrity bit
  1462. //
  1463. if ((Context->ContextFlags & KERB_SIGN_FLAGS) == 0)
  1464. {
  1465. if (SignatureBuffer->cbBuffer < sizeof(KERB_NULL_SIGNATURE))
  1466. {
  1467. Status = SEC_E_BUFFER_TOO_SMALL;
  1468. goto Cleanup;
  1469. }
  1470. SignatureBuffer->cbBuffer = sizeof(KERB_NULL_SIGNATURE);
  1471. *(PKERB_NULL_SIGNATURE) SignatureBuffer->pvBuffer = 0;
  1472. Status = STATUS_SUCCESS;
  1473. goto Cleanup;
  1474. }
  1475. Status = KerbGetChecksumAndEncryptionType(
  1476. Context,
  1477. QualityOfProtection,
  1478. &ChecksumType,
  1479. &EncryptType
  1480. );
  1481. if (!NT_SUCCESS(Status))
  1482. {
  1483. goto Cleanup;
  1484. }
  1485. Status = KerbMakeSignatureToken(
  1486. Context,
  1487. QualityOfProtection,
  1488. SignatureBuffer,
  1489. TotalBufferSize,
  1490. FALSE, // don't encrypt
  1491. MessageSequenceNumber,
  1492. &Signature,
  1493. &SequenceNumber
  1494. );
  1495. if (!NT_SUCCESS(Status))
  1496. {
  1497. goto Cleanup;
  1498. }
  1499. //
  1500. // Locate the checksum for the context, loading it if necessary from the
  1501. // the crypto support DLL
  1502. //
  1503. Status = CDLocateCheckSum(ChecksumType, &Check);
  1504. if (!NT_SUCCESS(Status))
  1505. {
  1506. DebugLog((DEB_ERROR,"Failed to load %d checksum: 0x%x. %ws, line %d\n",ChecksumType,Status, THIS_FILE, __LINE__));
  1507. goto Cleanup;
  1508. }
  1509. DsysAssert(Check->CheckSumSize <= sizeof(LocalChecksum));
  1510. Status = CDLocateCSystem(EncryptType, &CryptSystem);
  1511. if (!NT_SUCCESS(Status))
  1512. {
  1513. DebugLog((DEB_ERROR,"Failed to load %d crypt system: 0x%x. %ws, line %d\n",EncryptType,Status, THIS_FILE, __LINE__));
  1514. goto Cleanup;
  1515. }
  1516. //
  1517. // Generate a check sum of the message, and store it into the signature
  1518. // buffer.
  1519. //
  1520. if (NULL != Check->InitializeEx2)
  1521. {
  1522. Status = Check->InitializeEx2(
  1523. Context->SessionKey.keyvalue.value,
  1524. (ULONG) Context->SessionKey.keyvalue.length,
  1525. NULL,
  1526. KERB_SAFE_SALT,
  1527. &CheckBuffer
  1528. );
  1529. }
  1530. else
  1531. {
  1532. Status = Check->InitializeEx(
  1533. Context->SessionKey.keyvalue.value,
  1534. (ULONG) Context->SessionKey.keyvalue.length,
  1535. KERB_SAFE_SALT,
  1536. &CheckBuffer
  1537. );
  1538. }
  1539. if (!NT_SUCCESS(Status))
  1540. {
  1541. goto Cleanup;
  1542. }
  1543. //
  1544. // Sum in 8 bytes of the signature
  1545. //
  1546. Check->Sum(
  1547. CheckBuffer,
  1548. 8,
  1549. ((PUCHAR) Signature) -2
  1550. );
  1551. for (Index = 0; Index < MessageBuffers->cBuffers; Index++ )
  1552. {
  1553. if ((BUFFERTYPE(MessageBuffers->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  1554. (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY)) &&
  1555. (MessageBuffers->pBuffers[Index].cbBuffer != 0))
  1556. {
  1557. Check->Sum(
  1558. CheckBuffer,
  1559. MessageBuffers->pBuffers[Index].cbBuffer,
  1560. (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer
  1561. );
  1562. }
  1563. }
  1564. (void) Check->Finalize(CheckBuffer, LocalChecksum);
  1565. Status = Check->Finish(&CheckBuffer);
  1566. if (!NT_SUCCESS(Status))
  1567. {
  1568. goto Cleanup;
  1569. }
  1570. //
  1571. // Copy in the first 8 bytes of the checksum
  1572. //
  1573. RtlCopyMemory(
  1574. Signature->Checksum,
  1575. LocalChecksum,
  1576. 8
  1577. );
  1578. //
  1579. // Now we need to encrypt the sequence number, using the checksum as the
  1580. // IV
  1581. //
  1582. Status = CryptSystem->Initialize(
  1583. Context->SessionKey.keyvalue.value,
  1584. Context->SessionKey.keyvalue.length,
  1585. 0, // no options
  1586. &CryptBuffer
  1587. );
  1588. if (!NT_SUCCESS(Status))
  1589. {
  1590. goto Cleanup;
  1591. }
  1592. //
  1593. // Set the initial vector
  1594. //
  1595. Status = CryptSystem->Control(
  1596. CRYPT_CONTROL_SET_INIT_VECT,
  1597. CryptBuffer,
  1598. LocalChecksum,
  1599. 8
  1600. );
  1601. if (!NT_SUCCESS(Status))
  1602. {
  1603. goto Cleanup;
  1604. }
  1605. //
  1606. // Now encrypt the sequence number
  1607. //
  1608. Status = CryptSystem->Encrypt(
  1609. CryptBuffer,
  1610. Signature->SequenceNumber,
  1611. 8,
  1612. Signature->SequenceNumber,
  1613. &OutputSize
  1614. );
  1615. if (!NT_SUCCESS(Status))
  1616. {
  1617. goto Cleanup;
  1618. }
  1619. Cleanup:
  1620. if ( ( CryptBuffer != NULL) &&
  1621. ( CryptSystem != NULL ) )
  1622. {
  1623. CryptSystem->Discard(&CryptBuffer);
  1624. }
  1625. if (Context != NULL)
  1626. {
  1627. KerbDereferenceContext(Context);
  1628. }
  1629. D_DebugLog((DEB_TRACE_API, "SpMakeSignature returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  1630. return(KerbMapKerbNtStatusToNtStatus(Status));
  1631. }
  1632. //+-------------------------------------------------------------------------
  1633. //
  1634. // Function: SpVerifySignature
  1635. //
  1636. // Synopsis: Verifies a signed message buffer by calculating a checksum over all
  1637. // the non-read only data buffers and encrypting the checksum
  1638. // along with a nonce.
  1639. //
  1640. // Effects:
  1641. //
  1642. // Arguments: ContextHandle - Handle of the context to use to sign the
  1643. // message.
  1644. // MessageBuffers - Contains an array of signed buffers and
  1645. // a signature buffer.
  1646. // MessageSequenceNumber - Sequence number for this message,
  1647. // only used in datagram cases.
  1648. // QualityOfProtection - Unused flags.
  1649. //
  1650. // Requires: STATUS_INVALID_HANDLE - the context could not be found or
  1651. // was not configured for message integrity.
  1652. // STATUS_INVALID_PARAMETER - the signature buffer could not
  1653. // be found or was too small.
  1654. //
  1655. // Returns:
  1656. //
  1657. // Notes:
  1658. //
  1659. //
  1660. //--------------------------------------------------------------------------
  1661. NTSTATUS NTAPI
  1662. SpVerifySignature(
  1663. IN LSA_SEC_HANDLE ContextHandle,
  1664. IN PSecBufferDesc MessageBuffers,
  1665. IN ULONG MessageSequenceNumber,
  1666. OUT PULONG QualityOfProtection
  1667. )
  1668. {
  1669. NTSTATUS Status = STATUS_SUCCESS;
  1670. PKERB_CONTEXT Context = NULL;
  1671. PCHECKSUM_FUNCTION Check;
  1672. PSecBuffer SignatureBuffer = NULL;
  1673. ULONG Index;
  1674. PCHECKSUM_BUFFER CheckBuffer = NULL;
  1675. PKERB_GSS_SIGNATURE Signature;
  1676. LONG ChecksumType;
  1677. UCHAR LocalChecksum[KERB_MAX_CHECKSUM_LENGTH];
  1678. ULONG Protection;
  1679. ULONG TotalBufferSize = 0;
  1680. ULONG SequenceNumber;
  1681. D_DebugLog((DEB_TRACE_API,"SpVerifySignature Called\n"));
  1682. D_DebugLog((DEB_TRACE_USER, "Verify Signature handle = 0x%x\n",ContextHandle));
  1683. Status = KerbReferenceContextByLsaHandle(
  1684. ContextHandle,
  1685. FALSE, // don't unlink
  1686. &Context
  1687. );
  1688. if (!NT_SUCCESS(Status))
  1689. {
  1690. DebugLog((DEB_ERROR, "Invalid handle supplied for VerifySignature(0x%x) Status = 0x%x. %ws, line %d\n",
  1691. ContextHandle, Status, THIS_FILE, __LINE__));
  1692. goto Cleanup;
  1693. }
  1694. //
  1695. // Find the body and signature SecBuffers from pMessage
  1696. //
  1697. for (Index = 0; Index < MessageBuffers->cBuffers ; Index++ )
  1698. {
  1699. //
  1700. // We can't allow a combination of the two readonly buffer types, or you'll just
  1701. // get READONLY behavior.
  1702. //
  1703. if (( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY ) &&
  1704. ( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM ))
  1705. {
  1706. Status = SEC_E_INVALID_TOKEN;
  1707. DebugLog((DEB_ERROR, "Can't have readonly & readonly_w_checksum\n"));
  1708. goto Cleanup;
  1709. }
  1710. if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_TOKEN)
  1711. {
  1712. SignatureBuffer = &MessageBuffers->pBuffers[Index];
  1713. }
  1714. else if ((BUFFERTYPE(MessageBuffers->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  1715. (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY)))
  1716. {
  1717. TotalBufferSize += MessageBuffers->pBuffers[Index].cbBuffer;
  1718. }
  1719. }
  1720. if (SignatureBuffer == NULL)
  1721. {
  1722. DebugLog((DEB_ERROR, "No signature buffer found. %ws, line %d\n", THIS_FILE, __LINE__));
  1723. Status = STATUS_INVALID_PARAMETER;
  1724. goto Cleanup;
  1725. }
  1726. //
  1727. // Also, verify that the context was created with the integrity bit
  1728. //
  1729. if ((Context->ContextFlags & KERB_SIGN_FLAGS) == 0)
  1730. {
  1731. PKERB_NULL_SIGNATURE NullSignature = (PKERB_NULL_SIGNATURE) SignatureBuffer->pvBuffer;
  1732. if (SignatureBuffer->cbBuffer >= sizeof(KERB_NULL_SIGNATURE) &&
  1733. (*NullSignature == 0))
  1734. {
  1735. Status = STATUS_SUCCESS;
  1736. }
  1737. else
  1738. {
  1739. D_DebugLog((DEB_ERROR, "Bad signature %ws, %d\n", THIS_FILE, __LINE__));
  1740. Status = SEC_E_MESSAGE_ALTERED;
  1741. }
  1742. goto Cleanup;
  1743. }
  1744. //
  1745. // Verify the signature header
  1746. //
  1747. Status = KerbVerifySignatureToken(
  1748. Context,
  1749. SignatureBuffer,
  1750. TotalBufferSize,
  1751. FALSE, // don't decrypt
  1752. MessageSequenceNumber,
  1753. &Signature,
  1754. &Protection,
  1755. &ChecksumType,
  1756. NULL, // don't need crypt system
  1757. &SequenceNumber
  1758. );
  1759. if (!NT_SUCCESS(Status))
  1760. {
  1761. DebugLog((DEB_ERROR, "Failed to verify signature token: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  1762. goto Cleanup;
  1763. }
  1764. //
  1765. // Now compute the checksum and verify it
  1766. //
  1767. Status = CDLocateCheckSum(ChecksumType, &Check);
  1768. if (!NT_SUCCESS(Status))
  1769. {
  1770. DebugLog((DEB_ERROR,"Failed to load MD5 checksum: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  1771. goto Cleanup;
  1772. }
  1773. DsysAssert(Check->CheckSumSize <= sizeof(LocalChecksum));
  1774. //
  1775. // Generate a check sum of the message, and store it into the signature
  1776. // buffer.
  1777. //
  1778. //
  1779. // if available use the Ex2 version for keyed checksums where checksum
  1780. // must be passed in on verification
  1781. //
  1782. if (NULL != Check->InitializeEx2)
  1783. {
  1784. Status = Check->InitializeEx2(
  1785. Context->SessionKey.keyvalue.value,
  1786. (ULONG) Context->SessionKey.keyvalue.length,
  1787. Signature->Checksum,
  1788. KERB_SAFE_SALT,
  1789. &CheckBuffer
  1790. );
  1791. }
  1792. else
  1793. {
  1794. Status = Check->InitializeEx(
  1795. Context->SessionKey.keyvalue.value,
  1796. (ULONG) Context->SessionKey.keyvalue.length,
  1797. KERB_SAFE_SALT,
  1798. &CheckBuffer
  1799. );
  1800. }
  1801. if (!NT_SUCCESS(Status))
  1802. {
  1803. goto Cleanup;
  1804. }
  1805. //
  1806. // Sum in 8 bytes of the signature
  1807. //
  1808. Check->Sum(
  1809. CheckBuffer,
  1810. 8,
  1811. ((PUCHAR) Signature) -2
  1812. );
  1813. for (Index = 0; Index < MessageBuffers->cBuffers; Index++ )
  1814. {
  1815. if ((BUFFERTYPE(MessageBuffers->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  1816. (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY)) &&
  1817. (MessageBuffers->pBuffers[Index].cbBuffer != 0))
  1818. {
  1819. Check->Sum(
  1820. CheckBuffer,
  1821. MessageBuffers->pBuffers[Index].cbBuffer,
  1822. (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer
  1823. );
  1824. }
  1825. }
  1826. (void) Check->Finalize(CheckBuffer, LocalChecksum);
  1827. Status = Check->Finish(&CheckBuffer);
  1828. if (!NT_SUCCESS(Status))
  1829. {
  1830. goto Cleanup;
  1831. }
  1832. if (!RtlEqualMemory(
  1833. LocalChecksum,
  1834. Signature->Checksum,
  1835. 8))
  1836. {
  1837. D_DebugLog((DEB_ERROR, "Bad checksum %ws, %d\n", THIS_FILE, __LINE__));
  1838. Status = SEC_E_MESSAGE_ALTERED;
  1839. goto Cleanup;
  1840. }
  1841. if (ARGUMENT_PRESENT(QualityOfProtection))
  1842. {
  1843. *QualityOfProtection = Protection;
  1844. }
  1845. Cleanup:
  1846. if (Context != NULL)
  1847. {
  1848. KerbDereferenceContext(Context);
  1849. }
  1850. D_DebugLog((DEB_TRACE_API, "SpVerifySignature returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  1851. return(KerbMapKerbNtStatusToNtStatus(Status));
  1852. }
  1853. #define STREAM_CIPHER_BLOCKLEN 1
  1854. //+-------------------------------------------------------------------------
  1855. //
  1856. // Function: SpSealMessage
  1857. //
  1858. // Synopsis: Seals a message buffer by calculating a checksum over all
  1859. // the non-read only data buffers and encrypting the data, checksum
  1860. // and a sequence number.
  1861. //
  1862. // Effects:
  1863. //
  1864. // Arguments: ContextHandle - Handle of the context to use to sign the
  1865. // message.
  1866. // QualityOfProtection - Unused flags.
  1867. // MessageBuffers - Contains an array of buffers to sign and
  1868. // to store the signature.
  1869. // MessageSequenceNumber - Sequence number for this message,
  1870. // only used in datagram cases.
  1871. //
  1872. // Requires: STATUS_INVALID_HANDLE - the context could not be found or
  1873. // was not configured for message integrity.
  1874. // STATUS_INVALID_PARAMETER - the signature buffer could not
  1875. // be found.
  1876. // STATUS_BUFFER_TOO_SMALL - the signature buffer is too small
  1877. // to hold the signature
  1878. //
  1879. // Returns:
  1880. //
  1881. // Notes:
  1882. //
  1883. //
  1884. //--------------------------------------------------------------------------
  1885. NTSTATUS NTAPI
  1886. SpSealMessage(
  1887. IN LSA_SEC_HANDLE ContextHandle,
  1888. IN ULONG QualityOfProtection,
  1889. IN PSecBufferDesc MessageBuffers,
  1890. IN ULONG MessageSequenceNumber
  1891. )
  1892. {
  1893. NTSTATUS Status = STATUS_SUCCESS;
  1894. PKERB_CONTEXT Context = NULL;
  1895. PCHECKSUM_FUNCTION Check = NULL ;
  1896. PCRYPTO_SYSTEM CryptSystem = NULL ;
  1897. PSecBuffer SignatureBuffer = NULL;
  1898. ULONG Index;
  1899. PCHECKSUM_BUFFER CheckBuffer = NULL;
  1900. PCRYPT_STATE_BUFFER CryptBuffer = NULL;
  1901. PKERB_GSS_SEAL_SIGNATURE SealSignature;
  1902. UCHAR LocalChecksum[KERB_MAX_CHECKSUM_LENGTH];
  1903. UCHAR LocalKey[KERB_MAX_KEY_LENGTH];
  1904. UCHAR LocalBlockBuffer[KERB_MAX_BLOCK_LENGTH];
  1905. ULONG BeginBlockSize = 0;
  1906. PBYTE BeginBlockPointer = NULL;
  1907. ULONG EndBlockSize = 0;
  1908. ULONG EncryptBufferSize = 0;
  1909. PBYTE EncryptBuffer = NULL;
  1910. BOOLEAN DoEncryption = TRUE;
  1911. ULONG BlockSize = 1;
  1912. LONG ChecksumType = 0;
  1913. LONG EncryptType;
  1914. ULONG TotalBufferSize = 0;
  1915. ULONG OutputSize;
  1916. ULONG ContextAttributes;
  1917. ULONG SequenceNumber;
  1918. D_DebugLog((DEB_TRACE_API,"SpSealMessage Called\n"));
  1919. D_DebugLog((DEB_TRACE_USER, "SealMessage handle = 0x%x\n",ContextHandle));
  1920. Status = KerbReferenceContextByLsaHandle(
  1921. ContextHandle,
  1922. FALSE, // don't unlink
  1923. &Context
  1924. );
  1925. if (!NT_SUCCESS(Status))
  1926. {
  1927. DebugLog((DEB_ERROR, "Invalid handle supplied for SpSealMessage(0x%x) Status = 0x%x. %ws, line %d\n",
  1928. ContextHandle, Status, THIS_FILE, __LINE__));
  1929. goto Cleanup;
  1930. }
  1931. //
  1932. // get the encryption type for the context
  1933. //
  1934. Status = KerbGetChecksumAndEncryptionType(
  1935. Context,
  1936. QualityOfProtection,
  1937. &ChecksumType,
  1938. &EncryptType
  1939. );
  1940. if (!NT_SUCCESS(Status))
  1941. {
  1942. goto Cleanup;
  1943. }
  1944. //
  1945. // Locate the cryptsystem for the context, loading it if necessary from the
  1946. // the crypto support DLL
  1947. //
  1948. Status = CDLocateCSystem(EncryptType, &CryptSystem);
  1949. if (!NT_SUCCESS(Status))
  1950. {
  1951. DebugLog((DEB_ERROR,"Failed to load %d crypt system: 0x%x. %ws, line %d\n",EncryptType,Status, THIS_FILE, __LINE__));
  1952. goto Cleanup;
  1953. }
  1954. BlockSize = CryptSystem->BlockSize;
  1955. //
  1956. // Find the body and signature SecBuffers from pMessage
  1957. //
  1958. for (Index = 0; Index < MessageBuffers->cBuffers ; Index++ )
  1959. {
  1960. //
  1961. // We can't allow a combination of the two readonly buffer types, or you'll just
  1962. // get READONLY behavior.
  1963. //
  1964. if (( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY ) &&
  1965. ( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM ))
  1966. {
  1967. Status = SEC_E_INVALID_TOKEN;
  1968. DebugLog((DEB_ERROR, "Can't have readonly & readonly_w_checksum\n"));
  1969. goto Cleanup;
  1970. }
  1971. if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_TOKEN)
  1972. {
  1973. SignatureBuffer = &MessageBuffers->pBuffers[Index];
  1974. }
  1975. else if ((BUFFERTYPE(MessageBuffers->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  1976. (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY )))
  1977. {
  1978. //
  1979. // use real block size from crypt type
  1980. //
  1981. if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_PADDING)
  1982. {
  1983. if (STREAM_CIPHER_BLOCKLEN != BlockSize)
  1984. {
  1985. TotalBufferSize = ROUND_UP_COUNT(TotalBufferSize+1,BlockSize);
  1986. }
  1987. else
  1988. {
  1989. //
  1990. // For stream encryption, only 1 byte of padding
  1991. //
  1992. TotalBufferSize += BlockSize;
  1993. }
  1994. }
  1995. else
  1996. {
  1997. TotalBufferSize += MessageBuffers->pBuffers[Index].cbBuffer;
  1998. }
  1999. }
  2000. }
  2001. if (SignatureBuffer == NULL)
  2002. {
  2003. DebugLog((DEB_ERROR, "No signature buffer found. %ws, line %d\n", THIS_FILE, __LINE__));
  2004. Status = STATUS_INVALID_PARAMETER;
  2005. goto Cleanup;
  2006. }
  2007. ContextAttributes = Context->ContextAttributes;
  2008. //
  2009. // If we are not encrypting, just wrapping, remember that
  2010. //
  2011. if (QualityOfProtection == KERB_WRAP_NO_ENCRYPT)
  2012. {
  2013. DoEncryption = FALSE;
  2014. //
  2015. // Reset the block size because we are not really encrypting
  2016. //
  2017. }
  2018. //
  2019. // Verify that the context was created with the integrity bit
  2020. //
  2021. if (DoEncryption && ((Context->ContextFlags & ISC_RET_CONFIDENTIALITY) == 0))
  2022. {
  2023. DebugLog((DEB_ERROR,"Trying to seal without asking for confidentiality. %ws, line %d\n", THIS_FILE, __LINE__));
  2024. Status = SEC_E_UNSUPPORTED_FUNCTION;
  2025. goto Cleanup;
  2026. }
  2027. Status = KerbMakeSignatureToken(
  2028. Context,
  2029. QualityOfProtection,
  2030. SignatureBuffer,
  2031. TotalBufferSize,
  2032. TRUE, // do encrypt
  2033. MessageSequenceNumber,
  2034. (PKERB_GSS_SIGNATURE *) &SealSignature,
  2035. &SequenceNumber
  2036. );
  2037. if (!NT_SUCCESS(Status))
  2038. {
  2039. goto Cleanup;
  2040. }
  2041. //
  2042. // Locate the checksum for the context, loading it if necessary from the
  2043. // the crypto support DLL
  2044. //
  2045. Status = CDLocateCheckSum(ChecksumType, &Check);
  2046. if (!NT_SUCCESS(Status))
  2047. {
  2048. DebugLog((DEB_ERROR,"Failed to load %d checksum: 0x%x. %ws, line %d\n",ChecksumType,Status, THIS_FILE, __LINE__));
  2049. goto Cleanup;
  2050. }
  2051. DsysAssert(Check->CheckSumSize <= sizeof(LocalChecksum));
  2052. //
  2053. // Generate a check sum of the message, and store it into the signature
  2054. // buffer.
  2055. //
  2056. Status = Check->InitializeEx(
  2057. Context->SessionKey.keyvalue.value,
  2058. (ULONG) Context->SessionKey.keyvalue.length,
  2059. KERB_PRIV_SALT,
  2060. &CheckBuffer
  2061. );
  2062. if (!NT_SUCCESS(Status))
  2063. {
  2064. goto Cleanup;
  2065. }
  2066. if (DoEncryption)
  2067. {
  2068. //
  2069. // Create the encryption key by xoring with 0xf0f0f0f0
  2070. //
  2071. DsysAssert(Context->SessionKey.keyvalue.length <= sizeof(LocalKey));
  2072. if (Context->SessionKey.keyvalue.length > sizeof(LocalKey))
  2073. {
  2074. Status = SEC_E_UNSUPPORTED_FUNCTION;
  2075. goto Cleanup;
  2076. }
  2077. for (Index = 0; Index < Context->SessionKey.keyvalue.length ; Index++ )
  2078. {
  2079. LocalKey[Index] = Context->SessionKey.keyvalue.value[Index] ^ 0xf0;
  2080. }
  2081. Status = CryptSystem->Initialize(
  2082. LocalKey,
  2083. Context->SessionKey.keyvalue.length,
  2084. 0, // no options
  2085. &CryptBuffer
  2086. );
  2087. if (!NT_SUCCESS(Status))
  2088. {
  2089. goto Cleanup;
  2090. }
  2091. }
  2092. //
  2093. // Sum in 8 bytes of the signature
  2094. //
  2095. Check->Sum(
  2096. CheckBuffer,
  2097. 8,
  2098. ((PUCHAR) SealSignature) -2
  2099. );
  2100. //
  2101. // Sum the confounder
  2102. //
  2103. Check->Sum(
  2104. CheckBuffer,
  2105. KERB_GSS_SIG_CONFOUNDER_SIZE,
  2106. SealSignature->Confounder
  2107. );
  2108. if (DoEncryption)
  2109. {
  2110. if ((EncryptType == KERB_ETYPE_RC4_PLAIN) ||
  2111. (EncryptType == KERB_ETYPE_RC4_PLAIN_EXP))
  2112. {
  2113. Status = CryptSystem->Control(
  2114. CRYPT_CONTROL_SET_INIT_VECT,
  2115. CryptBuffer,
  2116. (PUCHAR) &SequenceNumber,
  2117. sizeof(ULONG)
  2118. );
  2119. if (!NT_SUCCESS(Status))
  2120. {
  2121. goto Cleanup;
  2122. }
  2123. }
  2124. //
  2125. // Encrypt the 8 confounder bytes
  2126. //
  2127. Status = CryptSystem->Encrypt(
  2128. CryptBuffer,
  2129. SealSignature->Confounder,
  2130. KERB_GSS_SIG_CONFOUNDER_SIZE,
  2131. SealSignature->Confounder,
  2132. &OutputSize
  2133. );
  2134. if (!NT_SUCCESS(Status))
  2135. {
  2136. goto Cleanup;
  2137. }
  2138. }
  2139. for (Index = 0; Index < MessageBuffers->cBuffers; Index++ )
  2140. {
  2141. if ((BUFFERTYPE(MessageBuffers->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  2142. (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY )) &&
  2143. (MessageBuffers->pBuffers[Index].cbBuffer != 0))
  2144. {
  2145. //
  2146. // If the SECBUFFER_READONLY_WITH_CHECKSUM is set, then don't encrypt the buffer, but be sure
  2147. // to checksum it. This is primarily for consistencies sake...
  2148. //
  2149. if (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM))
  2150. {
  2151. //
  2152. // Take into account that the input buffers may not all be aligned
  2153. // properly
  2154. //
  2155. DsysAssert(BeginBlockSize < BlockSize);
  2156. if (BeginBlockSize != 0)
  2157. {
  2158. //
  2159. // We have a fragment we still need to encrypt
  2160. //
  2161. EncryptBuffer = (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer +
  2162. (BlockSize - BeginBlockSize);
  2163. EncryptBufferSize = MessageBuffers->pBuffers[Index].cbBuffer -
  2164. (BlockSize - BeginBlockSize);
  2165. }
  2166. else
  2167. {
  2168. //
  2169. // There is no fragment to encrypt, so try to do the whole
  2170. // buffer
  2171. //
  2172. EncryptBuffer = (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer;
  2173. EncryptBufferSize = MessageBuffers->pBuffers[Index].cbBuffer;
  2174. }
  2175. EndBlockSize = EncryptBufferSize - ROUND_DOWN_COUNT(EncryptBufferSize,BlockSize);
  2176. DsysAssert(EndBlockSize < BlockSize);
  2177. EncryptBufferSize = EncryptBufferSize - EndBlockSize;
  2178. //
  2179. // If this is padding, fill it in with the appropriate data &
  2180. // length
  2181. //
  2182. if (MessageBuffers->pBuffers[Index].BufferType == SECBUFFER_PADDING)
  2183. {
  2184. if (MessageBuffers->pBuffers[Index].cbBuffer < BlockSize)
  2185. {
  2186. DebugLog((DEB_ERROR, "Pad buffer is too small: %d instead of %d. %ws, %d\n",
  2187. MessageBuffers->pBuffers[Index].cbBuffer,
  2188. BlockSize,
  2189. THIS_FILE,
  2190. __LINE__
  2191. ));
  2192. Status = STATUS_INVALID_PARAMETER;
  2193. goto Cleanup;
  2194. }
  2195. memset(
  2196. MessageBuffers->pBuffers[Index].pvBuffer,
  2197. BlockSize - BeginBlockSize,
  2198. BlockSize - BeginBlockSize
  2199. );
  2200. MessageBuffers->pBuffers[Index].cbBuffer = BlockSize - BeginBlockSize;
  2201. //
  2202. // If there is a fragment, we will encrypt the padding with the fragment.
  2203. // Otherwise we will do just a padding buffer.
  2204. //
  2205. if (BeginBlockSize != 0)
  2206. {
  2207. EncryptBufferSize = 0;
  2208. }
  2209. //
  2210. // The padding fixes up the end block.
  2211. //
  2212. EndBlockSize = 0;
  2213. }
  2214. }
  2215. //
  2216. // Checksum the whole buffer. We do this now to get the right amount of
  2217. // padding.
  2218. //
  2219. Check->Sum(
  2220. CheckBuffer,
  2221. MessageBuffers->pBuffers[Index].cbBuffer,
  2222. (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer
  2223. );
  2224. if (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM))
  2225. {
  2226. if (BeginBlockSize != 0)
  2227. {
  2228. RtlCopyMemory(
  2229. LocalBlockBuffer+BeginBlockSize,
  2230. MessageBuffers->pBuffers[Index].pvBuffer,
  2231. BlockSize - BeginBlockSize
  2232. );
  2233. if (DoEncryption)
  2234. {
  2235. //
  2236. // Now encrypt the buffer
  2237. //
  2238. Status = CryptSystem->Encrypt(
  2239. CryptBuffer,
  2240. LocalBlockBuffer,
  2241. BlockSize,
  2242. LocalBlockBuffer,
  2243. &OutputSize
  2244. );
  2245. if (!NT_SUCCESS(Status))
  2246. {
  2247. goto Cleanup;
  2248. }
  2249. }
  2250. //
  2251. // Copy the pieces back
  2252. //
  2253. RtlCopyMemory(
  2254. BeginBlockPointer,
  2255. LocalBlockBuffer,
  2256. BeginBlockSize
  2257. );
  2258. RtlCopyMemory(
  2259. MessageBuffers->pBuffers[Index].pvBuffer,
  2260. LocalBlockBuffer + BeginBlockSize,
  2261. BlockSize - BeginBlockSize
  2262. );
  2263. }
  2264. if (DoEncryption && (EncryptBufferSize != 0))
  2265. {
  2266. //
  2267. // Now encrypt the buffer
  2268. //
  2269. Status = CryptSystem->Encrypt(
  2270. CryptBuffer,
  2271. EncryptBuffer,
  2272. EncryptBufferSize,
  2273. EncryptBuffer,
  2274. &OutputSize
  2275. );
  2276. if (!NT_SUCCESS(Status))
  2277. {
  2278. goto Cleanup;
  2279. }
  2280. DsysAssert(OutputSize == EncryptBufferSize);
  2281. }
  2282. //
  2283. // Prepare for the next go-round
  2284. //
  2285. RtlCopyMemory(
  2286. LocalBlockBuffer,
  2287. EncryptBuffer+EncryptBufferSize,
  2288. EndBlockSize
  2289. );
  2290. BeginBlockSize = EndBlockSize;
  2291. BeginBlockPointer = (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer +
  2292. MessageBuffers->pBuffers[Index].cbBuffer -
  2293. EndBlockSize;
  2294. }
  2295. }
  2296. }
  2297. //
  2298. // Make sure there are no left-over bits
  2299. //
  2300. if (BeginBlockSize != 0)
  2301. {
  2302. DebugLog((DEB_ERROR,"Non-aligned buffer size to SealMessage: %d extra bytes\n",
  2303. BeginBlockSize ));
  2304. Status = SEC_E_INVALID_TOKEN;
  2305. goto Cleanup;
  2306. }
  2307. (void) Check->Finalize(CheckBuffer, LocalChecksum);
  2308. Status = Check->Finish(&CheckBuffer);
  2309. if (!NT_SUCCESS(Status))
  2310. {
  2311. goto Cleanup;
  2312. }
  2313. CheckBuffer = NULL;
  2314. //
  2315. // Copy in the first 8 bytes of the checksum
  2316. //
  2317. RtlCopyMemory(
  2318. SealSignature->Signature.Checksum,
  2319. LocalChecksum,
  2320. 8
  2321. );
  2322. if (DoEncryption)
  2323. {
  2324. CryptSystem->Discard( &CryptBuffer );
  2325. }
  2326. //
  2327. // Now we need to encrypt the sequence number, using the checksum as the
  2328. // IV
  2329. //
  2330. Status = CryptSystem->Initialize(
  2331. Context->SessionKey.keyvalue.value,
  2332. Context->SessionKey.keyvalue.length,
  2333. 0, // no options
  2334. &CryptBuffer
  2335. );
  2336. if (!NT_SUCCESS(Status))
  2337. {
  2338. goto Cleanup;
  2339. }
  2340. //
  2341. // Set the initial vector
  2342. //
  2343. Status = CryptSystem->Control(
  2344. CRYPT_CONTROL_SET_INIT_VECT,
  2345. CryptBuffer,
  2346. LocalChecksum,
  2347. 8
  2348. );
  2349. if (!NT_SUCCESS(Status))
  2350. {
  2351. goto Cleanup;
  2352. }
  2353. //
  2354. // Now encrypt the sequence number
  2355. //
  2356. Status = CryptSystem->Encrypt(
  2357. CryptBuffer,
  2358. SealSignature->Signature.SequenceNumber,
  2359. 8,
  2360. SealSignature->Signature.SequenceNumber,
  2361. &OutputSize
  2362. );
  2363. if (!NT_SUCCESS(Status))
  2364. {
  2365. goto Cleanup;
  2366. }
  2367. Cleanup:
  2368. if ( ( CryptBuffer != NULL ) &&
  2369. ( CryptSystem != NULL ) )
  2370. {
  2371. CryptSystem->Discard(&CryptBuffer);
  2372. }
  2373. if ( ( CheckBuffer != NULL ) &&
  2374. ( Check != NULL ) )
  2375. {
  2376. Check->Finish(&CheckBuffer);
  2377. }
  2378. if (Context != NULL)
  2379. {
  2380. KerbDereferenceContext(Context);
  2381. }
  2382. D_DebugLog((DEB_TRACE_API, "SpSealMessage returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  2383. return(KerbMapKerbNtStatusToNtStatus(Status));
  2384. }
  2385. //+-------------------------------------------------------------------------
  2386. //
  2387. // Function: KerbGetSealMessageBodySize
  2388. //
  2389. // Synopsis: From a input encrypted message, figures out where the
  2390. // body starts
  2391. //
  2392. // Effects:
  2393. //
  2394. // Arguments:
  2395. //
  2396. // Requires:
  2397. //
  2398. // Returns: 0 on failure, # of bytes of data on success
  2399. //
  2400. // Notes:
  2401. //
  2402. //
  2403. //--------------------------------------------------------------------------
  2404. ULONG
  2405. KerbGetSealMessageBodySize(
  2406. IN OUT PVOID * InputBuffer,
  2407. IN ULONG InputBufferSize
  2408. )
  2409. {
  2410. INT BufferSize = (INT) InputBufferSize;
  2411. PBYTE Buffer = (PBYTE) *InputBuffer;
  2412. INT DerBufferSize;
  2413. INT OidLength;
  2414. if ((BufferSize-=1) < 0)
  2415. return(0);
  2416. if (*(Buffer++) != 0x60)
  2417. return(0);
  2418. if ((DerBufferSize = der_read_length(&Buffer, &BufferSize)) < 0)
  2419. return(0);
  2420. if (DerBufferSize != BufferSize)
  2421. return(0);
  2422. if ((BufferSize-=1) < 0)
  2423. return(0);
  2424. if (*(Buffer++) != 0x06)
  2425. return(0);
  2426. if ((BufferSize-=1) < 0)
  2427. return(0);
  2428. OidLength = *(Buffer++);
  2429. if ((OidLength & 0x7fffffff) != OidLength) /* Overflow??? */
  2430. return(0);
  2431. if ((BufferSize-= (int) OidLength) < 0)
  2432. return(0);
  2433. Buffer+=OidLength;
  2434. if ((BufferSize-=2) < 0)
  2435. return(0);
  2436. Buffer += 2;
  2437. //
  2438. // take off size of header
  2439. //
  2440. if ((BufferSize -= sizeof(KERB_GSS_SEAL_SIGNATURE)) < 0)
  2441. {
  2442. return(0);
  2443. }
  2444. Buffer += sizeof(KERB_GSS_SEAL_SIGNATURE);
  2445. *InputBuffer = Buffer;
  2446. return((ULONG) BufferSize);
  2447. }
  2448. //+-------------------------------------------------------------------------
  2449. //
  2450. // Function: SpUnsealMessage
  2451. //
  2452. // Synopsis: Decrypts & Verifies an encrypted message according to
  2453. // RFC 1964 Unwrap() API description
  2454. //
  2455. // Effects:
  2456. //
  2457. // Arguments: ContextHandle - Handle of the context to use to sign the
  2458. // message.
  2459. // MessageBuffers - Contains an array of signed buffers and
  2460. // a signature buffer.
  2461. // MessageSequenceNumber - Sequence number for this message,
  2462. // only used in datagram cases.
  2463. // QualityOfProtection - Unused flags.
  2464. //
  2465. // Requires: STATUS_INVALID_HANDLE - the context could not be found or
  2466. // was not configured for message integrity.
  2467. // STATUS_INVALID_PARAMETER - the signature buffer could not
  2468. // be found or was too small.
  2469. //
  2470. // Returns:
  2471. //
  2472. // Notes:
  2473. //
  2474. //
  2475. //--------------------------------------------------------------------------
  2476. NTSTATUS NTAPI
  2477. SpUnsealMessage(
  2478. IN LSA_SEC_HANDLE ContextHandle,
  2479. IN PSecBufferDesc MessageBuffers,
  2480. IN ULONG MessageSequenceNumber,
  2481. OUT PULONG QualityOfProtection
  2482. )
  2483. {
  2484. NTSTATUS Status = STATUS_SUCCESS;
  2485. PKERB_CONTEXT Context = NULL;
  2486. PCHECKSUM_FUNCTION Check = NULL ;
  2487. PCRYPTO_SYSTEM CryptSystem = NULL ;
  2488. PSecBuffer SignatureBuffer = NULL;
  2489. PSecBuffer StreamBuffer = NULL;
  2490. SecBuffer LocalSignatureBuffer = {0};
  2491. SecBuffer LocalDataBuffer = {0};
  2492. SecBufferDesc LocalBufferDesc = {0};
  2493. PSecBufferDesc BufferList = NULL;
  2494. ULONG Index;
  2495. PCHECKSUM_BUFFER CheckBuffer = NULL;
  2496. PCRYPT_STATE_BUFFER CryptBuffer = NULL;
  2497. PKERB_GSS_SEAL_SIGNATURE SealSignature;
  2498. LONG ChecksumType;
  2499. UCHAR LocalChecksum[KERB_MAX_CHECKSUM_LENGTH];
  2500. UCHAR LocalKey[KERB_MAX_KEY_LENGTH];
  2501. UCHAR LocalBlockBuffer[KERB_MAX_BLOCK_LENGTH];
  2502. ULONG BeginBlockSize = 0;
  2503. PBYTE BeginBlockPointer = NULL;
  2504. ULONG EndBlockSize = 0;
  2505. ULONG EncryptBufferSize;
  2506. PBYTE EncryptBuffer;
  2507. BOOLEAN DoDecryption = TRUE;
  2508. ULONG BlockSize = 1;
  2509. ULONG Protection = 0;
  2510. ULONG TotalBufferSize = 0;
  2511. ULONG OutputSize;
  2512. ULONG ContextAttributes;
  2513. ULONG SequenceNumber;
  2514. D_DebugLog((DEB_TRACE_API,"SpUnsealSignature Called\n"));
  2515. D_DebugLog((DEB_TRACE_USER, "SealMessage handle = 0x%x\n",ContextHandle));
  2516. Status = KerbReferenceContextByLsaHandle(
  2517. ContextHandle,
  2518. FALSE, // don't unlink
  2519. &Context
  2520. );
  2521. if (!NT_SUCCESS(Status))
  2522. {
  2523. DebugLog((DEB_ERROR, "Invalid handle supplied for SpUnsealMessage (0x%x) Status = 0x%x. %ws, line %d\n",
  2524. ContextHandle, Status, THIS_FILE, __LINE__));
  2525. goto Cleanup;
  2526. }
  2527. //
  2528. // Find the body and signature SecBuffers from pMessage
  2529. //
  2530. for (Index = 0; Index < MessageBuffers->cBuffers ; Index++ )
  2531. {
  2532. //
  2533. // We can't allow a combination of the two readonly buffer types, or you'll just
  2534. // get READONLY behavior.
  2535. //
  2536. if (( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY ) &&
  2537. ( MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM ))
  2538. {
  2539. Status = SEC_E_INVALID_TOKEN;
  2540. DebugLog((DEB_ERROR, "Can't have readonly & readonly_w_checksum\n"));
  2541. goto Cleanup;
  2542. }
  2543. if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_TOKEN)
  2544. {
  2545. SignatureBuffer = &MessageBuffers->pBuffers[Index];
  2546. }
  2547. else if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_STREAM)
  2548. {
  2549. StreamBuffer = &MessageBuffers->pBuffers[Index];
  2550. //
  2551. // The total buffer size is everything in the stream buffer
  2552. //
  2553. TotalBufferSize = MessageBuffers->pBuffers[Index].cbBuffer;
  2554. }
  2555. else if ((MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY) == 0)
  2556. {
  2557. TotalBufferSize += MessageBuffers->pBuffers[Index].cbBuffer;
  2558. }
  2559. }
  2560. //
  2561. // Check for a stream buffer. If it is present, it contains the whole
  2562. // message
  2563. //
  2564. if (StreamBuffer != NULL)
  2565. {
  2566. if (SignatureBuffer != NULL)
  2567. {
  2568. DebugLog((DEB_ERROR,"Both stream and signature buffer present. %ws, line %d\n",THIS_FILE, __LINE__));
  2569. Status = STATUS_INVALID_PARAMETER;
  2570. goto Cleanup;
  2571. }
  2572. //
  2573. // Parse the stream to distinguish the header from the body
  2574. //
  2575. LocalSignatureBuffer = *StreamBuffer;
  2576. LocalSignatureBuffer.BufferType = SECBUFFER_TOKEN;
  2577. LocalDataBuffer = *StreamBuffer;
  2578. LocalDataBuffer.BufferType = SECBUFFER_DATA;
  2579. LocalDataBuffer.cbBuffer = KerbGetSealMessageBodySize(
  2580. &LocalDataBuffer.pvBuffer,
  2581. LocalDataBuffer.cbBuffer
  2582. );
  2583. if (LocalDataBuffer.cbBuffer == 0)
  2584. {
  2585. DebugLog((DEB_ERROR,"Failed to find header on stream buffer. %ws %d\n",
  2586. THIS_FILE,__LINE__ ));
  2587. Status = STATUS_INVALID_PARAMETER;
  2588. goto Cleanup;
  2589. }
  2590. LocalSignatureBuffer.cbBuffer = StreamBuffer->cbBuffer - LocalDataBuffer.cbBuffer;
  2591. SignatureBuffer = &LocalSignatureBuffer;
  2592. LocalBufferDesc.cBuffers = 1;
  2593. LocalBufferDesc.pBuffers = &LocalDataBuffer;
  2594. BufferList = &LocalBufferDesc;
  2595. //
  2596. // Adjust the total buffer size to remove the signature
  2597. //
  2598. TotalBufferSize -= LocalSignatureBuffer.cbBuffer;
  2599. }
  2600. else if (SignatureBuffer == NULL)
  2601. {
  2602. DebugLog((DEB_ERROR, "No signature buffer found. %ws, line %d\n", THIS_FILE, __LINE__));
  2603. Status = STATUS_INVALID_PARAMETER;
  2604. goto Cleanup;
  2605. }
  2606. else
  2607. {
  2608. BufferList = MessageBuffers;
  2609. }
  2610. ContextAttributes = Context->ContextAttributes;
  2611. //
  2612. // Verify the signature header
  2613. //
  2614. Status = KerbVerifySignatureToken(
  2615. Context,
  2616. SignatureBuffer,
  2617. TotalBufferSize,
  2618. TRUE, // do decrypt
  2619. MessageSequenceNumber,
  2620. (PKERB_GSS_SIGNATURE *) &SealSignature,
  2621. &Protection,
  2622. &ChecksumType,
  2623. &CryptSystem,
  2624. &SequenceNumber
  2625. );
  2626. if (!NT_SUCCESS(Status))
  2627. {
  2628. DebugLog((DEB_ERROR, "Failed to verify signature token: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  2629. goto Cleanup;
  2630. }
  2631. //
  2632. // If the protection level is no encryption, remember not to do the
  2633. // decryption
  2634. //
  2635. if (Protection == KERB_WRAP_NO_ENCRYPT)
  2636. {
  2637. DoDecryption = FALSE;
  2638. }
  2639. //
  2640. // Also, verify that the context was created with the Confidentiality bit
  2641. //
  2642. if ((DoDecryption && (Context->ContextFlags & ISC_RET_CONFIDENTIALITY) == 0))
  2643. {
  2644. DebugLog((DEB_ERROR,"Tried to decrypt using non-confidential context. %ws, line %d\n", THIS_FILE, __LINE__));
  2645. Status = SEC_E_UNSUPPORTED_FUNCTION;
  2646. goto Cleanup;
  2647. }
  2648. BlockSize = CryptSystem->BlockSize;
  2649. //
  2650. // Now compute the checksum and verify it
  2651. //
  2652. Status = CDLocateCheckSum(ChecksumType, &Check);
  2653. if (!NT_SUCCESS(Status))
  2654. {
  2655. DebugLog((DEB_ERROR,"Failed to load MD5 checksum: 0x%x. %ws, line %d\n",Status, THIS_FILE, __LINE__));
  2656. goto Cleanup;
  2657. }
  2658. //
  2659. // Create the encryption key by xoring with 0xf0f0f0f0
  2660. //
  2661. DsysAssert(Context->SessionKey.keyvalue.length <= sizeof(LocalKey));
  2662. if (Context->SessionKey.keyvalue.length > sizeof(LocalKey))
  2663. {
  2664. Status = SEC_E_UNSUPPORTED_FUNCTION;
  2665. goto Cleanup;
  2666. }
  2667. //
  2668. // Generate a check sum of the message, and store it into the signature
  2669. // buffer.
  2670. //
  2671. if (NULL != Check->InitializeEx2)
  2672. {
  2673. Status = Check->InitializeEx2(
  2674. Context->SessionKey.keyvalue.value,
  2675. (ULONG) Context->SessionKey.keyvalue.length,
  2676. SealSignature->Signature.Checksum,
  2677. KERB_PRIV_SALT,
  2678. &CheckBuffer
  2679. );
  2680. }
  2681. else
  2682. {
  2683. Status = Check->InitializeEx(
  2684. Context->SessionKey.keyvalue.value,
  2685. (ULONG) Context->SessionKey.keyvalue.length,
  2686. KERB_PRIV_SALT,
  2687. &CheckBuffer
  2688. );
  2689. }
  2690. if (!NT_SUCCESS(Status))
  2691. {
  2692. goto Cleanup;
  2693. }
  2694. for (Index = 0; Index < Context->SessionKey.keyvalue.length ; Index++ )
  2695. {
  2696. LocalKey[Index] = Context->SessionKey.keyvalue.value[Index] ^ 0xf0;
  2697. }
  2698. //
  2699. // Sum in 8 bytes of the signature
  2700. //
  2701. Check->Sum(
  2702. CheckBuffer,
  2703. 8,
  2704. ((PUCHAR) SealSignature) -2
  2705. );
  2706. if (DoDecryption)
  2707. {
  2708. Status = CryptSystem->Initialize(
  2709. LocalKey,
  2710. Context->SessionKey.keyvalue.length,
  2711. 0, // no options
  2712. &CryptBuffer
  2713. );
  2714. if (!NT_SUCCESS(Status))
  2715. {
  2716. goto Cleanup;
  2717. }
  2718. //
  2719. // Decrypt the confounder
  2720. //
  2721. if ((CryptSystem->EncryptionType == KERB_ETYPE_RC4_PLAIN) ||
  2722. (CryptSystem->EncryptionType == KERB_ETYPE_RC4_PLAIN_EXP))
  2723. {
  2724. Status = CryptSystem->Control(
  2725. CRYPT_CONTROL_SET_INIT_VECT,
  2726. CryptBuffer,
  2727. (PUCHAR) &SequenceNumber,
  2728. sizeof(ULONG)
  2729. );
  2730. if (!NT_SUCCESS(Status))
  2731. {
  2732. goto Cleanup;
  2733. }
  2734. }
  2735. OutputSize = KERB_GSS_SIG_CONFOUNDER_SIZE;
  2736. Status = CryptSystem->Decrypt(
  2737. CryptBuffer,
  2738. SealSignature->Confounder,
  2739. KERB_GSS_SIG_CONFOUNDER_SIZE,
  2740. SealSignature->Confounder,
  2741. &OutputSize
  2742. );
  2743. if (!NT_SUCCESS(Status))
  2744. {
  2745. goto Cleanup;
  2746. }
  2747. }
  2748. //
  2749. // Sum the confounder
  2750. //
  2751. Check->Sum(
  2752. CheckBuffer,
  2753. KERB_GSS_SIG_CONFOUNDER_SIZE,
  2754. SealSignature->Confounder
  2755. );
  2756. for (Index = 0; Index < BufferList->cBuffers; Index++ )
  2757. {
  2758. if ((BUFFERTYPE(BufferList->pBuffers[Index]) != SECBUFFER_TOKEN) &&
  2759. (!(BufferList->pBuffers[Index].BufferType & SECBUFFER_READONLY )) &&
  2760. (BufferList->pBuffers[Index].cbBuffer != 0))
  2761. {
  2762. if (!(MessageBuffers->pBuffers[Index].BufferType & SECBUFFER_READONLY_WITH_CHECKSUM))
  2763. {
  2764. //
  2765. // Take into account that the input buffers may not all be aligned
  2766. // properly
  2767. //
  2768. //
  2769. // If there is a fragment to decrypt, convert it to a block
  2770. // size fragment
  2771. //
  2772. if (BeginBlockSize != 0)
  2773. {
  2774. EncryptBuffer = (PBYTE) BufferList->pBuffers[Index].pvBuffer +
  2775. (BlockSize - BeginBlockSize);
  2776. EncryptBufferSize = BufferList->pBuffers[Index].cbBuffer -
  2777. (BlockSize - BeginBlockSize);
  2778. }
  2779. else
  2780. {
  2781. EncryptBuffer = (PBYTE) BufferList->pBuffers[Index].pvBuffer;
  2782. EncryptBufferSize = BufferList->pBuffers[Index].cbBuffer;
  2783. }
  2784. EndBlockSize = EncryptBufferSize - ROUND_DOWN_COUNT(EncryptBufferSize,BlockSize);
  2785. DsysAssert(EndBlockSize < BlockSize);
  2786. EncryptBufferSize = EncryptBufferSize - EndBlockSize;
  2787. if (BeginBlockSize != 0)
  2788. {
  2789. RtlCopyMemory(
  2790. LocalBlockBuffer+BeginBlockSize,
  2791. BufferList->pBuffers[Index].pvBuffer,
  2792. BlockSize - BeginBlockSize
  2793. );
  2794. //
  2795. // Now decrypt the buffer
  2796. //
  2797. if (DoDecryption)
  2798. {
  2799. Status = CryptSystem->Decrypt(
  2800. CryptBuffer,
  2801. LocalBlockBuffer,
  2802. BlockSize,
  2803. LocalBlockBuffer,
  2804. &OutputSize
  2805. );
  2806. if (!NT_SUCCESS(Status))
  2807. {
  2808. goto Cleanup;
  2809. }
  2810. }
  2811. //
  2812. // Then checksum the buffer
  2813. //
  2814. Check->Sum(
  2815. CheckBuffer,
  2816. BlockSize,
  2817. LocalBlockBuffer
  2818. );
  2819. //
  2820. // Copy the pieces back
  2821. //
  2822. RtlCopyMemory(
  2823. BeginBlockPointer,
  2824. LocalBlockBuffer,
  2825. BeginBlockSize
  2826. );
  2827. RtlCopyMemory(
  2828. BufferList->pBuffers[Index].pvBuffer,
  2829. LocalBlockBuffer + BeginBlockSize,
  2830. BlockSize - BeginBlockSize
  2831. );
  2832. }
  2833. //
  2834. // Decrypt the buffer first
  2835. //
  2836. if (DoDecryption)
  2837. {
  2838. OutputSize = BufferList->pBuffers[Index].cbBuffer;
  2839. Status = CryptSystem->Decrypt(
  2840. CryptBuffer,
  2841. EncryptBuffer,
  2842. EncryptBufferSize,
  2843. EncryptBuffer,
  2844. &OutputSize
  2845. );
  2846. if (!NT_SUCCESS(Status))
  2847. {
  2848. goto Cleanup;
  2849. }
  2850. // DsysAssert(OutputSize == BufferList->pBuffers[Index].cbBuffer);
  2851. }
  2852. //
  2853. // Prepare for the next go-round
  2854. //
  2855. RtlCopyMemory(
  2856. LocalBlockBuffer,
  2857. EncryptBuffer+EncryptBufferSize,
  2858. EndBlockSize
  2859. );
  2860. BeginBlockSize = EndBlockSize;
  2861. BeginBlockPointer = (PBYTE) MessageBuffers->pBuffers[Index].pvBuffer +
  2862. MessageBuffers->pBuffers[Index].cbBuffer -
  2863. EndBlockSize;
  2864. }
  2865. else
  2866. {
  2867. //
  2868. // Just include the checksum of this buffer, its READONLY_WITH_CHECKSUM.
  2869. //
  2870. EncryptBuffer = (PBYTE) BufferList->pBuffers[Index].pvBuffer;
  2871. EncryptBufferSize = BufferList->pBuffers[Index].cbBuffer;
  2872. }
  2873. //
  2874. // Then checksum the buffer
  2875. //
  2876. Check->Sum(
  2877. CheckBuffer,
  2878. EncryptBufferSize,
  2879. EncryptBuffer
  2880. );
  2881. }
  2882. }
  2883. (void) Check->Finalize(CheckBuffer, LocalChecksum);
  2884. Status = Check->Finish(&CheckBuffer);
  2885. CheckBuffer = NULL;
  2886. if (!NT_SUCCESS(Status))
  2887. {
  2888. goto Cleanup;
  2889. }
  2890. //
  2891. // Make sure there are no left-over bits
  2892. //
  2893. if (BeginBlockSize != 0)
  2894. {
  2895. DebugLog((DEB_ERROR,"Non-aligned buffer size to SealMessage: %d extra bytes\n",
  2896. BeginBlockSize ));
  2897. Status = SEC_E_INVALID_TOKEN;
  2898. goto Cleanup;
  2899. }
  2900. if (!RtlEqualMemory(
  2901. LocalChecksum,
  2902. SealSignature->Signature.Checksum,
  2903. 8))
  2904. {
  2905. D_DebugLog((DEB_ERROR, "Bad checksum %ws, %d\n", THIS_FILE, __LINE__));
  2906. Status = SEC_E_MESSAGE_ALTERED;
  2907. goto Cleanup;
  2908. }
  2909. if (ARGUMENT_PRESENT(QualityOfProtection))
  2910. {
  2911. *QualityOfProtection = Protection;
  2912. }
  2913. //
  2914. // If this was a stream input, return the data in the data buffer
  2915. //
  2916. if (StreamBuffer != NULL)
  2917. {
  2918. BYTE PaddingBytes;
  2919. //
  2920. // Pull the padding off the data buffer
  2921. //
  2922. if (LocalDataBuffer.cbBuffer < 1)
  2923. {
  2924. DebugLog((DEB_ERROR,"Data buffer is zero length!\n"));
  2925. Status = STATUS_INVALID_PARAMETER;
  2926. goto Cleanup;
  2927. }
  2928. PaddingBytes = *(((PBYTE)LocalDataBuffer.pvBuffer) + LocalDataBuffer.cbBuffer - 1 );
  2929. //
  2930. // Verify the padding:
  2931. //
  2932. if ((BlockSize >= PaddingBytes) &&
  2933. (LocalDataBuffer.cbBuffer >= PaddingBytes))
  2934. {
  2935. LocalDataBuffer.cbBuffer -= PaddingBytes;
  2936. for (Index = 0; Index < MessageBuffers->cBuffers; Index++ )
  2937. {
  2938. if (BUFFERTYPE(MessageBuffers->pBuffers[Index]) == SECBUFFER_DATA)
  2939. {
  2940. MessageBuffers->pBuffers[Index] = LocalDataBuffer;
  2941. break;
  2942. }
  2943. }
  2944. }
  2945. else
  2946. {
  2947. DebugLog((DEB_ERROR,"Bad padding: %d bytes\n", PaddingBytes));
  2948. Status = STATUS_INVALID_PARAMETER;
  2949. }
  2950. }
  2951. Cleanup:
  2952. if (Context != NULL)
  2953. {
  2954. KerbDereferenceContext(Context);
  2955. }
  2956. if ( ( CheckBuffer != NULL ) &&
  2957. ( Check != NULL ) )
  2958. {
  2959. Check->Finish(&CheckBuffer);
  2960. }
  2961. if ( ( CryptBuffer != NULL ) &&
  2962. ( CryptSystem != NULL ) )
  2963. {
  2964. CryptSystem->Discard(&CryptBuffer);
  2965. }
  2966. D_DebugLog((DEB_TRACE_API, "SpUnsealMessage returned 0x%x\n", KerbMapKerbNtStatusToNtStatus(Status)));
  2967. return(KerbMapKerbNtStatusToNtStatus(Status));
  2968. }
  2969. #ifndef WIN32_CHICAGO
  2970. //+-------------------------------------------------------------------------
  2971. //
  2972. // Function: SpGetContextToken
  2973. //
  2974. // Synopsis: returns a pointer to the token for a server-side context
  2975. //
  2976. // Effects:
  2977. //
  2978. // Arguments:
  2979. //
  2980. // Requires:
  2981. //
  2982. // Returns:
  2983. //
  2984. // Notes:
  2985. //
  2986. //
  2987. //--------------------------------------------------------------------------
  2988. NTSTATUS NTAPI
  2989. SpGetContextToken(
  2990. IN LSA_SEC_HANDLE ContextHandle,
  2991. OUT PHANDLE ImpersonationToken
  2992. )
  2993. {
  2994. NTSTATUS Status = STATUS_SUCCESS;
  2995. PKERB_CONTEXT Context = NULL;
  2996. LARGE_INTEGER CurrentTime;
  2997. LARGE_INTEGER ContextExpires;
  2998. D_DebugLog((DEB_TRACE_API,"SpGetContextToken called pid:0x%x, ctxt:0x%x\n", GetCurrentProcessId(), ContextHandle));
  2999. if (ImpersonationToken == NULL)
  3000. {
  3001. Status = STATUS_INVALID_PARAMETER;
  3002. DebugLog((DEB_ERROR, "Null token handle supplied for GetContextToken. %ws, line %d\n", THIS_FILE, __LINE__));
  3003. goto Cleanup;
  3004. }
  3005. Status = KerbReferenceContextByLsaHandle(
  3006. ContextHandle,
  3007. FALSE, // don't unlink
  3008. &Context
  3009. );
  3010. if (!NT_SUCCESS(Status))
  3011. {
  3012. DebugLog((DEB_ERROR, "Invalid handle supplied for GetContextToken(0x%x) Status = 0x%x. %ws, line %d\n",
  3013. ContextHandle, Status, THIS_FILE, __LINE__));
  3014. }
  3015. GetSystemTimeAsFileTime((PFILETIME) &CurrentTime);
  3016. KerbReadLockContexts();
  3017. *ImpersonationToken = Context->TokenHandle;
  3018. ContextExpires = Context->Lifetime;
  3019. KerbUnlockContexts();
  3020. if (KerbGlobalEnforceTime && ContextExpires.QuadPart < CurrentTime.QuadPart)
  3021. {
  3022. DebugLog((DEB_ERROR, "GetContextToken: Context 0x%x expired. %ws, line %d\n", ContextHandle, THIS_FILE, __LINE__));
  3023. Status = SEC_E_CONTEXT_EXPIRED;
  3024. *ImpersonationToken = NULL;
  3025. }
  3026. else if (*ImpersonationToken == NULL)
  3027. {
  3028. Status = SEC_E_NO_IMPERSONATION;
  3029. }
  3030. if (Context != NULL)
  3031. {
  3032. //
  3033. // Note: once we dereference the context the handle we return
  3034. // may go away or be re-used. That is the price we have to pay
  3035. // to avoid duplicating it.
  3036. //
  3037. KerbDereferenceContext(Context);
  3038. }
  3039. Cleanup:
  3040. D_DebugLog((DEB_TRACE_API,"SpGetContextToken returned 0x%x, pid:0x%x, ctxt:0x%x\n", KerbMapKerbNtStatusToNtStatus(Status), GetCurrentProcessId(), ContextHandle));
  3041. return(KerbMapKerbNtStatusToNtStatus(Status));
  3042. }
  3043. #endif // WIN32_CHICAGO
  3044. //+-------------------------------------------------------------------------
  3045. //
  3046. // Function: SpQueryContextAttributes
  3047. //
  3048. // Synopsis: Querys attributes of the specified context
  3049. //
  3050. // Effects:
  3051. //
  3052. // Arguments:
  3053. //
  3054. // Requires:
  3055. //
  3056. // Returns:
  3057. //
  3058. // Notes:
  3059. //
  3060. //
  3061. //--------------------------------------------------------------------------
  3062. NTSTATUS NTAPI
  3063. SpQueryContextAttributes(
  3064. IN LSA_SEC_HANDLE ContextHandle,
  3065. IN ULONG ContextAttribute,
  3066. IN OUT PVOID Buffer
  3067. )
  3068. {
  3069. NTSTATUS Status = STATUS_SUCCESS;
  3070. PKERB_CONTEXT Context = NULL;
  3071. PSecPkgContext_Sizes SizeInfo;
  3072. PSecPkgContext_Names NameInfo;
  3073. PSecPkgContext_DceInfo DceInfo;
  3074. PSecPkgContext_Lifespan LifespanInfo;
  3075. PSecPkgContext_Flags FlagsInfo;
  3076. PSecPkgContext_PackageInfo PackageInfo;
  3077. PSecPkgContext_NegotiationInfo NegInfo ;
  3078. PSecPkgContext_SessionKey SessionKeyInfo;
  3079. PSecPkgContext_KeyInfo KeyInfo;
  3080. PSecPkgContext_AccessToken AccessToken;
  3081. PSecPkgContext_TargetInformation TargetInformation;
  3082. ULONG PackageInfoSize = 0;
  3083. UNICODE_STRING FullName;
  3084. LONG ChecksumType;
  3085. LONG EncryptType;
  3086. PCRYPTO_SYSTEM CryptSystem = NULL ;
  3087. TimeStamp CurrentTime;
  3088. D_DebugLog((DEB_TRACE_API,"SpQueryContextAttributes called pid:0x%x, ctxt:0x%x, Attr:0x%x\n", GetCurrentProcessId(), ContextHandle, ContextAttribute));
  3089. Status = KerbReferenceContextByLsaHandle(
  3090. ContextHandle,
  3091. FALSE, // don't unlink
  3092. &Context
  3093. );
  3094. if (!NT_SUCCESS(Status))
  3095. {
  3096. DebugLog((DEB_ERROR, "Invalid handle supplied for QueryContextAttributes(0x%x) Status = 0x%x. %ws, line %d\n",
  3097. ContextHandle, Status, THIS_FILE, __LINE__));
  3098. goto Cleanup;
  3099. }
  3100. //
  3101. // Return the appropriate information
  3102. //
  3103. switch(ContextAttribute)
  3104. {
  3105. case SECPKG_ATTR_SIZES:
  3106. gss_OID_desc * MechId;
  3107. UINT MessageSize;
  3108. if ((Context->ContextAttributes & KERB_CONTEXT_USER_TO_USER) != 0)
  3109. {
  3110. MechId = gss_mech_krb5_u2u;
  3111. }
  3112. else
  3113. {
  3114. MechId = gss_mech_krb5_new;
  3115. }
  3116. //
  3117. // The sizes returned are used by RPC to determine whether to call
  3118. // MakeSignature or SealMessage. The signature size should be zero
  3119. // if neither is to be called, and the block size and trailer size
  3120. // should be zero if SignMessage is not to be called.
  3121. //
  3122. SizeInfo = (PSecPkgContext_Sizes) Buffer;
  3123. SizeInfo->cbMaxToken = KerbGlobalMaxTokenSize;
  3124. // If we need to be Gss Compatible, then the Signature buffer size is
  3125. // dependent on the message size. So, we'll set it to be largest pad
  3126. // for the largest message size, say 1G. But, don't tax dce style
  3127. // callers with extra bytes.
  3128. if (((Context->ContextFlags & ISC_RET_DATAGRAM) != 0) ||
  3129. ((Context->ContextFlags & ISC_RET_USED_DCE_STYLE) != 0))
  3130. {
  3131. MessageSize = 0;
  3132. }
  3133. else
  3134. {
  3135. MessageSize = KERB_MAX_MESSAGE_SIZE;
  3136. }
  3137. if ((Context->ContextFlags & (KERB_SIGN_FLAGS | ISC_RET_CONFIDENTIALITY)) != 0)
  3138. {
  3139. SizeInfo->cbMaxSignature = g_token_size(MechId, sizeof(KERB_GSS_SIGNATURE));
  3140. }
  3141. else
  3142. {
  3143. SizeInfo->cbMaxSignature = sizeof(KERB_NULL_SIGNATURE);
  3144. }
  3145. //
  3146. // get the encryption type for the context
  3147. //
  3148. Status = KerbGetChecksumAndEncryptionType(
  3149. Context,
  3150. KERB_WRAP_NO_ENCRYPT, // checksum not needed so use hardcoded QOP
  3151. &ChecksumType, // checksum not needed here
  3152. &EncryptType
  3153. );
  3154. if (!NT_SUCCESS(Status))
  3155. {
  3156. goto Cleanup;
  3157. }
  3158. //
  3159. // Locate the cryptsystem for the context, loading it if necessary from the
  3160. // the crypto support DLL
  3161. //
  3162. Status = CDLocateCSystem(EncryptType, &CryptSystem);
  3163. if (!NT_SUCCESS(Status))
  3164. {
  3165. DebugLog((DEB_ERROR,"Failed to load %d crypt system: 0x%x. %ws, line %d\n",EncryptType,Status, THIS_FILE, __LINE__));
  3166. goto Cleanup;
  3167. }
  3168. //
  3169. // RPC keys off the trailer size to tell whether or not
  3170. // to encrypt, not the flags from isc/asc. So, for dce style,
  3171. // say the blocksize & trailersize are zero.
  3172. //
  3173. if (((Context->ContextFlags & ISC_RET_CONFIDENTIALITY) != 0) ||
  3174. ((Context->ContextFlags & ISC_RET_USED_DCE_STYLE) == 0))
  3175. {
  3176. //
  3177. // Use block size from crypto system
  3178. //
  3179. SizeInfo->cbBlockSize = CryptSystem->BlockSize;
  3180. SizeInfo->cbSecurityTrailer =
  3181. g_token_size(MechId, sizeof(KERB_GSS_SEAL_SIGNATURE) + MessageSize) - MessageSize;
  3182. }
  3183. else
  3184. {
  3185. SizeInfo->cbBlockSize = 0;
  3186. SizeInfo->cbSecurityTrailer = 0;
  3187. }
  3188. break;
  3189. case SECPKG_ATTR_SESSION_KEY:
  3190. SessionKeyInfo = (PSecPkgContext_SessionKey) Buffer;
  3191. SessionKeyInfo->SessionKeyLength = Context->SessionKey.keyvalue.length;
  3192. if (SessionKeyInfo->SessionKeyLength != 0)
  3193. {
  3194. SessionKeyInfo->SessionKey = (PUCHAR)
  3195. UserFunctions->AllocateHeap(
  3196. SessionKeyInfo->SessionKeyLength);
  3197. if (SessionKeyInfo->SessionKey!=NULL)
  3198. {
  3199. RtlCopyMemory(
  3200. SessionKeyInfo->SessionKey,
  3201. Context->SessionKey.keyvalue.value,
  3202. Context->SessionKey.keyvalue.length
  3203. );
  3204. }
  3205. else
  3206. {
  3207. Status = STATUS_INSUFFICIENT_RESOURCES;
  3208. }
  3209. }
  3210. else
  3211. {
  3212. SessionKeyInfo->SessionKey = (PUCHAR) UserFunctions->AllocateHeap(1);
  3213. if (SessionKeyInfo->SessionKey!=NULL)
  3214. {
  3215. *(PUCHAR) SessionKeyInfo->SessionKey = 0;
  3216. }
  3217. else
  3218. {
  3219. Status = STATUS_INSUFFICIENT_RESOURCES;
  3220. }
  3221. }
  3222. break;
  3223. case SECPKG_ATTR_NAMES:
  3224. NameInfo = (PSecPkgContext_Names) Buffer;
  3225. if (!KERB_SUCCESS(KerbBuildFullServiceName(
  3226. &Context->ClientRealm,
  3227. &Context->ClientName,
  3228. &FullName
  3229. )))
  3230. {
  3231. Status = STATUS_INSUFFICIENT_RESOURCES;
  3232. goto Cleanup;
  3233. }
  3234. #ifndef WIN32_CHICAGO
  3235. NameInfo->sUserName = (LPWSTR) UserFunctions->AllocateHeap(FullName.Length + sizeof(WCHAR));
  3236. if (NameInfo->sUserName != NULL)
  3237. {
  3238. RtlCopyMemory(
  3239. NameInfo->sUserName,
  3240. FullName.Buffer,
  3241. FullName.Length
  3242. );
  3243. NameInfo->sUserName[FullName.Length/sizeof(WCHAR)] = L'\0';
  3244. }
  3245. #else // WIN32_CHICAGO
  3246. ANSI_STRING AnsiString;
  3247. RtlUnicodeStringToAnsiString( &AnsiString,
  3248. &FullName,
  3249. TRUE);
  3250. NameInfo->sUserName = (LPTSTR) UserFunctions->AllocateHeap(AnsiString.Length + sizeof(CHAR));
  3251. if (NameInfo->sUserName != NULL)
  3252. {
  3253. RtlCopyMemory(
  3254. NameInfo->sUserName,
  3255. AnsiString.Buffer,
  3256. AnsiString.Length
  3257. );
  3258. NameInfo->sUserName[AnsiString.Length] = '\0';
  3259. RtlFreeAnsiString(&AnsiString);
  3260. }
  3261. #endif // WIN32_CHICAGO
  3262. else
  3263. {
  3264. Status = STATUS_INSUFFICIENT_RESOURCES;
  3265. }
  3266. KerbFreeString(&FullName);
  3267. break;
  3268. case SECPKG_ATTR_DCE_INFO:
  3269. DceInfo = (PSecPkgContext_DceInfo) Buffer;
  3270. if (!KERB_SUCCESS(KerbBuildFullServiceName(
  3271. &Context->ClientRealm,
  3272. &Context->ClientName,
  3273. &FullName
  3274. )))
  3275. {
  3276. Status = STATUS_INSUFFICIENT_RESOURCES;
  3277. goto Cleanup;
  3278. }
  3279. DceInfo->AuthzSvc = RPC_C_AUTHZ_NAME;
  3280. #ifndef WIN32_CHICAGO
  3281. DceInfo->pPac = UserFunctions->AllocateHeap(FullName.Length + sizeof(WCHAR));
  3282. if (DceInfo->pPac != NULL)
  3283. {
  3284. RtlCopyMemory(
  3285. DceInfo->pPac,
  3286. FullName.Buffer,
  3287. FullName.Length
  3288. );
  3289. ((LPWSTR)DceInfo->pPac)[FullName.Length/sizeof(WCHAR)] = L'\0';
  3290. }
  3291. #else // WIN32_CHICAGO
  3292. RtlUnicodeStringToAnsiString( &AnsiString,
  3293. &FullName,
  3294. TRUE);
  3295. DceInfo->pPac = UserFunctions->AllocateHeap(AnsiString.Length + sizeof(CHAR));
  3296. if (DceInfo->pPac != NULL)
  3297. {
  3298. RtlCopyMemory(
  3299. DceInfo->pPac,
  3300. AnsiString.Buffer,
  3301. AnsiString.Length
  3302. );
  3303. ((LPTSTR) DceInfo->pPac)[AnsiString.Length] = '\0';
  3304. RtlFreeAnsiString(&AnsiString);
  3305. }
  3306. #endif // WIN32_CHICAGO
  3307. else
  3308. {
  3309. Status = STATUS_INSUFFICIENT_RESOURCES;
  3310. }
  3311. KerbFreeString(&FullName);
  3312. break;
  3313. case SECPKG_ATTR_TARGET_INFORMATION:
  3314. {
  3315. TargetInformation = (PSecPkgContext_TargetInformation) Buffer;
  3316. if (TargetInformation == NULL)
  3317. {
  3318. Status = STATUS_INVALID_PARAMETER;
  3319. break;
  3320. }
  3321. TargetInformation->MarshalledTargetInfo = NULL;
  3322. if (Context->pbMarshalledTargetInfo == NULL)
  3323. {
  3324. TargetInformation->MarshalledTargetInfo = NULL;
  3325. TargetInformation->MarshalledTargetInfoLength = 0;
  3326. break;
  3327. }
  3328. TargetInformation->MarshalledTargetInfo = (PUCHAR) UserFunctions->AllocateHeap(
  3329. Context->cbMarshalledTargetInfo
  3330. );
  3331. if (TargetInformation->MarshalledTargetInfo != NULL)
  3332. {
  3333. RtlCopyMemory(
  3334. TargetInformation->MarshalledTargetInfo,
  3335. Context->pbMarshalledTargetInfo,
  3336. Context->cbMarshalledTargetInfo
  3337. );
  3338. TargetInformation->MarshalledTargetInfoLength = Context->cbMarshalledTargetInfo;
  3339. }
  3340. else
  3341. {
  3342. Status = STATUS_INSUFFICIENT_RESOURCES;
  3343. }
  3344. break;
  3345. }
  3346. case SECPKG_ATTR_LIFESPAN:
  3347. LifespanInfo = (PSecPkgContext_Lifespan) Buffer;
  3348. if (KerbGetTime(Context->StartTime) != KerbGetTime(KerbGlobalHasNeverTime))
  3349. {
  3350. KerbUtcTimeToLocalTime(
  3351. &LifespanInfo->tsStart,
  3352. &(Context->StartTime)
  3353. );
  3354. D_DebugLog((DEB_TRACE, "Used context start time \n"));
  3355. }
  3356. else if (NULL != Context->TicketCacheEntry)
  3357. {
  3358. KerbUtcTimeToLocalTime(
  3359. &LifespanInfo->tsStart,
  3360. &(Context->TicketCacheEntry->StartTime)
  3361. );
  3362. KerbWriteLockContexts();
  3363. Context->StartTime = Context->TicketCacheEntry->StartTime;
  3364. KerbUnlockContexts();
  3365. DebugLog((DEB_ERROR, "Used tkt cache entry start time \n"));
  3366. }
  3367. else // set it to current time
  3368. {
  3369. // The context is not in a state where we've got a
  3370. // tkt cache entry, so let's use current time.
  3371. GetSystemTimeAsFileTime((PFILETIME)
  3372. &CurrentTime
  3373. );
  3374. KerbUtcTimeToLocalTime(
  3375. &LifespanInfo->tsStart,
  3376. &CurrentTime
  3377. );
  3378. DebugLog((DEB_ERROR, "NO START TIME PRESENT IN CONTEXT, or CACHE ENTRY!\n"));
  3379. }
  3380. KerbUtcTimeToLocalTime(
  3381. &LifespanInfo->tsExpiry,
  3382. &Context->Lifetime
  3383. );
  3384. break;
  3385. case SECPKG_ATTR_FLAGS:
  3386. FlagsInfo = (PSecPkgContext_Flags) Buffer;
  3387. if ((Context->ContextAttributes & KERB_CONTEXT_INBOUND) != 0)
  3388. {
  3389. FlagsInfo->Flags = KerbMapContextFlags( Context->ContextFlags );
  3390. }
  3391. else
  3392. {
  3393. FlagsInfo->Flags = Context->ContextFlags;
  3394. }
  3395. break;
  3396. #ifndef WIN32_CHICAGO
  3397. case SECPKG_ATTR_KEY_INFO:
  3398. PCRYPTO_SYSTEM CryptoSystem;
  3399. KeyInfo = (PSecPkgContext_KeyInfo) Buffer;
  3400. KeyInfo->KeySize = KerbIsKeyExportable(&Context->SessionKey) ? 56 : 128;
  3401. KeyInfo->EncryptAlgorithm = Context->SessionKey.keytype;
  3402. KeyInfo->SignatureAlgorithm = KERB_IS_DES_ENCRYPTION(Context->SessionKey.keytype) ? KERB_CHECKSUM_MD25 : KERB_CHECKSUM_HMAC_MD5;
  3403. KeyInfo->sSignatureAlgorithmName = NULL;
  3404. KeyInfo->sEncryptAlgorithmName = NULL;
  3405. //
  3406. // The checksum doesn't include a name, so don't fill it in - leave
  3407. // it as an empty string, so callers don't die when they
  3408. // try to manipulate it.
  3409. //
  3410. Status = CDLocateCSystem(KeyInfo->EncryptAlgorithm, &CryptoSystem);
  3411. if (NT_SUCCESS(Status))
  3412. {
  3413. KeyInfo->sEncryptAlgorithmName = (LPWSTR)
  3414. UserFunctions->AllocateHeap(sizeof(WCHAR) * ((ULONG) wcslen(CryptoSystem->Name) + 1));
  3415. if (KeyInfo->sEncryptAlgorithmName != NULL)
  3416. {
  3417. wcscpy(
  3418. KeyInfo->sEncryptAlgorithmName,
  3419. CryptoSystem->Name
  3420. );
  3421. KeyInfo->sSignatureAlgorithmName = (LPWSTR)
  3422. UserFunctions->AllocateHeap(sizeof(WCHAR));
  3423. if (KeyInfo->sSignatureAlgorithmName != NULL)
  3424. {
  3425. *KeyInfo->sSignatureAlgorithmName = L'\0';
  3426. }
  3427. else
  3428. {
  3429. Status = STATUS_INSUFFICIENT_RESOURCES;
  3430. UserFunctions->FreeHeap(KeyInfo->sEncryptAlgorithmName);
  3431. KeyInfo->sEncryptAlgorithmName = NULL;
  3432. }
  3433. }
  3434. else
  3435. {
  3436. Status = STATUS_INSUFFICIENT_RESOURCES;
  3437. }
  3438. }
  3439. break;
  3440. #endif // WIN32_CHICAGO
  3441. case SECPKG_ATTR_PACKAGE_INFO:
  3442. case SECPKG_ATTR_NEGOTIATION_INFO:
  3443. //
  3444. // Return the information about this package. This is useful for
  3445. // callers who used SPNEGO and don't know what package they got.
  3446. //
  3447. PackageInfo = (PSecPkgContext_PackageInfo) Buffer;
  3448. PackageInfoSize = sizeof(SecPkgInfo) + sizeof(KERBEROS_PACKAGE_NAME) + sizeof(KERBEROS_PACKAGE_COMMENT);
  3449. PackageInfo->PackageInfo = (PSecPkgInfo) UserFunctions->AllocateHeap(PackageInfoSize);
  3450. if (PackageInfo->PackageInfo == NULL)
  3451. {
  3452. Status = STATUS_INSUFFICIENT_RESOURCES;
  3453. goto Cleanup;
  3454. }
  3455. PackageInfo->PackageInfo->Name = (LPTSTR) (PackageInfo->PackageInfo + 1);
  3456. PackageInfo->PackageInfo->Comment = (LPTSTR) (((PBYTE) PackageInfo->PackageInfo->Name) + sizeof(KERBEROS_PACKAGE_NAME));
  3457. lstrcpy(
  3458. PackageInfo->PackageInfo->Name,
  3459. KERBEROS_PACKAGE_NAME
  3460. );
  3461. lstrcpy(
  3462. PackageInfo->PackageInfo->Comment,
  3463. KERBEROS_PACKAGE_COMMENT
  3464. );
  3465. PackageInfo->PackageInfo->wVersion = SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION;
  3466. PackageInfo->PackageInfo->wRPCID = RPC_C_AUTHN_GSS_KERBEROS;
  3467. PackageInfo->PackageInfo->fCapabilities = KERBEROS_CAPABILITIES;
  3468. PackageInfo->PackageInfo->cbMaxToken = KerbGlobalMaxTokenSize;
  3469. if ( ContextAttribute == SECPKG_ATTR_NEGOTIATION_INFO )
  3470. {
  3471. NegInfo = (PSecPkgContext_NegotiationInfo) PackageInfo ;
  3472. NegInfo->NegotiationState = Context->NegotiationInfo ;
  3473. }
  3474. break;
  3475. case SECPKG_ATTR_ACCESS_TOKEN:
  3476. {
  3477. AccessToken = (PSecPkgContext_AccessToken) Buffer;
  3478. //
  3479. // ClientTokenHandle can be NULL, for instance:
  3480. // 1. client side context.
  3481. // 2. incomplete server context.
  3482. //
  3483. AccessToken->AccessToken = (void*)Context->TokenHandle;
  3484. break;
  3485. }
  3486. default:
  3487. Status = STATUS_NOT_SUPPORTED;
  3488. break;
  3489. }
  3490. Cleanup:
  3491. if (Context != NULL)
  3492. {
  3493. KerbDereferenceContext(Context);
  3494. }
  3495. D_DebugLog((DEB_TRACE_API,"SpQueryContextAttributes returned 0x%x, pid:0x%x, ctxt:0x%x, Attr:0x%x\n", KerbMapKerbNtStatusToNtStatus(Status), GetCurrentProcessId(), ContextHandle, ContextAttribute));
  3496. return(KerbMapKerbNtStatusToNtStatus(Status));
  3497. }
  3498. //+-------------------------------------------------------------------------
  3499. //
  3500. // Function: SpQueryLsaModeContextAttributes
  3501. //
  3502. // Synopsis: Querys attributes of the specified context
  3503. //
  3504. // Effects:
  3505. //
  3506. // Arguments:
  3507. //
  3508. // Requires:
  3509. //
  3510. // Returns:
  3511. //
  3512. // Notes:
  3513. //
  3514. //
  3515. //--------------------------------------------------------------------------
  3516. NTSTATUS NTAPI
  3517. SpQueryLsaModeContextAttributes(
  3518. IN LSA_SEC_HANDLE ContextHandle,
  3519. IN ULONG ContextAttribute,
  3520. IN OUT PVOID Buffer
  3521. )
  3522. {
  3523. NTSTATUS Status = STATUS_SUCCESS;
  3524. PKERB_CONTEXT Context = NULL;
  3525. SecPkgContext_NativeNames NameInfo = {0};
  3526. BOOLEAN ContextsLocked = FALSE;
  3527. UNICODE_STRING ServerName = {0};
  3528. UNICODE_STRING ClientName = {0};
  3529. BOOLEAN IsClientContext = FALSE;
  3530. BOOLEAN TicketCacheLocked = FALSE;
  3531. SecPkgContext_NegotiationInfo NegInfo = {0};
  3532. ULONG PackageInfoSize;
  3533. D_DebugLog((DEB_TRACE_API,"SpQueryLsaModeContextAttributes called ctxt:0x%x, Attr:0x%x\n", ContextHandle, ContextAttribute));
  3534. Status = KerbReferenceContext(
  3535. ContextHandle,
  3536. FALSE, // don't unlink
  3537. &Context
  3538. );
  3539. if (!NT_SUCCESS(Status))
  3540. {
  3541. DebugLog((DEB_ERROR, "Invalid handle supplied for QueryContextAttributes(0x%x) Status = 0x%x. %ws, line %d\n",
  3542. ContextHandle, Status, THIS_FILE, __LINE__));
  3543. goto Cleanup;
  3544. }
  3545. //
  3546. // Ticket cache lock must be acquired prior to context lock according
  3547. // to the documented locking order; it is going to be needed for SECPKG_ATTR_NATIVE_NAMES,
  3548. // so grab it here to avoid a deadlock
  3549. //
  3550. if ( ContextAttribute == SECPKG_ATTR_NATIVE_NAMES )
  3551. {
  3552. KerbReadLockTicketCache();
  3553. TicketCacheLocked = TRUE;
  3554. }
  3555. KerbReadLockContexts();
  3556. ContextsLocked = TRUE;
  3557. //
  3558. // Return the appropriate information
  3559. //
  3560. switch(ContextAttribute)
  3561. {
  3562. case SECPKG_ATTR_NATIVE_NAMES:
  3563. //
  3564. // Get outbound names from the ticket
  3565. //
  3566. if (Context->ContextAttributes & KERB_CONTEXT_OUTBOUND)
  3567. {
  3568. IsClientContext = TRUE;
  3569. if (Context->TicketCacheEntry != NULL)
  3570. {
  3571. KERBERR KerbErr = KDC_ERR_NONE;
  3572. KerbErr = KerbConvertKdcNameToString(
  3573. &ServerName,
  3574. Context->TicketCacheEntry->ServiceName,
  3575. &Context->TicketCacheEntry->DomainName
  3576. );
  3577. if (KERB_SUCCESS(KerbErr))
  3578. {
  3579. KerbErr = KerbConvertKdcNameToString(
  3580. &ClientName,
  3581. Context->TicketCacheEntry->ClientName,
  3582. &Context->TicketCacheEntry->ClientDomainName
  3583. );
  3584. }
  3585. if (!KERB_SUCCESS(KerbErr))
  3586. {
  3587. Status = STATUS_INSUFFICIENT_RESOURCES;
  3588. goto Cleanup;
  3589. }
  3590. }
  3591. else
  3592. {
  3593. //
  3594. // We couldn't find the names, so return an error
  3595. //
  3596. Status = STATUS_OBJECT_NAME_NOT_FOUND;
  3597. goto Cleanup;
  3598. }
  3599. }
  3600. else
  3601. {
  3602. //
  3603. // We have a server context
  3604. //
  3605. ClientName = Context->ClientPrincipalName;
  3606. ServerName = Context->ServerPrincipalName;
  3607. }
  3608. if (ServerName.Length != 0)
  3609. {
  3610. Status = LsaFunctions->AllocateClientBuffer(
  3611. NULL,
  3612. ServerName.Length + sizeof(WCHAR),
  3613. (PVOID *) &NameInfo.sServerName
  3614. );
  3615. if (!NT_SUCCESS(Status))
  3616. {
  3617. goto Cleanup;
  3618. }
  3619. Status = LsaFunctions->CopyToClientBuffer(
  3620. NULL,
  3621. ServerName.Length + sizeof(WCHAR),
  3622. NameInfo.sServerName,
  3623. ServerName.Buffer
  3624. );
  3625. if (!NT_SUCCESS(Status))
  3626. {
  3627. goto Cleanup;
  3628. }
  3629. }
  3630. if (ClientName.Length != 0)
  3631. {
  3632. Status = LsaFunctions->AllocateClientBuffer(
  3633. NULL,
  3634. ClientName.Length + sizeof(WCHAR),
  3635. (PVOID *) &NameInfo.sClientName
  3636. );
  3637. if (!NT_SUCCESS(Status))
  3638. {
  3639. goto Cleanup;
  3640. }
  3641. Status = LsaFunctions->CopyToClientBuffer(
  3642. NULL,
  3643. ClientName.Length + sizeof(WCHAR),
  3644. NameInfo.sClientName,
  3645. ClientName.Buffer
  3646. );
  3647. if (!NT_SUCCESS(Status))
  3648. {
  3649. goto Cleanup;
  3650. }
  3651. }
  3652. //
  3653. // Copy the whole structure
  3654. //
  3655. #if _WIN64
  3656. SECPKG_CALL_INFO CallInfo;
  3657. if (!LsaFunctions->GetCallInfo(&CallInfo))
  3658. {
  3659. Status = STATUS_INTERNAL_ERROR;
  3660. goto Cleanup;
  3661. }
  3662. if (CallInfo.Attributes & SECPKG_CALL_WOWCLIENT)
  3663. {
  3664. SecPkgContext_NativeNamesW_WOW64 NameInfoWOW64;
  3665. NameInfoWOW64.sServerName = PtrToUlong(NameInfo.sServerName);
  3666. NameInfoWOW64.sClientName = PtrToUlong(NameInfo.sClientName);
  3667. Status = LsaFunctions->CopyToClientBuffer(
  3668. NULL,
  3669. sizeof(SecPkgContext_NativeNamesW_WOW64),
  3670. Buffer,
  3671. &NameInfoWOW64
  3672. );
  3673. }
  3674. else
  3675. {
  3676. #endif // _WIN64
  3677. Status = LsaFunctions->CopyToClientBuffer(
  3678. NULL,
  3679. sizeof(SecPkgContext_NativeNames),
  3680. Buffer,
  3681. &NameInfo
  3682. );
  3683. #if _WIN64
  3684. }
  3685. #endif // _WIN64
  3686. if (!NT_SUCCESS(Status))
  3687. {
  3688. goto Cleanup;
  3689. }
  3690. break;
  3691. case SECPKG_ATTR_NEGOTIATION_INFO:
  3692. {
  3693. PSecPkgInfo PackageInfo = NULL;
  3694. SecPkgInfo TmpInfo = {0};
  3695. ULONG Offset = sizeof(SecPkgInfo);
  3696. PackageInfoSize = sizeof(SecPkgInfo) +
  3697. ROUND_UP_COUNT(sizeof(KERBEROS_PACKAGE_NAME), ALIGN_LPTSTR) +
  3698. ROUND_UP_COUNT(sizeof(KERBEROS_PACKAGE_COMMENT), ALIGN_LPTSTR);
  3699. Status = LsaFunctions->AllocateClientBuffer(
  3700. NULL,
  3701. PackageInfoSize,
  3702. (PVOID *) &PackageInfo
  3703. );
  3704. if (!NT_SUCCESS(Status))
  3705. {
  3706. goto Cleanup;
  3707. }
  3708. TmpInfo.Name = (SEC_WCHAR*) RtlOffsetToPointer(
  3709. PackageInfo,
  3710. Offset
  3711. );
  3712. Status = LsaFunctions->CopyToClientBuffer(
  3713. NULL,
  3714. sizeof(KERBEROS_PACKAGE_NAME),
  3715. TmpInfo.Name,
  3716. KERBEROS_PACKAGE_NAME
  3717. );
  3718. if (!NT_SUCCESS(Status))
  3719. {
  3720. goto Cleanup;
  3721. }
  3722. Offset += ROUND_UP_COUNT(sizeof(KERBEROS_PACKAGE_NAME), ALIGN_LPTSTR);
  3723. TmpInfo.Comment = (SEC_WCHAR*) RtlOffsetToPointer(
  3724. PackageInfo,
  3725. Offset
  3726. );
  3727. Status = LsaFunctions->CopyToClientBuffer(
  3728. NULL,
  3729. sizeof(KERBEROS_PACKAGE_COMMENT),
  3730. TmpInfo.Comment,
  3731. KERBEROS_PACKAGE_COMMENT
  3732. );
  3733. if (!NT_SUCCESS(Status))
  3734. {
  3735. goto Cleanup;
  3736. }
  3737. TmpInfo.wVersion = SECURITY_SUPPORT_PROVIDER_INTERFACE_VERSION;
  3738. TmpInfo.wRPCID = RPC_C_AUTHN_GSS_KERBEROS;
  3739. TmpInfo.fCapabilities = KERBEROS_CAPABILITIES;
  3740. TmpInfo.cbMaxToken = KerbGlobalMaxTokenSize;
  3741. Status = LsaFunctions->CopyToClientBuffer(
  3742. NULL,
  3743. sizeof(SecPkgInfo),
  3744. PackageInfo,
  3745. &TmpInfo
  3746. );
  3747. if (!NT_SUCCESS(Status))
  3748. {
  3749. goto Cleanup;
  3750. }
  3751. NegInfo.PackageInfo = PackageInfo;
  3752. NegInfo.NegotiationState = Context->NegotiationInfo;
  3753. Status = LsaFunctions->CopyToClientBuffer(
  3754. NULL,
  3755. sizeof(SecPkgContext_NegotiationInfo),
  3756. Buffer,
  3757. &NegInfo
  3758. );
  3759. if (!NT_SUCCESS(Status))
  3760. {
  3761. goto Cleanup;
  3762. }
  3763. }
  3764. break;
  3765. default:
  3766. Status = STATUS_NOT_SUPPORTED;
  3767. break;
  3768. }
  3769. Cleanup:
  3770. if (TicketCacheLocked)
  3771. {
  3772. KerbUnlockTicketCache();
  3773. }
  3774. if (ContextsLocked)
  3775. {
  3776. KerbUnlockContexts();
  3777. }
  3778. if (Context != NULL)
  3779. {
  3780. KerbDereferenceContext(Context);
  3781. }
  3782. if (IsClientContext)
  3783. {
  3784. KerbFreeString(
  3785. &ClientName
  3786. );
  3787. KerbFreeString(
  3788. &ServerName
  3789. );
  3790. }
  3791. if (!NT_SUCCESS(Status))
  3792. {
  3793. if (NameInfo.sServerName != NULL)
  3794. {
  3795. LsaFunctions->FreeClientBuffer(
  3796. NULL,
  3797. NameInfo.sServerName
  3798. );
  3799. }
  3800. if (NameInfo.sClientName != NULL)
  3801. {
  3802. LsaFunctions->FreeClientBuffer(
  3803. NULL,
  3804. NameInfo.sClientName
  3805. );
  3806. }
  3807. }
  3808. D_DebugLog((DEB_TRACE_API,"SpQueryLsaModeContextAttributes returned 0x%x, pid:0x%x, ctxt:0x%x, Attr:0x%x\n", KerbMapKerbNtStatusToNtStatus(Status), GetCurrentProcessId(), ContextHandle, ContextAttribute));
  3809. return(KerbMapKerbNtStatusToNtStatus(Status));
  3810. }
  3811. //+-------------------------------------------------------------------------
  3812. //
  3813. // Function: SpCompleteAuthToken
  3814. //
  3815. // Synopsis: Completes a context (in Kerberos case, does nothing)
  3816. //
  3817. // Effects:
  3818. //
  3819. // Arguments:
  3820. //
  3821. // Requires:
  3822. //
  3823. // Returns:
  3824. //
  3825. // Notes:
  3826. //
  3827. //
  3828. //--------------------------------------------------------------------------
  3829. NTSTATUS NTAPI
  3830. SpCompleteAuthToken(
  3831. IN LSA_SEC_HANDLE ContextHandle,
  3832. IN PSecBufferDesc InputBuffer
  3833. )
  3834. {
  3835. return(STATUS_SUCCESS);
  3836. }
  3837. #ifndef WIN32_CHICAGO
  3838. NTSTATUS NTAPI
  3839. SpFormatCredentials(
  3840. IN PSecBuffer Credentials,
  3841. OUT PSecBuffer FormattedCredentials
  3842. )
  3843. {
  3844. return(STATUS_NOT_SUPPORTED);
  3845. }
  3846. NTSTATUS NTAPI
  3847. SpMarshallSupplementalCreds(
  3848. IN ULONG CredentialSize,
  3849. IN PUCHAR Credentials,
  3850. OUT PULONG MarshalledCredSize,
  3851. OUT PVOID * MarshalledCreds
  3852. )
  3853. {
  3854. return(STATUS_NOT_SUPPORTED);
  3855. }
  3856. #endif // WIN32_CHICAGO