r6484: fix NTLMSSP client against w2k and w2k3
[samba.git] / source4 / auth / ntlmssp / ntlmssp_client.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "auth/auth.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "lib/crypto/crypto.h"
29
30 /*********************************************************************
31  Client side NTLMSSP
32 *********************************************************************/
33
34 /**
35  * Next state function for the Initial packet
36  * 
37  * @param ntlmssp_state NTLMSSP State
38  * @param out_mem_ctx The DATA_BLOB *out will be allocated on this context
39  * @param in A NULL data blob (input ignored)
40  * @param out The initial negotiate request to the server, as an talloc()ed DATA_BLOB, on out_mem_ctx
41  * @return Errors or NT_STATUS_OK. 
42  */
43
44 NTSTATUS ntlmssp_client_initial(struct gensec_security *gensec_security, 
45                                 TALLOC_CTX *out_mem_ctx, 
46                                 DATA_BLOB in, DATA_BLOB *out) 
47 {
48         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
49
50         if (gensec_ntlmssp_state->unicode) {
51                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
52         } else {
53                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
54         }
55         
56         if (gensec_ntlmssp_state->use_ntlmv2) {
57                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
58         }
59
60         /* generate the ntlmssp negotiate packet */
61         msrpc_gen(out_mem_ctx, 
62                   out, "CddAA",
63                   "NTLMSSP",
64                   NTLMSSP_NEGOTIATE,
65                   gensec_ntlmssp_state->neg_flags,
66                   gensec_ntlmssp_state->get_domain(), 
67                   cli_credentials_get_workstation(gensec_security->credentials));
68
69         gensec_ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
70
71         return NT_STATUS_MORE_PROCESSING_REQUIRED;
72 }
73
74 /**
75  * Next state function for the Challenge Packet.  Generate an auth packet.
76  * 
77  * @param gensec_security GENSEC state
78  * @param out_mem_ctx Memory context for *out
79  * @param in The server challnege, as a DATA_BLOB.  reply.data must be NULL
80  * @param out The next request (auth packet) to the server, as an allocated DATA_BLOB, on the out_mem_ctx context
81  * @return Errors or NT_STATUS_OK. 
82  */
83
84 NTSTATUS ntlmssp_client_challenge(struct gensec_security *gensec_security, 
85                                   TALLOC_CTX *out_mem_ctx,
86                                   const DATA_BLOB in, DATA_BLOB *out) 
87 {
88         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
89         uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
90         DATA_BLOB server_domain_blob;
91         DATA_BLOB challenge_blob;
92         DATA_BLOB struct_blob = data_blob(NULL, 0);
93         char *server_domain;
94         const char *chal_parse_string;
95         const char *auth_gen_string;
96         uint8_t lm_hash[16];
97         DATA_BLOB lm_response = data_blob(NULL, 0);
98         DATA_BLOB nt_response = data_blob(NULL, 0);
99         DATA_BLOB session_key = data_blob(NULL, 0);
100         DATA_BLOB lm_session_key = data_blob(NULL, 0);
101         DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
102         NTSTATUS nt_status;
103
104         const char *user, *domain, *password;
105
106         if (!msrpc_parse(out_mem_ctx,
107                          &in, "CdBd",
108                          "NTLMSSP",
109                          &ntlmssp_command, 
110                          &server_domain_blob,
111                          &chal_flags)) {
112                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
113                 dump_data(2, in.data, in.length);
114
115                 return NT_STATUS_INVALID_PARAMETER;
116         }
117         
118         data_blob_free(&server_domain_blob);
119
120         DEBUG(3, ("Got challenge flags:\n"));
121         debug_ntlmssp_flags(chal_flags);
122
123         ntlmssp_handle_neg_flags(gensec_ntlmssp_state, chal_flags, gensec_ntlmssp_state->allow_lm_key);
124
125         if (gensec_ntlmssp_state->unicode) {
126                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
127                         chal_parse_string = "CdUdbddB";
128                 } else {
129                         chal_parse_string = "CdUdbdd";
130                 }
131                 auth_gen_string = "CdBBUUUBd";
132         } else {
133                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
134                         chal_parse_string = "CdAdbddB";
135                 } else {
136                         chal_parse_string = "CdAdbdd";
137                 }
138
139                 auth_gen_string = "CdBBAAABd";
140         }
141
142         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
143         debug_ntlmssp_flags(gensec_ntlmssp_state->neg_flags);
144
145         if (!msrpc_parse(out_mem_ctx,
146                          &in, chal_parse_string,
147                          "NTLMSSP",
148                          &ntlmssp_command, 
149                          &server_domain,
150                          &chal_flags,
151                          &challenge_blob, 8,
152                          &unkn1, &unkn2,
153                          &struct_blob)) {
154                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
155                 dump_data(2, in.data, in.length);
156                 return NT_STATUS_INVALID_PARAMETER;
157         }
158
159         gensec_ntlmssp_state->server_domain = server_domain;
160
161         if (challenge_blob.length != 8) {
162                 return NT_STATUS_INVALID_PARAMETER;
163         }
164
165         /* Correctly handle username in the original form of user@REALM, which is valid */
166         if (gensec_security->credentials->realm_obtained
167             > gensec_security->credentials->domain_obtained) {
168                 user = talloc_asprintf(out_mem_ctx, "%s@%s", 
169                                        cli_credentials_get_username(gensec_security->credentials), 
170                                        cli_credentials_get_realm(gensec_security->credentials));
171                 domain = NULL;
172         } else {
173                 user = cli_credentials_get_username(gensec_security->credentials);
174                 domain = cli_credentials_get_domain(gensec_security->credentials);
175         }
176
177         password = cli_credentials_get_password(gensec_security->credentials);
178
179         if (!password) {
180                 static const uint8_t zero[1];
181                 static const uint8_t zeros[16];
182                 /* do nothing - blobs are zero length */
183
184                 /* session key is all zeros */
185                 session_key = data_blob_talloc(gensec_ntlmssp_state, zeros, 16);
186                 lm_session_key = data_blob_talloc(gensec_ntlmssp_state, zeros, 16);
187
188                 lm_response = data_blob_talloc(gensec_ntlmssp_state, zero, 1);
189                 nt_response = data_blob(NULL, 0);
190                 
191                 /* not doing NLTM2 without a password */
192                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
193         } else if (gensec_ntlmssp_state->use_ntlmv2) {
194
195                 if (!struct_blob.length) {
196                         /* be lazy, match win2k - we can't do NTLMv2 without it */
197                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
198                         return NT_STATUS_INVALID_PARAMETER;
199                 }
200
201                 /* TODO: if the remote server is standalone, then we should replace 'domain'
202                    with the server name as supplied above */
203                 
204                 if (!SMBNTLMv2encrypt(user, 
205                                       domain, 
206                                       password, &challenge_blob, 
207                                       &struct_blob, 
208                                       &lm_response, &nt_response, 
209                                       NULL, &session_key)) {
210                         data_blob_free(&challenge_blob);
211                         data_blob_free(&struct_blob);
212                         return NT_STATUS_NO_MEMORY;
213                 }
214
215                 /* LM Key is incompatible... */
216                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
217
218         } else if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
219                 struct MD5Context md5_session_nonce_ctx;
220                 uint8_t nt_hash[16];
221                 uint8_t session_nonce[16];
222                 uint8_t session_nonce_hash[16];
223                 uint8_t user_session_key[16];
224                 E_md4hash(password, nt_hash);
225                 
226                 lm_response = data_blob_talloc(gensec_ntlmssp_state, NULL, 24);
227                 generate_random_buffer(lm_response.data, 8);
228                 memset(lm_response.data+8, 0, 16);
229
230                 memcpy(session_nonce, challenge_blob.data, 8);
231                 memcpy(&session_nonce[8], lm_response.data, 8);
232         
233                 MD5Init(&md5_session_nonce_ctx);
234                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
235                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
236                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
237
238                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
239                 DEBUG(5, ("challenge is: \n"));
240                 dump_data(5, session_nonce_hash, 8);
241                 
242                 nt_response = data_blob_talloc(gensec_ntlmssp_state, NULL, 24);
243                 SMBNTencrypt(password,
244                              session_nonce_hash,
245                              nt_response.data);
246
247                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
248
249                 SMBsesskeygen_ntv1(nt_hash, user_session_key);
250                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
251                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
252
253                 /* LM Key is incompatible... */
254                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
255         } else {
256                 uint8_t nt_hash[16];
257
258                 if (gensec_ntlmssp_state->use_nt_response) {
259                         nt_response = data_blob_talloc(gensec_ntlmssp_state, NULL, 24);
260                         SMBNTencrypt(password,challenge_blob.data,
261                                      nt_response.data);
262                         E_md4hash(password, nt_hash);
263                         session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
264                         SMBsesskeygen_ntv1(nt_hash, session_key.data);
265                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
266                 }
267
268                 /* lanman auth is insecure, it may be disabled */
269                 if (lp_client_lanman_auth()) {
270                         lm_response = data_blob_talloc(gensec_ntlmssp_state, NULL, 24);
271                         if (!SMBencrypt(password,challenge_blob.data,
272                                         lm_response.data)) {
273                                 /* If the LM password was too long (and therefore the LM hash being
274                                    of the first 14 chars only), don't send it */
275                                 data_blob_free(&lm_response);
276
277                                 /* LM Key is incompatible with 'long' passwords */
278                                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
279                         } else {
280                                 E_deshash(password, lm_hash);
281                                 lm_session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
282                                 memcpy(lm_session_key.data, lm_hash, 8);
283                                 memset(&lm_session_key.data[8], '\0', 8);
284
285                                 if (!gensec_ntlmssp_state->use_nt_response) {
286                                         session_key = lm_session_key;
287                                 }
288                         }
289                 } else {
290                         /* LM Key is incompatible... */
291                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
292                 }
293         }
294         
295         if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
296             && lp_client_lanman_auth() && lm_session_key.length == 16) {
297                 DATA_BLOB new_session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
298                 if (lm_response.length == 24) {
299                         SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data, 
300                                                   new_session_key.data);
301                 } else {
302                         static const uint8_t zeros[24];
303                         SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
304                                                   new_session_key.data);
305                 }
306                 new_session_key.length = 16;
307                 session_key = new_session_key;
308                 dump_data_pw("LM session key\n", session_key.data, session_key.length);
309         }
310
311
312         /* Key exchange encryptes a new client-generated session key with
313            the password-derived key */
314         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
315                 /* Make up a new session key */
316                 uint8_t client_session_key[16];
317                 generate_random_buffer(client_session_key, sizeof(client_session_key));
318
319                 /* Encrypt the new session key with the old one */
320                 encrypted_session_key = data_blob_talloc(gensec_ntlmssp_state, 
321                                                          client_session_key, sizeof(client_session_key));
322                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
323                 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
324                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
325
326                 /* Mark the new session key as the 'real' session key */
327                 session_key = data_blob_talloc(gensec_ntlmssp_state, client_session_key, sizeof(client_session_key));
328         }
329
330         /* this generates the actual auth packet */
331         if (!msrpc_gen(out_mem_ctx, 
332                        out, auth_gen_string, 
333                        "NTLMSSP", 
334                        NTLMSSP_AUTH, 
335                        lm_response.data, lm_response.length,
336                        nt_response.data, nt_response.length,
337                        domain, 
338                        user, 
339                        cli_credentials_get_workstation(gensec_security->credentials),
340                        encrypted_session_key.data, encrypted_session_key.length,
341                        gensec_ntlmssp_state->neg_flags)) {
342                 
343                 return NT_STATUS_NO_MEMORY;
344         }
345
346         gensec_ntlmssp_state->session_key = session_key;
347
348         /* The client might be using 56 or 40 bit weakened keys */
349         ntlmssp_weaken_keys(gensec_ntlmssp_state);
350
351         gensec_ntlmssp_state->chal = challenge_blob;
352         gensec_ntlmssp_state->lm_resp = lm_response;
353         gensec_ntlmssp_state->nt_resp = nt_response;
354
355         gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
356
357         nt_status = ntlmssp_sign_init(gensec_ntlmssp_state);
358         if (!NT_STATUS_IS_OK(nt_status)) {
359                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", 
360                           nt_errstr(nt_status)));
361                 return nt_status;
362         }
363
364         return nt_status;
365 }
366
367 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
368 {
369         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
370         NTSTATUS nt_status;
371
372         nt_status = gensec_ntlmssp_start(gensec_security);
373         NT_STATUS_NOT_OK_RETURN(nt_status);
374
375         gensec_ntlmssp_state = gensec_security->private_data;
376
377         gensec_ntlmssp_state->role = NTLMSSP_CLIENT;
378
379         gensec_ntlmssp_state->get_domain = lp_workgroup;
380
381         gensec_ntlmssp_state->unicode = lp_parm_bool(-1, "ntlmssp_client", "unicode", True);
382
383         gensec_ntlmssp_state->use_nt_response = lp_parm_bool(-1, "ntlmssp_client", "send_nt_reponse", True);
384
385         gensec_ntlmssp_state->allow_lm_key = (lp_lanman_auth() 
386                                           && lp_parm_bool(-1, "ntlmssp_client", "allow_lm_key", False));
387
388         gensec_ntlmssp_state->use_ntlmv2 = lp_client_ntlmv2_auth();
389
390         gensec_ntlmssp_state->expected_state = NTLMSSP_INITIAL;
391
392         gensec_ntlmssp_state->neg_flags = 
393                 NTLMSSP_NEGOTIATE_NTLM |
394                 NTLMSSP_REQUEST_TARGET;
395
396         if (lp_parm_bool(-1, "ntlmssp_client", "128bit", True)) {
397                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;               
398         }
399
400         if (lp_parm_bool(-1, "ntlmssp_client", "keyexchange", True)) {
401                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;          
402         }
403
404         if (lp_parm_bool(-1, "ntlmssp_client", "ntlm2", True)) {
405                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;             
406         } else {
407                 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
408                 gensec_ntlmssp_state->use_ntlmv2 = False;
409         }
410
411         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
412                 /*
413                  * We need to set this to allow a later SetPassword
414                  * via the SAMR pipe to succeed. Strange.... We could
415                  * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
416                  * 
417                  * Without this, Windows will not create the master key
418                  * that it thinks is only used for NTLMSSP signing and 
419                  * sealing.  (It is actually pulled out and used directly) 
420                  */
421                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
422         }
423         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
424                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
425         }
426         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
427                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
428         }
429
430         gensec_security->private_data = gensec_ntlmssp_state;
431
432         return NT_STATUS_OK;
433 }
434