selftest: use a separate var for printing out sub parts of lines with \r
[Samba/ekacnet.git] / source4 / torture / auth / ntlmssp.c
blobf2cc0a91824e8cc11ed52f847a6c0e041419f7b8
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/ntlmssp/ntlmssp.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "torture/torture.h"
26 static bool torture_ntlmssp_self_check(struct torture_context *tctx)
28 struct gensec_security *gensec_security;
29 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
30 DATA_BLOB data;
31 DATA_BLOB sig, expected_sig;
32 TALLOC_CTX *mem_ctx = tctx;
34 torture_assert_ntstatus_ok(tctx,
35 gensec_client_start(mem_ctx, &gensec_security, NULL, tctx->lp_ctx),
36 "gensec client start");
38 gensec_set_credentials(gensec_security, cmdline_credentials);
40 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
41 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
43 torture_assert_ntstatus_ok(tctx,
44 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
45 "Failed to start GENSEC for NTLMSSP");
47 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
49 gensec_ntlmssp_state->session_key = strhex_to_data_blob("0102030405060708090a0b0c0d0e0f00");
50 dump_data_pw("NTLMSSP session key: \n",
51 gensec_ntlmssp_state->session_key.data,
52 gensec_ntlmssp_state->session_key.length);
54 gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_NTLM2;
56 torture_assert_ntstatus_ok(tctx,
57 ntlmssp_sign_init(gensec_ntlmssp_state),
58 "Failed to sign_init");
60 data = strhex_to_data_blob("6a43494653");
61 gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
62 data.data, data.length, data.data, data.length, &sig);
64 expected_sig = strhex_to_data_blob("01000000e37f97f2544f4d7e00000000");
66 dump_data_pw("NTLMSSP calc sig: ", sig.data, sig.length);
67 dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
69 torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
71 torture_assert(tctx, 0 == memcmp(sig.data, expected_sig.data, sig.length),
72 "data mismatch");
74 torture_assert_ntstatus_equal(tctx,
75 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
76 data.data, data.length, data.data, data.length, &sig),
77 NT_STATUS_ACCESS_DENIED, "Check of just signed packet (should fail, wrong end)");
79 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
81 torture_assert_ntstatus_equal(tctx,
82 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
83 data.data, data.length, data.data, data.length, &sig),
84 NT_STATUS_NO_USER_SESSION_KEY, "Check of just signed packet without a session key should fail");
86 talloc_free(gensec_security);
88 torture_assert_ntstatus_ok(tctx,
89 gensec_client_start(mem_ctx, &gensec_security, NULL, tctx->lp_ctx),
90 "Failed to start GENSEC for NTLMSSP");
92 gensec_set_credentials(gensec_security, cmdline_credentials);
94 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
95 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
97 torture_assert_ntstatus_ok(tctx,
98 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
99 "GENSEC start mech by oid");
101 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
103 gensec_ntlmssp_state->session_key = strhex_to_data_blob("0102030405e538b0");
104 dump_data_pw("NTLMSSP session key: \n",
105 gensec_ntlmssp_state->session_key.data,
106 gensec_ntlmssp_state->session_key.length);
108 gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_KEY_EXCH;
110 torture_assert_ntstatus_ok(tctx,
111 ntlmssp_sign_init(gensec_ntlmssp_state),
112 "Failed to sign_init");
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);
118 expected_sig = strhex_to_data_blob("0100000078010900397420fe0e5a0f89");
120 dump_data_pw("NTLMSSP calc sig: ", sig.data, sig.length);
121 dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
123 torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
125 torture_assert(tctx, 0 == memcmp(sig.data+8, expected_sig.data+8, sig.length-8),
126 "data mismatch");
128 torture_assert_ntstatus_equal(tctx,
129 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
130 data.data, data.length, data.data, data.length, &sig),
131 NT_STATUS_ACCESS_DENIED, "Check of just signed packet (should fail, wrong end)");
133 sig.length /= 2;
135 torture_assert_ntstatus_equal(tctx,
136 gensec_ntlmssp_check_packet(gensec_security, gensec_security,
137 data.data, data.length, data.data, data.length, &sig),
138 NT_STATUS_ACCESS_DENIED, "Check of just signed packet with short sig");
140 talloc_free(gensec_security);
141 return true;
144 _PUBLIC_ struct torture_suite *torture_ntlmssp(TALLOC_CTX *mem_ctx)
146 struct torture_suite *suite = torture_suite_create(mem_ctx,
147 "NTLMSSP");
149 torture_suite_add_simple_test(suite, "NTLMSSP self check",
150 torture_ntlmssp_self_check);
152 return suite;