summaryrefslogtreecommitdiff
path: root/libcli/auth/tests/ntlm_check.c
blob: e87a0a276d4934db709dd73a8b3c8a112414ee48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Unit tests for the ntlm_check password hash check library.
 *
 *  Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * from cmocka.c:
 * These headers or their equivalents should be included prior to
 * including
 * this header file.
 *
 * #include <stdarg.h>
 * #include <stddef.h>
 * #include <setjmp.h>
 *
 * This allows test applications to use custom definitions of C standard
 * library functions and types.
 *
 */

/*
 * Note that the messaging routines (audit_message_send and get_event_server)
 * are not tested by these unit tests.  Currently they are for integration
 * test support, and as such are exercised by the integration tests.
 */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include "includes.h"
#include "../lib/crypto/crypto.h"
#include "librpc/gen_ndr/netlogon.h"
#include "libcli/auth/libcli_auth.h"
#include "auth/credentials/credentials.h"

struct ntlm_state {
	const char *username;
	const char *domain;
	DATA_BLOB challenge;
	DATA_BLOB ntlm;
	DATA_BLOB lm;
	DATA_BLOB ntlm_key;
	DATA_BLOB lm_key;
	const struct samr_Password *nt_hash;
};

static int test_ntlm_setup_with_options(void **state,
					int flags, bool upn)
{
	NTSTATUS status;
	DATA_BLOB challenge = {
		.data = discard_const_p(uint8_t, "I am a teapot"),
		.length = 8
	};
	struct ntlm_state *ntlm_state = talloc(NULL, struct ntlm_state);
	DATA_BLOB target_info = NTLMv2_generate_names_blob(ntlm_state,
							   NULL,
							   "serverdom");
	struct cli_credentials *creds = cli_credentials_init(ntlm_state);
	cli_credentials_set_username(creds,
				     "testuser",
				     CRED_SPECIFIED);
	cli_credentials_set_domain(creds,
				   "testdom",
				   CRED_SPECIFIED);
	cli_credentials_set_workstation(creds,
					"testwksta",
					CRED_SPECIFIED);
	cli_credentials_set_password(creds,
				     "testpass",
				     CRED_SPECIFIED);

	if (upn) {
		cli_credentials_set_principal(creds,
					      "testuser@samba.org",
					      CRED_SPECIFIED);
	}

	cli_credentials_get_ntlm_username_domain(creds,
						 ntlm_state,
						 &ntlm_state->username,
						 &ntlm_state->domain);

	status = cli_credentials_get_ntlm_response(creds,
						   ntlm_state,
						   &flags,
						   challenge,
						   NULL,
						   target_info,
						   &ntlm_state->lm,
						   &ntlm_state->ntlm,
						   &ntlm_state->lm_key,
						   &ntlm_state->ntlm_key);
	ntlm_state->challenge = challenge;

	ntlm_state->nt_hash = cli_credentials_get_nt_hash(creds,
							  ntlm_state);

	if (!NT_STATUS_IS_OK(status)) {
		return -1;
	}

	*state = ntlm_state;
	return 0;
}

static int test_ntlm_setup(void **state) {
	return test_ntlm_setup_with_options(state, 0, false);
}

static int test_ntlm_and_lm_setup(void **state) {
	return test_ntlm_setup_with_options(state,
					    CLI_CRED_LANMAN_AUTH,
					    false);
}

static int test_ntlm2_setup(void **state) {
	return test_ntlm_setup_with_options(state,
					    CLI_CRED_NTLM2,
					    false);
}

static int test_ntlmv2_setup(void **state) {
	return test_ntlm_setup_with_options(state,
					    CLI_CRED_NTLMv2_AUTH,
					    false);
}

static int test_ntlm_teardown(void **state)
{
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	TALLOC_FREE(ntlm_state);
	*state = NULL;
	return 0;
}

static void test_ntlm_allowed(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_ON,
				     0,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status), NT_STATUS_V(NT_STATUS_OK));
}

static void test_ntlm_allowed_lm_supplied(void **state)
{
	return test_ntlm_allowed(state);
}

static void test_ntlm_disabled(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_DISABLED,
				     0,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status), NT_STATUS_V(NT_STATUS_NTLM_BLOCKED));
}

static void test_ntlm2(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_ON,
				     0,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	/*
	 * NTLM2 session security (where the real challenge is the
	 * MD5(challenge, client-challenge) (in the first 8 bytes of
	 * the lm) isn't decoded by ntlm_password_check(), it must
	 * first be converted back into normal NTLM by the NTLMSSP
	 * layer
	 */
	assert_int_equal(NT_STATUS_V(status),
			 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD));
}

static void test_ntlm_mschapv2_only_allowed(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_MSCHAPv2_NTLMV2_ONLY,
				     MSV1_0_ALLOW_MSVCHAPV2,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status), NT_STATUS_V(NT_STATUS_OK));
}

static void test_ntlm_mschapv2_only_denied(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_MSCHAPv2_NTLMV2_ONLY,
				     0,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status),
			 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD));
}

static void test_ntlmv2_only_ntlmv2(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_NTLMV2_ONLY,
				     0,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status), NT_STATUS_V(NT_STATUS_OK));
}

static void test_ntlmv2_only_ntlm(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_NTLMV2_ONLY,
				     0,
				     &ntlm_state->challenge,
				     &ntlm_state->lm,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status),
			 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD));
}

static void test_ntlmv2_only_ntlm_and_lanman(void **state)
{
	return test_ntlmv2_only_ntlm(state);
}

static void test_ntlmv2_only_ntlm_once(void **state)
{
	DATA_BLOB user_sess_key, lm_sess_key;
	struct ntlm_state *ntlm_state
		= talloc_get_type_abort(*state,
					struct ntlm_state);
	NTSTATUS status;
	status = ntlm_password_check(ntlm_state,
				     false,
				     NTLM_AUTH_NTLMV2_ONLY,
				     0,
				     &ntlm_state->challenge,
				     &data_blob_null,
				     &ntlm_state->ntlm,
				     ntlm_state->username,
				     ntlm_state->username,
				     ntlm_state->domain,
				     NULL,
				     ntlm_state->nt_hash,
				     &user_sess_key,
				     &lm_sess_key);

	assert_int_equal(NT_STATUS_V(status),
			 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD));
}

int main(int argc, const char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(test_ntlm_allowed,
						test_ntlm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlm_allowed_lm_supplied,
						test_ntlm_and_lm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlm_disabled,
						test_ntlm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlm2,
						test_ntlm2_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlm_mschapv2_only_allowed,
						test_ntlm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlm_mschapv2_only_denied,
						test_ntlm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlm,
						test_ntlm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlm_and_lanman,
						test_ntlm_and_lm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlm_once,
						test_ntlm_setup,
						test_ntlm_teardown),
		cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlmv2,
						test_ntlmv2_setup,
						test_ntlm_teardown)
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
	return cmocka_run_group_tests(tests, NULL, NULL);
}