Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * Copyright (C) 2022 Andreas Schneider <asn@samba.org>
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 3 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, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 :
21 : #include <stdarg.h>
22 : #include <stddef.h>
23 : #include <stdint.h>
24 : #include <setjmp.h>
25 : #include <cmocka.h>
26 :
27 : #include "libcli/auth/smbencrypt.c"
28 :
29 : /* The following hexdumps are from a Windows Server 2022 time trace */
30 : static const uint8_t plaintext_data[] = {
31 : 0x14, 0x00, 0x50, 0x00, 0x61, 0x00, 0x24, 0x00,
32 : 0x24, 0x00, 0x77, 0x00, 0x30, 0x00, 0x72, 0x00,
33 : 0x64, 0x00, 0x40, 0x00, 0x32, 0x00, 0xc2, 0x34,
34 : 0x7d, 0x21, 0x79, 0x05, 0xef, 0x88, 0xd7, 0x11,
35 : 0xec, 0xe2, 0xce, 0xb5, 0xd4, 0x4d, 0x64, 0x2d,
36 : 0x15, 0x79, 0x01, 0x39, 0xb8, 0xb9, 0x89, 0x5c,
37 : 0x4e, 0x71, 0xbd, 0xf0, 0x14, 0x0c, 0x87, 0x72,
38 : 0xa5, 0xfa, 0x90, 0xbe, 0x62, 0x55, 0xad, 0x7f,
39 : 0xe9, 0x7f, 0x0d, 0x20, 0x19, 0x3a, 0x76, 0xbe,
40 : 0xb2, 0x14, 0x6d, 0x5b, 0x25, 0x1c, 0x67, 0x3a,
41 : 0x23, 0x45, 0x1f, 0x7e, 0x36, 0xa0, 0x95, 0xb7,
42 : 0xa7, 0xb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b2, 0xfa, 0x23, 0x37, 0xeb,
85 : 0xb0, 0x04, 0x53, 0xcb, 0xa1, 0xa9, 0xc4, 0x88,
86 : 0xdd, 0xf9, 0x80, 0xf5, 0xa9, 0xcd, 0x7b, 0xf8,
87 : 0x77, 0x0b, 0x19, 0x84, 0x4c, 0xef, 0x2c, 0x14,
88 : 0xa1, 0xdc, 0x9f, 0x2f, 0x41, 0xd0, 0xd0, 0x33,
89 : 0x29, 0x8a, 0xb9, 0x39, 0x7e, 0xc9, 0x7f, 0xe7,
90 : 0x63, 0x64, 0xa4, 0x7b, 0x4a, 0x6a, 0x33, 0xa7,
91 : 0xaa, 0xf6, 0xca, 0x98, 0xc4, 0x9b, 0x62, 0x5b,
92 : 0xcd, 0x53, 0x82, 0xbf, 0xf0, 0x0b, 0x9c, 0xe7,
93 : 0xb2, 0x44, 0x1b, 0x88, 0x71, 0x61, 0xa1, 0x36,
94 : 0x9e, 0x7a, 0x0a, 0x3c, 0x20, 0xd8, 0xff, 0xa1,
95 : 0x23, 0x66
96 : };
97 :
98 1 : static void torture_encode_pwd_buffer514_from_str(void **state)
99 : {
100 1 : uint8_t data[514] = {0};
101 1 : bool ok;
102 :
103 1 : ok = encode_pwd_buffer514_from_str(data, "Pa$$w0rd@2", STR_UNICODE);
104 1 : assert_true(ok);
105 :
106 : /* We can only compare the first 22 bytes as the rest is random data */
107 1 : assert_memory_equal(data, plaintext_data, 22);
108 1 : assert_memory_not_equal(data + 22,
109 : plaintext_data + 22,
110 : sizeof(plaintext_data) - 22);
111 1 : }
112 :
113 1 : static void torture_extract_pwd_blob_from_buffer514(void **state)
114 : {
115 1 : DATA_BLOB new_password = {
116 : .length = 0,
117 : };
118 1 : bool ok;
119 :
120 1 : ok = extract_pwd_blob_from_buffer514(NULL, plaintext_data, &new_password);
121 1 : assert_true(ok);
122 1 : assert_int_equal(new_password.length, 20);
123 1 : assert_memory_equal(new_password.data,
124 : plaintext_data + 2,
125 : new_password.length);
126 1 : data_blob_free(&new_password);
127 1 : }
128 :
129 1 : static void torture_decode_pwd_string_from_buffer514(void **state)
130 : {
131 1 : DATA_BLOB decoded_password = {
132 : .length = 0,
133 : };
134 1 : bool ok;
135 :
136 1 : ok = decode_pwd_string_from_buffer514(NULL,
137 : plaintext_data,
138 : CH_UTF16,
139 : &decoded_password);
140 1 : assert_true(ok);
141 1 : assert_memory_equal(decoded_password.data, "Pa$$w0rd@2", decoded_password.length);
142 1 : data_blob_free(&decoded_password);
143 1 : }
144 :
145 1 : int main(int argc, char *argv[])
146 : {
147 1 : int rc;
148 1 : const struct CMUnitTest tests[] = {
149 : cmocka_unit_test(torture_encode_pwd_buffer514_from_str),
150 : cmocka_unit_test(torture_extract_pwd_blob_from_buffer514),
151 : cmocka_unit_test(torture_decode_pwd_string_from_buffer514),
152 : };
153 :
154 1 : if (argc == 2) {
155 0 : cmocka_set_test_filter(argv[1]);
156 : }
157 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
158 :
159 1 : rc = cmocka_run_group_tests(tests, NULL, NULL);
160 :
161 1 : return rc;
162 : }
|