2 * Unit tests for the ntlm_check password hash check library.
4 * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
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/>.
23 * These headers or their equivalents should be included prior to
31 * This allows test applications to use custom definitions of C standard
32 * library functions and types.
37 * Note that the messaging routines (audit_message_send and get_event_server)
38 * are not tested by these unit tests. Currently they are for integration
39 * test support, and as such are exercised by the integration tests.
48 #include "librpc/gen_ndr/netlogon.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "auth/credentials/credentials.h"
60 const struct samr_Password
*nt_hash
;
63 static int test_ntlm_setup_with_options(void **state
,
67 DATA_BLOB challenge
= {
68 .data
= discard_const_p(uint8_t, "I am a teapot"),
71 struct ntlm_state
*ntlm_state
= talloc(NULL
, struct ntlm_state
);
72 DATA_BLOB target_info
= NTLMv2_generate_names_blob(ntlm_state
,
75 struct cli_credentials
*creds
= cli_credentials_init(ntlm_state
);
76 cli_credentials_set_username(creds
,
79 cli_credentials_set_domain(creds
,
82 cli_credentials_set_workstation(creds
,
85 cli_credentials_set_password(creds
,
90 cli_credentials_set_principal(creds
,
95 cli_credentials_get_ntlm_username_domain(creds
,
97 &ntlm_state
->username
,
100 status
= cli_credentials_get_ntlm_response(creds
,
109 &ntlm_state
->ntlm_key
);
110 ntlm_state
->challenge
= challenge
;
112 ntlm_state
->nt_hash
= cli_credentials_get_nt_hash(creds
,
115 if (!NT_STATUS_IS_OK(status
)) {
123 static int test_ntlm_setup(void **state
) {
124 return test_ntlm_setup_with_options(state
, 0, false);
127 static int test_ntlm_and_lm_setup(void **state
) {
128 return test_ntlm_setup_with_options(state
,
129 CLI_CRED_LANMAN_AUTH
,
133 static int test_ntlm2_setup(void **state
) {
134 return test_ntlm_setup_with_options(state
,
139 static int test_ntlmv2_setup(void **state
) {
140 return test_ntlm_setup_with_options(state
,
141 CLI_CRED_NTLMv2_AUTH
,
145 static int test_ntlm_teardown(void **state
)
147 struct ntlm_state
*ntlm_state
148 = talloc_get_type_abort(*state
,
150 TALLOC_FREE(ntlm_state
);
155 static void test_ntlm_allowed(void **state
)
157 DATA_BLOB user_sess_key
, lm_sess_key
;
158 struct ntlm_state
*ntlm_state
159 = talloc_get_type_abort(*state
,
162 status
= ntlm_password_check(ntlm_state
,
166 &ntlm_state
->challenge
,
169 ntlm_state
->username
,
170 ntlm_state
->username
,
177 assert_int_equal(NT_STATUS_V(status
), NT_STATUS_V(NT_STATUS_OK
));
180 static void test_ntlm_allowed_lm_supplied(void **state
)
182 test_ntlm_allowed(state
);
185 static void test_ntlm_disabled(void **state
)
187 DATA_BLOB user_sess_key
, lm_sess_key
;
188 struct ntlm_state
*ntlm_state
189 = talloc_get_type_abort(*state
,
192 status
= ntlm_password_check(ntlm_state
,
196 &ntlm_state
->challenge
,
199 ntlm_state
->username
,
200 ntlm_state
->username
,
207 assert_int_equal(NT_STATUS_V(status
), NT_STATUS_V(NT_STATUS_NTLM_BLOCKED
));
210 static void test_ntlm2(void **state
)
212 DATA_BLOB user_sess_key
, lm_sess_key
;
213 struct ntlm_state
*ntlm_state
214 = talloc_get_type_abort(*state
,
217 status
= ntlm_password_check(ntlm_state
,
221 &ntlm_state
->challenge
,
224 ntlm_state
->username
,
225 ntlm_state
->username
,
233 * NTLM2 session security (where the real challenge is the
234 * MD5(challenge, client-challenge) (in the first 8 bytes of
235 * the lm) isn't decoded by ntlm_password_check(), it must
236 * first be converted back into normal NTLM by the NTLMSSP
239 assert_int_equal(NT_STATUS_V(status
),
240 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD
));
243 static void test_ntlm_mschapv2_only_allowed(void **state
)
245 DATA_BLOB user_sess_key
, lm_sess_key
;
246 struct ntlm_state
*ntlm_state
247 = talloc_get_type_abort(*state
,
250 status
= ntlm_password_check(ntlm_state
,
252 NTLM_AUTH_MSCHAPv2_NTLMV2_ONLY
,
253 MSV1_0_ALLOW_MSVCHAPV2
,
254 &ntlm_state
->challenge
,
257 ntlm_state
->username
,
258 ntlm_state
->username
,
265 assert_int_equal(NT_STATUS_V(status
), NT_STATUS_V(NT_STATUS_OK
));
268 static void test_ntlm_mschapv2_only_denied(void **state
)
270 DATA_BLOB user_sess_key
, lm_sess_key
;
271 struct ntlm_state
*ntlm_state
272 = talloc_get_type_abort(*state
,
275 status
= ntlm_password_check(ntlm_state
,
277 NTLM_AUTH_MSCHAPv2_NTLMV2_ONLY
,
279 &ntlm_state
->challenge
,
282 ntlm_state
->username
,
283 ntlm_state
->username
,
290 assert_int_equal(NT_STATUS_V(status
),
291 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD
));
294 static void test_ntlmv2_only_ntlmv2(void **state
)
296 DATA_BLOB user_sess_key
, lm_sess_key
;
297 struct ntlm_state
*ntlm_state
298 = talloc_get_type_abort(*state
,
301 status
= ntlm_password_check(ntlm_state
,
303 NTLM_AUTH_NTLMV2_ONLY
,
305 &ntlm_state
->challenge
,
308 ntlm_state
->username
,
309 ntlm_state
->username
,
316 assert_int_equal(NT_STATUS_V(status
), NT_STATUS_V(NT_STATUS_OK
));
319 static void test_ntlmv2_only_ntlm(void **state
)
321 DATA_BLOB user_sess_key
, lm_sess_key
;
322 struct ntlm_state
*ntlm_state
323 = talloc_get_type_abort(*state
,
326 status
= ntlm_password_check(ntlm_state
,
328 NTLM_AUTH_NTLMV2_ONLY
,
330 &ntlm_state
->challenge
,
333 ntlm_state
->username
,
334 ntlm_state
->username
,
341 assert_int_equal(NT_STATUS_V(status
),
342 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD
));
345 static void test_ntlmv2_only_ntlm_and_lanman(void **state
)
347 test_ntlmv2_only_ntlm(state
);
350 static void test_ntlmv2_only_ntlm_once(void **state
)
352 DATA_BLOB user_sess_key
, lm_sess_key
;
353 struct ntlm_state
*ntlm_state
354 = talloc_get_type_abort(*state
,
357 status
= ntlm_password_check(ntlm_state
,
359 NTLM_AUTH_NTLMV2_ONLY
,
361 &ntlm_state
->challenge
,
364 ntlm_state
->username
,
365 ntlm_state
->username
,
372 assert_int_equal(NT_STATUS_V(status
),
373 NT_STATUS_V(NT_STATUS_WRONG_PASSWORD
));
376 int main(int argc
, const char **argv
)
378 const struct CMUnitTest tests
[] = {
379 cmocka_unit_test_setup_teardown(test_ntlm_allowed
,
382 cmocka_unit_test_setup_teardown(test_ntlm_allowed_lm_supplied
,
383 test_ntlm_and_lm_setup
,
385 cmocka_unit_test_setup_teardown(test_ntlm_disabled
,
388 cmocka_unit_test_setup_teardown(test_ntlm2
,
391 cmocka_unit_test_setup_teardown(test_ntlm_mschapv2_only_allowed
,
394 cmocka_unit_test_setup_teardown(test_ntlm_mschapv2_only_denied
,
397 cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlm
,
400 cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlm_and_lanman
,
401 test_ntlm_and_lm_setup
,
403 cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlm_once
,
406 cmocka_unit_test_setup_teardown(test_ntlmv2_only_ntlmv2
,
411 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
412 return cmocka_run_group_tests(tests
, NULL
, NULL
);