2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, client server side parsing
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8 Copyright (C) Stefan Metzmacher 2005
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.
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.
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.
26 #include "auth/auth.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "lib/crypto/crypto.h"
30 /*********************************************************************
32 *********************************************************************/
35 * Next state function for the Initial packet
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.
44 NTSTATUS ntlmssp_client_initial(struct gensec_security *gensec_security,
45 TALLOC_CTX *out_mem_ctx,
46 DATA_BLOB in, DATA_BLOB *out)
48 struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
49 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
51 if (ntlmssp_state->unicode) {
52 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
54 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
57 if (ntlmssp_state->use_ntlmv2) {
58 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
61 /* generate the ntlmssp negotiate packet */
62 msrpc_gen(out_mem_ctx,
66 ntlmssp_state->neg_flags,
67 ntlmssp_state->get_domain(),
68 cli_credentials_get_workstation(gensec_security->credentials));
70 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
72 return NT_STATUS_MORE_PROCESSING_REQUIRED;
76 * Next state function for the Challenge Packet. Generate an auth packet.
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.
84 NTSTATUS ntlmssp_client_challenge(struct gensec_security *gensec_security,
85 TALLOC_CTX *out_mem_ctx,
86 const DATA_BLOB in, DATA_BLOB *out)
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);
95 const char *chal_parse_string;
96 const char *auth_gen_string;
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);
105 const char *user, *domain, *password;
107 if (!msrpc_parse(ntlmssp_state,
113 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
114 dump_data(2, in.data, in.length);
116 return NT_STATUS_INVALID_PARAMETER;
119 data_blob_free(&server_domain_blob);
121 DEBUG(3, ("Got challenge flags:\n"));
122 debug_ntlmssp_flags(chal_flags);
124 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, ntlmssp_state->allow_lm_key);
126 if (ntlmssp_state->unicode) {
127 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
128 chal_parse_string = "CdUdbddB";
130 chal_parse_string = "CdUdbdd";
132 auth_gen_string = "CdBBUUUBd";
134 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
135 chal_parse_string = "CdAdbddB";
137 chal_parse_string = "CdAdbdd";
140 auth_gen_string = "CdBBAAABd";
143 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
144 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
146 if (!msrpc_parse(ntlmssp_state,
147 &in, chal_parse_string,
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;
160 ntlmssp_state->server_domain = server_domain;
162 if (challenge_blob.length != 8) {
163 return NT_STATUS_INVALID_PARAMETER;
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));
174 user = cli_credentials_get_username(gensec_security->credentials);
175 domain = cli_credentials_get_domain(gensec_security->credentials);
178 password = cli_credentials_get_password(gensec_security->credentials);
181 static const uint8_t zeros[16];
182 /* do nothing - blobs are zero length */
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);
188 /* not doing NLTM2 without a password */
189 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
190 } else if (ntlmssp_state->use_ntlmv2) {
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;
198 /* TODO: if the remote server is standalone, then we should replace 'domain'
199 with the server name as supplied above */
201 if (!SMBNTLMv2encrypt(user,
203 password, &challenge_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;
212 /* LM Key is incompatible... */
213 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
215 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
216 struct MD5Context md5_session_nonce_ctx;
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);
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);
227 memcpy(session_nonce, challenge_blob.data, 8);
228 memcpy(&session_nonce[8], lm_response.data, 8);
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);
235 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
236 DEBUG(5, ("challenge is: \n"));
237 dump_data(5, session_nonce_hash, 8);
239 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
240 SMBNTencrypt(password,
244 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
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);
250 /* LM Key is incompatible... */
251 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
255 if (ntlmssp_state->use_nt_response) {
256 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
257 SMBNTencrypt(password,challenge_blob.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);
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,
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);
274 /* LM Key is incompatible with 'long' passwords */
275 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
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);
282 if (!ntlmssp_state->use_nt_response) {
283 session_key = lm_session_key;
287 /* LM Key is incompatible... */
288 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
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);
299 static const uint8_t zeros[24];
300 SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
301 new_session_key.data);
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);
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));
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);
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));
327 /* this generates the actual auth packet */
328 if (!msrpc_gen(out_mem_ctx,
329 out, auth_gen_string,
332 lm_response.data, lm_response.length,
333 nt_response.data, nt_response.length,
336 cli_credentials_get_workstation(gensec_security->credentials),
337 encrypted_session_key.data, encrypted_session_key.length,
338 ntlmssp_state->neg_flags)) {
340 return NT_STATUS_NO_MEMORY;
343 ntlmssp_state->session_key = session_key;
345 /* The client might be using 56 or 40 bit weakened keys */
346 ntlmssp_weaken_keys(ntlmssp_state);
348 ntlmssp_state->chal = challenge_blob;
349 ntlmssp_state->lm_resp = lm_response;
350 ntlmssp_state->nt_resp = nt_response;
352 ntlmssp_state->expected_state = NTLMSSP_DONE;
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)));
364 static NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx, struct ntlmssp_state **ntlmssp_state)
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;
371 ZERO_STRUCTP(*ntlmssp_state);
373 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
375 (*ntlmssp_state)->get_domain = lp_workgroup;
377 (*ntlmssp_state)->unicode = lp_parm_bool(-1, "ntlmssp_client", "unicode", True);
379 (*ntlmssp_state)->use_nt_response = lp_parm_bool(-1, "ntlmssp_client", "send_nt_reponse", True);
381 (*ntlmssp_state)->allow_lm_key = (lp_lanman_auth()
382 && lp_parm_bool(-1, "ntlmssp_client", "allow_lm_key", False));
384 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
386 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
388 (*ntlmssp_state)->neg_flags =
389 NTLMSSP_NEGOTIATE_NTLM |
390 NTLMSSP_REQUEST_TARGET;
392 if (lp_parm_bool(-1, "ntlmssp_client", "128bit", True)) {
393 (*ntlmssp_state)->neg_flags |= NTLMSSP_NEGOTIATE_128;
396 if (lp_parm_bool(-1, "ntlmssp_client", "keyexchange", True)) {
397 (*ntlmssp_state)->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
400 if (lp_parm_bool(-1, "ntlmssp_client", "ntlm2", True)) {
401 (*ntlmssp_state)->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
403 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
404 (*ntlmssp_state)->use_ntlmv2 = False;
410 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
412 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
415 nt_status = gensec_ntlmssp_start(gensec_security);
416 NT_STATUS_NOT_OK_RETURN(nt_status);
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);
423 if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
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.
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)
433 gensec_ntlmssp_state->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
435 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
436 gensec_ntlmssp_state->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
438 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
439 gensec_ntlmssp_state->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
442 gensec_security->private_data = gensec_ntlmssp_state;