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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #include "includes.h"
22 #include "auth/gensec/gensec.h"
23 #include "auth/ntlmssp/ntlmssp.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "torture/torture.h"
26 
torture_ntlmssp_self_check(struct torture_context * tctx)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),
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 = 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 	talloc_free(gensec_security);
76 
77 	torture_assert_ntstatus_ok(tctx,
78 		gensec_client_start(mem_ctx, &gensec_security, NULL),
79 		"Failed to start GENSEC for NTLMSSP");
80 
81 	gensec_set_credentials(gensec_security, cmdline_credentials);
82 
83 	gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
84 	gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
85 
86 	torture_assert_ntstatus_ok(tctx,
87 		gensec_start_mech_by_oid(gensec_security, GENSEC_OID_NTLMSSP),
88 		"GENSEC start mech by oid");
89 
90 	gensec_ntlmssp_state = gensec_security->private_data;
91 
92 	gensec_ntlmssp_state->session_key = strhex_to_data_blob("0102030405e538b0");
93 	dump_data_pw("NTLMSSP session key: \n",
94 		     gensec_ntlmssp_state->session_key.data,
95 		     gensec_ntlmssp_state->session_key.length);
96 
97 	gensec_ntlmssp_state->neg_flags = NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_KEY_EXCH;
98 
99 	torture_assert_ntstatus_ok(tctx,
100 		ntlmssp_sign_init(gensec_ntlmssp_state),
101 		"Failed to sign_init");
102 
103 	data = strhex_to_data_blob("6a43494653");
104 	gensec_ntlmssp_sign_packet(gensec_security, gensec_security,
105 			    data.data, data.length, data.data, data.length, &sig);
106 
107 	expected_sig = strhex_to_data_blob("0100000078010900397420fe0e5a0f89");
108 
109 	dump_data_pw("NTLMSSP calc sig:     ", sig.data, sig.length);
110 	dump_data_pw("NTLMSSP expected sig: ", expected_sig.data, expected_sig.length);
111 
112 	torture_assert_int_equal(tctx, sig.length, expected_sig.length, "Wrong sig length");
113 
114 	torture_assert(tctx,  0 == memcmp(sig.data+8, expected_sig.data+8, sig.length-8),
115 				   "data mismatch");
116 
117 	talloc_free(gensec_security);
118 	return true;
119 }
120 
torture_ntlmssp(TALLOC_CTX * mem_ctx)121 _PUBLIC_ struct torture_suite *torture_ntlmssp(TALLOC_CTX *mem_ctx)
122 {
123 	struct torture_suite *suite = torture_suite_create(mem_ctx,
124 													   "NTLMSSP");
125 
126 	torture_suite_add_simple_test(suite, "NTLMSSP self check",
127 								   torture_ntlmssp_self_check);
128 
129 	return suite;
130 }
131