s4: Let the "setpassword" script finally use the "samdb_set_password" routine
[Samba/aatanasov.git] / source4 / torture / auth / ntlmssp.c
blobe62b150a4ba9e8439558048328d0e33b0d087778
1 /*
2 Unix SMB/CIFS implementation.
3 Small self-tests for the NTLMSSP code
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
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.
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.
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/>.
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 #include "param/param.h"
28 static bool torture_ntlmssp_self_check(struct torture_context *tctx)
30 struct gensec_security *gensec_security;
31 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
32 DATA_BLOB data;
33 DATA_BLOB sig, expected_sig;
34 TALLOC_CTX *mem_ctx = tctx;
36 torture_assert_ntstatus_ok(tctx,
37 gensec_client_start(mem_ctx, &gensec_security,
38 tctx->ev, lp_gensec_settings(tctx, tctx->lp_ctx)),
39 "gensec client start");
41 gensec_set_credentials(gensec_security, cmdline_credentials);
43 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
44 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
46 torture_assert_ntstatus_ok(tctx,
47 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
48 "Failed to start GENSEC for NTLMSSP");
50 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
52 gensec_ntlmssp_state->session_key = strhex_to_data_blob(tctx, "0102030405060708090a0b0c0d0e0f00");
53 dump_data_pw("NTLMSSP session key: \n",
54 gensec_ntlmssp_state->session_key.data,
55 gensec_ntlmssp_state->session_key.length);
57 gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_NTLM2;
59 torture_assert_ntstatus_ok(tctx,
60 ntlmssp_sign_init(gensec_ntlmssp_state),
61 "Failed to sign_init");
63 data = strhex_to_data_blob(tctx, "6a43494653");
64 gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
65 data.data, data.length, data.data, data.length, &sig);
67 expected_sig = strhex_to_data_blob(tctx, "01000000e37f97f2544f4d7e00000000");
69 dump_data_pw("NTLMSSP calc sig: ", sig.data, sig.length);
70 dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
72 torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
74 torture_assert_mem_equal(tctx, sig.data, expected_sig.data, sig.length,
75 "data mismatch");
77 torture_assert_ntstatus_equal(tctx,
78 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
79 data.data, data.length, data.data, data.length, &sig),
80 NT_STATUS_ACCESS_DENIED, "Check of just signed packet (should fail, wrong end)");
82 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
84 torture_assert_ntstatus_equal(tctx,
85 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
86 data.data, data.length, data.data, data.length, &sig),
87 NT_STATUS_NO_USER_SESSION_KEY, "Check of just signed packet without a session key should fail");
89 talloc_free(gensec_security);
91 torture_assert_ntstatus_ok(tctx,
92 gensec_client_start(mem_ctx, &gensec_security,
93 tctx->ev, lp_gensec_settings(tctx, tctx->lp_ctx)),
94 "Failed to start GENSEC for NTLMSSP");
96 gensec_set_credentials(gensec_security, cmdline_credentials);
98 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
99 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
101 torture_assert_ntstatus_ok(tctx,
102 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
103 "GENSEC start mech by oid");
105 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
107 gensec_ntlmssp_state->session_key = strhex_to_data_blob(tctx, "0102030405e538b0");
108 dump_data_pw("NTLMSSP session key: \n",
109 gensec_ntlmssp_state->session_key.data,
110 gensec_ntlmssp_state->session_key.length);
112 gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_KEY_EXCH;
114 torture_assert_ntstatus_ok(tctx,
115 ntlmssp_sign_init(gensec_ntlmssp_state),
116 "Failed to sign_init");
118 data = strhex_to_data_blob(tctx, "6a43494653");
119 gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
120 data.data, data.length, data.data, data.length, &sig);
122 expected_sig = strhex_to_data_blob(tctx, "0100000078010900397420fe0e5a0f89");
124 dump_data_pw("NTLMSSP calc sig: ", sig.data, sig.length);
125 dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
127 torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
129 torture_assert_mem_equal(tctx, sig.data+8, expected_sig.data+8, sig.length-8,
130 "data mismatch");
132 torture_assert_ntstatus_equal(tctx,
133 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
134 data.data, data.length, data.data, data.length, &sig),
135 NT_STATUS_ACCESS_DENIED, "Check of just signed packet (should fail, wrong end)");
137 sig.length /= 2;
139 torture_assert_ntstatus_equal(tctx,
140 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
141 data.data, data.length, data.data, data.length, &sig),
142 NT_STATUS_ACCESS_DENIED, "Check of just signed packet with short sig");
144 talloc_free(gensec_security);
145 return true;
148 struct torture_suite *torture_ntlmssp(TALLOC_CTX *mem_ctx)
150 struct torture_suite *suite = torture_suite_create(mem_ctx,
151 "NTLMSSP");
153 torture_suite_add_simple_test(suite, "NTLMSSP self check",
154 torture_ntlmssp_self_check);
156 return suite;