Install public header files again and include required prototypes.
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "auth/gensec/gensec.h"
22 #include "auth/gensec/gensec_proto.h"
23 #include "auth/ntlmssp/ntlmssp.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "torture/torture.h"
26
27 static bool torture_ntlmssp_self_check(struct torture_context *tctx)
28 {
29         struct gensec_security *gensec_security;
30         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
31         DATA_BLOB data;
32         DATA_BLOB sig, expected_sig;
33         TALLOC_CTX *mem_ctx = tctx;
34
35         torture_assert_ntstatus_ok(tctx, 
36                 gensec_client_start(mem_ctx, &gensec_security, NULL, tctx->lp_ctx),
37                 "gensec client start");
38
39         gensec_set_credentials(gensec_security, cmdline_credentials);
40
41         gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
42         gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
43
44         torture_assert_ntstatus_ok(tctx, 
45                         gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
46                         "Failed to start GENSEC for NTLMSSP");
47
48         gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
49
50         gensec_ntlmssp_state->session_key = strhex_to_data_blob("0102030405060708090a0b0c0d0e0f00");
51         dump_data_pw("NTLMSSP session key: \n", 
52                      gensec_ntlmssp_state->session_key.data,  
53                      gensec_ntlmssp_state->session_key.length);
54
55         gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_NTLM2;
56
57         torture_assert_ntstatus_ok(tctx,  
58                 ntlmssp_sign_init(gensec_ntlmssp_state),
59                 "Failed to sign_init");
60
61         data = strhex_to_data_blob("6a43494653");
62         gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
63                                    data.data, data.length, data.data, data.length, &sig);
64
65         expected_sig = strhex_to_data_blob("01000000e37f97f2544f4d7e00000000");
66
67         dump_data_pw("NTLMSSP calc sig:     ", sig.data, sig.length);
68         dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
69
70         torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
71
72         torture_assert(tctx, 0 == memcmp(sig.data, expected_sig.data, sig.length),
73                                    "data mismatch");
74
75         torture_assert_ntstatus_equal(tctx, 
76                                       gensec_ntlmssp_check_packet(gensec_security, gensec_security,
77                                                                   data.data, data.length, data.data, data.length, &sig),
78                                       NT_STATUS_ACCESS_DENIED, "Check of just signed packet (should fail, wrong end)");
79
80         gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
81
82         torture_assert_ntstatus_equal(tctx, 
83                                       gensec_ntlmssp_check_packet(gensec_security, gensec_security,
84                                                                   data.data, data.length, data.data, data.length, &sig),
85                                       NT_STATUS_NO_USER_SESSION_KEY, "Check of just signed packet without a session key should fail");
86
87         talloc_free(gensec_security);
88
89         torture_assert_ntstatus_ok(tctx, 
90                 gensec_client_start(mem_ctx, &gensec_security, NULL, tctx->lp_ctx),
91                 "Failed to start GENSEC for NTLMSSP");
92
93         gensec_set_credentials(gensec_security, cmdline_credentials);
94
95         gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
96         gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
97
98         torture_assert_ntstatus_ok(tctx, 
99                 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
100                 "GENSEC start mech by oid");
101
102         gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
103
104         gensec_ntlmssp_state->session_key = strhex_to_data_blob("0102030405e538b0");
105         dump_data_pw("NTLMSSP session key: \n", 
106                      gensec_ntlmssp_state->session_key.data,  
107                      gensec_ntlmssp_state->session_key.length);
108
109         gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_KEY_EXCH;
110
111         torture_assert_ntstatus_ok(tctx,  
112                 ntlmssp_sign_init(gensec_ntlmssp_state),
113                 "Failed to sign_init");
114
115         data = strhex_to_data_blob("6a43494653");
116         gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
117                             data.data, data.length, data.data, data.length, &sig);
118
119         expected_sig = strhex_to_data_blob("0100000078010900397420fe0e5a0f89");
120
121         dump_data_pw("NTLMSSP calc sig:     ", sig.data, sig.length);
122         dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
123
124         torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
125
126         torture_assert(tctx,  0 == memcmp(sig.data+8, expected_sig.data+8, sig.length-8),
127                                    "data mismatch");
128
129         torture_assert_ntstatus_equal(tctx, 
130                                       gensec_ntlmssp_check_packet(gensec_security, gensec_security,
131                                                                   data.data, data.length, data.data, data.length, &sig),
132                                       NT_STATUS_ACCESS_DENIED, "Check of just signed packet (should fail, wrong end)");
133
134         sig.length /= 2;
135
136         torture_assert_ntstatus_equal(tctx, 
137                                       gensec_ntlmssp_check_packet(gensec_security, gensec_security,
138                                                                   data.data, data.length, data.data, data.length, &sig),
139                                       NT_STATUS_ACCESS_DENIED, "Check of just signed packet with short sig");
140
141         talloc_free(gensec_security);
142         return true;
143 }
144
145 struct torture_suite *torture_ntlmssp(TALLOC_CTX *mem_ctx)
146 {
147         struct torture_suite *suite = torture_suite_create(mem_ctx, 
148                                                                                                            "NTLMSSP");
149
150         torture_suite_add_simple_test(suite, "NTLMSSP self check",
151                                                                    torture_ntlmssp_self_check);
152
153         return suite;
154 }