2
Unix SMB/Netbios implementation.
4
handle NLTMSSP, server side
6
Copyright (C) Andrew Tridgell 2001
7
Copyright (C) Andrew Bartlett 2001-2003
8
Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
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.
27
static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
28
DATA_BLOB reply, DATA_BLOB *next_request);
29
static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
30
const DATA_BLOB in, DATA_BLOB *out);
31
static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
32
const DATA_BLOB reply, DATA_BLOB *next_request);
33
static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
34
const DATA_BLOB request, DATA_BLOB *reply);
37
* Callbacks for NTLMSSP - for both client and server operating modes
41
static const struct ntlmssp_callbacks {
42
enum NTLMSSP_ROLE role;
43
enum NTLM_MESSAGE_TYPE ntlmssp_command;
44
NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
45
DATA_BLOB in, DATA_BLOB *out);
46
} ntlmssp_callbacks[] = {
47
{NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
48
{NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
49
{NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
50
{NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
51
{NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
52
{NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
57
* Print out the NTLMSSP flags for debugging
58
* @param neg_flags The flags from the packet
61
void debug_ntlmssp_flags(uint32 neg_flags)
63
DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
65
if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
66
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
67
if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
68
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
69
if (neg_flags & NTLMSSP_REQUEST_TARGET)
70
DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
71
if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
72
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
73
if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
74
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
75
if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM_STYLE)
76
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM_STYLE\n"));
77
if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
78
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
79
if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
80
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
81
if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
82
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
83
if (neg_flags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED)
84
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED\n"));
85
if (neg_flags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED)
86
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED\n"));
87
if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
88
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
89
if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
90
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
91
if (neg_flags & NTLMSSP_CHAL_ACCEPT_RESPONSE)
92
DEBUGADD(4, (" NTLMSSP_CHAL_ACCEPT_RESPONSE\n"));
93
if (neg_flags & NTLMSSP_CHAL_NON_NT_SESSION_KEY)
94
DEBUGADD(4, (" NTLMSSP_CHAL_NON_NT_SESSION_KEY\n"));
95
if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
96
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
97
if (neg_flags & NTLMSSP_CHAL_TARGET_INFO)
98
DEBUGADD(4, (" NTLMSSP_CHAL_TARGET_INFO\n"));
99
if (neg_flags & NTLMSSP_NEGOTIATE_128)
100
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
101
if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
102
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
103
if (neg_flags & NTLMSSP_NEGOTIATE_56)
104
DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
108
* Default challenge generation code.
112
static const uint8 *get_challenge(const struct ntlmssp_state *ntlmssp_state)
114
static uchar chal[8];
115
generate_random_buffer(chal, sizeof(chal));
121
* Default 'we can set the challenge to anything we like' implementation
125
static BOOL may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
131
* Default 'we can set the challenge to anything we like' implementation
133
* Does not actually do anything, as the value is always in the structure anyway.
137
static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
139
SMB_ASSERT(challenge->length == 8);
144
* Set a username on an NTLMSSP context - ensures it is talloc()ed
148
NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user)
150
ntlmssp_state->user = talloc_strdup(ntlmssp_state->mem_ctx, user ? user : "" );
151
if (!ntlmssp_state->user) {
152
return NT_STATUS_NO_MEMORY;
158
* Set a password on an NTLMSSP context - ensures it is talloc()ed
161
NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password)
164
ntlmssp_state->password = NULL;
166
ntlmssp_state->password = talloc_strdup(ntlmssp_state->mem_ctx, password);
167
if (!ntlmssp_state->password) {
168
return NT_STATUS_NO_MEMORY;
175
* Set a domain on an NTLMSSP context - ensures it is talloc()ed
178
NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
180
ntlmssp_state->domain = talloc_strdup(ntlmssp_state->mem_ctx, domain ? domain : "" );
181
if (!ntlmssp_state->domain) {
182
return NT_STATUS_NO_MEMORY;
188
* Set a workstation on an NTLMSSP context - ensures it is talloc()ed
191
NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation)
193
ntlmssp_state->workstation = talloc_strdup(ntlmssp_state->mem_ctx, workstation);
194
if (!ntlmssp_state->workstation) {
195
return NT_STATUS_NO_MEMORY;
201
* Store a DATA_BLOB containing an NTLMSSP response, for use later.
202
* This copies the data blob
205
NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
208
ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state->mem_ctx,
209
response.data, response.length);
214
* Next state function for the NTLMSSP state machine
216
* @param ntlmssp_state NTLMSSP State
217
* @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
218
* @param out The reply, as an allocated DATA_BLOB, caller to free.
219
* @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
222
NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
223
const DATA_BLOB in, DATA_BLOB *out)
226
uint32 ntlmssp_command;
229
if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
230
/* Called update after negotiations finished. */
231
DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
232
return NT_STATUS_INVALID_PARAMETER;
235
*out = data_blob(NULL, 0);
237
if (!in.length && ntlmssp_state->stored_response.length) {
238
input = ntlmssp_state->stored_response;
240
/* we only want to read the stored response once - overwrite it */
241
ntlmssp_state->stored_response = data_blob(NULL, 0);
247
switch (ntlmssp_state->role) {
249
ntlmssp_command = NTLMSSP_INITIAL;
252
/* 'datagram' mode - no neg packet */
253
ntlmssp_command = NTLMSSP_NEGOTIATE;
257
if (!msrpc_parse(&input, "Cd",
260
DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
261
dump_data(2, (const char *)input.data, input.length);
262
return NT_STATUS_INVALID_PARAMETER;
266
if (ntlmssp_command != ntlmssp_state->expected_state) {
267
DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
268
return NT_STATUS_INVALID_PARAMETER;
271
for (i=0; ntlmssp_callbacks[i].fn; i++) {
272
if (ntlmssp_callbacks[i].role == ntlmssp_state->role
273
&& ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
274
return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
278
DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
279
ntlmssp_state->role, ntlmssp_command));
281
return NT_STATUS_INVALID_PARAMETER;
285
* End an NTLMSSP state machine
287
* @param ntlmssp_state NTLMSSP State, free()ed by this function
290
void ntlmssp_end(NTLMSSP_STATE **ntlmssp_state)
292
TALLOC_CTX *mem_ctx = (*ntlmssp_state)->mem_ctx;
294
(*ntlmssp_state)->ref_count--;
296
if ((*ntlmssp_state)->ref_count == 0) {
297
data_blob_free(&(*ntlmssp_state)->chal);
298
data_blob_free(&(*ntlmssp_state)->lm_resp);
299
data_blob_free(&(*ntlmssp_state)->nt_resp);
301
talloc_destroy(mem_ctx);
304
*ntlmssp_state = NULL;
309
* Determine correct target name flags for reply, given server role
310
* and negotiated flags
312
* @param ntlmssp_state NTLMSSP State
313
* @param neg_flags The flags from the packet
314
* @param chal_flags The flags to be set in the reply packet
315
* @return The 'target name' string.
318
static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
319
uint32 neg_flags, uint32 *chal_flags)
321
if (neg_flags & NTLMSSP_REQUEST_TARGET) {
322
*chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
323
*chal_flags |= NTLMSSP_REQUEST_TARGET;
324
if (ntlmssp_state->server_role == ROLE_STANDALONE) {
325
*chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
326
return ntlmssp_state->get_global_myname();
328
*chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
329
return ntlmssp_state->get_domain();
336
static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
337
uint32 neg_flags, BOOL allow_lm) {
338
if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
339
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
340
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
341
ntlmssp_state->unicode = True;
343
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
344
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
345
ntlmssp_state->unicode = False;
348
if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
349
/* other end forcing us to use LM */
350
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
351
ntlmssp_state->use_ntlmv2 = False;
353
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
356
if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) {
357
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
360
if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
361
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
364
if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
365
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
368
if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
369
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
372
if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
373
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
376
if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
377
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
380
if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
381
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
384
/* Woop Woop - unknown flag for Windows compatibility...
385
What does this really do ? JRA. */
386
if (!(neg_flags & NTLMSSP_UNKNOWN_02000000)) {
387
ntlmssp_state->neg_flags &= ~NTLMSSP_UNKNOWN_02000000;
390
if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
391
ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
396
Weaken NTLMSSP keys to cope with down-level clients and servers.
398
We probably should have some parameters to control this, but as
399
it only occours for LM_KEY connections, and this is controlled
400
by the client lanman auth/lanman auth parameters, it isn't too bad.
403
DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
405
DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
406
ntlmssp_state->session_key.data,
407
ntlmssp_state->session_key.length);
409
/* Nothing to weaken. We certainly don't want to 'extend' the length... */
410
if (weakened_key.length < 16) {
411
/* perhaps there was no key? */
415
/* Key weakening not performed on the master key for NTLM2
416
and does not occour for NTLM1. Therefore we only need
417
to do this for the LM_KEY.
420
if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
421
/* LM key doesn't support 128 bit crypto, so this is
422
* the best we can do. If you negotiate 128 bit, but
423
* not 56, you end up with 40 bit... */
424
if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
425
weakened_key.data[7] = 0xa0;
426
} else { /* forty bits */
427
weakened_key.data[5] = 0xe5;
428
weakened_key.data[6] = 0x38;
429
weakened_key.data[7] = 0xb0;
431
weakened_key.length = 8;
437
* Next state function for the Negotiate packet
439
* @param ntlmssp_state NTLMSSP State
440
* @param request The request, as a DATA_BLOB
441
* @param request The reply, as an allocated DATA_BLOB, caller to free.
442
* @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
445
static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
446
const DATA_BLOB request, DATA_BLOB *reply)
448
DATA_BLOB struct_blob;
449
fstring dnsname, dnsdomname;
450
uint32 neg_flags = 0;
451
uint32 ntlmssp_command, chal_flags;
452
const uint8 *cryptkey;
453
const char *target_name;
455
/* parse the NTLMSSP packet */
457
file_save("ntlmssp_negotiate.dat", request.data, request.length);
460
if (request.length) {
461
if ((request.length < 16) || !msrpc_parse(&request, "Cdd",
465
DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
466
(unsigned int)request.length));
467
dump_data(2, (const char *)request.data, request.length);
468
return NT_STATUS_INVALID_PARAMETER;
470
debug_ntlmssp_flags(neg_flags);
473
ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
475
/* Ask our caller what challenge they would like in the packet */
476
cryptkey = ntlmssp_state->get_challenge(ntlmssp_state);
478
/* Check if we may set the challenge */
479
if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
480
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
483
/* The flags we send back are not just the negotiated flags,
484
* they are also 'what is in this packet'. Therfore, we
485
* operate on 'chal_flags' from here on
488
chal_flags = ntlmssp_state->neg_flags;
490
/* get the right name to fill in as 'target' */
491
target_name = ntlmssp_target_name(ntlmssp_state,
492
neg_flags, &chal_flags);
493
if (target_name == NULL)
494
return NT_STATUS_INVALID_PARAMETER;
496
ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
497
ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
500
/* This should be a 'netbios domain -> DNS domain' mapping */
501
dnsdomname[0] = '\0';
502
get_mydnsdomname(dnsdomname);
503
strlower_m(dnsdomname);
506
get_mydnsfullname(dnsname);
508
/* This creates the 'blob' of names that appears at the end of the packet */
509
if (chal_flags & NTLMSSP_CHAL_TARGET_INFO)
511
msrpc_gen(&struct_blob, "aaaaa",
512
NTLMSSP_NAME_TYPE_DOMAIN, target_name,
513
NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
514
NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
515
NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
518
struct_blob = data_blob(NULL, 0);
522
/* Marshel the packet in the right format, be it unicode or ASCII */
523
const char *gen_string;
524
if (ntlmssp_state->unicode) {
525
gen_string = "CdUdbddB";
527
gen_string = "CdAdbddB";
530
msrpc_gen(reply, gen_string,
537
struct_blob.data, struct_blob.length);
540
data_blob_free(&struct_blob);
542
ntlmssp_state->expected_state = NTLMSSP_AUTH;
544
return NT_STATUS_MORE_PROCESSING_REQUIRED;
548
* Next state function for the Authenticate packet
550
* @param ntlmssp_state NTLMSSP State
551
* @param request The request, as a DATA_BLOB
552
* @param request The reply, as an allocated DATA_BLOB, caller to free.
553
* @return Errors or NT_STATUS_OK.
556
static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
557
const DATA_BLOB request, DATA_BLOB *reply)
559
DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
560
DATA_BLOB user_session_key = data_blob(NULL, 0);
561
DATA_BLOB lm_session_key = data_blob(NULL, 0);
562
DATA_BLOB session_key = data_blob(NULL, 0);
563
uint32 ntlmssp_command, auth_flags;
564
NTSTATUS nt_status = NT_STATUS_OK;
567
BOOL doing_ntlm2 = False;
569
uchar session_nonce[16];
570
uchar session_nonce_hash[16];
572
const char *parse_string;
575
char *workstation = NULL;
577
/* parse the NTLMSSP packet */
578
*reply = data_blob(NULL, 0);
581
file_save("ntlmssp_auth.dat", request.data, request.length);
584
if (ntlmssp_state->unicode) {
585
parse_string = "CdBBUUUBd";
587
parse_string = "CdBBAAABd";
590
data_blob_free(&ntlmssp_state->lm_resp);
591
data_blob_free(&ntlmssp_state->nt_resp);
593
ntlmssp_state->user = NULL;
594
ntlmssp_state->domain = NULL;
595
ntlmssp_state->workstation = NULL;
597
/* now the NTLMSSP encoded auth hashes */
598
if (!msrpc_parse(&request, parse_string,
601
&ntlmssp_state->lm_resp,
602
&ntlmssp_state->nt_resp,
606
&encrypted_session_key,
610
SAFE_FREE(workstation);
611
data_blob_free(&encrypted_session_key);
614
/* Try again with a shorter string (Win9X truncates this packet) */
615
if (ntlmssp_state->unicode) {
616
parse_string = "CdBBUUU";
618
parse_string = "CdBBAAA";
621
/* now the NTLMSSP encoded auth hashes */
622
if (!msrpc_parse(&request, parse_string,
625
&ntlmssp_state->lm_resp,
626
&ntlmssp_state->nt_resp,
630
DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
631
dump_data(2, (const char *)request.data, request.length);
634
SAFE_FREE(workstation);
636
return NT_STATUS_INVALID_PARAMETER;
641
ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
643
if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
646
SAFE_FREE(workstation);
647
data_blob_free(&encrypted_session_key);
651
if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
654
SAFE_FREE(workstation);
655
data_blob_free(&encrypted_session_key);
659
if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
662
SAFE_FREE(workstation);
663
data_blob_free(&encrypted_session_key);
669
SAFE_FREE(workstation);
671
DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
672
ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
675
file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
676
file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
679
/* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
682
However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
684
if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
685
if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
686
struct MD5Context md5_session_nonce_ctx;
687
SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
691
memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
692
memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
694
MD5Init(&md5_session_nonce_ctx);
695
MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
696
MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
698
ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, session_nonce_hash, 8);
700
/* LM response is no longer useful */
701
data_blob_free(&ntlmssp_state->lm_resp);
703
/* We changed the effective challenge - set it */
704
if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
705
data_blob_free(&encrypted_session_key);
709
/* LM Key is incompatible. */
710
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
715
* Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
716
* is required (by "ntlm auth = no" and "lm auth = no" being set in the
717
* smb.conf file) and no NTLMv2 response was sent then the password check
718
* will fail here. JRA.
721
/* Finally, actually ask if the password is OK */
723
if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
724
&user_session_key, &lm_session_key))) {
725
data_blob_free(&encrypted_session_key);
729
dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
730
dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
732
/* Handle the different session key derivation for NTLM2 */
734
if (user_session_key.data && user_session_key.length == 16) {
735
session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
736
hmac_md5(user_session_key.data, session_nonce,
737
sizeof(session_nonce), session_key.data);
738
DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
739
dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
742
DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
743
session_key = data_blob(NULL, 0);
745
} else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
746
if (lm_session_key.data && lm_session_key.length >= 8) {
747
if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
748
session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
749
SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
751
DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
752
dump_data_pw("LM session key:\n", session_key.data, session_key.length);
754
/* use the key unmodified - it's
755
* probably a NULL key from the guest
757
session_key = lm_session_key;
760
DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
761
session_key = data_blob(NULL, 0);
763
} else if (user_session_key.data) {
764
session_key = user_session_key;
765
DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
766
dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
767
} else if (lm_session_key.data) {
768
session_key = lm_session_key;
769
DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
770
dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
772
DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
773
session_key = data_blob(NULL, 0);
776
/* With KEY_EXCH, the client supplies the proposed session key,
777
but encrypts it with the long-term key */
778
if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
779
if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
780
data_blob_free(&encrypted_session_key);
781
DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
782
(unsigned int)encrypted_session_key.length));
783
return NT_STATUS_INVALID_PARAMETER;
784
} else if (!session_key.data || session_key.length != 16) {
785
DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
786
(unsigned int)session_key.length));
787
ntlmssp_state->session_key = session_key;
789
dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
790
SamOEMhash(encrypted_session_key.data,
792
encrypted_session_key.length);
793
ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state->mem_ctx,
794
encrypted_session_key.data,
795
encrypted_session_key.length);
796
dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
797
encrypted_session_key.length);
800
ntlmssp_state->session_key = session_key;
803
if (!NT_STATUS_IS_OK(nt_status)) {
804
ntlmssp_state->session_key = data_blob(NULL, 0);
805
} else if (ntlmssp_state->session_key.length) {
806
nt_status = ntlmssp_sign_init(ntlmssp_state);
809
data_blob_free(&encrypted_session_key);
811
/* Only one authentication allowed per server state. */
812
ntlmssp_state->expected_state = NTLMSSP_DONE;
818
* Create an NTLMSSP state machine
820
* @param ntlmssp_state NTLMSSP State, allocated by this function
823
NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
827
mem_ctx = talloc_init("NTLMSSP context");
829
*ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
830
if (!*ntlmssp_state) {
831
DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
832
talloc_destroy(mem_ctx);
833
return NT_STATUS_NO_MEMORY;
836
(*ntlmssp_state)->role = NTLMSSP_SERVER;
838
(*ntlmssp_state)->mem_ctx = mem_ctx;
839
(*ntlmssp_state)->get_challenge = get_challenge;
840
(*ntlmssp_state)->set_challenge = set_challenge;
841
(*ntlmssp_state)->may_set_challenge = may_set_challenge;
843
(*ntlmssp_state)->get_global_myname = global_myname;
844
(*ntlmssp_state)->get_domain = lp_workgroup;
845
(*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
847
(*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
849
(*ntlmssp_state)->ref_count = 1;
851
(*ntlmssp_state)->neg_flags =
852
NTLMSSP_NEGOTIATE_128 |
853
NTLMSSP_NEGOTIATE_56 |
854
NTLMSSP_UNKNOWN_02000000 |
855
NTLMSSP_NEGOTIATE_NTLM |
856
NTLMSSP_NEGOTIATE_NTLM2 |
857
NTLMSSP_NEGOTIATE_KEY_EXCH |
858
NTLMSSP_NEGOTIATE_SIGN |
859
NTLMSSP_NEGOTIATE_SEAL;
864
/*********************************************************************
866
*********************************************************************/
869
* Next state function for the Initial packet
871
* @param ntlmssp_state NTLMSSP State
872
* @param request The request, as a DATA_BLOB. reply.data must be NULL
873
* @param request The reply, as an allocated DATA_BLOB, caller to free.
874
* @return Errors or NT_STATUS_OK.
877
static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
878
DATA_BLOB reply, DATA_BLOB *next_request)
880
if (ntlmssp_state->unicode) {
881
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
883
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
886
if (ntlmssp_state->use_ntlmv2) {
887
ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
890
/* generate the ntlmssp negotiate packet */
891
msrpc_gen(next_request, "CddAA",
894
ntlmssp_state->neg_flags,
895
ntlmssp_state->get_domain(),
896
ntlmssp_state->get_global_myname());
898
ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
900
return NT_STATUS_MORE_PROCESSING_REQUIRED;
904
* Next state function for the Challenge Packet. Generate an auth packet.
906
* @param ntlmssp_state NTLMSSP State
907
* @param request The request, as a DATA_BLOB. reply.data must be NULL
908
* @param request The reply, as an allocated DATA_BLOB, caller to free.
909
* @return Errors or NT_STATUS_OK.
912
static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
913
const DATA_BLOB reply, DATA_BLOB *next_request)
915
uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
916
DATA_BLOB server_domain_blob;
917
DATA_BLOB challenge_blob;
918
DATA_BLOB struct_blob = data_blob(NULL, 0);
920
const char *chal_parse_string;
921
const char *auth_gen_string;
922
DATA_BLOB lm_response = data_blob(NULL, 0);
923
DATA_BLOB nt_response = data_blob(NULL, 0);
924
DATA_BLOB session_key = data_blob(NULL, 0);
925
DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
926
NTSTATUS nt_status = NT_STATUS_OK;
928
if (!msrpc_parse(&reply, "CdBd",
933
DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
934
dump_data(2, (const char *)reply.data, reply.length);
936
return NT_STATUS_INVALID_PARAMETER;
939
data_blob_free(&server_domain_blob);
941
DEBUG(3, ("Got challenge flags:\n"));
942
debug_ntlmssp_flags(chal_flags);
944
ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
946
if (ntlmssp_state->unicode) {
947
if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
948
chal_parse_string = "CdUdbddB";
950
chal_parse_string = "CdUdbdd";
952
auth_gen_string = "CdBBUUUBd";
954
if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
955
chal_parse_string = "CdAdbddB";
957
chal_parse_string = "CdAdbdd";
960
auth_gen_string = "CdBBAAABd";
963
DEBUG(3, ("NTLMSSP: Set final flags:\n"));
964
debug_ntlmssp_flags(ntlmssp_state->neg_flags);
966
if (!msrpc_parse(&reply, chal_parse_string,
974
DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
975
dump_data(2, (const char *)reply.data, reply.length);
976
return NT_STATUS_INVALID_PARAMETER;
979
ntlmssp_state->server_domain = talloc_strdup(ntlmssp_state->mem_ctx,
982
SAFE_FREE(server_domain);
983
if (challenge_blob.length != 8) {
984
data_blob_free(&struct_blob);
985
return NT_STATUS_INVALID_PARAMETER;
988
if (!ntlmssp_state->password) {
989
static const uchar zeros[16];
990
/* do nothing - blobs are zero length */
992
/* session key is all zeros */
993
session_key = data_blob_talloc(ntlmssp_state->mem_ctx, zeros, 16);
995
/* not doing NLTM2 without a password */
996
ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
997
} else if (ntlmssp_state->use_ntlmv2) {
999
if (!struct_blob.length) {
1000
/* be lazy, match win2k - we can't do NTLMv2 without it */
1001
DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1002
return NT_STATUS_INVALID_PARAMETER;
1005
/* TODO: if the remote server is standalone, then we should replace 'domain'
1006
with the server name as supplied above */
1008
if (!SMBNTLMv2encrypt(ntlmssp_state->user,
1009
ntlmssp_state->domain,
1010
ntlmssp_state->password, &challenge_blob,
1012
&lm_response, &nt_response, &session_key)) {
1013
data_blob_free(&challenge_blob);
1014
data_blob_free(&struct_blob);
1015
return NT_STATUS_NO_MEMORY;
1017
} else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1018
struct MD5Context md5_session_nonce_ctx;
1020
uchar session_nonce[16];
1021
uchar session_nonce_hash[16];
1022
uchar user_session_key[16];
1023
E_md4hash(ntlmssp_state->password, nt_hash);
1025
lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1026
generate_random_buffer(lm_response.data, 8);
1027
memset(lm_response.data+8, 0, 16);
1029
memcpy(session_nonce, challenge_blob.data, 8);
1030
memcpy(&session_nonce[8], lm_response.data, 8);
1032
MD5Init(&md5_session_nonce_ctx);
1033
MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1034
MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1035
MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1037
DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1038
DEBUG(5, ("challenge is: \n"));
1039
dump_data(5, (const char *)session_nonce_hash, 8);
1041
nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1042
SMBNTencrypt(ntlmssp_state->password,
1046
session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1048
SMBsesskeygen_ntv1(nt_hash, NULL, user_session_key);
1049
hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1050
dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1054
E_deshash(ntlmssp_state->password, lm_hash);
1055
E_md4hash(ntlmssp_state->password, nt_hash);
1057
/* lanman auth is insecure, it may be disabled */
1058
if (lp_client_lanman_auth()) {
1059
lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1060
SMBencrypt(ntlmssp_state->password,challenge_blob.data,
1064
nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1065
SMBNTencrypt(ntlmssp_state->password,challenge_blob.data,
1068
session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1069
if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1070
&& lp_client_lanman_auth()) {
1071
SMBsesskeygen_lm_sess_key(lm_hash, lm_response.data,
1073
dump_data_pw("LM session key\n", session_key.data, session_key.length);
1075
SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1076
dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1079
data_blob_free(&struct_blob);
1081
/* Key exchange encryptes a new client-generated session key with
1082
the password-derived key */
1083
if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1084
/* Make up a new session key */
1085
uint8 client_session_key[16];
1086
generate_random_buffer(client_session_key, sizeof(client_session_key));
1088
/* Encrypt the new session key with the old one */
1089
encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1090
dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1091
SamOEMhash(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
1092
dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1094
/* Mark the new session key as the 'real' session key */
1095
data_blob_free(&session_key);
1096
session_key = data_blob_talloc(ntlmssp_state->mem_ctx, client_session_key, sizeof(client_session_key));
1099
/* this generates the actual auth packet */
1100
if (!msrpc_gen(next_request, auth_gen_string,
1103
lm_response.data, lm_response.length,
1104
nt_response.data, nt_response.length,
1105
ntlmssp_state->domain,
1106
ntlmssp_state->user,
1107
ntlmssp_state->get_global_myname(),
1108
encrypted_session_key.data, encrypted_session_key.length,
1109
ntlmssp_state->neg_flags)) {
1111
return NT_STATUS_NO_MEMORY;
1114
data_blob_free(&encrypted_session_key);
1116
data_blob_free(&ntlmssp_state->chal);
1118
ntlmssp_state->session_key = session_key;
1120
ntlmssp_state->chal = challenge_blob;
1121
ntlmssp_state->lm_resp = lm_response;
1122
ntlmssp_state->nt_resp = nt_response;
1124
ntlmssp_state->expected_state = NTLMSSP_DONE;
1126
if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1127
DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1133
NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
1135
TALLOC_CTX *mem_ctx;
1137
mem_ctx = talloc_init("NTLMSSP Client context");
1139
*ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
1140
if (!*ntlmssp_state) {
1141
DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1142
talloc_destroy(mem_ctx);
1143
return NT_STATUS_NO_MEMORY;
1146
(*ntlmssp_state)->role = NTLMSSP_CLIENT;
1148
(*ntlmssp_state)->mem_ctx = mem_ctx;
1150
(*ntlmssp_state)->get_global_myname = global_myname;
1151
(*ntlmssp_state)->get_domain = lp_workgroup;
1153
(*ntlmssp_state)->unicode = True;
1155
(*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1157
(*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1159
(*ntlmssp_state)->ref_count = 1;
1161
(*ntlmssp_state)->neg_flags =
1162
NTLMSSP_NEGOTIATE_128 |
1163
NTLMSSP_NEGOTIATE_NTLM |
1164
NTLMSSP_NEGOTIATE_NTLM2 |
1165
NTLMSSP_NEGOTIATE_KEY_EXCH |
1167
* We need to set this to allow a later SetPassword
1168
* via the SAMR pipe to succeed. Strange.... We could
1169
* also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
1171
NTLMSSP_NEGOTIATE_SIGN |
1172
NTLMSSP_REQUEST_TARGET;
1174
return NT_STATUS_OK;