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