2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Catalyst.Net Ltd 2024
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 <https://www.gnu.org/licenses/>.
28 #include "libcli/util/ntstatus.h"
29 #include "lib/crypto/gkdi.h"
30 #include "lib/crypto/gmsa.h"
32 #include "librpc/ndr/libndr.h"
33 #include "librpc/gen_ndr/gmsa.h"
34 #include "librpc/gen_ndr/ndr_gmsa.h"
35 #include "lib/util/time.h"
36 #include "libcli/security/dom_sid.h"
38 static void test_password_based_on_key_id(void **state)
40 static const uint8_t root_key_data[] = {
41 152, 203, 215, 84, 113, 216, 118, 177, 81, 128, 50, 160, 148,
42 132, 82, 244, 65, 179, 164, 219, 209, 14, 33, 131, 178, 193,
43 80, 248, 126, 23, 66, 227, 45, 221, 171, 12, 247, 15, 62,
44 179, 164, 217, 123, 179, 106, 118, 228, 74, 12, 2, 241, 229,
45 139, 55, 237, 155, 220, 122, 200, 245, 129, 222, 37, 15};
46 static const struct ProvRootKey root_key = {
47 .version = root_key_version_1,
48 .id = {.time_low = 0x170e972,
50 .time_hi_and_version = 0xaf4c,
51 .clock_seq = {0x99, 0x3b},
52 .node = {0xe0, 0x52, 0xd5, 0xbd, 0x12, 0x16}},
55 .data = discard_const_p(uint8_t, root_key_data),
56 .length = sizeof root_key_data,
60 .domain_id = "example.com",
62 .id = KDF_ALGORITHM_SP800_108_CTR_HMAC,
63 .param.sp800_108 = KDF_PARAM_SHA512,
65 static const uint8_t expected[GMSA_PASSWORD_LEN] = {
66 114, 92, 31, 204, 138, 249, 1, 76, 192, 65, 52, 248, 247,
67 191, 30, 213, 25, 38, 81, 21, 183, 167, 154, 102, 190, 234,
68 234, 116, 114, 18, 141, 208, 143, 38, 178, 115, 195, 26, 199,
69 214, 176, 229, 128, 160, 147, 249, 245, 67, 165, 191, 192, 78,
70 224, 50, 115, 8, 207, 124, 178, 121, 67, 135, 125, 113, 79,
71 0, 131, 43, 74, 48, 171, 239, 183, 228, 50, 212, 202, 215,
72 188, 182, 94, 127, 117, 217, 91, 17, 90, 80, 158, 176, 204,
73 151, 244, 107, 139, 65, 94, 148, 216, 212, 97, 53, 54, 253,
74 6, 201, 94, 93, 250, 213, 12, 82, 162, 246, 197, 254, 205,
75 8, 19, 153, 66, 72, 60, 167, 28, 65, 39, 218, 147, 82,
76 162, 11, 177, 78, 231, 200, 66, 121, 9, 196, 240, 7, 148,
77 190, 151, 96, 214, 246, 7, 110, 85, 0, 246, 28, 121, 3,
78 61, 212, 204, 101, 153, 121, 100, 91, 65, 28, 225, 241, 123,
79 115, 105, 138, 74, 187, 74, 188, 59, 17, 201, 229, 158, 170,
80 184, 141, 237, 179, 246, 135, 104, 204, 56, 228, 156, 182, 26,
81 90, 151, 147, 25, 142, 47, 172, 183, 165, 222, 240, 95, 63,
82 79, 88, 35, 205, 76, 26, 229, 107, 46, 16, 202, 102, 196,
83 18, 140, 211, 242, 226, 198, 154, 97, 199, 44, 220, 186, 76,
84 215, 54, 196, 44, 140, 145, 252, 99, 229, 179, 74, 150, 154,
85 70, 226, 45, 122, 156, 156, 75, 83, 24};
86 uint8_t out[GMSA_PASSWORD_NULL_TERMINATED_LEN];
87 TALLOC_CTX *mem_ctx = NULL;
89 mem_ctx = talloc_new(NULL);
90 assert_non_null(mem_ctx);
93 /* An arbitrary GKID. */
94 const struct Gkid gkid = {362, 0, 23};
95 /* An arbitrary time in the past. */
96 const NTTIME current_nt_time = 133524411756072082;
97 struct dom_sid account_sid;
102 "S-1-5-21-4119591868-3001629103-3445594911-48026",
106 /* Derive a password from the root key. */
107 status = gmsa_password_based_on_key_id(mem_ctx,
113 assert_true(NT_STATUS_IS_OK(status));
114 assert_memory_equal(expected, out, GMSA_PASSWORD_LEN);
118 uint64_t query_interval = 0;
119 uint64_t unchanged_interval = 0;
120 const struct MANAGEDPASSWORD_BLOB managed_pwd = {
123 .query_interval = &query_interval,
124 .unchanged_interval = &unchanged_interval}};
125 DATA_BLOB packed_blob = {};
126 enum ndr_err_code err;
128 err = ndr_push_struct_blob(
132 (ndr_push_flags_fn_t)ndr_push_MANAGEDPASSWORD_BLOB);
133 assert_int_equal(NDR_ERR_SUCCESS, err);
136 talloc_free(mem_ctx);
139 static void assert_gkid_equal(const struct Gkid g1, const struct Gkid g2)
141 assert_int_equal(g1.l0_idx, g2.l0_idx);
142 assert_int_equal(g1.l1_idx, g2.l1_idx);
143 assert_int_equal(g1.l2_idx, g2.l2_idx);
146 static void test_gkdi_rollover_interval(void **state)
151 ok = gkdi_rollover_interval(0, &interval);
153 assert_int_equal(0, interval);
155 ok = gkdi_rollover_interval(1, &interval);
157 assert_int_equal(UINT64_C(720000000000), interval);
159 ok = gkdi_rollover_interval(2, &interval);
161 assert_int_equal(UINT64_C(1440000000000), interval);
163 ok = gkdi_rollover_interval(3, &interval);
165 assert_int_equal(UINT64_C(2520000000000), interval);
167 ok = gkdi_rollover_interval(4, &interval);
169 assert_int_equal(UINT64_C(3240000000000), interval);
171 ok = gkdi_rollover_interval(5, &interval);
173 assert_int_equal(UINT64_C(4320000000000), interval);
175 ok = gkdi_rollover_interval(-1, &interval);
178 ok = gkdi_rollover_interval(-2, &interval);
181 ok = gkdi_rollover_interval(10675199, &interval);
183 assert_int_equal(UINT64_C(9223371720000000000), interval);
185 ok = gkdi_rollover_interval(-10675198, &interval);
188 ok = gkdi_rollover_interval(10675200, &interval);
190 assert_int_equal(UINT64_C(9223372800000000000), interval);
192 ok = gkdi_rollover_interval(-10675199, &interval);
195 ok = gkdi_rollover_interval(21350398, &interval);
197 * If we accepted this high of an interval, the result would be
198 * 18446743800000000000.
202 ok = gkdi_rollover_interval(-21350397, &interval);
205 ok = gkdi_rollover_interval(21350399, &interval);
206 assert_false(ok); /* too large to be represented */
208 ok = gkdi_rollover_interval(-21350398, &interval);
209 assert_false(ok); /* too small to be represented */
212 static void assert_get_interval_id(const NTTIME time,
213 const struct Gkid expected_gkid)
216 const bool valid = gkid_is_valid(expected_gkid);
221 const struct Gkid interval_id = gkdi_get_interval_id(time);
222 assert_gkid_equal(expected_gkid, interval_id);
226 static void test_get_interval_id(void **state)
228 assert_get_interval_id(0, Gkid(0, 0, 0));
230 assert_get_interval_id(gkdi_key_cycle_duration - 1, Gkid(0, 0, 0));
232 assert_get_interval_id(gkdi_key_cycle_duration, Gkid(0, 0, 1));
234 assert_get_interval_id(27 * gkdi_key_cycle_duration, Gkid(0, 0, 27));
236 assert_get_interval_id((gkdi_l2_key_iteration - 1) *
237 gkdi_key_cycle_duration,
238 Gkid(0, 0, gkdi_l2_key_iteration - 1));
240 assert_get_interval_id(gkdi_l2_key_iteration * gkdi_key_cycle_duration,
243 assert_get_interval_id(17 * gkdi_l2_key_iteration *
244 gkdi_key_cycle_duration,
247 assert_get_interval_id(((gkdi_l1_key_iteration - 1) *
248 gkdi_l2_key_iteration +
249 3) * gkdi_key_cycle_duration,
250 Gkid(0, gkdi_l1_key_iteration - 1, 3));
252 assert_get_interval_id(gkdi_l1_key_iteration * gkdi_l2_key_iteration *
253 gkdi_key_cycle_duration,
256 assert_get_interval_id(((1234 * gkdi_l1_key_iteration + 8) *
257 gkdi_l2_key_iteration +
258 13) * gkdi_key_cycle_duration,
261 assert_get_interval_id(INT64_MAX, Gkid(25019, 31, 29));
263 assert_get_interval_id(UINT64_MAX, Gkid(50039, 31, 27));
266 static void test_get_key_start_time(void **state)
268 NTTIME start_time = 0;
271 /* Try passing an invalid GKID. */
272 ok = gkdi_get_key_start_time(invalid_gkid, &start_time);
275 /* Try passing an L1 GKID rather than an L2 GKID. */
276 ok = gkdi_get_key_start_time(Gkid(0, 0, -1), &start_time);
279 /* Test some L2 GKIDs. */
281 ok = gkdi_get_key_start_time(Gkid(0, 0, 0), &start_time);
283 assert_int_equal(0, start_time);
285 ok = gkdi_get_key_start_time(Gkid(0, 0, 1), &start_time);
287 assert_int_equal(gkdi_key_cycle_duration, start_time);
289 ok = gkdi_get_key_start_time(Gkid(123, 18, 2), &start_time);
291 assert_int_equal(126530 * gkdi_key_cycle_duration, start_time);
293 ok = gkdi_get_key_start_time(Gkid(25019, 31, 29), &start_time);
295 assert_int_equal(25620477 * gkdi_key_cycle_duration, start_time);
297 ok = gkdi_get_key_start_time(Gkid(25019, 31, 30), &start_time);
299 assert_int_equal(UINT64_C(25620478) * gkdi_key_cycle_duration,
302 ok = gkdi_get_key_start_time(Gkid(50039, 31, 27), &start_time);
304 assert_int_equal(UINT64_C(51240955) * gkdi_key_cycle_duration,
308 * Test GKIDs so high that their start times can’t be represented in
312 ok = gkdi_get_key_start_time(Gkid(50039, 31, 28), &start_time);
315 ok = gkdi_get_key_start_time(Gkid(INT32_MAX, 31, 31), &start_time);
319 int main(int argc, char *argv[])
321 const struct CMUnitTest tests[] = {
322 cmocka_unit_test(test_password_based_on_key_id),
323 cmocka_unit_test(test_gkdi_rollover_interval),
324 cmocka_unit_test(test_get_interval_id),
325 cmocka_unit_test(test_get_key_start_time),
329 cmocka_set_test_filter(argv[1]);
331 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
332 return cmocka_run_group_tests(tests, NULL, NULL);