From 8daa83a594a2e98f39d764422bfbdbc62c9efd44 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 19:20:00 +0200 Subject: Adding upstream version 2:4.20.0+dfsg. Signed-off-by: Daniel Baumann --- libcli/auth/tests/ntlm_check.c | 413 +++++++++++++++++++++++ libcli/auth/tests/test_encode_decode.c | 162 +++++++++ libcli/auth/tests/test_gnutls.c | 524 +++++++++++++++++++++++++++++ libcli/auth/tests/test_rc4_passwd_buffer.c | 336 ++++++++++++++++++ libcli/auth/tests/test_schannel.c | 305 +++++++++++++++++ 5 files changed, 1740 insertions(+) create mode 100644 libcli/auth/tests/ntlm_check.c create mode 100644 libcli/auth/tests/test_encode_decode.c create mode 100644 libcli/auth/tests/test_gnutls.c create mode 100644 libcli/auth/tests/test_rc4_passwd_buffer.c create mode 100644 libcli/auth/tests/test_schannel.c (limited to 'libcli/auth/tests') diff --git a/libcli/auth/tests/ntlm_check.c b/libcli/auth/tests/ntlm_check.c new file mode 100644 index 0000000..79b8ec9 --- /dev/null +++ b/libcli/auth/tests/ntlm_check.c @@ -0,0 +1,413 @@ +/* + * Unit tests for the ntlm_check password hash check library. + * + * Copyright (C) Andrew Bartlett 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 . + * + */ + +/* + * from cmocka.c: + * These headers or their equivalents should be included prior to + * including + * this header file. + * + * #include + * #include + * #include + * + * 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 +#include +#include +#include +#include + +#include "includes.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) +{ + 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) +{ + 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); +} diff --git a/libcli/auth/tests/test_encode_decode.c b/libcli/auth/tests/test_encode_decode.c new file mode 100644 index 0000000..4683edf --- /dev/null +++ b/libcli/auth/tests/test_encode_decode.c @@ -0,0 +1,162 @@ +/* + * Unix SMB/CIFS implementation. + * + * Copyright (C) 2022 Andreas Schneider + * + * 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 . + */ + + +#include +#include +#include +#include +#include + +#include "libcli/auth/smbencrypt.c" + +/* The following hexdumps are from a Windows Server 2022 time trace */ +static const uint8_t plaintext_data[] = { + 0x14, 0x00, 0x50, 0x00, 0x61, 0x00, 0x24, 0x00, + 0x24, 0x00, 0x77, 0x00, 0x30, 0x00, 0x72, 0x00, + 0x64, 0x00, 0x40, 0x00, 0x32, 0x00, 0xc2, 0x34, + 0x7d, 0x21, 0x79, 0x05, 0xef, 0x88, 0xd7, 0x11, + 0xec, 0xe2, 0xce, 0xb5, 0xd4, 0x4d, 0x64, 0x2d, + 0x15, 0x79, 0x01, 0x39, 0xb8, 0xb9, 0x89, 0x5c, + 0x4e, 0x71, 0xbd, 0xf0, 0x14, 0x0c, 0x87, 0x72, + 0xa5, 0xfa, 0x90, 0xbe, 0x62, 0x55, 0xad, 0x7f, + 0xe9, 0x7f, 0x0d, 0x20, 0x19, 0x3a, 0x76, 0xbe, + 0xb2, 0x14, 0x6d, 0x5b, 0x25, 0x1c, 0x67, 0x3a, + 0x23, 0x45, 0x1f, 0x7e, 0x36, 0xa0, 0x95, 0xb7, + 0xa7, 0xb1, 0x33, 0xe1, 0xc4, 0xb6, 0xe6, 0x2d, + 0xd8, 0x2f, 0xe7, 0xdf, 0x01, 0xe8, 0xba, 0x02, + 0x54, 0x36, 0xe9, 0xb6, 0x5e, 0x00, 0x52, 0x9e, + 0x64, 0x00, 0xcb, 0x3c, 0x6d, 0x05, 0x43, 0x7d, + 0x01, 0x9c, 0x22, 0x18, 0x92, 0xe7, 0xa3, 0x55, + 0x65, 0x6d, 0x2e, 0xa3, 0x53, 0x6e, 0xc0, 0x67, + 0x26, 0xac, 0xaa, 0x98, 0xa4, 0xcb, 0xb4, 0x49, + 0x13, 0x60, 0xd4, 0x33, 0x2c, 0x77, 0x58, 0x5e, + 0x50, 0x45, 0xaa, 0x1e, 0x05, 0x15, 0x18, 0x59, + 0x55, 0xca, 0x14, 0x37, 0x31, 0xac, 0x63, 0xfc, + 0x63, 0xa8, 0x2a, 0xa9, 0x99, 0xec, 0x49, 0x87, + 0x64, 0x1d, 0x4e, 0xdd, 0xa3, 0xd0, 0xdc, 0x08, + 0x00, 0x17, 0xf4, 0x2f, 0x9c, 0x4a, 0x17, 0xc7, + 0xbd, 0x30, 0xb7, 0x0e, 0x81, 0xe4, 0xd5, 0x94, + 0x31, 0xff, 0xd6, 0xcc, 0xc6, 0xbb, 0x39, 0xcd, + 0x72, 0xfe, 0xa6, 0x3d, 0x0d, 0x88, 0x68, 0x40, + 0xf8, 0x51, 0x2b, 0xe6, 0xc9, 0xaa, 0x84, 0xf3, + 0xf4, 0x6e, 0x55, 0x37, 0xbf, 0x5d, 0x87, 0xce, + 0xa6, 0x80, 0x4f, 0x8f, 0x8f, 0x7b, 0xe8, 0x30, + 0xc3, 0x2e, 0x24, 0xc7, 0x3e, 0xf1, 0x9f, 0xa6, + 0x77, 0xca, 0x04, 0xbe, 0xb5, 0xe1, 0x40, 0x59, + 0x43, 0xc5, 0x30, 0xc8, 0xe7, 0xbf, 0xab, 0xfa, + 0x86, 0x62, 0xd9, 0x3a, 0x8e, 0xa9, 0x34, 0x73, + 0x20, 0x7b, 0x61, 0x1b, 0x0e, 0xca, 0x98, 0xec, + 0xa1, 0xc1, 0x78, 0xa9, 0xa7, 0x6c, 0x8c, 0xe3, + 0x21, 0x7d, 0xb9, 0x90, 0xe2, 0x73, 0x1a, 0x99, + 0x1d, 0x44, 0xa8, 0xd5, 0x7f, 0x0a, 0x59, 0x47, + 0xd0, 0xf5, 0x6c, 0x14, 0xff, 0x4a, 0x29, 0x20, + 0xb5, 0xfc, 0xe9, 0xf0, 0xa5, 0x35, 0x9e, 0x1c, + 0xa1, 0x4c, 0xec, 0xb5, 0x7d, 0x2d, 0x27, 0xff, + 0x7a, 0x42, 0x18, 0xb8, 0x53, 0x4e, 0xfb, 0xec, + 0xb1, 0xc1, 0x65, 0x2d, 0xa4, 0x69, 0x85, 0x56, + 0x61, 0x6d, 0x21, 0x66, 0x88, 0x31, 0xdf, 0xba, + 0x28, 0xc6, 0x9a, 0xf8, 0xb7, 0xf6, 0x2a, 0x43, + 0xba, 0x9b, 0x84, 0x14, 0xce, 0xa9, 0xc9, 0xf5, + 0x85, 0x6f, 0x31, 0x89, 0x8d, 0xfc, 0x25, 0x2e, + 0x98, 0x25, 0x5a, 0x03, 0xf0, 0xb8, 0x5d, 0x4a, + 0xd4, 0x4c, 0xc8, 0x62, 0x4e, 0xeb, 0x07, 0xc8, + 0x5c, 0x9e, 0x63, 0x30, 0xfe, 0x9f, 0x0f, 0x96, + 0xd0, 0xd7, 0x70, 0xad, 0xcd, 0x84, 0xbc, 0x1e, + 0x48, 0xa0, 0x20, 0x14, 0x10, 0xa4, 0xb1, 0x5b, + 0x05, 0x36, 0x9a, 0x6d, 0xb0, 0x10, 0x98, 0xbd, + 0x8d, 0xa2, 0xd1, 0xb2, 0xfa, 0x23, 0x37, 0xeb, + 0xb0, 0x04, 0x53, 0xcb, 0xa1, 0xa9, 0xc4, 0x88, + 0xdd, 0xf9, 0x80, 0xf5, 0xa9, 0xcd, 0x7b, 0xf8, + 0x77, 0x0b, 0x19, 0x84, 0x4c, 0xef, 0x2c, 0x14, + 0xa1, 0xdc, 0x9f, 0x2f, 0x41, 0xd0, 0xd0, 0x33, + 0x29, 0x8a, 0xb9, 0x39, 0x7e, 0xc9, 0x7f, 0xe7, + 0x63, 0x64, 0xa4, 0x7b, 0x4a, 0x6a, 0x33, 0xa7, + 0xaa, 0xf6, 0xca, 0x98, 0xc4, 0x9b, 0x62, 0x5b, + 0xcd, 0x53, 0x82, 0xbf, 0xf0, 0x0b, 0x9c, 0xe7, + 0xb2, 0x44, 0x1b, 0x88, 0x71, 0x61, 0xa1, 0x36, + 0x9e, 0x7a, 0x0a, 0x3c, 0x20, 0xd8, 0xff, 0xa1, + 0x23, 0x66 +}; + +static void torture_encode_pwd_buffer514_from_str(void **state) +{ + uint8_t data[514] = {0}; + bool ok; + + ok = encode_pwd_buffer514_from_str(data, "Pa$$w0rd@2", STR_UNICODE); + assert_true(ok); + + /* We can only compare the first 22 bytes as the rest is random data */ + assert_memory_equal(data, plaintext_data, 22); + assert_memory_not_equal(data + 22, + plaintext_data + 22, + sizeof(plaintext_data) - 22); +} + +static void torture_extract_pwd_blob_from_buffer514(void **state) +{ + DATA_BLOB new_password = { + .length = 0, + }; + bool ok; + + ok = extract_pwd_blob_from_buffer514(NULL, plaintext_data, &new_password); + assert_true(ok); + assert_int_equal(new_password.length, 20); + assert_memory_equal(new_password.data, + plaintext_data + 2, + new_password.length); + data_blob_free(&new_password); +} + +static void torture_decode_pwd_string_from_buffer514(void **state) +{ + DATA_BLOB decoded_password = { + .length = 0, + }; + bool ok; + + ok = decode_pwd_string_from_buffer514(NULL, + plaintext_data, + CH_UTF16, + &decoded_password); + assert_true(ok); + assert_memory_equal(decoded_password.data, "Pa$$w0rd@2", decoded_password.length); + data_blob_free(&decoded_password); +} + +int main(int argc, char *argv[]) +{ + int rc; + const struct CMUnitTest tests[] = { + cmocka_unit_test(torture_encode_pwd_buffer514_from_str), + cmocka_unit_test(torture_extract_pwd_blob_from_buffer514), + cmocka_unit_test(torture_decode_pwd_string_from_buffer514), + }; + + if (argc == 2) { + cmocka_set_test_filter(argv[1]); + } + cmocka_set_message_output(CM_OUTPUT_SUBUNIT); + + rc = cmocka_run_group_tests(tests, NULL, NULL); + + return rc; +} diff --git a/libcli/auth/tests/test_gnutls.c b/libcli/auth/tests/test_gnutls.c new file mode 100644 index 0000000..860f8b3 --- /dev/null +++ b/libcli/auth/tests/test_gnutls.c @@ -0,0 +1,524 @@ +/* + * Unix SMB/CIFS implementation. + * + * Copyright (C) 2019 Guenther Deschner + * + * 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 . + */ + +#include +#include +#include +#include +#include + +#include "includes.h" +#include "libcli/auth/libcli_auth.h" + +#include "lib/crypto/gnutls_helpers.h" +#include +#include + +static void torture_gnutls_aes_128_cfb_flags(void **state, + const DATA_BLOB session_key, + const DATA_BLOB seq_num_initial, + const DATA_BLOB confounder_initial, + const DATA_BLOB confounder_expected, + const DATA_BLOB clear_initial, + const DATA_BLOB crypt_expected) +{ + uint8_t confounder[8]; + DATA_BLOB io; + gnutls_cipher_hd_t cipher_hnd = NULL; + uint8_t sess_kf0[16] = {0}; + gnutls_datum_t key = { + .data = sess_kf0, + .size = sizeof(sess_kf0), + }; + uint32_t iv_size = + gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_CFB8); + uint8_t _iv[iv_size]; + gnutls_datum_t iv = { + .data = _iv, + .size = iv_size, + }; + uint32_t i; + int rc; + + assert_int_equal(session_key.length, 16); + assert_int_equal(seq_num_initial.length, 8); + assert_int_equal(confounder_initial.length, 8); + assert_int_equal(confounder_expected.length, 8); + assert_int_equal(clear_initial.length, crypt_expected.length); + + DEBUG(0,("checking buffer size: %d\n", (int)clear_initial.length)); + + io = data_blob_dup_talloc(NULL, clear_initial); + assert_non_null(io.data); + assert_int_equal(io.length, clear_initial.length); + + memcpy(confounder, confounder_initial.data, 8); + + DEBUG(0,("confounder before crypt:\n")); + dump_data(0, confounder, 8); + DEBUG(0,("initial seq num:\n")); + dump_data(0, seq_num_initial.data, 8); + DEBUG(0,("io data before crypt:\n")); + dump_data(0, io.data, io.length); + + for (i = 0; i < key.size; i++) { + key.data[i] = session_key.data[i] ^ 0xf0; + } + + ZERO_ARRAY(_iv); + + memcpy(iv.data + 0, seq_num_initial.data, 8); + memcpy(iv.data + 8, seq_num_initial.data, 8); + + rc = gnutls_cipher_init(&cipher_hnd, + GNUTLS_CIPHER_AES_128_CFB8, + &key, + &iv); + assert_int_equal(rc, 0); + + rc = gnutls_cipher_encrypt(cipher_hnd, + confounder, + 8); + assert_int_equal(rc, 0); + + rc = gnutls_cipher_encrypt(cipher_hnd, + io.data, + io.length); + assert_int_equal(rc, 0); + + DEBUG(0,("confounder after crypt:\n")); + dump_data(0, confounder, 8); + DEBUG(0,("initial seq num:\n")); + dump_data(0, seq_num_initial.data, 8); + DEBUG(0,("io data after crypt:\n")); + dump_data(0, io.data, io.length); + assert_memory_equal(io.data, crypt_expected.data, crypt_expected.length); + assert_memory_equal(confounder, confounder_expected.data, confounder_expected.length); + + rc = gnutls_cipher_decrypt(cipher_hnd, + confounder, + 8); + assert_int_equal(rc, 0); + + rc = gnutls_cipher_decrypt(cipher_hnd, + io.data, + io.length); + assert_int_equal(rc, 0); + gnutls_cipher_deinit(cipher_hnd); + + DEBUG(0,("confounder after decrypt:\n")); + dump_data(0, confounder, 8); + DEBUG(0,("initial seq num:\n")); + dump_data(0, seq_num_initial.data, 8); + DEBUG(0,("io data after decrypt:\n")); + dump_data(0, io.data, io.length); + assert_memory_equal(io.data, clear_initial.data, clear_initial.length); + assert_memory_equal(confounder, confounder_initial.data, confounder_initial.length); +} + +static void torture_gnutls_aes_128_cfb(void **state) +{ + const uint8_t _session_key[16] = { + 0x8E, 0xE8, 0x27, 0x85, 0x83, 0x41, 0x3C, 0x8D, + 0xC9, 0x54, 0x70, 0x75, 0x8E, 0xC9, 0x69, 0x91 + }; + const DATA_BLOB session_key = data_blob_const(_session_key, 16); + const uint8_t _seq_num_initial[8] = { + 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 + }; + const DATA_BLOB seq_num_initial = + data_blob_const(_seq_num_initial, 8); + const uint8_t _confounder_initial[8] = { + 0x6E, 0x09, 0x25, 0x94, 0x01, 0xA0, 0x09, 0x31 + }; + const DATA_BLOB confounder_initial = + data_blob_const(_confounder_initial, 8); + const uint8_t _confounder_expected[8] = { + 0xCA, 0xFB, 0xAC, 0xFB, 0xA8, 0x26, 0x75, 0x2A + }; + const DATA_BLOB confounder_expected = + data_blob_const(_confounder_expected, 8); + const uint8_t _clear_initial[] = { + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x8A, 0xE3, 0x13, 0x71, 0x02, 0xF4, 0x36, 0x71, + 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x40, 0x28, 0x00, 0x78, 0x57, 0x34, 0x12, + 0x34, 0x12, 0xCD, 0xAB, 0xEF, 0x00, 0x01, 0x23, + 0x45, 0x67, 0x89, 0xAB, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, + 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + const DATA_BLOB clear_initial = data_blob_const(_clear_initial, + sizeof(_clear_initial)); + const uint8_t crypt_buffer[] = { + 0xE2, 0xE5, 0xE3, 0x26, 0x45, 0xFB, 0xFC, 0xF3, + 0x9C, 0x14, 0xDD, 0xE1, 0x39, 0x23, 0xE0, 0x55, + 0xED, 0x8F, 0xF4, 0x92, 0xA1, 0xBD, 0xDC, 0x40, + 0x58, 0x6F, 0xD2, 0x5B, 0xF9, 0xC9, 0xA3, 0x87, + 0x46, 0x4B, 0x7F, 0xB2, 0x03, 0xD2, 0x35, 0x22, + 0x3E, 0x70, 0x9F, 0x1E, 0x3F, 0x1F, 0xDB, 0x7D, + 0x79, 0x88, 0x5A, 0x3D, 0xD3, 0x40, 0x1E, 0x69, + 0xD7, 0xE2, 0x1D, 0x5A, 0xE9, 0x3B, 0xE1, 0xE2, + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0xCA, 0x02, 0x00, 0x99, 0x9F, 0x0C, 0x01, 0xE6, + 0xD2, 0x00, 0xAF, 0xE0, 0x51, 0x88, 0x62, 0x50, + 0xB7, 0xE8, 0x6D, 0x63, 0x4B, 0x97, 0x05, 0xC1, + 0xD4, 0x83, 0x96, 0x29, 0x80, 0xAE, 0xD8, 0xA2, + 0xED, 0xC9, 0x5D, 0x0D, 0x29, 0xFF, 0x2C, 0x23, + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0x95, 0xDF, 0x80, 0x76, 0x0B, 0x17, 0x0E, 0xD8 + }; + const DATA_BLOB crypt_expected = data_blob_const(crypt_buffer, + sizeof(crypt_buffer)); + int buffer_sizes[] = { + 0, 1, 3, 7, 8, 9, 15, 16, 17 + }; + int i; + + torture_gnutls_aes_128_cfb_flags(state, + session_key, + seq_num_initial, + confounder_initial, + confounder_expected, + clear_initial, + crypt_expected); + + /* repeat the test for varying buffer sizes */ + + for (i = 0; i < ARRAY_SIZE(buffer_sizes); i++) { + DATA_BLOB clear_initial_trunc = + data_blob_const(clear_initial.data, buffer_sizes[i]); + DATA_BLOB crypt_expected_trunc = + data_blob_const(crypt_expected.data, buffer_sizes[i]); + torture_gnutls_aes_128_cfb_flags(state, + session_key, + seq_num_initial, + confounder_initial, + confounder_expected, + clear_initial_trunc, + crypt_expected_trunc); + } +} + +static void torture_gnutls_des_crypt56(void **state) +{ + static const uint8_t key[7] = { + 0x69, 0x88, 0x96, 0x8E, 0xB5, 0x3A, 0x24 + }; + static const uint8_t clear[8] = { + 0x3F, 0x49, 0x5B, 0x20, 0xA7, 0x84, 0xC2, 0x34 + }; + static const uint8_t crypt_expected[8] = { + 0x54, 0x86, 0xCF, 0x51, 0x49, 0x3A, 0x53, 0x5B + }; + + uint8_t crypt[8]; + uint8_t decrypt[8]; + int rc; + + rc = des_crypt56_gnutls(crypt, clear, key, SAMBA_GNUTLS_ENCRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 8); + + rc = des_crypt56_gnutls(decrypt, crypt, key, SAMBA_GNUTLS_DECRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(decrypt, clear, 8); +} + +static void torture_gnutls_E_P16(void **state) +{ + static const uint8_t key[14] = { + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0x69, 0x88, 0x96, 0x8E, 0xB5, 0x3A + }; + uint8_t buffer[16] = { + 0x9C, 0x14, 0xDD, 0xE1, 0x39, 0x23, 0xE0, 0x55, + 0x3F, 0x49, 0x5B, 0x20, 0xA7, 0x84, 0xC2, 0x34 + }; + static const uint8_t crypt_expected[16] = { + 0x41, 0x4A, 0x7B, 0xEA, 0xAB, 0xBB, 0x95, 0xCE, + 0x1D, 0xEA, 0xD9, 0xFF, 0xB0, 0xA9, 0xA4, 0x05 + }; + + int rc; + + rc = E_P16(key, buffer); + assert_int_equal(rc, 0); + assert_memory_equal(buffer, crypt_expected, 16); +} + +static void torture_gnutls_E_P24(void **state) +{ + static const uint8_t key[21] = { + 0xFB, 0x67, 0x99, 0xA4, 0x83, 0xF3, 0xD4, 0xED, + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0x69, 0x88, 0x96, 0x8E, 0x3A + }; + const uint8_t c8[8] = { + 0x44, 0xFB, 0xAC, 0xFB, 0x83, 0xB6, 0x75, 0x2A + }; + static const uint8_t crypt_expected[24] = { + 0x1A, 0x5E, 0x11, 0xA1, 0x59, 0xA9, 0x6B, 0x4E, + 0x12, 0x5D, 0x81, 0x75, 0xA6, 0x62, 0x15, 0x6D, + 0x5D, 0x20, 0x25, 0xC1, 0xA3, 0x92, 0xB3, 0x28 + }; + + uint8_t crypt[24]; + int rc; + + rc = E_P24(key, c8, crypt); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 24); +} + +static void torture_gnutls_SMBOWFencrypt(void **state) +{ + static const uint8_t password[16] = { + 'M', 'y', 'p', 'a', 's', 's', 'w', 'o', + 'r', 'd', 'i', 's', '1', '1', '1', '1' + }; + const uint8_t c8[8] = { + 0x79, 0x88, 0x5A, 0x3D, 0xD3, 0x40, 0x1E, 0x69 + }; + static const uint8_t crypt_expected[24] = { + 0x3F, 0xE3, 0x53, 0x75, 0x81, 0xB4, 0xF0, 0xE7, + 0x0C, 0xDE, 0xCD, 0xAE, 0x39, 0x1F, 0x14, 0xB4, + 0xA4, 0x2B, 0x3E, 0x39, 0x16, 0xFD, 0x1D, 0x62 + }; + + uint8_t crypt[24]; + int rc; + + rc = SMBOWFencrypt(password, c8, crypt); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 24); +} + +static void torture_gnutls_E_old_pw_hash(void **state) +{ + static uint8_t key[14] = { + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0x69, 0x88, 0x96, 0x8E, 0xB5, 0x3A + }; + uint8_t clear[16] = { + 0x9C, 0x14, 0xDD, 0xE1, 0x39, 0x23, 0xE0, 0x55, + 0x3F, 0x49, 0x5B, 0x20, 0xA7, 0x84, 0xC2, 0x34 + }; + static const uint8_t crypt_expected[16] = { + 0x6A, 0xC7, 0x08, 0xCA, 0x2A, 0xC1, 0xAA, 0x64, + 0x37, 0xEF, 0xBE, 0x58, 0xC2, 0x59, 0x33, 0xEC + }; + uint8_t crypt[16]; + int rc; + + rc = E_old_pw_hash(key, clear, crypt); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 16); +} + +static void torture_gnutls_des_crypt128(void **state) +{ + static uint8_t key[16] = { + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0xA9, 0x69, 0x88, 0x96, 0x8E, 0xB5, 0x3A, 0x24 + }; + static const uint8_t clear[8] = { + 0x3F, 0x49, 0x5B, 0x20, 0xA7, 0x84, 0xC2, 0x34 + }; + static const uint8_t crypt_expected[8] = { + 0x4C, 0xB4, 0x4B, 0xD3, 0xC8, 0xC1, 0xA5, 0x50 + }; + + uint8_t crypt[8]; + int rc; + + rc = des_crypt128(crypt, clear, key); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 8); +} + +static void torture_gnutls_des_crypt112(void **state) +{ + static uint8_t key[14] = { + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0x88, 0x96, 0x8E, 0xB5, 0x3A, 0x24 + }; + static const uint8_t clear[8] = { + 0x2F, 0x49, 0x5B, 0x20, 0xD7, 0x84, 0xC2, 0x34 + }; + static const uint8_t crypt_expected[8] = { + 0x87, 0x35, 0xFA, 0xA4, 0x5D, 0x7A, 0xA5, 0x05 + }; + + uint8_t crypt[8]; + uint8_t decrypt[8]; + int rc; + + rc = des_crypt112(crypt, clear, key, SAMBA_GNUTLS_ENCRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 8); + + rc = des_crypt112(decrypt, crypt, key, SAMBA_GNUTLS_DECRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(decrypt, clear, 8); +} + +static void torture_gnutls_des_crypt112_16(void **state) +{ + static uint8_t key[14] = { + 0x1E, 0x38, 0x27, 0x5B, 0x3B, 0xB8, 0x67, 0xEB, + 0x88, 0x96, 0x8E, 0xB5, 0x3A, 0x24 + }; + static const uint8_t clear[16] = { + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0xFB, 0x67, 0x99, 0xA4, 0x83, 0xF3, 0xD4, 0xED + }; + static const uint8_t crypt_expected[16] = { + 0x3C, 0x10, 0x37, 0x67, 0x96, 0x95, 0xF7, 0x96, + 0xAA, 0x03, 0xB9, 0xEA, 0xD6, 0xB3, 0xC3, 0x2D + }; + + uint8_t crypt[16]; + uint8_t decrypt[16]; + int rc; + + rc = des_crypt112_16(crypt, clear, key, SAMBA_GNUTLS_ENCRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 16); + + rc = des_crypt112_16(decrypt, crypt, key, SAMBA_GNUTLS_DECRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(decrypt, clear, 16); +} + +static void torture_gnutls_sam_rid_crypt(void **state) +{ + static const uint8_t clear[16] = { + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0x3F, 0x49, 0x5B, 0x20, 0xA7, 0x84, 0xC2, 0x34 + }; + static const uint8_t crypt_expected[16] = { + 0x1E, 0x38, 0x27, 0x5B, 0x3B, 0xB8, 0x67, 0xEB, + 0xFB, 0x67, 0x99, 0xA4, 0x83, 0xF3, 0xD4, 0xED + }; + + uint8_t crypt[16]; + uint8_t decrypt[16]; + int rid = 500; + int rc; + + rc = sam_rid_crypt(rid, clear, crypt, SAMBA_GNUTLS_ENCRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(crypt, crypt_expected, 16); + + rc = sam_rid_crypt(rid, crypt, decrypt, SAMBA_GNUTLS_DECRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(decrypt, clear, 16); +} + +static void torture_gnutls_SMBsesskeygen_lm_sess_key(void **state) +{ + static const uint8_t lm_hash[16] = { + 0xFB, 0x67, 0x99, 0xA4, 0x83, 0xF3, 0xD4, 0xED, + 0x9C, 0x14, 0xDD, 0xE1, 0x39, 0x23, 0xE0, 0x55 + }; + static const uint8_t lm_resp[24] = { + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0x1E, 0x38, 0x27, 0x5B, 0x3B, 0xB8, 0x67, 0xEB + }; + static const uint8_t crypt_expected[16] = { + 0x52, 0x8D, 0xB2, 0xD3, 0x89, 0x83, 0xFB, 0x9C, + 0x96, 0x45, 0x15, 0x4B, 0xC3, 0xF5, 0xD5, 0x7F + }; + + uint8_t crypt_sess_key[16]; + NTSTATUS status; + + status = SMBsesskeygen_lm_sess_key(lm_hash, lm_resp, crypt_sess_key); + assert_true(NT_STATUS_IS_OK(status)); + assert_memory_equal(crypt_sess_key, crypt_expected, 16); +} + +static void torture_gnutls_sess_crypt_blob(void **state) +{ + static uint8_t _key[16] = { + 0x1E, 0x38, 0x27, 0x5B, 0x3B, 0xB8, 0x67, 0xEB, + 0xFA, 0xEE, 0xE8, 0xBA, 0x06, 0x01, 0x2D, 0x95 + }; + DATA_BLOB key = data_blob_const(_key, 16); + static const uint8_t _clear[24] = { + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0x3F, 0x49, 0x5B, 0x20, 0xA7, 0x84, 0xC2, 0x34 + }; + DATA_BLOB clear = data_blob_const(_clear, 24); + static const uint8_t crypt_expected[24] = { + 0x2B, 0xDD, 0x3B, 0xFA, 0x48, 0xC9, 0x63, 0x56, + 0xAE, 0x8B, 0x3E, 0xCF, 0xEF, 0xDF, 0x7A, 0x42, + 0xB3, 0x00, 0x71, 0x7F, 0x5D, 0x1D, 0xE4, 0x70 + }; + DATA_BLOB crypt = data_blob(NULL, 24); + DATA_BLOB decrypt = data_blob(NULL, 24); + int rc; + + rc = sess_crypt_blob(&crypt, &clear, &key, SAMBA_GNUTLS_ENCRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(crypt.data, crypt_expected, 24); + + rc = sess_crypt_blob(&decrypt, &crypt, &key, SAMBA_GNUTLS_DECRYPT); + assert_int_equal(rc, 0); + assert_memory_equal(decrypt.data, clear.data, 24); +} + +int main(int argc, char *argv[]) +{ + int rc; + const struct CMUnitTest tests[] = { + cmocka_unit_test(torture_gnutls_aes_128_cfb), + cmocka_unit_test(torture_gnutls_des_crypt56), + cmocka_unit_test(torture_gnutls_E_P16), + cmocka_unit_test(torture_gnutls_E_P24), + cmocka_unit_test(torture_gnutls_SMBOWFencrypt), + cmocka_unit_test(torture_gnutls_E_old_pw_hash), + cmocka_unit_test(torture_gnutls_des_crypt128), + cmocka_unit_test(torture_gnutls_des_crypt112), + cmocka_unit_test(torture_gnutls_des_crypt112_16), + cmocka_unit_test(torture_gnutls_sam_rid_crypt), + cmocka_unit_test(torture_gnutls_SMBsesskeygen_lm_sess_key), + cmocka_unit_test(torture_gnutls_sess_crypt_blob), + }; + + if (argc == 2) { + cmocka_set_test_filter(argv[1]); + } + cmocka_set_message_output(CM_OUTPUT_SUBUNIT); + + rc = cmocka_run_group_tests(tests, NULL, NULL); + + return rc; +} diff --git a/libcli/auth/tests/test_rc4_passwd_buffer.c b/libcli/auth/tests/test_rc4_passwd_buffer.c new file mode 100644 index 0000000..6d97ac6 --- /dev/null +++ b/libcli/auth/tests/test_rc4_passwd_buffer.c @@ -0,0 +1,336 @@ +/* + * Unix SMB/CIFS implementation. + * + * Copyright (C) 2018-2019 Andreas Schneider + * + * 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 . + */ + +#include +#include +#include +#include +#include + +#include "includes.h" +#include "libcli/auth/libcli_auth.h" +#include "rpc_client/init_samr.h" + +#define PASSWORD "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + +static const uint8_t encrypted_test_blob[] = { + 0x37, 0x8e, 0x1d, 0xd5, 0xd3, 0x9f, 0xca, 0x8e, + 0x2f, 0x2d, 0xee, 0xc3, 0xb5, 0x50, 0xcd, 0x4e, + 0xc9, 0x08, 0x04, 0x68, 0x32, 0xc3, 0xac, 0x8e, + 0x53, 0x69, 0xd6, 0xb7, 0x56, 0xcc, 0xc0, 0xbe, + 0x4e, 0x96, 0xa7, 0x74, 0xe9, 0xaa, 0x10, 0x3d, + 0xd5, 0x8c, 0xaa, 0x12, 0x56, 0xb6, 0xf1, 0x85, + 0x21, 0xfa, 0xe9, 0xa1, 0x76, 0xe6, 0xa5, 0x33, + 0x33, 0x2f, 0x47, 0x29, 0xd6, 0xbd, 0xde, 0x64, + 0x4d, 0x15, 0x3e, 0x6a, 0x11, 0x9b, 0x52, 0xbf, + 0x7e, 0x3a, 0xeb, 0x1c, 0x55, 0xd1, 0xb2, 0xa4, + 0x35, 0x03, 0x6c, 0x39, 0x61, 0x28, 0x98, 0xc3, + 0x2d, 0xd4, 0x70, 0x69, 0x8b, 0x83, 0xe9, 0x62, + 0xbe, 0xd8, 0x72, 0x4e, 0xdf, 0xd4, 0xe9, 0xe3, + 0x46, 0x2a, 0xf9, 0x3c, 0x0f, 0x41, 0x62, 0xe1, + 0x43, 0xf0, 0x91, 0xbe, 0x72, 0xa0, 0xc9, 0x08, + 0x73, 0x20, 0x1f, 0x0d, 0x68, 0x2e, 0x32, 0xa1, + 0xb8, 0x9b, 0x08, 0xa1, 0xb4, 0x81, 0x6b, 0xf1, + 0xde, 0x0c, 0x28, 0x34, 0xe2, 0x65, 0x62, 0x54, + 0xeb, 0xc0, 0x71, 0x14, 0xad, 0x36, 0x43, 0x0e, + 0x92, 0x4d, 0x11, 0xe8, 0xdd, 0x2d, 0x5f, 0x05, + 0xff, 0x07, 0xda, 0x81, 0x4e, 0x27, 0x42, 0xa8, + 0xa9, 0x64, 0x4c, 0x74, 0xc8, 0x05, 0xbb, 0x83, + 0x5a, 0xd9, 0x90, 0x3e, 0x0d, 0x9d, 0xe5, 0x2f, + 0x08, 0xf9, 0x1b, 0xbd, 0x26, 0xc3, 0x0d, 0xac, + 0x43, 0xd5, 0x17, 0xf2, 0x61, 0xf5, 0x74, 0x9b, + 0xf3, 0x5b, 0x5f, 0xe1, 0x8a, 0xa6, 0xfd, 0xdf, + 0xff, 0xb5, 0x8b, 0xf1, 0x26, 0xf7, 0xe0, 0xa7, + 0x4f, 0x5b, 0xb8, 0x6d, 0xeb, 0xf6, 0x52, 0x68, + 0x8d, 0xa3, 0xd4, 0x7f, 0x56, 0x43, 0xaa, 0xec, + 0x58, 0x47, 0x03, 0xee, 0x9b, 0x59, 0xd9, 0x78, + 0x9a, 0xfb, 0x9e, 0xe9, 0xa6, 0x61, 0x4e, 0x6d, + 0x92, 0x35, 0xd3, 0x37, 0x6e, 0xf2, 0x34, 0x39, + 0xd4, 0xd2, 0xeb, 0xcf, 0x1c, 0x10, 0xb3, 0x2b, + 0x3e, 0x07, 0x42, 0x3e, 0x20, 0x90, 0x07, 0x3e, + 0xc7, 0xed, 0xd4, 0xdf, 0x50, 0xe5, 0xff, 0xaf, + 0x05, 0xce, 0x29, 0xbe, 0x01, 0xf8, 0xb0, 0x30, + 0x96, 0xae, 0x1b, 0x62, 0x23, 0x93, 0x91, 0x1a, + 0x52, 0x98, 0xd9, 0x59, 0xb8, 0x11, 0xec, 0xb8, + 0xcf, 0x20, 0x32, 0x90, 0x9e, 0xf2, 0x06, 0x43, + 0xb8, 0x36, 0xe3, 0x33, 0x4e, 0x6f, 0x75, 0xeb, + 0xf7, 0x6c, 0xac, 0x06, 0x5f, 0x24, 0x8e, 0x4a, + 0x03, 0xdf, 0x50, 0x31, 0xaa, 0x91, 0xd5, 0x85, + 0x95, 0x78, 0x5b, 0xf4, 0x7f, 0x3e, 0xbc, 0x41, + 0xfa, 0x10, 0xd3, 0x0f, 0x86, 0x8b, 0x23, 0xed, + 0xfc, 0xcc, 0x3e, 0x7d, 0x8c, 0xb4, 0x7c, 0xec, + 0x04, 0x7d, 0x12, 0x53, 0xa1, 0x30, 0xc5, 0xac, + 0xf3, 0x1e, 0x54, 0x1f, 0x97, 0x05, 0x86, 0x74, + 0x51, 0x13, 0x45, 0x98, 0xb8, 0x50, 0x62, 0x18, + 0x0f, 0x6d, 0x66, 0xa4, 0x88, 0x31, 0x76, 0xa3, + 0xb0, 0x75, 0x55, 0x44, 0x18, 0x7c, 0x67, 0xc7, + 0x09, 0x9c, 0xab, 0x53, 0x49, 0xc0, 0xc9, 0x27, + 0x53, 0xa6, 0x99, 0x01, 0x10, 0x49, 0x67, 0x8e, + 0x5b, 0x12, 0x96, 0x40, 0x16, 0x39, 0x11, 0x53, + 0x44, 0x8f, 0xa9, 0xbe, 0x84, 0xbe, 0xe0, 0x45, + 0xe3, 0xfd, 0x48, 0x46, 0x43, 0x53, 0x13, 0x5f, + 0xfa, 0xcf, 0x09, 0x67, 0x90, 0xa3, 0x94, 0xaa, + 0x0d, 0x1f, 0xc2, 0xc3, 0xd4, 0x7e, 0xc8, 0x14, + 0xbe, 0x84, 0xa5, 0x55, 0xee, 0x49, 0x8e, 0x03, + 0x1d, 0xaf, 0xad, 0x65, 0x2f, 0xf0, 0xd5, 0x90, + 0x5e, 0x8d, 0x29, 0x40, 0xba, 0x57, 0x26, 0xa8, + 0x6c, 0x4b, 0x59, 0x40, 0x4e, 0xc2, 0xc4, 0x88, + 0x0a, 0x06, 0x2b, 0x6c, 0x2a, 0xc7, 0x3f, 0xfe, + 0x37, 0x2c, 0x41, 0x58, 0xfe, 0x7e, 0xaf, 0xd1, + 0xd9, 0xd2, 0x9c, 0xd7, 0x8a, 0x01, 0x0e, 0x8c, + 0x9e, 0x8b, 0x5d, 0x72, 0x54, 0x00, 0xbe, 0xb2, + 0x9a, 0xc7, 0xfd, 0x83, 0x1e, 0x9c, 0x79, 0xdd, + 0x15, 0x13, 0xdc, 0x15, +}; + + +static const uint8_t encrypted_wkssvc_test_blob[] = { + 0x13, 0x79, 0x1f, 0x1a, 0x02, 0x15, 0x72, 0x1c, + 0xa6, 0x26, 0x37, 0xeb, 0x1d, 0x41, 0x7f, 0x76, + 0x11, 0x3f, 0x49, 0x4c, 0xf9, 0x69, 0x17, 0xc8, + 0x90, 0x92, 0x53, 0xb9, 0x3f, 0xcd, 0x06, 0xfe, + 0x5c, 0x17, 0x82, 0xbd, 0x86, 0xab, 0x49, 0xee, + 0x61, 0x76, 0x55, 0xc0, 0x10, 0x51, 0xcd, 0xd9, + 0x6f, 0x12, 0x86, 0xc6, 0x19, 0x59, 0x9a, 0x2f, + 0x27, 0x1d, 0x99, 0x30, 0x60, 0x0d, 0x65, 0xc6, + 0x43, 0xd6, 0xda, 0x6b, 0x66, 0x95, 0xd4, 0xca, + 0xf5, 0x04, 0xf7, 0x01, 0x5a, 0x55, 0xb0, 0x5e, + 0x72, 0x8a, 0x75, 0xe5, 0x33, 0x4c, 0xd8, 0xc4, + 0x0e, 0xf4, 0x6d, 0x23, 0xdd, 0x05, 0x90, 0xff, + 0xe0, 0x91, 0x7b, 0x62, 0x86, 0xee, 0x78, 0x31, + 0x07, 0xad, 0x8b, 0xf9, 0xdf, 0x6f, 0x8b, 0xbd, + 0x15, 0xde, 0x1b, 0xae, 0x84, 0x68, 0xe5, 0x41, + 0x7a, 0xe3, 0x47, 0x99, 0xba, 0x61, 0xe5, 0x51, + 0x64, 0x9a, 0xa0, 0x41, 0x44, 0xa1, 0x3a, 0x52, + 0x59, 0x7d, 0x6c, 0xcf, 0xcc, 0xf0, 0x11, 0xbc, + 0xb7, 0x51, 0xa9, 0xd8, 0xfd, 0xbf, 0x58, 0x77, + 0x28, 0x86, 0xa1, 0x27, 0x94, 0xe5, 0xf6, 0x1a, + 0x6b, 0x76, 0xf7, 0x72, 0x6e, 0x17, 0x09, 0xd8, + 0x3c, 0x6f, 0x39, 0x91, 0xea, 0x48, 0x98, 0xdc, + 0x1d, 0x50, 0x2e, 0x02, 0x6e, 0x7f, 0x80, 0x5d, + 0x6e, 0x96, 0xe1, 0xcf, 0x8b, 0x6b, 0xb6, 0xed, + 0xb4, 0x6a, 0x08, 0xd2, 0x45, 0x09, 0x88, 0x86, + 0x32, 0x58, 0xd8, 0x5e, 0x33, 0x8c, 0x29, 0x1a, + 0x8f, 0xc5, 0x54, 0x9b, 0xa8, 0x32, 0xb2, 0xc1, + 0x72, 0x14, 0x6c, 0x5d, 0x9d, 0xd3, 0xf2, 0x6c, + 0x6e, 0xa4, 0x84, 0x52, 0x26, 0x73, 0x7a, 0x30, + 0x56, 0x75, 0xef, 0xd1, 0x9d, 0xcd, 0xb7, 0x87, + 0xa9, 0x5c, 0xaf, 0xe6, 0xda, 0x1d, 0x3c, 0x9c, + 0xa3, 0xb1, 0x03, 0xb0, 0x8e, 0xf6, 0xc8, 0x8f, + 0x57, 0x1c, 0xce, 0x05, 0x54, 0x99, 0xf1, 0xf9, + 0x35, 0xe6, 0xf7, 0x67, 0x94, 0xb2, 0x67, 0x5b, + 0xe7, 0xa0, 0xa2, 0x1e, 0xa2, 0x74, 0xd3, 0x99, + 0x9c, 0xd5, 0xd9, 0x90, 0x86, 0x24, 0x0e, 0x1a, + 0x0d, 0xc8, 0x9e, 0x68, 0x4c, 0x43, 0x2f, 0x42, + 0xb1, 0x7c, 0xce, 0x1e, 0xb6, 0xac, 0x56, 0xb0, + 0x8d, 0x93, 0xf1, 0x53, 0x7d, 0x0e, 0x00, 0x46, + 0xba, 0x2e, 0x14, 0x7a, 0x0b, 0xaa, 0xcb, 0x07, + 0x9b, 0x09, 0x05, 0xa0, 0xd3, 0xa1, 0x80, 0xc2, + 0xd3, 0x59, 0x92, 0x27, 0x66, 0x1f, 0xdd, 0x76, + 0x36, 0xb3, 0x3c, 0xeb, 0xd7, 0x61, 0x94, 0xb1, + 0xf8, 0x3a, 0xe0, 0xba, 0x91, 0x0f, 0xef, 0x72, + 0x2b, 0x26, 0xc6, 0xb8, 0x6d, 0x0b, 0xdb, 0x60, + 0xf8, 0xb4, 0x98, 0xd7, 0x8b, 0x8d, 0xfb, 0xa7, + 0x4e, 0x27, 0xeb, 0x00, 0xe8, 0xf7, 0x5a, 0xec, + 0xf5, 0x60, 0x28, 0x37, 0xb2, 0xc4, 0x13, 0x48, + 0x2a, 0xe1, 0x34, 0xb2, 0x53, 0xcb, 0x44, 0x34, + 0x08, 0x7e, 0x56, 0x5c, 0x2b, 0x9b, 0xe2, 0xca, + 0x90, 0xb0, 0x57, 0xee, 0x10, 0x88, 0x39, 0x84, + 0xc6, 0x66, 0x07, 0x50, 0x63, 0xcc, 0x2a, 0x7c, + 0x99, 0x8c, 0x05, 0xf9, 0xf0, 0xb8, 0x62, 0xf0, + 0x92, 0xf7, 0x2a, 0x4a, 0x17, 0x14, 0x78, 0xa1, + 0x71, 0xb6, 0x42, 0xf0, 0x87, 0xa8, 0xa4, 0x48, + 0xeb, 0xdb, 0xed, 0x8a, 0x15, 0x19, 0x1a, 0xd9, + 0xfe, 0x6f, 0x07, 0x93, 0x5d, 0x39, 0xe8, 0x0e, + 0x47, 0xe6, 0x7a, 0x7d, 0x52, 0x2e, 0x40, 0x6f, + 0x31, 0x1b, 0xf6, 0x0c, 0xc2, 0x83, 0xae, 0xc1, + 0xf0, 0xf5, 0x71, 0xcd, 0xe2, 0xf5, 0x19, 0xb6, + 0xd8, 0xb0, 0x4d, 0xa9, 0x51, 0x1c, 0xb4, 0xaf, + 0x69, 0x9a, 0x89, 0xb6, 0x5b, 0x4d, 0xfa, 0x1b, + 0xca, 0xc8, 0x61, 0x92, 0x3a, 0xd6, 0x76, 0xad, + 0x5d, 0xa6, 0x17, 0x60, 0x3e, 0xea, 0x94, 0xcf, + 0x6d, 0x1b, 0x98, 0x5c, 0x19, 0x9e, 0x4e, 0xd3, + 0x21, 0x55, 0xda, 0xe3, +}; + +static void torture_decode_rc4_passwd_buffer(void **state) +{ + char *password_decoded = NULL; + size_t password_decoded_len = 0; + DATA_BLOB session_key = data_blob_const("SystemLibraryDTC", 16); + struct samr_CryptPasswordEx out_pwd_buf = { + .data = {0}, + }; + NTSTATUS status; + bool ok; + + memcpy(out_pwd_buf.data, + encrypted_test_blob, + sizeof(out_pwd_buf.data)); + + status = decode_rc4_passwd_buffer(&session_key, &out_pwd_buf); + assert_true(NT_STATUS_IS_OK(status)); + + ok = decode_pw_buffer(NULL, + out_pwd_buf.data, + &password_decoded, + &password_decoded_len, + CH_UTF16); + assert_true(ok); + assert_int_equal(password_decoded_len, strlen(PASSWORD)); + assert_string_equal(password_decoded, PASSWORD); +} + +static void torture_rc4_passwd_buffer(void **state) +{ + char *password_decoded = NULL; + size_t password_decoded_len = 0; + DATA_BLOB session_key = data_blob_const("SystemLibraryDTC", 16); + struct samr_CryptPasswordEx out_pwd_buf = { + .data = {0}, + }; + NTSTATUS status; + bool ok; + + status = init_samr_CryptPasswordEx(PASSWORD, + &session_key, + &out_pwd_buf); + assert_true(NT_STATUS_IS_OK(status)); + + status = decode_rc4_passwd_buffer(&session_key, &out_pwd_buf); + assert_true(NT_STATUS_IS_OK(status)); + + ok = decode_pw_buffer(NULL, + out_pwd_buf.data, + &password_decoded, + &password_decoded_len, + CH_UTF16); + assert_true(ok); + assert_int_equal(password_decoded_len, strlen(PASSWORD)); + assert_string_equal(password_decoded, PASSWORD); + talloc_free(password_decoded); +} + +static void torture_endode_decode_rc4_passwd_buffer(void **state) +{ + char *password_decoded = NULL; + size_t password_decoded_len = 0; + DATA_BLOB session_key = data_blob_const("SystemLibraryDTC", 16); + struct samr_CryptPasswordEx out_pwd_buf = { + .data = {0}, + }; + NTSTATUS status; + bool ok; + + status = encode_rc4_passwd_buffer(PASSWORD, + &session_key, + &out_pwd_buf); + assert_true(NT_STATUS_IS_OK(status)); + + status = decode_rc4_passwd_buffer(&session_key, &out_pwd_buf); + assert_true(NT_STATUS_IS_OK(status)); + + ok = decode_pw_buffer(NULL, + out_pwd_buf.data, + &password_decoded, + &password_decoded_len, + CH_UTF16); + assert_true(ok); + assert_int_equal(password_decoded_len, strlen(PASSWORD)); + assert_string_equal(password_decoded, PASSWORD); + talloc_free(password_decoded); +} + +static void torture_decode_wkssvc_join_password_buffer(void **state) +{ + DATA_BLOB session_key = data_blob_const("SystemLibraryDTC", 16); + struct wkssvc_PasswordBuffer pwd_buf = { + .data = {0}, + }; + char *password_decoded = NULL; + TALLOC_CTX *mem_ctx = NULL; + WERROR werr; + + mem_ctx = talloc_new(NULL); + assert_non_null(mem_ctx); + + memcpy(pwd_buf.data, + encrypted_wkssvc_test_blob, + sizeof(pwd_buf.data)); + + werr = decode_wkssvc_join_password_buffer(mem_ctx, + &pwd_buf, + &session_key, + &password_decoded); + assert_true(W_ERROR_IS_OK(werr)); + assert_non_null(password_decoded); + assert_string_equal(password_decoded, PASSWORD); + + TALLOC_FREE(mem_ctx); +} + +static void torture_wkssvc_join_password_buffer(void **state) +{ + DATA_BLOB session_key = data_blob_const("SystemLibraryDTC", 16); + struct wkssvc_PasswordBuffer *pwd_buf = NULL; + char *password_decoded = NULL; + TALLOC_CTX *mem_ctx = NULL; + WERROR werr; + + mem_ctx = talloc_new(NULL); + assert_non_null(mem_ctx); + + werr = encode_wkssvc_join_password_buffer(mem_ctx, + PASSWORD, + &session_key, + &pwd_buf); + assert_true(W_ERROR_IS_OK(werr)); + assert_non_null(pwd_buf); + + werr = decode_wkssvc_join_password_buffer(mem_ctx, + pwd_buf, + &session_key, + &password_decoded); + assert_true(W_ERROR_IS_OK(werr)); + assert_non_null(password_decoded); + assert_string_equal(password_decoded, PASSWORD); + + TALLOC_FREE(mem_ctx); +} + +int main(int argc, char *argv[]) +{ + int rc; + const struct CMUnitTest tests[] = { + cmocka_unit_test(torture_decode_rc4_passwd_buffer), + cmocka_unit_test(torture_rc4_passwd_buffer), + cmocka_unit_test(torture_endode_decode_rc4_passwd_buffer), + cmocka_unit_test(torture_decode_wkssvc_join_password_buffer), + cmocka_unit_test(torture_wkssvc_join_password_buffer), + }; + + if (argc == 2) { + cmocka_set_test_filter(argv[1]); + } + cmocka_set_message_output(CM_OUTPUT_SUBUNIT); + + rc = cmocka_run_group_tests(tests, NULL, NULL); + + return rc; +} diff --git a/libcli/auth/tests/test_schannel.c b/libcli/auth/tests/test_schannel.c new file mode 100644 index 0000000..b1c88fd --- /dev/null +++ b/libcli/auth/tests/test_schannel.c @@ -0,0 +1,305 @@ +/* + * Unix SMB/CIFS implementation. + * + * Copyright (C) 2019 Guenther Deschner + * + * 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 . + */ + +#include +#include +#include +#include +#include + +#include "includes.h" +#include "auth/gensec/schannel.c" + +static void torture_schannel_seal_flags(void **state, uint32_t flags, + const DATA_BLOB session_key, + const DATA_BLOB seq_num_initial, + const DATA_BLOB confounder_initial, + const DATA_BLOB confounder_expected, + const DATA_BLOB clear_initial, + const DATA_BLOB crypt_expected) +{ + NTSTATUS status; + struct schannel_state *schannel_state; + struct netlogon_creds_CredentialState *creds; + uint8_t confounder[8]; + DATA_BLOB io; + + assert_int_equal(session_key.length, 16); + assert_int_equal(seq_num_initial.length, 8); + assert_int_equal(confounder_initial.length, 8); + assert_int_equal(confounder_expected.length, 8); + assert_int_equal(clear_initial.length, crypt_expected.length); + + DEBUG(0,("checking buffer size: %d\n", (int)clear_initial.length)); + + schannel_state = talloc_zero(NULL, struct schannel_state); + assert_non_null(schannel_state); + creds = talloc_zero(schannel_state, + struct netlogon_creds_CredentialState); + assert_non_null(creds); + schannel_state->creds = creds; + + io = data_blob_dup_talloc(schannel_state, clear_initial); + assert_non_null(io.data); + assert_int_equal(io.length, clear_initial.length); + + schannel_state->creds->negotiate_flags = flags; + memcpy(schannel_state->creds->session_key, session_key.data, 16); + + memcpy(confounder, confounder_initial.data, 8); + + DEBUG(0,("confounder before crypt:\n")); + dump_data(0, confounder, 8); + dump_data(0, seq_num_initial.data, 8); + dump_data(0, io.data, io.length); + + status = netsec_do_seal(schannel_state, + seq_num_initial.data, + confounder, + io.data, + io.length, + true); + + assert_true(NT_STATUS_IS_OK(status)); + dump_data(0, io.data, io.length); + DEBUG(0,("confounder after crypt:\n")); + dump_data(0, confounder, 8); + dump_data(0, seq_num_initial.data, 8); + assert_memory_equal(io.data, crypt_expected.data, crypt_expected.length); + assert_memory_equal(confounder, confounder_expected.data, confounder_expected.length); + + status = netsec_do_seal(schannel_state, + seq_num_initial.data, + confounder, + io.data, + io.length, + false); + + assert_true(NT_STATUS_IS_OK(status)); + dump_data(0, io.data, io.length); + DEBUG(0,("confounder after decrypt:\n")); + dump_data(0, confounder, 8); + dump_data(0, seq_num_initial.data, 8); + assert_memory_equal(io.data, clear_initial.data, clear_initial.length); + assert_memory_equal(confounder, confounder_initial.data, confounder_initial.length); + + talloc_free(schannel_state); +} + +static void torture_schannel_seal_rc4(void **state) +{ + const uint8_t _session_key[16] = { + 0x14, 0xD5, 0x7F, 0x8D, 0x8E, 0xCF, 0xFB, 0x56, + 0x71, 0x29, 0x9D, 0x9C, 0x2A, 0x75, 0x00, 0xA1 + }; + const DATA_BLOB session_key = data_blob_const(_session_key, 16); + const uint8_t _seq_num_initial[8] = { + 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 + }; + const DATA_BLOB seq_num_initial = + data_blob_const(_seq_num_initial, 8); + const uint8_t _confounder_initial[8] = { + 0x1A, 0x5A, 0xE8, 0xC7, 0xBE, 0x4F, 0x1F, 0x07 + }; + const DATA_BLOB confounder_initial = + data_blob_const(_confounder_initial, 8); + const uint8_t _confounder_expected[8] = { + 0x25, 0x4A, 0x9C, 0x15, 0x82, 0x3E, 0x4A, 0x42 + }; + const DATA_BLOB confounder_expected = + data_blob_const(_confounder_expected, 8); + const uint8_t _clear_initial[] = { + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x8A, 0xE3, 0x13, 0x71, 0x02, 0xF4, 0x36, 0x71, + 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x40, 0x28, 0x00, 0x78, 0x57, 0x34, 0x12, + 0x34, 0x12, 0xCD, 0xAB, 0xEF, 0x00, 0x01, 0x23, + 0x45, 0x67, 0x89, 0xAB, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, + 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + const DATA_BLOB clear_initial = data_blob_const(_clear_initial, + sizeof(_clear_initial)); + const uint8_t crypt_buffer[] = { + 0x3E, 0x10, 0x74, 0xD2, 0x3C, 0x71, 0x57, 0x45, + 0xB8, 0xAA, 0xCF, 0xE3, 0x84, 0xBE, 0xC4, 0x00, + 0xF4, 0x4D, 0x88, 0x0A, 0x9B, 0xCC, 0x53, 0xFC, + 0x32, 0xAA, 0x8E, 0x4B, 0x0E, 0xDE, 0x5F, 0x7D, + 0x6D, 0x31, 0x4E, 0xAB, 0xE0, 0x7D, 0x37, 0x9D, + 0x3D, 0x16, 0xD8, 0xBA, 0x6A, 0xB0, 0xD0, 0x99, + 0x14, 0x05, 0x37, 0xCF, 0x63, 0xD3, 0xD7, 0x60, + 0x63, 0x3C, 0x03, 0x0A, 0x30, 0xA0, 0x3E, 0xC7, + 0xDA, 0x94, 0x3B, 0x40, 0x63, 0x74, 0xEF, 0xCF, + 0xE5, 0x48, 0x87, 0xE9, 0x6A, 0x5A, 0xC7, 0x61, + 0xF7, 0x09, 0xB7, 0x7C, 0xDE, 0xDB, 0xB0, 0x94, + 0x9B, 0x99, 0xC0, 0xA7, 0x7E, 0x78, 0x09, 0x35, + 0xB4, 0xF4, 0x11, 0xC3, 0xB3, 0x77, 0xB5, 0x77, + 0x25, 0xEE, 0xFD, 0x2F, 0x9A, 0x15, 0x95, 0x27, + 0x08, 0xDA, 0xD0, 0x28, 0xD6, 0x31, 0xB4, 0xB7, + 0x7A, 0x19, 0xBB, 0xF3, 0x78, 0xF8, 0xC2, 0x5B + }; + const DATA_BLOB crypt_expected = data_blob_const(crypt_buffer, + sizeof(crypt_buffer)); + int buffer_sizes[] = { + 0, 1, 3, 7, 8, 9, 15, 16, 17 + }; + int i; + + torture_schannel_seal_flags(state, 0, + session_key, + seq_num_initial, + confounder_initial, + confounder_expected, + clear_initial, + crypt_expected); + + /* repeat the test for varying buffer sizes */ + + for (i = 0; i < ARRAY_SIZE(buffer_sizes); i++) { + DATA_BLOB clear_initial_trunc = + data_blob_const(clear_initial.data, buffer_sizes[i]); + DATA_BLOB crypt_expected_trunc = + data_blob_const(crypt_expected.data, buffer_sizes[i]); + torture_schannel_seal_flags(state, 0, + session_key, + seq_num_initial, + confounder_initial, + confounder_expected, + clear_initial_trunc, + crypt_expected_trunc); + } +} + +static void torture_schannel_seal_aes(void **state) +{ + const uint8_t _session_key[16] = { + 0x8E, 0xE8, 0x27, 0x85, 0x83, 0x41, 0x3C, 0x8D, + 0xC9, 0x54, 0x70, 0x75, 0x8E, 0xC9, 0x69, 0x91 + }; + const DATA_BLOB session_key = data_blob_const(_session_key, 16); + const uint8_t _seq_num_initial[8] = { + 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 + }; + const DATA_BLOB seq_num_initial = + data_blob_const(_seq_num_initial, 8); + const uint8_t _confounder_initial[8] = { + 0x6E, 0x09, 0x25, 0x94, 0x01, 0xA0, 0x09, 0x31 + }; + const DATA_BLOB confounder_initial = + data_blob_const(_confounder_initial, 8); + const uint8_t _confounder_expected[8] = { + 0xCA, 0xFB, 0xAC, 0xFB, 0xA8, 0x26, 0x75, 0x2A + }; + const DATA_BLOB confounder_expected = + data_blob_const(_confounder_expected, 8); + const uint8_t _clear_initial[] = { + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x8A, 0xE3, 0x13, 0x71, 0x02, 0xF4, 0x36, 0x71, + 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x40, 0x28, 0x00, 0x78, 0x57, 0x34, 0x12, + 0x34, 0x12, 0xCD, 0xAB, 0xEF, 0x00, 0x01, 0x23, + 0x45, 0x67, 0x89, 0xAB, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, + 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + const DATA_BLOB clear_initial = data_blob_const(_clear_initial, + sizeof(_clear_initial)); + const uint8_t crypt_buffer[] = { + 0xE2, 0xE5, 0xE3, 0x26, 0x45, 0xFB, 0xFC, 0xF3, + 0x9C, 0x14, 0xDD, 0xE1, 0x39, 0x23, 0xE0, 0x55, + 0xED, 0x8F, 0xF4, 0x92, 0xA1, 0xBD, 0xDC, 0x40, + 0x58, 0x6F, 0xD2, 0x5B, 0xF9, 0xC9, 0xA3, 0x87, + 0x46, 0x4B, 0x7F, 0xB2, 0x03, 0xD2, 0x35, 0x22, + 0x3E, 0x70, 0x9F, 0x1E, 0x3F, 0x1F, 0xDB, 0x7D, + 0x79, 0x88, 0x5A, 0x3D, 0xD3, 0x40, 0x1E, 0x69, + 0xD7, 0xE2, 0x1D, 0x5A, 0xE9, 0x3B, 0xE1, 0xE2, + 0x98, 0xFD, 0xCB, 0x3A, 0xF7, 0xB5, 0x1C, 0xF8, + 0xCA, 0x02, 0x00, 0x99, 0x9F, 0x0C, 0x01, 0xE6, + 0xD2, 0x00, 0xAF, 0xE0, 0x51, 0x88, 0x62, 0x50, + 0xB7, 0xE8, 0x6D, 0x63, 0x4B, 0x97, 0x05, 0xC1, + 0xD4, 0x83, 0x96, 0x29, 0x80, 0xAE, 0xD8, 0xA2, + 0xED, 0xC9, 0x5D, 0x0D, 0x29, 0xFF, 0x2C, 0x23, + 0x02, 0xFA, 0x3B, 0xEE, 0xE8, 0xBA, 0x06, 0x01, + 0x95, 0xDF, 0x80, 0x76, 0x0B, 0x17, 0x0E, 0xD8 + }; + const DATA_BLOB crypt_expected = data_blob_const(crypt_buffer, + sizeof(crypt_buffer)); + int buffer_sizes[] = { + 0, 1, 3, 7, 8, 9, 15, 16, 17 + }; + int i; + + torture_schannel_seal_flags(state, NETLOGON_NEG_SUPPORTS_AES, + session_key, + seq_num_initial, + confounder_initial, + confounder_expected, + clear_initial, + crypt_expected); + + /* repeat the test for varying buffer sizes */ + + for (i = 0; i < ARRAY_SIZE(buffer_sizes); i++) { + DATA_BLOB clear_initial_trunc = + data_blob_const(clear_initial.data, buffer_sizes[i]); + DATA_BLOB crypt_expected_trunc = + data_blob_const(crypt_expected.data, buffer_sizes[i]); + torture_schannel_seal_flags(state, NETLOGON_NEG_SUPPORTS_AES, + session_key, + seq_num_initial, + confounder_initial, + confounder_expected, + clear_initial_trunc, + crypt_expected_trunc); + } +} + +int main(int argc, char *argv[]) +{ + int rc; + const struct CMUnitTest tests[] = { + cmocka_unit_test(torture_schannel_seal_rc4), + cmocka_unit_test(torture_schannel_seal_aes), + }; + + if (argc == 2) { + cmocka_set_test_filter(argv[1]); + } + cmocka_set_message_output(CM_OUTPUT_SUBUNIT); + + rc = cmocka_run_group_tests(tests, NULL, NULL); + + return rc; +} -- cgit v1.2.3