ceca0d6978a04d3803d9147770a507b8b2e7d594
[samba.git] / source / 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 The request, as a DATA_BLOB.  reply.data must be NULL
40  * @param out The reply, 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         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
50
51         if (ntlmssp_state->unicode) {
52                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
53         } else {
54                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
55         }
56         
57         if (ntlmssp_state->use_ntlmv2) {
58                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
59         }
60
61         /* generate the ntlmssp negotiate packet */
62         msrpc_gen(out_mem_ctx, 
63                   out, "CddAA",
64                   "NTLMSSP",
65                   NTLMSSP_NEGOTIATE,
66                   ntlmssp_state->neg_flags,
67                   ntlmssp_state->get_domain(), 
68                   cli_credentials_get_workstation(gensec_security->credentials));
69
70         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
71
72         return NT_STATUS_MORE_PROCESSING_REQUIRED;
73 }
74
75 /**
76  * Next state function for the Challenge Packet.  Generate an auth packet.
77  * 
78  * @param ntlmssp_state NTLMSSP State
79  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
80  * @param request The reply, as an allocated DATA_BLOB, caller to free.
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         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
90         uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
91         DATA_BLOB server_domain_blob;
92         DATA_BLOB challenge_blob;
93         DATA_BLOB struct_blob = data_blob(NULL, 0);
94         char *server_domain;
95         const char *chal_parse_string;
96         const char *auth_gen_string;
97         uint8_t lm_hash[16];
98         DATA_BLOB lm_response = data_blob(NULL, 0);
99         DATA_BLOB nt_response = data_blob(NULL, 0);
100         DATA_BLOB session_key = data_blob(NULL, 0);
101         DATA_BLOB lm_session_key = data_blob(NULL, 0);
102         DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
103         NTSTATUS nt_status;
104
105         const char *user, *domain, *password;
106
107         if (!msrpc_parse(ntlmssp_state, 
108                          &in, "CdBd",
109                          "NTLMSSP",
110                          &ntlmssp_command, 
111                          &server_domain_blob,
112                          &chal_flags)) {
113                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
114                 dump_data(2, in.data, in.length);
115
116                 return NT_STATUS_INVALID_PARAMETER;
117         }
118         
119         data_blob_free(&server_domain_blob);
120
121         DEBUG(3, ("Got challenge flags:\n"));
122         debug_ntlmssp_flags(chal_flags);
123
124         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, ntlmssp_state->allow_lm_key);
125
126         if (ntlmssp_state->unicode) {
127                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
128                         chal_parse_string = "CdUdbddB";
129                 } else {
130                         chal_parse_string = "CdUdbdd";
131                 }
132                 auth_gen_string = "CdBBUUUBd";
133         } else {
134                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
135                         chal_parse_string = "CdAdbddB";
136                 } else {
137                         chal_parse_string = "CdAdbdd";
138                 }
139
140                 auth_gen_string = "CdBBAAABd";
141         }
142
143         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
144         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
145
146         if (!msrpc_parse(ntlmssp_state, 
147                          &in, chal_parse_string,
148                          "NTLMSSP",
149                          &ntlmssp_command, 
150                          &server_domain,
151                          &chal_flags,
152                          &challenge_blob, 8,
153                          &unkn1, &unkn2,
154                          &struct_blob)) {
155                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
156                 dump_data(2, in.data, in.length);
157                 return NT_STATUS_INVALID_PARAMETER;
158         }
159
160         ntlmssp_state->server_domain = server_domain;
161
162         if (challenge_blob.length != 8) {
163                 return NT_STATUS_INVALID_PARAMETER;
164         }
165
166         /* Correctly handle username in the original form of user@REALM, which is valid */
167         if (gensec_security->credentials->realm_obtained
168             > gensec_security->credentials->domain_obtained) {
169                 user = talloc_asprintf(out_mem_ctx, "%s@%s", 
170                                        cli_credentials_get_username(gensec_security->credentials), 
171                                        cli_credentials_get_realm(gensec_security->credentials));
172                 domain = NULL;
173         } else {
174                 user = cli_credentials_get_username(gensec_security->credentials);
175                 domain = cli_credentials_get_domain(gensec_security->credentials);
176         }
177
178         password = cli_credentials_get_password(gensec_security->credentials);
179
180         if (!password) {
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(ntlmssp_state, zeros, 16);
186                 lm_session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
187
188                 /* not doing NLTM2 without a password */
189                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
190         } else if (ntlmssp_state->use_ntlmv2) {
191
192                 if (!struct_blob.length) {
193                         /* be lazy, match win2k - we can't do NTLMv2 without it */
194                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
195                         return NT_STATUS_INVALID_PARAMETER;
196                 }
197
198                 /* TODO: if the remote server is standalone, then we should replace 'domain'
199                    with the server name as supplied above */
200                 
201                 if (!SMBNTLMv2encrypt(user, 
202                                       domain, 
203                                       password, &challenge_blob, 
204                                       &struct_blob, 
205                                       &lm_response, &nt_response, 
206                                       NULL, &session_key)) {
207                         data_blob_free(&challenge_blob);
208                         data_blob_free(&struct_blob);
209                         return NT_STATUS_NO_MEMORY;
210                 }
211
212                 /* LM Key is incompatible... */
213                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
214
215         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
216                 struct MD5Context md5_session_nonce_ctx;
217                 uint8_t nt_hash[16];
218                 uint8_t session_nonce[16];
219                 uint8_t session_nonce_hash[16];
220                 uint8_t user_session_key[16];
221                 E_md4hash(password, nt_hash);
222                 
223                 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
224                 generate_random_buffer(lm_response.data, 8);
225                 memset(lm_response.data+8, 0, 16);
226
227                 memcpy(session_nonce, challenge_blob.data, 8);
228                 memcpy(&session_nonce[8], lm_response.data, 8);
229         
230                 MD5Init(&md5_session_nonce_ctx);
231                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
232                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
233                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
234
235                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
236                 DEBUG(5, ("challenge is: \n"));
237                 dump_data(5, session_nonce_hash, 8);
238                 
239                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
240                 SMBNTencrypt(password,
241                              session_nonce_hash,
242                              nt_response.data);
243
244                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
245
246                 SMBsesskeygen_ntv1(nt_hash, user_session_key);
247                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
248                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
249
250                 /* LM Key is incompatible... */
251                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
252         } else {
253                 uint8_t nt_hash[16];
254
255                 if (ntlmssp_state->use_nt_response) {
256                         nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
257                         SMBNTencrypt(password,challenge_blob.data,
258                                      nt_response.data);
259                         E_md4hash(password, nt_hash);
260                         session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
261                         SMBsesskeygen_ntv1(nt_hash, session_key.data);
262                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
263                 }
264
265                 /* lanman auth is insecure, it may be disabled */
266                 if (lp_client_lanman_auth()) {
267                         lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
268                         if (!SMBencrypt(password,challenge_blob.data,
269                                         lm_response.data)) {
270                                 /* If the LM password was too long (and therefore the LM hash being
271                                    of the first 14 chars only), don't send it */
272                                 data_blob_free(&lm_response);
273
274                                 /* LM Key is incompatible with 'long' passwords */
275                                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
276                         } else {
277                                 E_deshash(password, lm_hash);
278                                 lm_session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
279                                 memcpy(lm_session_key.data, lm_hash, 8);
280                                 memset(&lm_session_key.data[8], '\0', 8);
281
282                                 if (!ntlmssp_state->use_nt_response) {
283                                         session_key = lm_session_key;
284                                 }
285                         }
286                 } else {
287                         /* LM Key is incompatible... */
288                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
289                 }
290         }
291         
292         if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
293             && lp_client_lanman_auth() && lm_session_key.length == 16) {
294                 DATA_BLOB new_session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
295                 if (lm_response.length == 24) {
296                         SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data, 
297                                                   new_session_key.data);
298                 } else {
299                         static const uint8_t zeros[24];
300                         SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
301                                                   new_session_key.data);
302                 }
303                 new_session_key.length = 16;
304                 session_key = new_session_key;
305                 dump_data_pw("LM session key\n", session_key.data, session_key.length);
306         }
307
308
309         /* Key exchange encryptes a new client-generated session key with
310            the password-derived key */
311         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
312                 /* Make up a new session key */
313                 uint8_t client_session_key[16];
314                 generate_random_buffer(client_session_key, sizeof(client_session_key));
315
316                 /* Encrypt the new session key with the old one */
317                 encrypted_session_key = data_blob_talloc(ntlmssp_state, 
318                                                          client_session_key, sizeof(client_session_key));
319                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
320                 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
321                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
322
323                 /* Mark the new session key as the 'real' session key */
324                 session_key = data_blob_talloc(ntlmssp_state, client_session_key, sizeof(client_session_key));
325         }
326
327         /* this generates the actual auth packet */
328         if (!msrpc_gen(out_mem_ctx, 
329                        out, auth_gen_string, 
330                        "NTLMSSP", 
331                        NTLMSSP_AUTH, 
332                        lm_response.data, lm_response.length,
333                        nt_response.data, nt_response.length,
334                        domain, 
335                        user, 
336                        cli_credentials_get_workstation(gensec_security->credentials),
337                        encrypted_session_key.data, encrypted_session_key.length,
338                        ntlmssp_state->neg_flags)) {
339                 
340                 return NT_STATUS_NO_MEMORY;
341         }
342
343         ntlmssp_state->session_key = session_key;
344
345         /* The client might be using 56 or 40 bit weakened keys */
346         ntlmssp_weaken_keys(ntlmssp_state);
347
348         ntlmssp_state->chal = challenge_blob;
349         ntlmssp_state->lm_resp = lm_response;
350         ntlmssp_state->nt_resp = nt_response;
351
352         ntlmssp_state->expected_state = NTLMSSP_DONE;
353
354         nt_status = ntlmssp_sign_init(ntlmssp_state);
355         if (!NT_STATUS_IS_OK(nt_status)) {
356                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", 
357                           nt_errstr(nt_status)));
358                 return nt_status;
359         }
360
361         return nt_status;
362 }
363
364 static NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx, struct ntlmssp_state **ntlmssp_state)
365 {
366         *ntlmssp_state = talloc(mem_ctx, struct ntlmssp_state);
367         if (!*ntlmssp_state) {
368                 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
369                 return NT_STATUS_NO_MEMORY;
370         }
371         ZERO_STRUCTP(*ntlmssp_state);
372
373         (*ntlmssp_state)->role = NTLMSSP_CLIENT;
374
375         (*ntlmssp_state)->get_domain = lp_workgroup;
376
377         (*ntlmssp_state)->unicode = lp_parm_bool(-1, "ntlmssp_client", "unicode", True);
378
379         (*ntlmssp_state)->use_nt_response = lp_parm_bool(-1, "ntlmssp_client", "send_nt_reponse", True);
380
381         (*ntlmssp_state)->allow_lm_key = (lp_lanman_auth() 
382                                           && lp_parm_bool(-1, "ntlmssp_client", "allow_lm_key", False));
383
384         (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
385
386         (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
387
388         (*ntlmssp_state)->neg_flags = 
389                 NTLMSSP_NEGOTIATE_NTLM |
390                 NTLMSSP_REQUEST_TARGET;
391
392         if (lp_parm_bool(-1, "ntlmssp_client", "128bit", True)) {
393                 (*ntlmssp_state)->neg_flags |= NTLMSSP_NEGOTIATE_128;           
394         }
395
396         if (lp_parm_bool(-1, "ntlmssp_client", "keyexchange", True)) {
397                 (*ntlmssp_state)->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;              
398         }
399
400         if (lp_parm_bool(-1, "ntlmssp_client", "ntlm2", True)) {
401                 (*ntlmssp_state)->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;         
402         } else {
403                 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
404                 (*ntlmssp_state)->use_ntlmv2 = False;
405         }
406
407         return NT_STATUS_OK;
408 }
409
410 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
411 {
412         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
413         NTSTATUS nt_status;
414
415         nt_status = gensec_ntlmssp_start(gensec_security);
416         NT_STATUS_NOT_OK_RETURN(nt_status);
417
418         gensec_ntlmssp_state = gensec_security->private_data;
419         nt_status = ntlmssp_client_start(gensec_ntlmssp_state,
420                                          &gensec_ntlmssp_state->ntlmssp_state);
421         NT_STATUS_NOT_OK_RETURN(nt_status);
422
423         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
424                 /*
425                  * We need to set this to allow a later SetPassword
426                  * via the SAMR pipe to succeed. Strange.... We could
427                  * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
428                  * 
429                  * Without this, Windows will not create the master key
430                  * that it thinks is only used for NTLMSSP signing and 
431                  * sealing.  (It is actually pulled out and used directly) 
432                  */
433                 gensec_ntlmssp_state->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
434         }
435         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
436                 gensec_ntlmssp_state->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
437         }
438         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
439                 gensec_ntlmssp_state->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
440         }
441
442         gensec_security->private_data = gensec_ntlmssp_state;
443
444         return NT_STATUS_OK;
445 }
446