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 3 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, see <http://www.gnu.org/licenses/>.
24 struct auth_session_info
;
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "../lib/crypto/crypto.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/gensec/gensec.h"
32 #include "param/param.h"
33 #include "auth/ntlmssp/ntlmssp_private.h"
34 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
35 #include "../auth/ntlmssp/ntlmssp_ndr.h"
37 /*********************************************************************
39 *********************************************************************/
42 * Next state function for the Initial packet
44 * @param ntlmssp_state NTLMSSP State
45 * @param out_mem_ctx The DATA_BLOB *out will be allocated on this context
46 * @param in A NULL data blob (input ignored)
47 * @param out The initial negotiate request to the server, as an talloc()ed DATA_BLOB, on out_mem_ctx
48 * @return Errors or NT_STATUS_OK.
51 NTSTATUS
ntlmssp_client_initial(struct gensec_security
*gensec_security
,
52 TALLOC_CTX
*out_mem_ctx
,
53 DATA_BLOB in
, DATA_BLOB
*out
)
55 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
56 talloc_get_type_abort(gensec_security
->private_data
,
57 struct gensec_ntlmssp_context
);
58 struct ntlmssp_state
*ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
59 const char *domain
= ntlmssp_state
->client
.netbios_domain
;
60 const char *workstation
= ntlmssp_state
->client
.netbios_name
;
63 /* These don't really matter in the initial packet, so don't panic if they are not set */
72 if (ntlmssp_state
->unicode
) {
73 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_UNICODE
;
75 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_OEM
;
78 if (ntlmssp_state
->use_ntlmv2
) {
79 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_NTLM2
;
82 /* generate the ntlmssp negotiate packet */
83 status
= msrpc_gen(out_mem_ctx
,
87 ntlmssp_state
->neg_flags
,
91 if (!NT_STATUS_IS_OK(status
)) {
92 DEBUG(0, ("ntlmssp_client_initial: failed to generate "
93 "ntlmssp negotiate packet\n"));
97 if (DEBUGLEVEL
>= 10) {
98 struct NEGOTIATE_MESSAGE
*negotiate
= talloc(
99 ntlmssp_state
, struct NEGOTIATE_MESSAGE
);
100 if (negotiate
!= NULL
) {
101 status
= ntlmssp_pull_NEGOTIATE_MESSAGE(
102 out
, negotiate
, negotiate
);
103 if (NT_STATUS_IS_OK(status
)) {
104 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE
,
107 TALLOC_FREE(negotiate
);
111 ntlmssp_state
->expected_state
= NTLMSSP_CHALLENGE
;
113 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
117 * Next state function for the Challenge Packet. Generate an auth packet.
119 * @param gensec_security GENSEC state
120 * @param out_mem_ctx Memory context for *out
121 * @param in The server challnege, as a DATA_BLOB. reply.data must be NULL
122 * @param out The next request (auth packet) to the server, as an allocated DATA_BLOB, on the out_mem_ctx context
123 * @return Errors or NT_STATUS_OK.
126 NTSTATUS
ntlmssp_client_challenge(struct gensec_security
*gensec_security
,
127 TALLOC_CTX
*out_mem_ctx
,
128 const DATA_BLOB in
, DATA_BLOB
*out
)
130 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
131 talloc_get_type_abort(gensec_security
->private_data
,
132 struct gensec_ntlmssp_context
);
133 struct ntlmssp_state
*ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
134 uint32_t chal_flags
, ntlmssp_command
, unkn1
= 0, unkn2
= 0;
135 DATA_BLOB server_domain_blob
;
136 DATA_BLOB challenge_blob
;
137 DATA_BLOB target_info
= data_blob(NULL
, 0);
139 const char *chal_parse_string
;
140 const char *chal_parse_string_short
= NULL
;
141 const char *auth_gen_string
;
142 DATA_BLOB lm_response
= data_blob(NULL
, 0);
143 DATA_BLOB nt_response
= data_blob(NULL
, 0);
144 DATA_BLOB session_key
= data_blob(NULL
, 0);
145 DATA_BLOB lm_session_key
= data_blob(NULL
, 0);
146 DATA_BLOB encrypted_session_key
= data_blob(NULL
, 0);
149 const char *user
, *domain
;
151 TALLOC_CTX
*mem_ctx
= talloc_new(out_mem_ctx
);
153 return NT_STATUS_NO_MEMORY
;
156 if (!msrpc_parse(mem_ctx
,
162 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
163 dump_data(2, in
.data
, in
.length
);
164 talloc_free(mem_ctx
);
166 return NT_STATUS_INVALID_PARAMETER
;
169 data_blob_free(&server_domain_blob
);
171 DEBUG(3, ("Got challenge flags:\n"));
172 debug_ntlmssp_flags(chal_flags
);
174 ntlmssp_handle_neg_flags(ntlmssp_state
, chal_flags
, ntlmssp_state
->allow_lm_key
);
176 if (ntlmssp_state
->unicode
) {
177 if (chal_flags
& NTLMSSP_NEGOTIATE_TARGET_INFO
) {
178 chal_parse_string
= "CdUdbddB";
180 chal_parse_string
= "CdUdbdd";
181 chal_parse_string_short
= "CdUdb";
183 auth_gen_string
= "CdBBUUUBd";
185 if (chal_flags
& NTLMSSP_NEGOTIATE_TARGET_INFO
) {
186 chal_parse_string
= "CdAdbddB";
188 chal_parse_string
= "CdAdbdd";
189 chal_parse_string_short
= "CdAdb";
192 auth_gen_string
= "CdBBAAABd";
195 if (!msrpc_parse(mem_ctx
,
196 &in
, chal_parse_string
,
207 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
209 if (chal_parse_string_short
!= NULL
) {
211 * In the case where NTLMSSP_NEGOTIATE_TARGET_INFO
212 * is not used, some NTLMSSP servers don't return
213 * the unused unkn1 and unkn2 fields.
215 * https://bugzilla.samba.org/show_bug.cgi?id=10016
217 * Try and parse again without them.
219 ok
= msrpc_parse(mem_ctx
,
220 &in
, chal_parse_string_short
,
227 DEBUG(1, ("Failed to short parse "
228 "the NTLMSSP Challenge: (#2)\n"));
233 dump_data(2, in
.data
, in
.length
);
234 talloc_free(mem_ctx
);
235 return NT_STATUS_INVALID_PARAMETER
;
239 if (chal_flags
& NTLMSSP_TARGET_TYPE_SERVER
) {
240 ntlmssp_state
->server
.is_standalone
= true;
242 ntlmssp_state
->server
.is_standalone
= false;
244 /* TODO: parse struct_blob and fill in the rest */
245 ntlmssp_state
->server
.netbios_name
= "";
246 ntlmssp_state
->server
.netbios_domain
= server_domain
;
247 ntlmssp_state
->server
.dns_name
= "";
248 ntlmssp_state
->server
.dns_domain
= "";
250 if (challenge_blob
.length
!= 8) {
251 talloc_free(mem_ctx
);
252 return NT_STATUS_INVALID_PARAMETER
;
255 cli_credentials_get_ntlm_username_domain(gensec_security
->credentials
, mem_ctx
,
258 if (ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
259 flags
|= CLI_CRED_NTLM2
;
261 if (ntlmssp_state
->use_ntlmv2
) {
262 flags
|= CLI_CRED_NTLMv2_AUTH
;
264 if (ntlmssp_state
->use_nt_response
) {
265 flags
|= CLI_CRED_NTLM_AUTH
;
267 if (lpcfg_client_lanman_auth(gensec_security
->settings
->lp_ctx
)) {
268 flags
|= CLI_CRED_LANMAN_AUTH
;
271 nt_status
= cli_credentials_get_ntlm_response(gensec_security
->credentials
, mem_ctx
,
272 &flags
, challenge_blob
, target_info
,
273 &lm_response
, &nt_response
,
274 &lm_session_key
, &session_key
);
276 if (!NT_STATUS_IS_OK(nt_status
)) {
280 if (!(flags
& CLI_CRED_LANMAN_AUTH
)) {
281 /* LM Key is still possible, just silly, so we do not
282 * allow it. Fortunetly all LM crypto is off by
283 * default and we require command line options to end
285 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
288 if (!(flags
& CLI_CRED_NTLM2
)) {
289 /* NTLM2 is incompatible... */
290 ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_NTLM2
;
293 if ((ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
)
294 && lpcfg_client_lanman_auth(gensec_security
->settings
->lp_ctx
) && lm_session_key
.length
== 16) {
295 DATA_BLOB new_session_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
296 if (lm_response
.length
== 24) {
297 SMBsesskeygen_lm_sess_key(lm_session_key
.data
, lm_response
.data
,
298 new_session_key
.data
);
300 static const uint8_t zeros
[24];
301 SMBsesskeygen_lm_sess_key(lm_session_key
.data
, zeros
,
302 new_session_key
.data
);
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_secret_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(mem_ctx
, client_session_key
, sizeof(client_session_key
));
327 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
328 debug_ntlmssp_flags(ntlmssp_state
->neg_flags
);
330 /* this generates the actual auth packet */
331 nt_status
= msrpc_gen(mem_ctx
,
332 out
, auth_gen_string
,
335 lm_response
.data
, lm_response
.length
,
336 nt_response
.data
, nt_response
.length
,
339 cli_credentials_get_workstation(gensec_security
->credentials
),
340 encrypted_session_key
.data
, encrypted_session_key
.length
,
341 ntlmssp_state
->neg_flags
);
342 if (!NT_STATUS_IS_OK(nt_status
)) {
343 talloc_free(mem_ctx
);
347 ntlmssp_state
->session_key
= session_key
;
348 talloc_steal(ntlmssp_state
, session_key
.data
);
350 talloc_steal(out_mem_ctx
, out
->data
);
352 ntlmssp_state
->expected_state
= NTLMSSP_DONE
;
354 if (gensec_security
->want_features
& (GENSEC_FEATURE_SIGN
|GENSEC_FEATURE_SEAL
)) {
355 nt_status
= ntlmssp_sign_init(ntlmssp_state
);
356 if (!NT_STATUS_IS_OK(nt_status
)) {
357 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n",
358 nt_errstr(nt_status
)));
359 talloc_free(mem_ctx
);
364 talloc_free(mem_ctx
);
368 NTSTATUS
gensec_ntlmssp_client_start(struct gensec_security
*gensec_security
)
370 struct gensec_ntlmssp_context
*gensec_ntlmssp
;
371 struct ntlmssp_state
*ntlmssp_state
;
374 nt_status
= gensec_ntlmssp_start(gensec_security
);
375 NT_STATUS_NOT_OK_RETURN(nt_status
);
378 talloc_get_type_abort(gensec_security
->private_data
,
379 struct gensec_ntlmssp_context
);
381 ntlmssp_state
= talloc_zero(gensec_ntlmssp
,
382 struct ntlmssp_state
);
383 if (!ntlmssp_state
) {
384 return NT_STATUS_NO_MEMORY
;
387 gensec_ntlmssp
->ntlmssp_state
= ntlmssp_state
;
389 ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
391 ntlmssp_state
->role
= NTLMSSP_CLIENT
;
393 ntlmssp_state
->client
.netbios_domain
= lpcfg_workgroup(gensec_security
->settings
->lp_ctx
);
394 ntlmssp_state
->client
.netbios_name
= cli_credentials_get_workstation(gensec_security
->credentials
);
396 ntlmssp_state
->unicode
= gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "unicode", true);
398 ntlmssp_state
->use_nt_response
= gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "send_nt_reponse", true);
400 ntlmssp_state
->allow_lm_key
= (lpcfg_client_lanman_auth(gensec_security
->settings
->lp_ctx
)
401 && (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "allow_lm_key", false)
402 || gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "lm_key", false)));
404 ntlmssp_state
->use_ntlmv2
= lpcfg_client_ntlmv2_auth(gensec_security
->settings
->lp_ctx
);
406 ntlmssp_state
->expected_state
= NTLMSSP_INITIAL
;
408 ntlmssp_state
->neg_flags
=
409 NTLMSSP_NEGOTIATE_NTLM
|
410 NTLMSSP_REQUEST_TARGET
;
412 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "128bit", true)) {
413 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_128
;
416 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "56bit", false)) {
417 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_56
;
420 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "lm_key", false)) {
421 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_LM_KEY
;
424 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "keyexchange", true)) {
425 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_KEY_EXCH
;
428 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "alwayssign", true)) {
429 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_ALWAYS_SIGN
;
432 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_client", "ntlm2", true)) {
433 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_NTLM2
;
435 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
436 ntlmssp_state
->use_ntlmv2
= false;
439 if (gensec_security
->want_features
& GENSEC_FEATURE_SESSION_KEY
) {
441 * We need to set this to allow a later SetPassword
442 * via the SAMR pipe to succeed. Strange.... We could
443 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
445 * Without this, Windows will not create the master key
446 * that it thinks is only used for NTLMSSP signing and
447 * sealing. (It is actually pulled out and used directly)
449 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
451 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
452 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
454 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
455 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
456 ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SEAL
;