RIP BOOL. Convert BOOL -> bool. I found a few interesting
[kai/samba.git] / source3 / libsmb / ntlmssp.c
index 6b551e8774c4bd7df761cfea90810f9b1517548f..7205d57a0a24b93fb5360b0e1df50ed2e7d3224d 100644 (file)
@@ -9,7 +9,7 @@
 
    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 2 of the License, or
+   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,
@@ -18,8 +18,7 @@
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -72,6 +71,8 @@ void debug_ntlmssp_flags(uint32 neg_flags)
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SIGN\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_SEAL) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SEAL\n"));
+       if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM_STYLE)
+               DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_DATAGRAM_STYLE\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_LM_KEY\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE) 
@@ -86,6 +87,10 @@ void debug_ntlmssp_flags(uint32 neg_flags)
                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_CHAL_ACCEPT_RESPONSE)
+               DEBUGADD(4, ("  NTLMSSP_CHAL_ACCEPT_RESPONSE\n"));
+       if (neg_flags & NTLMSSP_CHAL_NON_NT_SESSION_KEY)
+               DEBUGADD(4, ("  NTLMSSP_CHAL_NON_NT_SESSION_KEY\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM2\n"));
        if (neg_flags & NTLMSSP_CHAL_TARGET_INFO) 
@@ -94,6 +99,8 @@ void debug_ntlmssp_flags(uint32 neg_flags)
                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"));
 }
 
 /**
@@ -114,7 +121,7 @@ static const uint8 *get_challenge(const struct ntlmssp_state *ntlmssp_state)
  *
  */
    
-static BOOL may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
+static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
 {
        return True;
 }
@@ -147,18 +154,41 @@ NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user)
 }
 
 /** 
- * Set a password on an NTLMSSP context - ensures it is talloc()ed 
+ * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed 
+ *
+ */
+NTSTATUS ntlmssp_set_hashes(NTLMSSP_STATE *ntlmssp_state,
+               const unsigned char lm_hash[16],
+               const unsigned char nt_hash[16]) 
+{
+       ntlmssp_state->lm_hash = (unsigned char *)
+               TALLOC_MEMDUP(ntlmssp_state->mem_ctx, lm_hash, 16);
+       ntlmssp_state->nt_hash = (unsigned char *)
+               TALLOC_MEMDUP(ntlmssp_state->mem_ctx, nt_hash, 16);
+       if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
+               TALLOC_FREE(ntlmssp_state->lm_hash);
+               TALLOC_FREE(ntlmssp_state->nt_hash);
+               return NT_STATUS_NO_MEMORY;
+       }
+       return NT_STATUS_OK;
+}
+
+/** 
+ * Converts a password to the hashes on an NTLMSSP context.
  *
  */
 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password) 
 {
        if (!password) {
-               ntlmssp_state->password = NULL;
+               ntlmssp_state->lm_hash = NULL;
+               ntlmssp_state->nt_hash = NULL;
        } else {
-               ntlmssp_state->password = talloc_strdup(ntlmssp_state->mem_ctx, password);
-               if (!ntlmssp_state->password) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               unsigned char lm_hash[16];
+               unsigned char nt_hash[16];
+
+               E_deshash(password, lm_hash);
+               E_md4hash(password, nt_hash);
+               return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
        }
        return NT_STATUS_OK;
 }
@@ -202,6 +232,50 @@ NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
        return NT_STATUS_OK;
 }
 
+/**
+ * Request features for the NTLMSSP negotiation
+ *
+ * @param ntlmssp_state NTLMSSP state
+ * @param feature_list List of space seperated features requested from NTLMSSP.
+ */
+void ntlmssp_want_feature_list(NTLMSSP_STATE *ntlmssp_state, char *feature_list)
+{
+       /*
+        * We need to set this to allow a later SetPassword
+        * via the SAMR pipe to succeed. Strange.... We could
+        * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
+        */
+       if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
+       }
+}
+
+/**
+ * Request a feature for the NTLMSSP negotiation
+ *
+ * @param ntlmssp_state NTLMSSP state
+ * @param feature Bit flag specifying the requested feature
+ */
+void ntlmssp_want_feature(NTLMSSP_STATE *ntlmssp_state, uint32 feature)
+{
+       /* As per JRA's comment above */
+       if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if (feature & NTLMSSP_FEATURE_SIGN) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if (feature & NTLMSSP_FEATURE_SEAL) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
+       }
+}
 /**
  * Next state function for the NTLMSSP state machine
  * 
@@ -224,13 +298,13 @@ NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       *out = data_blob(NULL, 0);
+       *out = data_blob_null;
 
        if (!in.length && ntlmssp_state->stored_response.length) {
                input = ntlmssp_state->stored_response;
                
                /* we only want to read the stored response once - overwrite it */
-               ntlmssp_state->stored_response = data_blob(NULL, 0);
+               ntlmssp_state->stored_response = data_blob_null;
        } else {
                input = in;
        }
@@ -250,7 +324,7 @@ NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
                                 "NTLMSSP",
                                 &ntlmssp_command)) {
                        DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
-                       dump_data(2, (const char *)input.data, input.length);
+                       dump_data(2, input.data, input.length);
                        return NT_STATUS_INVALID_PARAMETER;
                }
        }
@@ -326,7 +400,7 @@ static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
 }
 
 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
-                                     uint32 neg_flags, BOOL allow_lm) {
+                                     uint32 neg_flags, bool allow_lm) {
        if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
                ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
@@ -345,8 +419,8 @@ static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
        }
 
-       if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) {
-               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
        }
 
        if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
@@ -355,9 +429,6 @@ static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
 
        if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
-               if (neg_flags & NTLMSSP_NEGOTIATE_56) {
-                       ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
-               }
        }
 
        if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
@@ -368,10 +439,23 @@ static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
        }
 
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
+       }
+
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
+       }
+
+       /* Woop Woop - unknown flag for Windows compatibility...
+          What does this really do ? JRA. */
+       if (!(neg_flags & NTLMSSP_UNKNOWN_02000000)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_UNKNOWN_02000000;
+       }
+
        if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
                ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
        }
-       
 }
 
 /**
@@ -382,25 +466,37 @@ static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
  by the client lanman auth/lanman auth parameters, it isn't too bad.
 */
 
-void ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state)
+DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
 {
+       DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
+                                       ntlmssp_state->session_key.data,
+                                       ntlmssp_state->session_key.length);
+
+       /* Nothing to weaken.  We certainly don't want to 'extend' the length... */
+       if (weakened_key.length < 16) {
+               /* perhaps there was no key? */
+               return weakened_key;
+       }
+
        /* Key weakening not performed on the master key for NTLM2
           and does not occour for NTLM1.  Therefore we only need
           to do this for the LM_KEY.
        */
 
        if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
-               if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
-                       ;
-               } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
-                       ntlmssp_state->session_key.data[7] = 0xa0;
+               /* LM key doesn't support 128 bit crypto, so this is
+                * the best we can do.  If you negotiate 128 bit, but
+                * not 56, you end up with 40 bit... */
+               if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
+                       weakened_key.data[7] = 0xa0;
                } else { /* forty bits */
-                       ntlmssp_state->session_key.data[5] = 0xe5;
-                       ntlmssp_state->session_key.data[6] = 0x38;
-                       ntlmssp_state->session_key.data[7] = 0xb0;
+                       weakened_key.data[5] = 0xe5;
+                       weakened_key.data[6] = 0x38;
+                       weakened_key.data[7] = 0xb0;
                }
-               ntlmssp_state->session_key.length = 8;
+               weakened_key.length = 8;
        }
+       return weakened_key;
 }
 
 /**
@@ -419,7 +515,6 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
        fstring dnsname, dnsdomname;
        uint32 neg_flags = 0;
        uint32 ntlmssp_command, chal_flags;
-       char *cliname=NULL, *domname=NULL;
        const uint8 *cryptkey;
        const char *target_name;
 
@@ -429,23 +524,15 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
 #endif
 
        if (request.length) {
-               if (!msrpc_parse(&request, "CddAA",
-                                "NTLMSSP",
-                                &ntlmssp_command,
-                                &neg_flags,
-                                &cliname,
-                                &domname)) {
-                       DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate:\n"));
-                       dump_data(2, (const char *)request.data, request.length);
+               if ((request.length < 16) || !msrpc_parse(&request, "Cdd",
+                                                       "NTLMSSP",
+                                                       &ntlmssp_command,
+                                                       &neg_flags)) {
+                       DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
+                               (unsigned int)request.length));
+                       dump_data(2, request.data, request.length);
                        return NT_STATUS_INVALID_PARAMETER;
                }
-               
-               DEBUG(10, ("ntlmssp_server_negotiate: client = %s, domain = %s\n",
-                               cliname ? cliname : "", domname ? domname : ""));
-
-               SAFE_FREE(cliname);
-               SAFE_FREE(domname);
-               
                debug_ntlmssp_flags(neg_flags);
        }
        
@@ -494,7 +581,7 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
                          NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
                          0, "");
        } else {
-               struct_blob = data_blob(NULL, 0);
+               struct_blob = data_blob_null;
        }
 
        {
@@ -535,15 +622,15 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                                    const DATA_BLOB request, DATA_BLOB *reply) 
 {
-       DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
-       DATA_BLOB user_session_key = data_blob(NULL, 0);
-       DATA_BLOB lm_session_key = data_blob(NULL, 0);
-       DATA_BLOB session_key = data_blob(NULL, 0);
+       DATA_BLOB encrypted_session_key = data_blob_null;
+       DATA_BLOB user_session_key = data_blob_null;
+       DATA_BLOB lm_session_key = data_blob_null;
+       DATA_BLOB session_key = data_blob_null;
        uint32 ntlmssp_command, auth_flags;
        NTSTATUS nt_status = NT_STATUS_OK;
 
        /* used by NTLM2 */
-       BOOL doing_ntlm2 = False;
+       bool doing_ntlm2 = False;
 
        uchar session_nonce[16];
        uchar session_nonce_hash[16];
@@ -554,7 +641,7 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
        char *workstation = NULL;
 
        /* parse the NTLMSSP packet */
-       *reply = data_blob(NULL, 0);
+       *reply = data_blob_null;
 
 #if 0
        file_save("ntlmssp_auth.dat", request.data, request.length);
@@ -607,7 +694,7 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                                 &user, 
                                 &workstation)) {
                        DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
-                       dump_data(2, (const char *)request.data, request.length);
+                       dump_data(2, request.data, request.length);
                        SAFE_FREE(domain);
                        SAFE_FREE(user);
                        SAFE_FREE(workstation);
@@ -719,25 +806,34 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                        
                } else {
                        DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
-                       session_key = data_blob(NULL, 0);
+                       session_key = data_blob_null;
                }
        } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
                if (lm_session_key.data && lm_session_key.length >= 8) {
                        if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
                                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
+                               if (session_key.data == NULL) {
+                                       return NT_STATUS_NO_MEMORY;
+                               }
                                SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data, 
                                                          session_key.data);
                                DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
-                               dump_data_pw("LM session key:\n", session_key.data, session_key.length);
                        } else {
-                               /* use the key unmodified - it's
-                                * probably a NULL key from the guest
-                                * login */
-                               session_key = lm_session_key;
+                               static const uint8 zeros[24] = { 0, };
+                               session_key = data_blob_talloc(
+                                       ntlmssp_state->mem_ctx, NULL, 16);
+                               if (session_key.data == NULL) {
+                                       return NT_STATUS_NO_MEMORY;
+                               }
+                               SMBsesskeygen_lm_sess_key(
+                                       lm_session_key.data, zeros,
+                                       session_key.data);
                        }
+                       dump_data_pw("LM session key:\n", session_key.data,
+                                    session_key.length);
                } else {
                        DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
-                       session_key = data_blob(NULL, 0);
+                       session_key = data_blob_null;
                }
        } else if (user_session_key.data) {
                session_key = user_session_key;
@@ -749,7 +845,7 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
        } else {
                DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
-               session_key = data_blob(NULL, 0);
+               session_key = data_blob_null;
        }
 
        /* With KEY_EXCH, the client supplies the proposed session key, 
@@ -779,11 +875,8 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                ntlmssp_state->session_key = session_key;
        }
 
-       /* The client might need us to use a partial-strength session key */
-       ntlmssp_weaken_keys(ntlmssp_state);
-
        if (!NT_STATUS_IS_OK(nt_status)) {
-               ntlmssp_state->session_key = data_blob(NULL, 0);
+               ntlmssp_state->session_key = data_blob_null;
        } else if (ntlmssp_state->session_key.length) {
                nt_status = ntlmssp_sign_init(ntlmssp_state);
        }
@@ -832,6 +925,9 @@ NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
 
        (*ntlmssp_state)->neg_flags = 
                NTLMSSP_NEGOTIATE_128 |
+               NTLMSSP_NEGOTIATE_56 |
+               NTLMSSP_UNKNOWN_02000000 |
+               NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
                NTLMSSP_NEGOTIATE_NTLM |
                NTLMSSP_NEGOTIATE_NTLM2 |
                NTLMSSP_NEGOTIATE_KEY_EXCH |
@@ -895,14 +991,14 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
        uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
        DATA_BLOB server_domain_blob;
        DATA_BLOB challenge_blob;
-       DATA_BLOB struct_blob = data_blob(NULL, 0);
+       DATA_BLOB struct_blob = data_blob_null;
        char *server_domain;
        const char *chal_parse_string;
        const char *auth_gen_string;
-       DATA_BLOB lm_response = data_blob(NULL, 0);
-       DATA_BLOB nt_response = data_blob(NULL, 0);
-       DATA_BLOB session_key = data_blob(NULL, 0);
-       DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
+       DATA_BLOB lm_response = data_blob_null;
+       DATA_BLOB nt_response = data_blob_null;
+       DATA_BLOB session_key = data_blob_null;
+       DATA_BLOB encrypted_session_key = data_blob_null;
        NTSTATUS nt_status = NT_STATUS_OK;
 
        if (!msrpc_parse(&reply, "CdBd",
@@ -911,7 +1007,7 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                         &server_domain_blob,
                         &chal_flags)) {
                DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
-               dump_data(2, (const char *)reply.data, reply.length);
+               dump_data(2, reply.data, reply.length);
 
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -952,7 +1048,7 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                         &unkn1, &unkn2,
                         &struct_blob)) {
                DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
-               dump_data(2, (const char *)reply.data, reply.length);
+               dump_data(2, reply.data, reply.length);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -965,8 +1061,8 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!ntlmssp_state->password) {
-               static const uchar zeros[16];
+       if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
+               static const uchar zeros[16] = { 0, };
                /* do nothing - blobs are zero length */
 
                /* session key is all zeros */
@@ -985,9 +1081,9 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                /* TODO: if the remote server is standalone, then we should replace 'domain'
                   with the server name as supplied above */
                
-               if (!SMBNTLMv2encrypt(ntlmssp_state->user, 
+               if (!SMBNTLMv2encrypt_hash(ntlmssp_state->user, 
                                      ntlmssp_state->domain, 
-                                     ntlmssp_state->password, &challenge_blob, 
+                                     ntlmssp_state->nt_hash, &challenge_blob, 
                                      &struct_blob, 
                                      &lm_response, &nt_response, &session_key)) {
                        data_blob_free(&challenge_blob);
@@ -996,11 +1092,9 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                }
        } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
                struct MD5Context md5_session_nonce_ctx;
-               uchar nt_hash[16];
                uchar session_nonce[16];
                uchar session_nonce_hash[16];
                uchar user_session_key[16];
-               E_md4hash(ntlmssp_state->password, nt_hash);
                
                lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
                generate_random_buffer(lm_response.data, 8);
@@ -1016,43 +1110,38 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
 
                DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
                DEBUG(5, ("challenge is: \n"));
-               dump_data(5, (const char *)session_nonce_hash, 8);
+               dump_data(5, session_nonce_hash, 8);
                
                nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-               SMBNTencrypt(ntlmssp_state->password,
+               SMBNTencrypt_hash(ntlmssp_state->nt_hash,
                             session_nonce_hash,
                             nt_response.data);
 
                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
 
-               SMBsesskeygen_ntv1(nt_hash, NULL, user_session_key);
+               SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, NULL, user_session_key);
                hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
                dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
        } else {
-               uchar lm_hash[16];
-               uchar nt_hash[16];
-               E_deshash(ntlmssp_state->password, lm_hash);
-               E_md4hash(ntlmssp_state->password, nt_hash);
-               
                /* lanman auth is insecure, it may be disabled */
                if (lp_client_lanman_auth()) {
                        lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-                       SMBencrypt(ntlmssp_state->password,challenge_blob.data,
+                       SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
                                   lm_response.data);
                }
                
                nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-               SMBNTencrypt(ntlmssp_state->password,challenge_blob.data,
+               SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
                             nt_response.data);
                
                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
                if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
                    && lp_client_lanman_auth()) {
-                       SMBsesskeygen_lm_sess_key(lm_hash, lm_response.data,
+                       SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
                                        session_key.data);
                        dump_data_pw("LM session key\n", session_key.data, session_key.length);
                } else {
-                       SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
+                       SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, NULL, session_key.data);
                        dump_data_pw("NT session key:\n", session_key.data, session_key.length);
                }
        }
@@ -1097,9 +1186,6 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
 
        ntlmssp_state->session_key = session_key;
 
-       /* The client might be using 56 or 40 bit weakened keys */
-       ntlmssp_weaken_keys(ntlmssp_state);
-
        ntlmssp_state->chal = challenge_blob;
        ntlmssp_state->lm_resp = lm_response;
        ntlmssp_state->nt_resp = nt_response;
@@ -1143,15 +1229,10 @@ NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
 
        (*ntlmssp_state)->neg_flags = 
                NTLMSSP_NEGOTIATE_128 |
+               NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
                NTLMSSP_NEGOTIATE_NTLM |
                NTLMSSP_NEGOTIATE_NTLM2 |
                NTLMSSP_NEGOTIATE_KEY_EXCH |
-               /*
-                * We need to set this to allow a later SetPassword
-                * via the SAMR pipe to succeed. Strange.... We could
-                * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
-                * */
-               NTLMSSP_NEGOTIATE_SIGN |
                NTLMSSP_REQUEST_TARGET;
 
        return NT_STATUS_OK;