[SCM] Samba Shared Repository - branch master updated

Günther Deschner gd at samba.org
Mon May 31 07:28:46 MDT 2010


The branch, master has been updated
       via  62708fb... s3:ntlmssp Move ntlmssp_sign.c from source3 to common code.
       via  d6fa371... s3:ntlmssp Use a TALLOC_CTX for ntlmssp_sign_packet() and ntlmssp_seal_packet()
       via  ebae21f... ntlmssp: Make the ntlmssp.h from source3/ a common header
       via  9a76932... s3:ntlmssp Add two unused variables to match the Samba4 ntlmssp.h
       via  723ea68... s3:auth Remove AUTH_NTLMSSP_STATE typedef.
       via  3b70686... s3:auth Make AUTH_NTLMSSP_STATE a private structure.
      from  a950c6c... s3:configre: work around shell problem on HP-UX in AC macro

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 62708fbd1bf6a54d880011ce7d95ee43e08cf30e
Author: Andrew Bartlett <abartlet at samba.org>
Date:   Tue May 25 20:58:52 2010 +1000

    s3:ntlmssp Move ntlmssp_sign.c from source3 to common code.
    
    This needs a small re-arrangement of the supporting code.
    
    Andrew Bartlett
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Signed-off-by: Günther Deschner <gd at samba.org>

commit d6fa371b92681a327a86239721fc5990d91ad74f
Author: Andrew Bartlett <abartlet at samba.org>
Date:   Tue May 25 20:55:40 2010 +1000

    s3:ntlmssp Use a TALLOC_CTX for ntlmssp_sign_packet() and ntlmssp_seal_packet()
    
    This ensures the results can't be easily left to leak.
    
    Andrew Bartlett
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Signed-off-by: Günther Deschner <gd at samba.org>

commit ebae21f0235b957c8faeeb51c926724909d353e9
Author: Andrew Bartlett <abartlet at samba.org>
Date:   Tue May 25 20:19:22 2010 +1000

    ntlmssp: Make the ntlmssp.h from source3/ a common header
    
    The code is not yet in common, but I hope to fix that soon.
    
    Andrew Bartlett
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Signed-off-by: Günther Deschner <gd at samba.org>

commit 9a76932ccfe680b9bf224e0dcac7f9cdb7ce9f4e
Author: Andrew Bartlett <abartlet at samba.org>
Date:   Tue May 25 19:58:00 2010 +1000

    s3:ntlmssp Add two unused variables to match the Samba4 ntlmssp.h
    
    Andrew Bartlett
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Signed-off-by: Günther Deschner <gd at samba.org>

commit 723ea68d3b69a9fc31dc02256c4fc323dc9278ce
Author: Andrew Bartlett <abartlet at samba.org>
Date:   Tue May 25 15:40:35 2010 +1000

    s3:auth Remove AUTH_NTLMSSP_STATE typedef.
    
    typedefs are no longer preferred Samba style.
    
    Andrew Bartlett
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Signed-off-by: Günther Deschner <gd at samba.org>

commit 3b706865f6bae7a2b04590da160bda939a3bafe5
Author: Andrew Bartlett <abartlet at samba.org>
Date:   Tue May 25 15:34:06 2010 +1000

    s3:auth Make AUTH_NTLMSSP_STATE a private structure.
    
    This makes it a little easier for it to writen in terms of GENSEC in future.
    
    Andrew Bartlett
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>
    Signed-off-by: Günther Deschner <gd at samba.org>

-----------------------------------------------------------------------

Summary of changes:
 libcli/auth/ntlmssp.c                     |   76 ++++
 libcli/auth/ntlmssp.h                     |  161 ++++++++
 libcli/auth/ntlmssp_private.h             |   25 ++
 libcli/auth/ntlmssp_sign.c                |  573 +++++++++++++++++++++++++++++
 source3/Makefile.in                       |    3 +-
 source3/auth/auth_ntlmssp.c               |  115 +++++-
 source3/include/auth.h                    |    7 +-
 source3/include/ntdomain.h                |    2 +-
 source3/include/ntlmssp.h                 |  135 -------
 source3/include/proto.h                   |   53 ++--
 source3/libads/sasl.c                     |    8 +-
 source3/libsmb/cliconnect.c               |    2 +-
 source3/libsmb/clifsinfo.c                |    2 +-
 source3/libsmb/ntlmssp.c                  |   54 +---
 source3/libsmb/ntlmssp_sign.c             |  550 ---------------------------
 source3/libsmb/smb_seal.c                 |   10 +-
 source3/rpc_client/cli_pipe.c             |   17 +-
 source3/rpc_server/srv_pipe.c             |   81 +++--
 source3/smbd/globals.h                    |    2 +-
 source3/smbd/seal.c                       |   13 +-
 source3/smbd/sesssetup.c                  |   35 +-
 source3/smbd/smb2_sesssetup.c             |   32 +-
 source3/utils/ntlm_auth.c                 |    2 +-
 source3/winbindd/winbindd_ccache_access.c |    2 +-
 24 files changed, 1077 insertions(+), 883 deletions(-)
 create mode 100644 libcli/auth/ntlmssp.c
 create mode 100644 libcli/auth/ntlmssp.h
 create mode 100644 libcli/auth/ntlmssp_private.h
 create mode 100644 libcli/auth/ntlmssp_sign.c
 delete mode 100644 source3/include/ntlmssp.h
 delete mode 100644 source3/libsmb/ntlmssp_sign.c


Changeset truncated at 500 lines:

diff --git a/libcli/auth/ntlmssp.c b/libcli/auth/ntlmssp.c
new file mode 100644
index 0000000..1be764e
--- /dev/null
+++ b/libcli/auth/ntlmssp.c
@@ -0,0 +1,76 @@
+/*
+   Unix SMB/Netbios implementation.
+   Version 3.0
+   handle NLTMSSP, server side
+
+   Copyright (C) Andrew Tridgell      2001
+   Copyright (C) Andrew Bartlett 2001-2003
+   Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "../libcli/auth/ntlmssp.h"
+#include "../libcli/auth/ntlmssp_private.h"
+
+/**
+ * Print out the NTLMSSP flags for debugging
+ * @param neg_flags The flags from the packet
+ */
+void debug_ntlmssp_flags(uint32_t neg_flags)
+{
+	DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
+
+	if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_UNICODE\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM\n"));
+	if (neg_flags & NTLMSSP_REQUEST_TARGET)
+		DEBUGADD(4, ("  NTLMSSP_REQUEST_TARGET\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SIGN\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SEAL\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_DATAGRAM\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_LM_KEY\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NETWARE\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
+	if (neg_flags & NTLMSSP_REQUEST_NON_NT_SESSION_KEY)
+		DEBUGADD(4, ("  NTLMSSP_REQUEST_NON_NT_SESSION_KEY\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM2\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_TARGET_INFO\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_VERSION)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_VERSION\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_128)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_128\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
+	if (neg_flags & NTLMSSP_NEGOTIATE_56)
+		DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_56\n"));
+}
diff --git a/libcli/auth/ntlmssp.h b/libcli/auth/ntlmssp.h
new file mode 100644
index 0000000..d0a282c
--- /dev/null
+++ b/libcli/auth/ntlmssp.h
@@ -0,0 +1,161 @@
+/*
+   Unix SMB/CIFS implementation.
+   SMB parameters and setup
+   Copyright (C) Andrew Tridgell 1992-1997
+   Copyright (C) Luke Kenneth Casson Leighton 1996-1997
+   Copyright (C) Paul Ashton 1997
+   Copyright (C) Andrew Bartlett 2010
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "../librpc/gen_ndr/ntlmssp.h"
+
+/* NTLMSSP mode */
+enum ntlmssp_role
+{
+	NTLMSSP_SERVER,
+	NTLMSSP_CLIENT
+};
+
+/* NTLMSSP message types */
+enum ntlmssp_message_type
+{
+	NTLMSSP_INITIAL = 0 /* samba internal state */,
+	NTLMSSP_NEGOTIATE = 1,
+	NTLMSSP_CHALLENGE = 2,
+	NTLMSSP_AUTH      = 3,
+	NTLMSSP_UNKNOWN   = 4,
+	NTLMSSP_DONE      = 5 /* samba final state */
+};
+
+#define NTLMSSP_FEATURE_SESSION_KEY        0x00000001
+#define NTLMSSP_FEATURE_SIGN               0x00000002
+#define NTLMSSP_FEATURE_SEAL               0x00000004
+#define NTLMSSP_FEATURE_CCACHE		   0x00000008
+
+union ntlmssp_crypt_state;
+
+struct ntlmssp_state
+{
+	enum ntlmssp_role role;
+	uint32_t expected_state;
+
+	bool unicode;
+	bool use_ntlmv2;
+	bool use_ccache;
+	bool use_nt_response;  /* Set to 'False' to debug what happens when the NT response is omited */
+	bool allow_lm_key;     /* The LM_KEY code is not very secure... */
+
+	const char *user;
+	const char *domain;
+	uint8_t *nt_hash;
+	uint8_t *lm_hash;
+
+	struct {
+		const char *netbios_name;
+		const char *netbios_domain;
+	} client;
+
+	struct {
+		bool is_standalone;
+		const char *netbios_name;
+		const char *netbios_domain;
+		const char *dns_name;
+		const char *dns_domain;
+	} server;
+
+	DATA_BLOB internal_chal; /* Random challenge as supplied to the client for NTLM authentication */
+
+	DATA_BLOB chal; /* Random challenge as input into the actual NTLM (or NTLM2) authentication */
+	DATA_BLOB lm_resp;
+	DATA_BLOB nt_resp;
+	DATA_BLOB session_key;
+
+	uint32_t neg_flags; /* the current state of negotiation with the NTLMSSP partner */
+
+	/**
+	 * Private data for the callback functions
+	 */
+	void *callback_private;
+
+	/**
+	 * Callback to get the 'challenge' used for NTLM authentication.
+	 *
+	 * @param ntlmssp_state This structure
+	 * @return 8 bytes of challenge data, determined by the server to be the challenge for NTLM authentication
+	 *
+	 */
+	NTSTATUS (*get_challenge)(const struct ntlmssp_state *ntlmssp_state,
+				  uint8_t challenge[8]);
+
+	/**
+	 * Callback to find if the challenge used by NTLM authentication may be modified
+	 *
+	 * The NTLM2 authentication scheme modifies the effective challenge, but this is not compatiable with the
+	 * current 'security=server' implementation..
+	 *
+	 * @param ntlmssp_state This structure
+	 * @return Can the challenge be set to arbitary values?
+	 *
+	 */
+	bool (*may_set_challenge)(const struct ntlmssp_state *ntlmssp_state);
+
+	/**
+	 * Callback to set the 'challenge' used for NTLM authentication.
+	 *
+	 * The callback may use the void *auth_context to store state information, but the same value is always available
+	 * from the DATA_BLOB chal on this structure.
+	 *
+	 * @param ntlmssp_state This structure
+	 * @param challenge 8 bytes of data, agreed by the client and server to be the effective challenge for NTLM2 authentication
+	 *
+	 */
+	NTSTATUS (*set_challenge)(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge);
+
+	/**
+	 * Callback to check the user's password.
+	 *
+	 * The callback must reads the feilds of this structure for the information it needs on the user
+	 * @param ntlmssp_state This structure
+	 * @param nt_session_key If an NT session key is returned by the authentication process, return it here
+	 * @param lm_session_key If an LM session key is returned by the authentication process, return it here
+	 *
+	 */
+	NTSTATUS (*check_password)(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *nt_session_key, DATA_BLOB *lm_session_key);
+
+	union ntlmssp_crypt_state *crypt;
+};
+
+/* The following definitions come from libcli/auth/ntlmssp_sign.c  */
+
+NTSTATUS ntlmssp_sign_packet(struct ntlmssp_state *ntlmssp_state,
+			     TALLOC_CTX *sig_mem_ctx,
+			     const uint8_t *data, size_t length,
+			     const uint8_t *whole_pdu, size_t pdu_length,
+			     DATA_BLOB *sig);
+NTSTATUS ntlmssp_check_packet(struct ntlmssp_state *ntlmssp_state,
+			      const uint8_t *data, size_t length,
+			      const uint8_t *whole_pdu, size_t pdu_length,
+			      const DATA_BLOB *sig) ;
+NTSTATUS ntlmssp_seal_packet(struct ntlmssp_state *ntlmssp_state,
+			     TALLOC_CTX *sig_mem_ctx,
+			     uint8_t *data, size_t length,
+			     const uint8_t *whole_pdu, size_t pdu_length,
+			     DATA_BLOB *sig);
+NTSTATUS ntlmssp_unseal_packet(struct ntlmssp_state *ntlmssp_state,
+			       uint8_t *data, size_t length,
+			       const uint8_t *whole_pdu, size_t pdu_length,
+			       const DATA_BLOB *sig);
+NTSTATUS ntlmssp_sign_init(struct ntlmssp_state *ntlmssp_state);
diff --git a/libcli/auth/ntlmssp_private.h b/libcli/auth/ntlmssp_private.h
new file mode 100644
index 0000000..0f0c7db
--- /dev/null
+++ b/libcli/auth/ntlmssp_private.h
@@ -0,0 +1,25 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *  Version 3.0
+ *  NTLMSSP Signing routines
+ *  Copyright (C) Andrew Bartlett 2003-2005
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* For structures internal to the NTLMSSP implementation that should not be exposed */
+
+/* The following definitions come from libcli/auth/ntlmssp.c  */
+
+void debug_ntlmssp_flags(uint32_t neg_flags);
diff --git a/libcli/auth/ntlmssp_sign.c b/libcli/auth/ntlmssp_sign.c
new file mode 100644
index 0000000..61b52c6
--- /dev/null
+++ b/libcli/auth/ntlmssp_sign.c
@@ -0,0 +1,573 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *  Version 3.0
+ *  NTLMSSP Signing routines
+ *  Copyright (C) Andrew Bartlett 2003-2005
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "includes.h"
+#include "../libcli/auth/ntlmssp.h"
+#include "../libcli/auth/libcli_auth.h"
+#include "../lib/crypto/md5.h"
+#include "../lib/crypto/arcfour.h"
+#include "../lib/crypto/hmacmd5.h"
+#include "../lib/crypto/crc32.h"
+#include "../libcli/auth/ntlmssp_private.h"
+
+#define CLI_SIGN "session key to client-to-server signing key magic constant"
+#define CLI_SEAL "session key to client-to-server sealing key magic constant"
+#define SRV_SIGN "session key to server-to-client signing key magic constant"
+#define SRV_SEAL "session key to server-to-client sealing key magic constant"
+
+/**
+ * Some notes on the NTLM2 code:
+ *
+ * NTLM2 is a AEAD system.  This means that the data encrypted is not
+ * all the data that is signed.  In DCE-RPC case, the headers of the
+ * DCE-RPC packets are also signed.  This prevents some of the
+ * fun-and-games one might have by changing them.
+ *
+ */
+
+static void dump_arc4_state(const char *description,
+			    struct arcfour_state *state)
+{
+	dump_data_pw(description, state->sbox, sizeof(state->sbox));
+}
+
+static void calc_ntlmv2_key(uint8_t subkey[16],
+			    DATA_BLOB session_key,
+			    const char *constant)
+{
+	struct MD5Context ctx3;
+	MD5Init(&ctx3);
+	MD5Update(&ctx3, session_key.data, session_key.length);
+	MD5Update(&ctx3, (const uint8_t *)constant, strlen(constant)+1);
+	MD5Final(subkey, &ctx3);
+}
+
+enum ntlmssp_direction {
+	NTLMSSP_SEND,
+	NTLMSSP_RECEIVE
+};
+
+struct ntlmssp_crypt_direction {
+	uint32_t seq_num;
+	uint8_t sign_key[16];
+	struct arcfour_state seal_state;
+};
+
+union ntlmssp_crypt_state {
+	/* NTLM */
+	struct ntlmssp_crypt_direction ntlm;
+
+	/* NTLM2 */
+	struct {
+		struct ntlmssp_crypt_direction sending;
+		struct ntlmssp_crypt_direction receiving;
+	} ntlm2;
+};
+
+static NTSTATUS ntlmssp_make_packet_signature(struct ntlmssp_state *ntlmssp_state,
+					      TALLOC_CTX *sig_mem_ctx,
+					      const uint8_t *data, size_t length,
+					      const uint8_t *whole_pdu, size_t pdu_length,
+					      enum ntlmssp_direction direction,
+					      DATA_BLOB *sig, bool encrypt_sig)
+{
+	if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
+		HMACMD5Context ctx;
+		uint8_t digest[16];
+		uint8_t seq_num[4];
+
+		*sig = data_blob_talloc(sig_mem_ctx, NULL, NTLMSSP_SIG_SIZE);
+		if (!sig->data) {
+			return NT_STATUS_NO_MEMORY;
+		}
+
+		switch (direction) {
+		case NTLMSSP_SEND:
+			DEBUG(100,("ntlmssp_make_packet_signature: SEND seq = %u, len = %u, pdu_len = %u\n",
+				ntlmssp_state->crypt->ntlm2.sending.seq_num,
+				(unsigned int)length,
+				(unsigned int)pdu_length));
+
+			SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.sending.seq_num);
+			ntlmssp_state->crypt->ntlm2.sending.seq_num++;
+			hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.sending.sign_key, 16, &ctx);
+			break;
+		case NTLMSSP_RECEIVE:
+
+			DEBUG(100,("ntlmssp_make_packet_signature: RECV seq = %u, len = %u, pdu_len = %u\n",
+				ntlmssp_state->crypt->ntlm2.receiving.seq_num,
+				(unsigned int)length,
+				(unsigned int)pdu_length));
+
+			SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.receiving.seq_num);
+			ntlmssp_state->crypt->ntlm2.receiving.seq_num++;
+			hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16, &ctx);
+			break;
+		}
+
+		dump_data_pw("pdu data ", whole_pdu, pdu_length);
+
+		hmac_md5_update(seq_num, sizeof(seq_num), &ctx);
+		hmac_md5_update(whole_pdu, pdu_length, &ctx);
+		hmac_md5_final(digest, &ctx);
+
+		if (encrypt_sig && (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
+			switch (direction) {
+			case NTLMSSP_SEND:
+				arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
+						   digest, 8);
+				break;
+			case NTLMSSP_RECEIVE:
+				arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
+						   digest, 8);
+				break;
+			}
+		}
+
+		SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
+		memcpy(sig->data + 4, digest, 8);
+		memcpy(sig->data + 12, seq_num, 4);
+
+		dump_data_pw("ntlmssp v2 sig ", sig->data, sig->length);
+
+	} else {
+		bool ok;
+		uint32_t crc;
+
+		crc = crc32_calc_buffer(data, length);
+
+		ok = msrpc_gen(sig_mem_ctx,
+			       sig, "dddd",
+			       NTLMSSP_SIGN_VERSION, 0, crc,
+			       ntlmssp_state->crypt->ntlm.seq_num);
+		if (!ok) {
+			return NT_STATUS_NO_MEMORY;
+		}
+
+		ntlmssp_state->crypt->ntlm.seq_num++;
+
+		dump_arc4_state("ntlmssp hash: \n",
+				&ntlmssp_state->crypt->ntlm.seal_state);
+		arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
+				   sig->data+4, sig->length-4);
+	}
+	return NT_STATUS_OK;
+}
+
+NTSTATUS ntlmssp_sign_packet(struct ntlmssp_state *ntlmssp_state,
+			     TALLOC_CTX *sig_mem_ctx,
+			     const uint8_t *data, size_t length,
+			     const uint8_t *whole_pdu, size_t pdu_length,
+			     DATA_BLOB *sig)
+{
+	NTSTATUS nt_status;
+
+	if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
+		DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
+		return NT_STATUS_INVALID_PARAMETER;
+	}
+
+	if (!ntlmssp_state->session_key.length) {
+		DEBUG(3, ("NO session key, cannot check sign packet\n"));
+		return NT_STATUS_NO_USER_SESSION_KEY;
+	}
+
+	nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
+						  sig_mem_ctx,
+						  data, length,
+						  whole_pdu, pdu_length,
+						  NTLMSSP_SEND, sig, true);
+
+	return nt_status;
+}
+
+/**
+ * Check the signature of an incoming packet
+ * @note caller *must* check that the signature is the size it expects
+ *
+ */
+
+NTSTATUS ntlmssp_check_packet(struct ntlmssp_state *ntlmssp_state,
+			      const uint8_t *data, size_t length,
+			      const uint8_t *whole_pdu, size_t pdu_length,
+			      const DATA_BLOB *sig)
+{
+	DATA_BLOB local_sig;
+	NTSTATUS nt_status;
+	TALLOC_CTX *tmp_ctx;


-- 
Samba Shared Repository


More information about the samba-cvs mailing list