r13467: Add new parametric options (for testing) controlling LM_KEY and 56-bit
[ira/wip.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 target_info = data_blob(NULL, 0);
93         char *server_domain;
94         const char *chal_parse_string;
95         const char *auth_gen_string;
96         DATA_BLOB lm_response = data_blob(NULL, 0);
97         DATA_BLOB nt_response = data_blob(NULL, 0);
98         DATA_BLOB session_key = data_blob(NULL, 0);
99         DATA_BLOB lm_session_key = data_blob(NULL, 0);
100         DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
101         NTSTATUS nt_status;
102         int flags = 0;
103         const char *user, *domain;
104
105         TALLOC_CTX *mem_ctx = talloc_new(out_mem_ctx);
106         if (!mem_ctx) {
107                 return NT_STATUS_NO_MEMORY;
108         }
109
110         if (!msrpc_parse(mem_ctx,
111                          &in, "CdBd",
112                          "NTLMSSP",
113                          &ntlmssp_command, 
114                          &server_domain_blob,
115                          &chal_flags)) {
116                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
117                 dump_data(2, in.data, in.length);
118                 talloc_free(mem_ctx);
119
120                 return NT_STATUS_INVALID_PARAMETER;
121         }
122         
123         data_blob_free(&server_domain_blob);
124
125         DEBUG(3, ("Got challenge flags:\n"));
126         debug_ntlmssp_flags(chal_flags);
127
128         ntlmssp_handle_neg_flags(gensec_ntlmssp_state, chal_flags, gensec_ntlmssp_state->allow_lm_key);
129
130         if (gensec_ntlmssp_state->unicode) {
131                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
132                         chal_parse_string = "CdUdbddB";
133                 } else {
134                         chal_parse_string = "CdUdbdd";
135                 }
136                 auth_gen_string = "CdBBUUUBd";
137         } else {
138                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
139                         chal_parse_string = "CdAdbddB";
140                 } else {
141                         chal_parse_string = "CdAdbdd";
142                 }
143
144                 auth_gen_string = "CdBBAAABd";
145         }
146
147         if (!msrpc_parse(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                          &target_info)) {
156                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
157                 dump_data(2, in.data, in.length);
158                 talloc_free(mem_ctx);
159                 return NT_STATUS_INVALID_PARAMETER;
160         }
161
162         gensec_ntlmssp_state->server_domain = server_domain;
163
164         if (challenge_blob.length != 8) {
165                 talloc_free(mem_ctx);
166                 return NT_STATUS_INVALID_PARAMETER;
167         }
168
169         cli_credentials_get_ntlm_username_domain(gensec_security->credentials, mem_ctx, 
170                                                  &user, &domain);
171
172         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
173                 flags |= CLI_CRED_NTLM2;
174         }
175         if (gensec_ntlmssp_state->use_ntlmv2) {
176                 flags |= CLI_CRED_NTLMv2_AUTH;
177         }
178         if (gensec_ntlmssp_state->use_nt_response) {
179                 flags |= CLI_CRED_NTLM_AUTH;
180         }
181         if (lp_client_lanman_auth()) {
182                 flags |= CLI_CRED_LANMAN_AUTH;
183         }
184
185         nt_status = cli_credentials_get_ntlm_response(gensec_security->credentials, mem_ctx, 
186                                                       &flags, challenge_blob, target_info,
187                                                       &lm_response, &nt_response, 
188                                                       &lm_session_key, &session_key);
189
190         if (!NT_STATUS_IS_OK(nt_status)) {
191                 return nt_status;
192         }
193         
194         if (!(flags & CLI_CRED_LANMAN_AUTH)) {
195                 /* LM Key is incompatible... */
196                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
197         }
198
199         if (!(flags & CLI_CRED_NTLM2)) {
200                 /* NTLM2 is incompatible... */
201                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
202         }
203         
204         if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
205             && lp_client_lanman_auth() && lm_session_key.length == 16) {
206                 DATA_BLOB new_session_key = data_blob_talloc(mem_ctx, NULL, 16);
207                 if (lm_response.length == 24) {
208                         SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data, 
209                                                   new_session_key.data);
210                 } else {
211                         static const uint8_t zeros[24];
212                         SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
213                                                   new_session_key.data);
214                 }
215                 session_key = new_session_key;
216                 dump_data_pw("LM session key\n", session_key.data, session_key.length);
217         }
218
219
220         /* Key exchange encryptes a new client-generated session key with
221            the password-derived key */
222         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
223                 /* Make up a new session key */
224                 uint8_t client_session_key[16];
225                 generate_random_buffer(client_session_key, sizeof(client_session_key));
226
227                 /* Encrypt the new session key with the old one */
228                 encrypted_session_key = data_blob_talloc(gensec_ntlmssp_state, 
229                                                          client_session_key, sizeof(client_session_key));
230                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
231                 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
232                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
233
234                 /* Mark the new session key as the 'real' session key */
235                 session_key = data_blob_talloc(mem_ctx, client_session_key, sizeof(client_session_key));
236         }
237
238         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
239         debug_ntlmssp_flags(gensec_ntlmssp_state->neg_flags);
240
241         /* this generates the actual auth packet */
242         if (!msrpc_gen(mem_ctx, 
243                        out, auth_gen_string, 
244                        "NTLMSSP", 
245                        NTLMSSP_AUTH, 
246                        lm_response.data, lm_response.length,
247                        nt_response.data, nt_response.length,
248                        domain, 
249                        user, 
250                        cli_credentials_get_workstation(gensec_security->credentials),
251                        encrypted_session_key.data, encrypted_session_key.length,
252                        gensec_ntlmssp_state->neg_flags)) {
253                 talloc_free(mem_ctx);
254                 return NT_STATUS_NO_MEMORY;
255         }
256
257         gensec_ntlmssp_state->session_key = session_key;
258         talloc_steal(gensec_ntlmssp_state, session_key.data);
259
260         talloc_steal(out_mem_ctx, out->data);
261
262         /* The client might be using 56 or 40 bit weakened keys */
263         ntlmssp_weaken_keys(gensec_ntlmssp_state);
264
265         gensec_ntlmssp_state->chal = challenge_blob;
266         gensec_ntlmssp_state->lm_resp = lm_response;
267         talloc_steal(gensec_ntlmssp_state->lm_resp.data, lm_response.data);
268         gensec_ntlmssp_state->nt_resp = nt_response;
269         talloc_steal(gensec_ntlmssp_state->nt_resp.data, nt_response.data);
270
271         gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
272
273         if (gensec_security->want_features & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) {
274                 nt_status = ntlmssp_sign_init(gensec_ntlmssp_state);
275                 if (!NT_STATUS_IS_OK(nt_status)) {
276                         DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", 
277                                   nt_errstr(nt_status)));
278                         talloc_free(mem_ctx);
279                         return nt_status;
280                 }
281         }
282
283         talloc_free(mem_ctx);
284         return NT_STATUS_OK;
285 }
286
287 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
288 {
289         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
290         NTSTATUS nt_status;
291
292         nt_status = gensec_ntlmssp_start(gensec_security);
293         NT_STATUS_NOT_OK_RETURN(nt_status);
294
295         gensec_ntlmssp_state = gensec_security->private_data;
296
297         gensec_ntlmssp_state->role = NTLMSSP_CLIENT;
298
299         gensec_ntlmssp_state->get_domain = lp_workgroup;
300
301         gensec_ntlmssp_state->unicode = lp_parm_bool(-1, "ntlmssp_client", "unicode", True);
302
303         gensec_ntlmssp_state->use_nt_response = lp_parm_bool(-1, "ntlmssp_client", "send_nt_reponse", True);
304
305         gensec_ntlmssp_state->allow_lm_key = (lp_client_lanman_auth() 
306                                               && (lp_parm_bool(-1, "ntlmssp_client", "allow_lm_key", False)
307                                                   || lp_parm_bool(-1, "ntlmssp_client", "lm_key", False)));
308
309         gensec_ntlmssp_state->use_ntlmv2 = lp_client_ntlmv2_auth();
310
311         gensec_ntlmssp_state->expected_state = NTLMSSP_INITIAL;
312
313         gensec_ntlmssp_state->neg_flags = 
314                 NTLMSSP_NEGOTIATE_NTLM |
315                 NTLMSSP_REQUEST_TARGET;
316
317         if (lp_parm_bool(-1, "ntlmssp_client", "128bit", True)) {
318                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;               
319         }
320
321         if (lp_parm_bool(-1, "ntlmssp_client", "56bit", False)) {
322                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;                
323         }
324
325         if (lp_parm_bool(-1, "ntlmssp_client", "lm_key", False)) {
326                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
327         }
328
329         if (lp_parm_bool(-1, "ntlmssp_client", "keyexchange", True)) {
330                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;          
331         }
332
333         if (lp_parm_bool(-1, "ntlmssp_client", "ntlm2", True)) {
334                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;             
335         } else {
336                 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
337                 gensec_ntlmssp_state->use_ntlmv2 = False;
338         }
339
340         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
341                 /*
342                  * We need to set this to allow a later SetPassword
343                  * via the SAMR pipe to succeed. Strange.... We could
344                  * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
345                  * 
346                  * Without this, Windows will not create the master key
347                  * that it thinks is only used for NTLMSSP signing and 
348                  * sealing.  (It is actually pulled out and used directly) 
349                  */
350                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
351         }
352         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
353                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
354         }
355         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
356                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
357         }
358
359         gensec_security->private_data = gensec_ntlmssp_state;
360
361         return NT_STATUS_OK;
362 }
363