6fb07914dd049dc38f2080cae004e51db0172b84
[ab/samba.git/.git] / source4 / torture / auth / ntlmssp.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Small self-tests for the NTLMSSP code
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "auth/auth.h"
23 #include "lib/cmdline/popt_common.h"
24
25 BOOL torture_ntlmssp_self_check(void) 
26 {
27         struct gensec_security *gensec_security;
28         struct ntlmssp_state *ntlmssp_state;
29         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
30         DATA_BLOB data;
31         DATA_BLOB sig, expected_sig;
32         NTSTATUS status;
33
34         status = gensec_client_start(NULL, &gensec_security);
35
36         if (!NT_STATUS_IS_OK(status)) {
37                 return False;
38         }
39
40         gensec_set_credentials(gensec_security, cmdline_credentials);
41
42         status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP);
43
44         if (!NT_STATUS_IS_OK(status)) {
45                 return False;
46         }
47
48         gensec_ntlmssp_state = gensec_security->private_data;
49         ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
50
51         ntlmssp_state->session_key = strhex_to_data_blob("0102030405060708090a0b0c0d0e0f00");
52         dump_data_pw("NTLMSSP session key: \n", 
53                      ntlmssp_state->session_key.data,  
54                      ntlmssp_state->session_key.length);
55
56         ntlmssp_state->server_use_session_keys = True;
57         ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_NTLM2;
58
59         if (!NT_STATUS_IS_OK(status = ntlmssp_sign_init(ntlmssp_state))) {
60                 printf("Failed to sign_init: %s\n", nt_errstr(status));
61                 return False;
62         }
63
64         data = strhex_to_data_blob("6a43494653");
65         gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
66                                    data.data, data.length, data.data, data.length, &sig);
67
68         expected_sig = strhex_to_data_blob("01000000e37f97f2544f4d7e00000000");
69
70         dump_data_pw("NTLMSSP calc sig:     ", sig.data, sig.length);
71         dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
72
73         if (sig.length != expected_sig.length) {
74                 printf("Wrong sig length: %d != %d\n", sig.length, expected_sig.length);
75                 return False;
76         }
77
78         if (memcmp(sig.data, expected_sig.data, sig.length)) {
79                 return False;
80         }
81
82         talloc_free(gensec_security);
83
84         status = gensec_client_start(NULL, &gensec_security);
85
86         if (!NT_STATUS_IS_OK(status)) {
87                 return False;
88         }
89
90         gensec_set_credentials(gensec_security, cmdline_credentials);
91
92         status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP);
93
94         if (!NT_STATUS_IS_OK(status)) {
95                 return False;
96         }
97
98         gensec_ntlmssp_state = gensec_security->private_data;
99         ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
100
101         ntlmssp_state->session_key = strhex_to_data_blob("0102030405e538b0");
102         dump_data_pw("NTLMSSP session key: \n", 
103                      ntlmssp_state->session_key.data,  
104                      ntlmssp_state->session_key.length);
105
106         ntlmssp_state->server_use_session_keys = True;
107         ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_KEY_EXCH;
108
109         if (!NT_STATUS_IS_OK(status = ntlmssp_sign_init(ntlmssp_state))) {
110                 printf("Failed to sign_init: %s\n", nt_errstr(status));
111                 return False;
112         }
113
114         data = strhex_to_data_blob("6a43494653");
115         gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
116                             data.data, data.length, data.data, data.length, &sig);
117
118         expected_sig = strhex_to_data_blob("0100000078010900397420fe0e5a0f89");
119
120         dump_data_pw("NTLMSSP calc sig:     ", sig.data, sig.length);
121         dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
122
123         if (sig.length != expected_sig.length) {
124                 printf("Wrong sig length: %d != %d\n", sig.length, expected_sig.length);
125                 return False;
126         }
127
128         if (memcmp(sig.data+8, expected_sig.data+8, sig.length-8)) {
129                 return False;
130         }
131
132         talloc_free(gensec_security);
133
134         return True;
135 }