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