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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
|
#!/usr/bin/env python3
# Unix SMB/CIFS implementation.
# Copyright (C) Catalyst.Net Ltd 2023
#
# 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 <https://www.gnu.org/licenses/>.
#
import sys
import os
sys.path.insert(0, "bin/python")
os.environ["PYTHONUNBUFFERED"] = "1"
import secrets
from typing import ClassVar, Optional
from samba.dcerpc import gkdi, misc
from samba.gkdi import (
Algorithm,
Gkid,
KEY_CYCLE_DURATION,
KEY_LEN_BYTES,
MAX_CLOCK_SKEW,
NtTime,
NtTimeDelta,
SeedKeyPair,
)
from samba.hresult import HRES_E_INVALIDARG, HRES_NTE_BAD_KEY, HRES_NTE_NO_KEY
from samba.nt_time import timedelta_from_nt_time_delta
from samba.tests.gkdi import GetKeyError, GkdiBaseTest, ROOT_KEY_START_TIME
from samba.tests.krb5.kdc_base_test import KDCBaseTest
class GkdiKdcBaseTest(GkdiBaseTest, KDCBaseTest):
def new_root_key(self, *args, **kwargs) -> misc.GUID:
samdb = self.get_samdb()
domain_dn = self.get_server_dn(samdb)
return self.create_root_key(samdb, domain_dn, *args, **kwargs)
def gkdi_conn(self) -> gkdi.gkdi:
# The seed keys used by Group Managed Service Accounts correspond to the
# Enterprise DCs SID (S-1-5-9); as such they can be retrieved only by
# server accounts.
return self.gkdi_connect(
self.dc_host,
self.get_lp(),
self.get_cached_creds(account_type=self.AccountType.SERVER),
)
def check_rpc_get_key(
self, root_key_id: Optional[misc.GUID], gkid: Gkid
) -> SeedKeyPair:
got_key_pair = self.rpc_get_key(
self.gkdi_conn(), self.gmsa_sd, root_key_id, gkid
)
expected_key_pair = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
gkid,
root_key_id_hint=got_key_pair.root_key_id if root_key_id is None else None,
)
self.assertEqual(
got_key_pair.root_key_id,
expected_key_pair.root_key_id,
"root key IDs must match",
)
self.assertEqual(got_key_pair, expected_key_pair, "key pairs must match")
return got_key_pair
class GkdiExplicitRootKeyTests(GkdiKdcBaseTest):
def test_current_l0_idx(self):
"""Request a key with the current L0 index. This index is regularly
incremented every 427 days or so."""
root_key_id = self.new_root_key()
# It actually doesn’t matter what we specify for the L1 and L2 indices.
# We’ll get the same result regardless — they just cannot specify a key
# from the future.
current_gkid = self.current_gkid(self.get_samdb())
key = self.check_rpc_get_key(root_key_id, current_gkid)
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
def test_previous_l0_idx(self):
"""Request a key with a previous L0 index."""
root_key_id = self.new_root_key(use_start_time=ROOT_KEY_START_TIME)
# It actually doesn’t matter what we specify for the L1 and L2 indices.
# We’ll get the same result regardless.
previous_l0_idx = self.current_gkid(self.get_samdb()).l0_idx - 1
key = self.check_rpc_get_key(root_key_id, Gkid(previous_l0_idx, 0, 0))
# Expect to get an L1 seed key.
self.assertIsNotNone(key.l1_key)
self.assertIsNone(key.l2_key)
self.assertEqual(Gkid(previous_l0_idx, 31, 31), key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
def test_algorithm_sha1(self):
"""Test with the SHA1 algorithm."""
key = self.check_rpc_get_key(
self.new_root_key(hash_algorithm=Algorithm.SHA1),
self.current_gkid(self.get_samdb()),
)
self.assertIs(Algorithm.SHA1, key.hash_algorithm)
def test_algorithm_sha256(self):
"""Test with the SHA256 algorithm."""
key = self.check_rpc_get_key(
self.new_root_key(hash_algorithm=Algorithm.SHA256),
self.current_gkid(self.get_samdb()),
)
self.assertIs(Algorithm.SHA256, key.hash_algorithm)
def test_algorithm_sha384(self):
"""Test with the SHA384 algorithm."""
key = self.check_rpc_get_key(
self.new_root_key(hash_algorithm=Algorithm.SHA384),
self.current_gkid(self.get_samdb()),
)
self.assertIs(Algorithm.SHA384, key.hash_algorithm)
def test_algorithm_sha512(self):
"""Test with the SHA512 algorithm."""
key = self.check_rpc_get_key(
self.new_root_key(hash_algorithm=Algorithm.SHA512),
self.current_gkid(self.get_samdb()),
)
self.assertIs(Algorithm.SHA512, key.hash_algorithm)
def test_algorithm_none(self):
"""Test without a specified algorithm."""
key = self.check_rpc_get_key(
self.new_root_key(hash_algorithm=None),
self.current_gkid(self.get_samdb()),
)
self.assertIs(Algorithm.SHA256, key.hash_algorithm)
def test_future_key(self):
"""Try to request a key from the future."""
root_key_id = self.new_root_key(use_start_time=ROOT_KEY_START_TIME)
future_gkid = self.current_gkid(
self.get_samdb(),
offset=timedelta_from_nt_time_delta(
NtTimeDelta(KEY_CYCLE_DURATION + MAX_CLOCK_SKEW)
)
)
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, root_key_id, future_gkid)
self.assertEqual(
HRES_E_INVALIDARG,
err.exception.args[0],
"requesting a key from the future should fail with INVALID_PARAMETER",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, root_key_id, future_gkid)
self.assertEqual(
HRES_E_INVALIDARG,
rpc_err.exception.args[0],
"requesting a key from the future should fail with INVALID_PARAMETER",
)
def test_root_key_use_start_time_zero(self):
"""Attempt to use a root key with an effective time of zero."""
root_key_id = self.new_root_key(use_start_time=NtTime(0))
gkid = self.current_gkid(self.get_samdb())
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_NTE_BAD_KEY,
err.exception.args[0],
"using a root key with an effective time of zero should fail with BAD_KEY",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_NTE_BAD_KEY,
rpc_err.exception.args[0],
"using a root key with an effective time of zero should fail with BAD_KEY",
)
def test_root_key_use_start_time_too_low(self):
"""Attempt to use a root key with an effective time set too low."""
root_key_id = self.new_root_key(use_start_time=NtTime(ROOT_KEY_START_TIME - 1))
gkid = self.current_gkid(self.get_samdb())
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
err.exception.args[0],
"using a root key with too low effective time should fail with"
" INVALID_PARAMETER",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
rpc_err.exception.args[0],
"using a root key with too low effective time should fail with"
" INVALID_PARAMETER",
)
def test_before_valid(self):
"""Attempt to use a key before it is valid."""
gkid = self.current_gkid(self.get_samdb())
valid_start_time = NtTime(
gkid.start_nt_time() + KEY_CYCLE_DURATION + MAX_CLOCK_SKEW
)
# Using a valid root key is allowed.
valid_root_key_id = self.new_root_key(use_start_time=valid_start_time)
self.check_rpc_get_key(valid_root_key_id, gkid)
# But attempting to use a root key that is not yet valid will result in
# an INVALID_PARAMETER error.
invalid_root_key_id = self.new_root_key(use_start_time=valid_start_time + 1)
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, invalid_root_key_id, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
err.exception.args[0],
"using a key before it is valid should fail with INVALID_PARAMETER",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, invalid_root_key_id, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
rpc_err.exception.args[0],
"using a key before it is valid should fail with INVALID_PARAMETER",
)
def test_non_existent_root_key(self):
"""Attempt to use a non‐existent root key."""
root_key_id = misc.GUID(secrets.token_bytes(16))
gkid = self.current_gkid(self.get_samdb())
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_NTE_NO_KEY,
err.exception.args[0],
"using a non‐existent root key should fail with NO_KEY",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_NTE_NO_KEY,
rpc_err.exception.args[0],
"using a non‐existent root key should fail with NO_KEY",
)
def test_root_key_wrong_length(self):
"""Attempt to use a root key that is the wrong length."""
root_key_id = self.new_root_key(data=bytes(KEY_LEN_BYTES // 2))
gkid = self.current_gkid(self.get_samdb())
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_NTE_BAD_KEY,
err.exception.args[0],
"using a root key that is the wrong length should fail with BAD_KEY",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, root_key_id, gkid)
self.assertEqual(
HRES_NTE_BAD_KEY,
rpc_err.exception.args[0],
"using a root key that is the wrong length should fail with BAD_KEY",
)
class GkdiImplicitRootKeyTests(GkdiKdcBaseTest):
_root_key: ClassVar[misc.GUID]
@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
cls._root_key = None
def setUp(self) -> None:
super().setUp()
if self._root_key is None:
# GKDI requires a root key to operate. Creating a root key here
# saves creating one before every test.
#
# We cannot delete this key after the tests have run, as Windows
# might have decided to cache it to be used in subsequent runs. It
# will keep a root key cached even if the corresponding AD object
# has been deleted, leading to various problems later.
cls = type(self)
cls._root_key = self.new_root_key(use_start_time=ROOT_KEY_START_TIME)
def test_l1_seed_key(self):
"""Request a key and expect to receive an L1 seed key."""
gkid = Gkid(300, 0, 31)
key = self.check_rpc_get_key(None, gkid)
# Expect to get an L1 seed key.
self.assertIsNotNone(key.l1_key)
self.assertIsNone(key.l2_key)
self.assertEqual(gkid, key.gkid)
def test_l2_seed_key(self):
"""Request a key and expect to receive an L2 seed key."""
gkid = Gkid(300, 0, 0)
key = self.check_rpc_get_key(None, gkid)
# Expect to get an L2 seed key.
self.assertIsNone(key.l1_key)
self.assertIsNotNone(key.l2_key)
self.assertEqual(gkid, key.gkid)
def test_both_seed_keys(self):
"""Request a key and expect to receive L1 and L2 seed keys."""
gkid = Gkid(300, 1, 0)
key = self.check_rpc_get_key(None, gkid)
# Expect to get both L1 and L2 seed keys.
self.assertIsNotNone(key.l1_key)
self.assertIsNotNone(key.l2_key)
self.assertEqual(gkid, key.gkid)
def test_both_seed_keys_no_hint(self):
"""Request a key, but don’t specify ‘root_key_id_hint’."""
gkid = Gkid(300, 1, 0)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
None,
gkid,
)
# Expect to get both L1 and L2 seed keys.
self.assertIsNotNone(key.l1_key)
self.assertIsNotNone(key.l2_key)
self.assertEqual(gkid, key.gkid)
def test_request_l0_seed_key(self):
"""Attempt to request an L0 seed key."""
gkid = Gkid.l0_seed_key(300)
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, None, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
err.exception.args[0],
"requesting an L0 seed key should fail with INVALID_PARAMETER",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, None, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
rpc_err.exception.args[0],
"requesting an L0 seed key should fail with INVALID_PARAMETER",
)
def test_request_l1_seed_key(self):
"""Attempt to request an L1 seed key."""
gkid = Gkid.l1_seed_key(300, 0)
with self.assertRaises(GetKeyError) as err:
self.get_key(self.get_samdb(), self.gmsa_sd, None, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
err.exception.args[0],
"requesting an L1 seed key should fail with INVALID_PARAMETER",
)
with self.assertRaises(GetKeyError) as rpc_err:
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, None, gkid)
self.assertEqual(
HRES_E_INVALIDARG,
rpc_err.exception.args[0],
"requesting an L1 seed key should fail with INVALID_PARAMETER",
)
def test_request_default_seed_key(self):
"""Try to make a request with the default GKID."""
gkid = Gkid.default()
self.assertRaises(
NotImplementedError,
self.get_key,
self.get_samdb(),
self.gmsa_sd,
None,
gkid,
)
self.rpc_get_key(self.gkdi_conn(), self.gmsa_sd, None, gkid)
class GkdiSelfTests(GkdiKdcBaseTest):
def test_current_l0_idx_l1_seed_key(self):
"""Request a key with the current L0 index, expecting to receive an L1
seed key."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA512,
guid=misc.GUID("89f70521-9d66-441f-c314-1b462f9b1052"),
data=bytes.fromhex(
"a6ef87dbbbf86b6bbe55750b941f13ca99efe5185e2e2bded5b838d8a0e77647"
"0537e68cae45a7a0f4b1d6c9bf5494c3f879e172e326557cdbb6a56e8799a722"
),
)
current_gkid = Gkid(255, 24, 31)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(255, 2, 5),
current_time=current_gkid.start_nt_time(),
)
# Expect to get an L1 seed key.
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA512, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"bd538a073490f3cf9451c933025de9b22c97eaddaffa94b379e2b919a4bed147"
"5bc67f6a9175b139c69204c57d4300a0141ffe34d12ced84614593b1aa13af1c"
),
key.l1_key,
)
self.assertIsNone(key.l2_key)
def test_current_l0_idx_l2_seed_key(self):
"""Request a key with the current L0 index, expecting to receive an L2
seed key."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA512,
guid=misc.GUID("1a3d6c30-aa81-cb7f-d3fe-80775d135dfe"),
data=bytes.fromhex(
"dfd95be3153a0805c65694e7d284aace5ab0aa493350025eb8dbc6df0b4e9256"
"fb4cbfbe6237ce3732694e2608760076b67082d39abd3c0fedba1b8873645064"
),
)
current_gkid = Gkid(321, 0, 12)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(321, 0, 1),
current_time=current_gkid.start_nt_time(),
)
# Expect to get an L2 seed key.
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA512, key.hash_algorithm)
self.assertIsNone(key.l1_key)
self.assertEqual(
bytes.fromhex(
"bbbd9376cd16c247ed40f5912d1908218c08f0915bae02fe02cbfb3753bde406"
"f9c553acd95143cf63906a0440e3cf237d2335ae4e4b9cd2d946a71351ebcb7b"
),
key.l2_key,
)
def test_current_l0_idx_both_seed_keys(self):
"""Request a key with the current L0 index, expecting to receive L1 and
L2 seed keys."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA512,
guid=misc.GUID("09de0b38-c743-7abf-44ea-7a3c3e404314"),
data=bytes.fromhex(
"d5912d0eb3bd60e1371b1e525dd83be7fc5baf77018b0dba6bd948b7a98ebe5a"
"f37674332506a46c52c108a62f2a3e89251ad1bde6d539004679c0658853bb68"
),
)
current_gkid = Gkid(123, 21, 0)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(123, 2, 1),
current_time=current_gkid.start_nt_time(),
)
# Expect to get both L1 and L2 seed keys.
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA512, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"b1f7c5896e7dc791d9c0aaf8ca7dbab8c172a4f8b873db488a3c4cbd0f559b11"
"52ffba39d4aff2d9e8aada90b27a3c94a5af996f4b8f584a4f37ccab4d505d3d"
),
key.l1_key,
)
self.assertEqual(
bytes.fromhex(
"133c9bbd20d9227aeb38dfcd3be6bcbfc5983ba37202088ff5c8a70511214506"
"a69c195a8807cd844bcb955e9569c8e4d197759f28577cc126d15f16a7da4ee0"
),
key.l2_key,
)
def test_previous_l0_idx(self):
"""Request a key with a previous L0 index."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA512,
guid=misc.GUID("27136e8f-e093-6fe3-e57f-1d915b102e1c"),
data=bytes.fromhex(
"b41118c60a19cafa5ecf858d1a2a2216527b2daedf386e9d599e42a46add6c7d"
"c93868619761c880ff3674a77c6e5fbf3434d130a9727bb2cd2a2557bdcfc752"
),
)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(100, 20, 30),
current_time=Gkid(101, 2, 3).start_nt_time(),
)
# Expect to get an L1 seed key.
self.assertEqual(Gkid(100, 31, 31), key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA512, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"935cbdc06198eb28fa44b8d8278f51072c4613999236585041ede8e72d02fe95"
"e3454f046382cbc0a700779b79474dd7e080509d76302d2937407e96e3d3d022"
),
key.l1_key,
)
self.assertIsNone(key.l2_key)
def test_sha1(self):
"""Request a key derived with SHA1."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA1,
guid=misc.GUID("970abad6-fe55-073a-caf1-b801d3f26bd3"),
data=bytes.fromhex(
"3bed03bf0fb7d4013149154f24ca2d59b98db6d588cb1f54eca083855e25eb28"
"d3562a01adc78c4b70e0b72a59515863e7732b853fba02dd7646e63108441211"
),
)
current_gkid = Gkid(1, 2, 3)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(1, 1, 1),
current_time=current_gkid.start_nt_time(),
)
# Expect to get both L1 and L2 seed keys.
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA1, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"576cb68f2e52eb739f817b488c3590d86f1c2c365f3fc9201d9c7fee7494853d"
"58746ee13e48f18aa6fa69f7157de3d07de34e13836792b7c088ffb6914a89c2"
),
key.l1_key,
)
self.assertEqual(
bytes.fromhex(
"3ffb825adaf116b6533207d568a30ed3d3f21c68840941c9456684f9afa11b05"
"6e0c59391b4d88c495d984c3d680029cc5c594630f34179119c1c5acaae5e90e"
),
key.l2_key,
)
def test_sha256(self):
"""Request a key derived with SHA256."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA256,
guid=misc.GUID("45e26207-ed33-dcd5-925a-518a0deef69e"),
data=bytes.fromhex(
"28b5b6503d3c1d24814de781bb7bfce3ef69eed1ce4809372bee2c506270c5f0"
"b5c6df597472623f256c86daa0991e8a11a1705f21b2cfdc0bb9db4ba23246a2"
),
)
current_gkid = Gkid(222, 22, 22)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(222, 11, 0),
current_time=current_gkid.start_nt_time(),
)
# Expect to get both L1 and L2 seed keys.
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA256, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"57aced6e75f83f3af4f879b38b60f090b42e4bfa022fae3e6fd94280b469b0ec"
"15d8b853a870b5fbdf28708cce19273b74a573acbe0deda8ef515db4691e2dcb"
),
key.l1_key,
)
self.assertEqual(
bytes.fromhex(
"752a0879ae2424c0504c7493599f13e588e1bbdc252f83325ad5b1fb91c24c89"
"01d440f3ff9ffba59fcd65bb975732d9f383dd50b898174bb9393e383d25d540"
),
key.l2_key,
)
def test_sha384(self):
"""Request a key derived with SHA384."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA384,
guid=misc.GUID("66e6d9f7-4924-f3fc-fe34-605634d42ebd"),
data=bytes.fromhex(
"23e5ba86cbd88f7b432ee66dbb03bf4eebf401cbfc3df735d4d728b503c87f84"
"3207c6f6153f190dfe85a86cb8d8b74df13b25305981be8d7e29c96ee54c9630"
),
)
current_gkid = Gkid(287, 28, 27)
key = self.get_key(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
Gkid(287, 8, 7),
current_time=current_gkid.start_nt_time(),
)
# Expect to get both L1 and L2 seed keys.
self.assertEqual(current_gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA384, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"fabadd7a9a63df57d6832df7a735aebb6e181888b2eaf301a2e4ff9a70246d38"
"ab1d2416325bf3eb726a0267bab4bd950c7291f05ea5f17197ece56992af3eb8"
),
key.l1_key,
)
self.assertEqual(
bytes.fromhex(
"ec1c65634b5694818e1d341da9996db8f2a1ef6a2c776a7126a7ebd18b37a073"
"afdac44c41b167b14e4b872d485bbb6d7b70964215d0e84a2ff142a9d943f205"
),
key.l2_key,
)
def test_derive_key_exact(self):
"""Derive a key at an exact GKID."""
root_key_id = self.new_root_key(
use_start_time=ROOT_KEY_START_TIME,
hash_algorithm=Algorithm.SHA512,
guid=misc.GUID("d95fb06f-5a9c-1829-e20d-27f3f2ecfbeb"),
data=bytes.fromhex(
"489f3531c537774d432d6b97e3bc1f43d2e8c6dc17eb0e4fd9a0870d2f1ebf92"
"e2496668a8b5bd11aea2d32d0aab716f48fe569f5c9b50ff3f9bf5deaea572fb"
),
)
gkid = Gkid(333, 22, 11)
key = self.get_key_exact(
self.get_samdb(),
self.gmsa_sd,
root_key_id,
gkid,
current_time=self.current_nt_time(self.get_samdb()),
)
self.assertEqual(gkid, key.gkid)
self.assertEqual(root_key_id, key.root_key_id)
self.assertEqual(Algorithm.SHA512, key.hash_algorithm)
self.assertEqual(
bytes.fromhex(
"d6ab3b14f4f4c8908aa3464011b39f10a8bfadb9974af90f7d9a9fede2fdc6e5"
"f68a628ec00f9994a3abd8a52ae9e2db4f68e83648311e9d7765f2535515b5e2"
),
key.key,
)
if __name__ == "__main__":
import unittest
unittest.main()
|