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/>.
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"
27 static bool torture_ntlmssp_self_check(struct torture_context *tctx)
29 struct gensec_security *gensec_security;
30 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
32 DATA_BLOB sig, expected_sig;
33 TALLOC_CTX *mem_ctx = tctx;
35 torture_assert_ntstatus_ok(tctx,
36 gensec_client_start(mem_ctx, &gensec_security, NULL, tctx->lp_ctx),
37 "gensec client start");
39 gensec_set_credentials(gensec_security, cmdline_credentials);
41 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
42 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
44 torture_assert_ntstatus_ok(tctx,
45 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
46 "Failed to start GENSEC for NTLMSSP");
48 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
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);
55 gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_NTLM2;
57 torture_assert_ntstatus_ok(tctx,
58 ntlmssp_sign_init(gensec_ntlmssp_state),
59 "Failed to sign_init");
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);
65 expected_sig = strhex_to_data_blob("01000000e37f97f2544f4d7e00000000");
67 dump_data_pw("NTLMSSP calc sig: ", sig.data, sig.length);
68 dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
70 torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
72 torture_assert(tctx, 0 == memcmp(sig.data, expected_sig.data, sig.length),
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)");
80 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
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");
87 talloc_free(gensec_security);
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");
93 gensec_set_credentials(gensec_security, cmdline_credentials);
95 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
96 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
98 torture_assert_ntstatus_ok(tctx,
99 gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
100 "GENSEC start mech by oid");
102 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
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);
109 gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_KEY_EXCH;
111 torture_assert_ntstatus_ok(tctx,
112 ntlmssp_sign_init(gensec_ntlmssp_state),
113 "Failed to sign_init");
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);
119 expected_sig = strhex_to_data_blob("0100000078010900397420fe0e5a0f89");
121 dump_data_pw("NTLMSSP calc sig: ", sig.data, sig.length);
122 dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
124 torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
126 torture_assert(tctx, 0 == memcmp(sig.data+8, expected_sig.data+8, sig.length-8),
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)");
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");
141 talloc_free(gensec_security);
145 struct torture_suite *torture_ntlmssp(TALLOC_CTX *mem_ctx)
147 struct torture_suite *suite = torture_suite_create(mem_ctx,
150 torture_suite_add_simple_test(suite, "NTLMSSP self check",
151 torture_ntlmssp_self_check);